]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_STM32F103_Primer_GCC/timertest.c
Update version numbers to V4.8.0
[freertos] / Demo / CORTEX_STM32F103_Primer_GCC / timertest.c
1 /*\r
2         FreeRTOS.org V4.8.0 - 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         *                                                                                                                                                 *\r
29         * SAVE TIME AND MONEY!  Why not get us to quote to get FreeRTOS.org               *\r
30         * running on your hardware - or even write all or part of your application*\r
31         * for you?  See http://www.OpenRTOS.com for details.                                      *\r
32         *                                                                                                                                                 *\r
33         ***************************************************************************\r
34         ***************************************************************************\r
35 \r
36         Please ensure to read the configuration and relevant port sections of the\r
37         online documentation.\r
38 \r
39         http://www.FreeRTOS.org - Documentation, latest information, license and \r
40         contact details.\r
41 \r
42         http://www.SafeRTOS.com - A version that is certified for use in safety \r
43         critical systems.\r
44 \r
45         http://www.OpenRTOS.com - Commercial support, development, porting, \r
46         licensing and training services.\r
47 */\r
48 \r
49 /* High speed timer test as described in main.c. */\r
50 \r
51 /* Scheduler includes. */\r
52 #include "FreeRTOS.h"\r
53 \r
54 /* Library includes. */\r
55 #include "stm32f10x_lib.h"\r
56 #include "stm32f10x_tim.h"\r
57 #include "stm32f10x_map.h"\r
58 \r
59 /* The set frequency of the interrupt.  Deviations from this are measured as\r
60 the jitter. */\r
61 #define timerINTERRUPT_FREQUENCY                ( ( unsigned portSHORT ) 20000 )\r
62 \r
63 /* The expected time between each of the timer interrupts - if the jitter was\r
64 zero. */\r
65 #define timerEXPECTED_DIFFERENCE_VALUE  ( configCPU_CLOCK_HZ / timerINTERRUPT_FREQUENCY )\r
66 \r
67 /* The highest available interrupt priority. */\r
68 #define timerHIGHEST_PRIORITY                   ( 0 )\r
69 \r
70 /* Misc defines. */\r
71 #define timerMAX_32BIT_VALUE                    ( 0xffffffffUL )\r
72 #define timerTIMER_1_COUNT_VALUE                ( * ( ( unsigned long * ) ( TIMER1_BASE + 0x48 ) ) )\r
73 \r
74 /* The number of interrupts to pass before we start looking at the jitter. */\r
75 #define timerSETTLE_TIME                        5\r
76 \r
77 /*-----------------------------------------------------------*/\r
78 \r
79 /*\r
80  * Configures the two timers used to perform the test.\r
81  */\r
82 void vSetupTimerTest( void );\r
83 \r
84 /* Interrupt handler in which the jitter is measured. */\r
85 void vTimer2IntHandler( void );\r
86 \r
87 /* Stores the value of the maximum recorded jitter between interrupts. */\r
88 volatile unsigned portSHORT usMaxJitter = 0;\r
89 \r
90 /*-----------------------------------------------------------*/\r
91 \r
92 void vSetupTimerTest( void )\r
93 {\r
94 unsigned long ulFrequency;\r
95 TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;\r
96 NVIC_InitTypeDef NVIC_InitStructure;\r
97 \r
98 \r
99         /* Enable timer clocks */\r
100         RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM2, ENABLE );\r
101         RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3, ENABLE );\r
102 \r
103         /* Initialise data. */\r
104         TIM_DeInit( TIM2 );\r
105         TIM_DeInit( TIM3 );\r
106         TIM_TimeBaseStructInit( &TIM_TimeBaseStructure );\r
107 \r
108         /* Time base configuration for timer 2 - which generates the interrupts. */\r
109         ulFrequency = configCPU_CLOCK_HZ / timerINTERRUPT_FREQUENCY;    \r
110         TIM_TimeBaseStructure.TIM_Period = ( unsigned portSHORT ) ( ulFrequency & 0xffffUL );\r
111         TIM_TimeBaseStructure.TIM_Prescaler = 0x0;\r
112         TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;\r
113         TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;\r
114         TIM_TimeBaseInit( TIM2, &TIM_TimeBaseStructure );\r
115         TIM_ARRPreloadConfig( TIM2, ENABLE );\r
116 \r
117         \r
118         /* Configuration for timer 3 which is used as a high resolution time\r
119         measurement. */\r
120         TIM_TimeBaseStructure.TIM_Period = ( unsigned portSHORT ) 0xffff;\r
121         TIM_TimeBaseInit( TIM3, &TIM_TimeBaseStructure );\r
122         TIM_ARRPreloadConfig( TIM3, ENABLE );\r
123         \r
124         /* Enable TIM2 IT.  TIM3 does not generate an interrupt. */\r
125         NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;\r
126         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;\r
127         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = timerHIGHEST_PRIORITY;\r
128         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;\r
129         NVIC_Init( &NVIC_InitStructure );       \r
130         TIM_ITConfig( TIM2, TIM_IT_Update, ENABLE );\r
131 \r
132         /* Finally, enable both timers. */\r
133         TIM_Cmd( TIM2, ENABLE );\r
134         TIM_Cmd( TIM3, ENABLE );\r
135 }\r
136 /*-----------------------------------------------------------*/\r
137 \r
138 void vTimer2IntHandler( void )\r
139 {\r
140 static unsigned portSHORT usLastCount = 0, usSettleCount = 0, usMaxDifference = 0;\r
141 unsigned portSHORT usThisCount, usDifference;\r
142 \r
143         /* Capture the free running timer 3 value as we enter the interrupt. */\r
144         usThisCount = TIM3->CNT;\r
145         \r
146         if( usSettleCount >= timerSETTLE_TIME )\r
147         {\r
148                 /* What is the difference between the timer value in this interrupt\r
149                 and the value from the last interrupt. */\r
150                 usDifference = usThisCount - usLastCount;\r
151 \r
152                 /* Store the difference in the timer values if it is larger than the\r
153                 currently stored largest value.  The difference over and above the\r
154                 expected difference will give the 'jitter' in the processing of these\r
155                 interrupts. */\r
156                 if( usDifference > usMaxDifference )\r
157                 {\r
158                         usMaxDifference = usDifference;\r
159                         usMaxJitter = usMaxDifference - timerEXPECTED_DIFFERENCE_VALUE;\r
160                 }\r
161         }\r
162         else\r
163         {\r
164                 /* Don't bother storing any values for the first couple of\r
165                 interrupts. */\r
166                 usSettleCount++;\r
167         }\r
168 \r
169         /* Remember what the timer value was this time through, so we can calculate\r
170         the difference the next time through. */\r
171         usLastCount = usThisCount;\r
172 \r
173     TIM_ClearITPendingBit( TIM2, TIM_IT_Update );\r
174 }\r
175 \r
176 \r
177 \r
178 \r
179 \r
180 \r
181 \r
182 \r