port.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. /* Scheduler includes. */
  31. #include "FreeRTOS.h"
  32. #include "task.h"
  33. /* Constants required to setup the initial task context. */
  34. #define portINITIAL_SPSR ( ( StackType_t ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */
  35. #define portTHUMB_MODE_BIT ( ( StackType_t ) 0x20 )
  36. #define portINSTRUCTION_SIZE ( ( StackType_t ) 4 )
  37. #define portNO_CRITICAL_SECTION_NESTING ( ( StackType_t ) 0 )
  38. /* Constants required to setup the tick ISR. */
  39. #define portENABLE_TIMER ( ( uint8_t ) 0x01 )
  40. #define portPRESCALE_VALUE 0x00
  41. #define portINTERRUPT_ON_MATCH ( ( uint32_t ) 0x01 )
  42. #define portRESET_COUNT_ON_MATCH ( ( uint32_t ) 0x02 )
  43. /* Constants required to setup the VIC for the tick ISR. */
  44. #define portTIMER_VIC_CHANNEL ( ( uint32_t ) 0x0004 )
  45. #define portTIMER_VIC_CHANNEL_BIT ( ( uint32_t ) 0x0010 )
  46. #define portTIMER_VIC_ENABLE ( ( uint32_t ) 0x0020 )
  47. /* Constants required to handle interrupts. */
  48. #define portTIMER_MATCH_ISR_BIT ( ( uint8_t ) 0x01 )
  49. #define portCLEAR_VIC_INTERRUPT ( ( uint32_t ) 0 )
  50. /*-----------------------------------------------------------*/
  51. /* The code generated by the Keil compiler does not maintain separate
  52. stack and frame pointers. The portENTER_CRITICAL macro cannot therefore
  53. use the stack as per other ports. Instead a variable is used to keep
  54. track of the critical section nesting. This variable has to be stored
  55. as part of the task context and must be initialised to a non zero value. */
  56. #define portNO_CRITICAL_NESTING ( ( uint32_t ) 0 )
  57. volatile uint32_t ulCriticalNesting = 9999UL;
  58. /*-----------------------------------------------------------*/
  59. /* Setup the timer to generate the tick interrupts. */
  60. static void prvSetupTimerInterrupt( void );
  61. /*
  62. * The scheduler can only be started from ARM mode, so
  63. * vPortStartFirstSTask() is defined in portISR.c.
  64. */
  65. extern __asm void vPortStartFirstTask( void );
  66. /*-----------------------------------------------------------*/
  67. /*
  68. * See header file for description.
  69. */
  70. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  71. {
  72. StackType_t *pxOriginalTOS;
  73. /* Setup the initial stack of the task. The stack is set exactly as
  74. expected by the portRESTORE_CONTEXT() macro.
  75. Remember where the top of the (simulated) stack is before we place
  76. anything on it. */
  77. pxOriginalTOS = pxTopOfStack;
  78. /* To ensure asserts in tasks.c don't fail, although in this case the assert
  79. is not really required. */
  80. pxTopOfStack--;
  81. /* First on the stack is the return address - which in this case is the
  82. start of the task. The offset is added to make the return address appear
  83. as it would within an IRQ ISR. */
  84. *pxTopOfStack = ( StackType_t ) pxCode + portINSTRUCTION_SIZE;
  85. pxTopOfStack--;
  86. *pxTopOfStack = ( StackType_t ) 0xaaaaaaaa; /* R14 */
  87. pxTopOfStack--;
  88. *pxTopOfStack = ( StackType_t ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
  89. pxTopOfStack--;
  90. *pxTopOfStack = ( StackType_t ) 0x12121212; /* R12 */
  91. pxTopOfStack--;
  92. *pxTopOfStack = ( StackType_t ) 0x11111111; /* R11 */
  93. pxTopOfStack--;
  94. *pxTopOfStack = ( StackType_t ) 0x10101010; /* R10 */
  95. pxTopOfStack--;
  96. *pxTopOfStack = ( StackType_t ) 0x09090909; /* R9 */
  97. pxTopOfStack--;
  98. *pxTopOfStack = ( StackType_t ) 0x08080808; /* R8 */
  99. pxTopOfStack--;
  100. *pxTopOfStack = ( StackType_t ) 0x07070707; /* R7 */
  101. pxTopOfStack--;
  102. *pxTopOfStack = ( StackType_t ) 0x06060606; /* R6 */
  103. pxTopOfStack--;
  104. *pxTopOfStack = ( StackType_t ) 0x05050505; /* R5 */
  105. pxTopOfStack--;
  106. *pxTopOfStack = ( StackType_t ) 0x04040404; /* R4 */
  107. pxTopOfStack--;
  108. *pxTopOfStack = ( StackType_t ) 0x03030303; /* R3 */
  109. pxTopOfStack--;
  110. *pxTopOfStack = ( StackType_t ) 0x02020202; /* R2 */
  111. pxTopOfStack--;
  112. *pxTopOfStack = ( StackType_t ) 0x01010101; /* R1 */
  113. pxTopOfStack--;
  114. *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
  115. pxTopOfStack--;
  116. /* The last thing onto the stack is the status register, which is set for
  117. system mode, with interrupts enabled. */
  118. *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;
  119. if( ( ( uint32_t ) pxCode & 0x01UL ) != 0x00UL )
  120. {
  121. /* We want the task to start in thumb mode. */
  122. *pxTopOfStack |= portTHUMB_MODE_BIT;
  123. }
  124. pxTopOfStack--;
  125. /* The code generated by the Keil compiler does not maintain separate
  126. stack and frame pointers. The portENTER_CRITICAL macro cannot therefore
  127. use the stack as per other ports. Instead a variable is used to keep
  128. track of the critical section nesting. This variable has to be stored
  129. as part of the task context and is initially set to zero. */
  130. *pxTopOfStack = portNO_CRITICAL_SECTION_NESTING;
  131. return pxTopOfStack;
  132. }
  133. /*-----------------------------------------------------------*/
  134. BaseType_t xPortStartScheduler( void )
  135. {
  136. /* Start the timer that generates the tick ISR. */
  137. prvSetupTimerInterrupt();
  138. /* Start the first task. This is done from portISR.c as ARM mode must be
  139. used. */
  140. vPortStartFirstTask();
  141. /* Should not get here! */
  142. return 0;
  143. }
  144. /*-----------------------------------------------------------*/
  145. void vPortEndScheduler( void )
  146. {
  147. /* It is unlikely that the ARM port will require this function as there
  148. is nothing to return to. If this is required - stop the tick ISR then
  149. return back to main. */
  150. }
  151. /*-----------------------------------------------------------*/
  152. #if configUSE_PREEMPTION == 0
  153. /*
  154. * The cooperative scheduler requires a normal IRQ service routine to
  155. * simply increment the system tick.
  156. */
  157. void vNonPreemptiveTick( void ) __irq;
  158. void vNonPreemptiveTick( void ) __irq
  159. {
  160. /* Increment the tick count - this may make a delaying task ready
  161. to run - but a context switch is not performed. */
  162. xTaskIncrementTick();
  163. T0IR = portTIMER_MATCH_ISR_BIT; /* Clear the timer event */
  164. VICVectAddr = portCLEAR_VIC_INTERRUPT; /* Acknowledge the Interrupt */
  165. }
  166. #else
  167. /*
  168. **************************************************************************
  169. * The preemptive scheduler ISR is written in assembler and can be found
  170. * in the portASM.s file. This will only get used if portUSE_PREEMPTION
  171. * is set to 1 in portmacro.h
  172. **************************************************************************
  173. */
  174. void vPreemptiveTick( void );
  175. #endif
  176. /*-----------------------------------------------------------*/
  177. static void prvSetupTimerInterrupt( void )
  178. {
  179. uint32_t ulCompareMatch;
  180. /* A 1ms tick does not require the use of the timer prescale. This is
  181. defaulted to zero but can be used if necessary. */
  182. T0PR = portPRESCALE_VALUE;
  183. /* Calculate the match value required for our wanted tick rate. */
  184. ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
  185. /* Protect against divide by zero. Using an if() statement still results
  186. in a warning - hence the #if. */
  187. #if portPRESCALE_VALUE != 0
  188. {
  189. ulCompareMatch /= ( portPRESCALE_VALUE + 1 );
  190. }
  191. #endif
  192. T0MR0 = ulCompareMatch;
  193. /* Generate tick with timer 0 compare match. */
  194. T0MCR = portRESET_COUNT_ON_MATCH | portINTERRUPT_ON_MATCH;
  195. /* Setup the VIC for the timer. */
  196. VICIntSelect &= ~( portTIMER_VIC_CHANNEL_BIT );
  197. VICIntEnable |= portTIMER_VIC_CHANNEL_BIT;
  198. /* The ISR installed depends on whether the preemptive or cooperative
  199. scheduler is being used. */
  200. #if configUSE_PREEMPTION == 1
  201. {
  202. VICVectAddr0 = ( uint32_t ) vPreemptiveTick;
  203. }
  204. #else
  205. {
  206. VICVectAddr0 = ( uint32_t ) vNonPreemptiveTick;
  207. }
  208. #endif
  209. VICVectCntl0 = portTIMER_VIC_CHANNEL | portTIMER_VIC_ENABLE;
  210. /* Start the timer - interrupts are disabled when this function is called
  211. so it is okay to do this here. */
  212. T0TCR = portENABLE_TIMER;
  213. }
  214. /*-----------------------------------------------------------*/
  215. void vPortEnterCritical( void )
  216. {
  217. /* Disable interrupts as per portDISABLE_INTERRUPTS(); */
  218. __disable_irq();
  219. /* Now interrupts are disabled ulCriticalNesting can be accessed
  220. directly. Increment ulCriticalNesting to keep a count of how many times
  221. portENTER_CRITICAL() has been called. */
  222. ulCriticalNesting++;
  223. }
  224. /*-----------------------------------------------------------*/
  225. void vPortExitCritical( void )
  226. {
  227. if( ulCriticalNesting > portNO_CRITICAL_NESTING )
  228. {
  229. /* Decrement the nesting count as we are leaving a critical section. */
  230. ulCriticalNesting--;
  231. /* If the nesting level has reached zero then interrupts should be
  232. re-enabled. */
  233. if( ulCriticalNesting == portNO_CRITICAL_NESTING )
  234. {
  235. /* Enable interrupts as per portEXIT_CRITICAL(). */
  236. __enable_irq();
  237. }
  238. }
  239. }
  240. /*-----------------------------------------------------------*/