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