port.c 9.5 KB

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