]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/MicroBlaze/port.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Source / portable / GCC / MicroBlaze / port.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 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.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*-----------------------------------------------------------\r
29  * Implementation of functions defined in portable.h for the MicroBlaze port.\r
30  *----------------------------------------------------------*/\r
31 \r
32 \r
33 /* Scheduler includes. */\r
34 #include "FreeRTOS.h"\r
35 #include "task.h"\r
36 \r
37 /* Standard includes. */\r
38 #include <string.h>\r
39 \r
40 /* Hardware includes. */\r
41 #include <xintc.h>\r
42 #include <xintc_i.h>\r
43 #include <xtmrctr.h>\r
44 \r
45 #if( configSUPPORT_DYNAMIC_ALLOCATION == 0 )\r
46         #error configSUPPORT_DYNAMIC_ALLOCATION must be set to 1 to use this port.\r
47 #endif\r
48 \r
49 /* Tasks are started with interrupts enabled. */\r
50 #define portINITIAL_MSR_STATE           ( ( StackType_t ) 0x02 )\r
51 \r
52 /* Tasks are started with a critical section nesting of 0 - however prior\r
53 to the scheduler being commenced we don't want the critical nesting level\r
54 to reach zero, so it is initialised to a high value. */\r
55 #define portINITIAL_NESTING_VALUE       ( 0xff )\r
56 \r
57 /* Our hardware setup only uses one counter. */\r
58 #define portCOUNTER_0                           0\r
59 \r
60 /* The stack used by the ISR is filled with a known value to assist in\r
61 debugging. */\r
62 #define portISR_STACK_FILL_VALUE        0x55555555\r
63 \r
64 /* Counts the nesting depth of calls to portENTER_CRITICAL().  Each task\r
65 maintains it's own count, so this variable is saved as part of the task\r
66 context. */\r
67 volatile UBaseType_t uxCriticalNesting = portINITIAL_NESTING_VALUE;\r
68 \r
69 /* To limit the amount of stack required by each task, this port uses a\r
70 separate stack for interrupts. */\r
71 uint32_t *pulISRStack;\r
72 \r
73 /*-----------------------------------------------------------*/\r
74 \r
75 /*\r
76  * Sets up the periodic ISR used for the RTOS tick.  This uses timer 0, but\r
77  * could have alternatively used the watchdog timer or timer 1.\r
78  */\r
79 static void prvSetupTimerInterrupt( void );\r
80 /*-----------------------------------------------------------*/\r
81 \r
82 /*\r
83  * Initialise the stack of a task to look exactly as if a call to\r
84  * portSAVE_CONTEXT had been made.\r
85  *\r
86  * See the header file portable.h.\r
87  */\r
88 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
89 {\r
90 extern void *_SDA2_BASE_, *_SDA_BASE_;\r
91 const uint32_t ulR2 = ( uint32_t ) &_SDA2_BASE_;\r
92 const uint32_t ulR13 = ( uint32_t ) &_SDA_BASE_;\r
93 \r
94         /* Place a few bytes of known values on the bottom of the stack.\r
95         This is essential for the Microblaze port and these lines must\r
96         not be omitted.  The parameter value will overwrite the\r
97         0x22222222 value during the function prologue. */\r
98         *pxTopOfStack = ( StackType_t ) 0x11111111;\r
99         pxTopOfStack--;\r
100         *pxTopOfStack = ( StackType_t ) 0x22222222;\r
101         pxTopOfStack--;\r
102         *pxTopOfStack = ( StackType_t ) 0x33333333;\r
103         pxTopOfStack--;\r
104 \r
105         /* First stack an initial value for the critical section nesting.  This\r
106         is initialised to zero as tasks are started with interrupts enabled. */\r
107         *pxTopOfStack = ( StackType_t ) 0x00;   /* R0. */\r
108 \r
109         /* Place an initial value for all the general purpose registers. */\r
110         pxTopOfStack--;\r
111         *pxTopOfStack = ( StackType_t ) ulR2;   /* R2 - small data area. */\r
112         pxTopOfStack--;\r
113         *pxTopOfStack = ( StackType_t ) 0x03;   /* R3. */\r
114         pxTopOfStack--;\r
115         *pxTopOfStack = ( StackType_t ) 0x04;   /* R4. */\r
116         pxTopOfStack--;\r
117         *pxTopOfStack = ( StackType_t ) pvParameters;/* R5 contains the function call parameters. */\r
118         pxTopOfStack--;\r
119         *pxTopOfStack = ( StackType_t ) 0x06;   /* R6. */\r
120         pxTopOfStack--;\r
121         *pxTopOfStack = ( StackType_t ) 0x07;   /* R7. */\r
122         pxTopOfStack--;\r
123         *pxTopOfStack = ( StackType_t ) 0x08;   /* R8. */\r
124         pxTopOfStack--;\r
125         *pxTopOfStack = ( StackType_t ) 0x09;   /* R9. */\r
126         pxTopOfStack--;\r
127         *pxTopOfStack = ( StackType_t ) 0x0a;   /* R10. */\r
128         pxTopOfStack--;\r
129         *pxTopOfStack = ( StackType_t ) 0x0b;   /* R11. */\r
130         pxTopOfStack--;\r
131         *pxTopOfStack = ( StackType_t ) 0x0c;   /* R12. */\r
132         pxTopOfStack--;\r
133         *pxTopOfStack = ( StackType_t ) ulR13;  /* R13 - small data read write area. */\r
134         pxTopOfStack--;\r
135         *pxTopOfStack = ( StackType_t ) pxCode; /* R14. */\r
136         pxTopOfStack--;\r
137         *pxTopOfStack = ( StackType_t ) 0x0f;   /* R15. */\r
138         pxTopOfStack--;\r
139         *pxTopOfStack = ( StackType_t ) 0x10;   /* R16. */\r
140         pxTopOfStack--;\r
141         *pxTopOfStack = ( StackType_t ) 0x11;   /* R17. */\r
142         pxTopOfStack--;\r
143         *pxTopOfStack = ( StackType_t ) 0x12;   /* R18. */\r
144         pxTopOfStack--;\r
145         *pxTopOfStack = ( StackType_t ) 0x13;   /* R19. */\r
146         pxTopOfStack--;\r
147         *pxTopOfStack = ( StackType_t ) 0x14;   /* R20. */\r
148         pxTopOfStack--;\r
149         *pxTopOfStack = ( StackType_t ) 0x15;   /* R21. */\r
150         pxTopOfStack--;\r
151         *pxTopOfStack = ( StackType_t ) 0x16;   /* R22. */\r
152         pxTopOfStack--;\r
153         *pxTopOfStack = ( StackType_t ) 0x17;   /* R23. */\r
154         pxTopOfStack--;\r
155         *pxTopOfStack = ( StackType_t ) 0x18;   /* R24. */\r
156         pxTopOfStack--;\r
157         *pxTopOfStack = ( StackType_t ) 0x19;   /* R25. */\r
158         pxTopOfStack--;\r
159         *pxTopOfStack = ( StackType_t ) 0x1a;   /* R26. */\r
160         pxTopOfStack--;\r
161         *pxTopOfStack = ( StackType_t ) 0x1b;   /* R27. */\r
162         pxTopOfStack--;\r
163         *pxTopOfStack = ( StackType_t ) 0x1c;   /* R28. */\r
164         pxTopOfStack--;\r
165         *pxTopOfStack = ( StackType_t ) 0x1d;   /* R29. */\r
166         pxTopOfStack--;\r
167         *pxTopOfStack = ( StackType_t ) 0x1e;   /* R30. */\r
168         pxTopOfStack--;\r
169 \r
170         /* The MSR is stacked between R30 and R31. */\r
171         *pxTopOfStack = portINITIAL_MSR_STATE;\r
172         pxTopOfStack--;\r
173 \r
174         *pxTopOfStack = ( StackType_t ) 0x1f;   /* R31. */\r
175         pxTopOfStack--;\r
176 \r
177         /* Return a pointer to the top of the stack we have generated so this can\r
178         be stored in the task control block for the task. */\r
179         return pxTopOfStack;\r
180 }\r
181 /*-----------------------------------------------------------*/\r
182 \r
183 BaseType_t xPortStartScheduler( void )\r
184 {\r
185 extern void ( __FreeRTOS_interrupt_Handler )( void );\r
186 extern void ( vStartFirstTask )( void );\r
187 \r
188 \r
189         /* Setup the FreeRTOS interrupt handler.  Code copied from crt0.s. */\r
190         asm volatile (  "la     r6, r0, __FreeRTOS_interrupt_handler            \n\t" \\r
191                                         "sw     r6, r1, r0                                                                      \n\t" \\r
192                                         "lhu r7, r1, r0                                                                 \n\t" \\r
193                                         "shi r7, r0, 0x12                                                               \n\t" \\r
194                                         "shi r6, r0, 0x16 " );\r
195 \r
196         /* Setup the hardware to generate the tick.  Interrupts are disabled when\r
197         this function is called. */\r
198         prvSetupTimerInterrupt();\r
199 \r
200         /* Allocate the stack to be used by the interrupt handler. */\r
201         pulISRStack = ( uint32_t * ) pvPortMalloc( configMINIMAL_STACK_SIZE * sizeof( StackType_t ) );\r
202 \r
203         /* Restore the context of the first task that is going to run. */\r
204         if( pulISRStack != NULL )\r
205         {\r
206                 /* Fill the ISR stack with a known value to facilitate debugging. */\r
207                 memset( pulISRStack, portISR_STACK_FILL_VALUE, configMINIMAL_STACK_SIZE * sizeof( StackType_t ) );\r
208                 pulISRStack += ( configMINIMAL_STACK_SIZE - 1 );\r
209 \r
210                 /* Kick off the first task. */\r
211                 vStartFirstTask();\r
212         }\r
213 \r
214         /* Should not get here as the tasks are now running! */\r
215         return pdFALSE;\r
216 }\r
217 /*-----------------------------------------------------------*/\r
218 \r
219 void vPortEndScheduler( void )\r
220 {\r
221         /* Not implemented. */\r
222 }\r
223 /*-----------------------------------------------------------*/\r
224 \r
225 /*\r
226  * Manual context switch called by portYIELD or taskYIELD.\r
227  */\r
228 void vPortYield( void )\r
229 {\r
230 extern void VPortYieldASM( void );\r
231 \r
232         /* Perform the context switch in a critical section to assure it is\r
233         not interrupted by the tick ISR.  It is not a problem to do this as\r
234         each task maintains it's own interrupt status. */\r
235         portENTER_CRITICAL();\r
236                 /* Jump directly to the yield function to ensure there is no\r
237                 compiler generated prologue code. */\r
238                 asm volatile (  "bralid r14, VPortYieldASM              \n\t" \\r
239                                                 "or r0, r0, r0                                  \n\t" );\r
240         portEXIT_CRITICAL();\r
241 }\r
242 /*-----------------------------------------------------------*/\r
243 \r
244 /*\r
245  * Hardware initialisation to generate the RTOS tick.\r
246  */\r
247 static void prvSetupTimerInterrupt( void )\r
248 {\r
249 XTmrCtr xTimer;\r
250 const uint32_t ulCounterValue = configCPU_CLOCK_HZ / configTICK_RATE_HZ;\r
251 UBaseType_t uxMask;\r
252 \r
253         /* The OPB timer1 is used to generate the tick.  Use the provided library\r
254         functions to enable the timer and set the tick frequency. */\r
255         XTmrCtr_mDisable( XPAR_OPB_TIMER_1_BASEADDR, XPAR_OPB_TIMER_1_DEVICE_ID );\r
256         XTmrCtr_Initialize( &xTimer, XPAR_OPB_TIMER_1_DEVICE_ID );\r
257         XTmrCtr_mSetLoadReg( XPAR_OPB_TIMER_1_BASEADDR, portCOUNTER_0, ulCounterValue );\r
258         XTmrCtr_mSetControlStatusReg( XPAR_OPB_TIMER_1_BASEADDR, portCOUNTER_0, XTC_CSR_LOAD_MASK | XTC_CSR_INT_OCCURED_MASK );\r
259 \r
260         /* Set the timer interrupt enable bit while maintaining the other bit\r
261         states. */\r
262         uxMask = XIntc_In32( ( XPAR_OPB_INTC_0_BASEADDR + XIN_IER_OFFSET ) );\r
263         uxMask |= XPAR_OPB_TIMER_1_INTERRUPT_MASK;\r
264         XIntc_Out32( ( XPAR_OPB_INTC_0_BASEADDR + XIN_IER_OFFSET ), ( uxMask ) );\r
265 \r
266         XTmrCtr_Start( &xTimer, XPAR_OPB_TIMER_1_DEVICE_ID );\r
267         XTmrCtr_mSetControlStatusReg(XPAR_OPB_TIMER_1_BASEADDR, portCOUNTER_0, XTC_CSR_ENABLE_TMR_MASK | XTC_CSR_ENABLE_INT_MASK | XTC_CSR_AUTO_RELOAD_MASK | XTC_CSR_DOWN_COUNT_MASK | XTC_CSR_INT_OCCURED_MASK );\r
268         XIntc_mAckIntr( XPAR_INTC_SINGLE_BASEADDR, 1 );\r
269 }\r
270 /*-----------------------------------------------------------*/\r
271 \r
272 /*\r
273  * The interrupt handler placed in the interrupt vector when the scheduler is\r
274  * started.  The task context has already been saved when this is called.\r
275  * This handler determines the interrupt source and calls the relevant\r
276  * peripheral handler.\r
277  */\r
278 void vTaskISRHandler( void )\r
279 {\r
280 static uint32_t ulPending;\r
281 \r
282         /* Which interrupts are pending? */\r
283         ulPending = XIntc_In32( ( XPAR_INTC_SINGLE_BASEADDR + XIN_IVR_OFFSET ) );\r
284 \r
285         if( ulPending < XPAR_INTC_MAX_NUM_INTR_INPUTS )\r
286         {\r
287                 static XIntc_VectorTableEntry *pxTablePtr;\r
288                 static XIntc_Config *pxConfig;\r
289                 static uint32_t ulInterruptMask;\r
290 \r
291                 ulInterruptMask = ( uint32_t ) 1 << ulPending;\r
292 \r
293                 /* Get the configuration data using the device ID */\r
294                 pxConfig = &XIntc_ConfigTable[ ( uint32_t ) XPAR_INTC_SINGLE_DEVICE_ID ];\r
295 \r
296                 pxTablePtr = &( pxConfig->HandlerTable[ ulPending ] );\r
297                 if( pxConfig->AckBeforeService & ( ulInterruptMask  ) )\r
298                 {\r
299                         XIntc_mAckIntr( pxConfig->BaseAddress, ulInterruptMask );\r
300                         pxTablePtr->Handler( pxTablePtr->CallBackRef );\r
301                 }\r
302                 else\r
303                 {\r
304                         pxTablePtr->Handler( pxTablePtr->CallBackRef );\r
305                         XIntc_mAckIntr( pxConfig->BaseAddress, ulInterruptMask );\r
306                 }\r
307         }\r
308 }\r
309 /*-----------------------------------------------------------*/\r
310 \r
311 /*\r
312  * Handler for the timer interrupt.\r
313  */\r
314 void vTickISR( void *pvBaseAddress )\r
315 {\r
316 uint32_t ulCSR;\r
317 \r
318         /* Increment the RTOS tick - this might cause a task to unblock. */\r
319         if( xTaskIncrementTick() != pdFALSE )\r
320         {\r
321                 vTaskSwitchContext();\r
322         }\r
323 \r
324         /* Clear the timer interrupt */\r
325         ulCSR = XTmrCtr_mGetControlStatusReg(XPAR_OPB_TIMER_1_BASEADDR, 0);\r
326         XTmrCtr_mSetControlStatusReg( XPAR_OPB_TIMER_1_BASEADDR, portCOUNTER_0, ulCSR );\r
327 }\r
328 /*-----------------------------------------------------------*/\r
329 \r
330 \r
331 \r
332 \r
333 \r