port.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 SH2A port.
  30. *----------------------------------------------------------*/
  31. /* Scheduler includes. */
  32. #include "FreeRTOS.h"
  33. #include "task.h"
  34. /* Library includes. */
  35. #include "string.h"
  36. /*-----------------------------------------------------------*/
  37. /* The SR assigned to a newly created task. The only important thing in this
  38. value is for all interrupts to be enabled. */
  39. #define portINITIAL_SR ( 0UL )
  40. /* Dimensions the array into which the floating point context is saved.
  41. Allocate enough space for FPR0 to FPR15, FPUL and FPSCR, each of which is 4
  42. bytes big. If this number is changed then the 72 in portasm.src also needs
  43. changing. */
  44. #define portFLOP_REGISTERS_TO_STORE ( 18 )
  45. #define portFLOP_STORAGE_SIZE ( portFLOP_REGISTERS_TO_STORE * 4 )
  46. #if( configSUPPORT_DYNAMIC_ALLOCATION == 0 )
  47. #error configSUPPORT_DYNAMIC_ALLOCATION must be 1 to use this port.
  48. #endif
  49. /*-----------------------------------------------------------*/
  50. /*
  51. * The TRAPA handler used to force a context switch.
  52. */
  53. void vPortYield( void );
  54. /*
  55. * Function to start the first task executing - defined in portasm.src.
  56. */
  57. extern void vPortStartFirstTask( void );
  58. /*
  59. * Obtains the current GBR value - defined in portasm.src.
  60. */
  61. extern uint32_t ulPortGetGBR( void );
  62. /*-----------------------------------------------------------*/
  63. /*
  64. * See header file for description.
  65. */
  66. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  67. {
  68. /* Mark the end of the stack - used for debugging only and can be removed. */
  69. *pxTopOfStack = 0x11111111UL;
  70. pxTopOfStack--;
  71. *pxTopOfStack = 0x22222222UL;
  72. pxTopOfStack--;
  73. *pxTopOfStack = 0x33333333UL;
  74. pxTopOfStack--;
  75. /* SR. */
  76. *pxTopOfStack = portINITIAL_SR;
  77. pxTopOfStack--;
  78. /* PC. */
  79. *pxTopOfStack = ( uint32_t ) pxCode;
  80. pxTopOfStack--;
  81. /* PR. */
  82. *pxTopOfStack = 15;
  83. pxTopOfStack--;
  84. /* 14. */
  85. *pxTopOfStack = 14;
  86. pxTopOfStack--;
  87. /* R13. */
  88. *pxTopOfStack = 13;
  89. pxTopOfStack--;
  90. /* R12. */
  91. *pxTopOfStack = 12;
  92. pxTopOfStack--;
  93. /* R11. */
  94. *pxTopOfStack = 11;
  95. pxTopOfStack--;
  96. /* R10. */
  97. *pxTopOfStack = 10;
  98. pxTopOfStack--;
  99. /* R9. */
  100. *pxTopOfStack = 9;
  101. pxTopOfStack--;
  102. /* R8. */
  103. *pxTopOfStack = 8;
  104. pxTopOfStack--;
  105. /* R7. */
  106. *pxTopOfStack = 7;
  107. pxTopOfStack--;
  108. /* R6. */
  109. *pxTopOfStack = 6;
  110. pxTopOfStack--;
  111. /* R5. */
  112. *pxTopOfStack = 5;
  113. pxTopOfStack--;
  114. /* R4. */
  115. *pxTopOfStack = ( uint32_t ) pvParameters;
  116. pxTopOfStack--;
  117. /* R3. */
  118. *pxTopOfStack = 3;
  119. pxTopOfStack--;
  120. /* R2. */
  121. *pxTopOfStack = 2;
  122. pxTopOfStack--;
  123. /* R1. */
  124. *pxTopOfStack = 1;
  125. pxTopOfStack--;
  126. /* R0 */
  127. *pxTopOfStack = 0;
  128. pxTopOfStack--;
  129. /* MACL. */
  130. *pxTopOfStack = 16;
  131. pxTopOfStack--;
  132. /* MACH. */
  133. *pxTopOfStack = 17;
  134. pxTopOfStack--;
  135. /* GBR. */
  136. *pxTopOfStack = ulPortGetGBR();
  137. /* GBR = global base register.
  138. VBR = vector base register.
  139. TBR = jump table base register.
  140. R15 is the stack pointer. */
  141. return pxTopOfStack;
  142. }
  143. /*-----------------------------------------------------------*/
  144. BaseType_t xPortStartScheduler( void )
  145. {
  146. extern void vApplicationSetupTimerInterrupt( void );
  147. /* Call an application function to set up the timer that will generate the
  148. tick interrupt. This way the application can decide which peripheral to
  149. use. A demo application is provided to show a suitable example. */
  150. vApplicationSetupTimerInterrupt();
  151. /* Start the first task. This will only restore the standard registers and
  152. not the flop registers. This does not really matter though because the only
  153. flop register that is initialised to a particular value is fpscr, and it is
  154. only initialised to the current value, which will still be the current value
  155. when the first task starts executing. */
  156. trapa( portSTART_SCHEDULER_TRAP_NO );
  157. /* Should not get here. */
  158. return pdFAIL;
  159. }
  160. /*-----------------------------------------------------------*/
  161. void vPortEndScheduler( void )
  162. {
  163. /* Not implemented as there is nothing to return to. */
  164. }
  165. /*-----------------------------------------------------------*/
  166. void vPortYield( void )
  167. {
  168. int32_t lInterruptMask;
  169. /* Ensure the yield trap runs at the same priority as the other interrupts
  170. that can cause a context switch. */
  171. lInterruptMask = get_imask();
  172. /* taskYIELD() can only be called from a task, not an interrupt, so the
  173. current interrupt mask can only be 0 or portKERNEL_INTERRUPT_PRIORITY and
  174. the mask can be set without risk of accidentally lowering the mask value. */
  175. set_imask( portKERNEL_INTERRUPT_PRIORITY );
  176. trapa( portYIELD_TRAP_NO );
  177. /* Restore the interrupt mask to whatever it was previously (when the
  178. function was entered). */
  179. set_imask( ( int ) lInterruptMask );
  180. }
  181. /*-----------------------------------------------------------*/
  182. BaseType_t xPortUsesFloatingPoint( TaskHandle_t xTask )
  183. {
  184. uint32_t *pulFlopBuffer;
  185. BaseType_t xReturn;
  186. extern void * volatile pxCurrentTCB;
  187. /* This function tells the kernel that the task referenced by xTask is
  188. going to use the floating point registers and therefore requires the
  189. floating point registers saved as part of its context. */
  190. /* Passing NULL as xTask is used to indicate that the calling task is the
  191. subject task - so pxCurrentTCB is the task handle. */
  192. if( xTask == NULL )
  193. {
  194. xTask = ( TaskHandle_t ) pxCurrentTCB;
  195. }
  196. /* Allocate a buffer large enough to hold all the flop registers. */
  197. pulFlopBuffer = ( uint32_t * ) pvPortMalloc( portFLOP_STORAGE_SIZE );
  198. if( pulFlopBuffer != NULL )
  199. {
  200. /* Start with the registers in a benign state. */
  201. memset( ( void * ) pulFlopBuffer, 0x00, portFLOP_STORAGE_SIZE );
  202. /* The first thing to get saved in the buffer is the FPSCR value -
  203. initialise this to the current FPSCR value. */
  204. *pulFlopBuffer = get_fpscr();
  205. /* Use the task tag to point to the flop buffer. Pass pointer to just
  206. above the buffer because the flop save routine uses a pre-decrement. */
  207. vTaskSetApplicationTaskTag( xTask, ( void * ) ( pulFlopBuffer + portFLOP_REGISTERS_TO_STORE ) );
  208. xReturn = pdPASS;
  209. }
  210. else
  211. {
  212. xReturn = pdFAIL;
  213. }
  214. return xReturn;
  215. }
  216. /*-----------------------------------------------------------*/