]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/Renesas/RX200/port.c
4db42778f35c8d0c2e572412e7581c1d2951a59b
[freertos] / FreeRTOS / Source / portable / Renesas / RX200 / 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 RX200 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 \r
116 /*-----------------------------------------------------------*/\r
117 \r
118 /* The following lines are to ensure vSoftwareInterruptEntry can be referenced,\r
119  and therefore installed in the vector table, when the FreeRTOS code is built\r
120 as a library. */\r
121 extern BaseType_t vSoftwareInterruptEntry;\r
122 const BaseType_t * p_vSoftwareInterruptEntry = &vSoftwareInterruptEntry;\r
123 \r
124 /*-----------------------------------------------------------*/\r
125 \r
126 /*\r
127  * Function to start the first task executing - written in asm code as direct\r
128  * access to registers is required.\r
129  */\r
130 static void prvStartFirstTask( void );\r
131 \r
132 /*\r
133  * Software interrupt handler.  Performs the actual context switch (saving and\r
134  * restoring of registers).  Written in asm code as direct register access is\r
135  * required.\r
136  */\r
137 static void prvYieldHandler( void );\r
138 \r
139 /*\r
140  * The entry point for the software interrupt handler.  This is the function\r
141  * that calls the inline asm function prvYieldHandler().  It is installed in\r
142  * the vector table, but the code that installs it is in prvYieldHandler rather\r
143  * than using a #pragma.\r
144  */\r
145 void vSoftwareInterruptISR( void );\r
146 \r
147 /*-----------------------------------------------------------*/\r
148 \r
149 /* This is accessed by the inline assembler functions so is file scope for\r
150 convenience. */\r
151 extern void *pxCurrentTCB;\r
152 extern void vTaskSwitchContext( void );\r
153 \r
154 /*-----------------------------------------------------------*/\r
155 \r
156 /*\r
157  * See header file for description.\r
158  */\r
159 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
160 {\r
161         /* Offset to end up on 8 byte boundary. */\r
162         pxTopOfStack--;\r
163 \r
164         /* R0 is not included as it is the stack pointer. */\r
165         *pxTopOfStack = 0x00;\r
166         pxTopOfStack--;\r
167     *pxTopOfStack = 0x00;\r
168         pxTopOfStack--;\r
169         *pxTopOfStack = portINITIAL_PSW;\r
170         pxTopOfStack--;\r
171         *pxTopOfStack = ( StackType_t ) pxCode;\r
172 \r
173         /* When debugging it can be useful if every register is set to a known\r
174         value.  Otherwise code space can be saved by just setting the registers\r
175         that need to be set. */\r
176         #ifdef USE_FULL_REGISTER_INITIALISATION\r
177         {\r
178                 pxTopOfStack--;\r
179                 *pxTopOfStack = 0x12345678;     /* r15. */\r
180                 pxTopOfStack--;\r
181                 *pxTopOfStack = 0xaaaabbbb;\r
182                 pxTopOfStack--;\r
183                 *pxTopOfStack = 0xdddddddd;\r
184                 pxTopOfStack--;\r
185                 *pxTopOfStack = 0xcccccccc;\r
186                 pxTopOfStack--;\r
187                 *pxTopOfStack = 0xbbbbbbbb;\r
188                 pxTopOfStack--;\r
189                 *pxTopOfStack = 0xaaaaaaaa;\r
190                 pxTopOfStack--;\r
191                 *pxTopOfStack = 0x99999999;\r
192                 pxTopOfStack--;\r
193                 *pxTopOfStack = 0x88888888;\r
194                 pxTopOfStack--;\r
195                 *pxTopOfStack = 0x77777777;\r
196                 pxTopOfStack--;\r
197                 *pxTopOfStack = 0x66666666;\r
198                 pxTopOfStack--;\r
199                 *pxTopOfStack = 0x55555555;\r
200                 pxTopOfStack--;\r
201                 *pxTopOfStack = 0x44444444;\r
202                 pxTopOfStack--;\r
203                 *pxTopOfStack = 0x33333333;\r
204                 pxTopOfStack--;\r
205                 *pxTopOfStack = 0x22222222;\r
206                 pxTopOfStack--;\r
207         }\r
208         #else\r
209         {\r
210                 pxTopOfStack -= 15;\r
211         }\r
212         #endif\r
213 \r
214         *pxTopOfStack = ( StackType_t ) pvParameters; /* R1 */\r
215         pxTopOfStack--;\r
216         *pxTopOfStack = 0x12345678; /* Accumulator. */\r
217         pxTopOfStack--;\r
218         *pxTopOfStack = 0x87654321; /* Accumulator. */\r
219 \r
220         return pxTopOfStack;\r
221 }\r
222 /*-----------------------------------------------------------*/\r
223 \r
224 BaseType_t xPortStartScheduler( void )\r
225 {\r
226 extern void vApplicationSetupTimerInterrupt( void );\r
227 \r
228         /* Use pxCurrentTCB just so it does not get optimised away. */\r
229         if( pxCurrentTCB != NULL )\r
230         {\r
231                 /* Call an application function to set up the timer that will generate the\r
232                 tick interrupt.  This way the application can decide which peripheral to\r
233                 use.  A demo application is provided to show a suitable example. */\r
234                 vApplicationSetupTimerInterrupt();\r
235 \r
236                 /* Enable the software interrupt. */\r
237                 _IEN( _ICU_SWINT ) = 1;\r
238 \r
239                 /* Ensure the software interrupt is clear. */\r
240                 _IR( _ICU_SWINT ) = 0;\r
241 \r
242                 /* Ensure the software interrupt is set to the kernel priority. */\r
243                 _IPR( _ICU_SWINT ) = configKERNEL_INTERRUPT_PRIORITY;\r
244 \r
245                 /* Start the first task. */\r
246                 prvStartFirstTask();\r
247         }\r
248 \r
249         /* Just to make sure the function is not optimised away. */\r
250         ( void ) vSoftwareInterruptISR();\r
251 \r
252         /* Should not get here. */\r
253         return pdFAIL;\r
254 }\r
255 /*-----------------------------------------------------------*/\r
256 \r
257 #pragma inline_asm prvStartFirstTask\r
258 static void prvStartFirstTask( void )\r
259 {\r
260         /* When starting the scheduler there is nothing that needs moving to the\r
261         interrupt stack because the function is not called from an interrupt.\r
262         Just ensure the current stack is the user stack. */\r
263         SETPSW  U\r
264 \r
265         /* Obtain the location of the stack associated with which ever task\r
266         pxCurrentTCB is currently pointing to. */\r
267         MOV.L   #_pxCurrentTCB, R15\r
268         MOV.L   [R15], R15\r
269         MOV.L   [R15], R0\r
270 \r
271         /* Restore the registers from the stack of the task pointed to by\r
272         pxCurrentTCB. */\r
273     POP         R15\r
274     MVTACLO     R15             /* Accumulator low 32 bits. */\r
275     POP         R15\r
276     MVTACHI     R15             /* Accumulator high 32 bits. */\r
277     POPM        R1-R15          /* R1 to R15 - R0 is not included as it is the SP. */\r
278     RTE                                 /* This pops the remaining registers. */\r
279     NOP\r
280     NOP\r
281 }\r
282 /*-----------------------------------------------------------*/\r
283 \r
284 #pragma interrupt ( vTickISR( vect = _VECT( configTICK_VECTOR ), enable ) )\r
285 void vTickISR( void )\r
286 {\r
287         /* Increment the tick, and perform any processing the new tick value\r
288         necessitates. */\r
289         set_ipl( configMAX_SYSCALL_INTERRUPT_PRIORITY );\r
290         {\r
291                 if( xTaskIncrementTick() != pdFALSE )\r
292                 {\r
293                         taskYIELD();\r
294                 }\r
295         }\r
296         set_ipl( configKERNEL_INTERRUPT_PRIORITY );\r
297 }\r
298 /*-----------------------------------------------------------*/\r
299 \r
300 void vSoftwareInterruptISR( void )\r
301 {\r
302         prvYieldHandler();\r
303 }\r
304 /*-----------------------------------------------------------*/\r
305 \r
306 #pragma inline_asm prvYieldHandler\r
307 static void prvYieldHandler( void )\r
308 {\r
309         /* Re-enable interrupts. */\r
310         SETPSW  I\r
311 \r
312         /* Move the data that was automatically pushed onto the interrupt stack when\r
313         the interrupt occurred from the interrupt stack to the user stack.\r
314 \r
315         R15 is saved before it is clobbered. */\r
316         PUSH.L  R15\r
317 \r
318         /* Read the user stack pointer. */\r
319         MVFC    USP, R15\r
320 \r
321         /* Move the address down to the data being moved. */\r
322         SUB             #12, R15\r
323         MVTC    R15, USP\r
324 \r
325         /* Copy the data across. */\r
326         MOV.L   [ R0 ], [ R15 ] ; R15\r
327         MOV.L   4[ R0 ], 4[ R15 ]  ; PC\r
328         MOV.L   8[ R0 ], 8[ R15 ]  ; PSW\r
329 \r
330         /* Move the interrupt stack pointer to its new correct position. */\r
331         ADD     #12, R0\r
332 \r
333         /* All the rest of the registers are saved directly to the user stack. */\r
334         SETPSW  U\r
335 \r
336         /* Save the rest of the general registers (R15 has been saved already). */\r
337         PUSHM   R1-R14\r
338 \r
339         /* Save the accumulator. */\r
340         MVFACHI R15\r
341         PUSH.L  R15\r
342         MVFACMI R15     ; Middle order word.\r
343         SHLL    #16, R15 ; Shifted left as it is restored to the low order word.\r
344         PUSH.L  R15\r
345 \r
346         /* Save the stack pointer to the TCB. */\r
347         MOV.L   #_pxCurrentTCB, R15\r
348         MOV.L   [ R15 ], R15\r
349         MOV.L   R0, [ R15 ]\r
350 \r
351         /* Ensure the interrupt mask is set to the syscall priority while the kernel\r
352         structures are being accessed. */\r
353         MVTIPL  #configMAX_SYSCALL_INTERRUPT_PRIORITY\r
354 \r
355         /* Select the next task to run. */\r
356         BSR.A   _vTaskSwitchContext\r
357 \r
358         /* Reset the interrupt mask as no more data structure access is required. */\r
359         MVTIPL  #configKERNEL_INTERRUPT_PRIORITY\r
360 \r
361         /* Load the stack pointer of the task that is now selected as the Running\r
362         state task from its TCB. */\r
363         MOV.L   #_pxCurrentTCB,R15\r
364         MOV.L   [ R15 ], R15\r
365         MOV.L   [ R15 ], R0\r
366 \r
367         /* Restore the context of the new task.  The PSW (Program Status Word) and\r
368         PC will be popped by the RTE instruction. */\r
369         POP             R15\r
370         MVTACLO R15\r
371         POP             R15\r
372         MVTACHI R15\r
373         POPM    R1-R15\r
374         RTE\r
375         NOP\r
376         NOP\r
377 }\r
378 /*-----------------------------------------------------------*/\r
379 \r
380 void vPortEndScheduler( void )\r
381 {\r
382         /* Not implemented in ports where there is nothing to return to.\r
383         Artificially force an assert. */\r
384         configASSERT( pxCurrentTCB == NULL );\r
385 \r
386         /* The following line is just to prevent the symbol getting optimised away. */\r
387         ( void ) vTaskSwitchContext();\r
388 }\r
389 /*-----------------------------------------------------------*/\r
390 \r
391 \r
392 \r