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