2 FreeRTOS.org V5.0.2 - Copyright (C) 2003-2008 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 ***************************************************************************
\r
29 * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
\r
30 * and even write all or part of your application on your behalf. *
\r
31 * See http://www.OpenRTOS.com for details of the services we provide to *
\r
32 * expedite your project. *
\r
34 ***************************************************************************
\r
35 ***************************************************************************
\r
37 Please ensure to read the configuration and relevant port sections of the
\r
38 online documentation.
\r
40 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
43 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
46 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
47 licensing and training services.
\r
50 /*-----------------------------------------------------------
\r
51 * Implementation of functions defined in portable.h for the MicroBlaze port.
\r
52 *----------------------------------------------------------*/
\r
55 /* Scheduler includes. */
\r
56 #include "FreeRTOS.h"
\r
59 /* Standard includes. */
\r
62 /* Hardware includes. */
\r
64 #include <xintc_i.h>
\r
65 #include <xtmrctr.h>
\r
67 /* Tasks are started with interrupts enabled. */
\r
68 #define portINITIAL_MSR_STATE ( ( portSTACK_TYPE ) 0x02 )
\r
70 /* Tasks are started with a critical section nesting of 0 - however prior
\r
71 to the scheduler being commenced we don't want the critical nesting level
\r
72 to reach zero, so it is initialised to a high value. */
\r
73 #define portINITIAL_NESTING_VALUE ( 0xff )
\r
75 /* Our hardware setup only uses one counter. */
\r
76 #define portCOUNTER_0 0
\r
78 /* The stack used by the ISR is filled with a known value to assist in
\r
80 #define portISR_STACK_FILL_VALUE 0x55555555
\r
82 /* Counts the nesting depth of calls to portENTER_CRITICAL(). Each task
\r
83 maintains it's own count, so this variable is saved as part of the task
\r
85 volatile unsigned portBASE_TYPE uxCriticalNesting = portINITIAL_NESTING_VALUE;
\r
87 /* To limit the amount of stack required by each task, this port uses a
\r
88 separate stack for interrupts. */
\r
89 unsigned portLONG *pulISRStack;
\r
91 /*-----------------------------------------------------------*/
\r
94 * Sets up the periodic ISR used for the RTOS tick. This uses timer 0, but
\r
95 * could have alternatively used the watchdog timer or timer 1.
\r
97 static void prvSetupTimerInterrupt( void );
\r
98 /*-----------------------------------------------------------*/
\r
101 * Initialise the stack of a task to look exactly as if a call to
\r
102 * portSAVE_CONTEXT had been made.
\r
104 * See the header file portable.h.
\r
106 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
108 extern void *_SDA2_BASE_, *_SDA_BASE_;
\r
109 const unsigned portLONG ulR2 = ( unsigned portLONG ) &_SDA2_BASE_;
\r
110 const unsigned portLONG ulR13 = ( unsigned portLONG ) &_SDA_BASE_;
\r
112 /* Place a few bytes of known values on the bottom of the stack.
\r
113 This is essential for the Microblaze port and these lines must
\r
114 not be omitted. The parameter value will overwrite the
\r
115 0x22222222 value during the function prologue. */
\r
116 *pxTopOfStack = ( portSTACK_TYPE ) 0x11111111;
\r
118 *pxTopOfStack = ( portSTACK_TYPE ) 0x22222222;
\r
120 *pxTopOfStack = ( portSTACK_TYPE ) 0x33333333;
\r
123 /* First stack an initial value for the critical section nesting. This
\r
124 is initialised to zero as tasks are started with interrupts enabled. */
\r
125 *pxTopOfStack = ( portSTACK_TYPE ) 0x00; /* R0. */
\r
127 /* Place an initial value for all the general purpose registers. */
\r
129 *pxTopOfStack = ( portSTACK_TYPE ) ulR2; /* R2 - small data area. */
\r
131 *pxTopOfStack = ( portSTACK_TYPE ) 0x03; /* R3. */
\r
133 *pxTopOfStack = ( portSTACK_TYPE ) 0x04; /* R4. */
\r
135 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;/* R5 contains the function call parameters. */
\r
137 *pxTopOfStack = ( portSTACK_TYPE ) 0x06; /* R6. */
\r
139 *pxTopOfStack = ( portSTACK_TYPE ) 0x07; /* R7. */
\r
141 *pxTopOfStack = ( portSTACK_TYPE ) 0x08; /* R8. */
\r
143 *pxTopOfStack = ( portSTACK_TYPE ) 0x09; /* R9. */
\r
145 *pxTopOfStack = ( portSTACK_TYPE ) 0x0a; /* R10. */
\r
147 *pxTopOfStack = ( portSTACK_TYPE ) 0x0b; /* R11. */
\r
149 *pxTopOfStack = ( portSTACK_TYPE ) 0x0c; /* R12. */
\r
151 *pxTopOfStack = ( portSTACK_TYPE ) ulR13; /* R13 - small data read write area. */
\r
153 *pxTopOfStack = ( portSTACK_TYPE ) pxCode; /* R14. */
\r
155 *pxTopOfStack = ( portSTACK_TYPE ) 0x0f; /* R15. */
\r
157 *pxTopOfStack = ( portSTACK_TYPE ) 0x10; /* R16. */
\r
159 *pxTopOfStack = ( portSTACK_TYPE ) 0x11; /* R17. */
\r
161 *pxTopOfStack = ( portSTACK_TYPE ) 0x12; /* R18. */
\r
163 *pxTopOfStack = ( portSTACK_TYPE ) 0x13; /* R19. */
\r
165 *pxTopOfStack = ( portSTACK_TYPE ) 0x14; /* R20. */
\r
167 *pxTopOfStack = ( portSTACK_TYPE ) 0x15; /* R21. */
\r
169 *pxTopOfStack = ( portSTACK_TYPE ) 0x16; /* R22. */
\r
171 *pxTopOfStack = ( portSTACK_TYPE ) 0x17; /* R23. */
\r
173 *pxTopOfStack = ( portSTACK_TYPE ) 0x18; /* R24. */
\r
175 *pxTopOfStack = ( portSTACK_TYPE ) 0x19; /* R25. */
\r
177 *pxTopOfStack = ( portSTACK_TYPE ) 0x1a; /* R26. */
\r
179 *pxTopOfStack = ( portSTACK_TYPE ) 0x1b; /* R27. */
\r
181 *pxTopOfStack = ( portSTACK_TYPE ) 0x1c; /* R28. */
\r
183 *pxTopOfStack = ( portSTACK_TYPE ) 0x1d; /* R29. */
\r
185 *pxTopOfStack = ( portSTACK_TYPE ) 0x1e; /* R30. */
\r
188 /* The MSR is stacked between R30 and R31. */
\r
189 *pxTopOfStack = portINITIAL_MSR_STATE;
\r
192 *pxTopOfStack = ( portSTACK_TYPE ) 0x1f; /* R31. */
\r
195 /* Return a pointer to the top of the stack we have generated so this can
\r
196 be stored in the task control block for the task. */
\r
197 return pxTopOfStack;
\r
199 /*-----------------------------------------------------------*/
\r
201 portBASE_TYPE xPortStartScheduler( void )
\r
203 extern void ( __FreeRTOS_interrupt_Handler )( void );
\r
204 extern void ( vStartFirstTask )( void );
\r
207 /* Setup the FreeRTOS interrupt handler. Code copied from crt0.s. */
\r
208 asm volatile ( "la r6, r0, __FreeRTOS_interrupt_handler \n\t" \
\r
209 "sw r6, r1, r0 \n\t" \
\r
210 "lhu r7, r1, r0 \n\t" \
\r
211 "shi r7, r0, 0x12 \n\t" \
\r
212 "shi r6, r0, 0x16 " );
\r
214 /* Setup the hardware to generate the tick. Interrupts are disabled when
\r
215 this function is called. */
\r
216 prvSetupTimerInterrupt();
\r
218 /* Allocate the stack to be used by the interrupt handler. */
\r
219 pulISRStack = ( unsigned portLONG * ) pvPortMalloc( configMINIMAL_STACK_SIZE * sizeof( portSTACK_TYPE ) );
\r
221 /* Restore the context of the first task that is going to run. */
\r
222 if( pulISRStack != NULL )
\r
224 /* Fill the ISR stack with a known value to facilitate debugging. */
\r
225 memset( pulISRStack, portISR_STACK_FILL_VALUE, configMINIMAL_STACK_SIZE * sizeof( portSTACK_TYPE ) );
\r
226 pulISRStack += ( configMINIMAL_STACK_SIZE - 1 );
\r
228 /* Kick off the first task. */
\r
232 /* Should not get here as the tasks are now running! */
\r
235 /*-----------------------------------------------------------*/
\r
237 void vPortEndScheduler( void )
\r
239 /* Not implemented. */
\r
241 /*-----------------------------------------------------------*/
\r
244 * Manual context switch called by portYIELD or taskYIELD.
\r
246 void vPortYield( void )
\r
248 extern void VPortYieldASM( void );
\r
250 /* Perform the context switch in a critical section to assure it is
\r
251 not interrupted by the tick ISR. It is not a problem to do this as
\r
252 each task maintains it's own interrupt status. */
\r
253 portENTER_CRITICAL();
\r
254 /* Jump directly to the yield function to ensure there is no
\r
255 compiler generated prologue code. */
\r
256 asm volatile ( "bralid r14, VPortYieldASM \n\t" \
\r
257 "or r0, r0, r0 \n\t" );
\r
258 portEXIT_CRITICAL();
\r
260 /*-----------------------------------------------------------*/
\r
263 * Hardware initialisation to generate the RTOS tick.
\r
265 static void prvSetupTimerInterrupt( void )
\r
268 const unsigned portLONG ulCounterValue = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
\r
269 unsigned portBASE_TYPE uxMask;
\r
271 /* The OPB timer1 is used to generate the tick. Use the provided library
\r
272 functions to enable the timer and set the tick frequency. */
\r
273 XTmrCtr_mDisable( XPAR_OPB_TIMER_1_BASEADDR, XPAR_OPB_TIMER_1_DEVICE_ID );
\r
274 XTmrCtr_Initialize( &xTimer, XPAR_OPB_TIMER_1_DEVICE_ID );
\r
275 XTmrCtr_mSetLoadReg( XPAR_OPB_TIMER_1_BASEADDR, portCOUNTER_0, ulCounterValue );
\r
276 XTmrCtr_mSetControlStatusReg( XPAR_OPB_TIMER_1_BASEADDR, portCOUNTER_0, XTC_CSR_LOAD_MASK | XTC_CSR_INT_OCCURED_MASK );
\r
278 /* Set the timer interrupt enable bit while maintaining the other bit
\r
280 uxMask = XIntc_In32( ( XPAR_OPB_INTC_0_BASEADDR + XIN_IER_OFFSET ) );
\r
281 uxMask |= XPAR_OPB_TIMER_1_INTERRUPT_MASK;
\r
282 XIntc_Out32( ( XPAR_OPB_INTC_0_BASEADDR + XIN_IER_OFFSET ), ( uxMask ) );
\r
284 XTmrCtr_Start( &xTimer, XPAR_OPB_TIMER_1_DEVICE_ID );
\r
285 XTmrCtr_mSetControlStatusReg(XPAR_OPB_TIMER_1_BASEADDR, portCOUNTER_0, XTC_CSR_ENABLE_TMR_MASK | XTC_CSR_ENABLE_INT_MASK | XTC_CSR_AUTO_RELOAD_MASK | XTC_CSR_DOWN_COUNT_MASK | XTC_CSR_INT_OCCURED_MASK );
\r
286 XIntc_mAckIntr( XPAR_INTC_SINGLE_BASEADDR, 1 );
\r
288 /*-----------------------------------------------------------*/
\r
291 * The interrupt handler placed in the interrupt vector when the scheduler is
\r
292 * started. The task context has already been saved when this is called.
\r
293 * This handler determines the interrupt source and calls the relevant
\r
294 * peripheral handler.
\r
296 void vTaskISRHandler( void )
\r
298 static unsigned portLONG ulPending;
\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
305 static XIntc_VectorTableEntry *pxTablePtr;
\r
306 static XIntc_Config *pxConfig;
\r
307 static unsigned portLONG ulInterruptMask;
\r
309 ulInterruptMask = ( unsigned portLONG ) 1 << ulPending;
\r
311 /* Get the configuration data using the device ID */
\r
312 pxConfig = &XIntc_ConfigTable[ ( unsigned portLONG ) XPAR_INTC_SINGLE_DEVICE_ID ];
\r
314 pxTablePtr = &( pxConfig->HandlerTable[ ulPending ] );
\r
315 if( pxConfig->AckBeforeService & ( ulInterruptMask ) )
\r
317 XIntc_mAckIntr( pxConfig->BaseAddress, ulInterruptMask );
\r
318 pxTablePtr->Handler( pxTablePtr->CallBackRef );
\r
322 pxTablePtr->Handler( pxTablePtr->CallBackRef );
\r
323 XIntc_mAckIntr( pxConfig->BaseAddress, ulInterruptMask );
\r
327 /*-----------------------------------------------------------*/
\r
330 * Handler for the timer interrupt.
\r
332 void vTickISR( void *pvBaseAddress )
\r
334 unsigned portLONG ulCSR;
\r
336 /* Increment the RTOS tick - this might cause a task to unblock. */
\r
337 vTaskIncrementTick();
\r
339 /* Clear the timer interrupt */
\r
340 ulCSR = XTmrCtr_mGetControlStatusReg(XPAR_OPB_TIMER_1_BASEADDR, 0);
\r
341 XTmrCtr_mSetControlStatusReg( XPAR_OPB_TIMER_1_BASEADDR, portCOUNTER_0, ulCSR );
\r
343 /* If we are using the preemptive scheduler then we also need to determine
\r
344 if this tick should cause a context switch. */
\r
345 #if configUSE_PREEMPTION == 1
\r
346 vTaskSwitchContext();
\r
349 /*-----------------------------------------------------------*/
\r