Dear All,
I've implemented a firmware that is running well but sometimes I have an issue.
I've implemented a sleep routine like this:
void sleep_us(uint us) { uint target; uchar overflow; uint now; TIM_TFLG2_TOF = 1; target = TIM_TCNT + us; if (TIM_TCNT > target) { while(TIM_TFLG2_TOF == 0); TIM_TFLG2_TOF = 1; } while(TIM_TCNT < target); }
This routine is used to sleep a specified amouth of microseconds.
It is then used to implement the sleep_ms routine that is like this:
void sleep_ms(uint ms) { uint i; for (i = 0; i < ms; i++) sleep_us(1000); }
Everything seems to go just fine, but sometimes the sleep_ms sleeps for more time than required. For example, even if I require a sleep of 1000 ms it could happen that the sleep_ms blocks for some seconds.
While calling the sleep_us and sleep_ms there are two PIT active along with their interrupt service routine, but I don't think that they could justify the so-long sleep interval.
The timer init configuration is set using a prescaler in order to have a 1us period.
void timer_init() { TIM_TSCR1_PRNT = 1; TIM_PTPSR = 0x2f; TIM_OCPD = 0xff; TIM_TSCR1_TEN = 1; }
I cannot figure out why the sleep should wait more time than expected. Can someone give me some hints on how to solve this problem?
Thanks in advance,
ale
IM_
PTPSR = tim_prescaler;
TIM_TSCR1_TEN = 0; TIM_OCPD = 0xff; // scollego tutti dalle uscite. TIM_TSCR1_TEN = 1;