cancel
Showing results for 
Search instead for 
Did you mean: 

Is Systick the way to generate a delay function

bl
Associate II
Posted on April 12, 2013 at 08:49

Hi Folks (and Clive)

I have generated timing delays using systick and I want some comments on whether it was the right way to go:-

GPIO_InitTypeDef GPIO_InitStructure;

static __IO uint32_t TimingDelay;

void Delay(__IO uint32_t nTime)

    {

    TimingDelay = nTime;

    while (TimingDelay != 0)

;

    }

void TimingDelay_Decrement(void)

    {

    if (TimingDelay != 0x00)

{

TimingDelay--;

}

    }

extern ''C'' void SysTick_Handler(void)

    {

    TimingDelay_Decrement();

    }

I believe that systick is a low priority - so can this code get stuck if there is a pending higher priority interrupt??

CS

3 REPLIES 3
zzdz2
Associate II
Posted on April 12, 2013 at 11:29

I make short delays without interrupts:

#define TICKSGN ((SysTick_VAL_CURRENT_Msk+1)>>1)
#define WAITMAX (SysTick_VAL_CURRENT_Msk >> 1)
static inline void waittick(uint32_t step)
{
static uint32_t tick0;
if (!step)
{
tick0 = SysTick->VAL;
return;
}
tick0 -= step;
while ((tick0 - SysTick->VAL) & TICKSGN)
{
__NOP();
__NOP();
}
}
static inline void sleep(uint32_t delay)
{
waittick(0);
delay++;
while (delay > WAITMAX)
{
waittick(WAITMAX);
delay -= WAITMAX;
}
waittick(delay);
}

Posted on April 12, 2013 at 13:30

You can change the priority of the SysTick interrupt. Generally speaking you shouldn't be spending enough time in another interrupt for this to be a problem, or using multi millisecond delays in other interrupt routines.

My preference would be for SysTick to increment a millisecond tick variable continuously, and have the delay routine observe that. For finer precision, delays could also use DWT_CYCCNT
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
bl
Associate II
Posted on April 16, 2013 at 10:20

Hi folks 

Thanks for your comments - its good to know I'm not fundamentally going in the wrong direction!! I will have a look at the suggestions for other types of delays as I'm sure they will be useful.

CS