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