]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/MicroBlaze/port.c
Update version number to 9.0.0rc2.
[freertos] / FreeRTOS / Source / portable / GCC / MicroBlaze / port.c
1 /*\r
2     FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /*-----------------------------------------------------------\r
71  * Implementation of functions defined in portable.h for the MicroBlaze port.\r
72  *----------------------------------------------------------*/\r
73 \r
74 \r
75 /* Scheduler includes. */\r
76 #include "FreeRTOS.h"\r
77 #include "task.h"\r
78 \r
79 /* Standard includes. */\r
80 #include <string.h>\r
81 \r
82 /* Hardware includes. */\r
83 #include <xintc.h>\r
84 #include <xintc_i.h>\r
85 #include <xtmrctr.h>\r
86 \r
87 #if( configSUPPORT_DYNAMIC_ALLOCATION == 0 )\r
88         #error configSUPPORT_DYNAMIC_ALLOCATION must be set to 1 to use this port.\r
89 #endif\r
90 \r
91 /* Tasks are started with interrupts enabled. */\r
92 #define portINITIAL_MSR_STATE           ( ( StackType_t ) 0x02 )\r
93 \r
94 /* Tasks are started with a critical section nesting of 0 - however prior\r
95 to the scheduler being commenced we don't want the critical nesting level\r
96 to reach zero, so it is initialised to a high value. */\r
97 #define portINITIAL_NESTING_VALUE       ( 0xff )\r
98 \r
99 /* Our hardware setup only uses one counter. */\r
100 #define portCOUNTER_0                           0\r
101 \r
102 /* The stack used by the ISR is filled with a known value to assist in\r
103 debugging. */\r
104 #define portISR_STACK_FILL_VALUE        0x55555555\r
105 \r
106 /* Counts the nesting depth of calls to portENTER_CRITICAL().  Each task\r
107 maintains it's own count, so this variable is saved as part of the task\r
108 context. */\r
109 volatile UBaseType_t uxCriticalNesting = portINITIAL_NESTING_VALUE;\r
110 \r
111 /* To limit the amount of stack required by each task, this port uses a\r
112 separate stack for interrupts. */\r
113 uint32_t *pulISRStack;\r
114 \r
115 /*-----------------------------------------------------------*/\r
116 \r
117 /*\r
118  * Sets up the periodic ISR used for the RTOS tick.  This uses timer 0, but\r
119  * could have alternatively used the watchdog timer or timer 1.\r
120  */\r
121 static void prvSetupTimerInterrupt( void );\r
122 /*-----------------------------------------------------------*/\r
123 \r
124 /*\r
125  * Initialise the stack of a task to look exactly as if a call to\r
126  * portSAVE_CONTEXT had been made.\r
127  *\r
128  * See the header file portable.h.\r
129  */\r
130 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
131 {\r
132 extern void *_SDA2_BASE_, *_SDA_BASE_;\r
133 const uint32_t ulR2 = ( uint32_t ) &_SDA2_BASE_;\r
134 const uint32_t ulR13 = ( uint32_t ) &_SDA_BASE_;\r
135 \r
136         /* Place a few bytes of known values on the bottom of the stack.\r
137         This is essential for the Microblaze port and these lines must\r
138         not be omitted.  The parameter value will overwrite the\r
139         0x22222222 value during the function prologue. */\r
140         *pxTopOfStack = ( StackType_t ) 0x11111111;\r
141         pxTopOfStack--;\r
142         *pxTopOfStack = ( StackType_t ) 0x22222222;\r
143         pxTopOfStack--;\r
144         *pxTopOfStack = ( StackType_t ) 0x33333333;\r
145         pxTopOfStack--;\r
146 \r
147         /* First stack an initial value for the critical section nesting.  This\r
148         is initialised to zero as tasks are started with interrupts enabled. */\r
149         *pxTopOfStack = ( StackType_t ) 0x00;   /* R0. */\r
150 \r
151         /* Place an initial value for all the general purpose registers. */\r
152         pxTopOfStack--;\r
153         *pxTopOfStack = ( StackType_t ) ulR2;   /* R2 - small data area. */\r
154         pxTopOfStack--;\r
155         *pxTopOfStack = ( StackType_t ) 0x03;   /* R3. */\r
156         pxTopOfStack--;\r
157         *pxTopOfStack = ( StackType_t ) 0x04;   /* R4. */\r
158         pxTopOfStack--;\r
159         *pxTopOfStack = ( StackType_t ) pvParameters;/* R5 contains the function call parameters. */\r
160         pxTopOfStack--;\r
161         *pxTopOfStack = ( StackType_t ) 0x06;   /* R6. */\r
162         pxTopOfStack--;\r
163         *pxTopOfStack = ( StackType_t ) 0x07;   /* R7. */\r
164         pxTopOfStack--;\r
165         *pxTopOfStack = ( StackType_t ) 0x08;   /* R8. */\r
166         pxTopOfStack--;\r
167         *pxTopOfStack = ( StackType_t ) 0x09;   /* R9. */\r
168         pxTopOfStack--;\r
169         *pxTopOfStack = ( StackType_t ) 0x0a;   /* R10. */\r
170         pxTopOfStack--;\r
171         *pxTopOfStack = ( StackType_t ) 0x0b;   /* R11. */\r
172         pxTopOfStack--;\r
173         *pxTopOfStack = ( StackType_t ) 0x0c;   /* R12. */\r
174         pxTopOfStack--;\r
175         *pxTopOfStack = ( StackType_t ) ulR13;  /* R13 - small data read write area. */\r
176         pxTopOfStack--;\r
177         *pxTopOfStack = ( StackType_t ) pxCode; /* R14. */\r
178         pxTopOfStack--;\r
179         *pxTopOfStack = ( StackType_t ) 0x0f;   /* R15. */\r
180         pxTopOfStack--;\r
181         *pxTopOfStack = ( StackType_t ) 0x10;   /* R16. */\r
182         pxTopOfStack--;\r
183         *pxTopOfStack = ( StackType_t ) 0x11;   /* R17. */\r
184         pxTopOfStack--;\r
185         *pxTopOfStack = ( StackType_t ) 0x12;   /* R18. */\r
186         pxTopOfStack--;\r
187         *pxTopOfStack = ( StackType_t ) 0x13;   /* R19. */\r
188         pxTopOfStack--;\r
189         *pxTopOfStack = ( StackType_t ) 0x14;   /* R20. */\r
190         pxTopOfStack--;\r
191         *pxTopOfStack = ( StackType_t ) 0x15;   /* R21. */\r
192         pxTopOfStack--;\r
193         *pxTopOfStack = ( StackType_t ) 0x16;   /* R22. */\r
194         pxTopOfStack--;\r
195         *pxTopOfStack = ( StackType_t ) 0x17;   /* R23. */\r
196         pxTopOfStack--;\r
197         *pxTopOfStack = ( StackType_t ) 0x18;   /* R24. */\r
198         pxTopOfStack--;\r
199         *pxTopOfStack = ( StackType_t ) 0x19;   /* R25. */\r
200         pxTopOfStack--;\r
201         *pxTopOfStack = ( StackType_t ) 0x1a;   /* R26. */\r
202         pxTopOfStack--;\r
203         *pxTopOfStack = ( StackType_t ) 0x1b;   /* R27. */\r
204         pxTopOfStack--;\r
205         *pxTopOfStack = ( StackType_t ) 0x1c;   /* R28. */\r
206         pxTopOfStack--;\r
207         *pxTopOfStack = ( StackType_t ) 0x1d;   /* R29. */\r
208         pxTopOfStack--;\r
209         *pxTopOfStack = ( StackType_t ) 0x1e;   /* R30. */\r
210         pxTopOfStack--;\r
211 \r
212         /* The MSR is stacked between R30 and R31. */\r
213         *pxTopOfStack = portINITIAL_MSR_STATE;\r
214         pxTopOfStack--;\r
215 \r
216         *pxTopOfStack = ( StackType_t ) 0x1f;   /* R31. */\r
217         pxTopOfStack--;\r
218 \r
219         /* Return a pointer to the top of the stack we have generated so this can\r
220         be stored in the task control block for the task. */\r
221         return pxTopOfStack;\r
222 }\r
223 /*-----------------------------------------------------------*/\r
224 \r
225 BaseType_t xPortStartScheduler( void )\r
226 {\r
227 extern void ( __FreeRTOS_interrupt_Handler )( void );\r
228 extern void ( vStartFirstTask )( void );\r
229 \r
230 \r
231         /* Setup the FreeRTOS interrupt handler.  Code copied from crt0.s. */\r
232         asm volatile (  "la     r6, r0, __FreeRTOS_interrupt_handler            \n\t" \\r
233                                         "sw     r6, r1, r0                                                                      \n\t" \\r
234                                         "lhu r7, r1, r0                                                                 \n\t" \\r
235                                         "shi r7, r0, 0x12                                                               \n\t" \\r
236                                         "shi r6, r0, 0x16 " );\r
237 \r
238         /* Setup the hardware to generate the tick.  Interrupts are disabled when\r
239         this function is called. */\r
240         prvSetupTimerInterrupt();\r
241 \r
242         /* Allocate the stack to be used by the interrupt handler. */\r
243         pulISRStack = ( uint32_t * ) pvPortMalloc( configMINIMAL_STACK_SIZE * sizeof( StackType_t ) );\r
244 \r
245         /* Restore the context of the first task that is going to run. */\r
246         if( pulISRStack != NULL )\r
247         {\r
248                 /* Fill the ISR stack with a known value to facilitate debugging. */\r
249                 memset( pulISRStack, portISR_STACK_FILL_VALUE, configMINIMAL_STACK_SIZE * sizeof( StackType_t ) );\r
250                 pulISRStack += ( configMINIMAL_STACK_SIZE - 1 );\r
251 \r
252                 /* Kick off the first task. */\r
253                 vStartFirstTask();\r
254         }\r
255 \r
256         /* Should not get here as the tasks are now running! */\r
257         return pdFALSE;\r
258 }\r
259 /*-----------------------------------------------------------*/\r
260 \r
261 void vPortEndScheduler( void )\r
262 {\r
263         /* Not implemented. */\r
264 }\r
265 /*-----------------------------------------------------------*/\r
266 \r
267 /*\r
268  * Manual context switch called by portYIELD or taskYIELD.\r
269  */\r
270 void vPortYield( void )\r
271 {\r
272 extern void VPortYieldASM( void );\r
273 \r
274         /* Perform the context switch in a critical section to assure it is\r
275         not interrupted by the tick ISR.  It is not a problem to do this as\r
276         each task maintains it's own interrupt status. */\r
277         portENTER_CRITICAL();\r
278                 /* Jump directly to the yield function to ensure there is no\r
279                 compiler generated prologue code. */\r
280                 asm volatile (  "bralid r14, VPortYieldASM              \n\t" \\r
281                                                 "or r0, r0, r0                                  \n\t" );\r
282         portEXIT_CRITICAL();\r
283 }\r
284 /*-----------------------------------------------------------*/\r
285 \r
286 /*\r
287  * Hardware initialisation to generate the RTOS tick.\r
288  */\r
289 static void prvSetupTimerInterrupt( void )\r
290 {\r
291 XTmrCtr xTimer;\r
292 const uint32_t ulCounterValue = configCPU_CLOCK_HZ / configTICK_RATE_HZ;\r
293 UBaseType_t uxMask;\r
294 \r
295         /* The OPB timer1 is used to generate the tick.  Use the provided library\r
296         functions to enable the timer and set the tick frequency. */\r
297         XTmrCtr_mDisable( XPAR_OPB_TIMER_1_BASEADDR, XPAR_OPB_TIMER_1_DEVICE_ID );\r
298         XTmrCtr_Initialize( &xTimer, XPAR_OPB_TIMER_1_DEVICE_ID );\r
299         XTmrCtr_mSetLoadReg( XPAR_OPB_TIMER_1_BASEADDR, portCOUNTER_0, ulCounterValue );\r
300         XTmrCtr_mSetControlStatusReg( XPAR_OPB_TIMER_1_BASEADDR, portCOUNTER_0, XTC_CSR_LOAD_MASK | XTC_CSR_INT_OCCURED_MASK );\r
301 \r
302         /* Set the timer interrupt enable bit while maintaining the other bit\r
303         states. */\r
304         uxMask = XIntc_In32( ( XPAR_OPB_INTC_0_BASEADDR + XIN_IER_OFFSET ) );\r
305         uxMask |= XPAR_OPB_TIMER_1_INTERRUPT_MASK;\r
306         XIntc_Out32( ( XPAR_OPB_INTC_0_BASEADDR + XIN_IER_OFFSET ), ( uxMask ) );\r
307 \r
308         XTmrCtr_Start( &xTimer, XPAR_OPB_TIMER_1_DEVICE_ID );\r
309         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
310         XIntc_mAckIntr( XPAR_INTC_SINGLE_BASEADDR, 1 );\r
311 }\r
312 /*-----------------------------------------------------------*/\r
313 \r
314 /*\r
315  * The interrupt handler placed in the interrupt vector when the scheduler is\r
316  * started.  The task context has already been saved when this is called.\r
317  * This handler determines the interrupt source and calls the relevant\r
318  * peripheral handler.\r
319  */\r
320 void vTaskISRHandler( void )\r
321 {\r
322 static uint32_t ulPending;\r
323 \r
324         /* Which interrupts are pending? */\r
325         ulPending = XIntc_In32( ( XPAR_INTC_SINGLE_BASEADDR + XIN_IVR_OFFSET ) );\r
326 \r
327         if( ulPending < XPAR_INTC_MAX_NUM_INTR_INPUTS )\r
328         {\r
329                 static XIntc_VectorTableEntry *pxTablePtr;\r
330                 static XIntc_Config *pxConfig;\r
331                 static uint32_t ulInterruptMask;\r
332 \r
333                 ulInterruptMask = ( uint32_t ) 1 << ulPending;\r
334 \r
335                 /* Get the configuration data using the device ID */\r
336                 pxConfig = &XIntc_ConfigTable[ ( uint32_t ) XPAR_INTC_SINGLE_DEVICE_ID ];\r
337 \r
338                 pxTablePtr = &( pxConfig->HandlerTable[ ulPending ] );\r
339                 if( pxConfig->AckBeforeService & ( ulInterruptMask  ) )\r
340                 {\r
341                         XIntc_mAckIntr( pxConfig->BaseAddress, ulInterruptMask );\r
342                         pxTablePtr->Handler( pxTablePtr->CallBackRef );\r
343                 }\r
344                 else\r
345                 {\r
346                         pxTablePtr->Handler( pxTablePtr->CallBackRef );\r
347                         XIntc_mAckIntr( pxConfig->BaseAddress, ulInterruptMask );\r
348                 }\r
349         }\r
350 }\r
351 /*-----------------------------------------------------------*/\r
352 \r
353 /*\r
354  * Handler for the timer interrupt.\r
355  */\r
356 void vTickISR( void *pvBaseAddress )\r
357 {\r
358 uint32_t ulCSR;\r
359 \r
360         /* Increment the RTOS tick - this might cause a task to unblock. */\r
361         if( xTaskIncrementTick() != pdFALSE )\r
362         {\r
363                 vTaskSwitchContext();\r
364         }\r
365 \r
366         /* Clear the timer interrupt */\r
367         ulCSR = XTmrCtr_mGetControlStatusReg(XPAR_OPB_TIMER_1_BASEADDR, 0);\r
368         XTmrCtr_mSetControlStatusReg( XPAR_OPB_TIMER_1_BASEADDR, portCOUNTER_0, ulCSR );\r
369 }\r
370 /*-----------------------------------------------------------*/\r
371 \r
372 \r
373 \r
374 \r
375 \r