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 RX600 port.
31  *----------------------------------------------------------*/
32 
33 /* Scheduler includes. */
34 #include "FreeRTOS.h"
35 #include "task.h"
36 
37 /* Library includes. */
38 #include "string.h"
39 
40 /* Hardware specifics. */
41 #include "iodefine.h"
42 
43 /*-----------------------------------------------------------*/
44 
45 /* Tasks should start with interrupts enabled and in Supervisor mode, therefore
46 PSW is set with U and I set, and PM and IPL clear. */
47 #define portINITIAL_PSW     ( ( StackType_t ) 0x00030000 )
48 #define portINITIAL_FPSW    ( ( StackType_t ) 0x00000100 )
49 
50 /*-----------------------------------------------------------*/
51 
52 /* The following lines are to ensure vSoftwareInterruptEntry can be referenced,
53  and therefore installed in the vector table, when the FreeRTOS code is built
54 as a library. */
55 extern BaseType_t vSoftwareInterruptEntry;
56 const BaseType_t * p_vSoftwareInterruptEntry = &vSoftwareInterruptEntry;
57 
58 /*-----------------------------------------------------------*/
59 
60 /*
61  * Function to start the first task executing - written in asm code as direct
62  * access to registers is required.
63  */
64 static void prvStartFirstTask( void );
65 
66 /*
67  * Software interrupt handler.  Performs the actual context switch (saving and
68  * restoring of registers).  Written in asm code as direct register access is
69  * required.
70  */
71 static void prvYieldHandler( void );
72 
73 /*
74  * The entry point for the software interrupt handler.  This is the function
75  * that calls the inline asm function prvYieldHandler().  It is installed in
76  * the vector table, but the code that installs it is in prvYieldHandler rather
77  * than using a #pragma.
78  */
79 void vSoftwareInterruptISR( void );
80 
81 /*-----------------------------------------------------------*/
82 
83 /* This is accessed by the inline assembler functions so is file scope for
84 convenience. */
85 extern void *pxCurrentTCB;
86 extern void vTaskSwitchContext( void );
87 
88 /*-----------------------------------------------------------*/
89 
90 /*
91  * See header file for description.
92  */
pxPortInitialiseStack(StackType_t * pxTopOfStack,TaskFunction_t pxCode,void * pvParameters)93 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
94 {
95     /* R0 is not included as it is the stack pointer. */
96 
97     *pxTopOfStack = 0x00;
98     pxTopOfStack--;
99     *pxTopOfStack = portINITIAL_PSW;
100     pxTopOfStack--;
101     *pxTopOfStack = ( StackType_t ) pxCode;
102 
103     /* When debugging it can be useful if every register is set to a known
104     value.  Otherwise code space can be saved by just setting the registers
105     that need to be set. */
106     #ifdef USE_FULL_REGISTER_INITIALISATION
107     {
108         pxTopOfStack--;
109         *pxTopOfStack = 0xffffffff; /* r15. */
110         pxTopOfStack--;
111         *pxTopOfStack = 0xeeeeeeee;
112         pxTopOfStack--;
113         *pxTopOfStack = 0xdddddddd;
114         pxTopOfStack--;
115         *pxTopOfStack = 0xcccccccc;
116         pxTopOfStack--;
117         *pxTopOfStack = 0xbbbbbbbb;
118         pxTopOfStack--;
119         *pxTopOfStack = 0xaaaaaaaa;
120         pxTopOfStack--;
121         *pxTopOfStack = 0x99999999;
122         pxTopOfStack--;
123         *pxTopOfStack = 0x88888888;
124         pxTopOfStack--;
125         *pxTopOfStack = 0x77777777;
126         pxTopOfStack--;
127         *pxTopOfStack = 0x66666666;
128         pxTopOfStack--;
129         *pxTopOfStack = 0x55555555;
130         pxTopOfStack--;
131         *pxTopOfStack = 0x44444444;
132         pxTopOfStack--;
133         *pxTopOfStack = 0x33333333;
134         pxTopOfStack--;
135         *pxTopOfStack = 0x22222222;
136         pxTopOfStack--;
137     }
138     #else
139     {
140         pxTopOfStack -= 15;
141     }
142     #endif
143 
144     *pxTopOfStack = ( StackType_t ) pvParameters; /* R1 */
145     pxTopOfStack--;
146     *pxTopOfStack = portINITIAL_FPSW;
147     pxTopOfStack--;
148     *pxTopOfStack = 0x12345678; /* Accumulator. */
149     pxTopOfStack--;
150     *pxTopOfStack = 0x87654321; /* Accumulator. */
151 
152     return pxTopOfStack;
153 }
154 /*-----------------------------------------------------------*/
155 
xPortStartScheduler(void)156 BaseType_t xPortStartScheduler( void )
157 {
158 extern void vApplicationSetupTimerInterrupt( void );
159 
160     /* Use pxCurrentTCB just so it does not get optimised away. */
161     if( pxCurrentTCB != NULL )
162     {
163         /* Call an application function to set up the timer that will generate the
164         tick interrupt.  This way the application can decide which peripheral to
165         use.  A demo application is provided to show a suitable example. */
166         vApplicationSetupTimerInterrupt();
167 
168         /* Enable the software interrupt. */
169         _IEN( _ICU_SWINT ) = 1;
170 
171         /* Ensure the software interrupt is clear. */
172         _IR( _ICU_SWINT ) = 0;
173 
174         /* Ensure the software interrupt is set to the kernel priority. */
175         _IPR( _ICU_SWINT ) = configKERNEL_INTERRUPT_PRIORITY;
176 
177         /* Start the first task. */
178         prvStartFirstTask();
179     }
180 
181     /* Just to make sure the function is not optimised away. */
182     ( void ) vSoftwareInterruptISR();
183 
184     /* Should not get here. */
185     return pdFAIL;
186 }
187 /*-----------------------------------------------------------*/
188 
189 #pragma inline_asm prvStartFirstTask
prvStartFirstTask(void)190 static void prvStartFirstTask( void )
191 {
192     /* When starting the scheduler there is nothing that needs moving to the
193     interrupt stack because the function is not called from an interrupt.
194     Just ensure the current stack is the user stack. */
195     SETPSW  U
196 
197     /* Obtain the location of the stack associated with which ever task
198     pxCurrentTCB is currently pointing to. */
199     MOV.L   #_pxCurrentTCB, R15
200     MOV.L   [R15], R15
201     MOV.L   [R15], R0
202 
203     /* Restore the registers from the stack of the task pointed to by
204     pxCurrentTCB. */
205     POP     R15
206     MVTACLO R15         /* Accumulator low 32 bits. */
207     POP     R15
208     MVTACHI R15         /* Accumulator high 32 bits. */
209     POP     R15
210     MVTC    R15,FPSW    /* Floating point status word. */
211     POPM    R1-R15      /* R1 to R15 - R0 is not included as it is the SP. */
212     RTE                 /* This pops the remaining registers. */
213     NOP
214     NOP
215 }
216 /*-----------------------------------------------------------*/
217 
218 #pragma interrupt ( vTickISR( vect = _VECT( configTICK_VECTOR ), enable ) )
vTickISR(void)219 void vTickISR( void )
220 {
221     /* Increment the tick, and perform any processing the new tick value
222     necessitates. */
223     set_ipl( configMAX_SYSCALL_INTERRUPT_PRIORITY );
224     {
225         if( xTaskIncrementTick() != pdFALSE )
226         {
227             taskYIELD();
228         }
229     }
230     set_ipl( configKERNEL_INTERRUPT_PRIORITY );
231 }
232 /*-----------------------------------------------------------*/
233 
vSoftwareInterruptISR(void)234 void vSoftwareInterruptISR( void )
235 {
236     prvYieldHandler();
237 }
238 /*-----------------------------------------------------------*/
239 
240 #pragma inline_asm prvYieldHandler
prvYieldHandler(void)241 static void prvYieldHandler( void )
242 {
243     /* Re-enable interrupts. */
244     SETPSW  I
245 
246     /* Move the data that was automatically pushed onto the interrupt stack when
247     the interrupt occurred from the interrupt stack to the user stack.
248 
249     R15 is saved before it is clobbered. */
250     PUSH.L  R15
251 
252     /* Read the user stack pointer. */
253     MVFC    USP, R15
254 
255     /* Move the address down to the data being moved. */
256     SUB     #12, R15
257     MVTC    R15, USP
258 
259     /* Copy the data across. */
260     MOV.L   [ R0 ], [ R15 ] ; R15
261     MOV.L   4[ R0 ], 4[ R15 ]  ; PC
262     MOV.L   8[ R0 ], 8[ R15 ]  ; PSW
263 
264     /* Move the interrupt stack pointer to its new correct position. */
265     ADD #12, R0
266 
267     /* All the rest of the registers are saved directly to the user stack. */
268     SETPSW  U
269 
270     /* Save the rest of the general registers (R15 has been saved already). */
271     PUSHM   R1-R14
272 
273     /* Save the FPSW and accumulator. */
274     MVFC    FPSW, R15
275     PUSH.L  R15
276     MVFACHI R15
277     PUSH.L  R15
278     MVFACMI R15 ; Middle order word.
279     SHLL    #16, R15 ; Shifted left as it is restored to the low order word.
280     PUSH.L  R15
281 
282     /* Save the stack pointer to the TCB. */
283     MOV.L   #_pxCurrentTCB, R15
284     MOV.L   [ R15 ], R15
285     MOV.L   R0, [ R15 ]
286 
287     /* Ensure the interrupt mask is set to the syscall priority while the kernel
288     structures are being accessed. */
289     MVTIPL  #configMAX_SYSCALL_INTERRUPT_PRIORITY
290 
291     /* Select the next task to run. */
292     BSR.A   _vTaskSwitchContext
293 
294     /* Reset the interrupt mask as no more data structure access is required. */
295     MVTIPL  #configKERNEL_INTERRUPT_PRIORITY
296 
297     /* Load the stack pointer of the task that is now selected as the Running
298     state task from its TCB. */
299     MOV.L   #_pxCurrentTCB,R15
300     MOV.L   [ R15 ], R15
301     MOV.L   [ R15 ], R0
302 
303     /* Restore the context of the new task.  The PSW (Program Status Word) and
304     PC will be popped by the RTE instruction. */
305     POP     R15
306     MVTACLO R15
307     POP     R15
308     MVTACHI R15
309     POP     R15
310     MVTC    R15,FPSW
311     POPM    R1-R15
312     RTE
313     NOP
314     NOP
315 }
316 /*-----------------------------------------------------------*/
317 
vPortEndScheduler(void)318 void vPortEndScheduler( void )
319 {
320     /* Not implemented in ports where there is nothing to return to.
321     Artificially force an assert. */
322     configASSERT( pxCurrentTCB == NULL );
323 
324     /* The following line is just to prevent the symbol getting optimised away. */
325     ( void ) vTaskSwitchContext();
326 }
327 /*-----------------------------------------------------------*/
328