port.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 ST STR75x ARM7
  30. * port.
  31. *----------------------------------------------------------*/
  32. /* Library includes. */
  33. #include "75x_tb.h"
  34. #include "75x_eic.h"
  35. /* Scheduler includes. */
  36. #include "FreeRTOS.h"
  37. #include "task.h"
  38. /* Constants required to setup the initial stack. */
  39. #define portINITIAL_SPSR ( ( StackType_t ) 0x3f ) /* System mode, THUMB mode, interrupts enabled. */
  40. #define portINSTRUCTION_SIZE ( ( StackType_t ) 4 )
  41. /* Constants required to handle critical sections. */
  42. #define portNO_CRITICAL_NESTING ( ( uint32_t ) 0 )
  43. /* Prescale used on the timer clock when calculating the tick period. */
  44. #define portPRESCALE 20
  45. /*-----------------------------------------------------------*/
  46. /* Setup the TB to generate the tick interrupts. */
  47. static void prvSetupTimerInterrupt( void );
  48. /* ulCriticalNesting will get set to zero when the first task starts. It
  49. cannot be initialised to 0 as this will cause interrupts to be enabled
  50. during the kernel initialisation process. */
  51. uint32_t ulCriticalNesting = ( uint32_t ) 9999;
  52. /* Tick interrupt routines for preemptive operation. */
  53. __arm void vPortPreemptiveTick( void );
  54. /*-----------------------------------------------------------*/
  55. /*
  56. * Initialise the stack of a task to look exactly as if a call to
  57. * portSAVE_CONTEXT had been called.
  58. *
  59. * See header file for description.
  60. */
  61. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  62. {
  63. StackType_t *pxOriginalTOS;
  64. pxOriginalTOS = pxTopOfStack;
  65. /* To ensure asserts in tasks.c don't fail, although in this case the assert
  66. is not really required. */
  67. pxTopOfStack--;
  68. /* Setup the initial stack of the task. The stack is set exactly as
  69. expected by the portRESTORE_CONTEXT() macro. */
  70. /* First on the stack is the return address - which in this case is the
  71. start of the task. The offset is added to make the return address appear
  72. as it would within an IRQ ISR. */
  73. *pxTopOfStack = ( StackType_t ) pxCode + portINSTRUCTION_SIZE;
  74. pxTopOfStack--;
  75. *pxTopOfStack = ( StackType_t ) 0xaaaaaaaa; /* R14 */
  76. pxTopOfStack--;
  77. *pxTopOfStack = ( StackType_t ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
  78. pxTopOfStack--;
  79. *pxTopOfStack = ( StackType_t ) 0x12121212; /* R12 */
  80. pxTopOfStack--;
  81. *pxTopOfStack = ( StackType_t ) 0x11111111; /* R11 */
  82. pxTopOfStack--;
  83. *pxTopOfStack = ( StackType_t ) 0x10101010; /* R10 */
  84. pxTopOfStack--;
  85. *pxTopOfStack = ( StackType_t ) 0x09090909; /* R9 */
  86. pxTopOfStack--;
  87. *pxTopOfStack = ( StackType_t ) 0x08080808; /* R8 */
  88. pxTopOfStack--;
  89. *pxTopOfStack = ( StackType_t ) 0x07070707; /* R7 */
  90. pxTopOfStack--;
  91. *pxTopOfStack = ( StackType_t ) 0x06060606; /* R6 */
  92. pxTopOfStack--;
  93. *pxTopOfStack = ( StackType_t ) 0x05050505; /* R5 */
  94. pxTopOfStack--;
  95. *pxTopOfStack = ( StackType_t ) 0x04040404; /* R4 */
  96. pxTopOfStack--;
  97. *pxTopOfStack = ( StackType_t ) 0x03030303; /* R3 */
  98. pxTopOfStack--;
  99. *pxTopOfStack = ( StackType_t ) 0x02020202; /* R2 */
  100. pxTopOfStack--;
  101. *pxTopOfStack = ( StackType_t ) 0x01010101; /* R1 */
  102. pxTopOfStack--;
  103. /* When the task starts is will expect to find the function parameter in
  104. R0. */
  105. *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
  106. pxTopOfStack--;
  107. /* The status register is set for system mode, with interrupts enabled. */
  108. *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;
  109. pxTopOfStack--;
  110. /* Interrupt flags cannot always be stored on the stack and will
  111. instead be stored in a variable, which is then saved as part of the
  112. tasks context. */
  113. *pxTopOfStack = portNO_CRITICAL_NESTING;
  114. return pxTopOfStack;
  115. }
  116. /*-----------------------------------------------------------*/
  117. BaseType_t xPortStartScheduler( void )
  118. {
  119. extern void vPortStartFirstTask( void );
  120. /* Start the timer that generates the tick ISR. Interrupts are disabled
  121. here already. */
  122. prvSetupTimerInterrupt();
  123. /* Start the first task. */
  124. vPortStartFirstTask();
  125. /* Should not get here! */
  126. return 0;
  127. }
  128. /*-----------------------------------------------------------*/
  129. void vPortEndScheduler( void )
  130. {
  131. /* It is unlikely that the ARM port will require this function as there
  132. is nothing to return to. */
  133. }
  134. /*-----------------------------------------------------------*/
  135. __arm void vPortPreemptiveTick( void )
  136. {
  137. /* Increment the tick counter. */
  138. if( xTaskIncrementTick() != pdFALSE )
  139. {
  140. /* Select a new task to execute. */
  141. vTaskSwitchContext();
  142. }
  143. TB_ClearITPendingBit( TB_IT_Update );
  144. }
  145. /*-----------------------------------------------------------*/
  146. static void prvSetupTimerInterrupt( void )
  147. {
  148. EIC_IRQInitTypeDef EIC_IRQInitStructure;
  149. TB_InitTypeDef TB_InitStructure;
  150. /* Setup the EIC for the TB. */
  151. EIC_IRQInitStructure.EIC_IRQChannelCmd = ENABLE;
  152. EIC_IRQInitStructure.EIC_IRQChannel = TB_IRQChannel;
  153. EIC_IRQInitStructure.EIC_IRQChannelPriority = 1;
  154. EIC_IRQInit(&EIC_IRQInitStructure);
  155. /* Setup the TB for the generation of the tick interrupt. */
  156. TB_InitStructure.TB_Mode = TB_Mode_Timing;
  157. TB_InitStructure.TB_CounterMode = TB_CounterMode_Down;
  158. TB_InitStructure.TB_Prescaler = portPRESCALE - 1;
  159. TB_InitStructure.TB_AutoReload = ( ( configCPU_CLOCK_HZ / portPRESCALE ) / configTICK_RATE_HZ );
  160. TB_Init(&TB_InitStructure);
  161. /* Enable TB Update interrupt */
  162. TB_ITConfig(TB_IT_Update, ENABLE);
  163. /* Clear TB Update interrupt pending bit */
  164. TB_ClearITPendingBit(TB_IT_Update);
  165. /* Enable TB */
  166. TB_Cmd(ENABLE);
  167. }
  168. /*-----------------------------------------------------------*/
  169. __arm __interwork void vPortEnterCritical( void )
  170. {
  171. /* Disable interrupts first! */
  172. __disable_interrupt();
  173. /* Now interrupts are disabled ulCriticalNesting can be accessed
  174. directly. Increment ulCriticalNesting to keep a count of how many times
  175. portENTER_CRITICAL() has been called. */
  176. ulCriticalNesting++;
  177. }
  178. /*-----------------------------------------------------------*/
  179. __arm __interwork void vPortExitCritical( void )
  180. {
  181. if( ulCriticalNesting > portNO_CRITICAL_NESTING )
  182. {
  183. /* Decrement the nesting count as we are leaving a critical section. */
  184. ulCriticalNesting--;
  185. /* If the nesting level has reached zero then interrupts should be
  186. re-enabled. */
  187. if( ulCriticalNesting == portNO_CRITICAL_NESTING )
  188. {
  189. __enable_interrupt();
  190. }
  191. }
  192. }
  193. /*-----------------------------------------------------------*/