port.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * FreeRTOS Kernel V11.3.0
  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. /*-----------------------------------------------------------
  29. * Implementation of functions defined in portable.h for the ARM CM4F port.
  30. *----------------------------------------------------------*/
  31. /* Scheduler includes. */
  32. #include "FreeRTOS.h"
  33. #include "task.h"
  34. /* Constants required to manipulate the NVIC. */
  35. #define portNVIC_SYSTICK_CTRL ( ( volatile uint32_t * ) 0xe000e010 )
  36. #define portNVIC_SYSTICK_LOAD ( ( volatile uint32_t * ) 0xe000e014 )
  37. #define portNVIC_SHPR3_REG ( ( volatile uint32_t * ) 0xe000ed20 )
  38. #define portNVIC_SYSTICK_CLK 0x00000004
  39. #define portNVIC_SYSTICK_INT 0x00000002
  40. #define portNVIC_SYSTICK_ENABLE 0x00000001
  41. #define portMIN_INTERRUPT_PRIORITY ( 255UL )
  42. #define portNVIC_PENDSV_PRI ( ( ( uint32_t ) portMIN_INTERRUPT_PRIORITY ) << 16UL )
  43. #define portNVIC_SYSTICK_PRI ( ( ( uint32_t ) portMIN_INTERRUPT_PRIORITY ) << 24UL )
  44. /* Masks off all bits but the VECTACTIVE bits in the ICSR register. */
  45. #define portVECTACTIVE_MASK ( 0xFFUL )
  46. /* Constants required to manipulate the VFP. */
  47. #define portFPCCR ( ( volatile uint32_t * ) 0xe000ef34 ) /* Floating point context control register. */
  48. #define portASPEN_AND_LSPEN_BITS ( 0x3UL << 30UL )
  49. /* Constants required to set up the initial stack. */
  50. #define portINITIAL_XPSR ( 0x01000000 )
  51. #define portINITIAL_EXC_RETURN ( 0xfffffffd )
  52. /* Let the user override the pre-loading of the initial LR with the address of
  53. * prvTaskExitError() in case it messes up unwinding of the stack in the
  54. * debugger. */
  55. #ifdef configTASK_RETURN_ADDRESS
  56. #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS
  57. #else
  58. #define portTASK_RETURN_ADDRESS prvTaskExitError
  59. #endif
  60. /* For strict compliance with the Cortex-M spec the task start address should
  61. * have bit-0 clear, as it is loaded into the PC on exit from an ISR. */
  62. #define portSTART_ADDRESS_MASK ( ( StackType_t ) 0xfffffffeUL )
  63. /* The priority used by the kernel is assigned to a variable to make access
  64. * from inline assembler easier. */
  65. const uint32_t ulKernelPriority = portMIN_INTERRUPT_PRIORITY;
  66. /* Each task maintains its own interrupt status in the critical nesting
  67. * variable. */
  68. static uint32_t ulCriticalNesting = 0xaaaaaaaaUL;
  69. /*
  70. * Setup the timer to generate the tick interrupts.
  71. */
  72. static void prvSetupTimerInterrupt( void );
  73. /*
  74. * Exception handlers.
  75. */
  76. void SysTick_Handler( void );
  77. /*
  78. * Functions defined in port_asm.asm.
  79. */
  80. extern void vPortEnableVFP( void );
  81. extern void vPortStartFirstTask( void );
  82. /*
  83. * Used to catch tasks that attempt to return from their implementing function.
  84. */
  85. static void prvTaskExitError( void );
  86. /* This exists purely to allow the const to be used from within the
  87. * port_asm.asm assembly file. */
  88. const uint32_t ulMaxSyscallInterruptPriorityConst = configMAX_SYSCALL_INTERRUPT_PRIORITY;
  89. /*-----------------------------------------------------------*/
  90. /*
  91. * See header file for description.
  92. */
  93. StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
  94. TaskFunction_t pxCode,
  95. void * pvParameters )
  96. {
  97. /* Simulate the stack frame as it would be created by a context switch
  98. * interrupt. */
  99. /* Offset added to account for the way the MCU uses the stack on entry/exit
  100. * of interrupts, and to ensure alignment. */
  101. pxTopOfStack--;
  102. *pxTopOfStack = portINITIAL_XPSR; /* xPSR */
  103. pxTopOfStack--;
  104. *pxTopOfStack = ( ( StackType_t ) pxCode ) & portSTART_ADDRESS_MASK; /* PC */
  105. pxTopOfStack--;
  106. *pxTopOfStack = ( StackType_t ) portTASK_RETURN_ADDRESS; /* LR */
  107. /* Save code space by skipping register initialisation. */
  108. pxTopOfStack -= 5; /* R12, R3, R2 and R1. */
  109. *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
  110. /* A save method is being used that requires each task to maintain its
  111. * own exec return value. */
  112. pxTopOfStack--;
  113. *pxTopOfStack = portINITIAL_EXC_RETURN;
  114. pxTopOfStack -= 8; /* R11, R10, R9, R8, R7, R6, R5 and R4. */
  115. return pxTopOfStack;
  116. }
  117. /*-----------------------------------------------------------*/
  118. static void prvTaskExitError( void )
  119. {
  120. /* A function that implements a task must not exit or attempt to return to
  121. * its caller as there is nothing to return to. If a task wants to exit it
  122. * should instead call vTaskDelete( NULL ).
  123. *
  124. * Artificially force an assert() to be triggered if configASSERT() is
  125. * defined, then stop here so application writers can catch the error. */
  126. configASSERT( ulCriticalNesting == ~0UL );
  127. portDISABLE_INTERRUPTS();
  128. for( ; ; )
  129. {
  130. }
  131. }
  132. /*-----------------------------------------------------------*/
  133. /*
  134. * See header file for description.
  135. */
  136. BaseType_t xPortStartScheduler( void )
  137. {
  138. /* configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0.
  139. * See https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
  140. configASSERT( ( configMAX_SYSCALL_INTERRUPT_PRIORITY ) );
  141. /* Make PendSV and SysTick the lowest priority interrupts. */
  142. *( portNVIC_SHPR3_REG ) |= portNVIC_PENDSV_PRI;
  143. *( portNVIC_SHPR3_REG ) |= portNVIC_SYSTICK_PRI;
  144. /* Start the timer that generates the tick ISR. Interrupts are disabled
  145. * here already. */
  146. prvSetupTimerInterrupt();
  147. /* Initialise the critical nesting count ready for the first task. */
  148. ulCriticalNesting = 0;
  149. /* Ensure the VFP is enabled - it should be anyway. */
  150. vPortEnableVFP();
  151. /* Lazy save always. */
  152. *( portFPCCR ) |= portASPEN_AND_LSPEN_BITS;
  153. /* Start the first task. */
  154. vPortStartFirstTask();
  155. /* Should not get here! */
  156. return 0;
  157. }
  158. /*-----------------------------------------------------------*/
  159. void vPortEndScheduler( void )
  160. {
  161. /* Not implemented in ports where there is nothing to return to.
  162. * Artificially force an assert. */
  163. configASSERT( ulCriticalNesting == 1000UL );
  164. }
  165. /*-----------------------------------------------------------*/
  166. void vPortYield( void )
  167. {
  168. /* Set a PendSV to request a context switch. */
  169. *( portNVIC_INT_CTRL ) = portNVIC_PENDSVSET;
  170. /* Barriers are normally not required but do ensure the code is completely
  171. * within the specified behaviour for the architecture. */
  172. __DSB();
  173. __ISB();
  174. }
  175. /*-----------------------------------------------------------*/
  176. void vPortEnterCritical( void )
  177. {
  178. portDISABLE_INTERRUPTS();
  179. ulCriticalNesting++;
  180. __DSB();
  181. __ISB();
  182. /* This is not the interrupt safe version of the enter critical function so
  183. * assert() if it is being called from an interrupt context. Only API
  184. * functions that end in "FromISR" can be used in an interrupt. Only assert if
  185. * the critical nesting count is 1 to protect against recursive calls if the
  186. * assert function also uses a critical section. */
  187. if( ulCriticalNesting == 1 )
  188. {
  189. configASSERT( ( ( *( portNVIC_INT_CTRL ) ) & portVECTACTIVE_MASK ) == 0 );
  190. }
  191. }
  192. /*-----------------------------------------------------------*/
  193. void vPortExitCritical( void )
  194. {
  195. configASSERT( ulCriticalNesting );
  196. ulCriticalNesting--;
  197. if( ulCriticalNesting == 0 )
  198. {
  199. portENABLE_INTERRUPTS();
  200. }
  201. }
  202. /*-----------------------------------------------------------*/
  203. void SysTick_Handler( void )
  204. {
  205. uint32_t ulDummy;
  206. ulDummy = portSET_INTERRUPT_MASK_FROM_ISR();
  207. traceISR_ENTER();
  208. {
  209. if( xTaskIncrementTick() != pdFALSE )
  210. {
  211. traceISR_EXIT_TO_SCHEDULER();
  212. /* Pend a context switch. */
  213. *( portNVIC_INT_CTRL ) = portNVIC_PENDSVSET;
  214. }
  215. else
  216. {
  217. traceISR_EXIT();
  218. }
  219. }
  220. portCLEAR_INTERRUPT_MASK_FROM_ISR( ulDummy );
  221. }
  222. /*-----------------------------------------------------------*/
  223. /*
  224. * Setup the systick timer to generate the tick interrupts at the required
  225. * frequency.
  226. */
  227. void prvSetupTimerInterrupt( void )
  228. {
  229. /* Configure SysTick to interrupt at the requested rate. */
  230. *( portNVIC_SYSTICK_LOAD ) = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
  231. *( portNVIC_SYSTICK_CTRL ) = portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE;
  232. }