cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f10x rtc frequency measurement using internal timer

chmorgan
Associate II
Posted on August 13, 2014 at 00:06

Hello.

I'm following the procedure in AN2604 and trying to use an internal timer to measure the rtc frequency by looping back the rtc into a timer pin, in my case PA8 (Timer 1 channel 1), and then using PWM input capture mode.

I was wondering if anyone has tried to do this before and been successful with it. I'm looking at the stm32 standard peripheral library pwm input example and have implemented my code using that as a basis. I can paste here but really I'd like to know if the idea is feasible at all given a 72Mhz system clock and the ~512Hz rtc signal to measure.

#stm32f1
7 REPLIES 7
Posted on August 13, 2014 at 02:13

You can prescale the timer, 72 MHz / 4 = 18 MHz, 18 MHz / 512 Hz = 35156.25 which would fit in the 16-bit counter.

You might want to use Input Capture, and use that to count 1-8 periods via the input prescaler

The F2/F4 have 32-bit timers, and direct internal connectivity to LSI/LSE
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
chmorgan
Associate II
Posted on August 13, 2014 at 13:08

Hmm. I do recall that input capture is a bit simpler than the pwm input capture. I'm getting oddly inconsistent results using pwm input capture but I've been assuming thats my misconfiguration of the capture or that my ISR isn't correct.

On the topic of precision, the rtc calibration requires measurements in the ppm range. Have you done this rtc calibration with input capture and gotten enough precision to accomplish the calibration? I can't seem to find anyone that has actually used this approach, either pwm capture or input capture, to self calibrate the rtc using the HSE.

Posted on August 13, 2014 at 15:12

I've tinkered with it, depends on what clock you're trying to calibrate LSI/LSE. The former is wildly unstable, being dependent on voltage and temperature. I was playing with TCXO and GPS 1PPS sources.

I would use HSE to clock the part, not the VCO/PLL. I would measure a significant number of cycles, this could be done with Input Capture, and an IRQ/DMA of the CCRx, and then doing delta computations for the ticks per cycle. Consider also counting the external clocks (not the period), and do that over a longer observation window. A minute of 512 Hz would easily fit in 16-bit
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
chmorgan
Associate II
Posted on August 14, 2014 at 12:58

Hi Clive.

In this case we are using the HSE for the system clock.

I'm not sure what you mean by measuring external clocks over a larger window. Could you explain more? Is that something that the input capture can be configured to help with?

Posted on August 14, 2014 at 14:07

Externally clocking is another mode of the counter, you'd review the count periodically, by say using the SysTick or TIM (clocking the system via HSE) and counting off seconds, or longer periods.

Each second CNT should advance ~512, in a minute it should advance ~30720. It might be possible to latch this into CCRx registers, or use DMA

In Input Capture you could measure the period (or 1,2,4,8 periods as permitted) in ticks of 8 MHz (HSE)

512 Hz / 4 = 128 Hz

8000000 / 128 = 62500  (fits in 16-bit)

So with Input Capture, Input Prescaler of 4, Timer @ 8 MHz, you should be able to observe CCR1 advance by ~62500 at each CC1 interrupt.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
chmorgan
Associate II
Posted on August 18, 2014 at 16:10

Ahh I see.

I'm trying this approach now. While I am seeing some odd amount of measurement error that I can only imagine may be due to using the timer prescaler, but can't explain, this approach has promise.

So far it looks like measurement times on the order of 10s of minutes or more are required to get any level of accuracy nearing the >100ppm that is trying to be calibrated away. May end up having to go with some hardware to measure it more accurately although there could be some measurement error I'm introducing somewhere.

To recap, I'm using:

- TIM1 in external clock mode with a prescaler calculated to fit the total count inside of a 16bit number.

- Systick (via the OS on the board) to provide a system time measurement.

- Configure TIM1 to overflow at X minutes of pulses + prescaler

- Upon ISR for TIM1 overflow the system time is captured

chmorgan
Associate II
Posted on August 18, 2014 at 20:31

And of course I mean <100ppm, not >100ppm.