port.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 RX600 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 defined( configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H ) && ( configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H == 1 )
  38. #include "platform.h"
  39. #else
  40. #include "iodefine.h"
  41. #endif
  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. /*-----------------------------------------------------------*/
  48. /* The following lines are to ensure vSoftwareInterruptEntry can be referenced,
  49. and therefore installed in the vector table, when the FreeRTOS code is built
  50. as a library. */
  51. extern BaseType_t vSoftwareInterruptEntry;
  52. const BaseType_t * p_vSoftwareInterruptEntry = &vSoftwareInterruptEntry;
  53. /*-----------------------------------------------------------*/
  54. /*
  55. * Function to start the first task executing - written in asm code as direct
  56. * access to registers is required.
  57. */
  58. static void prvStartFirstTask( void );
  59. /*
  60. * Software interrupt handler. Performs the actual context switch (saving and
  61. * restoring of registers). Written in asm code as direct register access is
  62. * required.
  63. */
  64. static void prvYieldHandler( void );
  65. /*
  66. * The entry point for the software interrupt handler. This is the function
  67. * that calls the inline asm function prvYieldHandler(). It is installed in
  68. * the vector table, but the code that installs it is in prvYieldHandler rather
  69. * than using a #pragma.
  70. */
  71. void vSoftwareInterruptISR( void );
  72. /*-----------------------------------------------------------*/
  73. /* This is accessed by the inline assembler functions so is file scope for
  74. convenience. */
  75. extern void *pxCurrentTCB;
  76. extern void vTaskSwitchContext( void );
  77. /*-----------------------------------------------------------*/
  78. /*
  79. * See header file for description.
  80. */
  81. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  82. {
  83. /* R0 is not included as it is the stack pointer. */
  84. *pxTopOfStack = 0x00;
  85. pxTopOfStack--;
  86. *pxTopOfStack = portINITIAL_PSW;
  87. pxTopOfStack--;
  88. *pxTopOfStack = ( StackType_t ) pxCode;
  89. /* When debugging it can be useful if every register is set to a known
  90. value. Otherwise code space can be saved by just setting the registers
  91. that need to be set. */
  92. #ifdef USE_FULL_REGISTER_INITIALISATION
  93. {
  94. pxTopOfStack--;
  95. *pxTopOfStack = 0xffffffff; /* r15. */
  96. pxTopOfStack--;
  97. *pxTopOfStack = 0xeeeeeeee;
  98. pxTopOfStack--;
  99. *pxTopOfStack = 0xdddddddd;
  100. pxTopOfStack--;
  101. *pxTopOfStack = 0xcccccccc;
  102. pxTopOfStack--;
  103. *pxTopOfStack = 0xbbbbbbbb;
  104. pxTopOfStack--;
  105. *pxTopOfStack = 0xaaaaaaaa;
  106. pxTopOfStack--;
  107. *pxTopOfStack = 0x99999999;
  108. pxTopOfStack--;
  109. *pxTopOfStack = 0x88888888;
  110. pxTopOfStack--;
  111. *pxTopOfStack = 0x77777777;
  112. pxTopOfStack--;
  113. *pxTopOfStack = 0x66666666;
  114. pxTopOfStack--;
  115. *pxTopOfStack = 0x55555555;
  116. pxTopOfStack--;
  117. *pxTopOfStack = 0x44444444;
  118. pxTopOfStack--;
  119. *pxTopOfStack = 0x33333333;
  120. pxTopOfStack--;
  121. *pxTopOfStack = 0x22222222;
  122. pxTopOfStack--;
  123. }
  124. #else
  125. {
  126. pxTopOfStack -= 15;
  127. }
  128. #endif
  129. *pxTopOfStack = ( StackType_t ) pvParameters; /* R1 */
  130. pxTopOfStack--;
  131. *pxTopOfStack = portINITIAL_FPSW;
  132. pxTopOfStack--;
  133. *pxTopOfStack = 0x11111111; /* Accumulator 0. */
  134. pxTopOfStack--;
  135. *pxTopOfStack = 0x22222222; /* Accumulator 0. */
  136. pxTopOfStack--;
  137. *pxTopOfStack = 0x33333333; /* Accumulator 0. */
  138. pxTopOfStack--;
  139. *pxTopOfStack = 0x44444444; /* Accumulator 1. */
  140. pxTopOfStack--;
  141. *pxTopOfStack = 0x55555555; /* Accumulator 1. */
  142. pxTopOfStack--;
  143. *pxTopOfStack = 0x66666666; /* Accumulator 1. */
  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. /* Just to make sure the function is not optimised away. */
  167. ( void ) vSoftwareInterruptISR();
  168. /* Should not get here. */
  169. return pdFAIL;
  170. }
  171. /*-----------------------------------------------------------*/
  172. #pragma inline_asm prvStartFirstTask
  173. static void prvStartFirstTask( void )
  174. {
  175. /* When starting the scheduler there is nothing that needs moving to the
  176. interrupt stack because the function is not called from an interrupt.
  177. Just ensure the current stack is the user stack. */
  178. SETPSW U
  179. /* Obtain the location of the stack associated with which ever task
  180. pxCurrentTCB is currently pointing to. */
  181. MOV.L #_pxCurrentTCB, R15
  182. MOV.L [R15], R15
  183. MOV.L [R15], R0
  184. /* Restore the registers from the stack of the task pointed to by
  185. pxCurrentTCB. */
  186. POP R15
  187. MVTACLO R15, A0 /* Accumulator low 32 bits. */
  188. POP R15
  189. MVTACHI R15, A0 /* Accumulator high 32 bits. */
  190. POP R15
  191. MVTACGU R15, A0 /* Accumulator guard. */
  192. POP R15
  193. MVTACLO R15, A1 /* Accumulator low 32 bits. */
  194. POP R15
  195. MVTACHI R15, A1 /* Accumulator high 32 bits. */
  196. POP R15
  197. MVTACGU R15, A1 /* Accumulator guard. */
  198. POP R15
  199. MVTC R15,FPSW /* Floating point status word. */
  200. POPM R1-R15 /* R1 to R15 - R0 is not included as it is the SP. */
  201. RTE /* This pops the remaining registers. */
  202. NOP
  203. NOP
  204. }
  205. /*-----------------------------------------------------------*/
  206. #pragma interrupt ( vTickISR( vect = _VECT( configTICK_VECTOR ), enable ) )
  207. void vTickISR( void )
  208. {
  209. /* Increment the tick, and perform any processing the new tick value
  210. necessitates. */
  211. set_ipl( configMAX_SYSCALL_INTERRUPT_PRIORITY );
  212. {
  213. if( xTaskIncrementTick() != pdFALSE )
  214. {
  215. taskYIELD();
  216. }
  217. }
  218. set_ipl( configKERNEL_INTERRUPT_PRIORITY );
  219. }
  220. /*-----------------------------------------------------------*/
  221. void vSoftwareInterruptISR( void )
  222. {
  223. prvYieldHandler();
  224. }
  225. /*-----------------------------------------------------------*/
  226. #pragma inline_asm prvYieldHandler
  227. static void prvYieldHandler( void )
  228. {
  229. /* Re-enable interrupts. */
  230. SETPSW I
  231. /* Move the data that was automatically pushed onto the interrupt stack when
  232. the interrupt occurred from the interrupt stack to the user stack.
  233. R15 is saved before it is clobbered. */
  234. PUSH.L R15
  235. /* Read the user stack pointer. */
  236. MVFC USP, R15
  237. /* Move the address down to the data being moved. */
  238. SUB #12, R15
  239. MVTC R15, USP
  240. /* Copy the data across. */
  241. MOV.L [ R0 ], [ R15 ] ; R15
  242. MOV.L 4[ R0 ], 4[ R15 ] ; PC
  243. MOV.L 8[ R0 ], 8[ R15 ] ; PSW
  244. /* Move the interrupt stack pointer to its new correct position. */
  245. ADD #12, R0
  246. /* All the rest of the registers are saved directly to the user stack. */
  247. SETPSW U
  248. /* Save the rest of the general registers (R15 has been saved already). */
  249. PUSHM R1-R14
  250. /* Save the FPSW and accumulators. */
  251. MVFC FPSW, R15
  252. PUSH.L R15
  253. MVFACGU #0, A1, R15
  254. PUSH.L R15
  255. MVFACHI #0, A1, R15
  256. PUSH.L R15
  257. MVFACLO #0, A1, R15 ; Low order word.
  258. PUSH.L R15
  259. MVFACGU #0, A0, R15
  260. PUSH.L R15
  261. MVFACHI #0, A0, R15
  262. PUSH.L R15
  263. MVFACLO #0, A0, R15 ; Low order word.
  264. PUSH.L R15
  265. /* Save the stack pointer to the TCB. */
  266. MOV.L #_pxCurrentTCB, R15
  267. MOV.L [ R15 ], R15
  268. MOV.L R0, [ R15 ]
  269. /* Ensure the interrupt mask is set to the syscall priority while the kernel
  270. structures are being accessed. */
  271. MVTIPL #configMAX_SYSCALL_INTERRUPT_PRIORITY
  272. /* Select the next task to run. */
  273. BSR.A _vTaskSwitchContext
  274. /* Reset the interrupt mask as no more data structure access is required. */
  275. MVTIPL #configKERNEL_INTERRUPT_PRIORITY
  276. /* Load the stack pointer of the task that is now selected as the Running
  277. state task from its TCB. */
  278. MOV.L #_pxCurrentTCB,R15
  279. MOV.L [ R15 ], R15
  280. MOV.L [ R15 ], R0
  281. /* Restore the context of the new task. The PSW (Program Status Word) and
  282. PC will be popped by the RTE instruction. */
  283. POP R15
  284. MVTACLO R15, A0 /* Accumulator low 32 bits. */
  285. POP R15
  286. MVTACHI R15, A0 /* Accumulator high 32 bits. */
  287. POP R15
  288. MVTACGU R15, A0 /* Accumulator guard. */
  289. POP R15
  290. MVTACLO R15, A1 /* Accumulator low 32 bits. */
  291. POP R15
  292. MVTACHI R15, A1 /* Accumulator high 32 bits. */
  293. POP R15
  294. MVTACGU R15, A1 /* Accumulator guard. */
  295. POP R15
  296. MVTC R15,FPSW
  297. POPM R1-R15
  298. RTE
  299. NOP
  300. NOP
  301. }
  302. /*-----------------------------------------------------------*/
  303. void vPortEndScheduler( void )
  304. {
  305. /* Not implemented in ports where there is nothing to return to.
  306. Artificially force an assert. */
  307. configASSERT( pxCurrentTCB == NULL );
  308. /* The following line is just to prevent the symbol getting optimised away. */
  309. ( void ) vTaskSwitchContext();
  310. }
  311. /*-----------------------------------------------------------*/