Forum > Embedded - ARM

STM32 microseconds delay

(1/1)

funlw65:
I'm aware that if you need microseconds timing precision, STM32L1 is not a good bet. For that matter, is better to use an 8bit/16bit micro and interface it with I2C or SPI. Onewire protocol is one example where you need good timing for the thing to work.

Still, I have a delay function in assembler that is good enough for such a case, it works well when I use it with HAL/LL driver no matter the level of optimization in the gcc compiler, but it needs some tweaking if I use it with SPL driver and it depends on the optimization level. If optimization is disabled, if I use SPL, I can't make it work. Worse, I do not posses an oscilloscope. As I understand, freepascal do not optimize for ARM yet...

Anyway, I need help to write this function in freepascal, can anyone know how to do it? Thank you, whatever the outcome!


--- Code: C  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---static __INLINE void my_delay_us(uint32_t us){  us *= 24; // it was 32  us--;  asm volatile("   mov r0, %[us]\n\t"               "1: subs r0, #5\n\t"               "   bhi 1b\n\t"               :               : [us] "r" (us)               : "r0");}

MiR:
The best solution is to read the chapter about timers in the reference manual of your chip.
When you know the operating frequency of your chip you can get very good results for complex timing requirements.

Another good option is to use the cycle counter of cortex-m chips, a quick google query brought up this page:

https://github.com/keatis/dwt_delay/blob/master/dwt_delay.c

which shows the idea...

you will have to include cortexm3.pp in your code to get the DWT Registers resolved.

funlw65:
Last time I tried DWT it was a fail, obviously my fault. Thank you for the link, I will try that, first in C as I really needed a good, reliable solution, then in freepascal.

Navigation

[0] Message Index

Go to full version