2 * FreeRTOS Kernel V10.4.4
\r
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * SPDX-License-Identifier: MIT
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
8 * this software and associated documentation files (the "Software"), to deal in
\r
9 * the Software without restriction, including without limitation the rights to
\r
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
11 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
12 * subject to the following conditions:
\r
14 * The above copyright notice and this permission notice shall be included in all
\r
15 * copies or substantial portions of the Software.
\r
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
24 * https://www.FreeRTOS.org
\r
25 * https://github.com/FreeRTOS
\r
29 /*-----------------------------------------------------------
\r
30 * Implementation of functions defined in portable.h for the MicroBlaze port.
\r
31 *----------------------------------------------------------*/
\r
34 /* Scheduler includes. */
\r
35 #include "FreeRTOS.h"
\r
38 /* Standard includes. */
\r
41 /* Hardware includes. */
\r
42 #include <xintc_i.h>
\r
43 #include <xil_exception.h>
\r
44 #include <microblaze_exceptions_g.h>
\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
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
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
60 #define portINITIAL_FSR ( 0U )
\r
61 /*-----------------------------------------------------------*/
\r
64 * Initialise the interrupt controller instance.
\r
66 static int32_t prvInitialiseInterruptController( void );
\r
68 /* Ensure the interrupt controller instance variable is initialised before it is
\r
69 * used, and that the initialisation only happens once.
\r
71 static int32_t prvEnsureInterruptControllerIsInitialised( void );
\r
73 /*-----------------------------------------------------------*/
\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
78 volatile UBaseType_t uxCriticalNesting = portINITIAL_NESTING_VALUE;
\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
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
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
99 /*-----------------------------------------------------------*/
\r
102 * Initialise the stack of a task to look exactly as if a call to
\r
103 * portSAVE_CONTEXT had been made.
\r
105 * See the portable.h header file.
\r
107 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\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
113 /* Place a few bytes of known values on the bottom of the stack.
\r
114 This is essential for the Microblaze port and these lines must
\r
116 *pxTopOfStack = ( StackType_t ) 0x00000000;
\r
118 *pxTopOfStack = ( StackType_t ) 0x00000000;
\r
120 *pxTopOfStack = ( StackType_t ) 0x00000000;
\r
123 #if( XPAR_MICROBLAZE_USE_FPU != 0 )
\r
124 /* The FSR value placed in the initial task context is just 0. */
\r
125 *pxTopOfStack = portINITIAL_FSR;
\r
129 /* The MSR value placed in the initial task context should have interrupts
\r
130 disabled. Each task will enable interrupts automatically when it enters
\r
131 the running state for the first time. */
\r
132 *pxTopOfStack = mfmsr() & ~portMSR_IE;
\r
134 #if( MICROBLAZE_EXCEPTIONS_ENABLED == 1 )
\r
136 /* Ensure exceptions are enabled for the task. */
\r
137 *pxTopOfStack |= portMSR_EE;
\r
143 /* First stack an initial value for the critical section nesting. This
\r
144 is initialised to zero. */
\r
145 *pxTopOfStack = ( StackType_t ) 0x00;
\r
147 /* R0 is always zero. */
\r
148 /* R1 is the SP. */
\r
150 /* Place an initial value for all the general purpose registers. */
\r
152 *pxTopOfStack = ( StackType_t ) ulR2; /* R2 - read only small data area. */
\r
154 *pxTopOfStack = ( StackType_t ) 0x03; /* R3 - return values and temporaries. */
\r
156 *pxTopOfStack = ( StackType_t ) 0x04; /* R4 - return values and temporaries. */
\r
158 *pxTopOfStack = ( StackType_t ) pvParameters;/* R5 contains the function call parameters. */
\r
160 #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING
\r
162 *pxTopOfStack = ( StackType_t ) 0x06; /* R6 - other parameters and temporaries. Used as the return address from vPortTaskEntryPoint. */
\r
164 *pxTopOfStack = ( StackType_t ) 0x07; /* R7 - other parameters and temporaries. */
\r
166 *pxTopOfStack = ( StackType_t ) 0x08; /* R8 - other parameters and temporaries. */
\r
168 *pxTopOfStack = ( StackType_t ) 0x09; /* R9 - other parameters and temporaries. */
\r
170 *pxTopOfStack = ( StackType_t ) 0x0a; /* R10 - other parameters and temporaries. */
\r
172 *pxTopOfStack = ( StackType_t ) 0x0b; /* R11 - temporaries. */
\r
174 *pxTopOfStack = ( StackType_t ) 0x0c; /* R12 - temporaries. */
\r
180 *pxTopOfStack = ( StackType_t ) ulR13; /* R13 - read/write small data area. */
\r
182 *pxTopOfStack = ( StackType_t ) pxCode; /* R14 - return address for interrupt. */
\r
184 *pxTopOfStack = ( StackType_t ) NULL; /* R15 - return address for subroutine. */
\r
186 #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING
\r
188 *pxTopOfStack = ( StackType_t ) 0x10; /* R16 - return address for trap (debugger). */
\r
190 *pxTopOfStack = ( StackType_t ) 0x11; /* R17 - return address for exceptions, if configured. */
\r
192 *pxTopOfStack = ( StackType_t ) 0x12; /* R18 - reserved for assembler and compiler temporaries. */
\r
198 *pxTopOfStack = ( StackType_t ) 0x00; /* R19 - must be saved across function calls. Callee-save. Seems to be interpreted as the frame pointer. */
\r
200 #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING
\r
202 *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 = ( StackType_t ) 0x15; /* R21 - must be saved across function calls. Callee-save. */
\r
206 *pxTopOfStack = ( StackType_t ) 0x16; /* R22 - must be saved across function calls. Callee-save. */
\r
208 *pxTopOfStack = ( StackType_t ) 0x17; /* R23 - must be saved across function calls. Callee-save. */
\r
210 *pxTopOfStack = ( StackType_t ) 0x18; /* R24 - must be saved across function calls. Callee-save. */
\r
212 *pxTopOfStack = ( StackType_t ) 0x19; /* R25 - must be saved across function calls. Callee-save. */
\r
214 *pxTopOfStack = ( StackType_t ) 0x1a; /* R26 - must be saved across function calls. Callee-save. */
\r
216 *pxTopOfStack = ( StackType_t ) 0x1b; /* R27 - must be saved across function calls. Callee-save. */
\r
218 *pxTopOfStack = ( StackType_t ) 0x1c; /* R28 - must be saved across function calls. Callee-save. */
\r
220 *pxTopOfStack = ( StackType_t ) 0x1d; /* R29 - must be saved across function calls. Callee-save. */
\r
222 *pxTopOfStack = ( StackType_t ) 0x1e; /* R30 - must be saved across function calls. Callee-save. */
\r
224 *pxTopOfStack = ( StackType_t ) 0x1f; /* R31 - must be saved across function calls. Callee-save. */
\r
227 pxTopOfStack -= 13;
\r
230 /* Return a pointer to the top of the stack that has been generated so this
\r
231 can be stored in the task control block for the task. */
\r
232 return pxTopOfStack;
\r
234 /*-----------------------------------------------------------*/
\r
236 BaseType_t xPortStartScheduler( void )
\r
238 extern void ( vPortStartFirstTask )( void );
\r
239 extern uint32_t _stack[];
\r
241 /* Setup the hardware to generate the tick. Interrupts are disabled when
\r
242 this function is called.
\r
244 This port uses an application defined callback function to install the tick
\r
245 interrupt handler because the kernel will run on lots of different
\r
246 MicroBlaze and FPGA configurations - not all of which will have the same
\r
247 timer peripherals defined or available. An example definition of
\r
248 vApplicationSetupTimerInterrupt() is provided in the official demo
\r
249 application that accompanies this port. */
\r
250 vApplicationSetupTimerInterrupt();
\r
252 /* Reuse the stack from main() as the stack for the interrupts/exceptions. */
\r
253 pulISRStack = ( uint32_t * ) _stack;
\r
255 /* Ensure there is enough space for the functions called from the interrupt
\r
256 service routines to write back into the stack frame of the caller. */
\r
259 /* Restore the context of the first task that is going to run. From here
\r
260 on, the created tasks will be executing. */
\r
261 vPortStartFirstTask();
\r
263 /* Should not get here as the tasks are now running! */
\r
266 /*-----------------------------------------------------------*/
\r
268 void vPortEndScheduler( void )
\r
270 /* Not implemented in ports where there is nothing to return to.
\r
271 Artificially force an assert. */
\r
272 configASSERT( uxCriticalNesting == 1000UL );
\r
274 /*-----------------------------------------------------------*/
\r
277 * Manual context switch called by portYIELD or taskYIELD.
\r
279 void vPortYield( void )
\r
281 extern void VPortYieldASM( void );
\r
283 /* Perform the context switch in a critical section to assure it is
\r
284 not interrupted by the tick ISR. It is not a problem to do this as
\r
285 each task maintains its own interrupt status. */
\r
286 portENTER_CRITICAL();
\r
288 /* Jump directly to the yield function to ensure there is no
\r
289 compiler generated prologue code. */
\r
290 asm volatile ( "bralid r14, VPortYieldASM \n\t" \
\r
291 "or r0, r0, r0 \n\t" );
\r
293 portEXIT_CRITICAL();
\r
295 /*-----------------------------------------------------------*/
\r
297 void vPortEnableInterrupt( uint8_t ucInterruptID )
\r
301 /* An API function is provided to enable an interrupt in the interrupt
\r
302 controller because the interrupt controller instance variable is private
\r
304 lReturn = prvEnsureInterruptControllerIsInitialised();
\r
305 if( lReturn == pdPASS )
\r
307 XIntc_Enable( &xInterruptControllerInstance, ucInterruptID );
\r
310 configASSERT( lReturn );
\r
312 /*-----------------------------------------------------------*/
\r
314 void vPortDisableInterrupt( uint8_t ucInterruptID )
\r
318 /* An API function is provided to disable an interrupt in the interrupt
\r
319 controller because the interrupt controller instance variable is private
\r
321 lReturn = prvEnsureInterruptControllerIsInitialised();
\r
323 if( lReturn == pdPASS )
\r
325 XIntc_Disable( &xInterruptControllerInstance, ucInterruptID );
\r
328 configASSERT( lReturn );
\r
330 /*-----------------------------------------------------------*/
\r
332 BaseType_t xPortInstallInterruptHandler( uint8_t ucInterruptID, XInterruptHandler pxHandler, void *pvCallBackRef )
\r
336 /* An API function is provided to install an interrupt handler because the
\r
337 interrupt controller instance variable is private to this file. */
\r
339 lReturn = prvEnsureInterruptControllerIsInitialised();
\r
341 if( lReturn == pdPASS )
\r
343 lReturn = XIntc_Connect( &xInterruptControllerInstance, ucInterruptID, pxHandler, pvCallBackRef );
\r
346 if( lReturn == XST_SUCCESS )
\r
351 configASSERT( lReturn == pdPASS );
\r
355 /*-----------------------------------------------------------*/
\r
357 static int32_t prvEnsureInterruptControllerIsInitialised( void )
\r
359 static int32_t lInterruptControllerInitialised = pdFALSE;
\r
362 /* Ensure the interrupt controller instance variable is initialised before
\r
363 it is used, and that the initialisation only happens once. */
\r
364 if( lInterruptControllerInitialised != pdTRUE )
\r
366 lReturn = prvInitialiseInterruptController();
\r
368 if( lReturn == pdPASS )
\r
370 lInterruptControllerInitialised = pdTRUE;
\r
380 /*-----------------------------------------------------------*/
\r
383 * Handler for the timer interrupt. This is the handler that the application
\r
384 * defined callback function vApplicationSetupTimerInterrupt() should install.
\r
386 void vPortTickISR( void *pvUnused )
\r
388 extern void vApplicationClearTimerInterrupt( void );
\r
390 /* Ensure the unused parameter does not generate a compiler warning. */
\r
393 /* This port uses an application defined callback function to clear the tick
\r
394 interrupt because the kernel will run on lots of different MicroBlaze and
\r
395 FPGA configurations - not all of which will have the same timer peripherals
\r
396 defined or available. An example definition of
\r
397 vApplicationClearTimerInterrupt() is provided in the official demo
\r
398 application that accompanies this port. */
\r
399 vApplicationClearTimerInterrupt();
\r
401 /* Increment the RTOS tick - this might cause a task to unblock. */
\r
402 if( xTaskIncrementTick() != pdFALSE )
\r
404 /* Force vTaskSwitchContext() to be called as the interrupt exits. */
\r
405 ulTaskSwitchRequested = 1;
\r
408 /*-----------------------------------------------------------*/
\r
410 static int32_t prvInitialiseInterruptController( void )
\r
414 lStatus = XIntc_Initialize( &xInterruptControllerInstance, configINTERRUPT_CONTROLLER_TO_USE );
\r
416 if( lStatus == XST_SUCCESS )
\r
418 /* Initialise the exception table. */
\r
419 Xil_ExceptionInit();
\r
421 /* Service all pending interrupts each time the handler is entered. */
\r
422 XIntc_SetIntrSvcOption( xInterruptControllerInstance.BaseAddress, XIN_SVC_ALL_ISRS_OPTION );
\r
424 /* Install exception handlers if the MicroBlaze is configured to handle
\r
425 exceptions, and the application defined constant
\r
426 configINSTALL_EXCEPTION_HANDLERS is set to 1. */
\r
427 #if ( MICROBLAZE_EXCEPTIONS_ENABLED == 1 ) && ( configINSTALL_EXCEPTION_HANDLERS == 1 )
\r
429 vPortExceptionsInstallHandlers();
\r
431 #endif /* MICROBLAZE_EXCEPTIONS_ENABLED */
\r
433 /* Start the interrupt controller. Interrupts are enabled when the
\r
434 scheduler starts. */
\r
435 lStatus = XIntc_Start( &xInterruptControllerInstance, XIN_REAL_MODE );
\r
437 if( lStatus == XST_SUCCESS )
\r
447 configASSERT( lStatus == pdPASS );
\r
451 /*-----------------------------------------------------------*/
\r