port.c 16 KB

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