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
31 /* Scheduler includes. */
\r
32 #include "FreeRTOS.h"
\r
34 /* Library includes. */
\r
35 #include "hw_ints.h"
\r
36 #include "hw_memmap.h"
\r
37 #include "hw_types.h"
\r
38 #include "interrupt.h"
\r
40 #include "lmi_timer.h"
\r
41 #include "hw_timer.h"
\r
43 /* The set frequency of the interrupt. Deviations from this are measured as
\r
45 #define timerINTERRUPT_FREQUENCY ( 20000UL )
\r
47 /* The expected time between each of the timer interrupts - if the jitter was
\r
49 #define timerEXPECTED_DIFFERENCE_VALUE ( configCPU_CLOCK_HZ / timerINTERRUPT_FREQUENCY )
\r
51 /* The highest available interrupt priority. */
\r
52 #define timerHIGHEST_PRIORITY ( 0 )
\r
55 #define timerMAX_32BIT_VALUE ( 0xffffffffUL )
\r
56 #define timerTIMER_1_COUNT_VALUE ( * ( ( volatile unsigned long * ) ( ( unsigned long ) TIMER1_BASE + 0x48UL ) ) )
\r
58 /*-----------------------------------------------------------*/
\r
60 /* Interrupt handler in which the jitter is measured. */
\r
61 void Timer0IntHandler( void );
\r
63 /* Stores the value of the maximum recorded jitter between interrupts. */
\r
64 volatile unsigned long ulMaxJitter = 0;
\r
66 /*-----------------------------------------------------------*/
\r
68 void vSetupHighFrequencyTimer( void )
\r
70 unsigned long ulFrequency;
\r
72 /* Timer zero is used to generate the interrupts, and timer 1 is used
\r
73 to measure the jitter. */
\r
74 SysCtlPeripheralEnable( SYSCTL_PERIPH_TIMER0 );
\r
75 SysCtlPeripheralEnable( SYSCTL_PERIPH_TIMER1 );
\r
76 TimerConfigure( TIMER0_BASE, TIMER_CFG_32_BIT_PER );
\r
77 TimerConfigure( TIMER1_BASE, TIMER_CFG_32_BIT_PER );
\r
79 /* Set the timer interrupt to be above the kernel - highest. */
\r
80 IntPrioritySet( INT_TIMER0A, timerHIGHEST_PRIORITY );
\r
82 /* Just used to measure time. */
\r
83 TimerLoadSet(TIMER1_BASE, TIMER_A, timerMAX_32BIT_VALUE );
\r
85 /* Ensure interrupts do not start until the scheduler is running. */
\r
86 portDISABLE_INTERRUPTS();
\r
88 /* The rate at which the timer will interrupt. */
\r
89 ulFrequency = configCPU_CLOCK_HZ / timerINTERRUPT_FREQUENCY;
\r
90 TimerLoadSet( TIMER0_BASE, TIMER_A, ulFrequency );
\r
91 IntEnable( INT_TIMER0A );
\r
92 TimerIntEnable( TIMER0_BASE, TIMER_TIMA_TIMEOUT );
\r
94 /* Enable both timers. */
\r
95 TimerEnable( TIMER0_BASE, TIMER_A );
\r
96 TimerEnable( TIMER1_BASE, TIMER_A );
\r
98 /*-----------------------------------------------------------*/
\r
100 void Timer0IntHandler( void )
\r
102 unsigned long ulDifference;
\r
103 volatile unsigned long ulCurrentCount;
\r
104 static unsigned long ulMaxDifference = 0, ulLastCount = 0;
\r
106 /* We use the timer 1 counter value to measure the clock cycles between
\r
107 the timer 0 interrupts. */
\r
108 ulCurrentCount = timerTIMER_1_COUNT_VALUE;
\r
110 TimerIntClear( TIMER0_BASE, TIMER_TIMA_TIMEOUT );
\r
112 if( ulCurrentCount < ulLastCount )
\r
114 /* How many times has timer 1 counted since the last interrupt? */
\r
115 ulDifference = ulLastCount - ulCurrentCount;
\r
117 /* Is this the largest difference we have measured yet? */
\r
118 if( ulDifference > ulMaxDifference )
\r
120 ulMaxDifference = ulDifference;
\r
121 ulMaxJitter = ulMaxDifference - timerEXPECTED_DIFFERENCE_VALUE;
\r
125 ulLastCount = ulCurrentCount;
\r