2013-04-11 11:49 PM
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??CS2013-04-12 02:29 AM
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);
}
2013-04-12 04:30 AM
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_CYCCNT2013-04-16 01:20 AM
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