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