2 * FreeRTOS Kernel V11.2.0
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
29 /*-----------------------------------------------------------
30 * Implementation of functions defined in portable.h for the MicroBlaze port.
31 *----------------------------------------------------------*/
34 /* Scheduler includes. */
38 /* Standard includes. */
41 /* Hardware includes. */
43 #include <xil_exception.h>
44 #include <microblaze_exceptions_g.h>
45 #include <microblaze_instructions.h>
47 /* Tasks are started with a critical section nesting of 0 - however, prior to
48 * the scheduler being commenced interrupts should not be enabled, so the critical
49 * nesting variable is initialised to a non-zero value. */
50 #define portINITIAL_NESTING_VALUE ( 0xff )
52 /* The bit within the MSR register that enabled/disables interrupts and
53 * exceptions respectively. */
54 #define portMSR_IE ( 0x02U )
55 #define portMSR_EE ( 0x100U )
57 /* If the floating point unit is included in the MicroBlaze build, then the
58 * FSR register is saved as part of the task context. portINITIAL_FSR is the value
59 * given to the FSR register when the initial context is set up for a task being
61 #define portINITIAL_FSR ( 0U )
63 * Global counter used for calculation of run time statistics of tasks.
64 * Defined only when the relevant option is turned on
66 #if (configGENERATE_RUN_TIME_STATS==1)
67 volatile uint32_t ulHighFrequencyTimerTicks;
70 /*-----------------------------------------------------------*/
73 * Initialise the interrupt controller instance.
75 static int32_t prvInitialiseInterruptController( void );
77 /* Ensure the interrupt controller instance variable is initialised before it is
78 * used, and that the initialisation only happens once.
80 static int32_t prvEnsureInterruptControllerIsInitialised( void );
82 /*-----------------------------------------------------------*/
84 /* Counts the nesting depth of calls to portENTER_CRITICAL(). Each task
85 * maintains its own count, so this variable is saved as part of the task
87 volatile UBaseType_t uxCriticalNesting = portINITIAL_NESTING_VALUE;
89 /* This port uses a separate stack for interrupts. This prevents the stack of
90 * every task needing to be large enough to hold an entire interrupt stack on top
91 * of the task stack. */
92 uint32_t * pulISRStack;
94 /* If an interrupt requests a context switch, then ulTaskSwitchRequested will
95 * get set to 1. ulTaskSwitchRequested is inspected just before the main interrupt
96 * handler exits. If, at that time, ulTaskSwitchRequested is set to 1, the kernel
97 * will call vTaskSwitchContext() to ensure the task that runs immediately after
98 * the interrupt exists is the highest priority task that is able to run. This is
99 * an unusual mechanism, but is used for this port because a single interrupt can
100 * cause the servicing of multiple peripherals - and it is inefficient to call
101 * vTaskSwitchContext() multiple times as each peripheral is serviced. */
102 volatile uint32_t ulTaskSwitchRequested = 0UL;
104 /* The instance of the interrupt controller used by this port. This is required
105 * by the Xilinx library API functions. */
106 static XIntc xInterruptControllerInstance;
108 /*-----------------------------------------------------------*/
111 * Initialise the stack of a task to look exactly as if a call to
112 * portSAVE_CONTEXT had been made.
114 * See the portable.h header file.
116 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
117 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
118 StackType_t * pxEndOfStack,
119 TaskFunction_t pxCode,
120 void * pvParameters )
122 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
123 TaskFunction_t pxCode,
124 void * pvParameters )
127 extern void *_SDA2_BASE_;
128 extern void *_SDA_BASE_;
129 const UINTPTR ulR2 = ( UINTPTR ) &_SDA2_BASE_;
130 const UINTPTR ulR13 = ( UINTPTR ) &_SDA_BASE_;
132 extern void _start1( void );
134 /* Place a few bytes of known values on the bottom of the stack.
135 * This is essential for the Microblaze port and these lines must
137 *pxTopOfStack = ( StackType_t ) 0x00000000;
139 *pxTopOfStack = ( StackType_t ) 0x00000000;
141 *pxTopOfStack = ( StackType_t ) 0x00000000;
144 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
145 /* Store the stack limits. */
146 *pxTopOfStack = ( StackType_t ) ( pxTopOfStack + 3 );
148 *pxTopOfStack = ( StackType_t ) pxEndOfStack;
152 #if ( XPAR_MICROBLAZE_USE_FPU != 0 )
153 /* The FSR value placed in the initial task context is just 0. */
154 *pxTopOfStack = portINITIAL_FSR;
158 /* The MSR value placed in the initial task context should have interrupts
159 * disabled. Each task will enable interrupts automatically when it enters
160 * the running state for the first time. */
161 *pxTopOfStack = mfmsr() & ~portMSR_IE;
163 #if ( MICROBLAZE_EXCEPTIONS_ENABLED == 1 )
165 /* Ensure exceptions are enabled for the task. */
166 *pxTopOfStack |= portMSR_EE;
172 /* First stack an initial value for the critical section nesting. This
173 * is initialised to zero. */
174 *pxTopOfStack = ( StackType_t ) 0x00;
176 /* R0 is always zero. */
179 /* Place an initial value for all the general purpose registers. */
181 *pxTopOfStack = ( StackType_t ) ulR2; /* R2 - read only small data area. */
183 *pxTopOfStack = ( StackType_t ) 0x03; /* R3 - return values and temporaries. */
185 *pxTopOfStack = ( StackType_t ) 0x04; /* R4 - return values and temporaries. */
187 *pxTopOfStack = ( StackType_t ) pvParameters; /* R5 contains the function call parameters. */
189 #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING
191 *pxTopOfStack = ( StackType_t ) 0x06; /* R6 - other parameters and temporaries. */
193 *pxTopOfStack = ( StackType_t ) 0x07; /* R7 - other parameters and temporaries. */
195 *pxTopOfStack = ( StackType_t ) NULL; /* R8 - other parameters and temporaries. */
197 *pxTopOfStack = ( StackType_t ) 0x09; /* R9 - other parameters and temporaries. */
199 *pxTopOfStack = ( StackType_t ) 0x0a; /* R10 - other parameters and temporaries. */
201 *pxTopOfStack = ( StackType_t ) 0x0b; /* R11 - temporaries. */
203 *pxTopOfStack = ( StackType_t ) 0x0c; /* R12 - temporaries. */
205 #else /* ifdef portPRE_LOAD_STACK_FOR_DEBUGGING */
207 #endif /* ifdef portPRE_LOAD_STACK_FOR_DEBUGGING */
209 *pxTopOfStack = ( StackType_t ) ulR13; /* R13 - read/write small data area. */
211 *pxTopOfStack = ( StackType_t ) pxCode; /* R14 - return address for interrupt. */
213 *pxTopOfStack = ( StackType_t ) _start1; /* R15 - return address for subroutine. */
215 #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING
217 *pxTopOfStack = ( StackType_t ) 0x10; /* R16 - return address for trap (debugger). */
219 *pxTopOfStack = ( StackType_t ) 0x11; /* R17 - return address for exceptions, if configured. */
221 *pxTopOfStack = ( StackType_t ) 0x12; /* R18 - reserved for assembler and compiler temporaries. */
227 *pxTopOfStack = ( StackType_t ) 0x00; /* R19 - must be saved across function calls. Callee-save. Seems to be interpreted as the frame pointer. */
229 #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING
231 *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. */
233 *pxTopOfStack = ( StackType_t ) 0x15; /* R21 - must be saved across function calls. Callee-save. */
235 *pxTopOfStack = ( StackType_t ) 0x16; /* R22 - must be saved across function calls. Callee-save. */
237 *pxTopOfStack = ( StackType_t ) 0x17; /* R23 - must be saved across function calls. Callee-save. */
239 *pxTopOfStack = ( StackType_t ) 0x18; /* R24 - must be saved across function calls. Callee-save. */
241 *pxTopOfStack = ( StackType_t ) 0x19; /* R25 - must be saved across function calls. Callee-save. */
243 *pxTopOfStack = ( StackType_t ) 0x1a; /* R26 - must be saved across function calls. Callee-save. */
245 *pxTopOfStack = ( StackType_t ) 0x1b; /* R27 - must be saved across function calls. Callee-save. */
247 *pxTopOfStack = ( StackType_t ) 0x1c; /* R28 - must be saved across function calls. Callee-save. */
249 *pxTopOfStack = ( StackType_t ) 0x1d; /* R29 - must be saved across function calls. Callee-save. */
251 *pxTopOfStack = ( StackType_t ) 0x1e; /* R30 - must be saved across function calls. Callee-save. */
253 *pxTopOfStack = ( StackType_t ) 0x1f; /* R31 - must be saved across function calls. Callee-save. */
255 #else /* ifdef portPRE_LOAD_STACK_FOR_DEBUGGING */
257 #endif /* ifdef portPRE_LOAD_STACK_FOR_DEBUGGING */
259 /* Return a pointer to the top of the stack that has been generated so this
260 * can be stored in the task control block for the task. */
263 /*-----------------------------------------------------------*/
265 BaseType_t xPortStartScheduler( void )
267 extern void ( vPortStartFirstTask )( void );
268 extern UINTPTR _stack[];
270 /* Setup the hardware to generate the tick. Interrupts are disabled when
271 * this function is called.
273 * This port uses an application defined callback function to install the tick
274 * interrupt handler because the kernel will run on lots of different
275 * MicroBlaze and FPGA configurations - not all of which will have the same
276 * timer peripherals defined or available. An example definition of
277 * vApplicationSetupTimerInterrupt() is provided in the official demo
278 * application that accompanies this port. */
279 vApplicationSetupTimerInterrupt();
281 /* Reuse the stack from main() as the stack for the interrupts/exceptions. */
282 pulISRStack = ( UINTPTR * ) _stack;
284 /* Ensure there is enough space for the functions called from the interrupt
285 * service routines to write back into the stack frame of the caller. */
288 /* Restore the context of the first task that is going to run. From here
289 * on, the created tasks will be executing. */
290 vPortStartFirstTask();
292 /* Should not get here as the tasks are now running! */
295 /*-----------------------------------------------------------*/
297 void vPortEndScheduler( void )
299 /* Not implemented in ports where there is nothing to return to.
300 * Artificially force an assert. */
301 configASSERT( uxCriticalNesting == 1000UL );
303 /*-----------------------------------------------------------*/
306 * Manual context switch called by portYIELD or taskYIELD.
308 void vPortYield( void )
310 extern void VPortYieldASM( void );
312 /* Perform the context switch in a critical section to assure it is
313 * not interrupted by the tick ISR. It is not a problem to do this as
314 * each task maintains its own interrupt status. */
315 portENTER_CRITICAL();
317 /* Jump directly to the yield function to ensure there is no
318 * compiler generated prologue code. */
320 asm volatile ( "brealid r14, VPortYieldASM \n\t" \
321 "or r0, r0, r0 \n\t" );
323 asm volatile ( "bralid r14, VPortYieldASM \n\t" \
324 "or r0, r0, r0 \n\t" );
329 /*-----------------------------------------------------------*/
331 void vPortEnableInterrupt( uint8_t ucInterruptID )
335 /* An API function is provided to enable an interrupt in the interrupt
336 * controller because the interrupt controller instance variable is private
338 lReturn = prvEnsureInterruptControllerIsInitialised();
340 if( lReturn == pdPASS )
342 /* Critical section protects read/modify/writer operation inside
344 portENTER_CRITICAL();
346 XIntc_Enable( &xInterruptControllerInstance, ucInterruptID );
351 configASSERT( lReturn == pdPASS );
353 /*-----------------------------------------------------------*/
355 void vPortDisableInterrupt( uint8_t ucInterruptID )
359 /* An API function is provided to disable an interrupt in the interrupt
360 * controller because the interrupt controller instance variable is private
362 lReturn = prvEnsureInterruptControllerIsInitialised();
364 if( lReturn == pdPASS )
366 XIntc_Disable( &xInterruptControllerInstance, ucInterruptID );
369 configASSERT( lReturn == pdPASS );
371 /*-----------------------------------------------------------*/
373 BaseType_t xPortInstallInterruptHandler( uint8_t ucInterruptID,
374 XInterruptHandler pxHandler,
375 void * pvCallBackRef )
379 /* An API function is provided to install an interrupt handler because the
380 * interrupt controller instance variable is private to this file. */
382 lReturn = prvEnsureInterruptControllerIsInitialised();
384 if( lReturn == pdPASS )
386 lReturn = XIntc_Connect( &xInterruptControllerInstance, ucInterruptID, pxHandler, pvCallBackRef );
389 if( lReturn == XST_SUCCESS )
394 configASSERT( lReturn == pdPASS );
398 /*-----------------------------------------------------------*/
400 void vPortRemoveInterruptHandler( uint8_t ucInterruptID )
404 /* An API function is provided to remove an interrupt handler because the
405 * interrupt controller instance variable is private to this file. */
407 lReturn = prvEnsureInterruptControllerIsInitialised();
409 if( lReturn == pdPASS )
411 XIntc_Disconnect( &xInterruptControllerInstance, ucInterruptID );
414 configASSERT( lReturn == pdPASS );
416 /*-----------------------------------------------------------*/
418 static int32_t prvEnsureInterruptControllerIsInitialised( void )
420 static int32_t lInterruptControllerInitialised = pdFALSE;
423 /* Ensure the interrupt controller instance variable is initialised before
424 * it is used, and that the initialisation only happens once. */
425 if( lInterruptControllerInitialised != pdTRUE )
427 lReturn = prvInitialiseInterruptController();
429 if( lReturn == pdPASS )
431 lInterruptControllerInitialised = pdTRUE;
441 /*-----------------------------------------------------------*/
444 * Handler for the timer interrupt. This is the handler that the application
445 * defined callback function vApplicationSetupTimerInterrupt() should install.
447 void vPortTickISR( void * pvUnused )
449 extern void vApplicationClearTimerInterrupt( void );
451 /* Ensure the unused parameter does not generate a compiler warning. */
454 /* The Xilinx implementation of generating run time task stats uses the same timer used for generating
455 * FreeRTOS ticks. In case user decides to generate run time stats the tick handler is called more
456 * frequently (10 times faster). The timer ick handler uses logic to handle the same. It handles
457 * the FreeRTOS tick once per 10 interrupts.
458 * For handling generation of run time stats, it increments a pre-defined counter every time the
459 * interrupt handler executes. */
460 #if (configGENERATE_RUN_TIME_STATS == 1)
461 ulHighFrequencyTimerTicks++;
462 if (!(ulHighFrequencyTimerTicks % 10))
465 /* This port uses an application defined callback function to clear the tick
466 * interrupt because the kernel will run on lots of different MicroBlaze and
467 * FPGA configurations - not all of which will have the same timer peripherals
468 * defined or available. An example definition of
469 * vApplicationClearTimerInterrupt() is provided in the official demo
470 * application that accompanies this port. */
471 vApplicationClearTimerInterrupt();
473 /* Increment the RTOS tick - this might cause a task to unblock. */
474 if( xTaskIncrementTick() != pdFALSE )
476 /* Force vTaskSwitchContext() to be called as the interrupt exits. */
477 ulTaskSwitchRequested = 1;
481 /*-----------------------------------------------------------*/
483 static int32_t prvInitialiseInterruptController( void )
487 lStatus = XIntc_Initialize( &xInterruptControllerInstance, configINTERRUPT_CONTROLLER_TO_USE );
489 if( lStatus == XST_SUCCESS )
491 /* Initialise the exception table. */
494 /* Service all pending interrupts each time the handler is entered. */
495 XIntc_SetIntrSvcOption( xInterruptControllerInstance.BaseAddress, XIN_SVC_ALL_ISRS_OPTION );
497 /* Install exception handlers if the MicroBlaze is configured to handle
498 * exceptions, and the application defined constant
499 * configINSTALL_EXCEPTION_HANDLERS is set to 1. */
500 #if ( MICROBLAZE_EXCEPTIONS_ENABLED == 1 ) && ( configINSTALL_EXCEPTION_HANDLERS == 1 )
502 vPortExceptionsInstallHandlers();
504 #endif /* MICROBLAZE_EXCEPTIONS_ENABLED */
506 /* Start the interrupt controller. Interrupts are enabled when the
507 * scheduler starts. */
508 lStatus = XIntc_Start( &xInterruptControllerInstance, XIN_REAL_MODE );
510 if( lStatus == XST_SUCCESS )
520 configASSERT( lStatus == pdPASS );
525 #if( configGENERATE_RUN_TIME_STATS == 1 )
527 * For Xilinx implementation this is a dummy function that does a redundant operation
528 * of zeroing out the global counter.
529 * It is called by FreeRTOS kernel.
531 void xCONFIGURE_TIMER_FOR_RUN_TIME_STATS (void)
533 ulHighFrequencyTimerTicks = 0;
536 * For Xilinx implementation this function returns the global counter used for
537 * run time task stats calculation.
538 * It is called by FreeRTOS kernel task handling logic.
540 uint32_t xGET_RUN_TIME_COUNTER_VALUE (void)
542 return ulHighFrequencyTimerTicks;
545 /*-----------------------------------------------------------*/