]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/MicroBlazeV9/port.c
Update version numbers in preparation for a new release.
[freertos] / FreeRTOS / Source / portable / GCC / MicroBlazeV9 / port.c
1 /*\r
2  * FreeRTOS Kernel V10.1.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*-----------------------------------------------------------\r
29  * Implementation of functions defined in portable.h for the MicroBlaze port.\r
30  *----------------------------------------------------------*/\r
31 \r
32 \r
33 /* Scheduler includes. */\r
34 #include "FreeRTOS.h"\r
35 #include "task.h"\r
36 \r
37 /* Standard includes. */\r
38 #include <string.h>\r
39 \r
40 /* Hardware includes. */\r
41 #include <xintc_i.h>\r
42 #include <xil_exception.h>\r
43 #include <microblaze_exceptions_g.h>\r
44 \r
45 /* Tasks are started with a critical section nesting of 0 - however, prior to\r
46 the scheduler being commenced interrupts should not be enabled, so the critical\r
47 nesting variable is initialised to a non-zero value. */\r
48 #define portINITIAL_NESTING_VALUE       ( 0xff )\r
49 \r
50 /* The bit within the MSR register that enabled/disables interrupts and\r
51 exceptions respectively. */\r
52 #define portMSR_IE                                      ( 0x02U )\r
53 #define portMSR_EE                                      ( 0x100U )\r
54 \r
55 /* If the floating point unit is included in the MicroBlaze build, then the\r
56 FSR register is saved as part of the task context.  portINITIAL_FSR is the value\r
57 given to the FSR register when the initial context is set up for a task being\r
58 created. */\r
59 #define portINITIAL_FSR                         ( 0U )\r
60 \r
61 /*-----------------------------------------------------------*/\r
62 \r
63 /*\r
64  * Initialise the interrupt controller instance.\r
65  */\r
66 static int32_t prvInitialiseInterruptController( void );\r
67 \r
68 /* Ensure the interrupt controller instance variable is initialised before it is\r
69  * used, and that the initialisation only happens once.\r
70  */\r
71 static int32_t prvEnsureInterruptControllerIsInitialised( void );\r
72 \r
73 /*-----------------------------------------------------------*/\r
74 \r
75 /* Counts the nesting depth of calls to portENTER_CRITICAL().  Each task\r
76 maintains its own count, so this variable is saved as part of the task\r
77 context. */\r
78 volatile UBaseType_t uxCriticalNesting = portINITIAL_NESTING_VALUE;\r
79 \r
80 /* This port uses a separate stack for interrupts.  This prevents the stack of\r
81 every task needing to be large enough to hold an entire interrupt stack on top\r
82 of the task stack. */\r
83 uint32_t *pulISRStack;\r
84 \r
85 /* If an interrupt requests a context switch, then ulTaskSwitchRequested will\r
86 get set to 1.  ulTaskSwitchRequested is inspected just before the main interrupt\r
87 handler exits.  If, at that time, ulTaskSwitchRequested is set to 1, the kernel\r
88 will call vTaskSwitchContext() to ensure the task that runs immediately after\r
89 the interrupt exists is the highest priority task that is able to run.  This is\r
90 an unusual mechanism, but is used for this port because a single interrupt can\r
91 cause the servicing of multiple peripherals - and it is inefficient to call\r
92 vTaskSwitchContext() multiple times as each peripheral is serviced. */\r
93 volatile uint32_t ulTaskSwitchRequested = 0UL;\r
94 \r
95 /* The instance of the interrupt controller used by this port.  This is required\r
96 by the Xilinx library API functions. */\r
97 static XIntc xInterruptControllerInstance;\r
98 \r
99 /*-----------------------------------------------------------*/\r
100 \r
101 /*\r
102  * Initialise the stack of a task to look exactly as if a call to\r
103  * portSAVE_CONTEXT had been made.\r
104  *\r
105  * See the portable.h header file.\r
106  */\r
107 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
108 {\r
109 extern void *_SDA2_BASE_, *_SDA_BASE_;\r
110 const uint32_t ulR2 = ( uint32_t ) &_SDA2_BASE_;\r
111 const uint32_t ulR13 = ( uint32_t ) &_SDA_BASE_;\r
112 extern void _start1( void );\r
113 \r
114         /* Place a few bytes of known values on the bottom of the stack.\r
115         This is essential for the Microblaze port and these lines must\r
116         not be omitted. */\r
117         *pxTopOfStack = ( StackType_t ) 0x00000000;\r
118         pxTopOfStack--;\r
119         *pxTopOfStack = ( StackType_t ) 0x00000000;\r
120         pxTopOfStack--;\r
121         *pxTopOfStack = ( StackType_t ) 0x00000000;\r
122         pxTopOfStack--;\r
123 \r
124         #if( XPAR_MICROBLAZE_USE_FPU != 0 )\r
125                 /* The FSR value placed in the initial task context is just 0. */\r
126                 *pxTopOfStack = portINITIAL_FSR;\r
127                 pxTopOfStack--;\r
128         #endif\r
129 \r
130         /* The MSR value placed in the initial task context should have interrupts\r
131         disabled.  Each task will enable interrupts automatically when it enters\r
132         the running state for the first time. */\r
133         *pxTopOfStack = mfmsr() & ~portMSR_IE;\r
134 \r
135         #if( MICROBLAZE_EXCEPTIONS_ENABLED == 1 )\r
136         {\r
137                 /* Ensure exceptions are enabled for the task. */\r
138                 *pxTopOfStack |= portMSR_EE;\r
139         }\r
140         #endif\r
141 \r
142         pxTopOfStack--;\r
143 \r
144         /* First stack an initial value for the critical section nesting.  This\r
145         is initialised to zero. */\r
146         *pxTopOfStack = ( StackType_t ) 0x00;\r
147 \r
148         /* R0 is always zero. */\r
149         /* R1 is the SP. */\r
150 \r
151         /* Place an initial value for all the general purpose registers. */\r
152         pxTopOfStack--;\r
153         *pxTopOfStack = ( StackType_t ) ulR2;   /* R2 - read only small data area. */\r
154         pxTopOfStack--;\r
155         *pxTopOfStack = ( StackType_t ) 0x03;   /* R3 - return values and temporaries. */\r
156         pxTopOfStack--;\r
157         *pxTopOfStack = ( StackType_t ) 0x04;   /* R4 - return values and temporaries. */\r
158         pxTopOfStack--;\r
159         *pxTopOfStack = ( StackType_t ) pvParameters;/* R5 contains the function call parameters. */\r
160 \r
161         #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING\r
162                 pxTopOfStack--;\r
163                 *pxTopOfStack = ( StackType_t ) 0x06;   /* R6 - other parameters and temporaries. */\r
164                 pxTopOfStack--;\r
165                 *pxTopOfStack = ( StackType_t ) 0x07;   /* R7 - other parameters and temporaries. */\r
166                 pxTopOfStack--;\r
167                 *pxTopOfStack = ( StackType_t ) NULL;   /* R8 - other parameters and temporaries. */\r
168                 pxTopOfStack--;\r
169                 *pxTopOfStack = ( StackType_t ) 0x09;   /* R9 - other parameters and temporaries. */\r
170                 pxTopOfStack--;\r
171                 *pxTopOfStack = ( StackType_t ) 0x0a;   /* R10 - other parameters and temporaries. */\r
172                 pxTopOfStack--;\r
173                 *pxTopOfStack = ( StackType_t ) 0x0b;   /* R11 - temporaries. */\r
174                 pxTopOfStack--;\r
175                 *pxTopOfStack = ( StackType_t ) 0x0c;   /* R12 - temporaries. */\r
176                 pxTopOfStack--;\r
177         #else\r
178                 pxTopOfStack-= 8;\r
179         #endif\r
180 \r
181         *pxTopOfStack = ( StackType_t ) ulR13;  /* R13 - read/write small data area. */\r
182         pxTopOfStack--;\r
183         *pxTopOfStack = ( StackType_t ) pxCode; /* R14 - return address for interrupt. */\r
184         pxTopOfStack--;\r
185         *pxTopOfStack = ( StackType_t ) _start1;        /* R15 - return address for subroutine. */\r
186 \r
187         #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING\r
188                 pxTopOfStack--;\r
189                 *pxTopOfStack = ( StackType_t ) 0x10;   /* R16 - return address for trap (debugger). */\r
190                 pxTopOfStack--;\r
191                 *pxTopOfStack = ( StackType_t ) 0x11;   /* R17 - return address for exceptions, if configured. */\r
192                 pxTopOfStack--;\r
193                 *pxTopOfStack = ( StackType_t ) 0x12;   /* R18 - reserved for assembler and compiler temporaries. */\r
194                 pxTopOfStack--;\r
195         #else\r
196                 pxTopOfStack -= 4;\r
197         #endif\r
198 \r
199         *pxTopOfStack = ( StackType_t ) 0x00;   /* R19 - must be saved across function calls. Callee-save.  Seems to be interpreted as the frame pointer. */\r
200 \r
201         #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING\r
202                 pxTopOfStack--;\r
203                 *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
204                 pxTopOfStack--;\r
205                 *pxTopOfStack = ( StackType_t ) 0x15;   /* R21 - must be saved across function calls. Callee-save. */\r
206                 pxTopOfStack--;\r
207                 *pxTopOfStack = ( StackType_t ) 0x16;   /* R22 - must be saved across function calls. Callee-save. */\r
208                 pxTopOfStack--;\r
209                 *pxTopOfStack = ( StackType_t ) 0x17;   /* R23 - must be saved across function calls. Callee-save. */\r
210                 pxTopOfStack--;\r
211                 *pxTopOfStack = ( StackType_t ) 0x18;   /* R24 - must be saved across function calls. Callee-save. */\r
212                 pxTopOfStack--;\r
213                 *pxTopOfStack = ( StackType_t ) 0x19;   /* R25 - must be saved across function calls. Callee-save. */\r
214                 pxTopOfStack--;\r
215                 *pxTopOfStack = ( StackType_t ) 0x1a;   /* R26 - must be saved across function calls. Callee-save. */\r
216                 pxTopOfStack--;\r
217                 *pxTopOfStack = ( StackType_t ) 0x1b;   /* R27 - must be saved across function calls. Callee-save. */\r
218                 pxTopOfStack--;\r
219                 *pxTopOfStack = ( StackType_t ) 0x1c;   /* R28 - must be saved across function calls. Callee-save. */\r
220                 pxTopOfStack--;\r
221                 *pxTopOfStack = ( StackType_t ) 0x1d;   /* R29 - must be saved across function calls. Callee-save. */\r
222                 pxTopOfStack--;\r
223                 *pxTopOfStack = ( StackType_t ) 0x1e;   /* R30 - must be saved across function calls. Callee-save. */\r
224                 pxTopOfStack--;\r
225                 *pxTopOfStack = ( StackType_t ) 0x1f;   /* R31 - must be saved across function calls. Callee-save. */\r
226                 pxTopOfStack--;\r
227         #else\r
228                 pxTopOfStack -= 13;\r
229         #endif\r
230 \r
231         /* Return a pointer to the top of the stack that has been generated so this\r
232         can     be stored in the task control block for the task. */\r
233         return pxTopOfStack;\r
234 }\r
235 /*-----------------------------------------------------------*/\r
236 \r
237 BaseType_t xPortStartScheduler( void )\r
238 {\r
239 extern void ( vPortStartFirstTask )( void );\r
240 extern uint32_t _stack[];\r
241 \r
242         /* Setup the hardware to generate the tick.  Interrupts are disabled when\r
243         this function is called.\r
244 \r
245         This port uses an application defined callback function to install the tick\r
246         interrupt handler because the kernel will run on lots of different\r
247         MicroBlaze and FPGA configurations - not all of which will have the same\r
248         timer peripherals defined or available.  An example definition of\r
249         vApplicationSetupTimerInterrupt() is provided in the official demo\r
250         application that accompanies this port. */\r
251         vApplicationSetupTimerInterrupt();\r
252 \r
253         /* Reuse the stack from main() as the stack for the interrupts/exceptions. */\r
254         pulISRStack = ( uint32_t * ) _stack;\r
255 \r
256         /* Ensure there is enough space for the functions called from the interrupt\r
257         service routines to write back into the stack frame of the caller. */\r
258         pulISRStack -= 2;\r
259 \r
260         /* Restore the context of the first task that is going to run.  From here\r
261         on, the created tasks will be executing. */\r
262         vPortStartFirstTask();\r
263 \r
264         /* Should not get here as the tasks are now running! */\r
265         return pdFALSE;\r
266 }\r
267 /*-----------------------------------------------------------*/\r
268 \r
269 void vPortEndScheduler( void )\r
270 {\r
271         /* Not implemented in ports where there is nothing to return to.\r
272         Artificially force an assert. */\r
273         configASSERT( uxCriticalNesting == 1000UL );\r
274 }\r
275 /*-----------------------------------------------------------*/\r
276 \r
277 /*\r
278  * Manual context switch called by portYIELD or taskYIELD.\r
279  */\r
280 void vPortYield( void )\r
281 {\r
282 extern void VPortYieldASM( void );\r
283 \r
284         /* Perform the context switch in a critical section to assure it is\r
285         not interrupted by the tick ISR.  It is not a problem to do this as\r
286         each task maintains its own interrupt status. */\r
287         portENTER_CRITICAL();\r
288         {\r
289                 /* Jump directly to the yield function to ensure there is no\r
290                 compiler generated prologue code. */\r
291                 asm volatile (  "bralid r14, VPortYieldASM              \n\t" \\r
292                                                 "or r0, r0, r0                                  \n\t" );\r
293         }\r
294         portEXIT_CRITICAL();\r
295 }\r
296 /*-----------------------------------------------------------*/\r
297 \r
298 void vPortEnableInterrupt( uint8_t ucInterruptID )\r
299 {\r
300 int32_t lReturn;\r
301 \r
302         /* An API function is provided to enable an interrupt in the interrupt\r
303         controller because the interrupt controller instance variable is private\r
304         to this file. */\r
305         lReturn = prvEnsureInterruptControllerIsInitialised();\r
306         if( lReturn == pdPASS )\r
307         {\r
308                 /* Critical section protects read/modify/writer operation inside\r
309                 XIntc_Enable(). */\r
310                 portENTER_CRITICAL();\r
311                 {\r
312                         XIntc_Enable( &xInterruptControllerInstance, ucInterruptID );\r
313                 }\r
314                 portEXIT_CRITICAL();\r
315         }\r
316 \r
317         configASSERT( lReturn );\r
318 }\r
319 /*-----------------------------------------------------------*/\r
320 \r
321 void vPortDisableInterrupt( uint8_t ucInterruptID )\r
322 {\r
323 int32_t lReturn;\r
324 \r
325         /* An API function is provided to disable 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 \r
330         if( lReturn == pdPASS )\r
331         {\r
332                 XIntc_Disable( &xInterruptControllerInstance, ucInterruptID );\r
333         }\r
334 \r
335         configASSERT( lReturn );\r
336 }\r
337 /*-----------------------------------------------------------*/\r
338 \r
339 BaseType_t xPortInstallInterruptHandler( uint8_t ucInterruptID, XInterruptHandler pxHandler, void *pvCallBackRef )\r
340 {\r
341 int32_t lReturn;\r
342 \r
343         /* An API function is provided to install an interrupt handler because the\r
344         interrupt controller instance variable is private to this file. */\r
345 \r
346         lReturn = prvEnsureInterruptControllerIsInitialised();\r
347 \r
348         if( lReturn == pdPASS )\r
349         {\r
350                 lReturn = XIntc_Connect( &xInterruptControllerInstance, ucInterruptID, pxHandler, pvCallBackRef );\r
351         }\r
352 \r
353         if( lReturn == XST_SUCCESS )\r
354         {\r
355                 lReturn = pdPASS;\r
356         }\r
357 \r
358         configASSERT( lReturn == pdPASS );\r
359 \r
360         return lReturn;\r
361 }\r
362 /*-----------------------------------------------------------*/\r
363 \r
364 static int32_t prvEnsureInterruptControllerIsInitialised( void )\r
365 {\r
366 static int32_t lInterruptControllerInitialised = pdFALSE;\r
367 int32_t lReturn;\r
368 \r
369         /* Ensure the interrupt controller instance variable is initialised before\r
370         it is used, and that the initialisation only happens once. */\r
371         if( lInterruptControllerInitialised != pdTRUE )\r
372         {\r
373                 lReturn = prvInitialiseInterruptController();\r
374 \r
375                 if( lReturn == pdPASS )\r
376                 {\r
377                         lInterruptControllerInitialised = pdTRUE;\r
378                 }\r
379         }\r
380         else\r
381         {\r
382                 lReturn = pdPASS;\r
383         }\r
384 \r
385         return lReturn;\r
386 }\r
387 /*-----------------------------------------------------------*/\r
388 \r
389 /*\r
390  * Handler for the timer interrupt.  This is the handler that the application\r
391  * defined callback function vApplicationSetupTimerInterrupt() should install.\r
392  */\r
393 void vPortTickISR( void *pvUnused )\r
394 {\r
395 extern void vApplicationClearTimerInterrupt( void );\r
396 \r
397         /* Ensure the unused parameter does not generate a compiler warning. */\r
398         ( void ) pvUnused;\r
399 \r
400         /* This port uses an application defined callback function to clear the tick\r
401         interrupt because the kernel will run on lots of different MicroBlaze and\r
402         FPGA configurations - not all of which will have the same timer peripherals\r
403         defined or available.  An example definition of\r
404         vApplicationClearTimerInterrupt() is provided in the official demo\r
405         application that accompanies this port. */\r
406         vApplicationClearTimerInterrupt();\r
407 \r
408         /* Increment the RTOS tick - this might cause a task to unblock. */\r
409         if( xTaskIncrementTick() != pdFALSE )\r
410         {\r
411                 /* Force vTaskSwitchContext() to be called as the interrupt exits. */\r
412                 ulTaskSwitchRequested = 1;\r
413         }\r
414 }\r
415 /*-----------------------------------------------------------*/\r
416 \r
417 static int32_t prvInitialiseInterruptController( void )\r
418 {\r
419 int32_t lStatus;\r
420 \r
421         lStatus = XIntc_Initialize( &xInterruptControllerInstance, configINTERRUPT_CONTROLLER_TO_USE );\r
422 \r
423         if( lStatus == XST_SUCCESS )\r
424         {\r
425                 /* Initialise the exception table. */\r
426                 Xil_ExceptionInit();\r
427 \r
428             /* Service all pending interrupts each time the handler is entered. */\r
429             XIntc_SetIntrSvcOption( xInterruptControllerInstance.BaseAddress, XIN_SVC_ALL_ISRS_OPTION );\r
430 \r
431             /* Install exception handlers if the MicroBlaze is configured to handle\r
432             exceptions, and the application defined constant\r
433             configINSTALL_EXCEPTION_HANDLERS is set to 1. */\r
434                 #if ( MICROBLAZE_EXCEPTIONS_ENABLED == 1 ) && ( configINSTALL_EXCEPTION_HANDLERS == 1 )\r
435             {\r
436                 vPortExceptionsInstallHandlers();\r
437             }\r
438                 #endif /* MICROBLAZE_EXCEPTIONS_ENABLED */\r
439 \r
440                 /* Start the interrupt controller.  Interrupts are enabled when the\r
441                 scheduler starts. */\r
442                 lStatus = XIntc_Start( &xInterruptControllerInstance, XIN_REAL_MODE );\r
443 \r
444                 if( lStatus == XST_SUCCESS )\r
445                 {\r
446                         lStatus = pdPASS;\r
447                 }\r
448                 else\r
449                 {\r
450                         lStatus = pdFAIL;\r
451                 }\r
452         }\r
453 \r
454         configASSERT( lStatus == pdPASS );\r
455 \r
456         return lStatus;\r
457 }\r
458 /*-----------------------------------------------------------*/\r
459 \r
460 \r