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