port.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 ARM7 port.
  30. *----------------------------------------------------------*/
  31. /* Standard includes. */
  32. #include <stdlib.h>
  33. /* Scheduler includes. */
  34. #include "FreeRTOS.h"
  35. #include "task.h"
  36. /* Hardware includes. */
  37. #include <board.h>
  38. #include <pio/pio.h>
  39. #include <pio/pio_it.h>
  40. #include <pit/pit.h>
  41. #include <aic/aic.h>
  42. #include <tc/tc.h>
  43. #include <utility/led.h>
  44. #include <utility/trace.h>
  45. /*-----------------------------------------------------------*/
  46. /* Constants required to setup the initial stack. */
  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. /* Constants required to setup the PIT. */
  51. #define port1MHz_IN_Hz ( 1000000ul )
  52. #define port1SECOND_IN_uS ( 1000000.0 )
  53. /* Constants required to handle critical sections. */
  54. #define portNO_CRITICAL_NESTING ( ( uint32_t ) 0 )
  55. #define portINT_LEVEL_SENSITIVE 0
  56. #define portPIT_ENABLE ( ( uint16_t ) 0x1 << 24 )
  57. #define portPIT_INT_ENABLE ( ( uint16_t ) 0x1 << 25 )
  58. /*-----------------------------------------------------------*/
  59. /* Setup the PIT to generate the tick interrupts. */
  60. static void prvSetupTimerInterrupt( void );
  61. /* The PIT interrupt handler - the RTOS tick. */
  62. static void vPortTickISR( void );
  63. /* ulCriticalNesting will get set to zero when the first task starts. It
  64. cannot be initialised to 0 as this will cause interrupts to be enabled
  65. during the kernel initialisation process. */
  66. uint32_t ulCriticalNesting = ( uint32_t ) 9999;
  67. /*-----------------------------------------------------------*/
  68. /*
  69. * Initialise the stack of a task to look exactly as if a call to
  70. * portSAVE_CONTEXT had been called.
  71. *
  72. * See header file for description.
  73. */
  74. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  75. {
  76. StackType_t *pxOriginalTOS;
  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. /* Setup the initial stack of the task. The stack is set exactly as
  82. expected by the portRESTORE_CONTEXT() macro. */
  83. /* First on the stack is the return address - which in this case is the
  84. start of the task. The offset is added to make the return address appear
  85. as it would within an IRQ ISR. */
  86. *pxTopOfStack = ( StackType_t ) pxCode + portINSTRUCTION_SIZE;
  87. pxTopOfStack--;
  88. *pxTopOfStack = ( StackType_t ) 0xaaaaaaaa; /* R14 */
  89. pxTopOfStack--;
  90. *pxTopOfStack = ( StackType_t ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
  91. pxTopOfStack--;
  92. *pxTopOfStack = ( StackType_t ) 0x12121212; /* R12 */
  93. pxTopOfStack--;
  94. *pxTopOfStack = ( StackType_t ) 0x11111111; /* R11 */
  95. pxTopOfStack--;
  96. *pxTopOfStack = ( StackType_t ) 0x10101010; /* R10 */
  97. pxTopOfStack--;
  98. *pxTopOfStack = ( StackType_t ) 0x09090909; /* R9 */
  99. pxTopOfStack--;
  100. *pxTopOfStack = ( StackType_t ) 0x08080808; /* R8 */
  101. pxTopOfStack--;
  102. *pxTopOfStack = ( StackType_t ) 0x07070707; /* R7 */
  103. pxTopOfStack--;
  104. *pxTopOfStack = ( StackType_t ) 0x06060606; /* R6 */
  105. pxTopOfStack--;
  106. *pxTopOfStack = ( StackType_t ) 0x05050505; /* R5 */
  107. pxTopOfStack--;
  108. *pxTopOfStack = ( StackType_t ) 0x04040404; /* R4 */
  109. pxTopOfStack--;
  110. *pxTopOfStack = ( StackType_t ) 0x03030303; /* R3 */
  111. pxTopOfStack--;
  112. *pxTopOfStack = ( StackType_t ) 0x02020202; /* R2 */
  113. pxTopOfStack--;
  114. *pxTopOfStack = ( StackType_t ) 0x01010101; /* R1 */
  115. pxTopOfStack--;
  116. /* When the task starts is will expect to find the function parameter in
  117. R0. */
  118. *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
  119. pxTopOfStack--;
  120. /* The status register is set for system mode, with interrupts enabled. */
  121. *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;
  122. #ifdef THUMB_INTERWORK
  123. {
  124. /* We want the task to start in thumb mode. */
  125. *pxTopOfStack |= portTHUMB_MODE_BIT;
  126. }
  127. #endif
  128. pxTopOfStack--;
  129. /* Interrupt flags cannot always be stored on the stack and will
  130. instead be stored in a variable, which is then saved as part of the
  131. tasks context. */
  132. *pxTopOfStack = portNO_CRITICAL_NESTING;
  133. return pxTopOfStack;
  134. }
  135. /*-----------------------------------------------------------*/
  136. BaseType_t xPortStartScheduler( void )
  137. {
  138. extern void vPortStartFirstTask( void );
  139. /* Start the timer that generates the tick ISR. Interrupts are disabled
  140. here already. */
  141. prvSetupTimerInterrupt();
  142. /* Start the first task. */
  143. vPortStartFirstTask();
  144. /* Should not get here! */
  145. return 0;
  146. }
  147. /*-----------------------------------------------------------*/
  148. void vPortEndScheduler( void )
  149. {
  150. /* It is unlikely that the ARM port will require this function as there
  151. is nothing to return to. */
  152. }
  153. /*-----------------------------------------------------------*/
  154. static __arm void vPortTickISR( void )
  155. {
  156. volatile uint32_t ulDummy;
  157. /* Increment the tick count - which may wake some tasks but as the
  158. preemptive scheduler is not being used any woken task is not given
  159. processor time no matter what its priority. */
  160. if( xTaskIncrementTick() != pdFALSE )
  161. {
  162. vTaskSwitchContext();
  163. }
  164. /* Clear the PIT interrupt. */
  165. ulDummy = AT91C_BASE_PITC->PITC_PIVR;
  166. /* To remove compiler warning. */
  167. ( void ) ulDummy;
  168. /* The AIC is cleared in the asm wrapper, outside of this function. */
  169. }
  170. /*-----------------------------------------------------------*/
  171. static void prvSetupTimerInterrupt( void )
  172. {
  173. const uint32_t ulPeriodIn_uS = ( 1.0 / ( double ) configTICK_RATE_HZ ) * port1SECOND_IN_uS;
  174. /* Setup the PIT for the required frequency. */
  175. PIT_Init( ulPeriodIn_uS, BOARD_MCK / port1MHz_IN_Hz );
  176. /* Setup the PIT interrupt. */
  177. AIC_DisableIT( AT91C_ID_SYS );
  178. AIC_ConfigureIT( AT91C_ID_SYS, AT91C_AIC_PRIOR_LOWEST, vPortTickISR );
  179. AIC_EnableIT( AT91C_ID_SYS );
  180. PIT_EnableIT();
  181. }
  182. /*-----------------------------------------------------------*/
  183. void vPortEnterCritical( void )
  184. {
  185. /* Disable interrupts first! */
  186. __disable_irq();
  187. /* Now interrupts are disabled ulCriticalNesting can be accessed
  188. directly. Increment ulCriticalNesting to keep a count of how many times
  189. portENTER_CRITICAL() has been called. */
  190. ulCriticalNesting++;
  191. }
  192. /*-----------------------------------------------------------*/
  193. void vPortExitCritical( void )
  194. {
  195. if( ulCriticalNesting > portNO_CRITICAL_NESTING )
  196. {
  197. /* Decrement the nesting count as we are leaving a critical section. */
  198. ulCriticalNesting--;
  199. /* If the nesting level has reached zero then interrupts should be
  200. re-enabled. */
  201. if( ulCriticalNesting == portNO_CRITICAL_NESTING )
  202. {
  203. __enable_irq();
  204. }
  205. }
  206. }
  207. /*-----------------------------------------------------------*/