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