2 * FreeRTOS Kernel V11.2.0
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
29 /*-----------------------------------------------------------
30 * Implementation of functions defined in portable.h for the MicroBlaze port.
31 *----------------------------------------------------------*/
34 /* Scheduler includes. */
38 /* Standard includes. */
41 /* Hardware includes. */
46 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 0 )
47 #error configSUPPORT_DYNAMIC_ALLOCATION must be set to 1 to use this port.
50 /* Tasks are started with interrupts enabled. */
51 #define portINITIAL_MSR_STATE ( ( StackType_t ) 0x02 )
53 /* Tasks are started with a critical section nesting of 0 - however prior
54 * to the scheduler being commenced we don't want the critical nesting level
55 * to reach zero, so it is initialised to a high value. */
56 #define portINITIAL_NESTING_VALUE ( 0xff )
58 /* Our hardware setup only uses one counter. */
59 #define portCOUNTER_0 0
61 /* The stack used by the ISR is filled with a known value to assist in
63 #define portISR_STACK_FILL_VALUE 0x55555555
65 /* Counts the nesting depth of calls to portENTER_CRITICAL(). Each task
66 * maintains it's own count, so this variable is saved as part of the task
68 volatile UBaseType_t uxCriticalNesting = portINITIAL_NESTING_VALUE;
70 /* To limit the amount of stack required by each task, this port uses a
71 * separate stack for interrupts. */
72 uint32_t * pulISRStack;
74 /*-----------------------------------------------------------*/
77 * Sets up the periodic ISR used for the RTOS tick. This uses timer 0, but
78 * could have alternatively used the watchdog timer or timer 1.
80 static void prvSetupTimerInterrupt( void );
81 /*-----------------------------------------------------------*/
84 * Initialise the stack of a task to look exactly as if a call to
85 * portSAVE_CONTEXT had been made.
87 * See the header file portable.h.
89 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
90 TaskFunction_t pxCode,
93 extern void * _SDA2_BASE_;
94 extern void * _SDA_BASE_;
95 const uint32_t ulR2 = ( uint32_t ) &_SDA2_BASE_;
96 const uint32_t ulR13 = ( uint32_t ) &_SDA_BASE_;
98 /* Place a few bytes of known values on the bottom of the stack.
99 * This is essential for the Microblaze port and these lines must
100 * not be omitted. The parameter value will overwrite the
101 * 0x22222222 value during the function prologue. */
102 *pxTopOfStack = ( StackType_t ) 0x11111111;
104 *pxTopOfStack = ( StackType_t ) 0x22222222;
106 *pxTopOfStack = ( StackType_t ) 0x33333333;
109 /* First stack an initial value for the critical section nesting. This
110 * is initialised to zero as tasks are started with interrupts enabled. */
111 *pxTopOfStack = ( StackType_t ) 0x00; /* R0. */
113 /* Place an initial value for all the general purpose registers. */
115 *pxTopOfStack = ( StackType_t ) ulR2; /* R2 - small data area. */
117 *pxTopOfStack = ( StackType_t ) 0x03; /* R3. */
119 *pxTopOfStack = ( StackType_t ) 0x04; /* R4. */
121 *pxTopOfStack = ( StackType_t ) pvParameters; /* R5 contains the function call parameters. */
123 *pxTopOfStack = ( StackType_t ) 0x06; /* R6. */
125 *pxTopOfStack = ( StackType_t ) 0x07; /* R7. */
127 *pxTopOfStack = ( StackType_t ) 0x08; /* R8. */
129 *pxTopOfStack = ( StackType_t ) 0x09; /* R9. */
131 *pxTopOfStack = ( StackType_t ) 0x0a; /* R10. */
133 *pxTopOfStack = ( StackType_t ) 0x0b; /* R11. */
135 *pxTopOfStack = ( StackType_t ) 0x0c; /* R12. */
137 *pxTopOfStack = ( StackType_t ) ulR13; /* R13 - small data read write area. */
139 *pxTopOfStack = ( StackType_t ) pxCode; /* R14. */
141 *pxTopOfStack = ( StackType_t ) 0x0f; /* R15. */
143 *pxTopOfStack = ( StackType_t ) 0x10; /* R16. */
145 *pxTopOfStack = ( StackType_t ) 0x11; /* R17. */
147 *pxTopOfStack = ( StackType_t ) 0x12; /* R18. */
149 *pxTopOfStack = ( StackType_t ) 0x13; /* R19. */
151 *pxTopOfStack = ( StackType_t ) 0x14; /* R20. */
153 *pxTopOfStack = ( StackType_t ) 0x15; /* R21. */
155 *pxTopOfStack = ( StackType_t ) 0x16; /* R22. */
157 *pxTopOfStack = ( StackType_t ) 0x17; /* R23. */
159 *pxTopOfStack = ( StackType_t ) 0x18; /* R24. */
161 *pxTopOfStack = ( StackType_t ) 0x19; /* R25. */
163 *pxTopOfStack = ( StackType_t ) 0x1a; /* R26. */
165 *pxTopOfStack = ( StackType_t ) 0x1b; /* R27. */
167 *pxTopOfStack = ( StackType_t ) 0x1c; /* R28. */
169 *pxTopOfStack = ( StackType_t ) 0x1d; /* R29. */
171 *pxTopOfStack = ( StackType_t ) 0x1e; /* R30. */
174 /* The MSR is stacked between R30 and R31. */
175 *pxTopOfStack = portINITIAL_MSR_STATE;
178 *pxTopOfStack = ( StackType_t ) 0x1f; /* R31. */
181 /* Return a pointer to the top of the stack we have generated so this can
182 * be stored in the task control block for the task. */
185 /*-----------------------------------------------------------*/
187 BaseType_t xPortStartScheduler( void )
189 extern void( __FreeRTOS_interrupt_Handler )( void );
190 extern void( vStartFirstTask )( void );
193 /* Setup the FreeRTOS interrupt handler. Code copied from crt0.s. */
194 asm volatile ( "la r6, r0, __FreeRTOS_interrupt_handler \n\t" \
195 "sw r6, r1, r0 \n\t" \
196 "lhu r7, r1, r0 \n\t" \
197 "shi r7, r0, 0x12 \n\t" \
198 "shi r6, r0, 0x16 " );
200 /* Setup the hardware to generate the tick. Interrupts are disabled when
201 * this function is called. */
202 prvSetupTimerInterrupt();
204 /* Allocate the stack to be used by the interrupt handler. */
205 pulISRStack = ( uint32_t * ) pvPortMalloc( configMINIMAL_STACK_SIZE * sizeof( StackType_t ) );
207 /* Restore the context of the first task that is going to run. */
208 if( pulISRStack != NULL )
210 /* Fill the ISR stack with a known value to facilitate debugging. */
211 memset( pulISRStack, portISR_STACK_FILL_VALUE, configMINIMAL_STACK_SIZE * sizeof( StackType_t ) );
212 pulISRStack += ( configMINIMAL_STACK_SIZE - 1 );
214 /* Kick off the first task. */
218 /* Should not get here as the tasks are now running! */
221 /*-----------------------------------------------------------*/
223 void vPortEndScheduler( void )
225 /* Not implemented. */
227 /*-----------------------------------------------------------*/
230 * Manual context switch called by portYIELD or taskYIELD.
232 void vPortYield( void )
234 extern void VPortYieldASM( void );
236 /* Perform the context switch in a critical section to assure it is
237 * not interrupted by the tick ISR. It is not a problem to do this as
238 * each task maintains it's own interrupt status. */
239 portENTER_CRITICAL();
241 /* Jump directly to the yield function to ensure there is no
242 * compiler generated prologue code. */
243 asm volatile ( "bralid r14, VPortYieldASM \n\t" \
244 "or r0, r0, r0 \n\t" );
247 /*-----------------------------------------------------------*/
250 * Hardware initialisation to generate the RTOS tick.
252 static void prvSetupTimerInterrupt( void )
255 const uint32_t ulCounterValue = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
258 /* The OPB timer1 is used to generate the tick. Use the provided library
259 * functions to enable the timer and set the tick frequency. */
260 XTmrCtr_mDisable( XPAR_OPB_TIMER_1_BASEADDR, XPAR_OPB_TIMER_1_DEVICE_ID );
261 XTmrCtr_Initialize( &xTimer, XPAR_OPB_TIMER_1_DEVICE_ID );
262 XTmrCtr_mSetLoadReg( XPAR_OPB_TIMER_1_BASEADDR, portCOUNTER_0, ulCounterValue );
263 XTmrCtr_mSetControlStatusReg( XPAR_OPB_TIMER_1_BASEADDR, portCOUNTER_0, XTC_CSR_LOAD_MASK | XTC_CSR_INT_occurred_MASK );
265 /* Set the timer interrupt enable bit while maintaining the other bit
267 uxMask = XIntc_In32( ( XPAR_OPB_INTC_0_BASEADDR + XIN_IER_OFFSET ) );
268 uxMask |= XPAR_OPB_TIMER_1_INTERRUPT_MASK;
269 XIntc_Out32( ( XPAR_OPB_INTC_0_BASEADDR + XIN_IER_OFFSET ), ( uxMask ) );
271 XTmrCtr_Start( &xTimer, XPAR_OPB_TIMER_1_DEVICE_ID );
272 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_occurred_MASK );
273 XIntc_mAckIntr( XPAR_INTC_SINGLE_BASEADDR, 1 );
275 /*-----------------------------------------------------------*/
278 * The interrupt handler placed in the interrupt vector when the scheduler is
279 * started. The task context has already been saved when this is called.
280 * This handler determines the interrupt source and calls the relevant
281 * peripheral handler.
283 void vTaskISRHandler( void )
285 static uint32_t ulPending;
287 /* Which interrupts are pending? */
288 ulPending = XIntc_In32( ( XPAR_INTC_SINGLE_BASEADDR + XIN_IVR_OFFSET ) );
290 if( ulPending < XPAR_INTC_MAX_NUM_INTR_INPUTS )
292 static XIntc_VectorTableEntry * pxTablePtr;
293 static XIntc_Config * pxConfig;
294 static uint32_t ulInterruptMask;
296 ulInterruptMask = ( uint32_t ) 1 << ulPending;
298 /* Get the configuration data using the device ID */
299 pxConfig = &XIntc_ConfigTable[ ( uint32_t ) XPAR_INTC_SINGLE_DEVICE_ID ];
301 pxTablePtr = &( pxConfig->HandlerTable[ ulPending ] );
303 if( pxConfig->AckBeforeService & ( ulInterruptMask ) )
305 XIntc_mAckIntr( pxConfig->BaseAddress, ulInterruptMask );
306 pxTablePtr->Handler( pxTablePtr->CallBackRef );
310 pxTablePtr->Handler( pxTablePtr->CallBackRef );
311 XIntc_mAckIntr( pxConfig->BaseAddress, ulInterruptMask );
315 /*-----------------------------------------------------------*/
318 * Handler for the timer interrupt.
320 void vTickISR( void * pvBaseAddress )
324 /* Increment the RTOS tick - this might cause a task to unblock. */
325 if( xTaskIncrementTick() != pdFALSE )
327 vTaskSwitchContext();
330 /* Clear the timer interrupt */
331 ulCSR = XTmrCtr_mGetControlStatusReg( XPAR_OPB_TIMER_1_BASEADDR, 0 );
332 XTmrCtr_mSetControlStatusReg( XPAR_OPB_TIMER_1_BASEADDR, portCOUNTER_0, ulCSR );
334 /*-----------------------------------------------------------*/