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