]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_STM32F103_Keil/timertest.c
Ready for V5.2.0 release.
[freertos] / Demo / CORTEX_STM32F103_Keil / timertest.c
1 /*\r
2         FreeRTOS.org V5.2.0 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify it \r
7         under the terms of the GNU General Public License (version 2) as published\r
8         by the Free Software Foundation and modified by the FreeRTOS exception.\r
9 \r
10         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
11         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or \r
12         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for \r
13         more details.\r
14 \r
15         You should have received a copy of the GNU General Public License along \r
16         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59 \r
17         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
18 \r
19         A special exception to the GPL is included to allow you to distribute a \r
20         combined work that includes FreeRTOS.org without being obliged to provide\r
21         the source code for any proprietary components.  See the licensing section\r
22         of http://www.FreeRTOS.org for full details.\r
23 \r
24 \r
25         ***************************************************************************\r
26         *                                                                         *\r
27         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
28         *                                                                         *\r
29         * This is a concise, step by step, 'hands on' guide that describes both   *\r
30         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
31         * explains numerous examples that are written using the FreeRTOS API.     *\r
32         * Full source code for all the examples is provided in an accompanying    *\r
33         * .zip file.                                                              *\r
34         *                                                                         *\r
35         ***************************************************************************\r
36 \r
37         1 tab == 4 spaces!\r
38 \r
39         Please ensure to read the configuration and relevant port sections of the\r
40         online documentation.\r
41 \r
42         http://www.FreeRTOS.org - Documentation, latest information, license and\r
43         contact details.\r
44 \r
45         http://www.SafeRTOS.com - A version that is certified for use in safety\r
46         critical systems.\r
47 \r
48         http://www.OpenRTOS.com - Commercial support, development, porting,\r
49         licensing and training services.\r
50 */\r
51 \r
52 /* High speed timer test as described in main.c. */\r
53 \r
54 /* Scheduler includes. */\r
55 #include "FreeRTOS.h"\r
56 \r
57 /* Library includes. */\r
58 #include "stm32f10x_lib.h"\r
59 #include "stm32f10x_tim.h"\r
60 #include "stm32f10x_map.h"\r
61 \r
62 /* The set frequency of the interrupt.  Deviations from this are measured as\r
63 the jitter. */\r
64 #define timerINTERRUPT_FREQUENCY                ( ( unsigned portSHORT ) 20000 )\r
65 \r
66 /* The expected time between each of the timer interrupts - if the jitter was\r
67 zero. */\r
68 #define timerEXPECTED_DIFFERENCE_VALUE  ( configCPU_CLOCK_HZ / timerINTERRUPT_FREQUENCY )\r
69 \r
70 /* The highest available interrupt priority. */\r
71 #define timerHIGHEST_PRIORITY                   ( 0 )\r
72 \r
73 /* Misc defines. */\r
74 #define timerMAX_32BIT_VALUE                    ( 0xffffffffUL )\r
75 #define timerTIMER_1_COUNT_VALUE                ( * ( ( unsigned long * ) ( TIMER1_BASE + 0x48 ) ) )\r
76 \r
77 /* The number of interrupts to pass before we start looking at the jitter. */\r
78 #define timerSETTLE_TIME                        5\r
79 \r
80 /*-----------------------------------------------------------*/\r
81 \r
82 /*\r
83  * Configures the two timers used to perform the test.\r
84  */\r
85 void vSetupTimerTest( void );\r
86 \r
87 /* Interrupt handler in which the jitter is measured. */\r
88 void vTimer2IntHandler( void );\r
89 \r
90 /* Stores the value of the maximum recorded jitter between interrupts. */\r
91 volatile unsigned portSHORT usMaxJitter = 0;\r
92 \r
93 /*-----------------------------------------------------------*/\r
94 \r
95 void vSetupTimerTest( void )\r
96 {\r
97 unsigned long ulFrequency;\r
98 TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;\r
99 NVIC_InitTypeDef NVIC_InitStructure;\r
100 \r
101 \r
102         /* Enable timer clocks */\r
103         RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM2, ENABLE );\r
104         RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3, ENABLE );\r
105 \r
106         /* Initialise data. */\r
107         TIM_DeInit( TIM2 );\r
108         TIM_DeInit( TIM3 );\r
109         TIM_TimeBaseStructInit( &TIM_TimeBaseStructure );\r
110 \r
111         /* Time base configuration for timer 2 - which generates the interrupts. */\r
112         ulFrequency = configCPU_CLOCK_HZ / timerINTERRUPT_FREQUENCY;    \r
113         TIM_TimeBaseStructure.TIM_Period = ( unsigned portSHORT ) ( ulFrequency & 0xffffUL );\r
114         TIM_TimeBaseStructure.TIM_Prescaler = 0x0;\r
115         TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;\r
116         TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;\r
117         TIM_TimeBaseInit( TIM2, &TIM_TimeBaseStructure );\r
118         TIM_ARRPreloadConfig( TIM2, ENABLE );\r
119 \r
120         \r
121         /* Configuration for timer 3 which is used as a high resolution time\r
122         measurement. */\r
123         TIM_TimeBaseStructure.TIM_Period = ( unsigned portSHORT ) 0xffff;\r
124         TIM_TimeBaseInit( TIM3, &TIM_TimeBaseStructure );\r
125         TIM_ARRPreloadConfig( TIM3, ENABLE );\r
126         \r
127         /* Enable TIM2 IT.  TIM3 does not generate an interrupt. */\r
128         NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;\r
129         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;\r
130         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = timerHIGHEST_PRIORITY;\r
131         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;\r
132         NVIC_Init( &NVIC_InitStructure );       \r
133         TIM_ITConfig( TIM2, TIM_IT_Update, ENABLE );\r
134 \r
135         /* Finally, enable both timers. */\r
136         TIM_Cmd( TIM2, ENABLE );\r
137         TIM_Cmd( TIM3, ENABLE );\r
138 }\r
139 /*-----------------------------------------------------------*/\r
140 \r
141 void vTimer2IntHandler( void )\r
142 {\r
143 static unsigned portSHORT usLastCount = 0, usSettleCount = 0, usMaxDifference = 0;\r
144 unsigned portSHORT usThisCount, usDifference;\r
145 \r
146         /* Capture the free running timer 3 value as we enter the interrupt. */\r
147         usThisCount = TIM3->CNT;\r
148         \r
149         if( usSettleCount >= timerSETTLE_TIME )\r
150         {\r
151                 /* What is the difference between the timer value in this interrupt\r
152                 and the value from the last interrupt. */\r
153                 usDifference = usThisCount - usLastCount;\r
154 \r
155                 /* Store the difference in the timer values if it is larger than the\r
156                 currently stored largest value.  The difference over and above the\r
157                 expected difference will give the 'jitter' in the processing of these\r
158                 interrupts. */\r
159                 if( usDifference > usMaxDifference )\r
160                 {\r
161                         usMaxDifference = usDifference;\r
162                         usMaxJitter = usMaxDifference - timerEXPECTED_DIFFERENCE_VALUE;\r
163                 }\r
164         }\r
165         else\r
166         {\r
167                 /* Don't bother storing any values for the first couple of\r
168                 interrupts. */\r
169                 usSettleCount++;\r
170         }\r
171 \r
172         /* Remember what the timer value was this time through, so we can calculate\r
173         the difference the next time through. */\r
174         usLastCount = usThisCount;\r
175 \r
176     TIM_ClearITPendingBit( TIM2, TIM_IT_Update );\r
177 }\r
178 \r
179 \r
180 \r
181 \r
182 \r
183 \r
184 \r
185 \r