2 FreeRTOS.org V5.3.0 - Copyright (C) 2003-2009 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 it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS.org without being obliged to provide
\r
11 the source code for any proprietary components. Alternative commercial
\r
12 license and support terms are also available upon request. See the
\r
13 licensing section of http://www.FreeRTOS.org for full details.
\r
15 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
20 You should have received a copy of the GNU General Public License along
\r
21 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
22 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
52 /* High speed timer test as described in main.c. */
\r
55 /* Scheduler includes. */
\r
56 #include "FreeRTOS.h"
\r
58 /* Demo includes. */
\r
59 #include "partest.h"
\r
61 /* The number of interrupts to pass before we start looking at the jitter. */
\r
62 #define timerSETTLE_TIME 5
\r
64 /* The maximum value the 16bit timer can contain. */
\r
65 #define timerMAX_COUNT 0xffff
\r
67 /*-----------------------------------------------------------*/
\r
70 * Measure the time between this interrupt and the previous interrupt to
\r
71 * calculate the timing jitter. Remember the maximum value the jitter has
\r
72 * ever been calculated to be.
\r
74 static void prvCalculateAndStoreJitter( void );
\r
76 /*-----------------------------------------------------------*/
\r
78 /* The maximum time (in processor clocks) between two consecutive timer
\r
79 interrupts so far. */
\r
80 unsigned portSHORT usMaxJitter = 0;
\r
82 /*-----------------------------------------------------------*/
\r
84 void vSetupTimerTest( unsigned portSHORT usFrequencyHz )
\r
86 /* T2 is used to generate interrupts. T4 is used to provide an accurate
\r
87 time measurement. */
\r
93 /* Timer 2 is going to interrupt at usFrequencyHz Hz. */
\r
94 PR2 = ( unsigned portSHORT ) ( configCPU_CLOCK_HZ / ( unsigned portLONG ) usFrequencyHz );
\r
96 /* Timer 4 is going to free run from minimum to maximum value. */
\r
97 PR4 = ( unsigned portSHORT ) timerMAX_COUNT;
\r
99 /* Setup timer 2 interrupt priority to be above the kernel priority so
\r
100 the timer jitter is not effected by the kernel activity. */
\r
101 IPC1bits.T2IP = configKERNEL_INTERRUPT_PRIORITY + 1;
\r
103 /* Clear the interrupt as a starting condition. */
\r
106 /* Enable the interrupt. */
\r
109 /* Start both timers. */
\r
113 /*-----------------------------------------------------------*/
\r
115 static void prvCalculateAndStoreJitter( void )
\r
117 static unsigned portSHORT usLastCount = 0, usSettleCount = 0;
\r
118 unsigned portSHORT usThisCount, usDifference;
\r
120 /* Capture the timer value as we enter the interrupt. */
\r
121 usThisCount = TMR4;
\r
123 if( usSettleCount >= timerSETTLE_TIME )
\r
125 /* What is the difference between the timer value in this interrupt
\r
126 and the value from the last interrupt. */
\r
127 usDifference = usThisCount - usLastCount;
\r
129 /* Store the difference in the timer values if it is larger than the
\r
130 currently stored largest value. The difference over and above the
\r
131 expected difference will give the 'jitter' in the processing of these
\r
133 if( usDifference > usMaxJitter )
\r
135 usMaxJitter = usDifference;
\r
140 /* Don't bother storing any values for the first couple of
\r
145 /* Remember what the timer value was this time through, so we can calculate
\r
146 the difference the next time through. */
\r
147 usLastCount = usThisCount;
\r
149 /*-----------------------------------------------------------*/
\r
151 void __attribute__((__interrupt__, auto_psv)) _T2Interrupt( void )
\r
153 /* Work out the time between this and the previous interrupt. */
\r
154 prvCalculateAndStoreJitter();
\r
156 /* Clear the timer interrupt. */
\r