]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/Renesas/SH2A_FPU/port.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Source / portable / Renesas / SH2A_FPU / port.c
1 /*\r
2     FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43     \r
44     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 /*-----------------------------------------------------------\r
68  * Implementation of functions defined in portable.h for the SH2A port.\r
69  *----------------------------------------------------------*/\r
70 \r
71 /* Scheduler includes. */\r
72 #include "FreeRTOS.h"\r
73 #include "task.h"\r
74 \r
75 /* Library includes. */\r
76 #include "string.h"\r
77 \r
78 /*-----------------------------------------------------------*/\r
79 \r
80 /* The SR assigned to a newly created task.  The only important thing in this\r
81 value is for all interrupts to be enabled. */\r
82 #define portINITIAL_SR                          ( 0UL )\r
83 \r
84 /* Dimensions the array into which the floating point context is saved.  \r
85 Allocate enough space for FPR0 to FPR15, FPUL and FPSCR, each of which is 4\r
86 bytes big.  If this number is changed then the 72 in portasm.src also needs\r
87 changing. */\r
88 #define portFLOP_REGISTERS_TO_STORE     ( 18 )\r
89 #define portFLOP_STORAGE_SIZE           ( portFLOP_REGISTERS_TO_STORE * 4 )\r
90 \r
91 /*-----------------------------------------------------------*/\r
92 \r
93 /*\r
94  * The TRAPA handler used to force a context switch.\r
95  */\r
96 void vPortYield( void );\r
97 \r
98 /*\r
99  * Function to start the first task executing - defined in portasm.src.\r
100  */\r
101 extern void vPortStartFirstTask( void );\r
102 \r
103 /*\r
104  * Obtains the current GBR value - defined in portasm.src.\r
105  */\r
106 extern unsigned long ulPortGetGBR( void );\r
107 \r
108 /*-----------------------------------------------------------*/\r
109 \r
110 /* \r
111  * See header file for description. \r
112  */\r
113 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
114 {\r
115         /* Mark the end of the stack - used for debugging only and can be removed. */\r
116         *pxTopOfStack = 0x11111111UL;\r
117         pxTopOfStack--;\r
118         *pxTopOfStack = 0x22222222UL;\r
119         pxTopOfStack--;\r
120         *pxTopOfStack = 0x33333333UL;\r
121         pxTopOfStack--;\r
122 \r
123         /* SR. */\r
124         *pxTopOfStack = portINITIAL_SR; \r
125         pxTopOfStack--;\r
126         \r
127         /* PC. */\r
128         *pxTopOfStack = ( unsigned long ) pxCode;\r
129         pxTopOfStack--;\r
130         \r
131         /* PR. */\r
132         *pxTopOfStack = 15;\r
133         pxTopOfStack--;\r
134         \r
135         /* 14. */\r
136         *pxTopOfStack = 14;\r
137         pxTopOfStack--;\r
138 \r
139         /* R13. */\r
140         *pxTopOfStack = 13;\r
141         pxTopOfStack--;\r
142 \r
143         /* R12. */\r
144         *pxTopOfStack = 12;\r
145         pxTopOfStack--;\r
146 \r
147         /* R11. */\r
148         *pxTopOfStack = 11;\r
149         pxTopOfStack--;\r
150 \r
151         /* R10. */\r
152         *pxTopOfStack = 10;\r
153         pxTopOfStack--;\r
154 \r
155         /* R9. */\r
156         *pxTopOfStack = 9;\r
157         pxTopOfStack--;\r
158 \r
159         /* R8. */\r
160         *pxTopOfStack = 8;\r
161         pxTopOfStack--;\r
162 \r
163         /* R7. */\r
164         *pxTopOfStack = 7;\r
165         pxTopOfStack--;\r
166 \r
167         /* R6. */\r
168         *pxTopOfStack = 6;\r
169         pxTopOfStack--;\r
170 \r
171         /* R5. */\r
172         *pxTopOfStack = 5;\r
173         pxTopOfStack--;\r
174 \r
175         /* R4. */\r
176         *pxTopOfStack = ( unsigned long ) pvParameters;\r
177         pxTopOfStack--;\r
178 \r
179         /* R3. */\r
180         *pxTopOfStack = 3;\r
181         pxTopOfStack--;\r
182 \r
183         /* R2. */\r
184         *pxTopOfStack = 2;\r
185         pxTopOfStack--;\r
186 \r
187         /* R1. */\r
188         *pxTopOfStack = 1;\r
189         pxTopOfStack--;\r
190         \r
191         /* R0 */\r
192         *pxTopOfStack = 0;\r
193         pxTopOfStack--;\r
194         \r
195         /* MACL. */\r
196         *pxTopOfStack = 16;\r
197         pxTopOfStack--;\r
198         \r
199         /* MACH. */\r
200         *pxTopOfStack = 17;\r
201         pxTopOfStack--;\r
202         \r
203         /* GBR. */\r
204         *pxTopOfStack = ulPortGetGBR();\r
205         \r
206         /* GBR = global base register.\r
207            VBR = vector base register.\r
208            TBR = jump table base register.\r
209            R15 is the stack pointer. */\r
210 \r
211         return pxTopOfStack;\r
212 }\r
213 /*-----------------------------------------------------------*/\r
214 \r
215 portBASE_TYPE xPortStartScheduler( void )\r
216 {\r
217 extern void vApplicationSetupTimerInterrupt( void );\r
218 \r
219         /* Call an application function to set up the timer that will generate the\r
220         tick interrupt.  This way the application can decide which peripheral to \r
221         use.  A demo application is provided to show a suitable example. */\r
222         vApplicationSetupTimerInterrupt();\r
223 \r
224         /* Start the first task.  This will only restore the standard registers and\r
225         not the flop registers.  This does not really matter though because the only\r
226         flop register that is initialised to a particular value is fpscr, and it is\r
227         only initialised to the current value, which will still be the current value\r
228         when the first task starts executing. */\r
229         trapa( portSTART_SCHEDULER_TRAP_NO );\r
230 \r
231         /* Should not get here. */\r
232         return pdFAIL;\r
233 }\r
234 /*-----------------------------------------------------------*/\r
235 \r
236 void vPortEndScheduler( void )\r
237 {\r
238         /* Not implemented as there is nothing to return to. */\r
239 }\r
240 /*-----------------------------------------------------------*/\r
241 \r
242 void vPortYield( void )\r
243 {\r
244 long lInterruptMask;\r
245 \r
246         /* Ensure the yield trap runs at the same priority as the other interrupts\r
247         that can cause a context switch. */\r
248         lInterruptMask = get_imask();\r
249 \r
250         /* taskYIELD() can only be called from a task, not an interrupt, so the\r
251         current interrupt mask can only be 0 or portKERNEL_INTERRUPT_PRIORITY and\r
252         the mask can be set without risk of accidentally lowering the mask value. */    \r
253         set_imask( portKERNEL_INTERRUPT_PRIORITY );\r
254         \r
255         trapa( portYIELD_TRAP_NO );\r
256         \r
257         /* Restore the interrupt mask to whatever it was previously (when the\r
258         function was entered). */\r
259         set_imask( ( int ) lInterruptMask );\r
260 }\r
261 /*-----------------------------------------------------------*/\r
262 \r
263 portBASE_TYPE xPortUsesFloatingPoint( xTaskHandle xTask )\r
264 {\r
265 unsigned long *pulFlopBuffer;\r
266 portBASE_TYPE xReturn;\r
267 extern void * volatile pxCurrentTCB;\r
268 \r
269         /* This function tells the kernel that the task referenced by xTask is\r
270         going to use the floating point registers and therefore requires the\r
271         floating point registers saved as part of its context. */\r
272 \r
273         /* Passing NULL as xTask is used to indicate that the calling task is the\r
274         subject task - so pxCurrentTCB is the task handle. */\r
275         if( xTask == NULL )\r
276         {\r
277                 xTask = ( xTaskHandle ) pxCurrentTCB;\r
278         }\r
279 \r
280         /* Allocate a buffer large enough to hold all the flop registers. */\r
281         pulFlopBuffer = ( unsigned long * ) pvPortMalloc( portFLOP_STORAGE_SIZE );\r
282         \r
283         if( pulFlopBuffer != NULL )\r
284         {\r
285                 /* Start with the registers in a benign state. */\r
286                 memset( ( void * ) pulFlopBuffer, 0x00, portFLOP_STORAGE_SIZE );\r
287                 \r
288                 /* The first thing to get saved in the buffer is the FPSCR value -\r
289                 initialise this to the current FPSCR value. */\r
290                 *pulFlopBuffer = get_fpscr();\r
291                 \r
292                 /* Use the task tag to point to the flop buffer.  Pass pointer to just \r
293                 above the buffer because the flop save routine uses a pre-decrement. */\r
294                 vTaskSetApplicationTaskTag( xTask, ( void * ) ( pulFlopBuffer + portFLOP_REGISTERS_TO_STORE ) );                \r
295                 xReturn = pdPASS;\r
296         }\r
297         else\r
298         {\r
299                 xReturn = pdFAIL;\r
300         }\r
301         \r
302         return xReturn;\r
303 }\r
304 /*-----------------------------------------------------------*/\r
305 \r
306 \r