2 FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
67 /*-----------------------------------------------------------
\r
68 * Implementation of functions defined in portable.h for the MicroBlaze port.
\r
69 *----------------------------------------------------------*/
\r
72 /* Scheduler includes. */
\r
73 #include "FreeRTOS.h"
\r
76 /* Standard includes. */
\r
79 /* Hardware includes. */
\r
80 #include <xintc_i.h>
\r
81 #include <xil_exception.h>
\r
82 #include <microblaze_exceptions_g.h>
\r
84 /* Tasks are started with a critical section nesting of 0 - however, prior to
\r
85 the scheduler being commenced interrupts should not be enabled, so the critical
\r
86 nesting variable is initialised to a non-zero value. */
\r
87 #define portINITIAL_NESTING_VALUE ( 0xff )
\r
89 /* The bit within the MSR register that enabled/disables interrupts. */
\r
90 #define portMSR_IE ( 0x02U )
\r
92 /* If the floating point unit is included in the MicroBlaze build, then the
\r
93 FSR register is saved as part of the task context. portINITIAL_FSR is the value
\r
94 given to the FSR register when the initial context is set up for a task being
\r
96 #define portINITIAL_FSR ( 0U )
\r
97 /*-----------------------------------------------------------*/
\r
100 * Initialise the interrupt controller instance.
\r
102 static long prvInitialiseInterruptController( void );
\r
104 /* Ensure the interrupt controller instance variable is initialised before it is
\r
105 * used, and that the initialisation only happens once.
\r
107 static long prvEnsureInterruptControllerIsInitialised( void );
\r
109 /*-----------------------------------------------------------*/
\r
111 /* Counts the nesting depth of calls to portENTER_CRITICAL(). Each task
\r
112 maintains its own count, so this variable is saved as part of the task
\r
114 volatile unsigned portBASE_TYPE uxCriticalNesting = portINITIAL_NESTING_VALUE;
\r
116 /* This port uses a separate stack for interrupts. This prevents the stack of
\r
117 every task needing to be large enough to hold an entire interrupt stack on top
\r
118 of the task stack. */
\r
119 unsigned long *pulISRStack;
\r
121 /* If an interrupt requests a context switch, then ulTaskSwitchRequested will
\r
122 get set to 1. ulTaskSwitchRequested is inspected just before the main interrupt
\r
123 handler exits. If, at that time, ulTaskSwitchRequested is set to 1, the kernel
\r
124 will call vTaskSwitchContext() to ensure the task that runs immediately after
\r
125 the interrupt exists is the highest priority task that is able to run. This is
\r
126 an unusual mechanism, but is used for this port because a single interrupt can
\r
127 cause the servicing of multiple peripherals - and it is inefficient to call
\r
128 vTaskSwitchContext() multiple times as each peripheral is serviced. */
\r
129 volatile unsigned long ulTaskSwitchRequested = 0UL;
\r
131 /* The instance of the interrupt controller used by this port. This is required
\r
132 by the Xilinx library API functions. */
\r
133 static XIntc xInterruptControllerInstance;
\r
135 /*-----------------------------------------------------------*/
\r
138 * Initialise the stack of a task to look exactly as if a call to
\r
139 * portSAVE_CONTEXT had been made.
\r
141 * See the portable.h header file.
\r
143 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
145 extern void *_SDA2_BASE_, *_SDA_BASE_;
\r
146 const unsigned long ulR2 = ( unsigned long ) &_SDA2_BASE_;
\r
147 const unsigned long ulR13 = ( unsigned long ) &_SDA_BASE_;
\r
149 /* Place a few bytes of known values on the bottom of the stack.
\r
150 This is essential for the Microblaze port and these lines must
\r
152 *pxTopOfStack = ( portSTACK_TYPE ) 0x00000000;
\r
154 *pxTopOfStack = ( portSTACK_TYPE ) 0x00000000;
\r
156 *pxTopOfStack = ( portSTACK_TYPE ) 0x00000000;
\r
159 #if XPAR_MICROBLAZE_0_USE_FPU == 1
\r
160 /* The FSR value placed in the initial task context is just 0. */
\r
161 *pxTopOfStack = portINITIAL_FSR;
\r
165 /* The MSR value placed in the initial task context should have interrupts
\r
166 disabled. Each task will enable interrupts automatically when it enters
\r
167 the running state for the first time. */
\r
168 *pxTopOfStack = mfmsr() & ~portMSR_IE;
\r
171 /* First stack an initial value for the critical section nesting. This
\r
172 is initialised to zero. */
\r
173 *pxTopOfStack = ( portSTACK_TYPE ) 0x00;
\r
175 /* R0 is always zero. */
\r
176 /* R1 is the SP. */
\r
178 /* Place an initial value for all the general purpose registers. */
\r
180 *pxTopOfStack = ( portSTACK_TYPE ) ulR2; /* R2 - read only small data area. */
\r
182 *pxTopOfStack = ( portSTACK_TYPE ) 0x03; /* R3 - return values and temporaries. */
\r
184 *pxTopOfStack = ( portSTACK_TYPE ) 0x04; /* R4 - return values and temporaries. */
\r
186 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;/* R5 contains the function call parameters. */
\r
188 #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING
\r
190 *pxTopOfStack = ( portSTACK_TYPE ) 0x06; /* R6 - other parameters and temporaries. Used as the return address from vPortTaskEntryPoint. */
\r
192 *pxTopOfStack = ( portSTACK_TYPE ) 0x07; /* R7 - other parameters and temporaries. */
\r
194 *pxTopOfStack = ( portSTACK_TYPE ) 0x08; /* R8 - other parameters and temporaries. */
\r
196 *pxTopOfStack = ( portSTACK_TYPE ) 0x09; /* R9 - other parameters and temporaries. */
\r
198 *pxTopOfStack = ( portSTACK_TYPE ) 0x0a; /* R10 - other parameters and temporaries. */
\r
200 *pxTopOfStack = ( portSTACK_TYPE ) 0x0b; /* R11 - temporaries. */
\r
202 *pxTopOfStack = ( portSTACK_TYPE ) 0x0c; /* R12 - temporaries. */
\r
208 *pxTopOfStack = ( portSTACK_TYPE ) ulR13; /* R13 - read/write small data area. */
\r
210 *pxTopOfStack = ( portSTACK_TYPE ) pxCode; /* R14 - return address for interrupt. */
\r
212 *pxTopOfStack = ( portSTACK_TYPE ) NULL; /* R15 - return address for subroutine. */
\r
214 #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING
\r
216 *pxTopOfStack = ( portSTACK_TYPE ) 0x10; /* R16 - return address for trap (debugger). */
\r
218 *pxTopOfStack = ( portSTACK_TYPE ) 0x11; /* R17 - return address for exceptions, if configured. */
\r
220 *pxTopOfStack = ( portSTACK_TYPE ) 0x12; /* R18 - reserved for assembler and compiler temporaries. */
\r
226 *pxTopOfStack = ( portSTACK_TYPE ) 0x00; /* R19 - must be saved across function calls. Callee-save. Seems to be interpreted as the frame pointer. */
\r
228 #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING
\r
230 *pxTopOfStack = ( portSTACK_TYPE ) 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
232 *pxTopOfStack = ( portSTACK_TYPE ) 0x15; /* R21 - must be saved across function calls. Callee-save. */
\r
234 *pxTopOfStack = ( portSTACK_TYPE ) 0x16; /* R22 - must be saved across function calls. Callee-save. */
\r
236 *pxTopOfStack = ( portSTACK_TYPE ) 0x17; /* R23 - must be saved across function calls. Callee-save. */
\r
238 *pxTopOfStack = ( portSTACK_TYPE ) 0x18; /* R24 - must be saved across function calls. Callee-save. */
\r
240 *pxTopOfStack = ( portSTACK_TYPE ) 0x19; /* R25 - must be saved across function calls. Callee-save. */
\r
242 *pxTopOfStack = ( portSTACK_TYPE ) 0x1a; /* R26 - must be saved across function calls. Callee-save. */
\r
244 *pxTopOfStack = ( portSTACK_TYPE ) 0x1b; /* R27 - must be saved across function calls. Callee-save. */
\r
246 *pxTopOfStack = ( portSTACK_TYPE ) 0x1c; /* R28 - must be saved across function calls. Callee-save. */
\r
248 *pxTopOfStack = ( portSTACK_TYPE ) 0x1d; /* R29 - must be saved across function calls. Callee-save. */
\r
250 *pxTopOfStack = ( portSTACK_TYPE ) 0x1e; /* R30 - must be saved across function calls. Callee-save. */
\r
252 *pxTopOfStack = ( portSTACK_TYPE ) 0x1f; /* R31 - must be saved across function calls. Callee-save. */
\r
255 pxTopOfStack -= 13;
\r
258 /* Return a pointer to the top of the stack that has been generated so this
\r
259 can be stored in the task control block for the task. */
\r
260 return pxTopOfStack;
\r
262 /*-----------------------------------------------------------*/
\r
264 portBASE_TYPE xPortStartScheduler( void )
\r
266 extern void ( vPortStartFirstTask )( void );
\r
267 extern unsigned long _stack[];
\r
269 /* Setup the hardware to generate the tick. Interrupts are disabled when
\r
270 this function is called.
\r
272 This port uses an application defined callback function to install the tick
\r
273 interrupt handler because the kernel will run on lots of different
\r
274 MicroBlaze and FPGA configurations - not all of which will have the same
\r
275 timer peripherals defined or available. An example definition of
\r
276 vApplicationSetupTimerInterrupt() is provided in the official demo
\r
277 application that accompanies this port. */
\r
278 vApplicationSetupTimerInterrupt();
\r
280 /* Reuse the stack from main() as the stack for the interrupts/exceptions. */
\r
281 pulISRStack = ( unsigned long * ) _stack;
\r
283 /* Ensure there is enough space for the functions called from the interrupt
\r
284 service routines to write back into the stack frame of the caller. */
\r
287 /* Restore the context of the first task that is going to run. From here
\r
288 on, the created tasks will be executing. */
\r
289 vPortStartFirstTask();
\r
291 /* Should not get here as the tasks are now running! */
\r
294 /*-----------------------------------------------------------*/
\r
296 void vPortEndScheduler( void )
\r
298 /* Not implemented. */
\r
300 /*-----------------------------------------------------------*/
\r
303 * Manual context switch called by portYIELD or taskYIELD.
\r
305 void vPortYield( void )
\r
307 extern void VPortYieldASM( void );
\r
309 /* Perform the context switch in a critical section to assure it is
\r
310 not interrupted by the tick ISR. It is not a problem to do this as
\r
311 each task maintains its own interrupt status. */
\r
312 portENTER_CRITICAL();
\r
314 /* Jump directly to the yield function to ensure there is no
\r
315 compiler generated prologue code. */
\r
316 asm volatile ( "bralid r14, VPortYieldASM \n\t" \
\r
317 "or r0, r0, r0 \n\t" );
\r
319 portEXIT_CRITICAL();
\r
321 /*-----------------------------------------------------------*/
\r
323 void vPortEnableInterrupt( unsigned char ucInterruptID )
\r
327 /* An API function is provided to enable an interrupt in the interrupt
\r
328 controller because the interrupt controller instance variable is private
\r
330 lReturn = prvEnsureInterruptControllerIsInitialised();
\r
331 if( lReturn == pdPASS )
\r
333 XIntc_Enable( &xInterruptControllerInstance, ucInterruptID );
\r
336 configASSERT( lReturn );
\r
338 /*-----------------------------------------------------------*/
\r
340 void vPortDisableInterrupt( unsigned char ucInterruptID )
\r
344 /* An API function is provided to disable an interrupt in the interrupt
\r
345 controller because the interrupt controller instance variable is private
\r
347 lReturn = prvEnsureInterruptControllerIsInitialised();
\r
349 if( lReturn == pdPASS )
\r
351 XIntc_Disable( &xInterruptControllerInstance, ucInterruptID );
\r
354 configASSERT( lReturn );
\r
356 /*-----------------------------------------------------------*/
\r
358 portBASE_TYPE xPortInstallInterruptHandler( unsigned char ucInterruptID, XInterruptHandler pxHandler, void *pvCallBackRef )
\r
362 /* An API function is provided to install an interrupt handler because the
\r
363 interrupt controller instance variable is private to this file. */
\r
365 lReturn = prvEnsureInterruptControllerIsInitialised();
\r
367 if( lReturn == pdPASS )
\r
369 lReturn = XIntc_Connect( &xInterruptControllerInstance, ucInterruptID, pxHandler, pvCallBackRef );
\r
372 if( lReturn == XST_SUCCESS )
\r
377 configASSERT( lReturn == pdPASS );
\r
381 /*-----------------------------------------------------------*/
\r
383 static long prvEnsureInterruptControllerIsInitialised( void )
\r
385 static long lInterruptControllerInitialised = pdFALSE;
\r
388 /* Ensure the interrupt controller instance variable is initialised before
\r
389 it is used, and that the initialisation only happens once. */
\r
390 if( lInterruptControllerInitialised != pdTRUE )
\r
392 lReturn = prvInitialiseInterruptController();
\r
394 if( lReturn == pdPASS )
\r
396 lInterruptControllerInitialised = pdTRUE;
\r
406 /*-----------------------------------------------------------*/
\r
409 * Handler for the timer interrupt. This is the handler that the application
\r
410 * defined callback function vApplicationSetupTimerInterrupt() should install.
\r
412 void vPortTickISR( void *pvUnused )
\r
414 extern void vApplicationClearTimerInterrupt( void );
\r
416 /* Ensure the unused parameter does not generate a compiler warning. */
\r
419 /* This port uses an application defined callback function to clear the tick
\r
420 interrupt because the kernel will run on lots of different MicroBlaze and
\r
421 FPGA configurations - not all of which will have the same timer peripherals
\r
422 defined or available. An example definition of
\r
423 vApplicationClearTimerInterrupt() is provided in the official demo
\r
424 application that accompanies this port. */
\r
425 vApplicationClearTimerInterrupt();
\r
427 /* Increment the RTOS tick - this might cause a task to unblock. */
\r
428 vTaskIncrementTick();
\r
430 /* If the preemptive scheduler is being used then a context switch should be
\r
431 requested in case incrementing the tick unblocked a task, or a time slice
\r
432 should cause another task to enter the Running state. */
\r
433 #if configUSE_PREEMPTION == 1
\r
434 /* Force vTaskSwitchContext() to be called as the interrupt exits. */
\r
435 ulTaskSwitchRequested = 1;
\r
438 /*-----------------------------------------------------------*/
\r
440 static long prvInitialiseInterruptController( void )
\r
444 lStatus = XIntc_Initialize( &xInterruptControllerInstance, configINTERRUPT_CONTROLLER_TO_USE );
\r
446 if( lStatus == XST_SUCCESS )
\r
448 /* Initialise the exception table. */
\r
449 Xil_ExceptionInit();
\r
451 /* Service all pending interrupts each time the handler is entered. */
\r
452 XIntc_SetIntrSvcOption( xInterruptControllerInstance.BaseAddress, XIN_SVC_ALL_ISRS_OPTION );
\r
454 /* Install exception handlers if the MicroBlaze is configured to handle
\r
455 exceptions, and the application defined constant
\r
456 configINSTALL_EXCEPTION_HANDLERS is set to 1. */
\r
457 #if ( MICROBLAZE_EXCEPTIONS_ENABLED == 1 ) && ( configINSTALL_EXCEPTION_HANDLERS == 1 )
\r
459 vPortExceptionsInstallHandlers();
\r
461 #endif /* MICROBLAZE_EXCEPTIONS_ENABLED */
\r
463 /* Start the interrupt controller. Interrupts are enabled when the
\r
464 scheduler starts. */
\r
465 lStatus = XIntc_Start( &xInterruptControllerInstance, XIN_REAL_MODE );
\r
467 if( lStatus == XST_SUCCESS )
\r
477 configASSERT( lStatus == pdPASS );
\r
481 /*-----------------------------------------------------------*/
\r