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