cancel
Showing results for 
Search instead for 
Did you mean: 

Systick delay weird behavior. [STM32F103CB] [GNUArmEclipse]

zhausq
Associate III
Posted on December 26, 2015 at 00:51

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-f1
3 REPLIES 3
Posted on December 26, 2015 at 01:23

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 form

volatile 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
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
zhausq
Associate III
Posted on December 26, 2015 at 01:34

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.

zhausq
Associate III
Posted on December 26, 2015 at 23:39

Using volatile fixed it, my comparison code is working fine now. Thank you.