2 FreeRTOS V7.6.0 - Copyright (C) 2013 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 distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\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
80 #include <xintc_i.h>
\r
81 #include <xtmrctr.h>
\r
83 /* Tasks are started with interrupts enabled. */
\r
84 #define portINITIAL_MSR_STATE ( ( StackType_t ) 0x02 )
\r
86 /* Tasks are started with a critical section nesting of 0 - however prior
\r
87 to the scheduler being commenced we don't want the critical nesting level
\r
88 to reach zero, so it is initialised to a high value. */
\r
89 #define portINITIAL_NESTING_VALUE ( 0xff )
\r
91 /* Our hardware setup only uses one counter. */
\r
92 #define portCOUNTER_0 0
\r
94 /* The stack used by the ISR is filled with a known value to assist in
\r
96 #define portISR_STACK_FILL_VALUE 0x55555555
\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 UBaseType_t 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 uint32_t *pulISRStack;
\r
107 /*-----------------------------------------------------------*/
\r
110 * Sets up the periodic ISR used for the RTOS tick. This uses timer 0, but
\r
111 * could have alternatively used the watchdog timer or timer 1.
\r
113 static void prvSetupTimerInterrupt( void );
\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 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
124 extern void *_SDA2_BASE_, *_SDA_BASE_;
\r
125 const uint32_t ulR2 = ( uint32_t ) &_SDA2_BASE_;
\r
126 const uint32_t ulR13 = ( uint32_t ) &_SDA_BASE_;
\r
128 /* Place a few bytes of known values on the bottom of the stack.
\r
129 This is essential for the Microblaze port and these lines must
\r
130 not be omitted. The parameter value will overwrite the
\r
131 0x22222222 value during the function prologue. */
\r
132 *pxTopOfStack = ( StackType_t ) 0x11111111;
\r
134 *pxTopOfStack = ( StackType_t ) 0x22222222;
\r
136 *pxTopOfStack = ( StackType_t ) 0x33333333;
\r
139 /* First stack an initial value for the critical section nesting. This
\r
140 is initialised to zero as tasks are started with interrupts enabled. */
\r
141 *pxTopOfStack = ( StackType_t ) 0x00; /* R0. */
\r
143 /* Place an initial value for all the general purpose registers. */
\r
145 *pxTopOfStack = ( StackType_t ) ulR2; /* R2 - small data area. */
\r
147 *pxTopOfStack = ( StackType_t ) 0x03; /* R3. */
\r
149 *pxTopOfStack = ( StackType_t ) 0x04; /* R4. */
\r
151 *pxTopOfStack = ( StackType_t ) pvParameters;/* R5 contains the function call parameters. */
\r
153 *pxTopOfStack = ( StackType_t ) 0x06; /* R6. */
\r
155 *pxTopOfStack = ( StackType_t ) 0x07; /* R7. */
\r
157 *pxTopOfStack = ( StackType_t ) 0x08; /* R8. */
\r
159 *pxTopOfStack = ( StackType_t ) 0x09; /* R9. */
\r
161 *pxTopOfStack = ( StackType_t ) 0x0a; /* R10. */
\r
163 *pxTopOfStack = ( StackType_t ) 0x0b; /* R11. */
\r
165 *pxTopOfStack = ( StackType_t ) 0x0c; /* R12. */
\r
167 *pxTopOfStack = ( StackType_t ) ulR13; /* R13 - small data read write area. */
\r
169 *pxTopOfStack = ( StackType_t ) pxCode; /* R14. */
\r
171 *pxTopOfStack = ( StackType_t ) 0x0f; /* R15. */
\r
173 *pxTopOfStack = ( StackType_t ) 0x10; /* R16. */
\r
175 *pxTopOfStack = ( StackType_t ) 0x11; /* R17. */
\r
177 *pxTopOfStack = ( StackType_t ) 0x12; /* R18. */
\r
179 *pxTopOfStack = ( StackType_t ) 0x13; /* R19. */
\r
181 *pxTopOfStack = ( StackType_t ) 0x14; /* R20. */
\r
183 *pxTopOfStack = ( StackType_t ) 0x15; /* R21. */
\r
185 *pxTopOfStack = ( StackType_t ) 0x16; /* R22. */
\r
187 *pxTopOfStack = ( StackType_t ) 0x17; /* R23. */
\r
189 *pxTopOfStack = ( StackType_t ) 0x18; /* R24. */
\r
191 *pxTopOfStack = ( StackType_t ) 0x19; /* R25. */
\r
193 *pxTopOfStack = ( StackType_t ) 0x1a; /* R26. */
\r
195 *pxTopOfStack = ( StackType_t ) 0x1b; /* R27. */
\r
197 *pxTopOfStack = ( StackType_t ) 0x1c; /* R28. */
\r
199 *pxTopOfStack = ( StackType_t ) 0x1d; /* R29. */
\r
201 *pxTopOfStack = ( StackType_t ) 0x1e; /* R30. */
\r
204 /* The MSR is stacked between R30 and R31. */
\r
205 *pxTopOfStack = portINITIAL_MSR_STATE;
\r
208 *pxTopOfStack = ( StackType_t ) 0x1f; /* R31. */
\r
211 /* Return a pointer to the top of the stack we have generated so this can
\r
212 be stored in the task control block for the task. */
\r
213 return pxTopOfStack;
\r
215 /*-----------------------------------------------------------*/
\r
217 BaseType_t xPortStartScheduler( void )
\r
219 extern void ( __FreeRTOS_interrupt_Handler )( void );
\r
220 extern void ( vStartFirstTask )( void );
\r
223 /* Setup the FreeRTOS interrupt handler. Code copied from crt0.s. */
\r
224 asm volatile ( "la r6, r0, __FreeRTOS_interrupt_handler \n\t" \
\r
225 "sw r6, r1, r0 \n\t" \
\r
226 "lhu r7, r1, r0 \n\t" \
\r
227 "shi r7, r0, 0x12 \n\t" \
\r
228 "shi r6, r0, 0x16 " );
\r
230 /* Setup the hardware to generate the tick. Interrupts are disabled when
\r
231 this function is called. */
\r
232 prvSetupTimerInterrupt();
\r
234 /* Allocate the stack to be used by the interrupt handler. */
\r
235 pulISRStack = ( uint32_t * ) pvPortMalloc( configMINIMAL_STACK_SIZE * sizeof( StackType_t ) );
\r
237 /* Restore the context of the first task that is going to run. */
\r
238 if( pulISRStack != NULL )
\r
240 /* Fill the ISR stack with a known value to facilitate debugging. */
\r
241 memset( pulISRStack, portISR_STACK_FILL_VALUE, configMINIMAL_STACK_SIZE * sizeof( StackType_t ) );
\r
242 pulISRStack += ( configMINIMAL_STACK_SIZE - 1 );
\r
244 /* Kick off the first task. */
\r
248 /* Should not get here as the tasks are now running! */
\r
251 /*-----------------------------------------------------------*/
\r
253 void vPortEndScheduler( void )
\r
255 /* Not implemented. */
\r
257 /*-----------------------------------------------------------*/
\r
260 * Manual context switch called by portYIELD or taskYIELD.
\r
262 void vPortYield( void )
\r
264 extern void VPortYieldASM( void );
\r
266 /* Perform the context switch in a critical section to assure it is
\r
267 not interrupted by the tick ISR. It is not a problem to do this as
\r
268 each task maintains it's own interrupt status. */
\r
269 portENTER_CRITICAL();
\r
270 /* Jump directly to the yield function to ensure there is no
\r
271 compiler generated prologue code. */
\r
272 asm volatile ( "bralid r14, VPortYieldASM \n\t" \
\r
273 "or r0, r0, r0 \n\t" );
\r
274 portEXIT_CRITICAL();
\r
276 /*-----------------------------------------------------------*/
\r
279 * Hardware initialisation to generate the RTOS tick.
\r
281 static void prvSetupTimerInterrupt( void )
\r
284 const uint32_t ulCounterValue = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
\r
285 UBaseType_t uxMask;
\r
287 /* The OPB timer1 is used to generate the tick. Use the provided library
\r
288 functions to enable the timer and set the tick frequency. */
\r
289 XTmrCtr_mDisable( XPAR_OPB_TIMER_1_BASEADDR, XPAR_OPB_TIMER_1_DEVICE_ID );
\r
290 XTmrCtr_Initialize( &xTimer, XPAR_OPB_TIMER_1_DEVICE_ID );
\r
291 XTmrCtr_mSetLoadReg( XPAR_OPB_TIMER_1_BASEADDR, portCOUNTER_0, ulCounterValue );
\r
292 XTmrCtr_mSetControlStatusReg( XPAR_OPB_TIMER_1_BASEADDR, portCOUNTER_0, XTC_CSR_LOAD_MASK | XTC_CSR_INT_OCCURED_MASK );
\r
294 /* Set the timer interrupt enable bit while maintaining the other bit
\r
296 uxMask = XIntc_In32( ( XPAR_OPB_INTC_0_BASEADDR + XIN_IER_OFFSET ) );
\r
297 uxMask |= XPAR_OPB_TIMER_1_INTERRUPT_MASK;
\r
298 XIntc_Out32( ( XPAR_OPB_INTC_0_BASEADDR + XIN_IER_OFFSET ), ( uxMask ) );
\r
300 XTmrCtr_Start( &xTimer, XPAR_OPB_TIMER_1_DEVICE_ID );
\r
301 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
302 XIntc_mAckIntr( XPAR_INTC_SINGLE_BASEADDR, 1 );
\r
304 /*-----------------------------------------------------------*/
\r
307 * The interrupt handler placed in the interrupt vector when the scheduler is
\r
308 * started. The task context has already been saved when this is called.
\r
309 * This handler determines the interrupt source and calls the relevant
\r
310 * peripheral handler.
\r
312 void vTaskISRHandler( void )
\r
314 static uint32_t ulPending;
\r
316 /* Which interrupts are pending? */
\r
317 ulPending = XIntc_In32( ( XPAR_INTC_SINGLE_BASEADDR + XIN_IVR_OFFSET ) );
\r
319 if( ulPending < XPAR_INTC_MAX_NUM_INTR_INPUTS )
\r
321 static XIntc_VectorTableEntry *pxTablePtr;
\r
322 static XIntc_Config *pxConfig;
\r
323 static uint32_t ulInterruptMask;
\r
325 ulInterruptMask = ( uint32_t ) 1 << ulPending;
\r
327 /* Get the configuration data using the device ID */
\r
328 pxConfig = &XIntc_ConfigTable[ ( uint32_t ) XPAR_INTC_SINGLE_DEVICE_ID ];
\r
330 pxTablePtr = &( pxConfig->HandlerTable[ ulPending ] );
\r
331 if( pxConfig->AckBeforeService & ( ulInterruptMask ) )
\r
333 XIntc_mAckIntr( pxConfig->BaseAddress, ulInterruptMask );
\r
334 pxTablePtr->Handler( pxTablePtr->CallBackRef );
\r
338 pxTablePtr->Handler( pxTablePtr->CallBackRef );
\r
339 XIntc_mAckIntr( pxConfig->BaseAddress, ulInterruptMask );
\r
343 /*-----------------------------------------------------------*/
\r
346 * Handler for the timer interrupt.
\r
348 void vTickISR( void *pvBaseAddress )
\r
352 /* Increment the RTOS tick - this might cause a task to unblock. */
\r
353 if( xTaskIncrementTick() != pdFALSE )
\r
355 vTaskSwitchContext();
\r
358 /* Clear the timer interrupt */
\r
359 ulCSR = XTmrCtr_mGetControlStatusReg(XPAR_OPB_TIMER_1_BASEADDR, 0);
\r
360 XTmrCtr_mSetControlStatusReg( XPAR_OPB_TIMER_1_BASEADDR, portCOUNTER_0, ulCSR );
\r
362 /*-----------------------------------------------------------*/
\r