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 */
52 /*-----------------------------------------------------------*/
54 /* Tasks should start with interrupts enabled and in Supervisor mode, therefore
55 * PSW is set with U and I set, and PM and IPL clear. */
56 #define portINITIAL_PSW ( ( StackType_t ) 0x00030000 )
57 #define portINITIAL_FPSW ( ( StackType_t ) 0x00000100 )
59 /* These macros allow a critical section to be added around the call to
60 * xTaskIncrementTick(), which is only ever called from interrupts at the kernel
61 * priority - ie a known priority. Therefore these local macros are a slight
62 * optimisation compared to calling the global SET/CLEAR_INTERRUPT_MASK macros,
63 * which would require the old IPL to be read first and stored in a local variable. */
64 #define portDISABLE_INTERRUPTS_FROM_KERNEL_ISR() __asm volatile ( "MVTIPL %0" ::"i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY ) )
65 #define portENABLE_INTERRUPTS_FROM_KERNEL_ISR() __asm volatile ( "MVTIPL %0" ::"i" ( configKERNEL_INTERRUPT_PRIORITY ) )
67 /*-----------------------------------------------------------*/
70 * Function to start the first task executing - written in asm code as direct
71 * access to registers is required.
73 static void prvStartFirstTask( void ) __attribute__( ( naked ) );
76 * Software interrupt handler. Performs the actual context switch (saving and
77 * restoring of registers). Written in asm code as direct register access is
80 #if ( configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H == 1 )
82 R_BSP_PRAGMA_INTERRUPT( vSoftwareInterruptISR, VECT( ICU, SWINT ) )
83 R_BSP_ATTRIB_INTERRUPT void vSoftwareInterruptISR( void ) __attribute__( ( naked ) );
85 #else /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
87 void vSoftwareInterruptISR( void ) __attribute__( ( naked ) );
89 #endif /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
92 * The tick ISR handler. The peripheral used is configured by the application
93 * via a hook/callback function.
95 #if ( configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H == 1 )
97 R_BSP_PRAGMA_INTERRUPT( vTickISR, _VECT( configTICK_VECTOR ) )
98 R_BSP_ATTRIB_INTERRUPT void vTickISR( void ); /* Do not add __attribute__( ( interrupt ) ). */
100 #else /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
102 void vTickISR( void ) __attribute__( ( interrupt ) );
104 #endif /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
106 /*-----------------------------------------------------------*/
108 extern void * pxCurrentTCB;
110 /*-----------------------------------------------------------*/
113 * See header file for description.
115 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
116 TaskFunction_t pxCode,
117 void * pvParameters )
119 /* R0 is not included as it is the stack pointer. */
121 *pxTopOfStack = 0x00;
123 *pxTopOfStack = portINITIAL_PSW;
125 *pxTopOfStack = ( StackType_t ) pxCode;
127 /* When debugging it can be useful if every register is set to a known
128 * value. Otherwise code space can be saved by just setting the registers
129 * that need to be set. */
130 #ifdef USE_FULL_REGISTER_INITIALISATION
133 *pxTopOfStack = 0xffffffff; /* r15. */
135 *pxTopOfStack = 0xeeeeeeee;
137 *pxTopOfStack = 0xdddddddd;
139 *pxTopOfStack = 0xcccccccc;
141 *pxTopOfStack = 0xbbbbbbbb;
143 *pxTopOfStack = 0xaaaaaaaa;
145 *pxTopOfStack = 0x99999999;
147 *pxTopOfStack = 0x88888888;
149 *pxTopOfStack = 0x77777777;
151 *pxTopOfStack = 0x66666666;
153 *pxTopOfStack = 0x55555555;
155 *pxTopOfStack = 0x44444444;
157 *pxTopOfStack = 0x33333333;
159 *pxTopOfStack = 0x22222222;
162 #else /* ifdef USE_FULL_REGISTER_INITIALISATION */
166 #endif /* ifdef USE_FULL_REGISTER_INITIALISATION */
168 *pxTopOfStack = ( StackType_t ) pvParameters; /* R1 */
170 *pxTopOfStack = portINITIAL_FPSW;
172 *pxTopOfStack = 0x12345678; /* Accumulator. */
174 *pxTopOfStack = 0x87654321; /* Accumulator. */
178 /*-----------------------------------------------------------*/
180 BaseType_t xPortStartScheduler( void )
182 extern void vApplicationSetupTimerInterrupt( void );
184 /* Use pxCurrentTCB just so it does not get optimised away. */
185 if( pxCurrentTCB != NULL )
187 /* Call an application function to set up the timer that will generate the
188 * tick interrupt. This way the application can decide which peripheral to
189 * use. A demo application is provided to show a suitable example. */
190 vApplicationSetupTimerInterrupt();
192 /* Enable the software interrupt. */
193 _IEN( _ICU_SWINT ) = 1;
195 /* Ensure the software interrupt is clear. */
196 _IR( _ICU_SWINT ) = 0;
198 /* Ensure the software interrupt is set to the kernel priority. */
199 _IPR( _ICU_SWINT ) = configKERNEL_INTERRUPT_PRIORITY;
201 /* Start the first task. */
205 /* Should not get here. */
208 /*-----------------------------------------------------------*/
210 void vPortEndScheduler( void )
212 /* Not implemented in ports where there is nothing to return to.
213 * Artificially force an assert. */
214 configASSERT( pxCurrentTCB == NULL );
216 /*-----------------------------------------------------------*/
218 static void prvStartFirstTask( void )
223 /* When starting the scheduler there is nothing that needs moving to the
224 * interrupt stack because the function is not called from an interrupt.
225 * Just ensure the current stack is the user stack. */
229 /* Obtain the location of the stack associated with which ever task
230 * pxCurrentTCB is currently pointing to. */
231 "MOV.L #_pxCurrentTCB, R15 \n" \
232 "MOV.L [R15], R15 \n" \
233 "MOV.L [R15], R0 \n" \
236 /* Restore the registers from the stack of the task pointed to by
240 /* Accumulator low 32 bits. */
244 /* Accumulator high 32 bits. */
248 /* Floating point status word. */
249 "MVTC R15, FPSW \n" \
251 /* R1 to R15 - R0 is not included as it is the SP. */
254 /* This pops the remaining registers. */
260 /*-----------------------------------------------------------*/
262 void vSoftwareInterruptISR( void )
266 /* Re-enable interrupts. */
270 /* Move the data that was automatically pushed onto the interrupt stack when
271 * the interrupt occurred from the interrupt stack to the user stack.
273 * R15 is saved before it is clobbered. */
276 /* Read the user stack pointer. */
279 /* Move the address down to the data being moved. */
283 /* Copy the data across, R15, then PC, then PSW. */
284 "MOV.L [ R0 ], [ R15 ] \n" \
285 "MOV.L 4[ R0 ], 4[ R15 ] \n" \
286 "MOV.L 8[ R0 ], 8[ R15 ] \n" \
288 /* Move the interrupt stack pointer to its new correct position. */
291 /* All the rest of the registers are saved directly to the user stack. */
294 /* Save the rest of the general registers (R15 has been saved already). */
297 /* Save the FPSW and accumulator. */
298 "MVFC FPSW, R15 \n" \
306 /* Shifted left as it is restored to the low order word. */
310 /* Save the stack pointer to the TCB. */
311 "MOV.L #_pxCurrentTCB, R15 \n" \
312 "MOV.L [ R15 ], R15 \n" \
313 "MOV.L R0, [ R15 ] \n" \
316 /* Ensure the interrupt mask is set to the syscall priority while the kernel
317 * structures are being accessed. */
320 /* Select the next task to run. */
321 "BSR.A _vTaskSwitchContext \n" \
323 /* Reset the interrupt mask as no more data structure access is required. */
327 /* Load the stack pointer of the task that is now selected as the Running
328 * state task from its TCB. */
329 "MOV.L #_pxCurrentTCB,R15 \n" \
330 "MOV.L [ R15 ], R15 \n" \
331 "MOV.L [ R15 ], R0 \n" \
334 /* Restore the context of the new task. The PSW (Program Status Word) and
335 * PC will be popped by the RTE instruction. */
341 "MVTC R15, FPSW \n" \
346 ::"i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY ), "i" ( configKERNEL_INTERRUPT_PRIORITY )
349 /*-----------------------------------------------------------*/
351 void vTickISR( void )
353 /* Re-enabled interrupts. */
354 __asm volatile ( "SETPSW I" );
356 /* Increment the tick, and perform any processing the new tick value
357 * necessitates. Ensure IPL is at the max syscall value first. */
358 portDISABLE_INTERRUPTS_FROM_KERNEL_ISR();
360 if( xTaskIncrementTick() != pdFALSE )
365 portENABLE_INTERRUPTS_FROM_KERNEL_ISR();
367 /*-----------------------------------------------------------*/
369 uint32_t ulPortGetIPL( void )
378 /* This will never get executed, but keeps the compiler from complaining. */
381 /*-----------------------------------------------------------*/
383 void vPortSetIPL( uint32_t ulNewIPL )
385 /* Avoid compiler warning about unreferenced parameter. */
393 "AND #-0F000001H, R5 \n" \