2 FreeRTOS V5.4.0 - Copyright (C) 2003-2009 Richard Barry.
\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 "stm32f10x_lib.h"
\r
55 #include "stm32f10x_tim.h"
\r
56 #include "stm32f10x_map.h"
\r
58 /* The set frequency of the interrupt. Deviations from this are measured as
\r
60 #define timerINTERRUPT_FREQUENCY ( ( unsigned portSHORT ) 20000 )
\r
62 /* The expected time between each of the timer interrupts - if the jitter was
\r
64 #define timerEXPECTED_DIFFERENCE_VALUE ( configCPU_CLOCK_HZ / timerINTERRUPT_FREQUENCY )
\r
66 /* The highest available interrupt priority. */
\r
67 #define timerHIGHEST_PRIORITY ( 0 )
\r
70 #define timerMAX_32BIT_VALUE ( 0xffffffffUL )
\r
71 #define timerTIMER_1_COUNT_VALUE ( * ( ( unsigned long * ) ( TIMER1_BASE + 0x48 ) ) )
\r
73 /* The number of interrupts to pass before we start looking at the jitter. */
\r
74 #define timerSETTLE_TIME 5
\r
76 /*-----------------------------------------------------------*/
\r
79 * Configures the two timers used to perform the test.
\r
81 void vSetupTimerTest( void );
\r
83 /* Interrupt handler in which the jitter is measured. */
\r
84 void vTimer2IntHandler( void );
\r
86 /* Stores the value of the maximum recorded jitter between interrupts. */
\r
87 volatile unsigned portSHORT usMaxJitter = 0;
\r
89 /*-----------------------------------------------------------*/
\r
91 void vSetupTimerTest( void )
\r
93 unsigned long ulFrequency;
\r
94 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
\r
95 NVIC_InitTypeDef NVIC_InitStructure;
\r
98 /* Enable timer clocks */
\r
99 RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM2, ENABLE );
\r
100 RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3, ENABLE );
\r
102 /* Initialise data. */
\r
103 TIM_DeInit( TIM2 );
\r
104 TIM_DeInit( TIM3 );
\r
105 TIM_TimeBaseStructInit( &TIM_TimeBaseStructure );
\r
107 /* Time base configuration for timer 2 - which generates the interrupts. */
\r
108 ulFrequency = configCPU_CLOCK_HZ / timerINTERRUPT_FREQUENCY;
\r
109 TIM_TimeBaseStructure.TIM_Period = ( unsigned portSHORT ) ( ulFrequency & 0xffffUL );
\r
110 TIM_TimeBaseStructure.TIM_Prescaler = 0x0;
\r
111 TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
\r
112 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
\r
113 TIM_TimeBaseInit( TIM2, &TIM_TimeBaseStructure );
\r
114 TIM_ARRPreloadConfig( TIM2, ENABLE );
\r
117 /* Configuration for timer 3 which is used as a high resolution time
\r
119 TIM_TimeBaseStructure.TIM_Period = ( unsigned portSHORT ) 0xffff;
\r
120 TIM_TimeBaseInit( TIM3, &TIM_TimeBaseStructure );
\r
121 TIM_ARRPreloadConfig( TIM3, ENABLE );
\r
123 /* Enable TIM2 IT. TIM3 does not generate an interrupt. */
\r
124 NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;
\r
125 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
\r
126 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = timerHIGHEST_PRIORITY;
\r
127 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
\r
128 NVIC_Init( &NVIC_InitStructure );
\r
129 TIM_ITConfig( TIM2, TIM_IT_Update, ENABLE );
\r
131 /* Finally, enable both timers. */
\r
132 TIM_Cmd( TIM2, ENABLE );
\r
133 TIM_Cmd( TIM3, ENABLE );
\r
135 /*-----------------------------------------------------------*/
\r
137 void vTimer2IntHandler( void )
\r
139 static unsigned portSHORT usLastCount = 0, usSettleCount = 0, usMaxDifference = 0;
\r
140 unsigned portSHORT usThisCount, usDifference;
\r
142 /* Capture the free running timer 3 value as we enter the interrupt. */
\r
143 usThisCount = TIM3->CNT;
\r
145 if( usSettleCount >= timerSETTLE_TIME )
\r
147 /* What is the difference between the timer value in this interrupt
\r
148 and the value from the last interrupt. */
\r
149 usDifference = usThisCount - usLastCount;
\r
151 /* Store the difference in the timer values if it is larger than the
\r
152 currently stored largest value. The difference over and above the
\r
153 expected difference will give the 'jitter' in the processing of these
\r
155 if( usDifference > usMaxDifference )
\r
157 usMaxDifference = usDifference;
\r
158 usMaxJitter = usMaxDifference - timerEXPECTED_DIFFERENCE_VALUE;
\r
163 /* Don't bother storing any values for the first couple of
\r
168 /* Remember what the timer value was this time through, so we can calculate
\r
169 the difference the next time through. */
\r
170 usLastCount = usThisCount;
\r
172 TIM_ClearITPendingBit( TIM2, TIM_IT_Update );
\r