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