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