2 * FreeRTOS Kernel V10.4.4
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
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:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
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.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
29 /*-----------------------------------------------------------
30 * Implementation of functions defined in portable.h for the RX200 port.
31 *----------------------------------------------------------*/
33 /* Scheduler includes. */
37 /* Library includes. */
40 /* Hardware specifics. */
43 /*-----------------------------------------------------------*/
45 /* Tasks should start with interrupts enabled and in Supervisor mode, therefore
46 PSW is set with U and I set, and PM and IPL clear. */
47 #define portINITIAL_PSW ( ( StackType_t ) 0x00030000 )
49 /*-----------------------------------------------------------*/
51 /* The following lines are to ensure vSoftwareInterruptEntry can be referenced,
52 and therefore installed in the vector table, when the FreeRTOS code is built
54 extern BaseType_t vSoftwareInterruptEntry;
55 const BaseType_t * p_vSoftwareInterruptEntry = &vSoftwareInterruptEntry;
57 /*-----------------------------------------------------------*/
60 * Function to start the first task executing - written in asm code as direct
61 * access to registers is required.
63 static void prvStartFirstTask( void );
66 * Software interrupt handler. Performs the actual context switch (saving and
67 * restoring of registers). Written in asm code as direct register access is
70 static void prvYieldHandler( void );
73 * The entry point for the software interrupt handler. This is the function
74 * that calls the inline asm function prvYieldHandler(). It is installed in
75 * the vector table, but the code that installs it is in prvYieldHandler rather
76 * than using a #pragma.
78 void vSoftwareInterruptISR( void );
80 /*-----------------------------------------------------------*/
82 /* This is accessed by the inline assembler functions so is file scope for
84 extern void *pxCurrentTCB;
85 extern void vTaskSwitchContext( void );
87 /*-----------------------------------------------------------*/
90 * See header file for description.
92 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
94 /* Offset to end up on 8 byte boundary. */
97 /* R0 is not included as it is the stack pointer. */
100 *pxTopOfStack = 0x00;
102 *pxTopOfStack = portINITIAL_PSW;
104 *pxTopOfStack = ( StackType_t ) pxCode;
106 /* When debugging it can be useful if every register is set to a known
107 value. Otherwise code space can be saved by just setting the registers
108 that need to be set. */
109 #ifdef USE_FULL_REGISTER_INITIALISATION
112 *pxTopOfStack = 0x12345678; /* r15. */
114 *pxTopOfStack = 0xaaaabbbb;
116 *pxTopOfStack = 0xdddddddd;
118 *pxTopOfStack = 0xcccccccc;
120 *pxTopOfStack = 0xbbbbbbbb;
122 *pxTopOfStack = 0xaaaaaaaa;
124 *pxTopOfStack = 0x99999999;
126 *pxTopOfStack = 0x88888888;
128 *pxTopOfStack = 0x77777777;
130 *pxTopOfStack = 0x66666666;
132 *pxTopOfStack = 0x55555555;
134 *pxTopOfStack = 0x44444444;
136 *pxTopOfStack = 0x33333333;
138 *pxTopOfStack = 0x22222222;
147 *pxTopOfStack = ( StackType_t ) pvParameters; /* R1 */
149 *pxTopOfStack = 0x12345678; /* Accumulator. */
151 *pxTopOfStack = 0x87654321; /* Accumulator. */
155 /*-----------------------------------------------------------*/
157 BaseType_t xPortStartScheduler( void )
159 extern void vApplicationSetupTimerInterrupt( void );
161 /* Use pxCurrentTCB just so it does not get optimised away. */
162 if( pxCurrentTCB != NULL )
164 /* Call an application function to set up the timer that will generate the
165 tick interrupt. This way the application can decide which peripheral to
166 use. A demo application is provided to show a suitable example. */
167 vApplicationSetupTimerInterrupt();
169 /* Enable the software interrupt. */
170 _IEN( _ICU_SWINT ) = 1;
172 /* Ensure the software interrupt is clear. */
173 _IR( _ICU_SWINT ) = 0;
175 /* Ensure the software interrupt is set to the kernel priority. */
176 _IPR( _ICU_SWINT ) = configKERNEL_INTERRUPT_PRIORITY;
178 /* Start the first task. */
182 /* Just to make sure the function is not optimised away. */
183 ( void ) vSoftwareInterruptISR();
185 /* Should not get here. */
188 /*-----------------------------------------------------------*/
190 #pragma inline_asm prvStartFirstTask
191 static void prvStartFirstTask( void )
193 /* When starting the scheduler there is nothing that needs moving to the
194 interrupt stack because the function is not called from an interrupt.
195 Just ensure the current stack is the user stack. */
198 /* Obtain the location of the stack associated with which ever task
199 pxCurrentTCB is currently pointing to. */
200 MOV.L #_pxCurrentTCB, R15
204 /* Restore the registers from the stack of the task pointed to by
207 MVTACLO R15 /* Accumulator low 32 bits. */
209 MVTACHI R15 /* Accumulator high 32 bits. */
210 POPM R1-R15 /* R1 to R15 - R0 is not included as it is the SP. */
211 RTE /* This pops the remaining registers. */
215 /*-----------------------------------------------------------*/
217 #pragma interrupt ( vTickISR( vect = _VECT( configTICK_VECTOR ), enable ) )
218 void vTickISR( void )
220 /* Increment the tick, and perform any processing the new tick value
222 set_ipl( configMAX_SYSCALL_INTERRUPT_PRIORITY );
224 if( xTaskIncrementTick() != pdFALSE )
229 set_ipl( configKERNEL_INTERRUPT_PRIORITY );
231 /*-----------------------------------------------------------*/
233 void vSoftwareInterruptISR( void )
237 /*-----------------------------------------------------------*/
239 #pragma inline_asm prvYieldHandler
240 static void prvYieldHandler( void )
242 /* Re-enable interrupts. */
245 /* Move the data that was automatically pushed onto the interrupt stack when
246 the interrupt occurred from the interrupt stack to the user stack.
248 R15 is saved before it is clobbered. */
251 /* Read the user stack pointer. */
254 /* Move the address down to the data being moved. */
258 /* Copy the data across. */
259 MOV.L [ R0 ], [ R15 ] ; R15
260 MOV.L 4[ R0 ], 4[ R15 ] ; PC
261 MOV.L 8[ R0 ], 8[ R15 ] ; PSW
263 /* Move the interrupt stack pointer to its new correct position. */
266 /* All the rest of the registers are saved directly to the user stack. */
269 /* Save the rest of the general registers (R15 has been saved already). */
272 /* Save the accumulator. */
275 MVFACMI R15 ; Middle order word.
276 SHLL #16, R15 ; Shifted left as it is restored to the low order word.
279 /* Save the stack pointer to the TCB. */
280 MOV.L #_pxCurrentTCB, R15
284 /* Ensure the interrupt mask is set to the syscall priority while the kernel
285 structures are being accessed. */
286 MVTIPL #configMAX_SYSCALL_INTERRUPT_PRIORITY
288 /* Select the next task to run. */
289 BSR.A _vTaskSwitchContext
291 /* Reset the interrupt mask as no more data structure access is required. */
292 MVTIPL #configKERNEL_INTERRUPT_PRIORITY
294 /* Load the stack pointer of the task that is now selected as the Running
295 state task from its TCB. */
296 MOV.L #_pxCurrentTCB,R15
300 /* Restore the context of the new task. The PSW (Program Status Word) and
301 PC will be popped by the RTE instruction. */
311 /*-----------------------------------------------------------*/
313 void vPortEndScheduler( void )
315 /* Not implemented in ports where there is nothing to return to.
316 Artificially force an assert. */
317 configASSERT( pxCurrentTCB == NULL );
319 /* The following line is just to prevent the symbol getting optimised away. */
320 ( void ) vTaskSwitchContext();
322 /*-----------------------------------------------------------*/