dwt_delay icon indicating copy to clipboard operation
dwt_delay copied to clipboard

DWT _delay(microseconds)

Open RhonyD opened this issue 5 years ago • 10 comments

The timer function hangs or stops after a few seconds if placed in a infinite while loop.Please verify.

RhonyD avatar Mar 21 '19 11:03 RhonyD

Yep, it is possible after approx. 30 seconds @72Mhz. There was a typo in the definitions, so signed int was used instead of unsigned. Sorry for that :)

keatis avatar Mar 26 '19 22:03 keatis

I feel lucky that i didn't hit into such situation with this delay (or reason was not obvious). Anyway, I ran some simple loops after fix: did nothing, blinked LEDs, printed to uart. And it is still running for couple of hours. Pls reopen if it hangs again.

keatis avatar Mar 26 '19 23:03 keatis

i want to use 0.4 microseconds delay, for ws2812b led, but DWT dont support it, as it is unsigned int32

sumitsaboo07 avatar Nov 11 '19 13:11 sumitsaboo07

@sumitsaboo07 There's no nanodelay at the moment. I plan to add it as soon as I have any measurement tools so I can test accuracy of such delays.

You can copy-paste DWT_Delay() with other name and change a line where delayTicks is calculated, so it looks like delayTicks = ns * (SystemCoreClock / 1e9). This could give you a delay function in nanoseconds. But consider the fact, that you can't get accurate results with 100ns delays or less: one tick is roughly 14ns @72Mhz, and you always have some ticks as an overhead you can't eliminate. Therefore, it should suffice your need for 0.4 us = 400 ns delay. Good luck.

keatis avatar Nov 16 '19 17:11 keatis

is there any fastled library for atollic true studio, for ws2812 as we use in arduino?? as with DWT Delay also i am not able to blink ws2812b led.?

sumitsaboo07 avatar Nov 19 '19 10:11 sumitsaboo07

Sorry, i dont know if there's any good fastled lib for Atollic/Eclipse. But you can use this lib directly in arduino (stm32-based, of course), and add nano-delays as i wrote earlier.

keatis avatar Nov 22 '19 14:11 keatis

stumbled upon this by accident, because i couldn't get the DWT_init working too. i am also working with ws1812 leds, i use this library: https://github.com/hubmartin/WS2812B_STM32F3

domingguss avatar Dec 04 '19 14:12 domingguss

Hi. What is exact problem and what are you trying to do? If i got it right, the lib you linked doesn't require any delay library, since it uses TIM2 to drive DMA transfer.

Another guess is that optimization could interfere. What level do you use when compiling? Must admit, I didn't test with -O1 and higher. You can try changing vars to be volatile in DWT_Delay(): volatile uint32_t startTick ....

keatis avatar Dec 04 '19 15:12 keatis

There's definitely no need to define any variables as volatile, DWT->CYCCNT register is volatile and it's enough. FYI, I've successfully used this code with -Os -flto optimizations.

Nable80 avatar Dec 18 '19 00:12 Nable80

I have tried this on STM32F407VG. After changing header file name, build succeeded. But code gets trapped into an infinite loop inside the delay. Any idea how to fix it? THanks

TechSerpent avatar May 31 '21 16:05 TechSerpent