-
Notifications
You must be signed in to change notification settings - Fork 1k
delayMicrosecond sometimes makes much shorter delay than requested. #176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is how esp32duino handles the overflow problem:
I think this solution retains the full uint32_t resolution. Edit: better solution (just like delay() minus the unpredictable yield):
|
Thanks,
Using the interrupt based software counters (ie SysTick) is not the right approach, I think. |
I was just reading up on DWT->CYCCNT. |
that's precisely my question. Use DWT for those one that support it or doing the same implementation for all (using a timer or asm code). |
If you address timing issues, please also have a look at how Unfortunately, it seems that many Arduino cores (if not all) are getting this wrong: they all use unsigned subtraction to compare time values. While that may seem mathematically correct, it's not how it works for the C language (and many others), which is using modular arithmetic. This is the STM32 core implementation: Arduino_Core_STM32/cores/arduino/wiring_time.c Lines 41 to 44 in 0dd2047
The correct way to handle this (and timeout conditions in general) is to use a signed subtraction (or cast to a signed integer after subtraction, that yields the same result).
Actually, it is correct as long as the delay or timeout is less than or equal to INT_MAX, that means the time counter has wrapped around at most once. It would have caused much less trouble if they had defined For those who are not sure and want to try some code, I shared an example: |
I know that even if in your example it still one issue when sub instruction overflow int size:
Anyway, I'm currently not working on this at this time but all time stuff require a deep check. |
I see what you mean. In the Generally I don't feel safe doing subtractions on unsigned integers (been bitten before), however in this case it should be fine. |
Issuer reported here by ut2uz:
http://stm32duino.com/viewtopic.php?f=48&t=2960
Original implementation from wiring_time.h:
Proposed solution by ut2uz:
The text was updated successfully, but these errors were encountered: