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