2 FreeRTOS V8.0.0:rc1 - 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 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
67 * This file initialises three timers as follows:
\r
69 * Timer 0 and Timer 1 provide the interrupts that are used with the IntQ
\r
70 * standard demo tasks, which test interrupt nesting and using queues from
\r
71 * interrupts. Both these interrupts operate below the maximum syscall
\r
72 * interrupt priority.
\r
74 * Timer 2 is a much higher frequency timer that tests the nesting of interrupts
\r
75 * that execute above the maximum syscall interrupt priority.
\r
77 * All the timers can nest with the tick interrupt - creating a maximum
\r
78 * interrupt nesting depth of 4.
\r
80 * For convenience, the high frequency timer is also used to provide the time
\r
81 * base for the run time stats.
\r
84 /* Scheduler includes. */
\r
85 #include "FreeRTOS.h"
\r
87 /* Demo includes. */
\r
88 #include "IntQueueTimer.h"
\r
89 #include "IntQueue.h"
\r
91 /* Xilinx includes. */
\r
93 #include "xscugic.h"
\r
95 /* The frequencies at which the first two timers expire are slightly offset to
\r
96 ensure they don't remain synchronised. The frequency of the interrupt that
\r
97 operates above the max syscall interrupt priority is 10 times faster so really
\r
98 hammers the interrupt entry and exit code. */
\r
99 #define tmrTIMERS_USED 3
\r
100 #define tmrTIMER_0_FREQUENCY ( 2000UL )
\r
101 #define tmrTIMER_1_FREQUENCY ( 2001UL )
\r
102 #define tmrTIMER_2_FREQUENCY ( 20000UL )
\r
104 /*-----------------------------------------------------------*/
\r
107 * The single interrupt service routines that is used to service all three
\r
110 static void prvTimerHandler( void *CallBackRef );
\r
112 /*-----------------------------------------------------------*/
\r
114 /* Hardware constants. */
\r
115 static const BaseType_t xDeviceIDs[ tmrTIMERS_USED ] = { XPAR_XTTCPS_0_DEVICE_ID, XPAR_XTTCPS_1_DEVICE_ID, XPAR_XTTCPS_2_DEVICE_ID };
\r
116 static const BaseType_t xInterruptIDs[ tmrTIMERS_USED ] = { XPAR_XTTCPS_0_INTR, XPAR_XTTCPS_1_INTR, XPAR_XTTCPS_2_INTR };
\r
118 /* Timer configuration settings. */
\r
121 uint32_t OutputHz; /* Output frequency. */
\r
122 uint16_t Interval; /* Interval value. */
\r
123 uint8_t Prescaler; /* Prescaler value. */
\r
124 uint16_t Options; /* Option settings. */
\r
127 static TmrCntrSetup xTimerSettings[ tmrTIMERS_USED ] =
\r
129 { tmrTIMER_0_FREQUENCY, 0, 0, XTTCPS_OPTION_INTERVAL_MODE | XTTCPS_OPTION_WAVE_DISABLE },
\r
130 { tmrTIMER_1_FREQUENCY, 0, 0, XTTCPS_OPTION_INTERVAL_MODE | XTTCPS_OPTION_WAVE_DISABLE },
\r
131 { tmrTIMER_2_FREQUENCY, 0, 0, XTTCPS_OPTION_INTERVAL_MODE | XTTCPS_OPTION_WAVE_DISABLE }
\r
134 /* Lower priority number means higher logical priority, so
\r
135 configMAX_API_CALL_INTERRUPT_PRIORITY - 1 is above the maximum system call
\r
136 interrupt priority. */
\r
137 static const UBaseType_t uxInterruptPriorities[ tmrTIMERS_USED ] =
\r
139 configMAX_API_CALL_INTERRUPT_PRIORITY + 1,
\r
140 configMAX_API_CALL_INTERRUPT_PRIORITY,
\r
141 configMAX_API_CALL_INTERRUPT_PRIORITY - 1
\r
143 static XTtcPs xTimerInstances[ tmrTIMERS_USED ];
\r
145 /* Used to provide a means of ensuring the intended interrupt nesting depth is
\r
146 actually being reached. */
\r
147 extern uint32_t ulPortInterruptNesting;
\r
148 static uint32_t ulMaxRecordedNesting = 0;
\r
150 /* For convenience the high frequency timer increments a variable that is then
\r
151 used as the time base for the run time stats. */
\r
152 volatile uint32_t ulHighFrequencyTimerCounts = 0;
\r
154 /*-----------------------------------------------------------*/
\r
156 void vInitialiseTimerForIntQueueTest( void )
\r
158 BaseType_t xStatus;
\r
159 TmrCntrSetup *pxTimerSettings;
\r
160 extern XScuGic xInterruptController;
\r
162 XTtcPs *pxTimerInstance;
\r
163 XTtcPs_Config *pxTimerConfiguration;
\r
164 const uint8_t ucRisingEdge = 3;
\r
166 for( xTimer = 0; xTimer < tmrTIMERS_USED; xTimer++ )
\r
168 /* Look up the timer's configuration. */
\r
169 pxTimerInstance = &( xTimerInstances[ xTimer ] );
\r
170 pxTimerConfiguration = XTtcPs_LookupConfig( xDeviceIDs[ xTimer ] );
\r
171 configASSERT( pxTimerConfiguration );
\r
173 pxTimerSettings = &( xTimerSettings[ xTimer ] );
\r
175 /* Initialise the device. */
\r
176 xStatus = XTtcPs_CfgInitialize( pxTimerInstance, pxTimerConfiguration, pxTimerConfiguration->BaseAddress );
\r
177 if( xStatus != XST_SUCCESS )
\r
179 /* Not sure how to do this before XTtcPs_CfgInitialize is called
\r
180 as pxTimerInstance is set within XTtcPs_CfgInitialize(). */
\r
181 XTtcPs_Stop( pxTimerInstance );
\r
182 xStatus = XTtcPs_CfgInitialize( pxTimerInstance, pxTimerConfiguration, pxTimerConfiguration->BaseAddress );
\r
183 configASSERT( xStatus == XST_SUCCESS );
\r
186 /* Set the options. */
\r
187 XTtcPs_SetOptions( pxTimerInstance, pxTimerSettings->Options );
\r
189 /* The timer frequency is preset in the pxTimerSettings structure.
\r
190 Derive the values for the other structure members. */
\r
191 XTtcPs_CalcIntervalFromFreq( pxTimerInstance, pxTimerSettings->OutputHz, &( pxTimerSettings->Interval ), &( pxTimerSettings->Prescaler ) );
\r
193 /* Set the interval and prescale. */
\r
194 XTtcPs_SetInterval( pxTimerInstance, pxTimerSettings->Interval );
\r
195 XTtcPs_SetPrescaler( pxTimerInstance, pxTimerSettings->Prescaler );
\r
197 /* The priority must be the lowest possible. */
\r
198 XScuGic_SetPriorityTriggerType( &xInterruptController, xInterruptIDs[ xTimer ], uxInterruptPriorities[ xTimer ] << portPRIORITY_SHIFT, ucRisingEdge );
\r
200 /* Connect to the interrupt controller. */
\r
201 xStatus = XScuGic_Connect( &xInterruptController, xInterruptIDs[ xTimer ], ( Xil_InterruptHandler ) prvTimerHandler, ( void * ) pxTimerInstance );
\r
202 configASSERT( xStatus == XST_SUCCESS);
\r
204 /* Enable the interrupt in the GIC. */
\r
205 XScuGic_Enable( &xInterruptController, xInterruptIDs[ xTimer ] );
\r
207 /* Enable the interrupts in the timer. */
\r
208 XTtcPs_EnableInterrupts( pxTimerInstance, XTTCPS_IXR_INTERVAL_MASK );
\r
210 /* Start the timer. */
\r
211 XTtcPs_Start( pxTimerInstance );
\r
214 /*-----------------------------------------------------------*/
\r
216 static void prvTimerHandler( void *pvCallBackRef )
\r
218 uint32_t ulInterruptStatus;
\r
219 XTtcPs *pxTimer = ( XTtcPs * ) pvCallBackRef;
\r
220 BaseType_t xYieldRequired;
\r
222 /* Read the interrupt status, then write it back to clear the interrupt. */
\r
223 ulInterruptStatus = XTtcPs_GetInterruptStatus( pxTimer );
\r
224 XTtcPs_ClearInterruptStatus( pxTimer, ulInterruptStatus );
\r
226 /* Only one interrupt event type is expected. */
\r
227 configASSERT( ( XTTCPS_IXR_INTERVAL_MASK & ulInterruptStatus ) != 0 );
\r
229 /* Check the device ID to know which IntQueue demo to call. */
\r
230 if( pxTimer->Config.DeviceId == xDeviceIDs[ 0 ] )
\r
232 xYieldRequired = xFirstTimerHandler();
\r
234 else if( pxTimer->Config.DeviceId == xDeviceIDs[ 1 ] )
\r
236 xYieldRequired = xSecondTimerHandler();
\r
240 /* The high frequency timer is also used to generate the time base for
\r
241 the run time state. */
\r
242 ulHighFrequencyTimerCounts++;
\r
244 /* Latch the highest interrupt nesting count detected. */
\r
245 if( ulPortInterruptNesting > ulMaxRecordedNesting )
\r
247 ulMaxRecordedNesting = ulPortInterruptNesting;
\r
250 xYieldRequired = pdFALSE;
\r
253 /* If xYieldRequired is not pdFALSE then calling either xFirstTimerHandler()
\r
254 or xSecondTimerHandler() resulted in a task leaving the blocked state and
\r
255 the task that left the blocked state had a priority higher than the currently
\r
256 running task (the task this interrupt interrupted) - so a context switch
\r
257 should be performed so the interrupt returns directly to the higher priority
\r
258 task. xYieldRequired is tested inside the following macro. */
\r
259 portYIELD_FROM_ISR( xYieldRequired );
\r