2 * FreeRTOS Kernel V10.5.1
\r
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * SPDX-License-Identifier: MIT
\r
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
8 * this software and associated documentation files (the "Software"), to deal in
\r
9 * the Software without restriction, including without limitation the rights to
\r
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
11 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
12 * subject to the following conditions:
\r
14 * The above copyright notice and this permission notice shall be included in all
\r
15 * copies or substantial portions of the Software.
\r
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
24 * https://www.FreeRTOS.org
\r
25 * https://github.com/FreeRTOS
\r
29 /*-----------------------------------------------------------
\r
30 * Implementation of functions defined in portable.h for the SH2A port.
\r
31 *----------------------------------------------------------*/
\r
33 /* Scheduler includes. */
\r
34 #include "FreeRTOS.h"
\r
37 /* Library includes. */
\r
40 /* Hardware specifics. */
\r
41 #if ( configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H == 1 )
\r
43 #include "platform.h"
\r
45 #else /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
\r
47 #include "iodefine.h"
\r
49 #endif /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
\r
51 /*-----------------------------------------------------------*/
\r
53 /* Tasks should start with interrupts enabled and in Supervisor mode, therefore
\r
54 PSW is set with U and I set, and PM and IPL clear. */
\r
55 #define portINITIAL_PSW ( ( StackType_t ) 0x00030000 )
\r
56 #define portINITIAL_FPSW ( ( StackType_t ) 0x00000100 )
\r
58 /* These macros allow a critical section to be added around the call to
\r
59 xTaskIncrementTick(), which is only ever called from interrupts at the kernel
\r
60 priority - ie a known priority. Therefore these local macros are a slight
\r
61 optimisation compared to calling the global SET/CLEAR_INTERRUPT_MASK macros,
\r
62 which would require the old IPL to be read first and stored in a local variable. */
\r
63 #define portMASK_INTERRUPTS_FROM_KERNEL_ISR() __asm volatile ( "MVTIPL %0" ::"i"(configMAX_SYSCALL_INTERRUPT_PRIORITY) )
\r
64 #define portUNMASK_INTERRUPTS_FROM_KERNEL_ISR() __asm volatile ( "MVTIPL %0" ::"i"(configKERNEL_INTERRUPT_PRIORITY) )
\r
66 /*-----------------------------------------------------------*/
\r
69 * Function to start the first task executing - written in asm code as direct
\r
70 * access to registers is required.
\r
72 static void prvStartFirstTask( void ) __attribute__((naked));
\r
76 * Software interrupt handler. Performs the actual context switch (saving and
\r
77 * restoring of registers). Written in asm code as direct register access is
\r
80 #if ( configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H == 1 )
\r
82 R_BSP_PRAGMA_INTERRUPT( vSoftwareInterruptISR, VECT( ICU, SWINT ) )
\r
83 R_BSP_ATTRIB_INTERRUPT void vSoftwareInterruptISR( void ) __attribute__( ( naked ) );
\r
85 #else /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
\r
87 void vSoftwareInterruptISR( void ) __attribute__( ( naked ) );
\r
89 #endif /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
\r
92 * The tick ISR handler. The peripheral used is configured by the application
\r
93 * via a hook/callback function.
\r
95 #if ( configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H == 1 )
\r
97 R_BSP_PRAGMA_INTERRUPT( vTickISR, _VECT( configTICK_VECTOR ) )
\r
98 R_BSP_ATTRIB_INTERRUPT void vTickISR( void ); /* Do not add __attribute__( ( interrupt ) ). */
\r
100 #else /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
\r
102 void vTickISR( void ) __attribute__( ( interrupt ) );
\r
104 #endif /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
\r
106 /*-----------------------------------------------------------*/
\r
108 extern void *pxCurrentTCB;
\r
110 /*-----------------------------------------------------------*/
\r
113 * See header file for description.
\r
115 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
117 /* R0 is not included as it is the stack pointer. */
\r
119 *pxTopOfStack = 0x00;
\r
121 *pxTopOfStack = portINITIAL_PSW;
\r
123 *pxTopOfStack = ( StackType_t ) pxCode;
\r
125 /* When debugging it can be useful if every register is set to a known
\r
126 value. Otherwise code space can be saved by just setting the registers
\r
127 that need to be set. */
\r
128 #ifdef USE_FULL_REGISTER_INITIALISATION
\r
131 *pxTopOfStack = 0xffffffff; /* r15. */
\r
133 *pxTopOfStack = 0xeeeeeeee;
\r
135 *pxTopOfStack = 0xdddddddd;
\r
137 *pxTopOfStack = 0xcccccccc;
\r
139 *pxTopOfStack = 0xbbbbbbbb;
\r
141 *pxTopOfStack = 0xaaaaaaaa;
\r
143 *pxTopOfStack = 0x99999999;
\r
145 *pxTopOfStack = 0x88888888;
\r
147 *pxTopOfStack = 0x77777777;
\r
149 *pxTopOfStack = 0x66666666;
\r
151 *pxTopOfStack = 0x55555555;
\r
153 *pxTopOfStack = 0x44444444;
\r
155 *pxTopOfStack = 0x33333333;
\r
157 *pxTopOfStack = 0x22222222;
\r
162 pxTopOfStack -= 15;
\r
166 *pxTopOfStack = ( StackType_t ) pvParameters; /* R1 */
\r
168 *pxTopOfStack = portINITIAL_FPSW;
\r
170 *pxTopOfStack = 0x11111111; /* Accumulator 0. */
\r
172 *pxTopOfStack = 0x22222222; /* Accumulator 0. */
\r
174 *pxTopOfStack = 0x33333333; /* Accumulator 0. */
\r
176 *pxTopOfStack = 0x44444444; /* Accumulator 1. */
\r
178 *pxTopOfStack = 0x55555555; /* Accumulator 1. */
\r
180 *pxTopOfStack = 0x66666666; /* Accumulator 1. */
\r
182 return pxTopOfStack;
\r
184 /*-----------------------------------------------------------*/
\r
186 BaseType_t xPortStartScheduler( void )
\r
188 extern void vApplicationSetupTimerInterrupt( void );
\r
190 /* Use pxCurrentTCB just so it does not get optimised away. */
\r
191 if( pxCurrentTCB != NULL )
\r
193 /* Call an application function to set up the timer that will generate the
\r
194 tick interrupt. This way the application can decide which peripheral to
\r
195 use. A demo application is provided to show a suitable example. */
\r
196 vApplicationSetupTimerInterrupt();
\r
198 /* Enable the software interrupt. */
\r
199 _IEN( _ICU_SWINT ) = 1;
\r
201 /* Ensure the software interrupt is clear. */
\r
202 _IR( _ICU_SWINT ) = 0;
\r
204 /* Ensure the software interrupt is set to the kernel priority. */
\r
205 _IPR( _ICU_SWINT ) = configKERNEL_INTERRUPT_PRIORITY;
\r
207 /* Start the first task. */
\r
208 prvStartFirstTask();
\r
211 /* Should not get here. */
\r
214 /*-----------------------------------------------------------*/
\r
216 void vPortEndScheduler( void )
\r
218 /* Not implemented in ports where there is nothing to return to.
\r
219 Artificially force an assert. */
\r
220 configASSERT( pxCurrentTCB == NULL );
\r
222 /*-----------------------------------------------------------*/
\r
224 static void prvStartFirstTask( void )
\r
228 /* When starting the scheduler there is nothing that needs moving to the
\r
229 interrupt stack because the function is not called from an interrupt.
\r
230 Just ensure the current stack is the user stack. */
\r
233 /* Obtain the location of the stack associated with which ever task
\r
234 pxCurrentTCB is currently pointing to. */
\r
235 "MOV.L #_pxCurrentTCB, R15 \n" \
\r
236 "MOV.L [R15], R15 \n" \
\r
237 "MOV.L [R15], R0 \n" \
\r
239 /* Restore the registers from the stack of the task pointed to by
\r
243 /* Accumulator low 32 bits. */
\r
244 "MVTACLO R15, A0 \n" \
\r
247 /* Accumulator high 32 bits. */
\r
248 "MVTACHI R15, A0 \n" \
\r
251 /* Accumulator guard. */
\r
252 "MVTACGU R15, A0 \n" \
\r
255 /* Accumulator low 32 bits. */
\r
256 "MVTACLO R15, A1 \n" \
\r
259 /* Accumulator high 32 bits. */
\r
260 "MVTACHI R15, A1 \n" \
\r
263 /* Accumulator guard. */
\r
264 "MVTACGU R15, A1 \n" \
\r
267 /* Floating point status word. */
\r
268 "MVTC R15, FPSW \n" \
\r
270 /* R1 to R15 - R0 is not included as it is the SP. */
\r
273 /* This pops the remaining registers. */
\r
279 /*-----------------------------------------------------------*/
\r
281 void vSoftwareInterruptISR( void )
\r
285 /* Re-enable interrupts. */
\r
288 /* Move the data that was automatically pushed onto the interrupt stack when
\r
289 the interrupt occurred from the interrupt stack to the user stack.
\r
291 R15 is saved before it is clobbered. */
\r
294 /* Read the user stack pointer. */
\r
295 "MVFC USP, R15 \n" \
\r
297 /* Move the address down to the data being moved. */
\r
298 "SUB #12, R15 \n" \
\r
299 "MVTC R15, USP \n" \
\r
301 /* Copy the data across, R15, then PC, then PSW. */
\r
302 "MOV.L [ R0 ], [ R15 ] \n" \
\r
303 "MOV.L 4[ R0 ], 4[ R15 ] \n" \
\r
304 "MOV.L 8[ R0 ], 8[ R15 ] \n" \
\r
306 /* Move the interrupt stack pointer to its new correct position. */
\r
309 /* All the rest of the registers are saved directly to the user stack. */
\r
312 /* Save the rest of the general registers (R15 has been saved already). */
\r
313 "PUSHM R1-R14 \n" \
\r
315 /* Save the FPSW and accumulator. */
\r
316 "MVFC FPSW, R15 \n" \
\r
318 "MVFACGU #0, A1, R15 \n" \
\r
320 "MVFACHI #0, A1, R15 \n" \
\r
322 /* Low order word. */
\r
323 "MVFACLO #0, A1, R15 \n" \
\r
325 "MVFACGU #0, A0, R15 \n" \
\r
327 "MVFACHI #0, A0, R15 \n" \
\r
329 /* Low order word. */
\r
330 "MVFACLO #0, A0, R15 \n" \
\r
333 /* Save the stack pointer to the TCB. */
\r
334 "MOV.L #_pxCurrentTCB, R15 \n" \
\r
335 "MOV.L [ R15 ], R15 \n" \
\r
336 "MOV.L R0, [ R15 ] \n" \
\r
338 /* Ensure the interrupt mask is set to the syscall priority while the kernel
\r
339 structures are being accessed. */
\r
342 /* Select the next task to run. */
\r
343 "BSR.A _vTaskSwitchContext \n" \
\r
345 /* Reset the interrupt mask as no more data structure access is required. */
\r
348 /* Load the stack pointer of the task that is now selected as the Running
\r
349 state task from its TCB. */
\r
350 "MOV.L #_pxCurrentTCB,R15 \n" \
\r
351 "MOV.L [ R15 ], R15 \n" \
\r
352 "MOV.L [ R15 ], R0 \n" \
\r
354 /* Restore the context of the new task. The PSW (Program Status Word) and
\r
355 PC will be popped by the RTE instruction. */
\r
358 /* Accumulator low 32 bits. */
\r
359 "MVTACLO R15, A0 \n" \
\r
362 /* Accumulator high 32 bits. */
\r
363 "MVTACHI R15, A0 \n" \
\r
366 /* Accumulator guard. */
\r
367 "MVTACGU R15, A0 \n" \
\r
370 /* Accumulator low 32 bits. */
\r
371 "MVTACLO R15, A1 \n" \
\r
374 /* Accumulator high 32 bits. */
\r
375 "MVTACHI R15, A1 \n" \
\r
378 /* Accumulator guard. */
\r
379 "MVTACGU R15, A1 \n" \
\r
381 "MVTC R15, FPSW \n" \
\r
386 :: "i"(configMAX_SYSCALL_INTERRUPT_PRIORITY), "i"(configKERNEL_INTERRUPT_PRIORITY)
\r
389 /*-----------------------------------------------------------*/
\r
391 void vTickISR( void )
\r
393 /* Re-enabled interrupts. */
\r
394 __asm volatile( "SETPSW I" );
\r
396 /* Increment the tick, and perform any processing the new tick value
\r
397 necessitates. Ensure IPL is at the max syscall value first. */
\r
398 portMASK_INTERRUPTS_FROM_KERNEL_ISR();
\r
400 if( xTaskIncrementTick() != pdFALSE )
\r
405 portUNMASK_INTERRUPTS_FROM_KERNEL_ISR();
\r
407 /*-----------------------------------------------------------*/
\r
409 uint32_t ulPortGetIPL( void )
\r
413 "MVFC PSW, R1 \n" \
\r
414 "SHLR #24, R1 \n" \
\r
418 /* This will never get executed, but keeps the compiler from complaining. */
\r
421 /*-----------------------------------------------------------*/
\r
423 void vPortSetIPL( uint32_t ulNewIPL )
\r
428 "MVFC PSW, R5 \n" \
\r
429 "SHLL #24, R1 \n" \
\r
430 "AND #-0F000001H, R5 \n" \
\r
432 "MVTC R5, PSW \n" \
\r