cancel
Showing results for 
Search instead for 
Did you mean: 

DWT HSE Period Mesuring

smuryginim
Associate II
Posted on April 18, 2013 at 12:20

Hello!

Can i mesure period of 

High Speed External (HSE) oscillator period with help of  DWT?

I initialized DWT and can mesure number of cycles of operations.

I use stm32f207 microcontroller.

Thanks in advice.

p.s. Or maybe are there some other better methods. For example use Timers (is it possible to mesure 20 MHz signal?)

#dwt-hse-stm32
7 REPLIES 7
Posted on April 18, 2013 at 13:31

You have to measure an unknown clock against one you know, you could use TIM5/TIM11, and leverage different clocks against each other.

To measure a 20 MHz signal, you could use it as an external clock source, and either gate it, or periodically sample it, and use that cycle count to get period/frequency.

For slower clocks, you could use input compare, and time stamp edges against a faster clock (like 84 or 168 MHz), or divided down to fit 16-bit range, and measure the delta ticks between two, or more, events.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
smuryginim
Associate II
Posted on April 18, 2013 at 15:03

Thanks for quick answer!

I doesn't completely understood some of your methods.

For example, how to  ''leverage (TIM5/TIM11) different clocks against each other''? Could you give some examples, please?

or ''and either gate it, or periodically sample it, and use that cycle count to get period/frequency''? Some examples will be very appreciated.

Posted on April 18, 2013 at 16:16

Pull a copy of the RM0033

5.2.11 Internal/external clock measurement using TIM5/TIM11

 

It is possible to indirectly measure the frequencies of all on-board clock source generators by means of the input capture of TIM5 channel4 and TIM11 channel1 as shown in Figure 11 and Figure 11 (12?).

 

Internal/external clock measurement using TIM5 channel4

 

TIM5 has an input multiplexer which allows choosing whether the input capture is triggered by the I/O or by an internal clock. This selection is performed through the TI4_RMP [1:0] bits in the TIM5_OR register.

 

The primary purpose of having the LSE connected to the channel4 input capture is to be able to precisely measure the HSI (this requires to have the HSI used as the system clock

 

source). The number of HSI clock counts between consecutive edges of the LSE signal provides a measurement of the internal clock period. Taking advantage of the high precision of LSE crystals (typically a few tens of ppm) we can determine the internal clock frequency with the same resolution, and trim the source to compensate for manufacturing-process and/or temperature- and voltage-related frequency deviations. The HSI oscillator has dedicated, user-accessible calibration bits for this purpose.

There should be input compare examples with the firmware, this method will work for frequencies below say a few hundred KHz, where you can handle CC interrupts and read the CCRx registers. Probably have some examples for measuring LSI and LSE.

I might be convinced to create some others.

A good LSE 32.768KHz crystal allows you to work the calculation back for other clocks, ie you have a known clock, you can extrapolate/interpolate rates of other unknown ones.

The LSI is all over the map, I wouldn't call it remotely stable or predictable, impacted by voltage and temperature.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
smuryginim
Associate II
Posted on April 24, 2013 at 12:07

Clieve, can you help me?

Can i mesure 20 MHz with Input capture mode? Or use 20 Mhz signal to trigger timer? As i know max frequency for timers is 72 MHz and for how many periods of main CLK this signal must be stable (somewhere i saw information that for 3 ticks).

I tried to use timer with external clock source on stm32f207ZE microcontroller. But it doesn't worked. So is my code:

void initializeExternalTimer1(){
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* TIM1 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
/* GPIOE clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
/* GPIOE Configuration: PE.11(TIM1 CH2) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/* GPIOE Configuration: PE.7(TIM1 ETR) */
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
// GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
// GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource7, GPIO_AF_TIM1);
/* TIM1 Input trigger configuration: External Trigger connected to TI2 */
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV2;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM1, &TIM_ICInitStructure);
TIM_TIxExternalClockConfig (TIM1, TIM_TS_TI2FP2, TIM_ICPolarity_Rising, 0) ;
//TIM_ETRConfig(TIM1, TIM_TS_TI2FP2, TIM_ICPolarity_Rising, 0);
TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_External1);
//Mode External Trigger set to External Clock, Mode 1
//TIM_ETRClockMode1Config (TIM1, TIM_ExtTRGPSC_DIV2, TIM_ExtTRGPolarity_NonInverted, 0);
TIM_Cmd (TIM1, ENABLE);
}

Where can be my mistake? I have some misunderstanding on what source pin must be for Timer1 TI2. Is it the same that Timer1_CH2? Thanks in advice.

Posted on April 24, 2013 at 13:46

Can i mesure 20 MHz with Input capture mode? Or use 20 Mhz signal to trigger timer?

No input capture wouldn't be the way to go. You want to use the 20 MHz as an external clock source, and use the timer to integrate of 1 ms. The delta between 1 ms samples would be ~20000, ticks.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
smuryginim
Associate II
Posted on April 24, 2013 at 14:03

Thanks, but what pin i must use for this mode? Is my configuration in previous message right? Hear is my initialization for using TIM1 ETR pin. It starts to count, but then stops

void initializeExternalTimer1(){
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* TIM1 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
/* GPIOE clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
// /* GPIOE Configuration: PE.11(TIM1 CH2) */
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
// GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
// GPIO_Init(GPIOE, &GPIO_InitStructure);
/* GPIOE Configuration: PE.7(TIM1 ETR) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource7, GPIO_AF_TIM1);
/* TIM1 Input trigger configuration: External Trigger connected to TI2 */
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV2;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM1, &TIM_ICInitStructure);
//for TI
//TIM_TIxExternalClockConfig (TIM1, TIM_TS_TI2FP2, TIM_ICPolarity_Rising, 0) ;
//TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_External1);
//for ETR IN RM0033 p. 372
TIM1->SMCR = 0;
TIM1->SMCR |= TIM_ExtTRGPSC_DIV2; //2. set prescaller
TIM1->SMCR |= TIM_ICPolarity_Rising; //3. rising edge
TIM1->SMCR |= 0x4000; // 4. Enable external clock mode 2 by writing ECE=1
TIM_Cmd (TIM1, ENABLE);
}

Posted on April 24, 2013 at 15:06

Thanks, but what pin i must use for this mode? Is my configuration in previous message right?

I'm on the road now, so will keep the answers brief. As I recall you should be able to route CH1, CH2 or ETR as external clock sources.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..