2 FreeRTOS V8.0.1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
28 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
29 >>! obliged to provide the source code for proprietary components !<<
\r
30 >>! outside of the FreeRTOS kernel. !<<
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
66 /*-----------------------------------------------------------
\r
67 * Implementation of functions defined in portable.h for the MicroBlaze port.
\r
68 *----------------------------------------------------------*/
\r
71 /* Scheduler includes. */
\r
72 #include "FreeRTOS.h"
\r
75 /* Standard includes. */
\r
78 /* Hardware includes. */
\r
79 #include <xintc_i.h>
\r
80 #include <xil_exception.h>
\r
81 #include <microblaze_exceptions_g.h>
\r
83 /* Tasks are started with a critical section nesting of 0 - however, prior to
\r
84 the scheduler being commenced interrupts should not be enabled, so the critical
\r
85 nesting variable is initialised to a non-zero value. */
\r
86 #define portINITIAL_NESTING_VALUE ( 0xff )
\r
88 /* The bit within the MSR register that enabled/disables interrupts. */
\r
89 #define portMSR_IE ( 0x02U )
\r
91 /* If the floating point unit is included in the MicroBlaze build, then the
\r
92 FSR register is saved as part of the task context. portINITIAL_FSR is the value
\r
93 given to the FSR register when the initial context is set up for a task being
\r
95 #define portINITIAL_FSR ( 0U )
\r
96 /*-----------------------------------------------------------*/
\r
99 * Initialise the interrupt controller instance.
\r
101 static int32_t prvInitialiseInterruptController( void );
\r
103 /* Ensure the interrupt controller instance variable is initialised before it is
\r
104 * used, and that the initialisation only happens once.
\r
106 static int32_t prvEnsureInterruptControllerIsInitialised( void );
\r
108 /*-----------------------------------------------------------*/
\r
110 /* Counts the nesting depth of calls to portENTER_CRITICAL(). Each task
\r
111 maintains its own count, so this variable is saved as part of the task
\r
113 volatile UBaseType_t uxCriticalNesting = portINITIAL_NESTING_VALUE;
\r
115 /* This port uses a separate stack for interrupts. This prevents the stack of
\r
116 every task needing to be large enough to hold an entire interrupt stack on top
\r
117 of the task stack. */
\r
118 uint32_t *pulISRStack;
\r
120 /* If an interrupt requests a context switch, then ulTaskSwitchRequested will
\r
121 get set to 1. ulTaskSwitchRequested is inspected just before the main interrupt
\r
122 handler exits. If, at that time, ulTaskSwitchRequested is set to 1, the kernel
\r
123 will call vTaskSwitchContext() to ensure the task that runs immediately after
\r
124 the interrupt exists is the highest priority task that is able to run. This is
\r
125 an unusual mechanism, but is used for this port because a single interrupt can
\r
126 cause the servicing of multiple peripherals - and it is inefficient to call
\r
127 vTaskSwitchContext() multiple times as each peripheral is serviced. */
\r
128 volatile uint32_t ulTaskSwitchRequested = 0UL;
\r
130 /* The instance of the interrupt controller used by this port. This is required
\r
131 by the Xilinx library API functions. */
\r
132 static XIntc xInterruptControllerInstance;
\r
134 /*-----------------------------------------------------------*/
\r
137 * Initialise the stack of a task to look exactly as if a call to
\r
138 * portSAVE_CONTEXT had been made.
\r
140 * See the portable.h header file.
\r
142 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
144 extern void *_SDA2_BASE_, *_SDA_BASE_;
\r
145 const uint32_t ulR2 = ( uint32_t ) &_SDA2_BASE_;
\r
146 const uint32_t ulR13 = ( uint32_t ) &_SDA_BASE_;
\r
148 /* Place a few bytes of known values on the bottom of the stack.
\r
149 This is essential for the Microblaze port and these lines must
\r
151 *pxTopOfStack = ( StackType_t ) 0x00000000;
\r
153 *pxTopOfStack = ( StackType_t ) 0x00000000;
\r
155 *pxTopOfStack = ( StackType_t ) 0x00000000;
\r
158 #if XPAR_MICROBLAZE_0_USE_FPU == 1
\r
159 /* The FSR value placed in the initial task context is just 0. */
\r
160 *pxTopOfStack = portINITIAL_FSR;
\r
164 /* The MSR value placed in the initial task context should have interrupts
\r
165 disabled. Each task will enable interrupts automatically when it enters
\r
166 the running state for the first time. */
\r
167 *pxTopOfStack = mfmsr() & ~portMSR_IE;
\r
170 /* First stack an initial value for the critical section nesting. This
\r
171 is initialised to zero. */
\r
172 *pxTopOfStack = ( StackType_t ) 0x00;
\r
174 /* R0 is always zero. */
\r
175 /* R1 is the SP. */
\r
177 /* Place an initial value for all the general purpose registers. */
\r
179 *pxTopOfStack = ( StackType_t ) ulR2; /* R2 - read only small data area. */
\r
181 *pxTopOfStack = ( StackType_t ) 0x03; /* R3 - return values and temporaries. */
\r
183 *pxTopOfStack = ( StackType_t ) 0x04; /* R4 - return values and temporaries. */
\r
185 *pxTopOfStack = ( StackType_t ) pvParameters;/* R5 contains the function call parameters. */
\r
187 #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING
\r
189 *pxTopOfStack = ( StackType_t ) 0x06; /* R6 - other parameters and temporaries. Used as the return address from vPortTaskEntryPoint. */
\r
191 *pxTopOfStack = ( StackType_t ) 0x07; /* R7 - other parameters and temporaries. */
\r
193 *pxTopOfStack = ( StackType_t ) 0x08; /* R8 - other parameters and temporaries. */
\r
195 *pxTopOfStack = ( StackType_t ) 0x09; /* R9 - other parameters and temporaries. */
\r
197 *pxTopOfStack = ( StackType_t ) 0x0a; /* R10 - other parameters and temporaries. */
\r
199 *pxTopOfStack = ( StackType_t ) 0x0b; /* R11 - temporaries. */
\r
201 *pxTopOfStack = ( StackType_t ) 0x0c; /* R12 - temporaries. */
\r
207 *pxTopOfStack = ( StackType_t ) ulR13; /* R13 - read/write small data area. */
\r
209 *pxTopOfStack = ( StackType_t ) pxCode; /* R14 - return address for interrupt. */
\r
211 *pxTopOfStack = ( StackType_t ) NULL; /* R15 - return address for subroutine. */
\r
213 #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING
\r
215 *pxTopOfStack = ( StackType_t ) 0x10; /* R16 - return address for trap (debugger). */
\r
217 *pxTopOfStack = ( StackType_t ) 0x11; /* R17 - return address for exceptions, if configured. */
\r
219 *pxTopOfStack = ( StackType_t ) 0x12; /* R18 - reserved for assembler and compiler temporaries. */
\r
225 *pxTopOfStack = ( StackType_t ) 0x00; /* R19 - must be saved across function calls. Callee-save. Seems to be interpreted as the frame pointer. */
\r
227 #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING
\r
229 *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
231 *pxTopOfStack = ( StackType_t ) 0x15; /* R21 - must be saved across function calls. Callee-save. */
\r
233 *pxTopOfStack = ( StackType_t ) 0x16; /* R22 - must be saved across function calls. Callee-save. */
\r
235 *pxTopOfStack = ( StackType_t ) 0x17; /* R23 - must be saved across function calls. Callee-save. */
\r
237 *pxTopOfStack = ( StackType_t ) 0x18; /* R24 - must be saved across function calls. Callee-save. */
\r
239 *pxTopOfStack = ( StackType_t ) 0x19; /* R25 - must be saved across function calls. Callee-save. */
\r
241 *pxTopOfStack = ( StackType_t ) 0x1a; /* R26 - must be saved across function calls. Callee-save. */
\r
243 *pxTopOfStack = ( StackType_t ) 0x1b; /* R27 - must be saved across function calls. Callee-save. */
\r
245 *pxTopOfStack = ( StackType_t ) 0x1c; /* R28 - must be saved across function calls. Callee-save. */
\r
247 *pxTopOfStack = ( StackType_t ) 0x1d; /* R29 - must be saved across function calls. Callee-save. */
\r
249 *pxTopOfStack = ( StackType_t ) 0x1e; /* R30 - must be saved across function calls. Callee-save. */
\r
251 *pxTopOfStack = ( StackType_t ) 0x1f; /* R31 - must be saved across function calls. Callee-save. */
\r
254 pxTopOfStack -= 13;
\r
257 /* Return a pointer to the top of the stack that has been generated so this
\r
258 can be stored in the task control block for the task. */
\r
259 return pxTopOfStack;
\r
261 /*-----------------------------------------------------------*/
\r
263 BaseType_t xPortStartScheduler( void )
\r
265 extern void ( vPortStartFirstTask )( void );
\r
266 extern uint32_t _stack[];
\r
268 /* Setup the hardware to generate the tick. Interrupts are disabled when
\r
269 this function is called.
\r
271 This port uses an application defined callback function to install the tick
\r
272 interrupt handler because the kernel will run on lots of different
\r
273 MicroBlaze and FPGA configurations - not all of which will have the same
\r
274 timer peripherals defined or available. An example definition of
\r
275 vApplicationSetupTimerInterrupt() is provided in the official demo
\r
276 application that accompanies this port. */
\r
277 vApplicationSetupTimerInterrupt();
\r
279 /* Reuse the stack from main() as the stack for the interrupts/exceptions. */
\r
280 pulISRStack = ( uint32_t * ) _stack;
\r
282 /* Ensure there is enough space for the functions called from the interrupt
\r
283 service routines to write back into the stack frame of the caller. */
\r
286 /* Restore the context of the first task that is going to run. From here
\r
287 on, the created tasks will be executing. */
\r
288 vPortStartFirstTask();
\r
290 /* Should not get here as the tasks are now running! */
\r
293 /*-----------------------------------------------------------*/
\r
295 void vPortEndScheduler( void )
\r
297 /* Not implemented in ports where there is nothing to return to.
\r
298 Artificially force an assert. */
\r
299 configASSERT( uxCriticalNesting == 1000UL );
\r
301 /*-----------------------------------------------------------*/
\r
304 * Manual context switch called by portYIELD or taskYIELD.
\r
306 void vPortYield( void )
\r
308 extern void VPortYieldASM( void );
\r
310 /* Perform the context switch in a critical section to assure it is
\r
311 not interrupted by the tick ISR. It is not a problem to do this as
\r
312 each task maintains its own interrupt status. */
\r
313 portENTER_CRITICAL();
\r
315 /* Jump directly to the yield function to ensure there is no
\r
316 compiler generated prologue code. */
\r
317 asm volatile ( "bralid r14, VPortYieldASM \n\t" \
\r
318 "or r0, r0, r0 \n\t" );
\r
320 portEXIT_CRITICAL();
\r
322 /*-----------------------------------------------------------*/
\r
324 void vPortEnableInterrupt( uint8_t ucInterruptID )
\r
328 /* An API function is provided to enable an interrupt in the interrupt
\r
329 controller because the interrupt controller instance variable is private
\r
331 lReturn = prvEnsureInterruptControllerIsInitialised();
\r
332 if( lReturn == pdPASS )
\r
334 XIntc_Enable( &xInterruptControllerInstance, ucInterruptID );
\r
337 configASSERT( lReturn );
\r
339 /*-----------------------------------------------------------*/
\r
341 void vPortDisableInterrupt( uint8_t ucInterruptID )
\r
345 /* An API function is provided to disable an interrupt in the interrupt
\r
346 controller because the interrupt controller instance variable is private
\r
348 lReturn = prvEnsureInterruptControllerIsInitialised();
\r
350 if( lReturn == pdPASS )
\r
352 XIntc_Disable( &xInterruptControllerInstance, ucInterruptID );
\r
355 configASSERT( lReturn );
\r
357 /*-----------------------------------------------------------*/
\r
359 BaseType_t xPortInstallInterruptHandler( uint8_t ucInterruptID, XInterruptHandler pxHandler, void *pvCallBackRef )
\r
363 /* An API function is provided to install an interrupt handler because the
\r
364 interrupt controller instance variable is private to this file. */
\r
366 lReturn = prvEnsureInterruptControllerIsInitialised();
\r
368 if( lReturn == pdPASS )
\r
370 lReturn = XIntc_Connect( &xInterruptControllerInstance, ucInterruptID, pxHandler, pvCallBackRef );
\r
373 if( lReturn == XST_SUCCESS )
\r
378 configASSERT( lReturn == pdPASS );
\r
382 /*-----------------------------------------------------------*/
\r
384 static int32_t prvEnsureInterruptControllerIsInitialised( void )
\r
386 static int32_t lInterruptControllerInitialised = pdFALSE;
\r
389 /* Ensure the interrupt controller instance variable is initialised before
\r
390 it is used, and that the initialisation only happens once. */
\r
391 if( lInterruptControllerInitialised != pdTRUE )
\r
393 lReturn = prvInitialiseInterruptController();
\r
395 if( lReturn == pdPASS )
\r
397 lInterruptControllerInitialised = pdTRUE;
\r
407 /*-----------------------------------------------------------*/
\r
410 * Handler for the timer interrupt. This is the handler that the application
\r
411 * defined callback function vApplicationSetupTimerInterrupt() should install.
\r
413 void vPortTickISR( void *pvUnused )
\r
415 extern void vApplicationClearTimerInterrupt( void );
\r
417 /* Ensure the unused parameter does not generate a compiler warning. */
\r
420 /* This port uses an application defined callback function to clear the tick
\r
421 interrupt because the kernel will run on lots of different MicroBlaze and
\r
422 FPGA configurations - not all of which will have the same timer peripherals
\r
423 defined or available. An example definition of
\r
424 vApplicationClearTimerInterrupt() is provided in the official demo
\r
425 application that accompanies this port. */
\r
426 vApplicationClearTimerInterrupt();
\r
428 /* Increment the RTOS tick - this might cause a task to unblock. */
\r
429 if( xTaskIncrementTick() != pdFALSE )
\r
431 /* Force vTaskSwitchContext() to be called as the interrupt exits. */
\r
432 ulTaskSwitchRequested = 1;
\r
435 /*-----------------------------------------------------------*/
\r
437 static int32_t prvInitialiseInterruptController( void )
\r
441 lStatus = XIntc_Initialize( &xInterruptControllerInstance, configINTERRUPT_CONTROLLER_TO_USE );
\r
443 if( lStatus == XST_SUCCESS )
\r
445 /* Initialise the exception table. */
\r
446 Xil_ExceptionInit();
\r
448 /* Service all pending interrupts each time the handler is entered. */
\r
449 XIntc_SetIntrSvcOption( xInterruptControllerInstance.BaseAddress, XIN_SVC_ALL_ISRS_OPTION );
\r
451 /* Install exception handlers if the MicroBlaze is configured to handle
\r
452 exceptions, and the application defined constant
\r
453 configINSTALL_EXCEPTION_HANDLERS is set to 1. */
\r
454 #if ( MICROBLAZE_EXCEPTIONS_ENABLED == 1 ) && ( configINSTALL_EXCEPTION_HANDLERS == 1 )
\r
456 vPortExceptionsInstallHandlers();
\r
458 #endif /* MICROBLAZE_EXCEPTIONS_ENABLED */
\r
460 /* Start the interrupt controller. Interrupts are enabled when the
\r
461 scheduler starts. */
\r
462 lStatus = XIntc_Start( &xInterruptControllerInstance, XIN_REAL_MODE );
\r
464 if( lStatus == XST_SUCCESS )
\r
474 configASSERT( lStatus == pdPASS );
\r
478 /*-----------------------------------------------------------*/
\r