]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/Renesas/RX600v2/port.c
bbe03f163fa51d3c4be06c7bf832b877b9a60232
[freertos] / FreeRTOS / Source / portable / Renesas / RX600v2 / port.c
1 /*\r
2     FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
12 \r
13     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
14     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
15     >>!   obliged to provide the source code for proprietary components     !<<\r
16     >>!   outside of the FreeRTOS kernel.                                   !<<\r
17 \r
18     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
19     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
20     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
21     link: http://www.freertos.org/a00114.html\r
22 \r
23     1 tab == 4 spaces!\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    Having a problem?  Start by reading the FAQ "My application does   *\r
28      *    not run, what could be wrong?".  Have you defined configASSERT()?  *\r
29      *                                                                       *\r
30      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
31      *                                                                       *\r
32     ***************************************************************************\r
33 \r
34     ***************************************************************************\r
35      *                                                                       *\r
36      *    FreeRTOS provides completely free yet professionally developed,    *\r
37      *    robust, strictly quality controlled, supported, and cross          *\r
38      *    platform software that is more than just the market leader, it     *\r
39      *    is the industry's de facto standard.                               *\r
40      *                                                                       *\r
41      *    Help yourself get started quickly while simultaneously helping     *\r
42      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
43      *    tutorial book, reference manual, or both:                          *\r
44      *    http://www.FreeRTOS.org/Documentation                              *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     ***************************************************************************\r
49      *                                                                       *\r
50      *   Investing in training allows your team to be as productive as       *\r
51      *   possible as early as possible, lowering your overall development    *\r
52      *   cost, and enabling you to bring a more robust product to market     *\r
53      *   earlier than would otherwise be possible.  Richard Barry is both    *\r
54      *   the architect and key author of FreeRTOS, and so also the world's   *\r
55      *   leading authority on what is the world's most popular real time     *\r
56      *   kernel for deeply embedded MCU designs.  Obtaining your training    *\r
57      *   from Richard ensures your team will gain directly from his in-depth *\r
58      *   product knowledge and years of usage experience.  Contact Real Time *\r
59      *   Engineers Ltd to enquire about the FreeRTOS Masterclass, presented  *\r
60      *   by Richard Barry:  http://www.FreeRTOS.org/contact\r
61      *                                                                       *\r
62     ***************************************************************************\r
63 \r
64     ***************************************************************************\r
65      *                                                                       *\r
66      *    You are receiving this top quality software for free.  Please play *\r
67      *    fair and reciprocate by reporting any suspected issues and         *\r
68      *    participating in the community forum:                              *\r
69      *    http://www.FreeRTOS.org/support                                    *\r
70      *                                                                       *\r
71      *    Thank you!                                                         *\r
72      *                                                                       *\r
73     ***************************************************************************\r
74 \r
75     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
76     license and Real Time Engineers Ltd. contact details.\r
77 \r
78     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
79     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
80     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
81 \r
82     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
83     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
84 \r
85     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
86     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
87     licenses offer ticketed support, indemnification and commercial middleware.\r
88 \r
89     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
90     engineered and independently SIL3 certified version for use in safety and\r
91     mission critical applications that require provable dependability.\r
92 \r
93     1 tab == 4 spaces!\r
94 */\r
95 \r
96 /*-----------------------------------------------------------\r
97  * Implementation of functions defined in portable.h for the RX600 port.\r
98  *----------------------------------------------------------*/\r
99 \r
100 /* Scheduler includes. */\r
101 #include "FreeRTOS.h"\r
102 #include "task.h"\r
103 \r
104 /* Library includes. */\r
105 #include "string.h"\r
106 \r
107 /* Hardware specifics. */\r
108 #include "iodefine.h"\r
109 \r
110 /*-----------------------------------------------------------*/\r
111 \r
112 /* Tasks should start with interrupts enabled and in Supervisor mode, therefore\r
113 PSW is set with U and I set, and PM and IPL clear. */\r
114 #define portINITIAL_PSW     ( ( StackType_t ) 0x00030000 )\r
115 #define portINITIAL_FPSW    ( ( StackType_t ) 0x00000100 )\r
116 \r
117 /*-----------------------------------------------------------*/\r
118 \r
119 /* The following lines are to ensure vSoftwareInterruptEntry can be referenced,\r
120  and therefore installed in the vector table, when the FreeRTOS code is built\r
121 as a library. */\r
122 extern BaseType_t vSoftwareInterruptEntry;\r
123 const BaseType_t * p_vSoftwareInterruptEntry = &vSoftwareInterruptEntry;\r
124 \r
125 /*-----------------------------------------------------------*/\r
126 \r
127 /*\r
128  * Function to start the first task executing - written in asm code as direct\r
129  * access to registers is required.\r
130  */\r
131 static void prvStartFirstTask( void );\r
132 \r
133 /*\r
134  * Software interrupt handler.  Performs the actual context switch (saving and\r
135  * restoring of registers).  Written in asm code as direct register access is\r
136  * required.\r
137  */\r
138 static void prvYieldHandler( void );\r
139 \r
140 /*\r
141  * The entry point for the software interrupt handler.  This is the function\r
142  * that calls the inline asm function prvYieldHandler().  It is installed in\r
143  * the vector table, but the code that installs it is in prvYieldHandler rather\r
144  * than using a #pragma.\r
145  */\r
146 void vSoftwareInterruptISR( void );\r
147 \r
148 /*-----------------------------------------------------------*/\r
149 \r
150 /* This is accessed by the inline assembler functions so is file scope for\r
151 convenience. */\r
152 extern void *pxCurrentTCB;\r
153 extern void vTaskSwitchContext( void );\r
154 \r
155 /*-----------------------------------------------------------*/\r
156 \r
157 /*\r
158  * See header file for description.\r
159  */\r
160 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
161 {\r
162         /* R0 is not included as it is the stack pointer. */\r
163 \r
164         *pxTopOfStack = 0x00;\r
165         pxTopOfStack--;\r
166         *pxTopOfStack = portINITIAL_PSW;\r
167         pxTopOfStack--;\r
168         *pxTopOfStack = ( StackType_t ) pxCode;\r
169 \r
170         /* When debugging it can be useful if every register is set to a known\r
171         value.  Otherwise code space can be saved by just setting the registers\r
172         that need to be set. */\r
173         #ifdef USE_FULL_REGISTER_INITIALISATION\r
174         {\r
175                 pxTopOfStack--;\r
176                 *pxTopOfStack = 0xffffffff;     /* r15. */\r
177                 pxTopOfStack--;\r
178                 *pxTopOfStack = 0xeeeeeeee;\r
179                 pxTopOfStack--;\r
180                 *pxTopOfStack = 0xdddddddd;\r
181                 pxTopOfStack--;\r
182                 *pxTopOfStack = 0xcccccccc;\r
183                 pxTopOfStack--;\r
184                 *pxTopOfStack = 0xbbbbbbbb;\r
185                 pxTopOfStack--;\r
186                 *pxTopOfStack = 0xaaaaaaaa;\r
187                 pxTopOfStack--;\r
188                 *pxTopOfStack = 0x99999999;\r
189                 pxTopOfStack--;\r
190                 *pxTopOfStack = 0x88888888;\r
191                 pxTopOfStack--;\r
192                 *pxTopOfStack = 0x77777777;\r
193                 pxTopOfStack--;\r
194                 *pxTopOfStack = 0x66666666;\r
195                 pxTopOfStack--;\r
196                 *pxTopOfStack = 0x55555555;\r
197                 pxTopOfStack--;\r
198                 *pxTopOfStack = 0x44444444;\r
199                 pxTopOfStack--;\r
200                 *pxTopOfStack = 0x33333333;\r
201                 pxTopOfStack--;\r
202                 *pxTopOfStack = 0x22222222;\r
203                 pxTopOfStack--;\r
204         }\r
205         #else\r
206         {\r
207                 pxTopOfStack -= 15;\r
208         }\r
209         #endif\r
210 \r
211         *pxTopOfStack = ( StackType_t ) pvParameters; /* R1 */\r
212         pxTopOfStack--;\r
213         *pxTopOfStack = portINITIAL_FPSW;\r
214         pxTopOfStack--;\r
215         *pxTopOfStack = 0x11111111; /* Accumulator 0. */\r
216         pxTopOfStack--;\r
217         *pxTopOfStack = 0x22222222; /* Accumulator 0. */\r
218         pxTopOfStack--;\r
219         *pxTopOfStack = 0x33333333; /* Accumulator 0. */\r
220         pxTopOfStack--;\r
221         *pxTopOfStack = 0x44444444; /* Accumulator 1. */\r
222         pxTopOfStack--;\r
223         *pxTopOfStack = 0x55555555; /* Accumulator 1. */\r
224         pxTopOfStack--;\r
225         *pxTopOfStack = 0x66666666; /* Accumulator 1. */\r
226 \r
227         return pxTopOfStack;\r
228 }\r
229 /*-----------------------------------------------------------*/\r
230 \r
231 BaseType_t xPortStartScheduler( void )\r
232 {\r
233 extern void vApplicationSetupTimerInterrupt( void );\r
234 \r
235         /* Use pxCurrentTCB just so it does not get optimised away. */\r
236         if( pxCurrentTCB != NULL )\r
237         {\r
238                 /* Call an application function to set up the timer that will generate the\r
239                 tick interrupt.  This way the application can decide which peripheral to\r
240                 use.  A demo application is provided to show a suitable example. */\r
241                 vApplicationSetupTimerInterrupt();\r
242 \r
243                 /* Enable the software interrupt. */\r
244                 _IEN( _ICU_SWINT ) = 1;\r
245 \r
246                 /* Ensure the software interrupt is clear. */\r
247                 _IR( _ICU_SWINT ) = 0;\r
248 \r
249                 /* Ensure the software interrupt is set to the kernel priority. */\r
250                 _IPR( _ICU_SWINT ) = configKERNEL_INTERRUPT_PRIORITY;\r
251 \r
252                 /* Start the first task. */\r
253                 prvStartFirstTask();\r
254         }\r
255 \r
256         /* Just to make sure the function is not optimised away. */\r
257         ( void ) vSoftwareInterruptISR();\r
258 \r
259         /* Should not get here. */\r
260         return pdFAIL;\r
261 }\r
262 /*-----------------------------------------------------------*/\r
263 \r
264 #pragma inline_asm prvStartFirstTask\r
265 static void prvStartFirstTask( void )\r
266 {\r
267         /* When starting the scheduler there is nothing that needs moving to the\r
268         interrupt stack because the function is not called from an interrupt.\r
269         Just ensure the current stack is the user stack. */\r
270         SETPSW  U\r
271 \r
272         /* Obtain the location of the stack associated with which ever task\r
273         pxCurrentTCB is currently pointing to. */\r
274         MOV.L   #_pxCurrentTCB, R15\r
275         MOV.L   [R15], R15\r
276         MOV.L   [R15], R0\r
277 \r
278         /* Restore the registers from the stack of the task pointed to by\r
279         pxCurrentTCB. */\r
280     POP         R15\r
281     MVTACLO     R15, A0         /* Accumulator low 32 bits. */\r
282     POP         R15\r
283     MVTACHI     R15, A0         /* Accumulator high 32 bits. */\r
284     POP         R15\r
285     MVTACGU     R15, A0         /* Accumulator guard. */\r
286     POP         R15\r
287     MVTACLO     R15, A1         /* Accumulator low 32 bits. */\r
288     POP         R15\r
289     MVTACHI     R15, A1         /* Accumulator high 32 bits. */\r
290     POP         R15\r
291     MVTACGU     R15, A1         /* Accumulator guard. */\r
292     POP         R15\r
293     MVTC        R15,FPSW        /* Floating point status word. */\r
294     POPM        R1-R15          /* R1 to R15 - R0 is not included as it is the SP. */\r
295     RTE                                 /* This pops the remaining registers. */\r
296     NOP\r
297     NOP\r
298 }\r
299 /*-----------------------------------------------------------*/\r
300 \r
301 #pragma interrupt ( vTickISR( vect = _VECT( configTICK_VECTOR ), enable ) )\r
302 void vTickISR( void )\r
303 {\r
304         /* Increment the tick, and perform any processing the new tick value\r
305         necessitates. */\r
306         set_ipl( configMAX_SYSCALL_INTERRUPT_PRIORITY );\r
307         {\r
308                 if( xTaskIncrementTick() != pdFALSE )\r
309                 {\r
310                         taskYIELD();\r
311                 }\r
312         }\r
313         set_ipl( configKERNEL_INTERRUPT_PRIORITY );\r
314 }\r
315 /*-----------------------------------------------------------*/\r
316 \r
317 void vSoftwareInterruptISR( void )\r
318 {\r
319         prvYieldHandler();\r
320 }\r
321 /*-----------------------------------------------------------*/\r
322 \r
323 #pragma inline_asm prvYieldHandler\r
324 static void prvYieldHandler( void )\r
325 {\r
326         /* Re-enable interrupts. */\r
327         SETPSW  I\r
328 \r
329         /* Move the data that was automatically pushed onto the interrupt stack when\r
330         the interrupt occurred from the interrupt stack to the user stack.\r
331 \r
332         R15 is saved before it is clobbered. */\r
333         PUSH.L  R15\r
334 \r
335         /* Read the user stack pointer. */\r
336         MVFC    USP, R15\r
337 \r
338         /* Move the address down to the data being moved. */\r
339         SUB             #12, R15\r
340         MVTC    R15, USP\r
341 \r
342         /* Copy the data across. */\r
343         MOV.L   [ R0 ], [ R15 ] ; R15\r
344         MOV.L   4[ R0 ], 4[ R15 ]  ; PC\r
345         MOV.L   8[ R0 ], 8[ R15 ]  ; PSW\r
346 \r
347         /* Move the interrupt stack pointer to its new correct position. */\r
348         ADD     #12, R0\r
349 \r
350         /* All the rest of the registers are saved directly to the user stack. */\r
351         SETPSW  U\r
352 \r
353         /* Save the rest of the general registers (R15 has been saved already). */\r
354         PUSHM   R1-R14\r
355 \r
356         /* Save the FPSW and accumulators. */\r
357         MVFC    FPSW, R15\r
358         PUSH.L  R15\r
359         MVFACGU #0, A1, R15\r
360         PUSH.L  R15\r
361         MVFACHI #0, A1, R15\r
362         PUSH.L  R15\r
363         MVFACLO #0, A1, R15     ; Low order word.\r
364         PUSH.L  R15\r
365         MVFACGU #0, A0, R15\r
366         PUSH.L  R15\r
367         MVFACHI #0, A0, R15\r
368         PUSH.L  R15\r
369         MVFACLO #0, A0, R15     ; Low order word.\r
370         PUSH.L  R15\r
371 \r
372         /* Save the stack pointer to the TCB. */\r
373         MOV.L   #_pxCurrentTCB, R15\r
374         MOV.L   [ R15 ], R15\r
375         MOV.L   R0, [ R15 ]\r
376 \r
377         /* Ensure the interrupt mask is set to the syscall priority while the kernel\r
378         structures are being accessed. */\r
379         MVTIPL  #configMAX_SYSCALL_INTERRUPT_PRIORITY\r
380 \r
381         /* Select the next task to run. */\r
382         BSR.A   _vTaskSwitchContext\r
383 \r
384         /* Reset the interrupt mask as no more data structure access is required. */\r
385         MVTIPL  #configKERNEL_INTERRUPT_PRIORITY\r
386 \r
387         /* Load the stack pointer of the task that is now selected as the Running\r
388         state task from its TCB. */\r
389         MOV.L   #_pxCurrentTCB,R15\r
390         MOV.L   [ R15 ], R15\r
391         MOV.L   [ R15 ], R0\r
392 \r
393         /* Restore the context of the new task.  The PSW (Program Status Word) and\r
394         PC will be popped by the RTE instruction. */\r
395     POP         R15\r
396     MVTACLO     R15, A0         /* Accumulator low 32 bits. */\r
397     POP         R15\r
398     MVTACHI     R15, A0         /* Accumulator high 32 bits. */\r
399     POP         R15\r
400     MVTACGU     R15, A0         /* Accumulator guard. */\r
401     POP         R15\r
402     MVTACLO     R15, A1         /* Accumulator low 32 bits. */\r
403     POP         R15\r
404     MVTACHI     R15, A1         /* Accumulator high 32 bits. */\r
405     POP         R15\r
406     MVTACGU     R15, A1         /* Accumulator guard. */\r
407         POP             R15\r
408         MVTC    R15,FPSW\r
409         POPM    R1-R15\r
410         RTE\r
411         NOP\r
412         NOP\r
413 }\r
414 /*-----------------------------------------------------------*/\r
415 \r
416 void vPortEndScheduler( void )\r
417 {\r
418         /* Not implemented in ports where there is nothing to return to.\r
419         Artificially force an assert. */\r
420         configASSERT( pxCurrentTCB == NULL );\r
421 \r
422         /* The following line is just to prevent the symbol getting optimised away. */\r
423         ( void ) vTaskSwitchContext();\r
424 }\r
425 /*-----------------------------------------------------------*/\r
426 \r
427 \r
428 \r