port.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 Atmel AT91R40008
  30. * port.
  31. *
  32. * Components that can be compiled to either ARM or THUMB mode are
  33. * contained in this file. The ISR routines, which can only be compiled
  34. * to ARM mode are contained in portISR.c.
  35. *----------------------------------------------------------*/
  36. /* Standard includes. */
  37. #include <stdlib.h>
  38. /* Scheduler includes. */
  39. #include "FreeRTOS.h"
  40. #include "task.h"
  41. /* Hardware specific definitions. */
  42. #include "AT91R40008.h"
  43. #include "pio.h"
  44. #include "aic.h"
  45. #include "tc.h"
  46. /* Constants required to setup the task context. */
  47. #define portINITIAL_SPSR ( ( StackType_t ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */
  48. #define portTHUMB_MODE_BIT ( ( StackType_t ) 0x20 )
  49. #define portINSTRUCTION_SIZE ( ( StackType_t ) 4 )
  50. #define portNO_CRITICAL_SECTION_NESTING ( ( StackType_t ) 0 )
  51. #define portTICK_PRIORITY_6 ( 6 )
  52. /*-----------------------------------------------------------*/
  53. /* Setup the timer to generate the tick interrupts. */
  54. static void prvSetupTimerInterrupt( void );
  55. /*
  56. * The scheduler can only be started from ARM mode, so
  57. * vPortISRStartFirstSTask() is defined in portISR.c.
  58. */
  59. extern void vPortISRStartFirstTask( void );
  60. /*-----------------------------------------------------------*/
  61. /*
  62. * Initialise the stack of a task to look exactly as if a call to
  63. * portSAVE_CONTEXT had been called.
  64. *
  65. * See header file for description.
  66. */
  67. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  68. {
  69. StackType_t *pxOriginalTOS;
  70. pxOriginalTOS = pxTopOfStack;
  71. /* To ensure asserts in tasks.c don't fail, although in this case the assert
  72. is not really required. */
  73. pxTopOfStack--;
  74. /* Setup the initial stack of the task. The stack is set exactly as
  75. expected by the portRESTORE_CONTEXT() macro. */
  76. /* First on the stack is the return address - which in this case is the
  77. start of the task. The offset is added to make the return address appear
  78. as it would within an IRQ ISR. */
  79. *pxTopOfStack = ( StackType_t ) pxCode + portINSTRUCTION_SIZE;
  80. pxTopOfStack--;
  81. *pxTopOfStack = ( StackType_t ) 0xaaaaaaaa; /* R14 */
  82. pxTopOfStack--;
  83. *pxTopOfStack = ( StackType_t ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
  84. pxTopOfStack--;
  85. *pxTopOfStack = ( StackType_t ) 0x12121212; /* R12 */
  86. pxTopOfStack--;
  87. *pxTopOfStack = ( StackType_t ) 0x11111111; /* R11 */
  88. pxTopOfStack--;
  89. *pxTopOfStack = ( StackType_t ) 0x10101010; /* R10 */
  90. pxTopOfStack--;
  91. *pxTopOfStack = ( StackType_t ) 0x09090909; /* R9 */
  92. pxTopOfStack--;
  93. *pxTopOfStack = ( StackType_t ) 0x08080808; /* R8 */
  94. pxTopOfStack--;
  95. *pxTopOfStack = ( StackType_t ) 0x07070707; /* R7 */
  96. pxTopOfStack--;
  97. *pxTopOfStack = ( StackType_t ) 0x06060606; /* R6 */
  98. pxTopOfStack--;
  99. *pxTopOfStack = ( StackType_t ) 0x05050505; /* R5 */
  100. pxTopOfStack--;
  101. *pxTopOfStack = ( StackType_t ) 0x04040404; /* R4 */
  102. pxTopOfStack--;
  103. *pxTopOfStack = ( StackType_t ) 0x03030303; /* R3 */
  104. pxTopOfStack--;
  105. *pxTopOfStack = ( StackType_t ) 0x02020202; /* R2 */
  106. pxTopOfStack--;
  107. *pxTopOfStack = ( StackType_t ) 0x01010101; /* R1 */
  108. pxTopOfStack--;
  109. /* When the task starts is will expect to find the function parameter in
  110. R0. */
  111. *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
  112. pxTopOfStack--;
  113. /* The last thing onto the stack is the status register, which is set for
  114. system mode, with interrupts enabled. */
  115. *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;
  116. #ifdef THUMB_INTERWORK
  117. {
  118. /* We want the task to start in thumb mode. */
  119. *pxTopOfStack |= portTHUMB_MODE_BIT;
  120. }
  121. #endif
  122. pxTopOfStack--;
  123. /* Some optimisation levels use the stack differently to others. This
  124. means the interrupt flags cannot always be stored on the stack and will
  125. instead be stored in a variable, which is then saved as part of the
  126. tasks context. */
  127. *pxTopOfStack = portNO_CRITICAL_SECTION_NESTING;
  128. return pxTopOfStack;
  129. }
  130. /*-----------------------------------------------------------*/
  131. BaseType_t xPortStartScheduler( void )
  132. {
  133. /* Start the timer that generates the tick ISR. Interrupts are disabled
  134. here already. */
  135. prvSetupTimerInterrupt();
  136. /* Start the first task. */
  137. vPortISRStartFirstTask();
  138. /* Should not get here! */
  139. return 0;
  140. }
  141. /*-----------------------------------------------------------*/
  142. void vPortEndScheduler( void )
  143. {
  144. /* It is unlikely that the ARM port will require this function as there
  145. is nothing to return to. */
  146. }
  147. /*-----------------------------------------------------------*/
  148. /*
  149. * Setup the tick timer to generate the tick interrupts at the required frequency.
  150. */
  151. static void prvSetupTimerInterrupt( void )
  152. {
  153. volatile uint32_t ulDummy;
  154. /* Enable clock to the tick timer... */
  155. AT91C_BASE_PS->PS_PCER = portTIMER_CLK_ENABLE_BIT;
  156. /* Stop the tick timer... */
  157. portTIMER_REG_BASE_PTR->TC_CCR = TC_CLKDIS;
  158. /* Start with tick timer interrupts disabled... */
  159. portTIMER_REG_BASE_PTR->TC_IDR = 0xFFFFFFFF;
  160. /* Clear any pending tick timer interrupts... */
  161. ulDummy = portTIMER_REG_BASE_PTR->TC_SR;
  162. /* Store interrupt handler function address in tick timer vector register...
  163. The ISR installed depends on whether the preemptive or cooperative
  164. scheduler is being used. */
  165. #if configUSE_PREEMPTION == 1
  166. {
  167. extern void ( vPreemptiveTick )( void );
  168. AT91C_BASE_AIC->AIC_SVR[portTIMER_AIC_CHANNEL] = ( uint32_t ) vPreemptiveTick;
  169. }
  170. #else // else use cooperative scheduler
  171. {
  172. extern void ( vNonPreemptiveTick )( void );
  173. AT91C_BASE_AIC->AIC_SVR[portTIMER_AIC_CHANNEL] = ( uint32_t ) vNonPreemptiveTick;
  174. }
  175. #endif
  176. /* Tick timer interrupt level-sensitive, priority 6... */
  177. AT91C_BASE_AIC->AIC_SMR[ portTIMER_AIC_CHANNEL ] = AIC_SRCTYPE_INT_LEVEL_SENSITIVE | portTICK_PRIORITY_6;
  178. /* Enable the tick timer interrupt...
  179. First at timer level */
  180. portTIMER_REG_BASE_PTR->TC_IER = TC_CPCS;
  181. /* Then at the AIC level. */
  182. AT91C_BASE_AIC->AIC_IECR = (1 << portTIMER_AIC_CHANNEL);
  183. /* Calculate timer compare value to achieve the desired tick rate... */
  184. if( (configCPU_CLOCK_HZ / (configTICK_RATE_HZ * 2) ) <= 0xFFFF )
  185. {
  186. /* The tick rate is fast enough for us to use the faster timer input
  187. clock (main clock / 2). */
  188. portTIMER_REG_BASE_PTR->TC_CMR = TC_WAVE | TC_CLKS_MCK2 | TC_BURST_NONE | TC_CPCTRG;
  189. portTIMER_REG_BASE_PTR->TC_RC = configCPU_CLOCK_HZ / (configTICK_RATE_HZ * 2);
  190. }
  191. else
  192. {
  193. /* We must use a slower timer input clock (main clock / 8) because the
  194. tick rate is too slow for the faster input clock. */
  195. portTIMER_REG_BASE_PTR->TC_CMR = TC_WAVE | TC_CLKS_MCK8 | TC_BURST_NONE | TC_CPCTRG;
  196. portTIMER_REG_BASE_PTR->TC_RC = configCPU_CLOCK_HZ / (configTICK_RATE_HZ * 8);
  197. }
  198. /* Start tick timer... */
  199. portTIMER_REG_BASE_PTR->TC_CCR = TC_SWTRG | TC_CLKEN;
  200. }
  201. /*-----------------------------------------------------------*/