rtos.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. //****************************************************************************
  2. //
  3. // Copyright (C) 2010 ShenZhen ExceedSpace
  4. //
  5. // Author ZhuoYongHong
  6. //
  7. // File name: rtos.h
  8. // constant£¬macro, data structure£¬function protocol definition of lowlevel rtos interface
  9. //
  10. // Revision history
  11. //
  12. // 2011.09.08 ZhuoYongHong Initial version
  13. //
  14. //****************************************************************************
  15. #ifndef _XM_RTOS_H_
  16. #define _XM_RTOS_H_
  17. #if defined (__cplusplus)
  18. extern "C"{
  19. #endif
  20. #include <string.h> // required for memset() etc.
  21. #include <intrinsics.h>
  22. #include "FreeRTOS.h"
  23. #include "semphr.h"
  24. #include "event_groups.h"
  25. #include "task.h"
  26. #ifndef OS_I8
  27. #define OS_I8 signed char
  28. #endif
  29. #ifndef OS_U8
  30. #define OS_U8 unsigned char
  31. #endif
  32. #ifndef OS_I16
  33. #define OS_I16 signed short
  34. #endif
  35. #ifndef OS_U16
  36. #define OS_U16 unsigned short
  37. #endif
  38. #ifndef OS_I32
  39. #define OS_I32 long
  40. #endif
  41. #ifndef OS_U32
  42. #define OS_U32 unsigned OS_I32
  43. #endif
  44. #ifndef OS_INT
  45. #define OS_INT int
  46. #endif
  47. #ifndef OS_UINT
  48. #define OS_UINT unsigned OS_INT
  49. #endif
  50. #ifndef OS_TIME
  51. #define OS_TIME int
  52. #endif
  53. #ifndef OS_STAT
  54. #define OS_STAT OS_U8
  55. #endif
  56. #ifndef OS_PRIO
  57. #define OS_PRIO OS_U8
  58. #endif
  59. #ifndef OS_BOOL
  60. #define OS_BOOL OS_U8
  61. #endif
  62. #define OS_ERR_ISR_INDEX (100 )
  63. #define OS_ERR_ISR_VECTOR (101 )
  64. #define OS_ERR_ISR_PRIO (102 )
  65. #define OS_ERR_STACK (120 )
  66. #define OS_ERR_CSEMA_OVERFLOW (121 )
  67. #define OS_ERR_INV_TASK (128 )
  68. #define OS_ERR_INV_TIMER (129 )
  69. #define OS_ERR_INV_MAILBOX (130 )
  70. #define OS_ERR_INV_CSEMA (132 )
  71. #define OS_ERR_INV_RSEMA (133 )
  72. #define OS_ERR_MAILBOX_NOT1 (135 )
  73. #define OS_ERR_MAILBOX_DELETE (136 )
  74. #define OS_ERR_CSEMA_DELETE (137 )
  75. #define OS_ERR_RSEMA_DELETE (138 )
  76. #define OS_ERR_MAILBOX_NOT_IN_LIST (140 )
  77. #define OS_ERR_TASKLIST_CORRUPT (142 )
  78. #define OS_ERR_UNUSE_BEFORE_USE (150 )
  79. #define OS_ERR_LEAVEREGION_BEFORE_ENTERREGION (151 )
  80. #define OS_ERR_LEAVEINT (152 )
  81. #define OS_ERR_DICNT (153 )
  82. #define OS_ERR_INTERRUPT_DISABLED (154 )
  83. #define OS_ERR_TASK_ENDS_WITHOUT_TERMINATE (155 )
  84. #define OS_ERR_RESOURCE_OWNER (156 )
  85. #define OS_ERR_ILLEGAL_IN_ISR (160 )
  86. #define OS_ERR_ILLEGAL_IN_TIMER (161 )
  87. #define OS_ERR_ILLEGAL_OUT_ISR (162 )
  88. #define OS_ERR_NOT_IN_ISR (163 ) //*** OS_EnterInterrupt() has been called, but CPU is not in ISR state
  89. #define OS_ERR_IN_ISR (164 ) //*** OS_EnterInterrupt() has not been called, but CPU is in ISR state
  90. #define OS_ERR_INIT_NOT_CALLED (165 ) //*** OS_InitKern() was not called
  91. #define OS_ERR_2USE_TASK (170 )
  92. #define OS_ERR_2USE_TIMER (171 )
  93. #define OS_ERR_2USE_MAILBOX (172 )
  94. #define OS_ERR_2USE_BSEMA (173 )
  95. #define OS_ERR_2USE_CSEMA (174 )
  96. #define OS_ERR_2USE_RSEMA (175 )
  97. #define OS_ERR_2USE_MEMF (176 )
  98. #define OS_ERR_NESTED_RX_INT (180 )
  99. #define OS_ERR_MEMF_INV (190 )
  100. #define OS_ERR_MEMF_INV_PTR (191 )
  101. #define OS_ERR_MEMF_PTR_FREE (192 )
  102. #define OS_ERR_MEMF_RELEASE (193 )
  103. #define OS_ERR_POOLADDR (194 )
  104. #define OS_ERR_BLOCKSIZE (195 )
  105. #define OS_ERR_SUSPEND_TOO_OFTEN (200 )
  106. #define OS_ERR_RESUME_BEFORE_SUSPEND (201 )
  107. #define OS_ERR_TASK_PRIORITY (202 )
  108. #define OS_ERR_EVENTOBJ_INV (210 )
  109. #define OS_ERR_2USE_EVENTOBJ (211 )
  110. #define OS_ERR_EVENT_DELETE (212 )
  111. #define OS_ERR_NON_ALIGNED_INVALIDATE (230 ) // Cache invalidation needs to be cache line aligned
  112. typedef union {
  113. int Dummy; // Make sure a full integer (32 bit on 32 bit CPUs) is used.
  114. struct {
  115. OS_U8 Region;
  116. OS_U8 DI;
  117. } Cnt;
  118. } OS_COUNTERS;
  119. typedef struct OS_GLOBAL {
  120. OS_COUNTERS Counters;
  121. } OS_GLOBAL;
  122. #define OS_Counters OS_Global.Counters
  123. extern OS_GLOBAL OS_Global;
  124. #if (__CPU_MODE__== 1) // if THUMB mode
  125. #define OS_INTERWORK __interwork
  126. #define OS_DI() OS_DisableInt()
  127. #define OS_EI() OS_EnableInt()
  128. #else
  129. #define OS_INTERWORK
  130. #define OS_DI() __set_CPSR(__get_CPSR() | (1uL << 7)) // Optimization for ARM mode
  131. #define OS_EI() __set_CPSR(__get_CPSR() & ~(1uL << 7))
  132. #endif
  133. #if OS_DEBUG
  134. #define OS_ASSERT(Exp, ErrCode) { if (!(Exp)) OS_Error(ErrCode); }
  135. #else
  136. #define OS_ASSERT(Exp, ErrCode)
  137. #endif
  138. #if 0
  139. #define OS_DICnt OS_Counters.Cnt.DI
  140. #define OS_ASSERT_DICnt() OS_ASSERT(((OS_DICnt & 0xf0) == 0), OS_ERR_DICNT)
  141. #define OS_IncDI() { OS_ASSERT_DICnt(); OS_DI(); OS_DICnt++; }
  142. #define OS_DecRI() { OS_ASSERT_DICnt(); if (--OS_DICnt==0) OS_EI(); }
  143. #define OS_RESTORE_I() { OS_ASSERT_DICnt(); if (OS_DICnt==0) OS_EI(); }
  144. #else
  145. #define OS_DICnt OS_Counters.Cnt.DI
  146. #define OS_ASSERT_DICnt() OS_ASSERT(((OS_DICnt & 0xf0) == 0), OS_ERR_DICNT)
  147. #define OS_IncDI() { OS_ASSERT_DICnt(); OS_DI(); OS_DICnt++; }
  148. #define OS_DecRI() { OS_ASSERT_DICnt(); if (--OS_DICnt==0) OS_EI(); }
  149. #define OS_RESTORE_I() { OS_ASSERT_DICnt(); if (OS_DICnt==0) OS_EI(); }
  150. void OS_HandleTickEx ( void );
  151. #define OS_HandleTickEx FreeRTOS_Tick_Handler
  152. #endif
  153. #define RTOS_ID_TASK 0x5441534B
  154. typedef struct OS_TASK OS_TASK;
  155. struct OS_TASK
  156. {
  157. StaticTask_t task;
  158. unsigned int dummy1[4];
  159. unsigned int id;
  160. TaskHandle_t handle;
  161. unsigned int dummy2[2];
  162. } ;
  163. typedef void voidRoutine(void);
  164. typedef void OS_TIMERROUTINE(void);
  165. typedef void OS_TIMER_EX_ROUTINE(void *);
  166. void OS_TICK_Config ( unsigned FractPerInt, unsigned FractPerTick );
  167. #ifndef OS_STACKPTR
  168. #define OS_STACKPTR
  169. #endif
  170. #ifndef OS_ROM_DATA
  171. #define OS_ROM_DATA
  172. #endif
  173. #define CTPARA_TIMESLICE ,2
  174. #define OS_CREATE_TASK_PARA_TS ,OS_UINT TimeSlice
  175. #define OS_CREATETASK(pTask, Name, Hook, Priority, pStack) \
  176. OS_CreateTask (pTask, \
  177. Name, \
  178. Priority, \
  179. Hook, \
  180. (void OS_STACKPTR*)pStack, \
  181. sizeof(pStack) \
  182. CTPARA_TIMESLICE \
  183. )
  184. #define OS_CREATETASK_EX(pTask, Name, Hook, Priority, pStack, pContext) \
  185. OS_CreateTaskEx (pTask, \
  186. Name, \
  187. Priority, \
  188. Hook, \
  189. (void OS_STACKPTR*)pStack, \
  190. sizeof(pStack) \
  191. CTPARA_TIMESLICE, \
  192. pContext \
  193. )
  194. void OS_CreateTask ( OS_TASK * pTask,
  195. OS_ROM_DATA const char* Name,
  196. OS_U8 Priority,
  197. void (*pRoutine)(void),
  198. void OS_STACKPTR *pStack,
  199. OS_UINT StackSize
  200. OS_CREATE_TASK_PARA_TS
  201. );
  202. #define OS_CREATE_TASK_PARA_NAME OS_ROM_DATA const char* Name,
  203. void OS_CreateTaskEx ( OS_TASK * pTask,
  204. OS_CREATE_TASK_PARA_NAME
  205. OS_U8 Priority,
  206. void (*pRoutine)(void *),
  207. void OS_STACKPTR *pStack,
  208. OS_UINT StackSize
  209. OS_CREATE_TASK_PARA_TS,
  210. void * pContext
  211. );
  212. // Ends (terminates) a task.
  213. void OS_Terminate (OS_TASK* pTask);
  214. OS_TASK* OS_GetpCurrentTask (void);
  215. // Suspends the calling task until a specified time.
  216. // The calling task will be put into the TS_DELAY state until the time specified.
  217. // The OS_DelayUntil() function delays until the value of the time-variable OS_Time has reached a certain value. It
  218. // is very useful if you have to avoid accumulating delays.
  219. void OS_DelayUntil (int t);
  220. // Waits for the specified events for a given time, and clears the event memory after an event occurs.
  221. char OS_WaitEventTimed (char EventMask, OS_TIME TimeOut);
  222. // Waits for one of the events specified in the bitmask and clears the event memory after an event occurs.
  223. // If none of the specified events are signaled, the task is suspended. The first of the specified events will wake the task.
  224. // These events are signaled by another task, a S/W timer or an interrupt handler. Any bit in the 8-bit event mask may
  225. // enable the corresponding event.
  226. char OS_WaitEvent (char EventMask);
  227. // Waits for one of the events specified by the bitmask and clears only that event after it occurs.
  228. // Return value All masked events that have actually occurred.
  229. // If none of the specified events are signaled, the task is suspended. The first of the specified events will wake the task.
  230. // These events are signaled by another task, a S/W timer, or an interrupt handler. Any bit in the 8-bit event mask may
  231. // enable the corresponding event. All unmasked events remain unchanged.
  232. char OS_WaitSingleEvent (char EventMask);
  233. // Signals event(s) to a specified task.
  234. // If the specified task is waiting for one of these events, it will be put in the READY state and activated according to the rules of the scheduler.
  235. void OS_SignalEvent (char Event, OS_TASK* pTask);
  236. // Returns a list of events that have occurred for a specified task.
  237. // The event mask of the events that have actually occurred.
  238. // By calling this function, the actual events remain signaled. The event memory is not cleared. This is one way for a task
  239. // to find out which events have been signaled. The task is not suspended if no events are available.
  240. char OS_GetEventsOccurred (OS_TASK* pTask);
  241. // Returns the actual state of events and then clears the events of a specified task.
  242. // pTask The task who's event mask is to be returned,
  243. // NULL means current task.
  244. //
  245. // Return value The events that were actually signaled before clearing.
  246. char OS_ClearEvents (OS_TASK* pTask);
  247. #define RTOS_ID_RSEMA 0x5253454D
  248. typedef struct OS_RSEMA OS_RSEMA;
  249. struct OS_RSEMA
  250. {
  251. StaticSemaphore_t sema;
  252. unsigned int id;
  253. SemaphoreHandle_t handle;
  254. } ;
  255. //typedef StaticSemaphore_t OS_RSEMA;
  256. int OS_Use (OS_RSEMA* pRSema);
  257. void OS_Unuse (OS_RSEMA* pRSema);
  258. char OS_Request (OS_RSEMA* pRSema);
  259. void OS_CREATERSEMA (OS_RSEMA* pRSema);
  260. void OS_DeleteRSema (OS_RSEMA* pRSema);
  261. #define RTOS_ID_EVENT 0x45564E54 // "EVNT"
  262. typedef struct OS_EVENT OS_EVENT;
  263. struct OS_EVENT
  264. {
  265. StaticEventGroup_t event;
  266. unsigned int id;
  267. EventGroupHandle_t handle;
  268. } ;
  269. void OS_EVENT_Create (OS_EVENT* pEvent);
  270. void OS_EVENT_Delete (OS_EVENT* pEvent);
  271. void OS_EVENT_Set (OS_EVENT* pEvent);
  272. void OS_EVENT_Reset (OS_EVENT* pEvent);
  273. void OS_EVENT_Wait (OS_EVENT* pEvent);
  274. char OS_EVENT_WaitTimed (OS_EVENT* pEvent, OS_TIME Timeout);
  275. #define RTOS_ID_CSEMA 0x4353454D // "CSEM"
  276. typedef struct OS_CSEMA OS_CSEMA;
  277. struct OS_CSEMA
  278. {
  279. StaticSemaphore_t sema;
  280. unsigned int id;
  281. SemaphoreHandle_t handle;
  282. } ;
  283. void OS_CreateCSema (OS_CSEMA* pCSema, OS_UINT InitValue);
  284. void OS_CREATECSEMA (OS_CSEMA* pCSema);
  285. #define OS_CREATECSEMA(pCSema) OS_CreateCSema(pCSema,0)
  286. // Increments the counter of a semaphore.
  287. void OS_SignalCSema (OS_CSEMA * pCSema);
  288. // Decrements the counter of a semaphore.
  289. void OS_WaitCSema (OS_CSEMA* pCSema);
  290. void OS_DeleteCSema (OS_CSEMA* pCSema);
  291. #define RTOS_ID_MAILBOX 0x4D41494C // "MAIL"
  292. typedef struct OS_MAILBOX OS_MAILBOX;
  293. struct OS_MAILBOX
  294. {
  295. StaticMessageBuffer_t message;
  296. unsigned int id;
  297. void * handle;
  298. char min_msg[4];
  299. unsigned int size; // message size
  300. } ;
  301. void OS_CREATEMB (OS_MAILBOX* pMB, unsigned char sizeofMsg, unsigned int maxnofMsg, void* pMsg);
  302. void OS_DeleteMB (OS_MAILBOX* pMB);
  303. void OS_ClearMB (OS_MAILBOX* pMB);
  304. void OS_PutMail (OS_MAILBOX* pMB,void* pMail);
  305. void OS_PutMail1 (OS_MAILBOX* pMB, const char* pMail);
  306. char OS_GetMailTimed (OS_MAILBOX* pMB, void* pDest, OS_TIME Timeout);
  307. void OS_GetMail (OS_MAILBOX* pMB, void* pDest);
  308. void OS_GetMail1 (OS_MAILBOX* pMB,char* pDest);
  309. void OS_EnterRegion(void);
  310. void OS_LeaveRegion(void);
  311. #define OS_INTERWORK
  312. #define RTOS_ID_TIMER 0x54494D52 // "TIMR"
  313. typedef struct OS_TIMER OS_TIMER;
  314. struct OS_TIMER
  315. {
  316. StaticTimer_t timer;
  317. unsigned int id;
  318. TimerHandle_t handle;
  319. } ;
  320. typedef struct {
  321. OS_TIMER Timer;
  322. OS_TIMER_EX_ROUTINE * pfUser;
  323. void * pData;
  324. } OS_TIMER_EX;
  325. // Creates a software timer (but does not start it).
  326. void OS_CreateTimer (OS_TIMER* pTimer, OS_TIMERROUTINE* Callback, OS_TIME Timeout);
  327. // Starts a specified timer.
  328. void OS_StartTimer (OS_TIMER* pTimer);
  329. // Stops and deletes a specified timer.
  330. void OS_DeleteTimer (OS_TIMER* pTimer);
  331. // Restarts a specified timer with its initial time value.
  332. void OS_RetriggerTimer (OS_TIMER* pTimer);
  333. #define OS_CREATETIMER(pTimer,c,d) \
  334. OS_CreateTimer(pTimer,c,d); \
  335. OS_StartTimer(pTimer);
  336. void* OS_malloc(unsigned int n);
  337. void OS_free (void* pMemBlock);
  338. typedef void OS_ISR_HANDLER(void);
  339. OS_ISR_HANDLER* OS_ARM_InstallISRHandler (int ISRIndex, OS_ISR_HANDLER* pISRHandler);
  340. void OS_Delay (int ms); // Suspends the calling task for a specified period of time
  341. int OS_GetTime (void);
  342. unsigned int OS_GetTime32 (void);
  343. void XM_Lock (void);
  344. void XM_Unlock (void);
  345. void OS_InitKern(void);
  346. void OS_INIT_SYS_LOCKS(void);
  347. void OS_InitHW(void);
  348. void OS_Start (void);
  349. unsigned char OS_GetPriority (OS_TASK* pTask);
  350. void OS_SetPriority (OS_TASK* pTask, unsigned char Priority);
  351. void OS_Error(int ErrCode);
  352. void OS_ARM_EnableISRSource(int SourceIndex);
  353. void OS_ARM_DisableISRSource(int SourceIndex);
  354. /*
  355. *********************************************************************************************************
  356. * TIMER OPTIONS (see OSTmrStart() and OSTmrStop())
  357. *********************************************************************************************************
  358. */
  359. #define OS_TMR_OPT_NONE 0u /* No option selected */
  360. #define OS_TMR_OPT_ONE_SHOT 1u /* Timer will not automatically restart when it expires */
  361. #define OS_TMR_OPT_PERIODIC 2u /* Timer will automatically restart when it expires */
  362. #define OS_TMR_OPT_CALLBACK 3u /* OSTmrStop() option to call 'callback' w/ timer arg. */
  363. #define OS_TMR_OPT_CALLBACK_ARG 4u /* OSTmrStop() option to call 'callback' w/ new arg. */
  364. /*
  365. *********************************************************************************************************
  366. * TIMER STATES
  367. *********************************************************************************************************
  368. */
  369. #define OS_TMR_STATE_UNUSED 0u
  370. #define OS_TMR_STATE_STOPPED 1u
  371. #define OS_TMR_STATE_COMPLETED 2u
  372. #define OS_TMR_STATE_RUNNING 3u
  373. #if defined (__cplusplus)
  374. }
  375. #endif /* end of __cplusplus */
  376. #endif // _XM_RTOS_H_