port.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * FreeRTOS Kernel <DEVELOPMENT BRANCH>
  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 ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */
  40. #define portTHUMB_MODE_BIT ( ( StackType_t ) 0x20 )
  41. #define portINSTRUCTION_SIZE ( ( StackType_t ) 4 )
  42. /* Constants required to handle critical sections. */
  43. #define portNO_CRITICAL_NESTING ( ( uint32_t ) 0 )
  44. /* Prescale used on the timer clock when calculating the tick period. */
  45. #define portPRESCALE 20
  46. /*-----------------------------------------------------------*/
  47. /* Setup the TB to generate the tick interrupts. */
  48. static void prvSetupTimerInterrupt( void );
  49. /*-----------------------------------------------------------*/
  50. /*
  51. * Initialise the stack of a task to look exactly as if a call to
  52. * portSAVE_CONTEXT had been called.
  53. *
  54. * See header file for description.
  55. */
  56. StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
  57. TaskFunction_t pxCode,
  58. void * pvParameters )
  59. {
  60. StackType_t * pxOriginalTOS;
  61. pxOriginalTOS = pxTopOfStack;
  62. /* To ensure asserts in tasks.c don't fail, although in this case the assert
  63. * is not really required. */
  64. pxTopOfStack--;
  65. /* Setup the initial stack of the task. The stack is set exactly as
  66. * expected by the portRESTORE_CONTEXT() macro. */
  67. /* First on the stack is the return address - which in this case is the
  68. * start of the task. The offset is added to make the return address appear
  69. * as it would within an IRQ ISR. */
  70. *pxTopOfStack = ( StackType_t ) pxCode + portINSTRUCTION_SIZE;
  71. pxTopOfStack--;
  72. *pxTopOfStack = ( StackType_t ) 0xaaaaaaaa; /* R14 */
  73. pxTopOfStack--;
  74. *pxTopOfStack = ( StackType_t ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
  75. pxTopOfStack--;
  76. *pxTopOfStack = ( StackType_t ) 0x12121212; /* R12 */
  77. pxTopOfStack--;
  78. *pxTopOfStack = ( StackType_t ) 0x11111111; /* R11 */
  79. pxTopOfStack--;
  80. *pxTopOfStack = ( StackType_t ) 0x10101010; /* R10 */
  81. pxTopOfStack--;
  82. *pxTopOfStack = ( StackType_t ) 0x09090909; /* R9 */
  83. pxTopOfStack--;
  84. *pxTopOfStack = ( StackType_t ) 0x08080808; /* R8 */
  85. pxTopOfStack--;
  86. *pxTopOfStack = ( StackType_t ) 0x07070707; /* R7 */
  87. pxTopOfStack--;
  88. *pxTopOfStack = ( StackType_t ) 0x06060606; /* R6 */
  89. pxTopOfStack--;
  90. *pxTopOfStack = ( StackType_t ) 0x05050505; /* R5 */
  91. pxTopOfStack--;
  92. *pxTopOfStack = ( StackType_t ) 0x04040404; /* R4 */
  93. pxTopOfStack--;
  94. *pxTopOfStack = ( StackType_t ) 0x03030303; /* R3 */
  95. pxTopOfStack--;
  96. *pxTopOfStack = ( StackType_t ) 0x02020202; /* R2 */
  97. pxTopOfStack--;
  98. *pxTopOfStack = ( StackType_t ) 0x01010101; /* R1 */
  99. pxTopOfStack--;
  100. /* When the task starts is will expect to find the function parameter in
  101. * R0. */
  102. *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
  103. pxTopOfStack--;
  104. /* The status register is set for system mode, with interrupts enabled. */
  105. *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;
  106. #ifdef THUMB_INTERWORK
  107. {
  108. /* We want the task to start in thumb mode. */
  109. *pxTopOfStack |= portTHUMB_MODE_BIT;
  110. }
  111. #endif
  112. pxTopOfStack--;
  113. /* Interrupt flags cannot always be stored on the stack and will
  114. * instead be stored in a variable, which is then saved as part of the
  115. * tasks context. */
  116. *pxTopOfStack = portNO_CRITICAL_NESTING;
  117. return pxTopOfStack;
  118. }
  119. /*-----------------------------------------------------------*/
  120. BaseType_t xPortStartScheduler( void )
  121. {
  122. extern void vPortISRStartFirstTask( void );
  123. /* Start the timer that generates the tick ISR. Interrupts are disabled
  124. * here already. */
  125. prvSetupTimerInterrupt();
  126. /* Start the first task. */
  127. vPortISRStartFirstTask();
  128. /* Should not get here! */
  129. return 0;
  130. }
  131. /*-----------------------------------------------------------*/
  132. void vPortEndScheduler( void )
  133. {
  134. /* It is unlikely that the ARM port will require this function as there
  135. * is nothing to return to. */
  136. }
  137. /*-----------------------------------------------------------*/
  138. static void prvSetupTimerInterrupt( void )
  139. {
  140. EIC_IRQInitTypeDef EIC_IRQInitStructure;
  141. TB_InitTypeDef TB_InitStructure;
  142. /* Setup the EIC for the TB. */
  143. EIC_IRQInitStructure.EIC_IRQChannelCmd = ENABLE;
  144. EIC_IRQInitStructure.EIC_IRQChannel = TB_IRQChannel;
  145. EIC_IRQInitStructure.EIC_IRQChannelPriority = 1;
  146. EIC_IRQInit( &EIC_IRQInitStructure );
  147. /* Setup the TB for the generation of the tick interrupt. */
  148. TB_InitStructure.TB_Mode = TB_Mode_Timing;
  149. TB_InitStructure.TB_CounterMode = TB_CounterMode_Down;
  150. TB_InitStructure.TB_Prescaler = portPRESCALE - 1;
  151. TB_InitStructure.TB_AutoReload = ( ( configCPU_CLOCK_HZ / portPRESCALE ) / configTICK_RATE_HZ );
  152. TB_Init( &TB_InitStructure );
  153. /* Enable TB Update interrupt */
  154. TB_ITConfig( TB_IT_Update, ENABLE );
  155. /* Clear TB Update interrupt pending bit */
  156. TB_ClearITPendingBit( TB_IT_Update );
  157. /* Enable TB */
  158. TB_Cmd( ENABLE );
  159. }
  160. /*-----------------------------------------------------------*/