]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/MicroBlazeV8/port.c
Update version numbers to V7.4.1.
[freertos] / FreeRTOS / Source / portable / GCC / MicroBlazeV8 / port.c
1 /*\r
2     FreeRTOS V7.4.1 - Copyright (C) 2013 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 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not it can be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High \r
67     Integrity Systems, who sell the code with commercial support, \r
68     indemnification and middleware, under the OpenRTOS brand.\r
69     \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety \r
71     engineered and independently SIL3 certified version for use in safety and \r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /*-----------------------------------------------------------\r
76  * Implementation of functions defined in portable.h for the MicroBlaze port.\r
77  *----------------------------------------------------------*/\r
78 \r
79 \r
80 /* Scheduler includes. */\r
81 #include "FreeRTOS.h"\r
82 #include "task.h"\r
83 \r
84 /* Standard includes. */\r
85 #include <string.h>\r
86 \r
87 /* Hardware includes. */\r
88 #include <xintc_i.h>\r
89 #include <xil_exception.h>\r
90 #include <microblaze_exceptions_g.h>\r
91 \r
92 /* Tasks are started with a critical section nesting of 0 - however, prior to \r
93 the scheduler being commenced interrupts should not be enabled, so the critical \r
94 nesting variable is initialised to a non-zero value. */\r
95 #define portINITIAL_NESTING_VALUE       ( 0xff )\r
96 \r
97 /* The bit within the MSR register that enabled/disables interrupts. */\r
98 #define portMSR_IE                                      ( 0x02U )\r
99 \r
100 /* If the floating point unit is included in the MicroBlaze build, then the\r
101 FSR register is saved as part of the task context.  portINITIAL_FSR is the value\r
102 given to the FSR register when the initial context is set up for a task being\r
103 created. */\r
104 #define portINITIAL_FSR                         ( 0U )\r
105 /*-----------------------------------------------------------*/\r
106 \r
107 /*\r
108  * Initialise the interrupt controller instance.\r
109  */\r
110 static long prvInitialiseInterruptController( void );\r
111 \r
112 /* Ensure the interrupt controller instance variable is initialised before it is \r
113  * used, and that the initialisation only happens once. \r
114  */\r
115 static long prvEnsureInterruptControllerIsInitialised( void );\r
116 \r
117 /*-----------------------------------------------------------*/\r
118 \r
119 /* Counts the nesting depth of calls to portENTER_CRITICAL().  Each task \r
120 maintains its own count, so this variable is saved as part of the task\r
121 context. */\r
122 volatile unsigned portBASE_TYPE uxCriticalNesting = portINITIAL_NESTING_VALUE;\r
123 \r
124 /* This port uses a separate stack for interrupts.  This prevents the stack of\r
125 every task needing to be large enough to hold an entire interrupt stack on top\r
126 of the task stack. */\r
127 unsigned long *pulISRStack;\r
128 \r
129 /* If an interrupt requests a context switch, then ulTaskSwitchRequested will\r
130 get set to 1.  ulTaskSwitchRequested is inspected just before the main interrupt\r
131 handler exits.  If, at that time, ulTaskSwitchRequested is set to 1, the kernel\r
132 will call vTaskSwitchContext() to ensure the task that runs immediately after\r
133 the interrupt exists is the highest priority task that is able to run.  This is \r
134 an unusual mechanism, but is used for this port because a single interrupt can \r
135 cause the servicing of multiple peripherals - and it is inefficient to call\r
136 vTaskSwitchContext() multiple times as each peripheral is serviced. */\r
137 volatile unsigned long ulTaskSwitchRequested = 0UL;\r
138 \r
139 /* The instance of the interrupt controller used by this port.  This is required\r
140 by the Xilinx library API functions. */\r
141 static XIntc xInterruptControllerInstance;\r
142 \r
143 /*-----------------------------------------------------------*/\r
144 \r
145 /* \r
146  * Initialise the stack of a task to look exactly as if a call to \r
147  * portSAVE_CONTEXT had been made.\r
148  * \r
149  * See the portable.h header file.\r
150  */\r
151 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
152 {\r
153 extern void *_SDA2_BASE_, *_SDA_BASE_;\r
154 const unsigned long ulR2 = ( unsigned long ) &_SDA2_BASE_;\r
155 const unsigned long ulR13 = ( unsigned long ) &_SDA_BASE_;\r
156 \r
157         /* Place a few bytes of known values on the bottom of the stack. \r
158         This is essential for the Microblaze port and these lines must\r
159         not be omitted. */\r
160         *pxTopOfStack = ( portSTACK_TYPE ) 0x00000000;\r
161         pxTopOfStack--;\r
162         *pxTopOfStack = ( portSTACK_TYPE ) 0x00000000;\r
163         pxTopOfStack--;\r
164         *pxTopOfStack = ( portSTACK_TYPE ) 0x00000000;\r
165         pxTopOfStack--;\r
166 \r
167         #if XPAR_MICROBLAZE_0_USE_FPU == 1\r
168                 /* The FSR value placed in the initial task context is just 0. */\r
169                 *pxTopOfStack = portINITIAL_FSR;\r
170                 pxTopOfStack--;\r
171         #endif\r
172 \r
173         /* The MSR value placed in the initial task context should have interrupts\r
174         disabled.  Each task will enable interrupts automatically when it enters\r
175         the running state for the first time. */\r
176         *pxTopOfStack = mfmsr() & ~portMSR_IE;\r
177         pxTopOfStack--;\r
178 \r
179         /* First stack an initial value for the critical section nesting.  This\r
180         is initialised to zero. */\r
181         *pxTopOfStack = ( portSTACK_TYPE ) 0x00;\r
182         \r
183         /* R0 is always zero. */\r
184         /* R1 is the SP. */\r
185 \r
186         /* Place an initial value for all the general purpose registers. */\r
187         pxTopOfStack--;\r
188         *pxTopOfStack = ( portSTACK_TYPE ) ulR2;        /* R2 - read only small data area. */\r
189         pxTopOfStack--;\r
190         *pxTopOfStack = ( portSTACK_TYPE ) 0x03;        /* R3 - return values and temporaries. */\r
191         pxTopOfStack--;\r
192         *pxTopOfStack = ( portSTACK_TYPE ) 0x04;        /* R4 - return values and temporaries. */\r
193         pxTopOfStack--;\r
194         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;/* R5 contains the function call parameters. */\r
195 \r
196         #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING\r
197                 pxTopOfStack--;\r
198                 *pxTopOfStack = ( portSTACK_TYPE ) 0x06;        /* R6 - other parameters and temporaries.  Used as the return address from vPortTaskEntryPoint. */\r
199                 pxTopOfStack--;\r
200                 *pxTopOfStack = ( portSTACK_TYPE ) 0x07;        /* R7 - other parameters and temporaries. */\r
201                 pxTopOfStack--;\r
202                 *pxTopOfStack = ( portSTACK_TYPE ) 0x08;        /* R8 - other parameters and temporaries. */\r
203                 pxTopOfStack--;\r
204                 *pxTopOfStack = ( portSTACK_TYPE ) 0x09;        /* R9 - other parameters and temporaries. */\r
205                 pxTopOfStack--;\r
206                 *pxTopOfStack = ( portSTACK_TYPE ) 0x0a;        /* R10 - other parameters and temporaries. */\r
207                 pxTopOfStack--;\r
208                 *pxTopOfStack = ( portSTACK_TYPE ) 0x0b;        /* R11 - temporaries. */\r
209                 pxTopOfStack--;\r
210                 *pxTopOfStack = ( portSTACK_TYPE ) 0x0c;        /* R12 - temporaries. */\r
211                 pxTopOfStack--;\r
212         #else\r
213                 pxTopOfStack-= 8;\r
214         #endif\r
215         \r
216         *pxTopOfStack = ( portSTACK_TYPE ) ulR13;       /* R13 - read/write small data area. */\r
217         pxTopOfStack--;\r
218         *pxTopOfStack = ( portSTACK_TYPE ) pxCode;      /* R14 - return address for interrupt. */\r
219         pxTopOfStack--;\r
220         *pxTopOfStack = ( portSTACK_TYPE ) NULL;        /* R15 - return address for subroutine. */\r
221         \r
222         #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING\r
223                 pxTopOfStack--;\r
224                 *pxTopOfStack = ( portSTACK_TYPE ) 0x10;        /* R16 - return address for trap (debugger). */\r
225                 pxTopOfStack--;\r
226                 *pxTopOfStack = ( portSTACK_TYPE ) 0x11;        /* R17 - return address for exceptions, if configured. */\r
227                 pxTopOfStack--;\r
228                 *pxTopOfStack = ( portSTACK_TYPE ) 0x12;        /* R18 - reserved for assembler and compiler temporaries. */\r
229                 pxTopOfStack--;\r
230         #else\r
231                 pxTopOfStack -= 4;\r
232         #endif\r
233         \r
234         *pxTopOfStack = ( portSTACK_TYPE ) 0x00;        /* R19 - must be saved across function calls. Callee-save.  Seems to be interpreted as the frame pointer. */\r
235         \r
236         #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING \r
237                 pxTopOfStack--;\r
238                 *pxTopOfStack = ( portSTACK_TYPE ) 0x14;        /* R20 - reserved for storing a pointer to the Global Offset Table (GOT) in Position Independent Code (PIC). Non-volatile in non-PIC code. Must be saved across function calls. Callee-save.  Not used by FreeRTOS. */\r
239                 pxTopOfStack--;\r
240                 *pxTopOfStack = ( portSTACK_TYPE ) 0x15;        /* R21 - must be saved across function calls. Callee-save. */\r
241                 pxTopOfStack--;\r
242                 *pxTopOfStack = ( portSTACK_TYPE ) 0x16;        /* R22 - must be saved across function calls. Callee-save. */\r
243                 pxTopOfStack--;\r
244                 *pxTopOfStack = ( portSTACK_TYPE ) 0x17;        /* R23 - must be saved across function calls. Callee-save. */\r
245                 pxTopOfStack--;\r
246                 *pxTopOfStack = ( portSTACK_TYPE ) 0x18;        /* R24 - must be saved across function calls. Callee-save. */\r
247                 pxTopOfStack--;\r
248                 *pxTopOfStack = ( portSTACK_TYPE ) 0x19;        /* R25 - must be saved across function calls. Callee-save. */\r
249                 pxTopOfStack--;\r
250                 *pxTopOfStack = ( portSTACK_TYPE ) 0x1a;        /* R26 - must be saved across function calls. Callee-save. */\r
251                 pxTopOfStack--;\r
252                 *pxTopOfStack = ( portSTACK_TYPE ) 0x1b;        /* R27 - must be saved across function calls. Callee-save. */\r
253                 pxTopOfStack--;\r
254                 *pxTopOfStack = ( portSTACK_TYPE ) 0x1c;        /* R28 - must be saved across function calls. Callee-save. */\r
255                 pxTopOfStack--;\r
256                 *pxTopOfStack = ( portSTACK_TYPE ) 0x1d;        /* R29 - must be saved across function calls. Callee-save. */\r
257                 pxTopOfStack--;\r
258                 *pxTopOfStack = ( portSTACK_TYPE ) 0x1e;        /* R30 - must be saved across function calls. Callee-save. */\r
259                 pxTopOfStack--;\r
260                 *pxTopOfStack = ( portSTACK_TYPE ) 0x1f;        /* R31 - must be saved across function calls. Callee-save. */\r
261                 pxTopOfStack--;\r
262         #else\r
263                 pxTopOfStack -= 13;\r
264         #endif\r
265 \r
266         /* Return a pointer to the top of the stack that has been generated so this \r
267         can     be stored in the task control block for the task. */\r
268         return pxTopOfStack;\r
269 }\r
270 /*-----------------------------------------------------------*/\r
271 \r
272 portBASE_TYPE xPortStartScheduler( void )\r
273 {\r
274 extern void ( vPortStartFirstTask )( void );\r
275 extern unsigned long _stack[];\r
276 \r
277         /* Setup the hardware to generate the tick.  Interrupts are disabled when\r
278         this function is called.  \r
279         \r
280         This port uses an application defined callback function to install the tick\r
281         interrupt handler because the kernel will run on lots of different \r
282         MicroBlaze and FPGA configurations - not all of which will have the same \r
283         timer peripherals defined or available.  An example definition of\r
284         vApplicationSetupTimerInterrupt() is provided in the official demo\r
285         application that accompanies this port. */\r
286         vApplicationSetupTimerInterrupt();\r
287 \r
288         /* Reuse the stack from main() as the stack for the interrupts/exceptions. */\r
289         pulISRStack = ( unsigned long * ) _stack;\r
290 \r
291         /* Ensure there is enough space for the functions called from the interrupt\r
292         service routines to write back into the stack frame of the caller. */\r
293         pulISRStack -= 2;\r
294 \r
295         /* Restore the context of the first task that is going to run.  From here\r
296         on, the created tasks will be executing. */\r
297         vPortStartFirstTask();\r
298 \r
299         /* Should not get here as the tasks are now running! */\r
300         return pdFALSE;\r
301 }\r
302 /*-----------------------------------------------------------*/\r
303 \r
304 void vPortEndScheduler( void )\r
305 {\r
306         /* Not implemented. */\r
307 }\r
308 /*-----------------------------------------------------------*/\r
309 \r
310 /*\r
311  * Manual context switch called by portYIELD or taskYIELD.  \r
312  */\r
313 void vPortYield( void )\r
314 {\r
315 extern void VPortYieldASM( void );\r
316 \r
317         /* Perform the context switch in a critical section to assure it is\r
318         not interrupted by the tick ISR.  It is not a problem to do this as\r
319         each task maintains its own interrupt status. */\r
320         portENTER_CRITICAL();\r
321         {\r
322                 /* Jump directly to the yield function to ensure there is no\r
323                 compiler generated prologue code. */\r
324                 asm volatile (  "bralid r14, VPortYieldASM              \n\t" \\r
325                                                 "or r0, r0, r0                                  \n\t" );\r
326         }\r
327         portEXIT_CRITICAL();\r
328 }\r
329 /*-----------------------------------------------------------*/\r
330 \r
331 void vPortEnableInterrupt( unsigned char ucInterruptID )\r
332 {\r
333 long lReturn;\r
334 \r
335         /* An API function is provided to enable an interrupt in the interrupt\r
336         controller because the interrupt controller instance variable is private\r
337         to this file. */\r
338         lReturn = prvEnsureInterruptControllerIsInitialised();\r
339         if( lReturn == pdPASS )\r
340         {\r
341                 XIntc_Enable( &xInterruptControllerInstance, ucInterruptID );\r
342         }\r
343         \r
344         configASSERT( lReturn );\r
345 }\r
346 /*-----------------------------------------------------------*/\r
347 \r
348 void vPortDisableInterrupt( unsigned char ucInterruptID )\r
349 {\r
350 long lReturn;\r
351 \r
352         /* An API function is provided to disable an interrupt in the interrupt\r
353         controller because the interrupt controller instance variable is private\r
354         to this file. */\r
355         lReturn = prvEnsureInterruptControllerIsInitialised();\r
356         \r
357         if( lReturn == pdPASS )\r
358         {\r
359                 XIntc_Disable( &xInterruptControllerInstance, ucInterruptID );\r
360         }\r
361         \r
362         configASSERT( lReturn );\r
363 }\r
364 /*-----------------------------------------------------------*/\r
365 \r
366 portBASE_TYPE xPortInstallInterruptHandler( unsigned char ucInterruptID, XInterruptHandler pxHandler, void *pvCallBackRef )\r
367 {\r
368 long lReturn;\r
369 \r
370         /* An API function is provided to install an interrupt handler because the \r
371         interrupt controller instance variable is private to this file. */\r
372 \r
373         lReturn = prvEnsureInterruptControllerIsInitialised();\r
374         \r
375         if( lReturn == pdPASS )\r
376         {\r
377                 lReturn = XIntc_Connect( &xInterruptControllerInstance, ucInterruptID, pxHandler, pvCallBackRef );\r
378         }\r
379 \r
380         if( lReturn == XST_SUCCESS )\r
381         {\r
382                 lReturn = pdPASS;\r
383         }\r
384         \r
385         configASSERT( lReturn == pdPASS );\r
386 \r
387         return lReturn;\r
388 }\r
389 /*-----------------------------------------------------------*/\r
390 \r
391 static long prvEnsureInterruptControllerIsInitialised( void )\r
392 {\r
393 static long lInterruptControllerInitialised = pdFALSE;\r
394 long lReturn;\r
395 \r
396         /* Ensure the interrupt controller instance variable is initialised before\r
397         it is used, and that the initialisation only happens once. */\r
398         if( lInterruptControllerInitialised != pdTRUE )\r
399         {\r
400                 lReturn = prvInitialiseInterruptController();\r
401                 \r
402                 if( lReturn == pdPASS )\r
403                 {\r
404                         lInterruptControllerInitialised = pdTRUE;\r
405                 }\r
406         }\r
407         else\r
408         {\r
409                 lReturn = pdPASS;\r
410         }\r
411 \r
412         return lReturn;\r
413 }\r
414 /*-----------------------------------------------------------*/\r
415 \r
416 /* \r
417  * Handler for the timer interrupt.  This is the handler that the application\r
418  * defined callback function vApplicationSetupTimerInterrupt() should install.\r
419  */\r
420 void vPortTickISR( void *pvUnused )\r
421 {\r
422 extern void vApplicationClearTimerInterrupt( void );\r
423 \r
424         /* Ensure the unused parameter does not generate a compiler warning. */\r
425         ( void ) pvUnused;\r
426 \r
427         /* This port uses an application defined callback function to clear the tick\r
428         interrupt because the kernel will run on lots of different MicroBlaze and \r
429         FPGA configurations - not all of which will have the same timer peripherals \r
430         defined or available.  An example definition of\r
431         vApplicationClearTimerInterrupt() is provided in the official demo\r
432         application that accompanies this port. */      \r
433         vApplicationClearTimerInterrupt();\r
434 \r
435         /* Increment the RTOS tick - this might cause a task to unblock. */\r
436         vTaskIncrementTick();\r
437 \r
438         /* If the preemptive scheduler is being used then a context switch should be\r
439         requested in case incrementing the tick unblocked a task, or a time slice\r
440         should cause another task to enter the Running state. */\r
441         #if configUSE_PREEMPTION == 1\r
442                 /* Force vTaskSwitchContext() to be called as the interrupt exits. */\r
443                 ulTaskSwitchRequested = 1;\r
444         #endif\r
445 }\r
446 /*-----------------------------------------------------------*/\r
447 \r
448 static long prvInitialiseInterruptController( void )\r
449 {\r
450 long lStatus;\r
451 \r
452         lStatus = XIntc_Initialize( &xInterruptControllerInstance, configINTERRUPT_CONTROLLER_TO_USE );\r
453 \r
454         if( lStatus == XST_SUCCESS )\r
455         {\r
456                 /* Initialise the exception table. */\r
457                 Xil_ExceptionInit();\r
458 \r
459             /* Service all pending interrupts each time the handler is entered. */\r
460             XIntc_SetIntrSvcOption( xInterruptControllerInstance.BaseAddress, XIN_SVC_ALL_ISRS_OPTION );\r
461 \r
462             /* Install exception handlers if the MicroBlaze is configured to handle\r
463             exceptions, and the application defined constant\r
464             configINSTALL_EXCEPTION_HANDLERS is set to 1. */\r
465                 #if ( MICROBLAZE_EXCEPTIONS_ENABLED == 1 ) && ( configINSTALL_EXCEPTION_HANDLERS == 1 )\r
466             {\r
467                 vPortExceptionsInstallHandlers();\r
468             }\r
469                 #endif /* MICROBLAZE_EXCEPTIONS_ENABLED */\r
470 \r
471                 /* Start the interrupt controller.  Interrupts are enabled when the\r
472                 scheduler starts. */\r
473                 lStatus = XIntc_Start( &xInterruptControllerInstance, XIN_REAL_MODE );\r
474 \r
475                 if( lStatus == XST_SUCCESS )\r
476                 {\r
477                         lStatus = pdPASS;\r
478                 }\r
479                 else\r
480                 {\r
481                         lStatus = pdFAIL;\r
482                 }\r
483         }\r
484 \r
485         configASSERT( lStatus == pdPASS );\r
486 \r
487         return lStatus;\r
488 }\r
489 /*-----------------------------------------------------------*/\r
490 \r
491 \r