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
68 #include <xintc_i.h>
\r
69 #include <xtmrctr.h>
\r
70 #include <xil_exception.h>
\r
72 /* Tasks are started with interrupts enabled. */
\r
73 #define portINITIAL_MSR_STATE ( ( portSTACK_TYPE ) 0x02 )
\r
75 /* Tasks are started with a critical section nesting of 0 - however prior
\r
76 to the scheduler being commenced we don't want the critical nesting level
\r
77 to reach zero, so it is initialised to a high value. */
\r
78 #define portINITIAL_NESTING_VALUE ( 0xff )
\r
80 /* Our hardware setup only uses one counter. */
\r
81 #define portCOUNTER_0 0
\r
83 /* The stack used by the ISR is filled with a known value to assist in
\r
85 #define portISR_STACK_FILL_VALUE 0x55555555
\r
87 /*-----------------------------------------------------------*/
\r
90 * Initialise the interrupt controller instance.
\r
92 static portBASE_TYPE prvInitialiseInterruptController( void );
\r
95 * Call an application provided callback to set up the periodic interrupt used
\r
96 * for the RTOS tick. Using an application callback allows the application
\r
99 extern void vApplicationSetupTimerInterrupt( void );
\r
100 /*-----------------------------------------------------------*/
\r
102 /* Counts the nesting depth of calls to portENTER_CRITICAL(). Each task
\r
103 maintains it's own count, so this variable is saved as part of the task
\r
105 volatile unsigned portBASE_TYPE uxCriticalNesting = portINITIAL_NESTING_VALUE;
\r
107 /* To limit the amount of stack required by each task, this port uses a
\r
108 separate stack for interrupts. */
\r
109 unsigned long *pulISRStack;
\r
111 /* The instance of the interrupt controller used by this port. */
\r
112 static XIntc xInterruptControllerInstance;
\r
114 /*-----------------------------------------------------------*/
\r
117 * Initialise the stack of a task to look exactly as if a call to
\r
118 * portSAVE_CONTEXT had been made.
\r
120 * See the header file portable.h.
\r
122 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
124 extern void *_SDA2_BASE_, *_SDA_BASE_;
\r
125 const unsigned long ulR2 = ( unsigned long ) &_SDA2_BASE_;
\r
126 const unsigned long ulR13 = ( unsigned long ) &_SDA_BASE_;
\r
130 #ifdef XPAR_MICROBLAZE_USE_ICACHE
\r
131 microblaze_invalidate_icache();
\r
132 microblaze_enable_icache();
\r
135 #ifdef XPAR_MICROBLAZE_USE_DCACHE
\r
136 microblaze_invalidate_dcache();
\r
137 microblaze_enable_dcache();
\r
141 /* Place a few bytes of known values on the bottom of the stack.
\r
142 This is essential for the Microblaze port and these lines must
\r
143 not be omitted. The parameter value will overwrite the
\r
144 0x22222222 value during the function prologue. */
\r
145 *pxTopOfStack = ( portSTACK_TYPE ) 0x11111111;
\r
147 *pxTopOfStack = ( portSTACK_TYPE ) 0x22222222;
\r
149 *pxTopOfStack = ( portSTACK_TYPE ) 0x33333333;
\r
152 /* First stack an initial value for the critical section nesting. This
\r
153 is initialised to zero as tasks are started with interrupts enabled. */
\r
154 *pxTopOfStack = ( portSTACK_TYPE ) 0x00; /* R0 is always zero. */
\r
156 /* R1 is the SP. */
\r
158 /* Place an initial value for all the general purpose registers. */
\r
160 *pxTopOfStack = ( portSTACK_TYPE ) ulR2; /* R2 - read only small data area. */
\r
162 *pxTopOfStack = ( portSTACK_TYPE ) 0x03; /* R3 - return values and temporaries. */
\r
164 *pxTopOfStack = ( portSTACK_TYPE ) 0x04; /* R4 - return values and temporaries. */
\r
166 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;/* R5 contains the function call parameters. */
\r
168 *pxTopOfStack = ( portSTACK_TYPE ) 0x06; /* R6 - other parameters and temporaries. */
\r
170 *pxTopOfStack = ( portSTACK_TYPE ) 0x07; /* R7 - other parameters and temporaries. */
\r
172 *pxTopOfStack = ( portSTACK_TYPE ) 0x08; /* R8 - other parameters and temporaries. */
\r
174 *pxTopOfStack = ( portSTACK_TYPE ) 0x09; /* R9 - other parameters and temporaries. */
\r
176 *pxTopOfStack = ( portSTACK_TYPE ) 0x0a; /* R10 - other parameters and temporaries. */
\r
178 *pxTopOfStack = ( portSTACK_TYPE ) 0x0b; /* R11 - temporaries. */
\r
180 *pxTopOfStack = ( portSTACK_TYPE ) 0x0c; /* R12 - temporaries. */
\r
182 *pxTopOfStack = ( portSTACK_TYPE ) ulR13; /* R13 - read/write small data area. */
\r
184 *pxTopOfStack = ( portSTACK_TYPE ) pxCode; /* R14 - return address for interrupt. */
\r
186 *pxTopOfStack = ( portSTACK_TYPE ) 0x0f; /* R15 - return address for subroutine. */
\r
188 *pxTopOfStack = ( portSTACK_TYPE ) 0x10; /* R16 - return address for trap (debugger). */
\r
190 *pxTopOfStack = ( portSTACK_TYPE ) 0x11; /* R17 - return address for exceptions, if configured. */
\r
192 *pxTopOfStack = ( portSTACK_TYPE ) 0x12; /* R18 - reserved for assembler and compiler temporaries. */
\r
194 *pxTopOfStack = ( portSTACK_TYPE ) 0x13; /* R19 - must be saved across function calls. Callee-save. */
\r
196 *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
198 *pxTopOfStack = ( portSTACK_TYPE ) 0x15; /* R21 - must be saved across function calls. Callee-save. */
\r
200 *pxTopOfStack = ( portSTACK_TYPE ) 0x16; /* R22 - must be saved across function calls. Callee-save. */
\r
202 *pxTopOfStack = ( portSTACK_TYPE ) 0x17; /* R23 - must be saved across function calls. Callee-save. */
\r
204 *pxTopOfStack = ( portSTACK_TYPE ) 0x18; /* R24 - must be saved across function calls. Callee-save. */
\r
206 *pxTopOfStack = ( portSTACK_TYPE ) 0x19; /* R25 - must be saved across function calls. Callee-save. */
\r
208 *pxTopOfStack = ( portSTACK_TYPE ) 0x1a; /* R26 - must be saved across function calls. Callee-save. */
\r
210 *pxTopOfStack = ( portSTACK_TYPE ) 0x1b; /* R27 - must be saved across function calls. Callee-save. */
\r
212 *pxTopOfStack = ( portSTACK_TYPE ) 0x1c; /* R28 - must be saved across function calls. Callee-save. */
\r
214 *pxTopOfStack = ( portSTACK_TYPE ) 0x1d; /* R29 - must be saved across function calls. Callee-save. */
\r
216 *pxTopOfStack = ( portSTACK_TYPE ) 0x1e; /* R30 - must be saved across function calls. Callee-save. */
\r
219 /* The MSR is stacked between R30 and R31. */
\r
220 *pxTopOfStack = portINITIAL_MSR_STATE;
\r
223 *pxTopOfStack = ( portSTACK_TYPE ) 0x1f; /* R31 - must be saved across function calls. Callee-save. */
\r
226 /* Return a pointer to the top of the stack we have generated so this can
\r
227 be stored in the task control block for the task. */
\r
228 return pxTopOfStack;
\r
230 /*-----------------------------------------------------------*/
\r
232 portBASE_TYPE xPortStartScheduler( void )
\r
234 extern void ( vStartFirstTask )( void );
\r
236 /* Setup the hardware to generate the tick. Interrupts are disabled when
\r
237 this function is called. */
\r
238 vApplicationSetupTimerInterrupt();
\r
240 /* Allocate the stack to be used by the interrupt handler. */
\r
241 pulISRStack = ( unsigned long * ) pvPortMalloc( configINTERRUPT_STACK_SIZE * sizeof( portSTACK_TYPE ) );
\r
242 configASSERT( pulISRStack != NULL );
\r
244 /* Restore the context of the first task that is going to run. */
\r
245 if( pulISRStack != NULL )
\r
247 /* Fill the ISR stack with a known value to facilitate debugging. */
\r
248 memset( pulISRStack, portISR_STACK_FILL_VALUE, configMINIMAL_STACK_SIZE * sizeof( portSTACK_TYPE ) );
\r
249 pulISRStack += ( configMINIMAL_STACK_SIZE - 1 );
\r
251 /* From here on, the created tasks will be executing. */
\r
255 /* Should not get here as the tasks are now running! */
\r
258 /*-----------------------------------------------------------*/
\r
260 void vPortEndScheduler( void )
\r
262 /* Not implemented. */
\r
264 /*-----------------------------------------------------------*/
\r
267 * Manual context switch called by portYIELD or taskYIELD.
\r
269 void vPortYield( void )
\r
271 extern void VPortYieldASM( void );
\r
273 /* Perform the context switch in a critical section to assure it is
\r
274 not interrupted by the tick ISR. It is not a problem to do this as
\r
275 each task maintains it's own interrupt status. */
\r
276 portENTER_CRITICAL();
\r
278 /* Jump directly to the yield function to ensure there is no
\r
279 compiler generated prologue code. */
\r
280 asm volatile ( "bralid r14, VPortYieldASM \n\t" \
\r
281 "or r0, r0, r0 \n\t" );
\r
283 portEXIT_CRITICAL();
\r
285 /*-----------------------------------------------------------*/
\r
288 * The task context has already been saved when this is called.
\r
290 * This handler determines the interrupt source and calls the relevant
\r
291 * peripheral handler.
\r
293 void vTaskISRHandler( void )
\r
295 static unsigned long ulPending;
\r
296 static XIntc_VectorTableEntry *pxTablePtr;
\r
297 static XIntc_Config *pxConfig;
\r
298 static unsigned long ulInterruptMask;
\r
300 /* Which interrupts are pending? */
\r
301 ulPending = XIntc_In32( ( XPAR_INTC_SINGLE_BASEADDR + XIN_IVR_OFFSET ) );
\r
303 if( ulPending < XPAR_INTC_MAX_NUM_INTR_INPUTS )
\r
306 ulInterruptMask = ( unsigned long ) 1 << ulPending;
\r
308 /* Get the configuration data using the device ID */
\r
309 pxConfig = &XIntc_ConfigTable[ ( unsigned long ) XPAR_INTC_SINGLE_DEVICE_ID ];
\r
311 pxTablePtr = &( pxConfig->HandlerTable[ ulPending ] );
\r
312 if( pxConfig->AckBeforeService & ( ulInterruptMask ) )
\r
314 XIntc_AckIntr( pxConfig->BaseAddress, ulInterruptMask );
\r
315 pxTablePtr->Handler( pxTablePtr->CallBackRef );
\r
319 pxTablePtr->Handler( pxTablePtr->CallBackRef );
\r
320 XIntc_AckIntr( pxConfig->BaseAddress, ulInterruptMask );
\r
324 /*-----------------------------------------------------------*/
\r
326 void vPortEnableInterrupt( unsigned char ucInterruptID )
\r
328 XIntc_Enable( &xInterruptControllerInstance, ucInterruptID );
\r
330 /*-----------------------------------------------------------*/
\r
332 void vPortDisableInterrupt( unsigned char ucInterruptID )
\r
334 XIntc_Disable( &xInterruptControllerInstance, ucInterruptID );
\r
336 /*-----------------------------------------------------------*/
\r
338 portBASE_TYPE xPortInstallInterruptHandler( unsigned char ucInterruptID, XInterruptHandler pxHandler, void *pvCallBackRef )
\r
340 static portBASE_TYPE xInterruptControllerInitialised = pdFALSE;
\r
341 portBASE_TYPE xReturn = XST_SUCCESS;
\r
343 if( xInterruptControllerInitialised != pdTRUE )
\r
345 xReturn = prvInitialiseInterruptController();
\r
346 xInterruptControllerInitialised = pdTRUE;
\r
349 if( xReturn == XST_SUCCESS )
\r
351 xReturn = XIntc_Connect( &xInterruptControllerInstance, ucInterruptID, pxHandler, pvCallBackRef );
\r
354 if( xReturn == XST_SUCCESS )
\r
361 /*-----------------------------------------------------------*/
\r
364 * Handler for the timer interrupt.
\r
366 void vTickISR( void *pvUnused )
\r
368 extern void vApplicationClearTimerInterrupt();
\r
370 /* Ensure the unused parameter does not generate a compiler warning. */
\r
373 vApplicationClearTimerInterrupt();
\r
375 /* Increment the RTOS tick - this might cause a task to unblock. */
\r
376 vTaskIncrementTick();
\r
378 /* If we are using the preemptive scheduler then we also need to determine
\r
379 if this tick should cause a context switch. */
\r
380 #if configUSE_PREEMPTION == 1
\r
381 vTaskSwitchContext();
\r
384 /*-----------------------------------------------------------*/
\r
386 static portBASE_TYPE prvInitialiseInterruptController( void )
\r
388 portBASE_TYPE xStatus;
\r
390 xStatus = XIntc_Initialize( &xInterruptControllerInstance, configINTERRUPT_CONTROLLER_TO_USE );
\r
392 if( xStatus == XST_SUCCESS )
\r
394 /* Initialise the exception table. */
\r
395 Xil_ExceptionInit();
\r
397 /* Service all pending interrupts each time the handler is entered. */
\r
398 XIntc_SetIntrSvcOption( xInterruptControllerInstance.BaseAddress, XIN_SVC_ALL_ISRS_OPTION );
\r
400 /* Start the interrupt controller. Interrupts are enabled when the
\r
401 scheduler starts. */
\r
402 xStatus = XIntc_Start( &xInterruptControllerInstance, XIN_REAL_MODE );
\r
404 /* Ensure the compiler does not generate warnings for the unused
\r
405 iStatus valud if configASSERT() is not defined. */
\r
409 configASSERT( ( xStatus == ( portBASE_TYPE ) XST_SUCCESS ) )
\r