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