2015-12-25 03:51 PM
I am experiencing weird behavior from my systick delay function.
The following code does not work.void delay_ms(int time_ms){ uint32_t time_done = SystemUptime+time_ms; while(SystemUptime < time_done) { }}However this one does:void dummy(void){ asm volatile(''NOP''); asm volatile(''NOP'');}void delay_ms(int time_ms){ uint32_t time_done = SystemUptime+time_ms; while(SystemUptime < time_done) { dummy(); }}If i use only one nop or do the nop without it being in a function it won't work. What am I doing wrong? #systick-stm32-f12015-12-25 04:23 PM
Perhaps optimization, make sure you use volatile variables appropriately, and uint32_t would be a better choice.
Also, a fundamental issue with the comparisons. You need to use the formvolatile uint32_t currenttime; // changed by tick interrupt
void delay(uint32 delaytime)
{
uint32_t starttime = currenttime;
while((currenttime - starttime) < delaytime);
}
as this doesn't fail in the number space wrapping condition
2015-12-25 04:34 PM
Thank you for your fast reply, now that I think about it not using volatile is most likely the problem. I'll check and report back when I get access to my box again.
2015-12-26 02:39 PM
Using volatile fixed it, my comparison code is working fine now. Thank you.