]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/Renesas/SH2A_FPU/port.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Source / portable / Renesas / SH2A_FPU / port.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\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. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /*-----------------------------------------------------------\r
30  * Implementation of functions defined in portable.h for the SH2A port.\r
31  *----------------------------------------------------------*/\r
32 \r
33 /* Scheduler includes. */\r
34 #include "FreeRTOS.h"\r
35 #include "task.h"\r
36 \r
37 /* Library includes. */\r
38 #include "string.h"\r
39 \r
40 /*-----------------------------------------------------------*/\r
41 \r
42 /* The SR assigned to a newly created task.  The only important thing in this\r
43 value is for all interrupts to be enabled. */\r
44 #define portINITIAL_SR                          ( 0UL )\r
45 \r
46 /* Dimensions the array into which the floating point context is saved.\r
47 Allocate enough space for FPR0 to FPR15, FPUL and FPSCR, each of which is 4\r
48 bytes big.  If this number is changed then the 72 in portasm.src also needs\r
49 changing. */\r
50 #define portFLOP_REGISTERS_TO_STORE     ( 18 )\r
51 #define portFLOP_STORAGE_SIZE           ( portFLOP_REGISTERS_TO_STORE * 4 )\r
52 \r
53 #if( configSUPPORT_DYNAMIC_ALLOCATION == 0 )\r
54         #error configSUPPORT_DYNAMIC_ALLOCATION must be 1 to use this port.\r
55 #endif\r
56 \r
57 /*-----------------------------------------------------------*/\r
58 \r
59 /*\r
60  * The TRAPA handler used to force a context switch.\r
61  */\r
62 void vPortYield( void );\r
63 \r
64 /*\r
65  * Function to start the first task executing - defined in portasm.src.\r
66  */\r
67 extern void vPortStartFirstTask( void );\r
68 \r
69 /*\r
70  * Obtains the current GBR value - defined in portasm.src.\r
71  */\r
72 extern uint32_t ulPortGetGBR( void );\r
73 \r
74 /*-----------------------------------------------------------*/\r
75 \r
76 /*\r
77  * See header file for description.\r
78  */\r
79 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
80 {\r
81         /* Mark the end of the stack - used for debugging only and can be removed. */\r
82         *pxTopOfStack = 0x11111111UL;\r
83         pxTopOfStack--;\r
84         *pxTopOfStack = 0x22222222UL;\r
85         pxTopOfStack--;\r
86         *pxTopOfStack = 0x33333333UL;\r
87         pxTopOfStack--;\r
88 \r
89         /* SR. */\r
90         *pxTopOfStack = portINITIAL_SR;\r
91         pxTopOfStack--;\r
92 \r
93         /* PC. */\r
94         *pxTopOfStack = ( uint32_t ) pxCode;\r
95         pxTopOfStack--;\r
96 \r
97         /* PR. */\r
98         *pxTopOfStack = 15;\r
99         pxTopOfStack--;\r
100 \r
101         /* 14. */\r
102         *pxTopOfStack = 14;\r
103         pxTopOfStack--;\r
104 \r
105         /* R13. */\r
106         *pxTopOfStack = 13;\r
107         pxTopOfStack--;\r
108 \r
109         /* R12. */\r
110         *pxTopOfStack = 12;\r
111         pxTopOfStack--;\r
112 \r
113         /* R11. */\r
114         *pxTopOfStack = 11;\r
115         pxTopOfStack--;\r
116 \r
117         /* R10. */\r
118         *pxTopOfStack = 10;\r
119         pxTopOfStack--;\r
120 \r
121         /* R9. */\r
122         *pxTopOfStack = 9;\r
123         pxTopOfStack--;\r
124 \r
125         /* R8. */\r
126         *pxTopOfStack = 8;\r
127         pxTopOfStack--;\r
128 \r
129         /* R7. */\r
130         *pxTopOfStack = 7;\r
131         pxTopOfStack--;\r
132 \r
133         /* R6. */\r
134         *pxTopOfStack = 6;\r
135         pxTopOfStack--;\r
136 \r
137         /* R5. */\r
138         *pxTopOfStack = 5;\r
139         pxTopOfStack--;\r
140 \r
141         /* R4. */\r
142         *pxTopOfStack = ( uint32_t ) pvParameters;\r
143         pxTopOfStack--;\r
144 \r
145         /* R3. */\r
146         *pxTopOfStack = 3;\r
147         pxTopOfStack--;\r
148 \r
149         /* R2. */\r
150         *pxTopOfStack = 2;\r
151         pxTopOfStack--;\r
152 \r
153         /* R1. */\r
154         *pxTopOfStack = 1;\r
155         pxTopOfStack--;\r
156 \r
157         /* R0 */\r
158         *pxTopOfStack = 0;\r
159         pxTopOfStack--;\r
160 \r
161         /* MACL. */\r
162         *pxTopOfStack = 16;\r
163         pxTopOfStack--;\r
164 \r
165         /* MACH. */\r
166         *pxTopOfStack = 17;\r
167         pxTopOfStack--;\r
168 \r
169         /* GBR. */\r
170         *pxTopOfStack = ulPortGetGBR();\r
171 \r
172         /* GBR = global base register.\r
173            VBR = vector base register.\r
174            TBR = jump table base register.\r
175            R15 is the stack pointer. */\r
176 \r
177         return pxTopOfStack;\r
178 }\r
179 /*-----------------------------------------------------------*/\r
180 \r
181 BaseType_t xPortStartScheduler( void )\r
182 {\r
183 extern void vApplicationSetupTimerInterrupt( void );\r
184 \r
185         /* Call an application function to set up the timer that will generate the\r
186         tick interrupt.  This way the application can decide which peripheral to\r
187         use.  A demo application is provided to show a suitable example. */\r
188         vApplicationSetupTimerInterrupt();\r
189 \r
190         /* Start the first task.  This will only restore the standard registers and\r
191         not the flop registers.  This does not really matter though because the only\r
192         flop register that is initialised to a particular value is fpscr, and it is\r
193         only initialised to the current value, which will still be the current value\r
194         when the first task starts executing. */\r
195         trapa( portSTART_SCHEDULER_TRAP_NO );\r
196 \r
197         /* Should not get here. */\r
198         return pdFAIL;\r
199 }\r
200 /*-----------------------------------------------------------*/\r
201 \r
202 void vPortEndScheduler( void )\r
203 {\r
204         /* Not implemented as there is nothing to return to. */\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 void vPortYield( void )\r
209 {\r
210 int32_t lInterruptMask;\r
211 \r
212         /* Ensure the yield trap runs at the same priority as the other interrupts\r
213         that can cause a context switch. */\r
214         lInterruptMask = get_imask();\r
215 \r
216         /* taskYIELD() can only be called from a task, not an interrupt, so the\r
217         current interrupt mask can only be 0 or portKERNEL_INTERRUPT_PRIORITY and\r
218         the mask can be set without risk of accidentally lowering the mask value. */\r
219         set_imask( portKERNEL_INTERRUPT_PRIORITY );\r
220 \r
221         trapa( portYIELD_TRAP_NO );\r
222 \r
223         /* Restore the interrupt mask to whatever it was previously (when the\r
224         function was entered). */\r
225         set_imask( ( int ) lInterruptMask );\r
226 }\r
227 /*-----------------------------------------------------------*/\r
228 \r
229 BaseType_t xPortUsesFloatingPoint( TaskHandle_t xTask )\r
230 {\r
231 uint32_t *pulFlopBuffer;\r
232 BaseType_t xReturn;\r
233 extern void * volatile pxCurrentTCB;\r
234 \r
235         /* This function tells the kernel that the task referenced by xTask is\r
236         going to use the floating point registers and therefore requires the\r
237         floating point registers saved as part of its context. */\r
238 \r
239         /* Passing NULL as xTask is used to indicate that the calling task is the\r
240         subject task - so pxCurrentTCB is the task handle. */\r
241         if( xTask == NULL )\r
242         {\r
243                 xTask = ( TaskHandle_t ) pxCurrentTCB;\r
244         }\r
245 \r
246         /* Allocate a buffer large enough to hold all the flop registers. */\r
247         pulFlopBuffer = ( uint32_t * ) pvPortMalloc( portFLOP_STORAGE_SIZE );\r
248 \r
249         if( pulFlopBuffer != NULL )\r
250         {\r
251                 /* Start with the registers in a benign state. */\r
252                 memset( ( void * ) pulFlopBuffer, 0x00, portFLOP_STORAGE_SIZE );\r
253 \r
254                 /* The first thing to get saved in the buffer is the FPSCR value -\r
255                 initialise this to the current FPSCR value. */\r
256                 *pulFlopBuffer = get_fpscr();\r
257 \r
258                 /* Use the task tag to point to the flop buffer.  Pass pointer to just\r
259                 above the buffer because the flop save routine uses a pre-decrement. */\r
260                 vTaskSetApplicationTaskTag( xTask, ( void * ) ( pulFlopBuffer + portFLOP_REGISTERS_TO_STORE ) );\r
261                 xReturn = pdPASS;\r
262         }\r
263         else\r
264         {\r
265                 xReturn = pdFAIL;\r
266         }\r
267 \r
268         return xReturn;\r
269 }\r
270 /*-----------------------------------------------------------*/\r
271 \r
272 \r