2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 /* High speed timer test as described in main.c. */
\r
32 /* Scheduler includes. */
\r
33 #include "FreeRTOS.h"
\r
35 /* Demo includes. */
\r
36 #include "partest.h"
\r
38 /* The number of interrupts to pass before we start looking at the jitter. */
\r
39 #define timerSETTLE_TIME 5
\r
41 /* The maximum value the 16bit timer can contain. */
\r
42 #define timerMAX_COUNT 0xffff
\r
44 /*-----------------------------------------------------------*/
\r
47 * Measure the time between this interrupt and the previous interrupt to
\r
48 * calculate the timing jitter. Remember the maximum value the jitter has
\r
49 * ever been calculated to be.
\r
51 static void prvCalculateAndStoreJitter( void );
\r
53 /*-----------------------------------------------------------*/
\r
55 /* The maximum time (in processor clocks) between two consecutive timer
\r
56 interrupts so far. */
\r
57 unsigned short usMaxJitter = 0;
\r
59 /*-----------------------------------------------------------*/
\r
61 void vSetupTimerTest( unsigned short usFrequencyHz )
\r
63 /* T2 is used to generate interrupts. T4 is used to provide an accurate
\r
64 time measurement. */
\r
70 /* Timer 2 is going to interrupt at usFrequencyHz Hz. */
\r
71 PR2 = ( unsigned short ) ( configCPU_CLOCK_HZ / ( unsigned long ) usFrequencyHz );
\r
73 /* Timer 4 is going to free run from minimum to maximum value. */
\r
74 PR4 = ( unsigned short ) timerMAX_COUNT;
\r
76 /* Setup timer 2 interrupt priority to be above the kernel priority so
\r
77 the timer jitter is not effected by the kernel activity. */
\r
78 IPC1bits.T2IP = configKERNEL_INTERRUPT_PRIORITY + 1;
\r
80 /* Clear the interrupt as a starting condition. */
\r
83 /* Enable the interrupt. */
\r
86 /* Start both timers. */
\r
90 /*-----------------------------------------------------------*/
\r
92 static void prvCalculateAndStoreJitter( void )
\r
94 static unsigned short usLastCount = 0, usSettleCount = 0;
\r
95 unsigned short usThisCount, usDifference;
\r
97 /* Capture the timer value as we enter the interrupt. */
\r
100 if( usSettleCount >= timerSETTLE_TIME )
\r
102 /* What is the difference between the timer value in this interrupt
\r
103 and the value from the last interrupt. */
\r
104 usDifference = usThisCount - usLastCount;
\r
106 /* Store the difference in the timer values if it is larger than the
\r
107 currently stored largest value. The difference over and above the
\r
108 expected difference will give the 'jitter' in the processing of these
\r
110 if( usDifference > usMaxJitter )
\r
112 usMaxJitter = usDifference;
\r
117 /* Don't bother storing any values for the first couple of
\r
122 /* Remember what the timer value was this time through, so we can calculate
\r
123 the difference the next time through. */
\r
124 usLastCount = usThisCount;
\r
126 /*-----------------------------------------------------------*/
\r
128 void __attribute__((__interrupt__, auto_psv)) _T2Interrupt( void )
\r
130 /* Work out the time between this and the previous interrupt. */
\r
131 prvCalculateAndStoreJitter();
\r
133 /* Clear the timer interrupt. */
\r