2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
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 SH2A port.
31 *----------------------------------------------------------*/
33 /* Scheduler includes. */
37 /* Library includes. */
40 /* Hardware specifics. */
41 #if ( configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H == 1 )
45 #else /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
49 #endif /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
51 /*-----------------------------------------------------------*/
53 /* Tasks should start with interrupts enabled and in Supervisor mode, therefore
54 * PSW is set with U and I set, and PM and IPL clear. */
55 #define portINITIAL_PSW ( ( StackType_t ) 0x00030000 )
56 #define portINITIAL_FPSW ( ( StackType_t ) 0x00000100 )
58 /* These macros allow a critical section to be added around the call to
59 * xTaskIncrementTick(), which is only ever called from interrupts at the kernel
60 * priority - ie a known priority. Therefore these local macros are a slight
61 * optimisation compared to calling the global SET/CLEAR_INTERRUPT_MASK macros,
62 * which would require the old IPL to be read first and stored in a local variable. */
63 #define portMASK_INTERRUPTS_FROM_KERNEL_ISR() __asm volatile ( "MVTIPL %0" ::"i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY ) )
64 #define portUNMASK_INTERRUPTS_FROM_KERNEL_ISR() __asm volatile ( "MVTIPL %0" ::"i" ( configKERNEL_INTERRUPT_PRIORITY ) )
66 /*-----------------------------------------------------------*/
69 * Function to start the first task executing - written in asm code as direct
70 * access to registers is required.
72 static void prvStartFirstTask( void ) __attribute__( ( naked ) );
75 * Software interrupt handler. Performs the actual context switch (saving and
76 * restoring of registers). Written in asm code as direct register access is
79 #if ( configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H == 1 )
80 R_BSP_PRAGMA_INTERRUPT( vSoftwareInterruptISR, VECT( ICU, SWINT ) )
81 R_BSP_ATTRIB_INTERRUPT void vSoftwareInterruptISR( void ) __attribute__( ( naked ) );
82 #else /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
83 void vSoftwareInterruptISR( void ) __attribute__( ( naked ) );
84 #endif /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
87 * The tick ISR handler. The peripheral used is configured by the application
88 * via a hook/callback function.
90 #if ( configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H == 1 )
92 R_BSP_PRAGMA_INTERRUPT( vTickISR, _VECT( configTICK_VECTOR ) )
93 R_BSP_ATTRIB_INTERRUPT void vTickISR( void ); /* Do not add __attribute__( ( interrupt ) ). */
95 #else /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
97 void vTickISR( void ) __attribute__( ( interrupt ) );
99 #endif /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
100 /*-----------------------------------------------------------*/
102 extern void * pxCurrentTCB;
104 /*-----------------------------------------------------------*/
107 * See header file for description.
109 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
110 TaskFunction_t pxCode,
111 void * pvParameters )
113 /* R0 is not included as it is the stack pointer. */
115 *pxTopOfStack = 0x00;
117 *pxTopOfStack = portINITIAL_PSW;
119 *pxTopOfStack = ( StackType_t ) pxCode;
121 /* When debugging it can be useful if every register is set to a known
122 * value. Otherwise code space can be saved by just setting the registers
123 * that need to be set. */
124 #ifdef USE_FULL_REGISTER_INITIALISATION
127 *pxTopOfStack = 0xffffffff; /* r15. */
129 *pxTopOfStack = 0xeeeeeeee;
131 *pxTopOfStack = 0xdddddddd;
133 *pxTopOfStack = 0xcccccccc;
135 *pxTopOfStack = 0xbbbbbbbb;
137 *pxTopOfStack = 0xaaaaaaaa;
139 *pxTopOfStack = 0x99999999;
141 *pxTopOfStack = 0x88888888;
143 *pxTopOfStack = 0x77777777;
145 *pxTopOfStack = 0x66666666;
147 *pxTopOfStack = 0x55555555;
149 *pxTopOfStack = 0x44444444;
151 *pxTopOfStack = 0x33333333;
153 *pxTopOfStack = 0x22222222;
156 #else /* ifdef USE_FULL_REGISTER_INITIALISATION */
160 #endif /* ifdef USE_FULL_REGISTER_INITIALISATION */
162 *pxTopOfStack = ( StackType_t ) pvParameters; /* R1 */
164 *pxTopOfStack = portINITIAL_FPSW;
166 *pxTopOfStack = 0x11111111; /* Accumulator 0. */
168 *pxTopOfStack = 0x22222222; /* Accumulator 0. */
170 *pxTopOfStack = 0x33333333; /* Accumulator 0. */
172 *pxTopOfStack = 0x44444444; /* Accumulator 1. */
174 *pxTopOfStack = 0x55555555; /* Accumulator 1. */
176 *pxTopOfStack = 0x66666666; /* Accumulator 1. */
180 /*-----------------------------------------------------------*/
182 BaseType_t xPortStartScheduler( void )
184 extern void vApplicationSetupTimerInterrupt( void );
186 /* Use pxCurrentTCB just so it does not get optimised away. */
187 if( pxCurrentTCB != NULL )
189 /* Call an application function to set up the timer that will generate the
190 * tick interrupt. This way the application can decide which peripheral to
191 * use. A demo application is provided to show a suitable example. */
192 vApplicationSetupTimerInterrupt();
194 /* Enable the software interrupt. */
195 _IEN( _ICU_SWINT ) = 1;
197 /* Ensure the software interrupt is clear. */
198 _IR( _ICU_SWINT ) = 0;
200 /* Ensure the software interrupt is set to the kernel priority. */
201 _IPR( _ICU_SWINT ) = configKERNEL_INTERRUPT_PRIORITY;
203 /* Start the first task. */
207 /* Should not get here. */
210 /*-----------------------------------------------------------*/
212 void vPortEndScheduler( void )
214 /* Not implemented in ports where there is nothing to return to.
215 * Artificially force an assert. */
216 configASSERT( pxCurrentTCB == NULL );
218 /*-----------------------------------------------------------*/
220 static void prvStartFirstTask( void )
225 /* When starting the scheduler there is nothing that needs moving to the
226 * interrupt stack because the function is not called from an interrupt.
227 * Just ensure the current stack is the user stack. */
231 /* Obtain the location of the stack associated with which ever task
232 * pxCurrentTCB is currently pointing to. */
233 "MOV.L #_pxCurrentTCB, R15 \n" \
234 "MOV.L [R15], R15 \n" \
235 "MOV.L [R15], R0 \n" \
238 /* Restore the registers from the stack of the task pointed to by
242 /* Accumulator low 32 bits. */
243 "MVTACLO R15, A0 \n" \
246 /* Accumulator high 32 bits. */
247 "MVTACHI R15, A0 \n" \
250 /* Accumulator guard. */
251 "MVTACGU R15, A0 \n" \
254 /* Accumulator low 32 bits. */
255 "MVTACLO R15, A1 \n" \
258 /* Accumulator high 32 bits. */
259 "MVTACHI R15, A1 \n" \
262 /* Accumulator guard. */
263 "MVTACGU R15, A1 \n" \
266 /* Floating point status word. */
267 "MVTC R15, FPSW \n" \
269 /* R1 to R15 - R0 is not included as it is the SP. */
272 /* This pops the remaining registers. */
278 /*-----------------------------------------------------------*/
280 void vSoftwareInterruptISR( void )
284 /* Re-enable interrupts. */
288 /* Move the data that was automatically pushed onto the interrupt stack when
289 * the interrupt occurred from the interrupt stack to the user stack.
291 * R15 is saved before it is clobbered. */
294 /* Read the user stack pointer. */
297 /* Move the address down to the data being moved. */
301 /* Copy the data across, R15, then PC, then PSW. */
302 "MOV.L [ R0 ], [ R15 ] \n" \
303 "MOV.L 4[ R0 ], 4[ R15 ] \n" \
304 "MOV.L 8[ R0 ], 8[ R15 ] \n" \
306 /* Move the interrupt stack pointer to its new correct position. */
309 /* All the rest of the registers are saved directly to the user stack. */
312 /* Save the rest of the general registers (R15 has been saved already). */
315 /* Save the FPSW and accumulator. */
316 "MVFC FPSW, R15 \n" \
318 "MVFACGU #0, A1, R15 \n" \
320 "MVFACHI #0, A1, R15 \n" \
322 /* Low order word. */
323 "MVFACLO #0, A1, R15 \n" \
325 "MVFACGU #0, A0, R15 \n" \
327 "MVFACHI #0, A0, R15 \n" \
329 /* Low order word. */
330 "MVFACLO #0, A0, R15 \n" \
333 /* Save the stack pointer to the TCB. */
334 "MOV.L #_pxCurrentTCB, R15 \n" \
335 "MOV.L [ R15 ], R15 \n" \
336 "MOV.L R0, [ R15 ] \n" \
339 /* Ensure the interrupt mask is set to the syscall priority while the kernel
340 * structures are being accessed. */
343 /* Select the next task to run. */
344 "BSR.A _vTaskSwitchContext \n" \
346 /* Reset the interrupt mask as no more data structure access is required. */
350 /* Load the stack pointer of the task that is now selected as the Running
351 * state task from its TCB. */
352 "MOV.L #_pxCurrentTCB,R15 \n" \
353 "MOV.L [ R15 ], R15 \n" \
354 "MOV.L [ R15 ], R0 \n" \
357 /* Restore the context of the new task. The PSW (Program Status Word) and
358 * PC will be popped by the RTE instruction. */
361 /* Accumulator low 32 bits. */
362 "MVTACLO R15, A0 \n" \
365 /* Accumulator high 32 bits. */
366 "MVTACHI R15, A0 \n" \
369 /* Accumulator guard. */
370 "MVTACGU R15, A0 \n" \
373 /* Accumulator low 32 bits. */
374 "MVTACLO R15, A1 \n" \
377 /* Accumulator high 32 bits. */
378 "MVTACHI R15, A1 \n" \
381 /* Accumulator guard. */
382 "MVTACGU R15, A1 \n" \
384 "MVTC R15, FPSW \n" \
389 ::"i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY ), "i" ( configKERNEL_INTERRUPT_PRIORITY )
392 /*-----------------------------------------------------------*/
394 void vTickISR( void )
396 /* Re-enabled interrupts. */
397 __asm volatile ( "SETPSW I" );
399 /* Increment the tick, and perform any processing the new tick value
400 * necessitates. Ensure IPL is at the max syscall value first. */
401 portMASK_INTERRUPTS_FROM_KERNEL_ISR();
403 if( xTaskIncrementTick() != pdFALSE )
408 portUNMASK_INTERRUPTS_FROM_KERNEL_ISR();
410 /*-----------------------------------------------------------*/
412 uint32_t ulPortGetIPL( void )
421 /* This will never get executed, but keeps the compiler from complaining. */
424 /*-----------------------------------------------------------*/
426 void vPortSetIPL( uint32_t ulNewIPL )
428 /* Avoid compiler warning about unreferenced parameter. */
436 "AND #-0F000001H, R5 \n" \