port.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 SH2A port.
  30. *----------------------------------------------------------*/
  31. /* Scheduler includes. */
  32. #include "FreeRTOS.h"
  33. #include "task.h"
  34. /* Library includes. */
  35. #include "string.h"
  36. /* Hardware specifics. */
  37. #if ( configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H == 1 )
  38. #include "platform.h"
  39. #else /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
  40. #include "iodefine.h"
  41. #endif /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
  42. /*-----------------------------------------------------------*/
  43. /* Tasks should start with interrupts enabled and in Supervisor mode, therefore
  44. * PSW is set with U and I set, and PM and IPL clear. */
  45. #define portINITIAL_PSW ( ( StackType_t ) 0x00030000 )
  46. #define portINITIAL_FPSW ( ( StackType_t ) 0x00000100 )
  47. /* These macros allow a critical section to be added around the call to
  48. * xTaskIncrementTick(), which is only ever called from interrupts at the kernel
  49. * priority - ie a known priority. Therefore these local macros are a slight
  50. * optimisation compared to calling the global SET/CLEAR_INTERRUPT_MASK macros,
  51. * which would require the old IPL to be read first and stored in a local variable. */
  52. #define portDISABLE_INTERRUPTS_FROM_KERNEL_ISR() __asm volatile ( "MVTIPL %0" ::"i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY ) )
  53. #define portENABLE_INTERRUPTS_FROM_KERNEL_ISR() __asm volatile ( "MVTIPL %0" ::"i" ( configKERNEL_INTERRUPT_PRIORITY ) )
  54. /*-----------------------------------------------------------*/
  55. /*
  56. * Function to start the first task executing - written in asm code as direct
  57. * access to registers is required.
  58. */
  59. static void prvStartFirstTask( void ) __attribute__( ( naked ) );
  60. /*
  61. * Software interrupt handler. Performs the actual context switch (saving and
  62. * restoring of registers). Written in asm code as direct register access is
  63. * required.
  64. */
  65. #if ( configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H == 1 )
  66. R_BSP_PRAGMA_INTERRUPT( vSoftwareInterruptISR, VECT( ICU, SWINT ) )
  67. R_BSP_ATTRIB_INTERRUPT void vSoftwareInterruptISR( void ) __attribute__( ( naked ) );
  68. #else /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
  69. void vSoftwareInterruptISR( void ) __attribute__( ( naked ) );
  70. #endif /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
  71. /*
  72. * The tick ISR handler. The peripheral used is configured by the application
  73. * via a hook/callback function.
  74. */
  75. #if ( configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H == 1 )
  76. R_BSP_PRAGMA_INTERRUPT( vTickISR, _VECT( configTICK_VECTOR ) )
  77. R_BSP_ATTRIB_INTERRUPT void vTickISR( void ); /* Do not add __attribute__( ( interrupt ) ). */
  78. #else /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
  79. void vTickISR( void ) __attribute__( ( interrupt ) );
  80. #endif /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
  81. /*-----------------------------------------------------------*/
  82. extern void * pxCurrentTCB;
  83. /*-----------------------------------------------------------*/
  84. /*
  85. * See header file for description.
  86. */
  87. StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
  88. TaskFunction_t pxCode,
  89. void * pvParameters )
  90. {
  91. /* R0 is not included as it is the stack pointer. */
  92. *pxTopOfStack = 0x00;
  93. pxTopOfStack--;
  94. *pxTopOfStack = portINITIAL_PSW;
  95. pxTopOfStack--;
  96. *pxTopOfStack = ( StackType_t ) pxCode;
  97. /* When debugging it can be useful if every register is set to a known
  98. * value. Otherwise code space can be saved by just setting the registers
  99. * that need to be set. */
  100. #ifdef USE_FULL_REGISTER_INITIALISATION
  101. {
  102. pxTopOfStack--;
  103. *pxTopOfStack = 0xffffffff; /* r15. */
  104. pxTopOfStack--;
  105. *pxTopOfStack = 0xeeeeeeee;
  106. pxTopOfStack--;
  107. *pxTopOfStack = 0xdddddddd;
  108. pxTopOfStack--;
  109. *pxTopOfStack = 0xcccccccc;
  110. pxTopOfStack--;
  111. *pxTopOfStack = 0xbbbbbbbb;
  112. pxTopOfStack--;
  113. *pxTopOfStack = 0xaaaaaaaa;
  114. pxTopOfStack--;
  115. *pxTopOfStack = 0x99999999;
  116. pxTopOfStack--;
  117. *pxTopOfStack = 0x88888888;
  118. pxTopOfStack--;
  119. *pxTopOfStack = 0x77777777;
  120. pxTopOfStack--;
  121. *pxTopOfStack = 0x66666666;
  122. pxTopOfStack--;
  123. *pxTopOfStack = 0x55555555;
  124. pxTopOfStack--;
  125. *pxTopOfStack = 0x44444444;
  126. pxTopOfStack--;
  127. *pxTopOfStack = 0x33333333;
  128. pxTopOfStack--;
  129. *pxTopOfStack = 0x22222222;
  130. pxTopOfStack--;
  131. }
  132. #else /* ifdef USE_FULL_REGISTER_INITIALISATION */
  133. {
  134. pxTopOfStack -= 15;
  135. }
  136. #endif /* ifdef USE_FULL_REGISTER_INITIALISATION */
  137. *pxTopOfStack = ( StackType_t ) pvParameters; /* R1 */
  138. pxTopOfStack--;
  139. *pxTopOfStack = portINITIAL_FPSW;
  140. pxTopOfStack--;
  141. *pxTopOfStack = 0x12345678; /* Accumulator. */
  142. pxTopOfStack--;
  143. *pxTopOfStack = 0x87654321; /* Accumulator. */
  144. return pxTopOfStack;
  145. }
  146. /*-----------------------------------------------------------*/
  147. BaseType_t xPortStartScheduler( void )
  148. {
  149. extern void vApplicationSetupTimerInterrupt( void );
  150. /* Use pxCurrentTCB just so it does not get optimised away. */
  151. if( pxCurrentTCB != NULL )
  152. {
  153. /* Call an application function to set up the timer that will generate the
  154. * tick interrupt. This way the application can decide which peripheral to
  155. * use. A demo application is provided to show a suitable example. */
  156. vApplicationSetupTimerInterrupt();
  157. /* Enable the software interrupt. */
  158. _IEN( _ICU_SWINT ) = 1;
  159. /* Ensure the software interrupt is clear. */
  160. _IR( _ICU_SWINT ) = 0;
  161. /* Ensure the software interrupt is set to the kernel priority. */
  162. _IPR( _ICU_SWINT ) = configKERNEL_INTERRUPT_PRIORITY;
  163. /* Start the first task. */
  164. prvStartFirstTask();
  165. }
  166. /* Should not get here. */
  167. return pdFAIL;
  168. }
  169. /*-----------------------------------------------------------*/
  170. void vPortEndScheduler( void )
  171. {
  172. /* Not implemented in ports where there is nothing to return to.
  173. * Artificially force an assert. */
  174. configASSERT( pxCurrentTCB == NULL );
  175. }
  176. /*-----------------------------------------------------------*/
  177. static void prvStartFirstTask( void )
  178. {
  179. __asm volatile
  180. (
  181. /* When starting the scheduler there is nothing that needs moving to the
  182. * interrupt stack because the function is not called from an interrupt.
  183. * Just ensure the current stack is the user stack. */
  184. "SETPSW U \n" \
  185. /* Obtain the location of the stack associated with which ever task
  186. * pxCurrentTCB is currently pointing to. */
  187. "MOV.L #_pxCurrentTCB, R15 \n" \
  188. "MOV.L [R15], R15 \n" \
  189. "MOV.L [R15], R0 \n" \
  190. /* Restore the registers from the stack of the task pointed to by
  191. * pxCurrentTCB. */
  192. "POP R15 \n" \
  193. /* Accumulator low 32 bits. */
  194. "MVTACLO R15 \n" \
  195. "POP R15 \n" \
  196. /* Accumulator high 32 bits. */
  197. "MVTACHI R15 \n" \
  198. "POP R15 \n" \
  199. /* Floating point status word. */
  200. "MVTC R15, FPSW \n" \
  201. /* R1 to R15 - R0 is not included as it is the SP. */
  202. "POPM R1-R15 \n" \
  203. /* This pops the remaining registers. */
  204. "RTE \n" \
  205. "NOP \n" \
  206. "NOP \n"
  207. );
  208. }
  209. /*-----------------------------------------------------------*/
  210. void vSoftwareInterruptISR( void )
  211. {
  212. __asm volatile
  213. (
  214. /* Re-enable interrupts. */
  215. "SETPSW I \n" \
  216. /* Move the data that was automatically pushed onto the interrupt stack when
  217. * the interrupt occurred from the interrupt stack to the user stack.
  218. *
  219. * R15 is saved before it is clobbered. */
  220. "PUSH.L R15 \n" \
  221. /* Read the user stack pointer. */
  222. "MVFC USP, R15 \n" \
  223. /* Move the address down to the data being moved. */
  224. "SUB #12, R15 \n" \
  225. "MVTC R15, USP \n" \
  226. /* Copy the data across, R15, then PC, then PSW. */
  227. "MOV.L [ R0 ], [ R15 ] \n" \
  228. "MOV.L 4[ R0 ], 4[ R15 ] \n" \
  229. "MOV.L 8[ R0 ], 8[ R15 ] \n" \
  230. /* Move the interrupt stack pointer to its new correct position. */
  231. "ADD #12, R0 \n" \
  232. /* All the rest of the registers are saved directly to the user stack. */
  233. "SETPSW U \n" \
  234. /* Save the rest of the general registers (R15 has been saved already). */
  235. "PUSHM R1-R14 \n" \
  236. /* Save the FPSW and accumulator. */
  237. "MVFC FPSW, R15 \n" \
  238. "PUSH.L R15 \n" \
  239. "MVFACHI R15 \n" \
  240. "PUSH.L R15 \n" \
  241. /* Middle word. */
  242. "MVFACMI R15 \n" \
  243. /* Shifted left as it is restored to the low order word. */
  244. "SHLL #16, R15 \n" \
  245. "PUSH.L R15 \n" \
  246. /* Save the stack pointer to the TCB. */
  247. "MOV.L #_pxCurrentTCB, R15 \n" \
  248. "MOV.L [ R15 ], R15 \n" \
  249. "MOV.L R0, [ R15 ] \n" \
  250. /* Ensure the interrupt mask is set to the syscall priority while the kernel
  251. * structures are being accessed. */
  252. "MVTIPL %0 \n" \
  253. /* Select the next task to run. */
  254. "BSR.A _vTaskSwitchContext \n" \
  255. /* Reset the interrupt mask as no more data structure access is required. */
  256. "MVTIPL %1 \n" \
  257. /* Load the stack pointer of the task that is now selected as the Running
  258. * state task from its TCB. */
  259. "MOV.L #_pxCurrentTCB,R15 \n" \
  260. "MOV.L [ R15 ], R15 \n" \
  261. "MOV.L [ R15 ], R0 \n" \
  262. /* Restore the context of the new task. The PSW (Program Status Word) and
  263. * PC will be popped by the RTE instruction. */
  264. "POP R15 \n" \
  265. "MVTACLO R15 \n" \
  266. "POP R15 \n" \
  267. "MVTACHI R15 \n" \
  268. "POP R15 \n" \
  269. "MVTC R15, FPSW \n" \
  270. "POPM R1-R15 \n" \
  271. "RTE \n" \
  272. "NOP \n" \
  273. "NOP "
  274. ::"i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY ), "i" ( configKERNEL_INTERRUPT_PRIORITY )
  275. );
  276. }
  277. /*-----------------------------------------------------------*/
  278. void vTickISR( void )
  279. {
  280. /* Re-enabled interrupts. */
  281. __asm volatile ( "SETPSW I" );
  282. /* Increment the tick, and perform any processing the new tick value
  283. * necessitates. Ensure IPL is at the max syscall value first. */
  284. portDISABLE_INTERRUPTS_FROM_KERNEL_ISR();
  285. {
  286. if( xTaskIncrementTick() != pdFALSE )
  287. {
  288. taskYIELD();
  289. }
  290. }
  291. portENABLE_INTERRUPTS_FROM_KERNEL_ISR();
  292. }
  293. /*-----------------------------------------------------------*/
  294. uint32_t ulPortGetIPL( void )
  295. {
  296. __asm volatile
  297. (
  298. "MVFC PSW, R1 \n" \
  299. "SHLR #24, R1 \n" \
  300. "RTS "
  301. );
  302. /* This will never get executed, but keeps the compiler from complaining. */
  303. return 0;
  304. }
  305. /*-----------------------------------------------------------*/
  306. void vPortSetIPL( uint32_t ulNewIPL )
  307. {
  308. /* Avoid compiler warning about unreferenced parameter. */
  309. ( void ) ulNewIPL;
  310. __asm volatile
  311. (
  312. "PUSH R5 \n" \
  313. "MVFC PSW, R5 \n" \
  314. "SHLL #24, R1 \n" \
  315. "AND #-0F000001H, R5 \n" \
  316. "OR R1, R5 \n" \
  317. "MVTC R5, PSW \n" \
  318. "POP R5 \n" \
  319. "RTS "
  320. );
  321. }