1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - CTRDG - include
3   File:     ctrdg_task.h
4 
5   Copyright 2003-2008 Nintendo. All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law. They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Date:: 2008-09-18#$
14   $Rev: 8573 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 
18 #ifndef	NITRO_CTRDG_TASK_H_
19 #define	NITRO_CTRDG_TASK_H_
20 
21 #include <nitro.h>
22 
23 /* Task thread priority level */
24 #define CTRDG_TASK_PRIORITY_DEFAULT 20 // Enables the creation of multiple threads between the task and main threads, with lower priorities than that of the main thread.
25 
26 /* Task thread's stack size */
27 #define CTRDG_TASK_STACK_SIZE 1024
28 
29 struct CTRDGTaskInfo_tag;
30 
31 // Argument declares a CTRDGTaskInfo_tag function pointer.
32 typedef u32 (*CTRDG_TASK_FUNC) (struct CTRDGTaskInfo_tag *);
33 
34 /*
35  * The task information structure to request to the task thread.
36  */
37 typedef struct CTRDGTaskInfo_tag
38 {
39     // TRUE, in the interval between leaving the standby loop which waits for a task to come for the waiting task list in the thread, and the task's completion.
40     CTRDG_TASK_FUNC task;              /* Task function */
41     CTRDG_TASK_FUNC callback;          /* Callback */
42     u32     result;                    /* The task function's return value. */
43     u8     *data;                      /* The data to be written; only program commands can be used. */
44     u8     *adr;                       /* Address of the data to be read/written. */
45     u32     offset;                    /* The offset, in bytes, within the sector */
46     u32     size;                      /* Size */
47     u8     *dst;                       /* Address of the work region where the read data is stored */
48     u16     sec_num;                   /* Sector number */
49     u8      busy;                      /* If now processing */
50     u8      param[1];                  /* User-defined argument and return-value */
51 }
52 CTRDGTaskInfo;
53 
54 typedef struct
55 {
56     OSThread th[1];                    /* Thread context */
57     CTRDGTaskInfo *volatile list;      /* Waiting task list */
58     CTRDGTaskInfo end_task;            /* Task structure for end-command */
59 }
60 CTRDGiTaskWork;
61 
62 /*---------------------------------------------------------------------------*
63   Name:         CTRDGi_InitTaskThread
64 
65   Description:  Starts a task thread.
66 
67   Arguments:    p_work:     Internal work buffer.
68                            Used internally until CTRDGi_EndTaskThread() completes.
69 
70   Returns:      None.
71  *---------------------------------------------------------------------------*/
72 void    CTRDGi_InitTaskThread(void *p_work);
73 
74 /*---------------------------------------------------------------------------*
75   Name:         CTRDGi_IsTaskAvailable
76 
77   Description:  Checks if a task thread is currently available.
78 
79   Arguments:    None.
80 
81   Returns:      TRUE if currently available, and FALSE otherwise.
82  *---------------------------------------------------------------------------*/
83 BOOL    CTRDGi_IsTaskAvailable(void);
84 
85 /*---------------------------------------------------------------------------*
86   Name:         CTRDGi_InitTaskInfo
87 
88   Description:  Initializes a task information structure.
89                 Must be called once before using.
90 
91   Arguments:    pt:         Uninitialized task information structure
92 
93   Returns:      None.
94  *---------------------------------------------------------------------------*/
95 void    CTRDGi_InitTaskInfo(CTRDGTaskInfo * pt);
96 
97 /*---------------------------------------------------------------------------*
98   Name:         CTRDGi_IsTaskBusy
99 
100   Description:  Checks if task information is currently being used.
101 
102   Arguments:    pt:         Task information.
103 
104   Returns:      TRUE if currently being used, and FALSE otherwise.
105  *---------------------------------------------------------------------------*/
106 BOOL    CTRDGi_IsTaskBusy(volatile const CTRDGTaskInfo * pt);
107 
108 /*---------------------------------------------------------------------------*
109   Name:         CTRDGi_SetTask
110 
111   Description:  Adds a task to an internal thread.
112 
113   Arguments:    pt:         Currently unused task information
114                 task:       Task function
115                 callback:   Callback when task completes (ignored if NULL)
116 
117   Returns:      None.
118  *---------------------------------------------------------------------------*/
119 void    CTRDGi_SetTask(CTRDGTaskInfo * pt, CTRDG_TASK_FUNC task, CTRDG_TASK_FUNC callback);
120 
121 /*---------------------------------------------------------------------------*
122   Name:         CTRDGi_EndTaskThread
123 
124   Description:  Ends the task thread.
125 
126   Arguments:    callback:   Callback when task thread ends (ignored if NULL)
127                            This callback is called in the state just before the task thread ends, while interrupts are still disabled.
128 
129   Returns:      None.
130  *---------------------------------------------------------------------------*/
131 void    CTRDGi_EndTaskThread(CTRDG_TASK_FUNC callback);
132 
133 /*---------------------------------------------------------------------------*
134   Name:         CTRDG_SetTaskThreadPriority
135 
136   Description:  Changes the task thread's priority.
137 
138   Arguments:    priority:   The task thread's priority
139 
140   Returns:      None.
141  *---------------------------------------------------------------------------*/
142 void    CTRDG_SetTaskThreadPriority(u32 priority);
143 
144 #endif /* NITRO_CTRDG_TASK_H_ */
145