1 /*
2  * FreeRTOS Kernel V11.1.0
3  * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * SPDX-License-Identifier: MIT
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
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.
23  *
24  * https://www.FreeRTOS.org
25  * https://github.com/FreeRTOS
26  *
27  */
28 
29 /*-----------------------------------------------------------
30 * Implementation of functions defined in portable.h for the RXv3 DPFPU port.
31 *----------------------------------------------------------*/
32 
33 #warning Testing for DFPU support in this port is not yet complete
34 
35 /* Scheduler includes. */
36 #include "FreeRTOS.h"
37 #include "task.h"
38 
39 /* Library includes. */
40 #include "string.h"
41 
42 /* Hardware specifics. */
43 #if ( configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H == 1 )
44 
45     #include "platform.h"
46 
47 #else /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
48 
49     #include "iodefine.h"
50 
51 #endif /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
52 
53 /*-----------------------------------------------------------*/
54 
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 )
62 
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 )
69 
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 )
73 
74 /*-----------------------------------------------------------*/
75 
76 /*
77  * Function to start the first task executing - written in asm code as direct
78  * access to registers is required.
79  */
80 static void prvStartFirstTask( void );
81 
82 /*
83  * Software interrupt handler.  Performs the actual context switch (saving and
84  * restoring of registers).  Written in asm code as direct register access is
85  * required.
86  */
87 __interrupt void vSoftwareInterruptISR( void );
88 
89 /*
90  * The tick ISR handler.  The peripheral used is configured by the application
91  * via a hook/callback function.
92  */
93 __interrupt void vTickISR( void );
94 
95 /*-----------------------------------------------------------*/
96 
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 )
100 
101     StackType_t ulPortTaskHasDPFPUContext = portNO_DPFPU_CONTEXT;
102 
103 #endif /* configUSE_TASK_DPFPU_SUPPORT */
104 
105 /* This is accessed by the inline assembler functions so is file scope for
106  * convenience. */
107 extern void * pxCurrentTCB;
108 extern void vTaskSwitchContext( void );
109 
110 /*-----------------------------------------------------------*/
111 
112 /*
113  * See header file for description.
114  */
pxPortInitialiseStack(StackType_t * pxTopOfStack,TaskFunction_t pxCode,void * pvParameters)115 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
116                                      TaskFunction_t pxCode,
117                                      void * pvParameters )
118 {
119     /* R0 is not included as it is the stack pointer. */
120 
121     *pxTopOfStack = 0x00;
122     pxTopOfStack--;
123     *pxTopOfStack = portINITIAL_PSW;
124     pxTopOfStack--;
125     *pxTopOfStack = ( StackType_t ) pxCode;
126 
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
131     {
132         pxTopOfStack--;
133         *pxTopOfStack = 0xffffffff; /* r15. */
134         pxTopOfStack--;
135         *pxTopOfStack = 0xeeeeeeee;
136         pxTopOfStack--;
137         *pxTopOfStack = 0xdddddddd;
138         pxTopOfStack--;
139         *pxTopOfStack = 0xcccccccc;
140         pxTopOfStack--;
141         *pxTopOfStack = 0xbbbbbbbb;
142         pxTopOfStack--;
143         *pxTopOfStack = 0xaaaaaaaa;
144         pxTopOfStack--;
145         *pxTopOfStack = 0x99999999;
146         pxTopOfStack--;
147         *pxTopOfStack = 0x88888888;
148         pxTopOfStack--;
149         *pxTopOfStack = 0x77777777;
150         pxTopOfStack--;
151         *pxTopOfStack = 0x66666666;
152         pxTopOfStack--;
153         *pxTopOfStack = 0x55555555;
154         pxTopOfStack--;
155         *pxTopOfStack = 0x44444444;
156         pxTopOfStack--;
157         *pxTopOfStack = 0x33333333;
158         pxTopOfStack--;
159         *pxTopOfStack = 0x22222222;
160         pxTopOfStack--;
161     }
162     #else /* ifdef USE_FULL_REGISTER_INITIALISATION */
163     {
164         pxTopOfStack -= 15;
165     }
166     #endif /* ifdef USE_FULL_REGISTER_INITIALISATION */
167 
168     *pxTopOfStack = ( StackType_t ) pvParameters; /* R1 */
169     pxTopOfStack--;
170     *pxTopOfStack = portINITIAL_FPSW;
171     pxTopOfStack--;
172     *pxTopOfStack = 0x11111111; /* Accumulator 1. */
173     pxTopOfStack--;
174     *pxTopOfStack = 0x22222222; /* Accumulator 1. */
175     pxTopOfStack--;
176     *pxTopOfStack = 0x33333333; /* Accumulator 1. */
177     pxTopOfStack--;
178     *pxTopOfStack = 0x44444444; /* Accumulator 0. */
179     pxTopOfStack--;
180     *pxTopOfStack = 0x55555555; /* Accumulator 0. */
181     pxTopOfStack--;
182     *pxTopOfStack = 0x66666666; /* Accumulator 0. */
183 
184     #if ( configUSE_TASK_DPFPU_SUPPORT == 1 )
185     {
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. */
189         pxTopOfStack--;
190         *pxTopOfStack = portNO_DPFPU_CONTEXT;
191     }
192     #elif ( configUSE_TASK_DPFPU_SUPPORT == 2 )
193     {
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
197         {
198             pxTopOfStack -= 2;
199             *( double * ) pxTopOfStack = 1515.1515;  /* DR15. */
200             pxTopOfStack -= 2;
201             *( double * ) pxTopOfStack = 1414.1414;  /* DR14. */
202             pxTopOfStack -= 2;
203             *( double * ) pxTopOfStack = 1313.1313;  /* DR13. */
204             pxTopOfStack -= 2;
205             *( double * ) pxTopOfStack = 1212.1212;  /* DR12. */
206             pxTopOfStack -= 2;
207             *( double * ) pxTopOfStack = 1111.1111;  /* DR11. */
208             pxTopOfStack -= 2;
209             *( double * ) pxTopOfStack = 1010.1010;  /* DR10. */
210             pxTopOfStack -= 2;
211             *( double * ) pxTopOfStack = 909.0909;   /* DR9. */
212             pxTopOfStack -= 2;
213             *( double * ) pxTopOfStack = 808.0808;   /* DR8. */
214             pxTopOfStack -= 2;
215             *( double * ) pxTopOfStack = 707.0707;   /* DR7. */
216             pxTopOfStack -= 2;
217             *( double * ) pxTopOfStack = 606.0606;   /* DR6. */
218             pxTopOfStack -= 2;
219             *( double * ) pxTopOfStack = 505.0505;   /* DR5. */
220             pxTopOfStack -= 2;
221             *( double * ) pxTopOfStack = 404.0404;   /* DR4. */
222             pxTopOfStack -= 2;
223             *( double * ) pxTopOfStack = 303.0303;   /* DR3. */
224             pxTopOfStack -= 2;
225             *( double * ) pxTopOfStack = 202.0202;   /* DR2. */
226             pxTopOfStack -= 2;
227             *( double * ) pxTopOfStack = 101.0101;   /* DR1. */
228             pxTopOfStack -= 2;
229             *( double * ) pxTopOfStack = 9876.54321; /* DR0. */
230         }
231         #else /* ifdef USE_FULL_REGISTER_INITIALISATION */
232         {
233             pxTopOfStack -= portDPFPU_DATA_REGISTER_WORDS;
234             memset( pxTopOfStack, 0x00, portDPFPU_DATA_REGISTER_WORDS * sizeof( StackType_t ) );
235         }
236         #endif /* ifdef USE_FULL_REGISTER_INITIALISATION */
237         pxTopOfStack--;
238         *pxTopOfStack = portINITIAL_DECNT; /* DECNT. */
239         pxTopOfStack--;
240         *pxTopOfStack = portINITIAL_DCMR;  /* DCMR. */
241         pxTopOfStack--;
242         *pxTopOfStack = portINITIAL_DPSW;  /* DPSW. */
243     }
244     #elif ( configUSE_TASK_DPFPU_SUPPORT == 0 )
245     {
246         /* Omit DPFPU support. */
247     }
248     #else /* if ( configUSE_TASK_DPFPU_SUPPORT == 1 ) */
249     {
250         #error Invalid configUSE_TASK_DPFPU_SUPPORT setting - configUSE_TASK_DPFPU_SUPPORT must be set to 0, 1, 2, or left undefined.
251     }
252     #endif /* if ( configUSE_TASK_DPFPU_SUPPORT == 1 ) */
253 
254     return pxTopOfStack;
255 }
256 /*-----------------------------------------------------------*/
257 
258 #if ( configUSE_TASK_DPFPU_SUPPORT == 1 )
259 
vPortTaskUsesDPFPU(void)260     void vPortTaskUsesDPFPU( void )
261     {
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;
265     }
266 
267 #endif /* configUSE_TASK_DPFPU_SUPPORT */
268 /*-----------------------------------------------------------*/
269 
xPortStartScheduler(void)270 BaseType_t xPortStartScheduler( void )
271 {
272     extern void vApplicationSetupTimerInterrupt( void );
273 
274     /* Use pxCurrentTCB just so it does not get optimised away. */
275     if( pxCurrentTCB != NULL )
276     {
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();
281 
282         /* Enable the software interrupt. */
283         _IEN( _ICU_SWINT ) = 1;
284 
285         /* Ensure the software interrupt is clear. */
286         _IR( _ICU_SWINT ) = 0;
287 
288         /* Ensure the software interrupt is set to the kernel priority. */
289         _IPR( _ICU_SWINT ) = configKERNEL_INTERRUPT_PRIORITY;
290 
291         /* Start the first task. */
292         prvStartFirstTask();
293     }
294 
295     /* Should not get here. */
296     return pdFAIL;
297 }
298 /*-----------------------------------------------------------*/
299 
vPortEndScheduler(void)300 void vPortEndScheduler( void )
301 {
302     /* Not implemented in ports where there is nothing to return to.
303      * Artificially force an assert. */
304     configASSERT( pxCurrentTCB == NULL );
305 
306     /* The following line is just to prevent the symbol getting optimised away. */
307     ( void ) vTaskSwitchContext();
308 }
309 /*-----------------------------------------------------------*/
310 
prvStartFirstTask(void)311 static void prvStartFirstTask( void )
312 {
313     __asm volatile
314     (
315 
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. */
319         "SETPSW     U                       \n" \
320 
321 
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" \
327 
328 
329         /* Restore the registers from the stack of the task pointed to by
330          * pxCurrentTCB. */
331 
332         #if ( configUSE_TASK_DPFPU_SUPPORT == 1 )
333 
334             /* The restored ulPortTaskHasDPFPUContext is to be zero here.
335              * So, it is never necessary to restore the DPFPU context here. */
336             "POP        R15                                 \n" \
337             "MOV.L      #_ulPortTaskHasDPFPUContext, R14    \n" \
338             "MOV.L      R15, [R14]                          \n" \
339 
340         #elif ( configUSE_TASK_DPFPU_SUPPORT == 2 )
341             /* Restore the DPFPU context. */
342             "DPOPM.L    DPSW-DECNT              \n" \
343             "DPOPM.D    DR0-DR15                \n" \
344 
345         #endif /* if ( configUSE_TASK_DPFPU_SUPPORT == 1 ) */
346 
347         "POP        R15                     \n" \
348 
349         /* Accumulator low 32 bits. */
350         "MVTACLO    R15, A0                 \n" \
351         "POP        R15                     \n" \
352 
353         /* Accumulator high 32 bits. */
354         "MVTACHI    R15, A0                 \n" \
355         "POP        R15                     \n" \
356 
357         /* Accumulator guard. */
358         "MVTACGU    R15, A0                 \n" \
359         "POP        R15                     \n" \
360 
361         /* Accumulator low 32 bits. */
362         "MVTACLO    R15, A1                 \n" \
363         "POP        R15                     \n" \
364 
365         /* Accumulator high 32 bits. */
366         "MVTACHI    R15, A1                 \n" \
367         "POP        R15                     \n" \
368 
369         /* Accumulator guard. */
370         "MVTACGU    R15, A1                 \n" \
371         "POP        R15                     \n" \
372 
373         /* Floating point status word. */
374         "MVTC       R15, FPSW               \n" \
375 
376         /* R1 to R15 - R0 is not included as it is the SP. */
377         "POPM       R1-R15                  \n" \
378 
379         /* This pops the remaining registers. */
380         "RTE                                \n" \
381         "NOP                                \n" \
382         "NOP                                \n"
383     );
384 }
385 /*-----------------------------------------------------------*/
386 
387 #pragma vector = VECT( ICU, SWINT )
vSoftwareInterruptISR(void)388 __interrupt void vSoftwareInterruptISR( void )
389 {
390     __asm volatile
391     (
392         /* Re-enable interrupts. */
393         "SETPSW     I                           \n" \
394 
395 
396         /* Move the data that was automatically pushed onto the interrupt stack when
397          * the interrupt occurred from the interrupt stack to the user stack.
398          *
399          * R15 is saved before it is clobbered. */
400         "PUSH.L     R15                         \n" \
401 
402         /* Read the user stack pointer. */
403         "MVFC       USP, R15                    \n" \
404 
405         /* Move the address down to the data being moved. */
406         "SUB        #12, R15                    \n" \
407         "MVTC       R15, USP                    \n" \
408 
409         /* Copy the data across, R15, then PC, then PSW. */
410         "MOV.L      [ R0 ], [ R15 ]             \n" \
411         "MOV.L      4[ R0 ], 4[ R15 ]           \n" \
412         "MOV.L      8[ R0 ], 8[ R15 ]           \n" \
413 
414         /* Move the interrupt stack pointer to its new correct position. */
415         "ADD        #12, R0                     \n" \
416 
417         /* All the rest of the registers are saved directly to the user stack. */
418         "SETPSW     U                           \n" \
419 
420         /* Save the rest of the general registers (R15 has been saved already). */
421         "PUSHM      R1-R14                      \n" \
422 
423         /* Save the FPSW and accumulators. */
424         "MVFC       FPSW, R15                   \n"                       \
425         "PUSH.L     R15                         \n"                       \
426         "MVFACGU    #0, A1, R15                 \n"                       \
427         "PUSH.L     R15                         \n"                       \
428         "MVFACHI    #0, A1, R15                 \n"                       \
429         "PUSH.L     R15                         \n"                       \
430         "MVFACLO    #0, A1, R15                 \n" /* Low order word. */ \
431         "PUSH.L     R15                         \n"                       \
432         "MVFACGU    #0, A0, R15                 \n"                       \
433         "PUSH.L     R15                         \n"                       \
434         "MVFACHI    #0, A0, R15                 \n"                       \
435         "PUSH.L     R15                         \n"                       \
436         "MVFACLO    #0, A0, R15                 \n" /* Low order word. */ \
437         "PUSH.L     R15                         \n"                       \
438 
439         #if ( configUSE_TASK_DPFPU_SUPPORT == 1 )
440 
441             /* Does the task have a DPFPU context that needs saving?  If
442              * ulPortTaskHasDPFPUContext is 0 then no. */
443             "MOV.L      #_ulPortTaskHasDPFPUContext, R15    \n" \
444             "MOV.L      [R15], R15                          \n" \
445             "CMP        #0, R15                             \n" \
446 
447             /* Save the DPFPU context, if any. */
448             "BEQ.B      __lab1                      \n" \
449             "DPUSHM.D   DR0-DR15                    \n" \
450             "DPUSHM.L   DPSW-DECNT                  \n" \
451             "__lab1:                                \n" \
452 
453             /* Save ulPortTaskHasDPFPUContext itself. */
454             "PUSH.L     R15                         \n" \
455 
456         #elif ( configUSE_TASK_DPFPU_SUPPORT == 2 )
457             /* Save the DPFPU context, always. */
458             "DPUSHM.D   DR0-DR15                    \n" \
459             "DPUSHM.L   DPSW-DECNT                  \n" \
460 
461         #endif /* if ( configUSE_TASK_DPFPU_SUPPORT == 1 ) */
462 
463 
464         /* Save the stack pointer to the TCB. */
465         "MOV.L      #_pxCurrentTCB, R15         \n" \
466         "MOV.L      [ R15 ], R15                \n" \
467         "MOV.L      R0, [ R15 ]                 \n" \
468 
469 
470         /* Ensure the interrupt mask is set to the syscall priority while the kernel
471          * structures are being accessed. */
472         "MVTIPL     %0                          \n" \
473 
474         /* Select the next task to run. */
475         "BSR.A      _vTaskSwitchContext         \n" \
476 
477         /* Reset the interrupt mask as no more data structure access is required. */
478         "MVTIPL     %1                          \n" \
479 
480 
481         /* Load the stack pointer of the task that is now selected as the Running
482          * state task from its TCB. */
483         "MOV.L      #_pxCurrentTCB,R15          \n" \
484         "MOV.L      [ R15 ], R15                \n" \
485         "MOV.L      [ R15 ], R0                 \n" \
486 
487 
488         /* Restore the context of the new task.  The PSW (Program Status Word) and
489          * PC will be popped by the RTE instruction. */
490 
491         #if ( configUSE_TASK_DPFPU_SUPPORT == 1 )
492 
493             /* Is there a DPFPU context to restore?  If the restored
494              * ulPortTaskHasDPFPUContext is zero then no. */
495             "POP        R15                                 \n" \
496             "MOV.L      #_ulPortTaskHasDPFPUContext, R14    \n" \
497             "MOV.L      R15, [R14]                          \n" \
498             "CMP        #0, R15                             \n" \
499 
500             /* Restore the DPFPU context, if any. */
501             "BEQ.B      __lab2                      \n" \
502             "DPOPM.L    DPSW-DECNT                  \n" \
503             "DPOPM.D    DR0-DR15                    \n" \
504             "__lab2:                                \n" \
505 
506         #elif ( configUSE_TASK_DPFPU_SUPPORT == 2 )
507             /* Restore the DPFPU context, always. */
508             "DPOPM.L    DPSW-DECNT                  \n" \
509             "DPOPM.D    DR0-DR15                    \n" \
510 
511         #endif /* if( configUSE_TASK_DPFPU_SUPPORT == 1 ) */
512 
513         "POP        R15                         \n" \
514 
515         /* Accumulator low 32 bits. */
516         "MVTACLO    R15, A0                     \n" \
517         "POP        R15                         \n" \
518 
519         /* Accumulator high 32 bits. */
520         "MVTACHI    R15, A0                     \n" \
521         "POP        R15                         \n" \
522 
523         /* Accumulator guard. */
524         "MVTACGU    R15, A0                     \n" \
525         "POP        R15                         \n" \
526 
527         /* Accumulator low 32 bits. */
528         "MVTACLO    R15, A1                     \n" \
529         "POP        R15                         \n" \
530 
531         /* Accumulator high 32 bits. */
532         "MVTACHI    R15, A1                     \n" \
533         "POP        R15                         \n" \
534 
535         /* Accumulator guard. */
536         "MVTACGU    R15, A1                     \n" \
537         "POP        R15                         \n" \
538         "MVTC       R15, FPSW                   \n" \
539         "POPM       R1-R15                      \n" \
540         "RTE                                    \n" \
541         "NOP                                    \n" \
542         "NOP                                      "
543         portCDT_NO_PARSE( ::) "i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY ), "i" ( configKERNEL_INTERRUPT_PRIORITY )
544     );
545 }
546 /*-----------------------------------------------------------*/
547 
548 #pragma vector = _VECT( configTICK_VECTOR )
vTickISR(void)549 __interrupt void vTickISR( void )
550 {
551     /* Re-enable interrupts. */
552     __enable_interrupt();
553 
554     /* Increment the tick, and perform any processing the new tick value
555      * necessitates.  Ensure IPL is at the max syscall value first. */
556     __set_interrupt_level( configMAX_SYSCALL_INTERRUPT_PRIORITY );
557     {
558         if( xTaskIncrementTick() != pdFALSE )
559         {
560             taskYIELD();
561         }
562     }
563     __set_interrupt_level( configKERNEL_INTERRUPT_PRIORITY );
564 }
565 /*-----------------------------------------------------------*/
566