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