0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Do you have any timer to spare? \$\endgroup\$ – Long Pham Jul 23 at 11:08
  • \$\begingroup\$ 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 \$\endgroup\$ – Long Pham Jul 23 at 11:19
  • \$\begingroup\$ 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. \$\endgroup\$ – Rokta Jul 23 at 12:19
  • \$\begingroup\$ Unless you are trying to use the overdrive mode, you are substantially overestimating the required speed and time precision. \$\endgroup\$ – Chris Stratton Jul 23 at 14:46
1
\$\begingroup\$

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).

If you have a free UART TX pin, you can try implementing the procedure in Maxim Tutorial 214 Using a UART to Implement a 1-Wire Bus Master. I've found that it works best with the reset pulse sent at 7407 baud, and the data at 133333 baud. See also application note 27 for the CRC algorithm if you don't have this already.

\$\endgroup\$
  • 1
    \$\begingroup\$ This doesn't have to be anywhere near as precise as the asker thinks, a delay loop should work \$\endgroup\$ – Chris Stratton Jul 23 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.