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