port.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. /*-----------------------------------------------------------
  29. * Implementation of functions defined in portable.h for the Philips ARM7 port.
  30. *----------------------------------------------------------*/
  31. /*
  32. Changes from V3.2.2
  33. + Bug fix - The prescale value for the timer setup is now written to T0PR
  34. instead of T0PC. This bug would have had no effect unless a prescale
  35. value was actually used.
  36. */
  37. /* Standard includes. */
  38. #include <stdlib.h>
  39. #include <intrinsics.h>
  40. /* Scheduler includes. */
  41. #include "FreeRTOS.h"
  42. #include "task.h"
  43. /* Constants required to setup the tick ISR. */
  44. #define portENABLE_TIMER ( ( uint8_t ) 0x01 )
  45. #define portPRESCALE_VALUE 0x00
  46. #define portINTERRUPT_ON_MATCH ( ( uint32_t ) 0x01 )
  47. #define portRESET_COUNT_ON_MATCH ( ( uint32_t ) 0x02 )
  48. /* Constants required to setup the initial stack. */
  49. #define portINITIAL_SPSR ( ( StackType_t ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */
  50. #define portTHUMB_MODE_BIT ( ( StackType_t ) 0x20 )
  51. #define portINSTRUCTION_SIZE ( ( StackType_t ) 4 )
  52. /* Constants required to setup the PIT. */
  53. #define portPIT_CLOCK_DIVISOR ( ( uint32_t ) 16 )
  54. #define portPIT_COUNTER_VALUE ( ( ( configCPU_CLOCK_HZ / portPIT_CLOCK_DIVISOR ) / 1000UL ) * portTICK_PERIOD_MS )
  55. /* Constants required to handle interrupts. */
  56. #define portTIMER_MATCH_ISR_BIT ( ( uint8_t ) 0x01 )
  57. #define portCLEAR_VIC_INTERRUPT ( ( uint32_t ) 0 )
  58. /* Constants required to handle critical sections. */
  59. #define portNO_CRITICAL_NESTING ( ( uint32_t ) 0 )
  60. #define portINT_LEVEL_SENSITIVE 0
  61. #define portPIT_ENABLE ( ( uint16_t ) 0x1 << 24 )
  62. #define portPIT_INT_ENABLE ( ( uint16_t ) 0x1 << 25 )
  63. /* Constants required to setup the VIC for the tick ISR. */
  64. #define portTIMER_VIC_CHANNEL ( ( uint32_t ) 0x0004 )
  65. #define portTIMER_VIC_CHANNEL_BIT ( ( uint32_t ) 0x0010 )
  66. #define portTIMER_VIC_ENABLE ( ( uint32_t ) 0x0020 )
  67. /*-----------------------------------------------------------*/
  68. /* Setup the PIT to generate the tick interrupts. */
  69. static void prvSetupTimerInterrupt( void );
  70. /* ulCriticalNesting will get set to zero when the first task starts. It
  71. cannot be initialised to 0 as this will cause interrupts to be enabled
  72. during the kernel initialisation process. */
  73. uint32_t ulCriticalNesting = ( uint32_t ) 9999;
  74. /*-----------------------------------------------------------*/
  75. /*
  76. * Initialise the stack of a task to look exactly as if a call to
  77. * portSAVE_CONTEXT had been called.
  78. *
  79. * See header file for description.
  80. */
  81. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  82. {
  83. StackType_t *pxOriginalTOS;
  84. pxOriginalTOS = pxTopOfStack;
  85. /* Setup the initial stack of the task. The stack is set exactly as
  86. expected by the portRESTORE_CONTEXT() macro. */
  87. /* First on the stack is the return address - which in this case is the
  88. start of the task. The offset is added to make the return address appear
  89. as it would within an IRQ ISR. */
  90. *pxTopOfStack = ( StackType_t ) pxCode + portINSTRUCTION_SIZE;
  91. pxTopOfStack--;
  92. *pxTopOfStack = ( StackType_t ) 0xaaaaaaaa; /* R14 */
  93. pxTopOfStack--;
  94. *pxTopOfStack = ( StackType_t ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
  95. pxTopOfStack--;
  96. *pxTopOfStack = ( StackType_t ) 0x12121212; /* R12 */
  97. pxTopOfStack--;
  98. *pxTopOfStack = ( StackType_t ) 0x11111111; /* R11 */
  99. pxTopOfStack--;
  100. *pxTopOfStack = ( StackType_t ) 0x10101010; /* R10 */
  101. pxTopOfStack--;
  102. *pxTopOfStack = ( StackType_t ) 0x09090909; /* R9 */
  103. pxTopOfStack--;
  104. *pxTopOfStack = ( StackType_t ) 0x08080808; /* R8 */
  105. pxTopOfStack--;
  106. *pxTopOfStack = ( StackType_t ) 0x07070707; /* R7 */
  107. pxTopOfStack--;
  108. *pxTopOfStack = ( StackType_t ) 0x06060606; /* R6 */
  109. pxTopOfStack--;
  110. *pxTopOfStack = ( StackType_t ) 0x05050505; /* R5 */
  111. pxTopOfStack--;
  112. *pxTopOfStack = ( StackType_t ) 0x04040404; /* R4 */
  113. pxTopOfStack--;
  114. *pxTopOfStack = ( StackType_t ) 0x03030303; /* R3 */
  115. pxTopOfStack--;
  116. *pxTopOfStack = ( StackType_t ) 0x02020202; /* R2 */
  117. pxTopOfStack--;
  118. *pxTopOfStack = ( StackType_t ) 0x01010101; /* R1 */
  119. pxTopOfStack--;
  120. /* When the task starts is will expect to find the function parameter in
  121. R0. */
  122. *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
  123. pxTopOfStack--;
  124. /* The status register is set for system mode, with interrupts enabled. */
  125. *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;
  126. if( ( ( uint32_t ) pxCode & 0x01UL ) != 0x00UL )
  127. {
  128. /* We want the task to start in thumb mode. */
  129. *pxTopOfStack |= portTHUMB_MODE_BIT;
  130. }
  131. pxTopOfStack--;
  132. /* Interrupt flags cannot always be stored on the stack and will
  133. instead be stored in a variable, which is then saved as part of the
  134. tasks context. */
  135. *pxTopOfStack = portNO_CRITICAL_NESTING;
  136. return pxTopOfStack;
  137. }
  138. /*-----------------------------------------------------------*/
  139. BaseType_t xPortStartScheduler( void )
  140. {
  141. extern void vPortStartFirstTask( void );
  142. /* Start the timer that generates the tick ISR. Interrupts are disabled
  143. here already. */
  144. prvSetupTimerInterrupt();
  145. /* Start the first task. */
  146. vPortStartFirstTask();
  147. /* Should not get here! */
  148. return 0;
  149. }
  150. /*-----------------------------------------------------------*/
  151. void vPortEndScheduler( void )
  152. {
  153. /* It is unlikely that the ARM port will require this function as there
  154. is nothing to return to. */
  155. }
  156. /*-----------------------------------------------------------*/
  157. #if configUSE_PREEMPTION == 0
  158. /* The cooperative scheduler requires a normal IRQ service routine to
  159. simply increment the system tick. */
  160. static __arm __irq void vPortNonPreemptiveTick( void );
  161. static __arm __irq void vPortNonPreemptiveTick( void )
  162. {
  163. /* Increment the tick count - which may wake some tasks but as the
  164. preemptive scheduler is not being used any woken task is not given
  165. processor time no matter what its priority. */
  166. xTaskIncrementTick();
  167. /* Ready for the next interrupt. */
  168. T0IR = portTIMER_MATCH_ISR_BIT;
  169. VICVectAddr = portCLEAR_VIC_INTERRUPT;
  170. }
  171. #else
  172. /* This function is called from an asm wrapper, so does not require the __irq
  173. keyword. */
  174. void vPortPreemptiveTick( void );
  175. void vPortPreemptiveTick( void )
  176. {
  177. /* Increment the tick counter. */
  178. if( xTaskIncrementTick() != pdFALSE )
  179. {
  180. /* The new tick value might unblock a task. Ensure the highest task that
  181. is ready to execute is the task that will execute when the tick ISR
  182. exits. */
  183. vTaskSwitchContext();
  184. }
  185. /* Ready for the next interrupt. */
  186. T0IR = portTIMER_MATCH_ISR_BIT;
  187. VICVectAddr = portCLEAR_VIC_INTERRUPT;
  188. }
  189. #endif
  190. /*-----------------------------------------------------------*/
  191. static void prvSetupTimerInterrupt( void )
  192. {
  193. uint32_t ulCompareMatch;
  194. /* A 1ms tick does not require the use of the timer prescale. This is
  195. defaulted to zero but can be used if necessary. */
  196. T0PR = portPRESCALE_VALUE;
  197. /* Calculate the match value required for our wanted tick rate. */
  198. ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
  199. /* Protect against divide by zero. Using an if() statement still results
  200. in a warning - hence the #if. */
  201. #if portPRESCALE_VALUE != 0
  202. {
  203. ulCompareMatch /= ( portPRESCALE_VALUE + 1 );
  204. }
  205. #endif
  206. T0MR0 = ulCompareMatch;
  207. /* Generate tick with timer 0 compare match. */
  208. T0MCR = portRESET_COUNT_ON_MATCH | portINTERRUPT_ON_MATCH;
  209. /* Setup the VIC for the timer. */
  210. VICIntSelect &= ~( portTIMER_VIC_CHANNEL_BIT );
  211. VICIntEnable |= portTIMER_VIC_CHANNEL_BIT;
  212. /* The ISR installed depends on whether the preemptive or cooperative
  213. scheduler is being used. */
  214. #if configUSE_PREEMPTION == 1
  215. {
  216. extern void ( vPortPreemptiveTickEntry )( void );
  217. VICVectAddr0 = ( uint32_t ) vPortPreemptiveTickEntry;
  218. }
  219. #else
  220. {
  221. extern void ( vNonPreemptiveTick )( void );
  222. VICVectAddr0 = ( int32_t ) vPortNonPreemptiveTick;
  223. }
  224. #endif
  225. VICVectCntl0 = portTIMER_VIC_CHANNEL | portTIMER_VIC_ENABLE;
  226. /* Start the timer - interrupts are disabled when this function is called
  227. so it is okay to do this here. */
  228. T0TCR = portENABLE_TIMER;
  229. }
  230. /*-----------------------------------------------------------*/
  231. void vPortEnterCritical( void )
  232. {
  233. /* Disable interrupts first! */
  234. __disable_interrupt();
  235. /* Now interrupts are disabled ulCriticalNesting can be accessed
  236. directly. Increment ulCriticalNesting to keep a count of how many times
  237. portENTER_CRITICAL() has been called. */
  238. ulCriticalNesting++;
  239. }
  240. /*-----------------------------------------------------------*/
  241. void vPortExitCritical( void )
  242. {
  243. if( ulCriticalNesting > portNO_CRITICAL_NESTING )
  244. {
  245. /* Decrement the nesting count as we are leaving a critical section. */
  246. ulCriticalNesting--;
  247. /* If the nesting level has reached zero then interrupts should be
  248. re-enabled. */
  249. if( ulCriticalNesting == portNO_CRITICAL_NESTING )
  250. {
  251. __enable_interrupt();
  252. }
  253. }
  254. }
  255. /*-----------------------------------------------------------*/