2 FreeRTOS V7.0.1 - Copyright (C) 2011 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 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
54 /*-----------------------------------------------------------
\r
55 * Implementation of functions defined in portable.h for the MicroBlaze port.
\r
56 *----------------------------------------------------------*/
\r
59 /* Scheduler includes. */
\r
60 #include "FreeRTOS.h"
\r
63 /* Standard includes. */
\r
66 /* Hardware includes. */
\r
67 #include <xintc_i.h>
\r
68 #include <xil_exception.h>
\r
69 #include <microblaze_exceptions_g.h>
\r
71 /* Tasks are started with a critical section nesting of 0 - however, prior to
\r
72 the scheduler being commenced interrupts should not be enabled, so the critical
\r
73 nesting variable is initialised to a non-zero value. */
\r
74 #define portINITIAL_NESTING_VALUE ( 0xff )
\r
76 /* The bit within the MSR register that enabled/disables interrupts. */
\r
77 #define portMSR_IE ( 0x02U )
\r
79 /* If the floating point unit is included in the MicroBlaze build, then the
\r
80 FSR register is saved as part of the task context. portINITIAL_FSR is the value
\r
81 given to the FSR register when the initial context is set up for a task being
\r
83 #define portINITIAL_FSR ( 0U )
\r
84 /*-----------------------------------------------------------*/
\r
87 * Initialise the interrupt controller instance.
\r
89 static long prvInitialiseInterruptController( void );
\r
91 /* Ensure the interrupt controller instance variable is initialised before it is
\r
92 * used, and that the initialisation only happens once.
\r
94 static long prvEnsureInterruptControllerIsInitialised( void );
\r
96 /*-----------------------------------------------------------*/
\r
98 /* Counts the nesting depth of calls to portENTER_CRITICAL(). Each task
\r
99 maintains its own count, so this variable is saved as part of the task
\r
101 volatile unsigned portBASE_TYPE uxCriticalNesting = portINITIAL_NESTING_VALUE;
\r
103 /* This port uses a separate stack for interrupts. This prevents the stack of
\r
104 every task needing to be large enough to hold an entire interrupt stack on top
\r
105 of the task stack. */
\r
106 unsigned long *pulISRStack;
\r
108 /* If an interrupt requests a context switch, then ulTaskSwitchRequested will
\r
109 get set to 1. ulTaskSwitchRequested is inspected just before the main interrupt
\r
110 handler exits. If, at that time, ulTaskSwitchRequested is set to 1, the kernel
\r
111 will call vTaskSwitchContext() to ensure the task that runs immediately after
\r
112 the interrupt exists is the highest priority task that is able to run. This is
\r
113 an unusual mechanism, but is used for this port because a single interrupt can
\r
114 cause the servicing of multiple peripherals - and it is inefficient to call
\r
115 vTaskSwitchContext() multiple times as each peripheral is serviced. */
\r
116 volatile unsigned long ulTaskSwitchRequested = 0UL;
\r
118 /* The instance of the interrupt controller used by this port. This is required
\r
119 by the Xilinx library API functions. */
\r
120 static XIntc xInterruptControllerInstance;
\r
122 /*-----------------------------------------------------------*/
\r
125 * Initialise the stack of a task to look exactly as if a call to
\r
126 * portSAVE_CONTEXT had been made.
\r
128 * See the portable.h header file.
\r
130 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
132 extern void *_SDA2_BASE_, *_SDA_BASE_;
\r
133 const unsigned long ulR2 = ( unsigned long ) &_SDA2_BASE_;
\r
134 const unsigned long ulR13 = ( unsigned long ) &_SDA_BASE_;
\r
136 /* Place a few bytes of known values on the bottom of the stack.
\r
137 This is essential for the Microblaze port and these lines must
\r
139 *pxTopOfStack = ( portSTACK_TYPE ) 0x00000000;
\r
141 *pxTopOfStack = ( portSTACK_TYPE ) 0x00000000;
\r
143 *pxTopOfStack = ( portSTACK_TYPE ) 0x00000000;
\r
146 #if XPAR_MICROBLAZE_0_USE_FPU == 1
\r
147 /* The FSR value placed in the initial task context is just 0. */
\r
148 *pxTopOfStack = portINITIAL_FSR;
\r
152 /* The MSR value placed in the initial task context should have interrupts
\r
153 disabled. Each task will enable interrupts automatically when it enters
\r
154 the running state for the first time. */
\r
155 *pxTopOfStack = mfmsr() & ~portMSR_IE;
\r
158 /* First stack an initial value for the critical section nesting. This
\r
159 is initialised to zero. */
\r
160 *pxTopOfStack = ( portSTACK_TYPE ) 0x00;
\r
162 /* R0 is always zero. */
\r
163 /* R1 is the SP. */
\r
165 /* Place an initial value for all the general purpose registers. */
\r
167 *pxTopOfStack = ( portSTACK_TYPE ) ulR2; /* R2 - read only small data area. */
\r
169 *pxTopOfStack = ( portSTACK_TYPE ) 0x03; /* R3 - return values and temporaries. */
\r
171 *pxTopOfStack = ( portSTACK_TYPE ) 0x04; /* R4 - return values and temporaries. */
\r
173 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;/* R5 contains the function call parameters. */
\r
175 #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING
\r
177 *pxTopOfStack = ( portSTACK_TYPE ) 0x06; /* R6 - other parameters and temporaries. Used as the return address from vPortTaskEntryPoint. */
\r
179 *pxTopOfStack = ( portSTACK_TYPE ) 0x07; /* R7 - other parameters and temporaries. */
\r
181 *pxTopOfStack = ( portSTACK_TYPE ) 0x08; /* R8 - other parameters and temporaries. */
\r
183 *pxTopOfStack = ( portSTACK_TYPE ) 0x09; /* R9 - other parameters and temporaries. */
\r
185 *pxTopOfStack = ( portSTACK_TYPE ) 0x0a; /* R10 - other parameters and temporaries. */
\r
187 *pxTopOfStack = ( portSTACK_TYPE ) 0x0b; /* R11 - temporaries. */
\r
189 *pxTopOfStack = ( portSTACK_TYPE ) 0x0c; /* R12 - temporaries. */
\r
195 *pxTopOfStack = ( portSTACK_TYPE ) ulR13; /* R13 - read/write small data area. */
\r
197 *pxTopOfStack = ( portSTACK_TYPE ) pxCode; /* R14 - return address for interrupt. */
\r
199 *pxTopOfStack = ( portSTACK_TYPE ) NULL; /* R15 - return address for subroutine. */
\r
201 #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING
\r
203 *pxTopOfStack = ( portSTACK_TYPE ) 0x10; /* R16 - return address for trap (debugger). */
\r
205 *pxTopOfStack = ( portSTACK_TYPE ) 0x11; /* R17 - return address for exceptions, if configured. */
\r
207 *pxTopOfStack = ( portSTACK_TYPE ) 0x12; /* R18 - reserved for assembler and compiler temporaries. */
\r
213 *pxTopOfStack = ( portSTACK_TYPE ) 0x00; /* R19 - must be saved across function calls. Callee-save. Seems to be interpreted as the frame pointer. */
\r
215 #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING
\r
217 *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
219 *pxTopOfStack = ( portSTACK_TYPE ) 0x15; /* R21 - must be saved across function calls. Callee-save. */
\r
221 *pxTopOfStack = ( portSTACK_TYPE ) 0x16; /* R22 - must be saved across function calls. Callee-save. */
\r
223 *pxTopOfStack = ( portSTACK_TYPE ) 0x17; /* R23 - must be saved across function calls. Callee-save. */
\r
225 *pxTopOfStack = ( portSTACK_TYPE ) 0x18; /* R24 - must be saved across function calls. Callee-save. */
\r
227 *pxTopOfStack = ( portSTACK_TYPE ) 0x19; /* R25 - must be saved across function calls. Callee-save. */
\r
229 *pxTopOfStack = ( portSTACK_TYPE ) 0x1a; /* R26 - must be saved across function calls. Callee-save. */
\r
231 *pxTopOfStack = ( portSTACK_TYPE ) 0x1b; /* R27 - must be saved across function calls. Callee-save. */
\r
233 *pxTopOfStack = ( portSTACK_TYPE ) 0x1c; /* R28 - must be saved across function calls. Callee-save. */
\r
235 *pxTopOfStack = ( portSTACK_TYPE ) 0x1d; /* R29 - must be saved across function calls. Callee-save. */
\r
237 *pxTopOfStack = ( portSTACK_TYPE ) 0x1e; /* R30 - must be saved across function calls. Callee-save. */
\r
239 *pxTopOfStack = ( portSTACK_TYPE ) 0x1f; /* R31 - must be saved across function calls. Callee-save. */
\r
242 pxTopOfStack -= 13;
\r
245 /* Return a pointer to the top of the stack that has been generated so this
\r
246 can be stored in the task control block for the task. */
\r
247 return pxTopOfStack;
\r
249 /*-----------------------------------------------------------*/
\r
251 portBASE_TYPE xPortStartScheduler( void )
\r
253 extern void ( vPortStartFirstTask )( void );
\r
254 extern unsigned long _stack[];
\r
256 /* Setup the hardware to generate the tick. Interrupts are disabled when
\r
257 this function is called.
\r
259 This port uses an application defined callback function to install the tick
\r
260 interrupt handler because the kernel will run on lots of different
\r
261 MicroBlaze and FPGA configurations - not all of which will have the same
\r
262 timer peripherals defined or available. An example definition of
\r
263 vApplicationSetupTimerInterrupt() is provided in the official demo
\r
264 application that accompanies this port. */
\r
265 vApplicationSetupTimerInterrupt();
\r
267 /* Reuse the stack from main() as the stack for the interrupts/exceptions. */
\r
268 pulISRStack = ( unsigned long * ) _stack;
\r
270 /* Ensure there is enough space for the functions called from the interrupt
\r
271 service routines to write back into the stack frame of the caller. */
274 /* Restore the context of the first task that is going to run. From here
\r
275 on, the created tasks will be executing. */
\r
276 vPortStartFirstTask();
\r
278 /* Should not get here as the tasks are now running! */
\r
281 /*-----------------------------------------------------------*/
\r
283 void vPortEndScheduler( void )
\r
285 /* Not implemented. */
\r
287 /*-----------------------------------------------------------*/
\r
290 * Manual context switch called by portYIELD or taskYIELD.
\r
292 void vPortYield( void )
\r
294 extern void VPortYieldASM( void );
\r
296 /* Perform the context switch in a critical section to assure it is
\r
297 not interrupted by the tick ISR. It is not a problem to do this as
\r
298 each task maintains its own interrupt status. */
\r
299 portENTER_CRITICAL();
\r
301 /* Jump directly to the yield function to ensure there is no
\r
302 compiler generated prologue code. */
\r
303 asm volatile ( "bralid r14, VPortYieldASM \n\t" \
\r
304 "or r0, r0, r0 \n\t" );
\r
306 portEXIT_CRITICAL();
\r
308 /*-----------------------------------------------------------*/
\r
310 void vPortEnableInterrupt( unsigned char ucInterruptID )
\r
314 /* An API function is provided to enable an interrupt in the interrupt
\r
315 controller because the interrupt controller instance variable is private
\r
317 lReturn = prvEnsureInterruptControllerIsInitialised();
\r
318 if( lReturn == pdPASS )
\r
320 XIntc_Enable( &xInterruptControllerInstance, ucInterruptID );
\r
323 configASSERT( lReturn );
\r
325 /*-----------------------------------------------------------*/
\r
327 void vPortDisableInterrupt( unsigned char ucInterruptID )
\r
331 /* An API function is provided to disable an interrupt in the interrupt
\r
332 controller because the interrupt controller instance variable is private
\r
334 lReturn = prvEnsureInterruptControllerIsInitialised();
\r
336 if( lReturn == pdPASS )
\r
338 XIntc_Disable( &xInterruptControllerInstance, ucInterruptID );
\r
341 configASSERT( lReturn );
\r
343 /*-----------------------------------------------------------*/
\r
345 portBASE_TYPE xPortInstallInterruptHandler( unsigned char ucInterruptID, XInterruptHandler pxHandler, void *pvCallBackRef )
\r
349 /* An API function is provided to install an interrupt handler because the
\r
350 interrupt controller instance variable is private to this file. */
\r
352 lReturn = prvEnsureInterruptControllerIsInitialised();
\r
354 if( lReturn == pdPASS )
\r
356 lReturn = XIntc_Connect( &xInterruptControllerInstance, ucInterruptID, pxHandler, pvCallBackRef );
\r
359 if( lReturn == XST_SUCCESS )
\r
364 configASSERT( lReturn == pdPASS );
\r
368 /*-----------------------------------------------------------*/
\r
370 static long prvEnsureInterruptControllerIsInitialised( void )
\r
372 static long lInterruptControllerInitialised = pdFALSE;
\r
375 /* Ensure the interrupt controller instance variable is initialised before
\r
376 it is used, and that the initialisation only happens once. */
\r
377 if( lInterruptControllerInitialised != pdTRUE )
\r
379 lReturn = prvInitialiseInterruptController();
\r
381 if( lReturn == pdPASS )
\r
383 lInterruptControllerInitialised = pdTRUE;
\r
393 /*-----------------------------------------------------------*/
\r
396 * Handler for the timer interrupt. This is the handler that the application
\r
397 * defined callback function vApplicationSetupTimerInterrupt() should install.
\r
399 void vTickISR( void *pvUnused )
\r
401 extern void vApplicationClearTimerInterrupt( void );
\r
403 /* Ensure the unused parameter does not generate a compiler warning. */
\r
406 /* This port uses an application defined callback function to clear the tick
\r
407 interrupt because the kernel will run on lots of different MicroBlaze and
\r
408 FPGA configurations - not all of which will have the same timer peripherals
\r
409 defined or available. An example definition of
\r
410 vApplicationClearTimerInterrupt() is provided in the official demo
\r
411 application that accompanies this port. */
\r
412 vApplicationClearTimerInterrupt();
\r
414 /* Increment the RTOS tick - this might cause a task to unblock. */
\r
415 vTaskIncrementTick();
\r
417 /* If the preemptive scheduler is being used then a context switch should be
\r
418 requested in case incrementing the tick unblocked a task, or a time slice
\r
419 should cause another task to enter the Running state. */
\r
420 #if configUSE_PREEMPTION == 1
\r
421 /* Force vTaskSwitchContext() to be called as the interrupt exits. */
\r
422 ulTaskSwitchRequested = 1;
\r
425 /*-----------------------------------------------------------*/
\r
427 static long prvInitialiseInterruptController( void )
\r
431 lStatus = XIntc_Initialize( &xInterruptControllerInstance, configINTERRUPT_CONTROLLER_TO_USE );
\r
433 if( lStatus == XST_SUCCESS )
\r
435 /* Initialise the exception table. */
\r
436 Xil_ExceptionInit();
\r
438 /* Service all pending interrupts each time the handler is entered. */
\r
439 XIntc_SetIntrSvcOption( xInterruptControllerInstance.BaseAddress, XIN_SVC_ALL_ISRS_OPTION );
\r
441 /* Install exception handlers if the MicroBlaze is configured to handle
\r
442 exceptions, and the application defined constant
\r
443 configINSTALL_EXCEPTION_HANDLERS is set to 1. */
\r
444 #if ( MICROBLAZE_EXCEPTIONS_ENABLED == 1 ) && ( configINSTALL_EXCEPTION_HANDLERS == 1 )
\r
446 vPortExceptionsInstallHandlers();
\r
448 #endif /* MICROBLAZE_EXCEPTIONS_ENABLED */
\r
450 /* Start the interrupt controller. Interrupts are enabled when the
\r
451 scheduler starts. */
\r
452 lStatus = XIntc_Start( &xInterruptControllerInstance, XIN_REAL_MODE );
\r
454 if( lStatus == XST_SUCCESS )
\r
464 configASSERT( lStatus == pdPASS );
\r
468 /*-----------------------------------------------------------*/
\r