2 FreeRTOS V5.4.2 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
7 the terms of the GNU General Public License (version 2) as published by the
\r
8 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 without being obliged to provide the
\r
11 source code for proprietary components outside of the FreeRTOS kernel.
\r
12 Alternative commercial license and support terms are also available upon
\r
13 request. See the licensing section of http://www.FreeRTOS.org for full
\r
16 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
21 You should have received a copy of the GNU General Public License along
\r
22 with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59
\r
23 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
26 ***************************************************************************
\r
28 * Looking for a quick start? Then check out the FreeRTOS eBook! *
\r
29 * See http://www.FreeRTOS.org/Documentation for details *
\r
31 ***************************************************************************
\r
35 Please ensure to read the configuration and relevant port sections of the
\r
36 online documentation.
\r
38 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
41 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
44 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
45 licensing and training services.
\r
48 /* High speed timer test as described in main.c. */
\r
50 /* Scheduler includes. */
\r
51 #include "FreeRTOS.h"
\r
53 /* Library includes. */
\r
54 #include "hw_ints.h"
\r
55 #include "hw_memmap.h"
\r
56 #include "hw_types.h"
\r
57 #include "interrupt.h"
\r
59 #include "lmi_timer.h"
\r
61 /* The set frequency of the interrupt. Deviations from this are measured as
\r
63 #define timerINTERRUPT_FREQUENCY ( 20000UL )
\r
65 /* The expected time between each of the timer interrupts - if the jitter was
\r
67 #define timerEXPECTED_DIFFERENCE_VALUE ( configCPU_CLOCK_HZ / timerINTERRUPT_FREQUENCY )
\r
69 /* The highest available interrupt priority. */
\r
70 #define timerHIGHEST_PRIORITY ( 0 )
\r
73 #define timerMAX_32BIT_VALUE ( 0xffffffffUL )
\r
74 #define timerTIMER_1_COUNT_VALUE ( * ( ( unsigned long * ) ( TIMER1_BASE + 0x48 ) ) )
\r
76 /*-----------------------------------------------------------*/
\r
78 /* Interrupt handler in which the jitter is measured. */
\r
79 void Timer0IntHandler( void );
\r
81 /* Stores the value of the maximum recorded jitter between interrupts. */
\r
82 volatile unsigned portLONG ulMaxJitter = 0UL;
\r
84 /* Counts the total number of times that the high frequency timer has 'ticked'.
\r
85 This value is used by the run time stats function to work out what percentage
\r
86 of CPU time each task is taking. */
\r
87 volatile unsigned portLONG ulHighFrequencyTimerTicks = 0UL;
\r
88 /*-----------------------------------------------------------*/
\r
90 void vSetupHighFrequencyTimer( void )
\r
92 unsigned long ulFrequency;
\r
94 /* Timer zero is used to generate the interrupts, and timer 1 is used
\r
95 to measure the jitter. */
\r
96 SysCtlPeripheralEnable( SYSCTL_PERIPH_TIMER0 );
\r
97 SysCtlPeripheralEnable( SYSCTL_PERIPH_TIMER1 );
\r
98 TimerConfigure( TIMER0_BASE, TIMER_CFG_32_BIT_PER );
\r
99 TimerConfigure( TIMER1_BASE, TIMER_CFG_32_BIT_PER );
\r
101 /* Set the timer interrupt to be above the kernel - highest. */
\r
102 IntPrioritySet( INT_TIMER0A, timerHIGHEST_PRIORITY );
\r
104 /* Just used to measure time. */
\r
105 TimerLoadSet(TIMER1_BASE, TIMER_A, timerMAX_32BIT_VALUE );
\r
107 /* Ensure interrupts do not start until the scheduler is running. */
\r
108 portDISABLE_INTERRUPTS();
\r
110 /* The rate at which the timer will interrupt. */
\r
111 ulFrequency = configCPU_CLOCK_HZ / timerINTERRUPT_FREQUENCY;
\r
112 TimerLoadSet( TIMER0_BASE, TIMER_A, ulFrequency );
\r
113 IntEnable( INT_TIMER0A );
\r
114 TimerIntEnable( TIMER0_BASE, TIMER_TIMA_TIMEOUT );
\r
116 /* Enable both timers. */
\r
117 TimerEnable( TIMER0_BASE, TIMER_A );
\r
118 TimerEnable( TIMER1_BASE, TIMER_A );
\r
120 /*-----------------------------------------------------------*/
\r
122 void Timer0IntHandler( void )
\r
124 unsigned portLONG ulDifference;
\r
125 volatile unsigned portLONG ulCurrentCount;
\r
126 static unsigned portLONG ulMaxDifference = 0, ulLastCount = 0;
\r
128 /* We use the timer 1 counter value to measure the clock cycles between
\r
129 the timer 0 interrupts. */
\r
130 ulCurrentCount = timerTIMER_1_COUNT_VALUE;
\r
132 TimerIntClear( TIMER0_BASE, TIMER_TIMA_TIMEOUT );
\r
134 if( ulCurrentCount < ulLastCount )
\r
136 /* How many times has timer 1 counted since the last interrupt? */
\r
137 ulDifference = ulLastCount - ulCurrentCount;
\r
139 /* Is this the largest difference we have measured yet? */
\r
140 if( ulDifference > ulMaxDifference )
\r
142 ulMaxDifference = ulDifference;
\r
143 ulMaxJitter = ulMaxDifference - timerEXPECTED_DIFFERENCE_VALUE;
\r
147 ulLastCount = ulCurrentCount;
\r
149 /* Keep a count of the total number of 20KHz ticks. This is used by the
\r
150 run time stats functionality to calculate how much CPU time is used by
\r
152 ulHighFrequencyTimerTicks++;
\r