]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_STM32F107_GCC_Rowley/timertest.c
Remove unnecessary use of portLONG, portCHAR and portSHORT.
[freertos] / Demo / CORTEX_STM32F107_GCC_Rowley / timertest.c
1 /*\r
2     FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4     This file is part of the FreeRTOS distribution.\r
5 \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
14     license details.\r
15 \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
19     more details.\r
20 \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
24 \r
25 \r
26     ***************************************************************************\r
27     *                                                                         *\r
28     * The FreeRTOS eBook and reference manual are available to purchase for a *\r
29     * small fee. Help yourself get started quickly while also helping the     *\r
30     * FreeRTOS project! See http://www.FreeRTOS.org/Documentation for details *\r
31     *                                                                         *\r
32     ***************************************************************************\r
33 \r
34     1 tab == 4 spaces!\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 short ) 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 vSetupHighFrequencyTimer( void );\r
83 \r
84 /* Stores the value of the maximum recorded jitter between interrupts. */\r
85 volatile unsigned short usMaxJitter = 0;\r
86 \r
87 /* Variable that counts at 20KHz to provide the time base for the run time\r
88 stats. */\r
89 unsigned long ulRunTimeStatsClock = 0UL;\r
90 \r
91 /*-----------------------------------------------------------*/\r
92 \r
93 void vSetupHighFrequencyTimer( void )\r
94 {\r
95 unsigned long ulFrequency;\r
96 TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;\r
97 NVIC_InitTypeDef NVIC_InitStructure;\r
98 \r
99 \r
100         /* Enable timer clocks */\r
101         RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM2, ENABLE );\r
102         RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3, ENABLE );\r
103 \r
104         /* Initialise data. */\r
105         TIM_DeInit( TIM2 );\r
106         TIM_DeInit( TIM3 );\r
107         TIM_TimeBaseStructInit( &TIM_TimeBaseStructure );\r
108 \r
109         /* Time base configuration for timer 2 - which generates the interrupts. */\r
110         ulFrequency = configCPU_CLOCK_HZ / timerINTERRUPT_FREQUENCY;\r
111         TIM_TimeBaseStructure.TIM_Period = ( unsigned short ) ( ulFrequency & 0xffffUL );\r
112         TIM_TimeBaseStructure.TIM_Prescaler = 0x0;\r
113         TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;\r
114         TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;\r
115         TIM_TimeBaseInit( TIM2, &TIM_TimeBaseStructure );\r
116         TIM_ARRPreloadConfig( TIM2, ENABLE );\r
117 \r
118 \r
119         /* Configuration for timer 3 which is used as a high resolution time\r
120         measurement. */\r
121         TIM_TimeBaseStructure.TIM_Period = ( unsigned short ) 0xffff;\r
122         TIM_TimeBaseInit( TIM3, &TIM_TimeBaseStructure );\r
123         TIM_ARRPreloadConfig( TIM3, ENABLE );\r
124 \r
125         /* Enable TIM2 IT.  TIM3 does not generate an interrupt. */\r
126         NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;\r
127         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;\r
128         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = timerHIGHEST_PRIORITY;\r
129         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;\r
130         NVIC_Init( &NVIC_InitStructure );\r
131         TIM_ITConfig( TIM2, TIM_IT_Update, ENABLE );\r
132 \r
133         /* Finally, enable both timers. */\r
134         TIM_Cmd( TIM2, ENABLE );\r
135         TIM_Cmd( TIM3, ENABLE );\r
136 }\r
137 /*-----------------------------------------------------------*/\r
138 \r
139 void TIM2_IRQHandler( void )\r
140 {\r
141 static unsigned short usLastCount = 0, usSettleCount = 0, usMaxDifference = 0;\r
142 unsigned short usThisCount, usDifference;\r
143 \r
144         /* Capture the free running timer 3 value as we enter the interrupt. */\r
145         usThisCount = TIM3->CNT;\r
146 \r
147         if( usSettleCount >= timerSETTLE_TIME )\r
148         {\r
149                 /* What is the difference between the timer value in this interrupt\r
150                 and the value from the last interrupt. */\r
151                 usDifference = usThisCount - usLastCount;\r
152 \r
153                 /* Store the difference in the timer values if it is larger than the\r
154                 currently stored largest value.  The difference over and above the\r
155                 expected difference will give the 'jitter' in the processing of these\r
156                 interrupts. */\r
157                 if( usDifference > usMaxDifference )\r
158                 {\r
159                         usMaxDifference = usDifference;\r
160                         usMaxJitter = usMaxDifference - timerEXPECTED_DIFFERENCE_VALUE;\r
161                 }\r
162         }\r
163         else\r
164         {\r
165                 /* Don't bother storing any values for the first couple of\r
166                 interrupts. */\r
167                 usSettleCount++;\r
168         }\r
169 \r
170         /* Remember what the timer value was this time through, so we can calculate\r
171         the difference the next time through. */\r
172         usLastCount = usThisCount;\r
173 \r
174         /* Keep a count of the number of interrupts as a time base for the run time\r
175         stats collection. */\r
176         ulRunTimeStatsClock++;\r
177 \r
178     TIM_ClearITPendingBit( TIM2, TIM_IT_Update );\r
179 }\r
180 \r
181 \r
182 \r
183 \r
184 \r
185 \r
186 \r
187 \r