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