2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
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>
46 /* Tasks are started with a critical section nesting of 0 - however, prior to
47 * the scheduler being commenced interrupts should not be enabled, so the critical
48 * nesting variable is initialised to a non-zero value. */
49 #define portINITIAL_NESTING_VALUE ( 0xff )
51 /* The bit within the MSR register that enabled/disables interrupts and
52 * exceptions respectively. */
53 #define portMSR_IE ( 0x02U )
54 #define portMSR_EE ( 0x100U )
56 /* If the floating point unit is included in the MicroBlaze build, then the
57 * FSR register is saved as part of the task context. portINITIAL_FSR is the value
58 * given to the FSR register when the initial context is set up for a task being
60 #define portINITIAL_FSR ( 0U )
61 /*-----------------------------------------------------------*/
64 * Initialise the interrupt controller instance.
66 static int32_t prvInitialiseInterruptController( void );
68 /* Ensure the interrupt controller instance variable is initialised before it is
69 * used, and that the initialisation only happens once.
71 static int32_t prvEnsureInterruptControllerIsInitialised( void );
73 /*-----------------------------------------------------------*/
75 /* Counts the nesting depth of calls to portENTER_CRITICAL(). Each task
76 * maintains its own count, so this variable is saved as part of the task
78 volatile UBaseType_t uxCriticalNesting = portINITIAL_NESTING_VALUE;
80 /* This port uses a separate stack for interrupts. This prevents the stack of
81 * every task needing to be large enough to hold an entire interrupt stack on top
82 * of the task stack. */
83 uint32_t * pulISRStack;
85 /* If an interrupt requests a context switch, then ulTaskSwitchRequested will
86 * get set to 1. ulTaskSwitchRequested is inspected just before the main interrupt
87 * handler exits. If, at that time, ulTaskSwitchRequested is set to 1, the kernel
88 * will call vTaskSwitchContext() to ensure the task that runs immediately after
89 * the interrupt exists is the highest priority task that is able to run. This is
90 * an unusual mechanism, but is used for this port because a single interrupt can
91 * cause the servicing of multiple peripherals - and it is inefficient to call
92 * vTaskSwitchContext() multiple times as each peripheral is serviced. */
93 volatile uint32_t ulTaskSwitchRequested = 0UL;
95 /* The instance of the interrupt controller used by this port. This is required
96 * by the Xilinx library API functions. */
97 static XIntc xInterruptControllerInstance;
99 /*-----------------------------------------------------------*/
102 * Initialise the stack of a task to look exactly as if a call to
103 * portSAVE_CONTEXT had been made.
105 * See the portable.h header file.
107 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
108 TaskFunction_t pxCode,
109 void * pvParameters )
111 extern void * _SDA2_BASE_;
112 extern void * _SDA_BASE_;
113 const uint32_t ulR2 = ( uint32_t ) &_SDA2_BASE_;
114 const uint32_t ulR13 = ( uint32_t ) &_SDA_BASE_;
116 /* Place a few bytes of known values on the bottom of the stack.
117 * This is essential for the Microblaze port and these lines must
119 *pxTopOfStack = ( StackType_t ) 0x00000000;
121 *pxTopOfStack = ( StackType_t ) 0x00000000;
123 *pxTopOfStack = ( StackType_t ) 0x00000000;
126 #if ( XPAR_MICROBLAZE_USE_FPU != 0 )
127 /* The FSR value placed in the initial task context is just 0. */
128 *pxTopOfStack = portINITIAL_FSR;
132 /* The MSR value placed in the initial task context should have interrupts
133 * disabled. Each task will enable interrupts automatically when it enters
134 * the running state for the first time. */
135 *pxTopOfStack = mfmsr() & ~portMSR_IE;
137 #if ( MICROBLAZE_EXCEPTIONS_ENABLED == 1 )
139 /* Ensure exceptions are enabled for the task. */
140 *pxTopOfStack |= portMSR_EE;
146 /* First stack an initial value for the critical section nesting. This
147 * is initialised to zero. */
148 *pxTopOfStack = ( StackType_t ) 0x00;
150 /* R0 is always zero. */
153 /* Place an initial value for all the general purpose registers. */
155 *pxTopOfStack = ( StackType_t ) ulR2; /* R2 - read only small data area. */
157 *pxTopOfStack = ( StackType_t ) 0x03; /* R3 - return values and temporaries. */
159 *pxTopOfStack = ( StackType_t ) 0x04; /* R4 - return values and temporaries. */
161 *pxTopOfStack = ( StackType_t ) pvParameters; /* R5 contains the function call parameters. */
163 #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING
165 *pxTopOfStack = ( StackType_t ) 0x06; /* R6 - other parameters and temporaries. Used as the return address from vPortTaskEntryPoint. */
167 *pxTopOfStack = ( StackType_t ) 0x07; /* R7 - other parameters and temporaries. */
169 *pxTopOfStack = ( StackType_t ) 0x08; /* R8 - other parameters and temporaries. */
171 *pxTopOfStack = ( StackType_t ) 0x09; /* R9 - other parameters and temporaries. */
173 *pxTopOfStack = ( StackType_t ) 0x0a; /* R10 - other parameters and temporaries. */
175 *pxTopOfStack = ( StackType_t ) 0x0b; /* R11 - temporaries. */
177 *pxTopOfStack = ( StackType_t ) 0x0c; /* R12 - temporaries. */
179 #else /* ifdef portPRE_LOAD_STACK_FOR_DEBUGGING */
181 #endif /* ifdef portPRE_LOAD_STACK_FOR_DEBUGGING */
183 *pxTopOfStack = ( StackType_t ) ulR13; /* R13 - read/write small data area. */
185 *pxTopOfStack = ( StackType_t ) pxCode; /* R14 - return address for interrupt. */
187 *pxTopOfStack = ( StackType_t ) NULL; /* R15 - return address for subroutine. */
189 #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING
191 *pxTopOfStack = ( StackType_t ) 0x10; /* R16 - return address for trap (debugger). */
193 *pxTopOfStack = ( StackType_t ) 0x11; /* R17 - return address for exceptions, if configured. */
195 *pxTopOfStack = ( StackType_t ) 0x12; /* R18 - reserved for assembler and compiler temporaries. */
201 *pxTopOfStack = ( StackType_t ) 0x00; /* R19 - must be saved across function calls. Callee-save. Seems to be interpreted as the frame pointer. */
203 #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING
205 *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. */
207 *pxTopOfStack = ( StackType_t ) 0x15; /* R21 - must be saved across function calls. Callee-save. */
209 *pxTopOfStack = ( StackType_t ) 0x16; /* R22 - must be saved across function calls. Callee-save. */
211 *pxTopOfStack = ( StackType_t ) 0x17; /* R23 - must be saved across function calls. Callee-save. */
213 *pxTopOfStack = ( StackType_t ) 0x18; /* R24 - must be saved across function calls. Callee-save. */
215 *pxTopOfStack = ( StackType_t ) 0x19; /* R25 - must be saved across function calls. Callee-save. */
217 *pxTopOfStack = ( StackType_t ) 0x1a; /* R26 - must be saved across function calls. Callee-save. */
219 *pxTopOfStack = ( StackType_t ) 0x1b; /* R27 - must be saved across function calls. Callee-save. */
221 *pxTopOfStack = ( StackType_t ) 0x1c; /* R28 - must be saved across function calls. Callee-save. */
223 *pxTopOfStack = ( StackType_t ) 0x1d; /* R29 - must be saved across function calls. Callee-save. */
225 *pxTopOfStack = ( StackType_t ) 0x1e; /* R30 - must be saved across function calls. Callee-save. */
227 *pxTopOfStack = ( StackType_t ) 0x1f; /* R31 - must be saved across function calls. Callee-save. */
229 #else /* ifdef portPRE_LOAD_STACK_FOR_DEBUGGING */
231 #endif /* ifdef portPRE_LOAD_STACK_FOR_DEBUGGING */
233 /* Return a pointer to the top of the stack that has been generated so this
234 * can be stored in the task control block for the task. */
237 /*-----------------------------------------------------------*/
239 BaseType_t xPortStartScheduler( void )
241 extern void( vPortStartFirstTask )( void );
242 extern uint32_t _stack[];
244 /* Setup the hardware to generate the tick. Interrupts are disabled when
245 * this function is called.
247 * This port uses an application defined callback function to install the tick
248 * interrupt handler because the kernel will run on lots of different
249 * MicroBlaze and FPGA configurations - not all of which will have the same
250 * timer peripherals defined or available. An example definition of
251 * vApplicationSetupTimerInterrupt() is provided in the official demo
252 * application that accompanies this port. */
253 vApplicationSetupTimerInterrupt();
255 /* Reuse the stack from main() as the stack for the interrupts/exceptions. */
256 pulISRStack = ( uint32_t * ) _stack;
258 /* Ensure there is enough space for the functions called from the interrupt
259 * service routines to write back into the stack frame of the caller. */
262 /* Restore the context of the first task that is going to run. From here
263 * on, the created tasks will be executing. */
264 vPortStartFirstTask();
266 /* Should not get here as the tasks are now running! */
269 /*-----------------------------------------------------------*/
271 void vPortEndScheduler( void )
273 /* Not implemented in ports where there is nothing to return to.
274 * Artificially force an assert. */
275 configASSERT( uxCriticalNesting == 1000UL );
277 /*-----------------------------------------------------------*/
280 * Manual context switch called by portYIELD or taskYIELD.
282 void vPortYield( void )
284 extern void VPortYieldASM( void );
286 /* Perform the context switch in a critical section to assure it is
287 * not interrupted by the tick ISR. It is not a problem to do this as
288 * each task maintains its own interrupt status. */
289 portENTER_CRITICAL();
291 /* Jump directly to the yield function to ensure there is no
292 * compiler generated prologue code. */
293 asm volatile ( "bralid r14, VPortYieldASM \n\t" \
294 "or r0, r0, r0 \n\t" );
298 /*-----------------------------------------------------------*/
300 void vPortEnableInterrupt( uint8_t ucInterruptID )
304 /* An API function is provided to enable an interrupt in the interrupt
305 * controller because the interrupt controller instance variable is private
307 lReturn = prvEnsureInterruptControllerIsInitialised();
309 if( lReturn == pdPASS )
311 XIntc_Enable( &xInterruptControllerInstance, ucInterruptID );
314 configASSERT( lReturn );
316 /*-----------------------------------------------------------*/
318 void vPortDisableInterrupt( uint8_t ucInterruptID )
322 /* An API function is provided to disable an interrupt in the interrupt
323 * controller because the interrupt controller instance variable is private
325 lReturn = prvEnsureInterruptControllerIsInitialised();
327 if( lReturn == pdPASS )
329 XIntc_Disable( &xInterruptControllerInstance, ucInterruptID );
332 configASSERT( lReturn );
334 /*-----------------------------------------------------------*/
336 BaseType_t xPortInstallInterruptHandler( uint8_t ucInterruptID,
337 XInterruptHandler pxHandler,
338 void * pvCallBackRef )
342 /* An API function is provided to install an interrupt handler because the
343 * interrupt controller instance variable is private to this file. */
345 lReturn = prvEnsureInterruptControllerIsInitialised();
347 if( lReturn == pdPASS )
349 lReturn = XIntc_Connect( &xInterruptControllerInstance, ucInterruptID, pxHandler, pvCallBackRef );
352 if( lReturn == XST_SUCCESS )
357 configASSERT( lReturn == pdPASS );
361 /*-----------------------------------------------------------*/
363 static int32_t prvEnsureInterruptControllerIsInitialised( void )
365 static int32_t lInterruptControllerInitialised = pdFALSE;
368 /* Ensure the interrupt controller instance variable is initialised before
369 * it is used, and that the initialisation only happens once. */
370 if( lInterruptControllerInitialised != pdTRUE )
372 lReturn = prvInitialiseInterruptController();
374 if( lReturn == pdPASS )
376 lInterruptControllerInitialised = pdTRUE;
386 /*-----------------------------------------------------------*/
389 * Handler for the timer interrupt. This is the handler that the application
390 * defined callback function vApplicationSetupTimerInterrupt() should install.
392 void vPortTickISR( void * pvUnused )
394 extern void vApplicationClearTimerInterrupt( void );
396 /* Ensure the unused parameter does not generate a compiler warning. */
399 /* This port uses an application defined callback function to clear the tick
400 * interrupt because the kernel will run on lots of different MicroBlaze and
401 * FPGA configurations - not all of which will have the same timer peripherals
402 * defined or available. An example definition of
403 * vApplicationClearTimerInterrupt() is provided in the official demo
404 * application that accompanies this port. */
405 vApplicationClearTimerInterrupt();
407 /* Increment the RTOS tick - this might cause a task to unblock. */
408 if( xTaskIncrementTick() != pdFALSE )
410 /* Force vTaskSwitchContext() to be called as the interrupt exits. */
411 ulTaskSwitchRequested = 1;
414 /*-----------------------------------------------------------*/
416 static int32_t prvInitialiseInterruptController( void )
420 lStatus = XIntc_Initialize( &xInterruptControllerInstance, configINTERRUPT_CONTROLLER_TO_USE );
422 if( lStatus == XST_SUCCESS )
424 /* Initialise the exception table. */
427 /* Service all pending interrupts each time the handler is entered. */
428 XIntc_SetIntrSvcOption( xInterruptControllerInstance.BaseAddress, XIN_SVC_ALL_ISRS_OPTION );
430 /* Install exception handlers if the MicroBlaze is configured to handle
431 * exceptions, and the application defined constant
432 * configINSTALL_EXCEPTION_HANDLERS is set to 1. */
433 #if ( MICROBLAZE_EXCEPTIONS_ENABLED == 1 ) && ( configINSTALL_EXCEPTION_HANDLERS == 1 )
435 vPortExceptionsInstallHandlers();
437 #endif /* MICROBLAZE_EXCEPTIONS_ENABLED */
439 /* Start the interrupt controller. Interrupts are enabled when the
440 * scheduler starts. */
441 lStatus = XIntc_Start( &xInterruptControllerInstance, XIN_REAL_MODE );
443 if( lStatus == XST_SUCCESS )
453 configASSERT( lStatus == pdPASS );
457 /*-----------------------------------------------------------*/