]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_STM32F103_Primer_GCC/timertest.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Demo / CORTEX_STM32F103_Primer_GCC / timertest.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 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 vSetupTimerTest( void );\r
62 \r
63 /* Interrupt handler in which the jitter is measured. */\r
64 void vTimer2IntHandler( void );\r
65 \r
66 /* Stores the value of the maximum recorded jitter between interrupts. */\r
67 volatile unsigned short usMaxJitter = 0;\r
68 \r
69 /*-----------------------------------------------------------*/\r
70 \r
71 void vSetupTimerTest( void )\r
72 {\r
73 unsigned long ulFrequency;\r
74 TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;\r
75 NVIC_InitTypeDef NVIC_InitStructure;\r
76 \r
77 \r
78         /* Enable timer clocks */\r
79         RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM2, ENABLE );\r
80         RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3, ENABLE );\r
81 \r
82         /* Initialise data. */\r
83         TIM_DeInit( TIM2 );\r
84         TIM_DeInit( TIM3 );\r
85         TIM_TimeBaseStructInit( &TIM_TimeBaseStructure );\r
86 \r
87         /* Time base configuration for timer 2 - which generates the interrupts. */\r
88         ulFrequency = configCPU_CLOCK_HZ / timerINTERRUPT_FREQUENCY;    \r
89         TIM_TimeBaseStructure.TIM_Period = ( unsigned short ) ( ulFrequency & 0xffffUL );\r
90         TIM_TimeBaseStructure.TIM_Prescaler = 0x0;\r
91         TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;\r
92         TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;\r
93         TIM_TimeBaseInit( TIM2, &TIM_TimeBaseStructure );\r
94         TIM_ARRPreloadConfig( TIM2, ENABLE );\r
95 \r
96         \r
97         /* Configuration for timer 3 which is used as a high resolution time\r
98         measurement. */\r
99         TIM_TimeBaseStructure.TIM_Period = ( unsigned short ) 0xffff;\r
100         TIM_TimeBaseInit( TIM3, &TIM_TimeBaseStructure );\r
101         TIM_ARRPreloadConfig( TIM3, ENABLE );\r
102         \r
103         /* Enable TIM2 IT.  TIM3 does not generate an interrupt. */\r
104         NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;\r
105         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;\r
106         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = timerHIGHEST_PRIORITY;\r
107         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;\r
108         NVIC_Init( &NVIC_InitStructure );       \r
109         TIM_ITConfig( TIM2, TIM_IT_Update, ENABLE );\r
110 \r
111         /* Finally, enable both timers. */\r
112         TIM_Cmd( TIM2, ENABLE );\r
113         TIM_Cmd( TIM3, ENABLE );\r
114 }\r
115 /*-----------------------------------------------------------*/\r
116 \r
117 void vTimer2IntHandler( void )\r
118 {\r
119 static unsigned short usLastCount = 0, usSettleCount = 0, usMaxDifference = 0;\r
120 unsigned short usThisCount, usDifference;\r
121 \r
122         /* Capture the free running timer 3 value as we enter the interrupt. */\r
123         usThisCount = TIM3->CNT;\r
124         \r
125         if( usSettleCount >= timerSETTLE_TIME )\r
126         {\r
127                 /* What is the difference between the timer value in this interrupt\r
128                 and the value from the last interrupt. */\r
129                 usDifference = usThisCount - usLastCount;\r
130 \r
131                 /* Store the difference in the timer values if it is larger than the\r
132                 currently stored largest value.  The difference over and above the\r
133                 expected difference will give the 'jitter' in the processing of these\r
134                 interrupts. */\r
135                 if( usDifference > usMaxDifference )\r
136                 {\r
137                         usMaxDifference = usDifference;\r
138                         usMaxJitter = usMaxDifference - timerEXPECTED_DIFFERENCE_VALUE;\r
139                 }\r
140         }\r
141         else\r
142         {\r
143                 /* Don't bother storing any values for the first couple of\r
144                 interrupts. */\r
145                 usSettleCount++;\r
146         }\r
147 \r
148         /* Remember what the timer value was this time through, so we can calculate\r
149         the difference the next time through. */\r
150         usLastCount = usThisCount;\r
151 \r
152     TIM_ClearITPendingBit( TIM2, TIM_IT_Update );\r
153 }\r
154 \r
155 \r
156 \r
157 \r
158 \r
159 \r
160 \r
161 \r