port.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. /* 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, TaskFunction_t pxCode, void *pvParameters )
  88. {
  89. /* R0 is not included as it is the stack pointer. */
  90. *pxTopOfStack = 0x00;
  91. pxTopOfStack--;
  92. *pxTopOfStack = portINITIAL_PSW;
  93. pxTopOfStack--;
  94. *pxTopOfStack = ( StackType_t ) pxCode;
  95. /* When debugging it can be useful if every register is set to a known
  96. value. Otherwise code space can be saved by just setting the registers
  97. that need to be set. */
  98. #ifdef USE_FULL_REGISTER_INITIALISATION
  99. {
  100. pxTopOfStack--;
  101. *pxTopOfStack = 0xffffffff; /* r15. */
  102. pxTopOfStack--;
  103. *pxTopOfStack = 0xeeeeeeee;
  104. pxTopOfStack--;
  105. *pxTopOfStack = 0xdddddddd;
  106. pxTopOfStack--;
  107. *pxTopOfStack = 0xcccccccc;
  108. pxTopOfStack--;
  109. *pxTopOfStack = 0xbbbbbbbb;
  110. pxTopOfStack--;
  111. *pxTopOfStack = 0xaaaaaaaa;
  112. pxTopOfStack--;
  113. *pxTopOfStack = 0x99999999;
  114. pxTopOfStack--;
  115. *pxTopOfStack = 0x88888888;
  116. pxTopOfStack--;
  117. *pxTopOfStack = 0x77777777;
  118. pxTopOfStack--;
  119. *pxTopOfStack = 0x66666666;
  120. pxTopOfStack--;
  121. *pxTopOfStack = 0x55555555;
  122. pxTopOfStack--;
  123. *pxTopOfStack = 0x44444444;
  124. pxTopOfStack--;
  125. *pxTopOfStack = 0x33333333;
  126. pxTopOfStack--;
  127. *pxTopOfStack = 0x22222222;
  128. pxTopOfStack--;
  129. }
  130. #else
  131. {
  132. pxTopOfStack -= 15;
  133. }
  134. #endif
  135. *pxTopOfStack = ( StackType_t ) pvParameters; /* R1 */
  136. pxTopOfStack--;
  137. *pxTopOfStack = portINITIAL_FPSW;
  138. pxTopOfStack--;
  139. *pxTopOfStack = 0x12345678; /* Accumulator. */
  140. pxTopOfStack--;
  141. *pxTopOfStack = 0x87654321; /* Accumulator. */
  142. return pxTopOfStack;
  143. }
  144. /*-----------------------------------------------------------*/
  145. BaseType_t xPortStartScheduler( void )
  146. {
  147. extern void vApplicationSetupTimerInterrupt( void );
  148. /* Use pxCurrentTCB just so it does not get optimised away. */
  149. if( pxCurrentTCB != NULL )
  150. {
  151. /* Call an application function to set up the timer that will generate the
  152. tick interrupt. This way the application can decide which peripheral to
  153. use. A demo application is provided to show a suitable example. */
  154. vApplicationSetupTimerInterrupt();
  155. /* Enable the software interrupt. */
  156. _IEN( _ICU_SWINT ) = 1;
  157. /* Ensure the software interrupt is clear. */
  158. _IR( _ICU_SWINT ) = 0;
  159. /* Ensure the software interrupt is set to the kernel priority. */
  160. _IPR( _ICU_SWINT ) = configKERNEL_INTERRUPT_PRIORITY;
  161. /* Start the first task. */
  162. prvStartFirstTask();
  163. }
  164. /* Should not get here. */
  165. return pdFAIL;
  166. }
  167. /*-----------------------------------------------------------*/
  168. void vPortEndScheduler( void )
  169. {
  170. /* Not implemented in ports where there is nothing to return to.
  171. Artificially force an assert. */
  172. configASSERT( pxCurrentTCB == NULL );
  173. }
  174. /*-----------------------------------------------------------*/
  175. static void prvStartFirstTask( void )
  176. {
  177. __asm volatile
  178. (
  179. /* When starting the scheduler there is nothing that needs moving to the
  180. interrupt stack because the function is not called from an interrupt.
  181. Just ensure the current stack is the user stack. */
  182. "SETPSW U \n" \
  183. /* Obtain the location of the stack associated with which ever task
  184. pxCurrentTCB is currently pointing to. */
  185. "MOV.L #_pxCurrentTCB, R15 \n" \
  186. "MOV.L [R15], R15 \n" \
  187. "MOV.L [R15], R0 \n" \
  188. /* Restore the registers from the stack of the task pointed to by
  189. pxCurrentTCB. */
  190. "POP R15 \n" \
  191. /* Accumulator low 32 bits. */
  192. "MVTACLO R15 \n" \
  193. "POP R15 \n" \
  194. /* Accumulator high 32 bits. */
  195. "MVTACHI R15 \n" \
  196. "POP R15 \n" \
  197. /* Floating point status word. */
  198. "MVTC R15, FPSW \n" \
  199. /* R1 to R15 - R0 is not included as it is the SP. */
  200. "POPM R1-R15 \n" \
  201. /* This pops the remaining registers. */
  202. "RTE \n" \
  203. "NOP \n" \
  204. "NOP \n"
  205. );
  206. }
  207. /*-----------------------------------------------------------*/
  208. void vSoftwareInterruptISR( void )
  209. {
  210. __asm volatile
  211. (
  212. /* Re-enable interrupts. */
  213. "SETPSW I \n" \
  214. /* Move the data that was automatically pushed onto the interrupt stack when
  215. the interrupt occurred from the interrupt stack to the user stack.
  216. R15 is saved before it is clobbered. */
  217. "PUSH.L R15 \n" \
  218. /* Read the user stack pointer. */
  219. "MVFC USP, R15 \n" \
  220. /* Move the address down to the data being moved. */
  221. "SUB #12, R15 \n" \
  222. "MVTC R15, USP \n" \
  223. /* Copy the data across, R15, then PC, then PSW. */
  224. "MOV.L [ R0 ], [ R15 ] \n" \
  225. "MOV.L 4[ R0 ], 4[ R15 ] \n" \
  226. "MOV.L 8[ R0 ], 8[ R15 ] \n" \
  227. /* Move the interrupt stack pointer to its new correct position. */
  228. "ADD #12, R0 \n" \
  229. /* All the rest of the registers are saved directly to the user stack. */
  230. "SETPSW U \n" \
  231. /* Save the rest of the general registers (R15 has been saved already). */
  232. "PUSHM R1-R14 \n" \
  233. /* Save the FPSW and accumulator. */
  234. "MVFC FPSW, R15 \n" \
  235. "PUSH.L R15 \n" \
  236. "MVFACHI R15 \n" \
  237. "PUSH.L R15 \n" \
  238. /* Middle word. */
  239. "MVFACMI R15 \n" \
  240. /* Shifted left as it is restored to the low order word. */
  241. "SHLL #16, R15 \n" \
  242. "PUSH.L R15 \n" \
  243. /* Save the stack pointer to the TCB. */
  244. "MOV.L #_pxCurrentTCB, R15 \n" \
  245. "MOV.L [ R15 ], R15 \n" \
  246. "MOV.L R0, [ R15 ] \n" \
  247. /* Ensure the interrupt mask is set to the syscall priority while the kernel
  248. structures are being accessed. */
  249. "MVTIPL %0 \n" \
  250. /* Select the next task to run. */
  251. "BSR.A _vTaskSwitchContext \n" \
  252. /* Reset the interrupt mask as no more data structure access is required. */
  253. "MVTIPL %1 \n" \
  254. /* Load the stack pointer of the task that is now selected as the Running
  255. state task from its TCB. */
  256. "MOV.L #_pxCurrentTCB,R15 \n" \
  257. "MOV.L [ R15 ], R15 \n" \
  258. "MOV.L [ R15 ], R0 \n" \
  259. /* Restore the context of the new task. The PSW (Program Status Word) and
  260. PC will be popped by the RTE instruction. */
  261. "POP R15 \n" \
  262. "MVTACLO R15 \n" \
  263. "POP R15 \n" \
  264. "MVTACHI R15 \n" \
  265. "POP R15 \n" \
  266. "MVTC R15, FPSW \n" \
  267. "POPM R1-R15 \n" \
  268. "RTE \n" \
  269. "NOP \n" \
  270. "NOP "
  271. :: "i"(configMAX_SYSCALL_INTERRUPT_PRIORITY), "i"(configKERNEL_INTERRUPT_PRIORITY)
  272. );
  273. }
  274. /*-----------------------------------------------------------*/
  275. void vTickISR( void )
  276. {
  277. /* Re-enabled interrupts. */
  278. __asm volatile( "SETPSW I" );
  279. /* Increment the tick, and perform any processing the new tick value
  280. necessitates. Ensure IPL is at the max syscall value first. */
  281. portDISABLE_INTERRUPTS_FROM_KERNEL_ISR();
  282. {
  283. if( xTaskIncrementTick() != pdFALSE )
  284. {
  285. taskYIELD();
  286. }
  287. }
  288. portENABLE_INTERRUPTS_FROM_KERNEL_ISR();
  289. }
  290. /*-----------------------------------------------------------*/
  291. uint32_t ulPortGetIPL( void )
  292. {
  293. __asm volatile
  294. (
  295. "MVFC PSW, R1 \n" \
  296. "SHLR #24, R1 \n" \
  297. "RTS "
  298. );
  299. /* This will never get executed, but keeps the compiler from complaining. */
  300. return 0;
  301. }
  302. /*-----------------------------------------------------------*/
  303. void vPortSetIPL( uint32_t ulNewIPL )
  304. {
  305. /* Avoid compiler warning about unreferenced parameter. */
  306. ( void ) ulNewIPL;
  307. __asm volatile
  308. (
  309. "PUSH R5 \n" \
  310. "MVFC PSW, R5 \n" \
  311. "SHLL #24, R1 \n" \
  312. "AND #-0F000001H, R5 \n" \
  313. "OR R1, R5 \n" \
  314. "MVTC R5, PSW \n" \
  315. "POP R5 \n" \
  316. "RTS "
  317. );
  318. }