port.c 9.4 KB

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