-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Very incorrect timeout handling in pulseInLong #3830
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
Woa! I totally missed it 😄 Fixed by c7b2743 |
The fix does not handle Instead, try: |
True, matthijskooijman. I suggested my solution off the top of my head... the possibility of overflow should be taken into account as well. Thanks! |
@matthijskooijman , you are totally correct. |
Nothing springs to mind just now, but it does sound like something to investigate. If you want to investigate, have a look at this script I created to diff .elf file symbol tables (there is also a script to diff ASM output and reducing the amount of junk from differing addresses): https://github.com/matthijskooijman/scripts/blob/master/Arduino/diff-avr-sym |
After a loooooong (way longer than a pulse 😄 ) analysis it turned out that the three 32bit arguments involved in the subtraction lead the compiler to allocate these variables in the stack instead than using registers, thus the +72 byte. Strangely, optimizing the function with No math guru over the web came out with a better implementation so I'm merging @matthijskooijman's one with a note about this strange behaviour |
Ah, that makes sense - with my suggestion, the program needs to keep both track of |
The three While loops in pulseInLong (at wiring_pulse.c) increase "numloops" from 0 to "maxloops" as a way to check and enforce the timeout parameter. However, "maxloops" is defined in clock cycles, but each loop run takes much more than a single clock cycle.
This results in timeouts that are actually over 20 times longer than the user requests.
Since this function uses micros() anyway for timing the pulse, I suggest replacing the numloops/maxloops method with checks against micros(). For instance:
// After function header and pin definitions...
// etc.
The text was updated successfully, but these errors were encountered: