port.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
  3. All rights reserved
  4. VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
  5. This file is part of the FreeRTOS distribution.
  6. FreeRTOS is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License (version 2) as published by the
  8. Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
  9. ***************************************************************************
  10. >>! NOTE: The modification to the GPL is included to allow you to !<<
  11. >>! distribute a combined work that includes FreeRTOS without being !<<
  12. >>! obliged to provide the source code for proprietary components !<<
  13. >>! outside of the FreeRTOS kernel. !<<
  14. ***************************************************************************
  15. FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
  16. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  17. FOR A PARTICULAR PURPOSE. Full license text is available on the following
  18. link: http://www.freertos.org/a00114.html
  19. ***************************************************************************
  20. * *
  21. * FreeRTOS provides completely free yet professionally developed, *
  22. * robust, strictly quality controlled, supported, and cross *
  23. * platform software that is more than just the market leader, it *
  24. * is the industry's de facto standard. *
  25. * *
  26. * Help yourself get started quickly while simultaneously helping *
  27. * to support the FreeRTOS project by purchasing a FreeRTOS *
  28. * tutorial book, reference manual, or both: *
  29. * http://www.FreeRTOS.org/Documentation *
  30. * *
  31. ***************************************************************************
  32. http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
  33. the FAQ page "My application does not run, what could be wrong?". Have you
  34. defined configASSERT()?
  35. http://www.FreeRTOS.org/support - In return for receiving this top quality
  36. embedded software for free we request you assist our global community by
  37. participating in the support forum.
  38. http://www.FreeRTOS.org/training - Investing in training allows your team to
  39. be as productive as possible as early as possible. Now you can receive
  40. FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
  41. Ltd, and the world's leading authority on the world's leading RTOS.
  42. http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
  43. including FreeRTOS+Trace - an indispensable productivity tool, a DOS
  44. compatible FAT file system, and our tiny thread aware UDP/IP stack.
  45. http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
  46. Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
  47. http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
  48. Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
  49. licenses offer ticketed support, indemnification and commercial middleware.
  50. http://www.SafeRTOS.com - High Integrity Systems also provide a safety
  51. engineered and independently SIL3 certified version for use in safety and
  52. mission critical applications that require provable dependability.
  53. 1 tab == 4 spaces!
  54. */
  55. /*-----------------------------------------------------------
  56. * Implementation of functions defined in portable.h for the ARM CM0 port.
  57. *----------------------------------------------------------*/
  58. /* Scheduler includes. */
  59. #include "FreeRTOS.h"
  60. #include "task.h"
  61. /* Constants required to manipulate the NVIC. */
  62. #define portNVIC_SYSTICK_CTRL ( ( volatile uint32_t *) 0xe000e010 )
  63. #define portNVIC_SYSTICK_LOAD ( ( volatile uint32_t *) 0xe000e014 )
  64. #define portNVIC_INT_CTRL ( ( volatile uint32_t *) 0xe000ed04 )
  65. #define portNVIC_SYSPRI2 ( ( volatile uint32_t *) 0xe000ed20 )
  66. #define portNVIC_SYSTICK_CLK 0x00000004
  67. #define portNVIC_SYSTICK_INT 0x00000002
  68. #define portNVIC_SYSTICK_ENABLE 0x00000001
  69. #define portNVIC_PENDSVSET 0x10000000
  70. #define portMIN_INTERRUPT_PRIORITY ( 255UL )
  71. #define portNVIC_PENDSV_PRI ( portMIN_INTERRUPT_PRIORITY << 16UL )
  72. #define portNVIC_SYSTICK_PRI ( portMIN_INTERRUPT_PRIORITY << 24UL )
  73. /* Constants required to set up the initial stack. */
  74. #define portINITIAL_XPSR ( 0x01000000 )
  75. /* Constants used with memory barrier intrinsics. */
  76. #define portSY_FULL_READ_WRITE ( 15 )
  77. /* Each task maintains its own interrupt status in the critical nesting
  78. variable. */
  79. static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;
  80. /*
  81. * Setup the timer to generate the tick interrupts.
  82. */
  83. static void prvSetupTimerInterrupt( void );
  84. /*
  85. * Exception handlers.
  86. */
  87. void xPortPendSVHandler( void );
  88. void xPortSysTickHandler( void );
  89. void vPortSVCHandler( void );
  90. /*
  91. * Start first task is a separate function so it can be tested in isolation.
  92. */
  93. static void prvPortStartFirstTask( void );
  94. /*
  95. * Used to catch tasks that attempt to return from their implementing function.
  96. */
  97. static void prvTaskExitError( void );
  98. /*-----------------------------------------------------------*/
  99. /*
  100. * See header file for description.
  101. */
  102. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  103. {
  104. /* Simulate the stack frame as it would be created by a context switch
  105. interrupt. */
  106. pxTopOfStack--; /* Offset added to account for the way the MCU uses the stack on entry/exit of interrupts. */
  107. *pxTopOfStack = portINITIAL_XPSR; /* xPSR */
  108. pxTopOfStack--;
  109. *pxTopOfStack = ( StackType_t ) pxCode; /* PC */
  110. pxTopOfStack--;
  111. *pxTopOfStack = ( StackType_t ) prvTaskExitError; /* LR */
  112. pxTopOfStack -= 5; /* R12, R3, R2 and R1. */
  113. *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
  114. pxTopOfStack -= 8; /* R11..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. Artificially force an assert() to be triggered if configASSERT() is
  124. defined, then stop here so application writers can catch the error. */
  125. configASSERT( uxCriticalNesting == ~0UL );
  126. portDISABLE_INTERRUPTS();
  127. for( ;; );
  128. }
  129. /*-----------------------------------------------------------*/
  130. void vPortSVCHandler( void )
  131. {
  132. /* This function is no longer used, but retained for backward
  133. compatibility. */
  134. }
  135. /*-----------------------------------------------------------*/
  136. __asm void prvPortStartFirstTask( void )
  137. {
  138. extern pxCurrentTCB;
  139. PRESERVE8
  140. /* The MSP stack is not reset as, unlike on M3/4 parts, there is no vector
  141. table offset register that can be used to locate the initial stack value.
  142. Not all M0 parts have the application vector table at address 0. */
  143. ldr r3, =pxCurrentTCB /* Obtain location of pxCurrentTCB. */
  144. ldr r1, [r3]
  145. ldr r0, [r1] /* The first item in pxCurrentTCB is the task top of stack. */
  146. adds r0, #32 /* Discard everything up to r0. */
  147. msr psp, r0 /* This is now the new top of stack to use in the task. */
  148. movs r0, #2 /* Switch to the psp stack. */
  149. msr CONTROL, r0
  150. isb
  151. pop {r0-r5} /* Pop the registers that are saved automatically. */
  152. mov lr, r5 /* lr is now in r5. */
  153. pop {r3} /* The return address is now in r3. */
  154. pop {r2} /* Pop and discard the XPSR. */
  155. cpsie i /* The first task has its context and interrupts can be enabled. */
  156. bx r3 /* Finally, jump to the user defined task code. */
  157. ALIGN
  158. }
  159. /*-----------------------------------------------------------*/
  160. /*
  161. * See header file for description.
  162. */
  163. BaseType_t xPortStartScheduler( void )
  164. {
  165. /* Make PendSV, CallSV and SysTick the same priroity as the kernel. */
  166. *(portNVIC_SYSPRI2) |= portNVIC_PENDSV_PRI;
  167. *(portNVIC_SYSPRI2) |= portNVIC_SYSTICK_PRI;
  168. /* Start the timer that generates the tick ISR. Interrupts are disabled
  169. here already. */
  170. prvSetupTimerInterrupt();
  171. /* Initialise the critical nesting count ready for the first task. */
  172. uxCriticalNesting = 0;
  173. /* Start the first task. */
  174. prvPortStartFirstTask();
  175. /* Should not get here! */
  176. return 0;
  177. }
  178. /*-----------------------------------------------------------*/
  179. void vPortEndScheduler( void )
  180. {
  181. /* Not implemented in ports where there is nothing to return to.
  182. Artificially force an assert. */
  183. configASSERT( uxCriticalNesting == 1000UL );
  184. }
  185. /*-----------------------------------------------------------*/
  186. void vPortYield( void )
  187. {
  188. /* Set a PendSV to request a context switch. */
  189. *( portNVIC_INT_CTRL ) = portNVIC_PENDSVSET;
  190. /* Barriers are normally not required but do ensure the code is completely
  191. within the specified behaviour for the architecture. */
  192. __dsb( portSY_FULL_READ_WRITE );
  193. __isb( portSY_FULL_READ_WRITE );
  194. }
  195. /*-----------------------------------------------------------*/
  196. void vPortEnterCritical( void )
  197. {
  198. portDISABLE_INTERRUPTS();
  199. uxCriticalNesting++;
  200. __dsb( portSY_FULL_READ_WRITE );
  201. __isb( portSY_FULL_READ_WRITE );
  202. }
  203. /*-----------------------------------------------------------*/
  204. void vPortExitCritical( void )
  205. {
  206. configASSERT( uxCriticalNesting );
  207. uxCriticalNesting--;
  208. if( uxCriticalNesting == 0 )
  209. {
  210. portENABLE_INTERRUPTS();
  211. }
  212. }
  213. /*-----------------------------------------------------------*/
  214. __asm uint32_t ulSetInterruptMaskFromISR( void )
  215. {
  216. mrs r0, PRIMASK
  217. cpsid i
  218. bx lr
  219. }
  220. /*-----------------------------------------------------------*/
  221. __asm void vClearInterruptMaskFromISR( uint32_t ulMask )
  222. {
  223. msr PRIMASK, r0
  224. bx lr
  225. }
  226. /*-----------------------------------------------------------*/
  227. __asm void xPortPendSVHandler( void )
  228. {
  229. extern vTaskSwitchContext
  230. extern pxCurrentTCB
  231. PRESERVE8
  232. mrs r0, psp
  233. ldr r3, =pxCurrentTCB /* Get the location of the current TCB. */
  234. ldr r2, [r3]
  235. subs r0, #32 /* Make space for the remaining low registers. */
  236. str r0, [r2] /* Save the new top of stack. */
  237. stmia r0!, {r4-r7} /* Store the low registers that are not saved automatically. */
  238. mov r4, r8 /* Store the high registers. */
  239. mov r5, r9
  240. mov r6, r10
  241. mov r7, r11
  242. stmia r0!, {r4-r7}
  243. push {r3, r14}
  244. cpsid i
  245. bl vTaskSwitchContext
  246. cpsie i
  247. pop {r2, r3} /* lr goes in r3. r2 now holds tcb pointer. */
  248. ldr r1, [r2]
  249. ldr r0, [r1] /* The first item in pxCurrentTCB is the task top of stack. */
  250. adds r0, #16 /* Move to the high registers. */
  251. ldmia r0!, {r4-r7} /* Pop the high registers. */
  252. mov r8, r4
  253. mov r9, r5
  254. mov r10, r6
  255. mov r11, r7
  256. msr psp, r0 /* Remember the new top of stack for the task. */
  257. subs r0, #32 /* Go back for the low registers that are not automatically restored. */
  258. ldmia r0!, {r4-r7} /* Pop low registers. */
  259. bx r3
  260. ALIGN
  261. }
  262. /*-----------------------------------------------------------*/
  263. void xPortSysTickHandler( void )
  264. {
  265. uint32_t ulPreviousMask;
  266. ulPreviousMask = portSET_INTERRUPT_MASK_FROM_ISR();
  267. {
  268. /* Increment the RTOS tick. */
  269. if( xTaskIncrementTick() != pdFALSE )
  270. {
  271. /* Pend a context switch. */
  272. *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;
  273. }
  274. }
  275. portCLEAR_INTERRUPT_MASK_FROM_ISR( ulPreviousMask );
  276. }
  277. /*-----------------------------------------------------------*/
  278. /*
  279. * Setup the systick timer to generate the tick interrupts at the required
  280. * frequency.
  281. */
  282. void prvSetupTimerInterrupt( void )
  283. {
  284. /* Configure SysTick to interrupt at the requested rate. */
  285. *(portNVIC_SYSTICK_LOAD) = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
  286. *(portNVIC_SYSTICK_CTRL) = portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE;
  287. }
  288. /*-----------------------------------------------------------*/