# STM32L4 μs delay

I am trying to implement a onewire protocol on an STM32L432KCU, so I need quite precise timing (need delay precise as 2μs).

I was wondering if there is any way to do this using the HAL drivers (since the rest of the project I am working on is using HAL) or if any other workaround exists.

I have already tried to use this library but it seems not to work for stm32l4 series.

• Do you have any timer to spare? – Long Pham Jul 23 at 11:08
• DWT based delay should work for ARM Cortex M3 - M4 STM32s. Did you try to inspect the author's code? "change to whatever MCU you use" - line 23 file dwt_delay.c – Long Pham Jul 23 at 11:19
• If you need to use HAL drivers you could probably implement it with timer and DMA. Not sure how complex implementation would be as I didnt try anything similar. – Rokta Jul 23 at 12:19
• Unless you are trying to use the overdrive mode, you are substantially overestimating the required speed and time precision. – Chris Stratton Jul 23 at 14:46

HAL library functions are too slow for tasks involving precise timing, and the MCU in the linked post is twice as fast as yours, so it'd be even worse. For a bit-banging 1-wire implementation only 3 register accesses are needed: GPIOx->IDR to read the port, GPIOx->BSRR to output a bit, and SysTick->VAL, see below.
If you can't get DWT->CYCCNT to work, there is the SysTick->VAL counter, counting system clock cycles from the value in SysTick->LOAD downwards to 0, that can used to measure short intervals, just keep in mind that it gets reset once every millisecond (assuming you didn't mess with the HAL SysTick configuration).