port.c 6.3 KB

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