fix OneWire timing and DigitalInOut.switch_to_input() #973
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
OneWire didn't work at all, just sending one long pulse.
common_hal_digitalio_digitalinout_switch_to_input()
was buggy, actually failing to change the port direction. Fixed, and cleaned upDigitalInOut.c
a bit, usinghri_
calls to save a bit of time in certain cases, and avoiding some redundant operations.After that fix, OneWire was sort of working, but acting erratically, sometimes getting a CRC error. This problem was not present in 2.x. OneWire does bit-banging with precise time intervals using non-interruptable time-delay calls for small numbers of microseconds. It turns off interrupts while making these calls. But it was using
common_hal_mcu_delay_us()
, which callstick_delay()
, which depends on an interrupt to handleSysTick
reaching zero. So the time-delay calls were inaccurate in cases whenSysTick
reached zero would have interrupted, but could not. This was not a problem in 2.x because 2.x did not useSysTick
for general time-keeping, but only for delay operations.Replaced the
common_hal_mcu_delay_us()
calls inOneWire.c
with calls to a new routine just loops to use up time. Loop was calibrated on both SAMD21 and SAMD51 with the standard 48MHz and 120MHz clock frequencies.Tested on Metro M0 and M4. Fixes #964.