os_cpu_c.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. *********************************************************************************************************
  3. * uC/OS-II
  4. * The Real-Time Kernel
  5. *
  6. *
  7. * (c) Copyright 2006, Micrium, Weston, FL
  8. * All Rights Reserved
  9. *
  10. * ARM Cortex-M3 Port
  11. *
  12. * File : OS_CPU_C.C
  13. * Version : V2.89
  14. * By : Jean J. Labrosse
  15. * Brian Nagel
  16. *
  17. * For : ARMv7M Cortex-M3
  18. * Mode : Thumb2
  19. * Toolchain : RealView Development Suite
  20. * RealView Microcontroller Development Kit (MDK)
  21. * ARM Developer Suite (ADS)
  22. * Keil uVision
  23. *********************************************************************************************************
  24. */
  25. #define OS_CPU_GLOBALS
  26. #include <ucos_ii.h>
  27. /*
  28. *********************************************************************************************************
  29. * LOCAL VARIABLES
  30. *********************************************************************************************************
  31. */
  32. #if OS_TMR_EN > 0u
  33. static INT16U OSTmrCtr;
  34. #endif
  35. /*
  36. *********************************************************************************************************
  37. * SYS TICK DEFINES
  38. *********************************************************************************************************
  39. */
  40. #define OS_CPU_CM3_NVIC_ST_CTRL (*((volatile INT32U *)0xE000E010uL)) /* SysTick Ctrl & Status Reg. */
  41. #define OS_CPU_CM3_NVIC_ST_RELOAD (*((volatile INT32U *)0xE000E014uL)) /* SysTick Reload Value Reg. */
  42. #define OS_CPU_CM3_NVIC_ST_CURRENT (*((volatile INT32U *)0xE000E018uL)) /* SysTick Current Value Reg. */
  43. #define OS_CPU_CM3_NVIC_ST_CAL (*((volatile INT32U *)0xE000E01CuL)) /* SysTick Cal Value Reg. */
  44. #define OS_CPU_CM3_NVIC_PRIO_ST (*((volatile INT8U *)0xE000ED23uL)) /* SysTick Handler Prio Reg. */
  45. #define OS_CPU_CM3_NVIC_ST_CTRL_COUNT 0x00010000uL /* Count flag. */
  46. #define OS_CPU_CM3_NVIC_ST_CTRL_CLK_SRC 0x00000004uL /* Clock Source. */
  47. #define OS_CPU_CM3_NVIC_ST_CTRL_INTEN 0x00000002uL /* Interrupt enable. */
  48. #define OS_CPU_CM3_NVIC_ST_CTRL_ENABLE 0x00000001uL /* Counter mode. */
  49. #define OS_CPU_CM3_NVIC_PRIO_MIN 0xFFu /* Min handler prio. */
  50. /*
  51. *********************************************************************************************************
  52. * OS INITIALIZATION HOOK
  53. * (BEGINNING)
  54. *
  55. * Description: This function is called by OSInit() at the beginning of OSInit().
  56. *
  57. * Arguments : none
  58. *
  59. * Note(s) : 1) Interrupts should be disabled during this call.
  60. *********************************************************************************************************
  61. */
  62. #if OS_CPU_HOOKS_EN > 0u
  63. void OSInitHookBegin (void)
  64. {
  65. INT32U size;
  66. OS_STK *pstk;
  67. /* Clear exception stack for stack checking.*/
  68. pstk = &OS_CPU_ExceptStk[0];
  69. size = OS_CPU_EXCEPT_STK_SIZE;
  70. while (size > 0u) {
  71. size--;
  72. *pstk++ = (OS_STK)0;
  73. }
  74. OS_CPU_ExceptStkBase = &OS_CPU_ExceptStk[OS_CPU_EXCEPT_STK_SIZE - 1u];
  75. #if OS_TMR_EN > 0u
  76. OSTmrCtr = 0u;
  77. #endif
  78. }
  79. #endif
  80. /*
  81. *********************************************************************************************************
  82. * OS INITIALIZATION HOOK
  83. * (END)
  84. *
  85. * Description: This function is called by OSInit() at the end of OSInit().
  86. *
  87. * Arguments : none
  88. *
  89. * Note(s) : 1) Interrupts should be disabled during this call.
  90. *********************************************************************************************************
  91. */
  92. #if OS_CPU_HOOKS_EN > 0u
  93. void OSInitHookEnd (void)
  94. {
  95. }
  96. #endif
  97. /*
  98. *********************************************************************************************************
  99. * TASK CREATION HOOK
  100. *
  101. * Description: This function is called when a task is created.
  102. *
  103. * Arguments : ptcb is a pointer to the task control block of the task being created.
  104. *
  105. * Note(s) : 1) Interrupts are disabled during this call.
  106. *********************************************************************************************************
  107. */
  108. #if OS_CPU_HOOKS_EN > 0u
  109. void OSTaskCreateHook (OS_TCB *ptcb)
  110. {
  111. #if OS_APP_HOOKS_EN > 0u
  112. App_TaskCreateHook(ptcb);
  113. #else
  114. (void)ptcb; /* Prevent compiler warning */
  115. #endif
  116. }
  117. #endif
  118. /*
  119. *********************************************************************************************************
  120. * TASK DELETION HOOK
  121. *
  122. * Description: This function is called when a task is deleted.
  123. *
  124. * Arguments : ptcb is a pointer to the task control block of the task being deleted.
  125. *
  126. * Note(s) : 1) Interrupts are disabled during this call.
  127. *********************************************************************************************************
  128. */
  129. #if OS_CPU_HOOKS_EN > 0u
  130. void OSTaskDelHook (OS_TCB *ptcb)
  131. {
  132. #if OS_APP_HOOKS_EN > 0u
  133. App_TaskDelHook(ptcb);
  134. #else
  135. (void)ptcb; /* Prevent compiler warning */
  136. #endif
  137. }
  138. #endif
  139. /*
  140. *********************************************************************************************************
  141. * IDLE TASK HOOK
  142. *
  143. * Description: This function is called by the idle task. This hook has been added to allow you to do
  144. * such things as STOP the CPU to conserve power.
  145. *
  146. * Arguments : none
  147. *
  148. * Note(s) : 1) Interrupts are enabled during this call.
  149. *********************************************************************************************************
  150. */
  151. #if OS_CPU_HOOKS_EN > 0u
  152. void OSTaskIdleHook (void)
  153. {
  154. #if OS_APP_HOOKS_EN > 0u
  155. App_TaskIdleHook();
  156. #endif
  157. }
  158. #endif
  159. /*
  160. *********************************************************************************************************
  161. * TASK RETURN HOOK
  162. *
  163. * Description: This function is called if a task accidentally returns. In other words, a task should
  164. * either be an infinite loop or delete itself when done.
  165. *
  166. * Arguments : ptcb is a pointer to the task control block of the task that is returning.
  167. *
  168. * Note(s) : none
  169. *********************************************************************************************************
  170. */
  171. #if OS_CPU_HOOKS_EN > 0u
  172. void OSTaskReturnHook (OS_TCB *ptcb)
  173. {
  174. #if OS_APP_HOOKS_EN > 0u
  175. App_TaskReturnHook(ptcb);
  176. #else
  177. (void)ptcb;
  178. #endif
  179. }
  180. #endif
  181. /*
  182. *********************************************************************************************************
  183. * STATISTIC TASK HOOK
  184. *
  185. * Description: This function is called every second by uC/OS-II's statistics task. This allows your
  186. * application to add functionality to the statistics task.
  187. *
  188. * Arguments : none
  189. *********************************************************************************************************
  190. */
  191. #if OS_CPU_HOOKS_EN > 0u
  192. void OSTaskStatHook (void)
  193. {
  194. #if OS_APP_HOOKS_EN > 0u
  195. App_TaskStatHook();
  196. #endif
  197. }
  198. #endif
  199. /*
  200. *********************************************************************************************************
  201. * INITIALIZE A TASK'S STACK
  202. *
  203. * Description: This function is called by either OSTaskCreate() or OSTaskCreateExt() to initialize the
  204. * stack frame of the task being created. This function is highly processor specific.
  205. *
  206. * Arguments : task is a pointer to the task code
  207. *
  208. * p_arg is a pointer to a user supplied data area that will be passed to the task
  209. * when the task first executes.
  210. *
  211. * ptos is a pointer to the top of stack. It is assumed that 'ptos' points to
  212. * a 'free' entry on the task stack. If OS_STK_GROWTH is set to 1 then
  213. * 'ptos' will contain the HIGHEST valid address of the stack. Similarly, if
  214. * OS_STK_GROWTH is set to 0, the 'ptos' will contains the LOWEST valid address
  215. * of the stack.
  216. *
  217. * opt specifies options that can be used to alter the behavior of OSTaskStkInit().
  218. * (see uCOS_II.H for OS_TASK_OPT_xxx).
  219. *
  220. * Returns : Always returns the location of the new top-of-stack once the processor registers have
  221. * been placed on the stack in the proper order.
  222. *
  223. * Note(s) : 1) Interrupts are enabled when your task starts executing.
  224. * 2) All tasks run in Thread mode, using process stack.
  225. *********************************************************************************************************
  226. */
  227. OS_STK *OSTaskStkInit (void (*task)(void *p_arg), void *p_arg, OS_STK *ptos, INT16U opt)
  228. {
  229. OS_STK *stk;
  230. (void)opt; /* 'opt' is not used, prevent warning */
  231. stk = ptos; /* Load stack pointer */
  232. /* Registers stacked as if auto-saved on exception */
  233. *(stk) = (INT32U)0x01000000uL; /* xPSR */
  234. *(--stk) = (INT32U)task; /* Entry Point */
  235. *(--stk) = (INT32U)OS_TaskReturn; /* R14 (LR) */
  236. *(--stk) = (INT32U)0x12121212uL; /* R12 */
  237. *(--stk) = (INT32U)0x03030303uL; /* R3 */
  238. *(--stk) = (INT32U)0x02020202uL; /* R2 */
  239. *(--stk) = (INT32U)0x01010101uL; /* R1 */
  240. *(--stk) = (INT32U)p_arg; /* R0 : argument */
  241. /* Remaining registers saved on process stack */
  242. *(--stk) = (INT32U)0x11111111uL; /* R11 */
  243. *(--stk) = (INT32U)0x10101010uL; /* R10 */
  244. *(--stk) = (INT32U)0x09090909uL; /* R9 */
  245. *(--stk) = (INT32U)0x08080808uL; /* R8 */
  246. *(--stk) = (INT32U)0x07070707uL; /* R7 */
  247. *(--stk) = (INT32U)0x06060606uL; /* R6 */
  248. *(--stk) = (INT32U)0x05050505uL; /* R5 */
  249. *(--stk) = (INT32U)0x04040404uL; /* R4 */
  250. return (stk);
  251. }
  252. /*
  253. *********************************************************************************************************
  254. * TASK SWITCH HOOK
  255. *
  256. * Description: This function is called when a task switch is performed. This allows you to perform other
  257. * operations during a context switch.
  258. *
  259. * Arguments : none
  260. *
  261. * Note(s) : 1) Interrupts are disabled during this call.
  262. * 2) It is assumed that the global pointer 'OSTCBHighRdy' points to the TCB of the task that
  263. * will be 'switched in' (i.e. the highest priority task) and, 'OSTCBCur' points to the
  264. * task being switched out (i.e. the preempted task).
  265. *********************************************************************************************************
  266. */
  267. #if (OS_CPU_HOOKS_EN > 0u) && (OS_TASK_SW_HOOK_EN > 0u)
  268. void OSTaskSwHook (void)
  269. {
  270. #if OS_APP_HOOKS_EN > 0u
  271. App_TaskSwHook();
  272. #endif
  273. }
  274. #endif
  275. /*
  276. *********************************************************************************************************
  277. * OS_TCBInit() HOOK
  278. *
  279. * Description: This function is called by OS_TCBInit() after setting up most of the TCB.
  280. *
  281. * Arguments : ptcb is a pointer to the TCB of the task being created.
  282. *
  283. * Note(s) : 1) Interrupts may or may not be ENABLED during this call.
  284. *********************************************************************************************************
  285. */
  286. #if OS_CPU_HOOKS_EN > 0u
  287. void OSTCBInitHook (OS_TCB *ptcb)
  288. {
  289. #if OS_APP_HOOKS_EN > 0u
  290. App_TCBInitHook(ptcb);
  291. #else
  292. (void)ptcb; /* Prevent compiler warning */
  293. #endif
  294. }
  295. #endif
  296. /*
  297. *********************************************************************************************************
  298. * TICK HOOK
  299. *
  300. * Description: This function is called every tick.
  301. *
  302. * Arguments : none
  303. *
  304. * Note(s) : 1) Interrupts may or may not be ENABLED during this call.
  305. *********************************************************************************************************
  306. */
  307. #if (OS_CPU_HOOKS_EN > 0u) && (OS_TIME_TICK_HOOK_EN > 0u)
  308. void OSTimeTickHook (void)
  309. {
  310. #if OS_APP_HOOKS_EN > 0u
  311. App_TimeTickHook();
  312. #endif
  313. #if OS_TMR_EN > 0u
  314. OSTmrCtr++;
  315. if (OSTmrCtr >= (OS_TICKS_PER_SEC / OS_TMR_CFG_TICKS_PER_SEC)) {
  316. OSTmrCtr = 0;
  317. OSTmrSignal();
  318. }
  319. #endif
  320. }
  321. #endif
  322. /*
  323. *********************************************************************************************************
  324. * SYS TICK HANDLER
  325. *
  326. * Description: Handle the system tick (SysTick) interrupt, which is used to generate the uC/OS-II tick
  327. * interrupt.
  328. *
  329. * Arguments : none.
  330. *
  331. * Note(s) : 1) This function MUST be placed on entry 15 of the Cortex-M3 vector table.
  332. *********************************************************************************************************
  333. */
  334. void OS_CPU_SysTickHandler (void)
  335. {
  336. OS_CPU_SR cpu_sr;
  337. OS_ENTER_CRITICAL(); /* Tell uC/OS-II that we are starting an ISR */
  338. OSIntNesting++;
  339. OS_EXIT_CRITICAL();
  340. OSTimeTick(); /* Call uC/OS-II's OSTimeTick() */
  341. OSIntExit(); /* Tell uC/OS-II that we are leaving the ISR */
  342. }
  343. /*
  344. *********************************************************************************************************
  345. * INITIALIZE SYS TICK
  346. *
  347. * Description: Initialize the SysTick.
  348. *
  349. * Arguments : cnts is the number of SysTick counts between two OS tick interrupts.
  350. *
  351. * Note(s) : 1) This function MUST be called after OSStart() & after processor initialization.
  352. *********************************************************************************************************
  353. */
  354. void OS_CPU_SysTickInit (INT32U cnts)
  355. {
  356. OS_CPU_CM3_NVIC_ST_RELOAD = cnts - 1u;
  357. /* Set prio of SysTick handler to min prio. */
  358. OS_CPU_CM3_NVIC_PRIO_ST = OS_CPU_CM3_NVIC_PRIO_MIN;
  359. /* Enable timer. */
  360. OS_CPU_CM3_NVIC_ST_CTRL |= OS_CPU_CM3_NVIC_ST_CTRL_CLK_SRC | OS_CPU_CM3_NVIC_ST_CTRL_ENABLE;
  361. /* Enable timer interrupt. */
  362. OS_CPU_CM3_NVIC_ST_CTRL |= OS_CPU_CM3_NVIC_ST_CTRL_INTEN;
  363. }