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_i.h>
\r
70 #include <microblaze_exceptions_g.h>
\r
72 /* Tasks are started with a critical section nesting of 0 - however prior
\r
73 to the scheduler being commenced we don't want the critical nesting level
\r
74 to reach zero, so it is initialised to a high value. */
\r
75 #define portINITIAL_NESTING_VALUE ( 0xff )
\r
77 /* The bit within the MSR register that enabled/disables interrupts. */
\r
78 #define portMSR_IE ( 0x02U )
\r
80 #define portINITIAL_FSR ( 0U )
\r
81 /*-----------------------------------------------------------*/
\r
84 * Initialise the interrupt controller instance.
\r
86 static portBASE_TYPE prvInitialiseInterruptController( void );
\r
88 static void prvExceptionHandler( void *pvExceptionID );
\r
91 * Call an application provided callback to set up the periodic interrupt used
\r
92 * for the RTOS tick. Using an application callback allows the application
\r
95 extern void vApplicationSetupTimerInterrupt( void );
\r
96 /*-----------------------------------------------------------*/
\r
98 /* Counts the nesting depth of calls to portENTER_CRITICAL(). Each task
\r
99 maintains it's own count, so this variable is saved as part of the task
\r
101 volatile unsigned portBASE_TYPE uxCriticalNesting = portINITIAL_NESTING_VALUE;
\r
103 /* To limit the amount of stack required by each task, this port uses a
\r
104 separate stack for interrupts. */
\r
105 unsigned long *pulISRStack;
\r
107 /* If an interrupt requests a context switch then ulTaskSwitchRequested will
\r
108 get set to 1, which in turn will cause vTaskSwitchContext() to be called
\r
109 prior to a task context getting restored on exit from the interrupt. This
\r
110 mechanism is used as a single interrupt can cause multiple peripherals handlers
\r
111 to get called, and vTaskSwitchContext() should not get called in each handler. */
\r
112 volatile unsigned long ulTaskSwitchRequested = 0UL;
\r
114 /* The instance of the interrupt controller used by this port. */
\r
115 static XIntc xInterruptControllerInstance;
\r
117 /*-----------------------------------------------------------*/
\r
120 * Initialise the stack of a task to look exactly as if a call to
\r
121 * portSAVE_CONTEXT had been made.
\r
123 * See the header file portable.h.
\r
125 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
127 extern void *_SDA2_BASE_, *_SDA_BASE_;
\r
128 const unsigned long ulR2 = ( unsigned long ) &_SDA2_BASE_;
\r
129 const unsigned long ulR13 = ( unsigned long ) &_SDA_BASE_;
\r
131 /* Place a few bytes of known values on the bottom of the stack.
\r
132 This is essential for the Microblaze port and these lines must
\r
133 not be omitted. The parameter value will overwrite the
\r
134 0x22222222 value during the function prologue. */
\r
135 *pxTopOfStack = ( portSTACK_TYPE ) 0x11111111;
\r
137 *pxTopOfStack = ( portSTACK_TYPE ) 0x22222222;
\r
139 *pxTopOfStack = ( portSTACK_TYPE ) 0x33333333;
\r
142 /* The debugger will look at the previous stack frame. */
\r
143 *pxTopOfStack = ( portSTACK_TYPE ) 0x00000000;
\r
145 *pxTopOfStack = ( portSTACK_TYPE ) 0x00000000;
\r
147 *pxTopOfStack = ( portSTACK_TYPE ) 0x00000000;
\r
150 #if XPAR_MICROBLAZE_0_USE_FPU == 1
\r
151 /* The FSR value placed in the initial task context is just 0. */
\r
152 *pxTopOfStack = portINITIAL_FSR;
\r
156 /* The MSR value placed in the initial task context should have interrupts
\r
157 disabled. Each task will enable interrupts automatically when it enters
\r
158 the running state for the first time. */
\r
159 *pxTopOfStack = mfmsr() & ~portMSR_IE;
\r
162 /* First stack an initial value for the critical section nesting. This
\r
163 is initialised to zero. */
\r
164 *pxTopOfStack = ( portSTACK_TYPE ) 0x00;
\r
166 /* R0 is always zero. */
\r
167 /* R1 is the SP. */
\r
169 /* Place an initial value for all the general purpose registers. */
\r
171 *pxTopOfStack = ( portSTACK_TYPE ) ulR2; /* R2 - read only small data area. */
\r
173 *pxTopOfStack = ( portSTACK_TYPE ) 0x03; /* R3 - return values and temporaries. */
\r
175 *pxTopOfStack = ( portSTACK_TYPE ) 0x04; /* R4 - return values and temporaries. */
\r
177 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;/* R5 contains the function call parameters. */
\r
179 *pxTopOfStack = ( portSTACK_TYPE ) 0x06; /* R6 - other parameters and temporaries. Used as the return address from vPortTaskEntryPoint. */
\r
181 *pxTopOfStack = ( portSTACK_TYPE ) 0x07; /* R7 - other parameters and temporaries. */
\r
183 *pxTopOfStack = ( portSTACK_TYPE ) 0x08; /* R8 - other parameters and temporaries. */
\r
185 *pxTopOfStack = ( portSTACK_TYPE ) 0x09; /* R9 - other parameters and temporaries. */
\r
187 *pxTopOfStack = ( portSTACK_TYPE ) 0x0a; /* R10 - other parameters and temporaries. */
\r
189 *pxTopOfStack = ( portSTACK_TYPE ) 0x0b; /* R11 - temporaries. */
\r
191 *pxTopOfStack = ( portSTACK_TYPE ) 0x0c; /* R12 - temporaries. */
\r
193 *pxTopOfStack = ( portSTACK_TYPE ) ulR13; /* R13 - read/write small data area. */
\r
195 *pxTopOfStack = ( portSTACK_TYPE ) pxCode; /* R14 - return address for interrupt. */
\r
197 *pxTopOfStack = ( portSTACK_TYPE ) NULL; /* R15 - return address for subroutine. */
\r
199 *pxTopOfStack = ( portSTACK_TYPE ) 0x10; /* R16 - return address for trap (debugger). */
\r
201 *pxTopOfStack = ( portSTACK_TYPE ) 0x11; /* R17 - return address for exceptions, if configured. */
\r
203 *pxTopOfStack = ( portSTACK_TYPE ) 0x12; /* R18 - reserved for assembler and compiler temporaries. */
\r
205 *pxTopOfStack = ( portSTACK_TYPE ) 0x00; /* R19 - must be saved across function calls. Callee-save. Seems to be interpreted as the frame pointer. */
\r
207 *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
209 *pxTopOfStack = ( portSTACK_TYPE ) 0x15; /* R21 - must be saved across function calls. Callee-save. */
\r
211 *pxTopOfStack = ( portSTACK_TYPE ) 0x16; /* R22 - must be saved across function calls. Callee-save. */
\r
213 *pxTopOfStack = ( portSTACK_TYPE ) 0x17; /* R23 - must be saved across function calls. Callee-save. */
\r
215 *pxTopOfStack = ( portSTACK_TYPE ) 0x18; /* R24 - must be saved across function calls. Callee-save. */
\r
217 *pxTopOfStack = ( portSTACK_TYPE ) 0x19; /* R25 - must be saved across function calls. Callee-save. */
\r
219 *pxTopOfStack = ( portSTACK_TYPE ) 0x1a; /* R26 - must be saved across function calls. Callee-save. */
\r
221 *pxTopOfStack = ( portSTACK_TYPE ) 0x1b; /* R27 - must be saved across function calls. Callee-save. */
\r
223 *pxTopOfStack = ( portSTACK_TYPE ) 0x1c; /* R28 - must be saved across function calls. Callee-save. */
\r
225 *pxTopOfStack = ( portSTACK_TYPE ) 0x1d; /* R29 - must be saved across function calls. Callee-save. */
\r
227 *pxTopOfStack = ( portSTACK_TYPE ) 0x1e; /* R30 - must be saved across function calls. Callee-save. */
\r
229 *pxTopOfStack = ( portSTACK_TYPE ) 0x1f; /* R31 - must be saved across function calls. Callee-save. */
\r
232 /* Return a pointer to the top of the stack we have generated so this can
\r
233 be stored in the task control block for the task. */
\r
234 return pxTopOfStack;
\r
236 /*-----------------------------------------------------------*/
\r
238 portBASE_TYPE xPortStartScheduler( void )
\r
240 extern void ( vPortStartFirstTask )( void );
\r
241 extern unsigned long _stack[];
\r
243 /* Setup the hardware to generate the tick. Interrupts are disabled when
\r
244 this function is called. */
\r
245 vApplicationSetupTimerInterrupt();
\r
247 /* Reuse the stack from main as the stack for the interrupts/exceptions. */
\r
248 pulISRStack = ( unsigned long * ) _stack;
\r
250 /* Restore the context of the first task that is going to run. From here
\r
251 on, the created tasks will be executing. */
\r
252 vPortStartFirstTask();
\r
254 /* Should not get here as the tasks are now running! */
\r
257 /*-----------------------------------------------------------*/
\r
259 void vPortEndScheduler( void )
\r
261 /* Not implemented. */
\r
263 /*-----------------------------------------------------------*/
\r
266 * Manual context switch called by portYIELD or taskYIELD.
\r
268 void vPortYield( void )
\r
270 extern void VPortYieldASM( void );
\r
272 /* Perform the context switch in a critical section to assure it is
\r
273 not interrupted by the tick ISR. It is not a problem to do this as
\r
274 each task maintains it's own interrupt status. */
\r
275 portENTER_CRITICAL();
\r
277 /* Jump directly to the yield function to ensure there is no
\r
278 compiler generated prologue code. */
\r
279 asm volatile ( "bralid r14, VPortYieldASM \n\t" \
\r
280 "or r0, r0, r0 \n\t" );
\r
282 portEXIT_CRITICAL();
\r
284 /*-----------------------------------------------------------*/
\r
286 void vPortEnableInterrupt( unsigned char ucInterruptID )
\r
288 XIntc_Enable( &xInterruptControllerInstance, ucInterruptID );
\r
290 /*-----------------------------------------------------------*/
\r
292 void vPortDisableInterrupt( unsigned char ucInterruptID )
\r
294 XIntc_Disable( &xInterruptControllerInstance, ucInterruptID );
\r
296 /*-----------------------------------------------------------*/
\r
298 portBASE_TYPE xPortInstallInterruptHandler( unsigned char ucInterruptID, XInterruptHandler pxHandler, void *pvCallBackRef )
\r
300 static portBASE_TYPE xInterruptControllerInitialised = pdFALSE;
\r
301 portBASE_TYPE xReturn = XST_SUCCESS;
\r
303 if( xInterruptControllerInitialised != pdTRUE )
\r
305 xReturn = prvInitialiseInterruptController();
\r
306 xInterruptControllerInitialised = pdTRUE;
\r
309 if( xReturn == XST_SUCCESS )
\r
311 xReturn = XIntc_Connect( &xInterruptControllerInstance, ucInterruptID, pxHandler, pvCallBackRef );
\r
314 if( xReturn == XST_SUCCESS )
\r
321 /*-----------------------------------------------------------*/
\r
324 * Handler for the timer interrupt.
\r
326 void vTickISR( void *pvUnused )
\r
328 extern void vApplicationClearTimerInterrupt( void );
\r
330 /* Ensure the unused parameter does not generate a compiler warning. */
\r
333 vApplicationClearTimerInterrupt();
\r
335 /* Increment the RTOS tick - this might cause a task to unblock. */
\r
336 vTaskIncrementTick();
\r
338 /* If we are using the preemptive scheduler then we also need to determine
\r
339 if this tick should cause a context switch. */
\r
340 #if configUSE_PREEMPTION == 1
\r
341 /* Force vTaskSwitchContext() to be called as the interrupt exits. */
\r
342 ulTaskSwitchRequested = 1;
\r
345 /*-----------------------------------------------------------*/
\r
347 static void prvExceptionHandler( void *pvExceptionID )
\r
349 volatile unsigned long ulExceptionID;
\r
351 ulExceptionID = ( unsigned long ) pvExceptionID;
\r
357 ( void ) ulExceptionID;
\r
359 /*-----------------------------------------------------------*/
\r
361 static portBASE_TYPE prvInitialiseInterruptController( void )
\r
363 portBASE_TYPE xStatus;
\r
365 xStatus = XIntc_Initialize( &xInterruptControllerInstance, configINTERRUPT_CONTROLLER_TO_USE );
\r
367 if( xStatus == XST_SUCCESS )
\r
369 /* Initialise the exception table. */
\r
370 Xil_ExceptionInit();
\r
372 /* Service all pending interrupts each time the handler is entered. */
\r
373 XIntc_SetIntrSvcOption( xInterruptControllerInstance.BaseAddress, XIN_SVC_ALL_ISRS_OPTION );
\r
375 /* Install exception handlers. */
\r
376 #if MICROBLAZE_EXCEPTIONS_ENABLED == 1
\r
378 #if XPAR_MICROBLAZE_0_UNALIGNED_EXCEPTIONS == 1
\r
379 microblaze_register_exception_handler( XEXC_ID_UNALIGNED_ACCESS, prvExceptionHandler, ( void * ) XEXC_ID_UNALIGNED_ACCESS );
\r
380 #endif /* XPAR_MICROBLAZE_0_UNALIGNED_EXCEPTIONS*/
\r
382 #if XPAR_MICROBLAZE_0_ILL_OPCODE_EXCEPTION == 1
\r
383 microblaze_register_exception_handler( XEXC_ID_ILLEGAL_OPCODE, prvExceptionHandler, ( void * ) XEXC_ID_ILLEGAL_OPCODE );
\r
384 #endif /* XPAR_MICROBLAZE_0_ILL_OPCODE_EXCEPTION*/
\r
386 #if XPAR_MICROBLAZE_0_M_AXI_I_BUS_EXCEPTION == 1
\r
387 microblaze_register_exception_handler( XEXC_ID_M_AXI_I_EXCEPTION, prvExceptionHandler, ( void * ) XEXC_ID_M_AXI_I_EXCEPTION );
\r
388 #endif /* XPAR_MICROBLAZE_0_M_AXI_I_BUS_EXCEPTION*/
\r
390 #if XPAR_MICROBLAZE_0_M_AXI_D_BUS_EXCEPTION == 1
\r
391 microblaze_register_exception_handler( XEXC_ID_M_AXI_D_EXCEPTION, prvExceptionHandler, ( void * ) XEXC_ID_M_AXI_D_EXCEPTION );
\r
392 #endif /* XPAR_MICROBLAZE_0_M_AXI_D_BUS_EXCEPTION*/
\r
394 #if XPAR_MICROBLAZE_0_IPLB_BUS_EXCEPTION == 1
\r
395 microblaze_register_exception_handler( XEXC_ID_IPLB_EXCEPTION, prvExceptionHandler, ( void * ) XEXC_ID_IPLB_EXCEPTION );
\r
396 #endif /* XPAR_MICROBLAZE_0_IPLB_BUS_EXCEPTION*/
\r
398 #if XPAR_MICROBLAZE_0_DPLB_BUS_EXCEPTION == 1
\r
399 microblaze_register_exception_handler( XEXC_ID_DPLB_EXCEPTION, prvExceptionHandler, ( void * ) XEXC_ID_DPLB_EXCEPTION );
\r
400 #endif /* XPAR_MICROBLAZE_0_DPLB_BUS_EXCEPTION*/
\r
402 #if XPAR_MICROBLAZE_0_DIV_ZERO_EXCEPTION == 1
\r
403 microblaze_register_exception_handler( XEXC_ID_DIV_BY_ZERO, prvExceptionHandler, ( void * ) XEXC_ID_DIV_BY_ZERO );
\r
404 #endif /* XPAR_MICROBLAZE_0_DIV_ZERO_EXCEPTION*/
\r
406 #if XPAR_MICROBLAZE_0_FPU_EXCEPTION == 1
\r
407 microblaze_register_exception_handler( XEXC_ID_FPU, prvExceptionHandler, ( void * ) XEXC_ID_FPU );
\r
408 #endif /* XPAR_MICROBLAZE_0_FPU_EXCEPTION*/
\r
410 #if XPAR_MICROBLAZE_0_FSL_EXCEPTION == 1
\r
411 microblaze_register_exception_handler( XEXC_ID_FSL, prvExceptionHandler, ( void * ) XEXC_ID_FSL );
\r
412 #endif /* XPAR_MICROBLAZE_0_FSL_EXCEPTION*/
\r
414 #endif /* MICROBLAZE_EXCEPTIONS_ENABLED */
\r
416 /* Start the interrupt controller. Interrupts are enabled when the
\r
417 scheduler starts. */
\r
418 xStatus = XIntc_Start( &xInterruptControllerInstance, XIN_REAL_MODE );
\r
420 /* Ensure the compiler does not generate warnings for the unused
\r
421 iStatus valud if configASSERT() is not defined. */
\r
425 configASSERT( ( xStatus == ( portBASE_TYPE ) XST_SUCCESS ) )
\r
427 /*_RB_ Exception test code.
\r
429 "bralid r15, 1234 \n"
\r
436 /*-----------------------------------------------------------*/
\r