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