]> begriffs open source - freertos/blob - portable/Renesas/RX600v2/port.c
Add SPDX-License-Identifier: MIT to MIT licensed files.
[freertos] / portable / Renesas / RX600v2 / port.c
1 /*\r
2  * FreeRTOS Kernel <DEVELOPMENT BRANCH>\r
3  * Copyright (C) 2021 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * SPDX-License-Identifier: MIT
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
8  * this software and associated documentation files (the "Software"), to deal in\r
9  * the Software without restriction, including without limitation the rights to\r
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
11  * the Software, and to permit persons to whom the Software is furnished to do so,\r
12  * subject to the following conditions:\r
13  *\r
14  * The above copyright notice and this permission notice shall be included in all\r
15  * copies or substantial portions of the Software.\r
16  *\r
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
19  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
20  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
23  *\r
24  * https://www.FreeRTOS.org\r
25  * https://github.com/FreeRTOS\r
26  *\r
27  */\r
28 \r
29 /*-----------------------------------------------------------\r
30  * Implementation of functions defined in portable.h for the RX600 port.\r
31  *----------------------------------------------------------*/\r
32 \r
33 /* Scheduler includes. */\r
34 #include "FreeRTOS.h"\r
35 #include "task.h"\r
36 \r
37 /* Library includes. */\r
38 #include "string.h"\r
39 \r
40 /* Hardware specifics. */\r
41 #if defined( configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H ) && ( configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H == 1 )\r
42         #include "platform.h"\r
43 #else\r
44         #include "iodefine.h"\r
45 #endif\r
46 \r
47 /*-----------------------------------------------------------*/\r
48 \r
49 /* Tasks should start with interrupts enabled and in Supervisor mode, therefore\r
50 PSW is set with U and I set, and PM and IPL clear. */\r
51 #define portINITIAL_PSW     ( ( StackType_t ) 0x00030000 )\r
52 #define portINITIAL_FPSW    ( ( StackType_t ) 0x00000100 )\r
53 \r
54 /*-----------------------------------------------------------*/\r
55 \r
56 /* The following lines are to ensure vSoftwareInterruptEntry can be referenced,\r
57  and therefore installed in the vector table, when the FreeRTOS code is built\r
58 as a library. */\r
59 extern BaseType_t vSoftwareInterruptEntry;\r
60 const BaseType_t * p_vSoftwareInterruptEntry = &vSoftwareInterruptEntry;\r
61 \r
62 /*-----------------------------------------------------------*/\r
63 \r
64 /*\r
65  * Function to start the first task executing - written in asm code as direct\r
66  * access to registers is required.\r
67  */\r
68 static void prvStartFirstTask( void );\r
69 \r
70 /*\r
71  * Software interrupt handler.  Performs the actual context switch (saving and\r
72  * restoring of registers).  Written in asm code as direct register access is\r
73  * required.\r
74  */\r
75 static void prvYieldHandler( void );\r
76 \r
77 /*\r
78  * The entry point for the software interrupt handler.  This is the function\r
79  * that calls the inline asm function prvYieldHandler().  It is installed in\r
80  * the vector table, but the code that installs it is in prvYieldHandler rather\r
81  * than using a #pragma.\r
82  */\r
83 void vSoftwareInterruptISR( void );\r
84 \r
85 /*-----------------------------------------------------------*/\r
86 \r
87 /* This is accessed by the inline assembler functions so is file scope for\r
88 convenience. */\r
89 extern void *pxCurrentTCB;\r
90 extern void vTaskSwitchContext( void );\r
91 \r
92 /*-----------------------------------------------------------*/\r
93 \r
94 /*\r
95  * See header file for description.\r
96  */\r
97 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
98 {\r
99         /* R0 is not included as it is the stack pointer. */\r
100 \r
101         *pxTopOfStack = 0x00;\r
102         pxTopOfStack--;\r
103         *pxTopOfStack = portINITIAL_PSW;\r
104         pxTopOfStack--;\r
105         *pxTopOfStack = ( StackType_t ) pxCode;\r
106 \r
107         /* When debugging it can be useful if every register is set to a known\r
108         value.  Otherwise code space can be saved by just setting the registers\r
109         that need to be set. */\r
110         #ifdef USE_FULL_REGISTER_INITIALISATION\r
111         {\r
112                 pxTopOfStack--;\r
113                 *pxTopOfStack = 0xffffffff;     /* r15. */\r
114                 pxTopOfStack--;\r
115                 *pxTopOfStack = 0xeeeeeeee;\r
116                 pxTopOfStack--;\r
117                 *pxTopOfStack = 0xdddddddd;\r
118                 pxTopOfStack--;\r
119                 *pxTopOfStack = 0xcccccccc;\r
120                 pxTopOfStack--;\r
121                 *pxTopOfStack = 0xbbbbbbbb;\r
122                 pxTopOfStack--;\r
123                 *pxTopOfStack = 0xaaaaaaaa;\r
124                 pxTopOfStack--;\r
125                 *pxTopOfStack = 0x99999999;\r
126                 pxTopOfStack--;\r
127                 *pxTopOfStack = 0x88888888;\r
128                 pxTopOfStack--;\r
129                 *pxTopOfStack = 0x77777777;\r
130                 pxTopOfStack--;\r
131                 *pxTopOfStack = 0x66666666;\r
132                 pxTopOfStack--;\r
133                 *pxTopOfStack = 0x55555555;\r
134                 pxTopOfStack--;\r
135                 *pxTopOfStack = 0x44444444;\r
136                 pxTopOfStack--;\r
137                 *pxTopOfStack = 0x33333333;\r
138                 pxTopOfStack--;\r
139                 *pxTopOfStack = 0x22222222;\r
140                 pxTopOfStack--;\r
141         }\r
142         #else\r
143         {\r
144                 pxTopOfStack -= 15;\r
145         }\r
146         #endif\r
147 \r
148         *pxTopOfStack = ( StackType_t ) pvParameters; /* R1 */\r
149         pxTopOfStack--;\r
150         *pxTopOfStack = portINITIAL_FPSW;\r
151         pxTopOfStack--;\r
152         *pxTopOfStack = 0x11111111; /* Accumulator 0. */\r
153         pxTopOfStack--;\r
154         *pxTopOfStack = 0x22222222; /* Accumulator 0. */\r
155         pxTopOfStack--;\r
156         *pxTopOfStack = 0x33333333; /* Accumulator 0. */\r
157         pxTopOfStack--;\r
158         *pxTopOfStack = 0x44444444; /* Accumulator 1. */\r
159         pxTopOfStack--;\r
160         *pxTopOfStack = 0x55555555; /* Accumulator 1. */\r
161         pxTopOfStack--;\r
162         *pxTopOfStack = 0x66666666; /* Accumulator 1. */\r
163 \r
164         return pxTopOfStack;\r
165 }\r
166 /*-----------------------------------------------------------*/\r
167 \r
168 BaseType_t xPortStartScheduler( void )\r
169 {\r
170 extern void vApplicationSetupTimerInterrupt( void );\r
171 \r
172         /* Use pxCurrentTCB just so it does not get optimised away. */\r
173         if( pxCurrentTCB != NULL )\r
174         {\r
175                 /* Call an application function to set up the timer that will generate the\r
176                 tick interrupt.  This way the application can decide which peripheral to\r
177                 use.  A demo application is provided to show a suitable example. */\r
178                 vApplicationSetupTimerInterrupt();\r
179 \r
180                 /* Enable the software interrupt. */\r
181                 _IEN( _ICU_SWINT ) = 1;\r
182 \r
183                 /* Ensure the software interrupt is clear. */\r
184                 _IR( _ICU_SWINT ) = 0;\r
185 \r
186                 /* Ensure the software interrupt is set to the kernel priority. */\r
187                 _IPR( _ICU_SWINT ) = configKERNEL_INTERRUPT_PRIORITY;\r
188 \r
189                 /* Start the first task. */\r
190                 prvStartFirstTask();\r
191         }\r
192 \r
193         /* Just to make sure the function is not optimised away. */\r
194         ( void ) vSoftwareInterruptISR();\r
195 \r
196         /* Should not get here. */\r
197         return pdFAIL;\r
198 }\r
199 /*-----------------------------------------------------------*/\r
200 \r
201 #pragma inline_asm prvStartFirstTask\r
202 static void prvStartFirstTask( void )\r
203 {\r
204         /* When starting the scheduler there is nothing that needs moving to the\r
205         interrupt stack because the function is not called from an interrupt.\r
206         Just ensure the current stack is the user stack. */\r
207         SETPSW  U\r
208 \r
209         /* Obtain the location of the stack associated with which ever task\r
210         pxCurrentTCB is currently pointing to. */\r
211         MOV.L   #_pxCurrentTCB, R15\r
212         MOV.L   [R15], R15\r
213         MOV.L   [R15], R0\r
214 \r
215         /* Restore the registers from the stack of the task pointed to by\r
216         pxCurrentTCB. */\r
217     POP         R15\r
218     MVTACLO     R15, A0         /* Accumulator low 32 bits. */\r
219     POP         R15\r
220     MVTACHI     R15, A0         /* Accumulator high 32 bits. */\r
221     POP         R15\r
222     MVTACGU     R15, A0         /* Accumulator guard. */\r
223     POP         R15\r
224     MVTACLO     R15, A1         /* Accumulator low 32 bits. */\r
225     POP         R15\r
226     MVTACHI     R15, A1         /* Accumulator high 32 bits. */\r
227     POP         R15\r
228     MVTACGU     R15, A1         /* Accumulator guard. */\r
229     POP         R15\r
230     MVTC        R15,FPSW        /* Floating point status word. */\r
231     POPM        R1-R15          /* R1 to R15 - R0 is not included as it is the SP. */\r
232     RTE                                 /* This pops the remaining registers. */\r
233     NOP\r
234     NOP\r
235 }\r
236 /*-----------------------------------------------------------*/\r
237 \r
238 #pragma interrupt ( vTickISR( vect = _VECT( configTICK_VECTOR ), enable ) )\r
239 void vTickISR( void )\r
240 {\r
241         /* Increment the tick, and perform any processing the new tick value\r
242         necessitates. */\r
243         set_ipl( configMAX_SYSCALL_INTERRUPT_PRIORITY );\r
244         {\r
245                 if( xTaskIncrementTick() != pdFALSE )\r
246                 {\r
247                         taskYIELD();\r
248                 }\r
249         }\r
250         set_ipl( configKERNEL_INTERRUPT_PRIORITY );\r
251 }\r
252 /*-----------------------------------------------------------*/\r
253 \r
254 void vSoftwareInterruptISR( void )\r
255 {\r
256         prvYieldHandler();\r
257 }\r
258 /*-----------------------------------------------------------*/\r
259 \r
260 #pragma inline_asm prvYieldHandler\r
261 static void prvYieldHandler( void )\r
262 {\r
263         /* Re-enable interrupts. */\r
264         SETPSW  I\r
265 \r
266         /* Move the data that was automatically pushed onto the interrupt stack when\r
267         the interrupt occurred from the interrupt stack to the user stack.\r
268 \r
269         R15 is saved before it is clobbered. */\r
270         PUSH.L  R15\r
271 \r
272         /* Read the user stack pointer. */\r
273         MVFC    USP, R15\r
274 \r
275         /* Move the address down to the data being moved. */\r
276         SUB             #12, R15\r
277         MVTC    R15, USP\r
278 \r
279         /* Copy the data across. */\r
280         MOV.L   [ R0 ], [ R15 ] ; R15\r
281         MOV.L   4[ R0 ], 4[ R15 ]  ; PC\r
282         MOV.L   8[ R0 ], 8[ R15 ]  ; PSW\r
283 \r
284         /* Move the interrupt stack pointer to its new correct position. */\r
285         ADD     #12, R0\r
286 \r
287         /* All the rest of the registers are saved directly to the user stack. */\r
288         SETPSW  U\r
289 \r
290         /* Save the rest of the general registers (R15 has been saved already). */\r
291         PUSHM   R1-R14\r
292 \r
293         /* Save the FPSW and accumulators. */\r
294         MVFC    FPSW, R15\r
295         PUSH.L  R15\r
296         MVFACGU #0, A1, R15\r
297         PUSH.L  R15\r
298         MVFACHI #0, A1, R15\r
299         PUSH.L  R15\r
300         MVFACLO #0, A1, R15     ; Low order word.\r
301         PUSH.L  R15\r
302         MVFACGU #0, A0, R15\r
303         PUSH.L  R15\r
304         MVFACHI #0, A0, R15\r
305         PUSH.L  R15\r
306         MVFACLO #0, A0, R15     ; Low order word.\r
307         PUSH.L  R15\r
308 \r
309         /* Save the stack pointer to the TCB. */\r
310         MOV.L   #_pxCurrentTCB, R15\r
311         MOV.L   [ R15 ], R15\r
312         MOV.L   R0, [ R15 ]\r
313 \r
314         /* Ensure the interrupt mask is set to the syscall priority while the kernel\r
315         structures are being accessed. */\r
316         MVTIPL  #configMAX_SYSCALL_INTERRUPT_PRIORITY\r
317 \r
318         /* Select the next task to run. */\r
319         BSR.A   _vTaskSwitchContext\r
320 \r
321         /* Reset the interrupt mask as no more data structure access is required. */\r
322         MVTIPL  #configKERNEL_INTERRUPT_PRIORITY\r
323 \r
324         /* Load the stack pointer of the task that is now selected as the Running\r
325         state task from its TCB. */\r
326         MOV.L   #_pxCurrentTCB,R15\r
327         MOV.L   [ R15 ], R15\r
328         MOV.L   [ R15 ], R0\r
329 \r
330         /* Restore the context of the new task.  The PSW (Program Status Word) and\r
331         PC will be popped by the RTE instruction. */\r
332     POP         R15\r
333     MVTACLO     R15, A0         /* Accumulator low 32 bits. */\r
334     POP         R15\r
335     MVTACHI     R15, A0         /* Accumulator high 32 bits. */\r
336     POP         R15\r
337     MVTACGU     R15, A0         /* Accumulator guard. */\r
338     POP         R15\r
339     MVTACLO     R15, A1         /* Accumulator low 32 bits. */\r
340     POP         R15\r
341     MVTACHI     R15, A1         /* Accumulator high 32 bits. */\r
342     POP         R15\r
343     MVTACGU     R15, A1         /* Accumulator guard. */\r
344         POP             R15\r
345         MVTC    R15,FPSW\r
346         POPM    R1-R15\r
347         RTE\r
348         NOP\r
349         NOP\r
350 }\r
351 /*-----------------------------------------------------------*/\r
352 \r
353 void vPortEndScheduler( void )\r
354 {\r
355         /* Not implemented in ports where there is nothing to return to.\r
356         Artificially force an assert. */\r
357         configASSERT( pxCurrentTCB == NULL );\r
358 \r
359         /* The following line is just to prevent the symbol getting optimised away. */\r
360         ( void ) vTaskSwitchContext();\r
361 }\r
362 /*-----------------------------------------------------------*/\r
363 \r
364 \r
365 \r