]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_STM32F107_GCC_Rowley/timertest.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / CORTEX_STM32F107_GCC_Rowley / timertest.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /* High speed timer test as described in main.c. */\r
30 \r
31 /* Scheduler includes. */\r
32 #include "FreeRTOS.h"\r
33 \r
34 /* Library includes. */\r
35 #include "stm32f10x_lib.h"\r
36 #include "stm32f10x_tim.h"\r
37 #include "stm32f10x_map.h"\r
38 \r
39 /* The set frequency of the interrupt.  Deviations from this are measured as\r
40 the jitter. */\r
41 #define timerINTERRUPT_FREQUENCY                ( ( unsigned short ) 20000 )\r
42 \r
43 /* The expected time between each of the timer interrupts - if the jitter was\r
44 zero. */\r
45 #define timerEXPECTED_DIFFERENCE_VALUE  ( configCPU_CLOCK_HZ / timerINTERRUPT_FREQUENCY )\r
46 \r
47 /* The highest available interrupt priority. */\r
48 #define timerHIGHEST_PRIORITY                   ( 0 )\r
49 \r
50 /* Misc defines. */\r
51 #define timerMAX_32BIT_VALUE                    ( 0xffffffffUL )\r
52 #define timerTIMER_1_COUNT_VALUE                ( * ( ( unsigned long * ) ( TIMER1_BASE + 0x48 ) ) )\r
53 \r
54 /* The number of interrupts to pass before we start looking at the jitter. */\r
55 #define timerSETTLE_TIME                        5\r
56 \r
57 /*-----------------------------------------------------------*/\r
58 \r
59 /*\r
60  * Configures the two timers used to perform the test.\r
61  */\r
62 void vSetupHighFrequencyTimer( void );\r
63 \r
64 /* Stores the value of the maximum recorded jitter between interrupts. */\r
65 volatile unsigned short usMaxJitter = 0;\r
66 \r
67 /* Variable that counts at 20KHz to provide the time base for the run time\r
68 stats. */\r
69 unsigned long ulRunTimeStatsClock = 0UL;\r
70 \r
71 /*-----------------------------------------------------------*/\r
72 \r
73 void vSetupHighFrequencyTimer( void )\r
74 {\r
75 unsigned long ulFrequency;\r
76 TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;\r
77 NVIC_InitTypeDef NVIC_InitStructure;\r
78 \r
79 \r
80         /* Enable timer clocks */\r
81         RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM2, ENABLE );\r
82         RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3, ENABLE );\r
83 \r
84         /* Initialise data. */\r
85         TIM_DeInit( TIM2 );\r
86         TIM_DeInit( TIM3 );\r
87         TIM_TimeBaseStructInit( &TIM_TimeBaseStructure );\r
88 \r
89         /* Time base configuration for timer 2 - which generates the interrupts. */\r
90         ulFrequency = configCPU_CLOCK_HZ / timerINTERRUPT_FREQUENCY;\r
91         TIM_TimeBaseStructure.TIM_Period = ( unsigned short ) ( ulFrequency & 0xffffUL );\r
92         TIM_TimeBaseStructure.TIM_Prescaler = 0x0;\r
93         TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;\r
94         TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;\r
95         TIM_TimeBaseInit( TIM2, &TIM_TimeBaseStructure );\r
96         TIM_ARRPreloadConfig( TIM2, ENABLE );\r
97 \r
98 \r
99         /* Configuration for timer 3 which is used as a high resolution time\r
100         measurement. */\r
101         TIM_TimeBaseStructure.TIM_Period = ( unsigned short ) 0xffff;\r
102         TIM_TimeBaseInit( TIM3, &TIM_TimeBaseStructure );\r
103         TIM_ARRPreloadConfig( TIM3, ENABLE );\r
104 \r
105         /* Enable TIM2 IT.  TIM3 does not generate an interrupt. */\r
106         NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;\r
107         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;\r
108         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = timerHIGHEST_PRIORITY;\r
109         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;\r
110         NVIC_Init( &NVIC_InitStructure );\r
111         TIM_ITConfig( TIM2, TIM_IT_Update, ENABLE );\r
112 \r
113         /* Finally, enable both timers. */\r
114         TIM_Cmd( TIM2, ENABLE );\r
115         TIM_Cmd( TIM3, ENABLE );\r
116 }\r
117 /*-----------------------------------------------------------*/\r
118 \r
119 void TIM2_IRQHandler( void )\r
120 {\r
121 static unsigned short usLastCount = 0, usSettleCount = 0, usMaxDifference = 0;\r
122 unsigned short usThisCount, usDifference;\r
123 \r
124         /* Capture the free running timer 3 value as we enter the interrupt. */\r
125         usThisCount = TIM3->CNT;\r
126 \r
127         if( usSettleCount >= timerSETTLE_TIME )\r
128         {\r
129                 /* What is the difference between the timer value in this interrupt\r
130                 and the value from the last interrupt. */\r
131                 usDifference = usThisCount - usLastCount;\r
132 \r
133                 /* Store the difference in the timer values if it is larger than the\r
134                 currently stored largest value.  The difference over and above the\r
135                 expected difference will give the 'jitter' in the processing of these\r
136                 interrupts. */\r
137                 if( usDifference > usMaxDifference )\r
138                 {\r
139                         usMaxDifference = usDifference;\r
140                         usMaxJitter = usMaxDifference - timerEXPECTED_DIFFERENCE_VALUE;\r
141                 }\r
142         }\r
143         else\r
144         {\r
145                 /* Don't bother storing any values for the first couple of\r
146                 interrupts. */\r
147                 usSettleCount++;\r
148         }\r
149 \r
150         /* Remember what the timer value was this time through, so we can calculate\r
151         the difference the next time through. */\r
152         usLastCount = usThisCount;\r
153 \r
154         /* Keep a count of the number of interrupts as a time base for the run time\r
155         stats collection. */\r
156         ulRunTimeStatsClock++;\r
157 \r
158     TIM_ClearITPendingBit( TIM2, TIM_IT_Update );\r
159 }\r
160 \r
161 \r
162 \r
163 \r
164 \r
165 \r
166 \r
167 \r