port.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. * FreeRTOS Kernel V10.4.3
  3. * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * https://www.FreeRTOS.org
  23. * https://github.com/FreeRTOS
  24. *
  25. * 1 tab == 4 spaces!
  26. */
  27. /* IAR includes. */
  28. #include <intrinsics.h>
  29. /* Scheduler includes. */
  30. #include "FreeRTOS.h"
  31. #include "task.h"
  32. #if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1
  33. /* Check the configuration. */
  34. #if( configMAX_PRIORITIES > 32 )
  35. #error configUSE_PORT_OPTIMISED_TASK_SELECTION can only be set to 1 when configMAX_PRIORITIES is less than or equal to 32. It is very rare that a system requires more than 10 to 15 difference priorities as tasks that share a priority will time slice.
  36. #endif
  37. #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
  38. #ifndef configSETUP_TICK_INTERRUPT
  39. #error configSETUP_TICK_INTERRUPT() must be defined in FreeRTOSConfig.h to call the function that sets up the tick interrupt. A default that uses the PIT is provided in the official demo application.
  40. #endif
  41. #ifndef configCLEAR_TICK_INTERRUPT
  42. #error configCLEAR_TICK_INTERRUPT must be defined in FreeRTOSConfig.h to clear which ever interrupt was used to generate the tick interrupt. A default that uses the PIT is provided in the official demo application.
  43. #endif
  44. /* A critical section is exited when the critical section nesting count reaches
  45. this value. */
  46. #define portNO_CRITICAL_NESTING ( ( uint32_t ) 0 )
  47. /* Tasks are not created with a floating point context, but can be given a
  48. floating point context after they have been created. A variable is stored as
  49. part of the tasks context that holds portNO_FLOATING_POINT_CONTEXT if the task
  50. does not have an FPU context, or any other value if the task does have an FPU
  51. context. */
  52. #define portNO_FLOATING_POINT_CONTEXT ( ( StackType_t ) 0 )
  53. /* Constants required to setup the initial task context. */
  54. #define portINITIAL_SPSR ( ( StackType_t ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */
  55. #define portTHUMB_MODE_BIT ( ( StackType_t ) 0x20 )
  56. #define portTHUMB_MODE_ADDRESS ( 0x01UL )
  57. /* Masks all bits in the APSR other than the mode bits. */
  58. #define portAPSR_MODE_BITS_MASK ( 0x1F )
  59. /* The value of the mode bits in the APSR when the CPU is executing in user
  60. mode. */
  61. #define portAPSR_USER_MODE ( 0x10 )
  62. /*-----------------------------------------------------------*/
  63. /*
  64. * Starts the first task executing. This function is necessarily written in
  65. * assembly code so is implemented in portASM.s.
  66. */
  67. extern void vPortRestoreTaskContext( void );
  68. /*
  69. * Used to catch tasks that attempt to return from their implementing function.
  70. */
  71. static void prvTaskExitError( void );
  72. /*-----------------------------------------------------------*/
  73. /* A variable is used to keep track of the critical section nesting. This
  74. variable has to be stored as part of the task context and must be initialised to
  75. a non zero value to ensure interrupts don't inadvertently become unmasked before
  76. the scheduler starts. As it is stored as part of the task context it will
  77. automatically be set to 0 when the first task is started. */
  78. volatile uint32_t ulCriticalNesting = 9999UL;
  79. /* Saved as part of the task context. If ulPortTaskHasFPUContext is non-zero
  80. then a floating point context must be saved and restored for the task. */
  81. uint32_t ulPortTaskHasFPUContext = pdFALSE;
  82. /* Set to 1 to pend a context switch from an ISR. */
  83. uint32_t ulPortYieldRequired = pdFALSE;
  84. /* Counts the interrupt nesting depth. A context switch is only performed if
  85. if the nesting depth is 0. */
  86. uint32_t ulPortInterruptNesting = 0UL;
  87. /*-----------------------------------------------------------*/
  88. /*
  89. * See header file for description.
  90. */
  91. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  92. {
  93. /* Setup the initial stack of the task. The stack is set exactly as
  94. expected by the portRESTORE_CONTEXT() macro.
  95. The fist real value on the stack is the status register, which is set for
  96. system mode, with interrupts enabled. A few NULLs are added first to ensure
  97. GDB does not try decoding a non-existent return address. */
  98. *pxTopOfStack = NULL;
  99. pxTopOfStack--;
  100. *pxTopOfStack = NULL;
  101. pxTopOfStack--;
  102. *pxTopOfStack = NULL;
  103. pxTopOfStack--;
  104. *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;
  105. if( ( ( uint32_t ) pxCode & portTHUMB_MODE_ADDRESS ) != 0x00UL )
  106. {
  107. /* The task will start in THUMB mode. */
  108. *pxTopOfStack |= portTHUMB_MODE_BIT;
  109. }
  110. pxTopOfStack--;
  111. /* Next the return address, which in this case is the start of the task. */
  112. *pxTopOfStack = ( StackType_t ) pxCode;
  113. pxTopOfStack--;
  114. /* Next all the registers other than the stack pointer. */
  115. *pxTopOfStack = ( StackType_t ) prvTaskExitError; /* R14 */
  116. pxTopOfStack--;
  117. *pxTopOfStack = ( StackType_t ) 0x12121212; /* R12 */
  118. pxTopOfStack--;
  119. *pxTopOfStack = ( StackType_t ) 0x11111111; /* R11 */
  120. pxTopOfStack--;
  121. *pxTopOfStack = ( StackType_t ) 0x10101010; /* R10 */
  122. pxTopOfStack--;
  123. *pxTopOfStack = ( StackType_t ) 0x09090909; /* R9 */
  124. pxTopOfStack--;
  125. *pxTopOfStack = ( StackType_t ) 0x08080808; /* R8 */
  126. pxTopOfStack--;
  127. *pxTopOfStack = ( StackType_t ) 0x07070707; /* R7 */
  128. pxTopOfStack--;
  129. *pxTopOfStack = ( StackType_t ) 0x06060606; /* R6 */
  130. pxTopOfStack--;
  131. *pxTopOfStack = ( StackType_t ) 0x05050505; /* R5 */
  132. pxTopOfStack--;
  133. *pxTopOfStack = ( StackType_t ) 0x04040404; /* R4 */
  134. pxTopOfStack--;
  135. *pxTopOfStack = ( StackType_t ) 0x03030303; /* R3 */
  136. pxTopOfStack--;
  137. *pxTopOfStack = ( StackType_t ) 0x02020202; /* R2 */
  138. pxTopOfStack--;
  139. *pxTopOfStack = ( StackType_t ) 0x01010101; /* R1 */
  140. pxTopOfStack--;
  141. *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
  142. pxTopOfStack--;
  143. /* The task will start with a critical nesting count of 0 as interrupts are
  144. enabled. */
  145. *pxTopOfStack = portNO_CRITICAL_NESTING;
  146. pxTopOfStack--;
  147. #define ENABLE_FLOAT_CONTEXT_PROTECT
  148. #ifdef ENABLE_FLOAT_CONTEXT_PROTECT
  149. *pxTopOfStack = 0; // D0
  150. pxTopOfStack--;
  151. *pxTopOfStack = 0;
  152. pxTopOfStack--;
  153. *pxTopOfStack = 0;
  154. pxTopOfStack--;
  155. *pxTopOfStack = 0;
  156. pxTopOfStack--;
  157. *pxTopOfStack = 0;
  158. pxTopOfStack--;
  159. *pxTopOfStack = 0;
  160. pxTopOfStack--;
  161. *pxTopOfStack = 0;
  162. pxTopOfStack--;
  163. *pxTopOfStack = 0;
  164. pxTopOfStack--;
  165. *pxTopOfStack = 0;
  166. pxTopOfStack--;
  167. *pxTopOfStack = 0;
  168. pxTopOfStack--;
  169. *pxTopOfStack = 0;
  170. pxTopOfStack--;
  171. *pxTopOfStack = 0;
  172. pxTopOfStack--;
  173. *pxTopOfStack = 0;
  174. pxTopOfStack--;
  175. *pxTopOfStack = 0;
  176. pxTopOfStack--;
  177. *pxTopOfStack = 0;
  178. pxTopOfStack--;
  179. *pxTopOfStack = 0; // D7
  180. pxTopOfStack--;
  181. *pxTopOfStack = 0; // D8
  182. pxTopOfStack--;
  183. *pxTopOfStack = 0;
  184. pxTopOfStack--;
  185. *pxTopOfStack = 0;
  186. pxTopOfStack--;
  187. *pxTopOfStack = 0;
  188. pxTopOfStack--;
  189. *pxTopOfStack = 0;
  190. pxTopOfStack--;
  191. *pxTopOfStack = 0;
  192. pxTopOfStack--;
  193. *pxTopOfStack = 0;
  194. pxTopOfStack--;
  195. *pxTopOfStack = 0;
  196. pxTopOfStack--;
  197. *pxTopOfStack = 0;
  198. pxTopOfStack--;
  199. *pxTopOfStack = 0;
  200. pxTopOfStack--;
  201. *pxTopOfStack = 0;
  202. pxTopOfStack--;
  203. *pxTopOfStack = 0;
  204. pxTopOfStack--;
  205. *pxTopOfStack = 0;
  206. pxTopOfStack--;
  207. *pxTopOfStack = 0;
  208. pxTopOfStack--;
  209. *pxTopOfStack = 0;
  210. pxTopOfStack--;
  211. *pxTopOfStack = 0; // D15
  212. pxTopOfStack--;
  213. #if configFPU_D32 == 1
  214. *pxTopOfStack = 0; // D16
  215. pxTopOfStack--;
  216. *pxTopOfStack = 0;
  217. pxTopOfStack--;
  218. *pxTopOfStack = 0;
  219. pxTopOfStack--;
  220. *pxTopOfStack = 0;
  221. pxTopOfStack--;
  222. *pxTopOfStack = 0;
  223. pxTopOfStack--;
  224. *pxTopOfStack = 0;
  225. pxTopOfStack--;
  226. *pxTopOfStack = 0;
  227. pxTopOfStack--;
  228. *pxTopOfStack = 0;
  229. pxTopOfStack--;
  230. *pxTopOfStack = 0;
  231. pxTopOfStack--;
  232. *pxTopOfStack = 0;
  233. pxTopOfStack--;
  234. *pxTopOfStack = 0;
  235. pxTopOfStack--;
  236. *pxTopOfStack = 0;
  237. pxTopOfStack--;
  238. *pxTopOfStack = 0;
  239. pxTopOfStack--;
  240. *pxTopOfStack = 0;
  241. pxTopOfStack--;
  242. *pxTopOfStack = 0;
  243. pxTopOfStack--;
  244. *pxTopOfStack = 0; // D23
  245. pxTopOfStack--;
  246. *pxTopOfStack = 0; // D24
  247. pxTopOfStack--;
  248. *pxTopOfStack = 0;
  249. pxTopOfStack--;
  250. *pxTopOfStack = 0;
  251. pxTopOfStack--;
  252. *pxTopOfStack = 0;
  253. pxTopOfStack--;
  254. *pxTopOfStack = 0;
  255. pxTopOfStack--;
  256. *pxTopOfStack = 0;
  257. pxTopOfStack--;
  258. *pxTopOfStack = 0;
  259. pxTopOfStack--;
  260. *pxTopOfStack = 0;
  261. pxTopOfStack--;
  262. *pxTopOfStack = 0;
  263. pxTopOfStack--;
  264. *pxTopOfStack = 0;
  265. pxTopOfStack--;
  266. *pxTopOfStack = 0;
  267. pxTopOfStack--;
  268. *pxTopOfStack = 0;
  269. pxTopOfStack--;
  270. *pxTopOfStack = 0;
  271. pxTopOfStack--;
  272. *pxTopOfStack = 0;
  273. pxTopOfStack--;
  274. *pxTopOfStack = 0;
  275. pxTopOfStack--;
  276. *pxTopOfStack = 0; // D31
  277. pxTopOfStack--;
  278. #endif
  279. //FPSCR
  280. *pxTopOfStack = 0; // FPSCR
  281. pxTopOfStack--;
  282. *pxTopOfStack = 1; // enable floating point context
  283. #else
  284. /* The task will start without a floating point context. A task that uses
  285. the floating point hardware must call vPortTaskUsesFPU() before executing
  286. any floating point instructions. */
  287. *pxTopOfStack = portNO_FLOATING_POINT_CONTEXT;
  288. #endif
  289. return pxTopOfStack;
  290. }
  291. /*-----------------------------------------------------------*/
  292. static void prvTaskExitError( void )
  293. {
  294. /* A function that implements a task must not exit or attempt to return to
  295. its caller as there is nothing to return to. If a task wants to exit it
  296. should instead call vTaskDelete( NULL ).
  297. Artificially force an assert() to be triggered if configASSERT() is
  298. defined, then stop here so application writers can catch the error. */
  299. configASSERT( ulPortInterruptNesting == ~0UL );
  300. portDISABLE_INTERRUPTS();
  301. for( ;; );
  302. }
  303. /*-----------------------------------------------------------*/
  304. BaseType_t xPortStartScheduler( void )
  305. {
  306. uint32_t ulAPSR;
  307. /* Only continue if the CPU is not in User mode. The CPU must be in a
  308. Privileged mode for the scheduler to start. */
  309. __asm volatile ( "MRS %0, APSR" : "=r" ( ulAPSR ) );
  310. ulAPSR &= portAPSR_MODE_BITS_MASK;
  311. configASSERT( ulAPSR != portAPSR_USER_MODE );
  312. if( ulAPSR != portAPSR_USER_MODE )
  313. {
  314. /* Start the timer that generates the tick ISR. */
  315. configSETUP_TICK_INTERRUPT();
  316. vPortRestoreTaskContext();
  317. }
  318. /* Will only get here if vTaskStartScheduler() was called with the CPU in
  319. a non-privileged mode or the binary point register was not set to its lowest
  320. possible value. */
  321. return 0;
  322. }
  323. /*-----------------------------------------------------------*/
  324. void vPortEndScheduler( void )
  325. {
  326. /* Not implemented in ports where there is nothing to return to.
  327. Artificially force an assert. */
  328. configASSERT( ulCriticalNesting == 1000UL );
  329. }
  330. /*-----------------------------------------------------------*/
  331. void vPortEnterCritical( void )
  332. {
  333. portDISABLE_INTERRUPTS();
  334. /* Now interrupts are disabled ulCriticalNesting can be accessed
  335. directly. Increment ulCriticalNesting to keep a count of how many times
  336. portENTER_CRITICAL() has been called. */
  337. ulCriticalNesting++;
  338. /* This is not the interrupt safe version of the enter critical function so
  339. assert() if it is being called from an interrupt context. Only API
  340. functions that end in "FromISR" can be used in an interrupt. Only assert if
  341. the critical nesting count is 1 to protect against recursive calls if the
  342. assert function also uses a critical section. */
  343. if( ulCriticalNesting == 1 )
  344. {
  345. configASSERT( ulPortInterruptNesting == 0 );
  346. }
  347. }
  348. /*-----------------------------------------------------------*/
  349. void vPortExitCritical( void )
  350. {
  351. if( ulCriticalNesting > portNO_CRITICAL_NESTING )
  352. {
  353. /* Decrement the nesting count as the critical section is being
  354. exited. */
  355. ulCriticalNesting--;
  356. /* If the nesting level has reached zero then all interrupt
  357. priorities must be re-enabled. */
  358. if( ulCriticalNesting == portNO_CRITICAL_NESTING )
  359. {
  360. /* Critical nesting has reached zero so all interrupt priorities
  361. should be unmasked. */
  362. portENABLE_INTERRUPTS();
  363. }
  364. }
  365. }
  366. /*-----------------------------------------------------------*/
  367. void FreeRTOS_Tick_Handler( void )
  368. {
  369. portDISABLE_INTERRUPTS();
  370. /* Increment the RTOS tick. */
  371. if( xTaskIncrementTick() != pdFALSE )
  372. {
  373. ulPortYieldRequired = pdTRUE;
  374. }
  375. portENABLE_INTERRUPTS();
  376. configCLEAR_TICK_INTERRUPT();
  377. }
  378. /*-----------------------------------------------------------*/
  379. void vPortTaskUsesFPU( void )
  380. {
  381. uint32_t ulInitialFPSCR = 0;
  382. /* A task is registering the fact that it needs an FPU context. Set the
  383. FPU flag (which is saved as part of the task context). */
  384. ulPortTaskHasFPUContext = pdTRUE;
  385. /* Initialise the floating point status register. */
  386. __asm( "FMXR FPSCR, %0" :: "r" (ulInitialFPSCR) );
  387. }
  388. extern uint8_t interrupt_get_nest(void);
  389. BaseType_t xPortIsInInterrupt( void )
  390. {
  391. BaseType_t xReturn;
  392. if( interrupt_get_nest() == 0 )
  393. {
  394. xReturn = pdFALSE;
  395. }
  396. else
  397. {
  398. xReturn = pdTRUE;
  399. }
  400. return xReturn;
  401. }
  402. /*-----------------------------------------------------------*/