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