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