2 * FreeRTOS Kernel V10.5.0
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 RXv3 DPFPU port.
31 *----------------------------------------------------------*/
33 #warning Testing for DFPU support in this port is not yet complete
35 /* Scheduler includes. */
39 /* Library includes. */
42 /* Hardware specifics. */
43 #if ( configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H == 1 )
47 #else /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
51 #endif /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
53 /*-----------------------------------------------------------*/
55 /* Tasks should start with interrupts enabled and in Supervisor mode, therefore
56 * PSW is set with U and I set, and PM and IPL clear. */
57 #define portINITIAL_PSW ( ( StackType_t ) 0x00030000 )
58 #define portINITIAL_FPSW ( ( StackType_t ) 0x00000100 )
59 #define portINITIAL_DPSW ( ( StackType_t ) 0x00000100 )
60 #define portINITIAL_DCMR ( ( StackType_t ) 0x00000000 )
61 #define portINITIAL_DECNT ( ( StackType_t ) 0x00000001 )
63 /* Tasks are not created with a DPFPU context, but can be given a DPFPU context
64 * after they have been created. A variable is stored as part of the tasks context
65 * that holds portNO_DPFPU_CONTEXT if the task does not have a DPFPU context, or
66 * any other value if the task does have a DPFPU context. */
67 #define portNO_DPFPU_CONTEXT ( ( StackType_t ) 0 )
68 #define portHAS_DPFPU_CONTEXT ( ( StackType_t ) 1 )
70 /* The space on the stack required to hold the DPFPU data registers. This is 16
71 * 64-bit registers. */
72 #define portDPFPU_DATA_REGISTER_WORDS ( 16 * 2 )
74 /*-----------------------------------------------------------*/
77 * Function to start the first task executing - written in asm code as direct
78 * access to registers is required.
80 static void prvStartFirstTask( void );
83 * Software interrupt handler. Performs the actual context switch (saving and
84 * restoring of registers). Written in asm code as direct register access is
87 __interrupt void vSoftwareInterruptISR( void );
90 * The tick ISR handler. The peripheral used is configured by the application
91 * via a hook/callback function.
93 __interrupt void vTickISR( void );
95 /*-----------------------------------------------------------*/
97 /* Saved as part of the task context. If ulPortTaskHasDPFPUContext is non-zero
98 * then a DPFPU context must be saved and restored for the task. */
99 #if ( configUSE_TASK_DPFPU_SUPPORT == 1 )
101 StackType_t ulPortTaskHasDPFPUContext = portNO_DPFPU_CONTEXT;
103 #endif /* configUSE_TASK_DPFPU_SUPPORT */
105 /* This is accessed by the inline assembler functions so is file scope for
107 extern void * pxCurrentTCB;
108 extern void vTaskSwitchContext( void );
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 = 0x11111111; /* Accumulator 1. */
174 *pxTopOfStack = 0x22222222; /* Accumulator 1. */
176 *pxTopOfStack = 0x33333333; /* Accumulator 1. */
178 *pxTopOfStack = 0x44444444; /* Accumulator 0. */
180 *pxTopOfStack = 0x55555555; /* Accumulator 0. */
182 *pxTopOfStack = 0x66666666; /* Accumulator 0. */
184 #if ( configUSE_TASK_DPFPU_SUPPORT == 1 )
186 /* The task will start without a DPFPU context. A task that
187 * uses the DPFPU hardware must call vPortTaskUsesDPFPU() before
188 * executing any floating point instructions. */
190 *pxTopOfStack = portNO_DPFPU_CONTEXT;
192 #elif ( configUSE_TASK_DPFPU_SUPPORT == 2 )
194 /* The task will start with a DPFPU context. Leave enough
195 * space for the registers - and ensure they are initialised if desired. */
196 #ifdef USE_FULL_REGISTER_INITIALISATION
199 *(double *)pxTopOfStack = 1515.1515; /* DR15. */
201 *(double *)pxTopOfStack = 1414.1414; /* DR14. */
203 *(double *)pxTopOfStack = 1313.1313; /* DR13. */
205 *(double *)pxTopOfStack = 1212.1212; /* DR12. */
207 *(double *)pxTopOfStack = 1111.1111; /* DR11. */
209 *(double *)pxTopOfStack = 1010.1010; /* DR10. */
211 *(double *)pxTopOfStack = 909.0909; /* DR9. */
213 *(double *)pxTopOfStack = 808.0808; /* DR8. */
215 *(double *)pxTopOfStack = 707.0707; /* DR7. */
217 *(double *)pxTopOfStack = 606.0606; /* DR6. */
219 *(double *)pxTopOfStack = 505.0505; /* DR5. */
221 *(double *)pxTopOfStack = 404.0404; /* DR4. */
223 *(double *)pxTopOfStack = 303.0303; /* DR3. */
225 *(double *)pxTopOfStack = 202.0202; /* DR2. */
227 *(double *)pxTopOfStack = 101.0101; /* DR1. */
229 *(double *)pxTopOfStack = 9876.54321;/* DR0. */
231 #else /* ifdef USE_FULL_REGISTER_INITIALISATION */
233 pxTopOfStack -= portDPFPU_DATA_REGISTER_WORDS;
234 memset( pxTopOfStack, 0x00, portDPFPU_DATA_REGISTER_WORDS * sizeof( StackType_t ) );
236 #endif /* ifdef USE_FULL_REGISTER_INITIALISATION */
238 *pxTopOfStack = portINITIAL_DECNT; /* DECNT. */
240 *pxTopOfStack = portINITIAL_DCMR; /* DCMR. */
242 *pxTopOfStack = portINITIAL_DPSW; /* DPSW. */
244 #elif ( configUSE_TASK_DPFPU_SUPPORT == 0 )
246 /* Omit DPFPU support. */
248 #else /* if ( configUSE_TASK_DPFPU_SUPPORT == 1 ) */
250 #error Invalid configUSE_TASK_DPFPU_SUPPORT setting - configUSE_TASK_DPFPU_SUPPORT must be set to 0, 1, 2, or left undefined.
252 #endif /* if ( configUSE_TASK_DPFPU_SUPPORT == 1 ) */
256 /*-----------------------------------------------------------*/
258 #if ( configUSE_TASK_DPFPU_SUPPORT == 1 )
260 void vPortTaskUsesDPFPU( void )
262 /* A task is registering the fact that it needs a DPFPU context. Set the
263 * DPFPU flag (which is saved as part of the task context). */
264 ulPortTaskHasDPFPUContext = portHAS_DPFPU_CONTEXT;
267 #endif /* configUSE_TASK_DPFPU_SUPPORT */
268 /*-----------------------------------------------------------*/
270 BaseType_t xPortStartScheduler( void )
272 extern void vApplicationSetupTimerInterrupt( void );
274 /* Use pxCurrentTCB just so it does not get optimised away. */
275 if( pxCurrentTCB != NULL )
277 /* Call an application function to set up the timer that will generate the
278 * tick interrupt. This way the application can decide which peripheral to
279 * use. A demo application is provided to show a suitable example. */
280 vApplicationSetupTimerInterrupt();
282 /* Enable the software interrupt. */
283 _IEN( _ICU_SWINT ) = 1;
285 /* Ensure the software interrupt is clear. */
286 _IR( _ICU_SWINT ) = 0;
288 /* Ensure the software interrupt is set to the kernel priority. */
289 _IPR( _ICU_SWINT ) = configKERNEL_INTERRUPT_PRIORITY;
291 /* Start the first task. */
295 /* Should not get here. */
298 /*-----------------------------------------------------------*/
300 void vPortEndScheduler( void )
302 /* Not implemented in ports where there is nothing to return to.
303 * Artificially force an assert. */
304 configASSERT( pxCurrentTCB == NULL );
306 /* The following line is just to prevent the symbol getting optimised away. */
307 ( void ) vTaskSwitchContext();
309 /*-----------------------------------------------------------*/
311 static void prvStartFirstTask( void )
316 /* When starting the scheduler there is nothing that needs moving to the
317 * interrupt stack because the function is not called from an interrupt.
318 * Just ensure the current stack is the user stack. */
322 /* Obtain the location of the stack associated with which ever task
323 * pxCurrentTCB is currently pointing to. */
324 "MOV.L #_pxCurrentTCB, R15 \n"\
325 "MOV.L [R15], R15 \n"\
326 "MOV.L [R15], R0 \n"\
329 /* Restore the registers from the stack of the task pointed to by
332 #if ( configUSE_TASK_DPFPU_SUPPORT == 1 )
334 /* The restored ulPortTaskHasDPFPUContext is to be zero here.
335 * So, it is never necessary to restore the DPFPU context here. */
337 "MOV.L #_ulPortTaskHasDPFPUContext, R14 \n"\
338 "MOV.L R15, [R14] \n"\
340 #elif ( configUSE_TASK_DPFPU_SUPPORT == 2 )
342 /* Restore the DPFPU context. */
343 "DPOPM.L DPSW-DECNT \n"\
344 "DPOPM.D DR0-DR15 \n"\
346 #endif /* if ( configUSE_TASK_DPFPU_SUPPORT == 1 ) */
350 /* Accumulator low 32 bits. */
351 "MVTACLO R15, A0 \n"\
354 /* Accumulator high 32 bits. */
355 "MVTACHI R15, A0 \n"\
358 /* Accumulator guard. */
359 "MVTACGU R15, A0 \n"\
362 /* Accumulator low 32 bits. */
363 "MVTACLO R15, A1 \n"\
366 /* Accumulator high 32 bits. */
367 "MVTACHI R15, A1 \n"\
370 /* Accumulator guard. */
371 "MVTACGU R15, A1 \n"\
374 /* Floating point status word. */
377 /* R1 to R15 - R0 is not included as it is the SP. */
380 /* This pops the remaining registers. */
386 /*-----------------------------------------------------------*/
388 #pragma vector = VECT( ICU, SWINT )
389 __interrupt void vSoftwareInterruptISR( void )
393 /* Re-enable interrupts. */
397 /* Move the data that was automatically pushed onto the interrupt stack when
398 * the interrupt occurred from the interrupt stack to the user stack.
400 * R15 is saved before it is clobbered. */
403 /* Read the user stack pointer. */
406 /* Move the address down to the data being moved. */
410 /* Copy the data across, R15, then PC, then PSW. */
411 "MOV.L [ R0 ], [ R15 ] \n"\
412 "MOV.L 4[ R0 ], 4[ R15 ] \n"\
413 "MOV.L 8[ R0 ], 8[ R15 ] \n"\
415 /* Move the interrupt stack pointer to its new correct position. */
418 /* All the rest of the registers are saved directly to the user stack. */
421 /* Save the rest of the general registers (R15 has been saved already). */
424 /* Save the FPSW and accumulators. */
427 "MVFACGU #0, A1, R15 \n"\
429 "MVFACHI #0, A1, R15 \n"\
431 "MVFACLO #0, A1, R15 \n" /* Low order word. */ \
433 "MVFACGU #0, A0, R15 \n"\
435 "MVFACHI #0, A0, R15 \n"\
437 "MVFACLO #0, A0, R15 \n" /* Low order word. */ \
440 #if ( configUSE_TASK_DPFPU_SUPPORT == 1 )
442 /* Does the task have a DPFPU context that needs saving? If
443 * ulPortTaskHasDPFPUContext is 0 then no. */
444 "MOV.L #_ulPortTaskHasDPFPUContext, R15 \n"\
445 "MOV.L [R15], R15 \n"\
448 /* Save the DPFPU context, if any. */
450 "DPUSHM.D DR0-DR15 \n"\
451 "DPUSHM.L DPSW-DECNT \n"\
454 /* Save ulPortTaskHasDPFPUContext itself. */
457 #elif ( configUSE_TASK_DPFPU_SUPPORT == 2 )
459 /* Save the DPFPU context, always. */
460 "DPUSHM.D DR0-DR15 \n"\
461 "DPUSHM.L DPSW-DECNT \n"\
463 #endif /* if ( configUSE_TASK_DPFPU_SUPPORT == 1 ) */
466 /* Save the stack pointer to the TCB. */
467 "MOV.L #_pxCurrentTCB, R15 \n"\
468 "MOV.L [ R15 ], R15 \n"\
469 "MOV.L R0, [ R15 ] \n"\
472 /* Ensure the interrupt mask is set to the syscall priority while the kernel
473 * structures are being accessed. */
476 /* Select the next task to run. */
477 "BSR.A _vTaskSwitchContext \n"\
479 /* Reset the interrupt mask as no more data structure access is required. */
483 /* Load the stack pointer of the task that is now selected as the Running
484 * state task from its TCB. */
485 "MOV.L #_pxCurrentTCB,R15 \n"\
486 "MOV.L [ R15 ], R15 \n"\
487 "MOV.L [ R15 ], R0 \n"\
490 /* Restore the context of the new task. The PSW (Program Status Word) and
491 * PC will be popped by the RTE instruction. */
493 #if ( configUSE_TASK_DPFPU_SUPPORT == 1 )
495 /* Is there a DPFPU context to restore? If the restored
496 * ulPortTaskHasDPFPUContext is zero then no. */
498 "MOV.L #_ulPortTaskHasDPFPUContext, R14 \n"\
499 "MOV.L R15, [R14] \n"\
502 /* Restore the DPFPU context, if any. */
504 "DPOPM.L DPSW-DECNT \n"\
505 "DPOPM.D DR0-DR15 \n"\
508 #elif ( configUSE_TASK_DPFPU_SUPPORT == 2 )
510 /* Restore the DPFPU context, always. */
511 "DPOPM.L DPSW-DECNT \n"\
512 "DPOPM.D DR0-DR15 \n"\
514 #endif /* if( configUSE_TASK_DPFPU_SUPPORT == 1 ) */
518 /* Accumulator low 32 bits. */
519 "MVTACLO R15, A0 \n"\
522 /* Accumulator high 32 bits. */
523 "MVTACHI R15, A0 \n"\
526 /* Accumulator guard. */
527 "MVTACGU R15, A0 \n"\
530 /* Accumulator low 32 bits. */
531 "MVTACLO R15, A1 \n"\
534 /* Accumulator high 32 bits. */
535 "MVTACHI R15, A1 \n"\
538 /* Accumulator guard. */
539 "MVTACGU R15, A1 \n"\
546 portCDT_NO_PARSE( :: ) "i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY ), "i" ( configKERNEL_INTERRUPT_PRIORITY )
549 /*-----------------------------------------------------------*/
551 #pragma vector = _VECT( configTICK_VECTOR )
552 __interrupt void vTickISR( void )
554 /* Re-enable interrupts. */
555 __enable_interrupt();
557 /* Increment the tick, and perform any processing the new tick value
558 * necessitates. Ensure IPL is at the max syscall value first. */
559 __set_interrupt_level( configMAX_SYSCALL_INTERRUPT_PRIORITY );
561 if( xTaskIncrementTick() != pdFALSE )
566 __set_interrupt_level( configKERNEL_INTERRUPT_PRIORITY );
568 /*-----------------------------------------------------------*/