port.c 16 KB

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