port.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. /* Scheduler includes. */
  29. #include "FreeRTOS.h"
  30. #include "task.h"
  31. /*-----------------------------------------------------------
  32. * Implementation of functions defined in portable.h for the H8S port.
  33. *----------------------------------------------------------*/
  34. /*-----------------------------------------------------------*/
  35. /* When the task starts interrupts should be enabled. */
  36. #define portINITIAL_CCR ( ( StackType_t ) 0x00 )
  37. /* Hardware specific constants used to generate the RTOS tick from the TPU. */
  38. #define portCLEAR_ON_TGRA_COMPARE_MATCH ( ( uint8_t ) 0x20 )
  39. #define portCLOCK_DIV_64 ( ( uint8_t ) 0x03 )
  40. #define portCLOCK_DIV ( ( uint32_t ) 64 )
  41. #define portTGRA_INTERRUPT_ENABLE ( ( uint8_t ) 0x01 )
  42. #define portTIMER_CHANNEL ( ( uint8_t ) 0x02 )
  43. #define portMSTP13 ( ( uint16_t ) 0x2000 )
  44. /*
  45. * Setup TPU channel one for the RTOS tick at the requested frequency.
  46. */
  47. static void prvSetupTimerInterrupt( void );
  48. /*
  49. * The ISR used by portYIELD(). This is installed as a trap handler.
  50. */
  51. void vPortYield( void ) __attribute__ ( ( saveall, interrupt_handler ) );
  52. /*-----------------------------------------------------------*/
  53. /*
  54. * See header file for description.
  55. */
  56. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  57. {
  58. uint32_t ulValue;
  59. /* This requires an even address. */
  60. ulValue = ( uint32_t ) pxTopOfStack;
  61. if( ulValue & 1UL )
  62. {
  63. pxTopOfStack = pxTopOfStack - 1;
  64. }
  65. /* Place a few bytes of known values on the bottom of the stack.
  66. This is just useful for debugging. */
  67. pxTopOfStack--;
  68. *pxTopOfStack = 0xaa;
  69. pxTopOfStack--;
  70. *pxTopOfStack = 0xbb;
  71. pxTopOfStack--;
  72. *pxTopOfStack = 0xcc;
  73. pxTopOfStack--;
  74. *pxTopOfStack = 0xdd;
  75. /* The initial stack mimics an interrupt stack. First there is the program
  76. counter (24 bits). */
  77. ulValue = ( uint32_t ) pxCode;
  78. pxTopOfStack--;
  79. *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );
  80. pxTopOfStack--;
  81. ulValue >>= 8UL;
  82. *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );
  83. pxTopOfStack--;
  84. ulValue >>= 8UL;
  85. *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );
  86. /* Followed by the CCR. */
  87. pxTopOfStack--;
  88. *pxTopOfStack = portINITIAL_CCR;
  89. /* Next all the general purpose registers - with the parameters being passed
  90. in ER0. The parameter order must match that used by the compiler when the
  91. "saveall" function attribute is used. */
  92. /* ER6 */
  93. pxTopOfStack--;
  94. *pxTopOfStack = 0x66;
  95. pxTopOfStack--;
  96. *pxTopOfStack = 0x66;
  97. pxTopOfStack--;
  98. *pxTopOfStack = 0x66;
  99. pxTopOfStack--;
  100. *pxTopOfStack = 0x66;
  101. /* ER0 */
  102. ulValue = ( uint32_t ) pvParameters;
  103. pxTopOfStack--;
  104. *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );
  105. pxTopOfStack--;
  106. ulValue >>= 8UL;
  107. *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );
  108. pxTopOfStack--;
  109. ulValue >>= 8UL;
  110. *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );
  111. pxTopOfStack--;
  112. ulValue >>= 8UL;
  113. *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );
  114. /* ER1 */
  115. pxTopOfStack--;
  116. *pxTopOfStack = 0x11;
  117. pxTopOfStack--;
  118. *pxTopOfStack = 0x11;
  119. pxTopOfStack--;
  120. *pxTopOfStack = 0x11;
  121. pxTopOfStack--;
  122. *pxTopOfStack = 0x11;
  123. /* ER2 */
  124. pxTopOfStack--;
  125. *pxTopOfStack = 0x22;
  126. pxTopOfStack--;
  127. *pxTopOfStack = 0x22;
  128. pxTopOfStack--;
  129. *pxTopOfStack = 0x22;
  130. pxTopOfStack--;
  131. *pxTopOfStack = 0x22;
  132. /* ER3 */
  133. pxTopOfStack--;
  134. *pxTopOfStack = 0x33;
  135. pxTopOfStack--;
  136. *pxTopOfStack = 0x33;
  137. pxTopOfStack--;
  138. *pxTopOfStack = 0x33;
  139. pxTopOfStack--;
  140. *pxTopOfStack = 0x33;
  141. /* ER4 */
  142. pxTopOfStack--;
  143. *pxTopOfStack = 0x44;
  144. pxTopOfStack--;
  145. *pxTopOfStack = 0x44;
  146. pxTopOfStack--;
  147. *pxTopOfStack = 0x44;
  148. pxTopOfStack--;
  149. *pxTopOfStack = 0x44;
  150. /* ER5 */
  151. pxTopOfStack--;
  152. *pxTopOfStack = 0x55;
  153. pxTopOfStack--;
  154. *pxTopOfStack = 0x55;
  155. pxTopOfStack--;
  156. *pxTopOfStack = 0x55;
  157. pxTopOfStack--;
  158. *pxTopOfStack = 0x55;
  159. return pxTopOfStack;
  160. }
  161. /*-----------------------------------------------------------*/
  162. BaseType_t xPortStartScheduler( void )
  163. {
  164. extern void * pxCurrentTCB;
  165. /* Setup the hardware to generate the tick. */
  166. prvSetupTimerInterrupt();
  167. /* Restore the context of the first task that is going to run. This
  168. mirrors the function epilogue code generated by the compiler when the
  169. "saveall" function attribute is used. */
  170. asm volatile (
  171. "MOV.L @_pxCurrentTCB, ER6 \n\t"
  172. "MOV.L @ER6, ER7 \n\t"
  173. "LDM.L @SP+, (ER4-ER5) \n\t"
  174. "LDM.L @SP+, (ER0-ER3) \n\t"
  175. "MOV.L @ER7+, ER6 \n\t"
  176. "RTE \n\t"
  177. );
  178. ( void ) pxCurrentTCB;
  179. /* Should not get here. */
  180. return pdTRUE;
  181. }
  182. /*-----------------------------------------------------------*/
  183. void vPortEndScheduler( void )
  184. {
  185. /* It is unlikely that the h8 port will get stopped. */
  186. }
  187. /*-----------------------------------------------------------*/
  188. /*
  189. * Manual context switch. This is a trap handler. The "saveall" function
  190. * attribute is used so the context is saved by the compiler prologue. All
  191. * we have to do is save the stack pointer.
  192. */
  193. void vPortYield( void )
  194. {
  195. portSAVE_STACK_POINTER();
  196. vTaskSwitchContext();
  197. portRESTORE_STACK_POINTER();
  198. }
  199. /*-----------------------------------------------------------*/
  200. /*
  201. * The interrupt handler installed for the RTOS tick depends on whether the
  202. * preemptive or cooperative scheduler is being used.
  203. */
  204. #if( configUSE_PREEMPTION == 1 )
  205. /*
  206. * The preemptive scheduler is used so the ISR calls vTaskSwitchContext().
  207. * The function prologue saves the context so all we have to do is save
  208. * the stack pointer.
  209. */
  210. void vTickISR( void ) __attribute__ ( ( saveall, interrupt_handler ) );
  211. void vTickISR( void )
  212. {
  213. portSAVE_STACK_POINTER();
  214. if( xTaskIncrementTick() != pdFALSE )
  215. {
  216. vTaskSwitchContext();
  217. }
  218. /* Clear the interrupt. */
  219. TSR1 &= ~0x01;
  220. portRESTORE_STACK_POINTER();
  221. }
  222. #else
  223. /*
  224. * The cooperative scheduler is being used so all we have to do is
  225. * periodically increment the tick. This can just be a normal ISR and
  226. * the "saveall" attribute is not required.
  227. */
  228. void vTickISR( void ) __attribute__ ( ( interrupt_handler ) );
  229. void vTickISR( void )
  230. {
  231. xTaskIncrementTick();
  232. /* Clear the interrupt. */
  233. TSR1 &= ~0x01;
  234. }
  235. #endif
  236. /*-----------------------------------------------------------*/
  237. /*
  238. * Setup timer 1 compare match to generate a tick interrupt.
  239. */
  240. static void prvSetupTimerInterrupt( void )
  241. {
  242. const uint32_t ulCompareMatch = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) / portCLOCK_DIV;
  243. /* Turn the module on. */
  244. MSTPCR &= ~portMSTP13;
  245. /* Configure timer 1. */
  246. TCR1 = portCLEAR_ON_TGRA_COMPARE_MATCH | portCLOCK_DIV_64;
  247. /* Configure the compare match value for a tick of configTICK_RATE_HZ. */
  248. TGR1A = ulCompareMatch;
  249. /* Start the timer and enable the interrupt - we can do this here as
  250. interrupts are globally disabled when this function is called. */
  251. TIER1 |= portTGRA_INTERRUPT_ENABLE;
  252. TSTR |= portTIMER_CHANNEL;
  253. }
  254. /*-----------------------------------------------------------*/