port.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 portMASK_INTERRUPTS_FROM_KERNEL_ISR() __asm volatile ( "MVTIPL %0" ::"i"(configMAX_SYSCALL_INTERRUPT_PRIORITY) )
  53. #define portUNMASK_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 = 0x11111111; /* Accumulator 0. */
  140. pxTopOfStack--;
  141. *pxTopOfStack = 0x22222222; /* Accumulator 0. */
  142. pxTopOfStack--;
  143. *pxTopOfStack = 0x33333333; /* Accumulator 0. */
  144. pxTopOfStack--;
  145. *pxTopOfStack = 0x44444444; /* Accumulator 1. */
  146. pxTopOfStack--;
  147. *pxTopOfStack = 0x55555555; /* Accumulator 1. */
  148. pxTopOfStack--;
  149. *pxTopOfStack = 0x66666666; /* Accumulator 1. */
  150. return pxTopOfStack;
  151. }
  152. /*-----------------------------------------------------------*/
  153. BaseType_t xPortStartScheduler( void )
  154. {
  155. extern void vApplicationSetupTimerInterrupt( void );
  156. /* Use pxCurrentTCB just so it does not get optimised away. */
  157. if( pxCurrentTCB != NULL )
  158. {
  159. /* Call an application function to set up the timer that will generate the
  160. tick interrupt. This way the application can decide which peripheral to
  161. use. A demo application is provided to show a suitable example. */
  162. vApplicationSetupTimerInterrupt();
  163. /* Enable the software interrupt. */
  164. _IEN( _ICU_SWINT ) = 1;
  165. /* Ensure the software interrupt is clear. */
  166. _IR( _ICU_SWINT ) = 0;
  167. /* Ensure the software interrupt is set to the kernel priority. */
  168. _IPR( _ICU_SWINT ) = configKERNEL_INTERRUPT_PRIORITY;
  169. /* Start the first task. */
  170. prvStartFirstTask();
  171. }
  172. /* Should not get here. */
  173. return pdFAIL;
  174. }
  175. /*-----------------------------------------------------------*/
  176. void vPortEndScheduler( void )
  177. {
  178. /* Not implemented in ports where there is nothing to return to.
  179. Artificially force an assert. */
  180. configASSERT( pxCurrentTCB == NULL );
  181. }
  182. /*-----------------------------------------------------------*/
  183. static void prvStartFirstTask( void )
  184. {
  185. __asm volatile
  186. (
  187. /* When starting the scheduler there is nothing that needs moving to the
  188. interrupt stack because the function is not called from an interrupt.
  189. Just ensure the current stack is the user stack. */
  190. "SETPSW U \n" \
  191. /* Obtain the location of the stack associated with which ever task
  192. pxCurrentTCB is currently pointing to. */
  193. "MOV.L #_pxCurrentTCB, R15 \n" \
  194. "MOV.L [R15], R15 \n" \
  195. "MOV.L [R15], R0 \n" \
  196. /* Restore the registers from the stack of the task pointed to by
  197. pxCurrentTCB. */
  198. "POP R15 \n" \
  199. /* Accumulator low 32 bits. */
  200. "MVTACLO R15, A0 \n" \
  201. "POP R15 \n" \
  202. /* Accumulator high 32 bits. */
  203. "MVTACHI R15, A0 \n" \
  204. "POP R15 \n" \
  205. /* Accumulator guard. */
  206. "MVTACGU R15, A0 \n" \
  207. "POP R15 \n" \
  208. /* Accumulator low 32 bits. */
  209. "MVTACLO R15, A1 \n" \
  210. "POP R15 \n" \
  211. /* Accumulator high 32 bits. */
  212. "MVTACHI R15, A1 \n" \
  213. "POP R15 \n" \
  214. /* Accumulator guard. */
  215. "MVTACGU R15, A1 \n" \
  216. "POP R15 \n" \
  217. /* Floating point status word. */
  218. "MVTC R15, FPSW \n" \
  219. /* R1 to R15 - R0 is not included as it is the SP. */
  220. "POPM R1-R15 \n" \
  221. /* This pops the remaining registers. */
  222. "RTE \n" \
  223. "NOP \n" \
  224. "NOP \n"
  225. );
  226. }
  227. /*-----------------------------------------------------------*/
  228. void vSoftwareInterruptISR( void )
  229. {
  230. __asm volatile
  231. (
  232. /* Re-enable interrupts. */
  233. "SETPSW I \n" \
  234. /* Move the data that was automatically pushed onto the interrupt stack when
  235. the interrupt occurred from the interrupt stack to the user stack.
  236. R15 is saved before it is clobbered. */
  237. "PUSH.L R15 \n" \
  238. /* Read the user stack pointer. */
  239. "MVFC USP, R15 \n" \
  240. /* Move the address down to the data being moved. */
  241. "SUB #12, R15 \n" \
  242. "MVTC R15, USP \n" \
  243. /* Copy the data across, R15, then PC, then PSW. */
  244. "MOV.L [ R0 ], [ R15 ] \n" \
  245. "MOV.L 4[ R0 ], 4[ R15 ] \n" \
  246. "MOV.L 8[ R0 ], 8[ R15 ] \n" \
  247. /* Move the interrupt stack pointer to its new correct position. */
  248. "ADD #12, R0 \n" \
  249. /* All the rest of the registers are saved directly to the user stack. */
  250. "SETPSW U \n" \
  251. /* Save the rest of the general registers (R15 has been saved already). */
  252. "PUSHM R1-R14 \n" \
  253. /* Save the FPSW and accumulator. */
  254. "MVFC FPSW, R15 \n" \
  255. "PUSH.L R15 \n" \
  256. "MVFACGU #0, A1, R15 \n" \
  257. "PUSH.L R15 \n" \
  258. "MVFACHI #0, A1, R15 \n" \
  259. "PUSH.L R15 \n" \
  260. /* Low order word. */
  261. "MVFACLO #0, A1, R15 \n" \
  262. "PUSH.L R15 \n" \
  263. "MVFACGU #0, A0, R15 \n" \
  264. "PUSH.L R15 \n" \
  265. "MVFACHI #0, A0, R15 \n" \
  266. "PUSH.L R15 \n" \
  267. /* Low order word. */
  268. "MVFACLO #0, A0, R15 \n" \
  269. "PUSH.L R15 \n" \
  270. /* Save the stack pointer to the TCB. */
  271. "MOV.L #_pxCurrentTCB, R15 \n" \
  272. "MOV.L [ R15 ], R15 \n" \
  273. "MOV.L R0, [ R15 ] \n" \
  274. /* Ensure the interrupt mask is set to the syscall priority while the kernel
  275. structures are being accessed. */
  276. "MVTIPL %0 \n" \
  277. /* Select the next task to run. */
  278. "BSR.A _vTaskSwitchContext \n" \
  279. /* Reset the interrupt mask as no more data structure access is required. */
  280. "MVTIPL %1 \n" \
  281. /* Load the stack pointer of the task that is now selected as the Running
  282. state task from its TCB. */
  283. "MOV.L #_pxCurrentTCB,R15 \n" \
  284. "MOV.L [ R15 ], R15 \n" \
  285. "MOV.L [ R15 ], R0 \n" \
  286. /* Restore the context of the new task. The PSW (Program Status Word) and
  287. PC will be popped by the RTE instruction. */
  288. "POP R15 \n" \
  289. /* Accumulator low 32 bits. */
  290. "MVTACLO R15, A0 \n" \
  291. "POP R15 \n" \
  292. /* Accumulator high 32 bits. */
  293. "MVTACHI R15, A0 \n" \
  294. "POP R15 \n" \
  295. /* Accumulator guard. */
  296. "MVTACGU R15, A0 \n" \
  297. "POP R15 \n" \
  298. /* Accumulator low 32 bits. */
  299. "MVTACLO R15, A1 \n" \
  300. "POP R15 \n" \
  301. /* Accumulator high 32 bits. */
  302. "MVTACHI R15, A1 \n" \
  303. "POP R15 \n" \
  304. /* Accumulator guard. */
  305. "MVTACGU R15, A1 \n" \
  306. "POP R15 \n" \
  307. "MVTC R15, FPSW \n" \
  308. "POPM R1-R15 \n" \
  309. "RTE \n" \
  310. "NOP \n" \
  311. "NOP "
  312. :: "i"(configMAX_SYSCALL_INTERRUPT_PRIORITY), "i"(configKERNEL_INTERRUPT_PRIORITY)
  313. );
  314. }
  315. /*-----------------------------------------------------------*/
  316. void vTickISR( void )
  317. {
  318. /* Re-enabled interrupts. */
  319. __asm volatile( "SETPSW I" );
  320. /* Increment the tick, and perform any processing the new tick value
  321. necessitates. Ensure IPL is at the max syscall value first. */
  322. portMASK_INTERRUPTS_FROM_KERNEL_ISR();
  323. {
  324. if( xTaskIncrementTick() != pdFALSE )
  325. {
  326. taskYIELD();
  327. }
  328. }
  329. portUNMASK_INTERRUPTS_FROM_KERNEL_ISR();
  330. }
  331. /*-----------------------------------------------------------*/
  332. uint32_t ulPortGetIPL( void )
  333. {
  334. __asm volatile
  335. (
  336. "MVFC PSW, R1 \n" \
  337. "SHLR #24, R1 \n" \
  338. "RTS "
  339. );
  340. /* This will never get executed, but keeps the compiler from complaining. */
  341. return 0;
  342. }
  343. /*-----------------------------------------------------------*/
  344. void vPortSetIPL( uint32_t ulNewIPL )
  345. {
  346. /* Avoid compiler warning about unreferenced parameter. */
  347. ( void ) ulNewIPL;
  348. __asm volatile
  349. (
  350. "PUSH R5 \n" \
  351. "MVFC PSW, R5 \n" \
  352. "SHLL #24, R1 \n" \
  353. "AND #-0F000001H, R5 \n" \
  354. "OR R1, R5 \n" \
  355. "MVTC R5, PSW \n" \
  356. "POP R5 \n" \
  357. "RTS "
  358. );
  359. }