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