Real Time Clock, once again
Posted: Mon Apr 14, 2014 3:41 am
I am not sure if this is ChibiIOS question or maybe a general stm32 question or maybe even general programming question:
I am trying to implement precise event scheduling. I use
(that's 0.01ms sysclock)
Now, let's assume that I am generating PWM programmatically (that would be just an example):
This code is supposed to sleep till the next half-millisecond point in time, do job, and loop.
Now the reality: chTimeNow() would always use HSE, right? HSE is not exactly precise, it ticks +-10% to real time. So, my code would not generate a 1KHz PWM but it would generate something like a 910Hz or 1100Hz PWM. How can I improve precision?
I cannot use LSE because LSE granularity is 1ms (is it?), so I cannot measure half a ms times using LSE via RTC. Also there is 'sleep till RTC point' function.
Ideas? Suggestions? How stable is HSE precision? Should I run some dynamic precision test by measuring a second with LSE and with HSE, and adjust to that HSE time error coefficient? That would only help me is HSE imprecision is stable. Is it? Does it depend on temperature or the weather on mars or is it changing over time randomly?
I am trying to implement precise event scheduling. I use
Code: Select all
#define CH_FREQUENCY 100000(that's 0.01ms sysclock)
Now, let's assume that I am generating PWM programmatically (that would be just an example):
Code: Select all
while(true) {
time nextHalfMs = (chTimeNow() + 49) % 50;
timeToSleep = nextHalfMs - chTimeNow();
chThdSleep(timeToSleep);
toggelePin();
}This code is supposed to sleep till the next half-millisecond point in time, do job, and loop.
Now the reality: chTimeNow() would always use HSE, right? HSE is not exactly precise, it ticks +-10% to real time. So, my code would not generate a 1KHz PWM but it would generate something like a 910Hz or 1100Hz PWM. How can I improve precision?
I cannot use LSE because LSE granularity is 1ms (is it?), so I cannot measure half a ms times using LSE via RTC. Also there is 'sleep till RTC point' function.
Ideas? Suggestions? How stable is HSE precision? Should I run some dynamic precision test by measuring a second with LSE and with HSE, and adjust to that HSE time error coefficient? That would only help me is HSE imprecision is stable. Is it? Does it depend on temperature or the weather on mars or is it changing over time randomly?