STM32 Delay Function.
The first thing I noticed after switching from AVR to STM32 was the absence of the familiar delay functions delay_us() and delay_ms(). Well, if they're missing, they're missing, I thought at the time, and whenever I needed a delay, I did it like this.
And I picked the time_delay variable experimentally. No, of course, before implementing a delay with a for() loop, the first thing I did was search the internet for how people do it. I found an implementation using systick, but I didn't want to dive into how the timer works right away and still hoped to find a simpler way.
Time passed, and in one project I needed to measure time intervals a bit more precisely than a for() loop allowed. The situation was that after each read of a chip I had to wait while it updated its register values, and a lot of data had to be read and then displayed on a TFT display; as a result, the display was redrawn very rarely, and it looked dreary.
The solution was found by chance: in one of my previous articles I described how DWT works, and that's what I implemented it with.
Better, but these functions are themselves a source of error: first, when the function is called, registers are pushed onto the stack; second, the number of cycles is calculated, and the counter is reset and started; all of this adds up, and with a delay of a few microseconds the error will be significant. So what can we improve? Enable the counter once during initialization, write zero to the counter register, start the counter and never stop it again.
Done; now all that's left is to implement the delay function.
This implementation works even if the counter overflows after t0 is assigned. Let's look at two scenarios, but for simplicity we'll use uint8_t instead of uint32_t.
t0 = 1, us_count_tic = 100 and DWT->CYCCNT = 10; then we get (uint8_t)(10 - 1) < 100; the counter value will grow, and when it reaches 101 the loop will stop.
If t0 = 255, we get (uint8_t)(10 - 255) < 100; since delta returns a uint32_t value (in this case I replaced it with uint8_t), (uint8_t)(10 - 255) = 11, and the situation develops the same way as in the previous example.
That's all; I hope someone finds the article useful.
void delay(uint32_t time_delay)
{
uint32_t i;
for(i = 0; i < time_delay; i++);
}
And I picked the time_delay variable experimentally. No, of course, before implementing a delay with a for() loop, the first thing I did was search the internet for how people do it. I found an implementation using systick, but I didn't want to dive into how the timer works right away and still hoped to find a simpler way.
Time passed, and in one project I needed to measure time intervals a bit more precisely than a for() loop allowed. The situation was that after each read of a chip I had to wait while it updated its register values, and a lot of data had to be read and then displayed on a TFT display; as a result, the display was redrawn very rarely, and it looked dreary.
The solution was found by chance: in one of my previous articles I described how DWT works, and that's what I implemented it with.
#define DWT_CYCCNT *(volatile unsigned long *)0xE0001004
#define DWT_CONTROL *(volatile unsigned long *)0xE0001000
#define SCB_DEMCR *(volatile unsigned long *)0xE000EDFC
void delay_us(uint32_t us)
{
int32_t us_count_tick = us * (SystemCoreClock/1000000);
//enable the counter
SCB_DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
//reset the counter register
DWT_CYCCNT = 0;
//start the counter
DWT_CONTROL |= DWT_CTRL_CYCCNTENA_Msk;
while(DWT_CYCCNT < us_count_tick);
//stop the counter
DWT_CONTROL &= ~DWT_CTRL_CYCCNTENA_Msk;
}
//////////////////////////////
void delay_ms(uint32_t ms)
{
int32_t ms_count_tick = ms * (SystemCoreClock/1000);
//enable the counter
SCB_DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
//reset the counter register
DWT_CYCCNT = 0;
//start the counter
DWT_CONTROL|= DWT_CTRL_CYCCNTENA_Msk;
while(DWT_CYCCNT < ms_count_tick);
//stop the counter
DWT_CONTROL &= ~DWT_CTRL_CYCCNTENA_Msk;
}Better, but these functions are themselves a source of error: first, when the function is called, registers are pushed onto the stack; second, the number of cycles is calculated, and the counter is reset and started; all of this adds up, and with a delay of a few microseconds the error will be significant. So what can we improve? Enable the counter once during initialization, write zero to the counter register, start the counter and never stop it again.
#define DWT_CYCCNT *(volatile unsigned long *)0xE0001004
#define DWT_CONTROL *(volatile unsigned long *)0xE0001000
#define SCB_DEMCR *(volatile unsigned long *)0xE000EDFC
void DWT_Init(void)
{
//enable the counter
SCB_DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
//reset the counter register
DWT_CYCCNT = 0;
//start the counter
DWT_CONTROL |= DWT_CTRL_CYCCNTENA_Msk;
}
Done; now all that's left is to implement the delay function.
static __inline uint32_t delta(uint32_t t0, uint32_t t1)
{
return (t1 - t0);
}
void delay_us(uint32_t us)
{
uint32_t t0 = DWT->CYCCNT;
uint32_t us_count_tic = us * (SystemCoreClock/1000000);
while (delta(t0, DWT->CYCCNT) < us_count_tic) ;
}
This implementation works even if the counter overflows after t0 is assigned. Let's look at two scenarios, but for simplicity we'll use uint8_t instead of uint32_t.
t0 = 1, us_count_tic = 100 and DWT->CYCCNT = 10; then we get (uint8_t)(10 - 1) < 100; the counter value will grow, and when it reaches 101 the loop will stop.
If t0 = 255, we get (uint8_t)(10 - 255) < 100; since delta returns a uint32_t value (in this case I replaced it with uint8_t), (uint8_t)(10 - 255) = 11, and the situation develops the same way as in the previous example.
That's all; I hope someone finds the article useful.