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