2 * FreeRTOS Kernel V10.3.0
\r
3 * Copyright (C) 2020 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.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 /* High speed timer test as described in main.c. */
\r
30 /* Scheduler includes. */
\r
31 #include "FreeRTOS.h"
\r
33 /* Library includes. */
\r
34 #include "hw_ints.h"
\r
35 #include "hw_memmap.h"
\r
36 #include "hw_types.h"
\r
37 #include "interrupt.h"
\r
39 #include "lmi_timer.h"
\r
40 #include "hw_timer.h"
\r
42 /* The set frequency of the interrupt. Deviations from this are measured as
\r
44 #define timerINTERRUPT_FREQUENCY ( 20000UL )
\r
46 /* The expected time between each of the timer interrupts - if the jitter was
\r
48 #define timerEXPECTED_DIFFERENCE_VALUE ( configCPU_CLOCK_HZ / timerINTERRUPT_FREQUENCY )
\r
50 /* The highest available interrupt priority. */
\r
51 #define timerHIGHEST_PRIORITY ( 0 )
\r
54 #define timerMAX_32BIT_VALUE ( 0xffffffffUL )
\r
55 #define timerTIMER_1_COUNT_VALUE ( * ( ( volatile unsigned long * ) ( ( unsigned long ) TIMER1_BASE + 0x48UL ) ) )
\r
57 /*-----------------------------------------------------------*/
\r
59 /* Interrupt handler in which the jitter is measured. */
\r
60 void Timer0IntHandler( void );
\r
62 /* Stores the value of the maximum recorded jitter between interrupts. */
\r
63 volatile unsigned long ulMaxJitter = 0;
\r
65 /*-----------------------------------------------------------*/
\r
67 void vSetupHighFrequencyTimer( void )
\r
69 unsigned long ulFrequency;
\r
71 /* Timer zero is used to generate the interrupts, and timer 1 is used
\r
72 to measure the jitter. */
\r
73 SysCtlPeripheralEnable( SYSCTL_PERIPH_TIMER0 );
\r
74 SysCtlPeripheralEnable( SYSCTL_PERIPH_TIMER1 );
\r
75 TimerConfigure( TIMER0_BASE, TIMER_CFG_32_BIT_PER );
\r
76 TimerConfigure( TIMER1_BASE, TIMER_CFG_32_BIT_PER );
\r
78 /* Set the timer interrupt to be above the kernel - highest. */
\r
79 IntPrioritySet( INT_TIMER0A, timerHIGHEST_PRIORITY );
\r
81 /* Just used to measure time. */
\r
82 TimerLoadSet(TIMER1_BASE, TIMER_A, timerMAX_32BIT_VALUE );
\r
84 /* Ensure interrupts do not start until the scheduler is running. */
\r
85 portDISABLE_INTERRUPTS();
\r
87 /* The rate at which the timer will interrupt. */
\r
88 ulFrequency = configCPU_CLOCK_HZ / timerINTERRUPT_FREQUENCY;
\r
89 TimerLoadSet( TIMER0_BASE, TIMER_A, ulFrequency );
\r
90 IntEnable( INT_TIMER0A );
\r
91 TimerIntEnable( TIMER0_BASE, TIMER_TIMA_TIMEOUT );
\r
93 /* Enable both timers. */
\r
94 TimerEnable( TIMER0_BASE, TIMER_A );
\r
95 TimerEnable( TIMER1_BASE, TIMER_A );
\r
97 /*-----------------------------------------------------------*/
\r
99 void Timer0IntHandler( void )
\r
101 unsigned long ulDifference;
\r
102 volatile unsigned long ulCurrentCount;
\r
103 static unsigned long ulMaxDifference = 0, ulLastCount = 0;
\r
105 /* We use the timer 1 counter value to measure the clock cycles between
\r
106 the timer 0 interrupts. */
\r
107 ulCurrentCount = timerTIMER_1_COUNT_VALUE;
\r
109 TimerIntClear( TIMER0_BASE, TIMER_TIMA_TIMEOUT );
\r
111 if( ulCurrentCount < ulLastCount )
\r
113 /* How many times has timer 1 counted since the last interrupt? */
\r
114 ulDifference = ulLastCount - ulCurrentCount;
\r
116 /* Is this the largest difference we have measured yet? */
\r
117 if( ulDifference > ulMaxDifference )
\r
119 ulMaxDifference = ulDifference;
\r
120 ulMaxJitter = ulMaxDifference - timerEXPECTED_DIFFERENCE_VALUE;
\r
124 ulLastCount = ulCurrentCount;
\r