summaryrefslogtreecommitdiff
path: root/shared-module
diff options
context:
space:
mode:
authorDan Halbert <halbert@halwitz.org>2018-06-28 23:06:08 -0400
committerDan Halbert <halbert@halwitz.org>2018-06-28 23:32:20 -0400
commit52a11547cbd3d8bf132bdfdd2fa3f804920665f3 (patch)
tree028a1560a72033dd718471cad94a94ec779a0841 /shared-module
parent6908eed7ef5e9002be13322b95b17af8c9bab475 (diff)
fix OneWire timing and DigitalInOut.switch_to_input()
Diffstat (limited to 'shared-module')
-rw-r--r--shared-module/bitbangio/OneWire.c21
1 files changed, 13 insertions, 8 deletions
diff --git a/shared-module/bitbangio/OneWire.c b/shared-module/bitbangio/OneWire.c
index e77bdb3b2..b226b9d78 100644
--- a/shared-module/bitbangio/OneWire.c
+++ b/shared-module/bitbangio/OneWire.c
@@ -24,6 +24,8 @@
* THE SOFTWARE.
*/
+#include "mphalport.h"
+
#include "common-hal/microcontroller/Pin.h"
#include "shared-bindings/bitbangio/OneWire.h"
#include "shared-bindings/microcontroller/__init__.h"
@@ -49,14 +51,17 @@ void shared_module_bitbangio_onewire_deinit(bitbangio_onewire_obj_t* self) {
common_hal_digitalio_digitalinout_deinit(&self->pin);
}
+// We can't use common_hal_mcu_delay_us() here because it needs interrupts to be accurate
+// due to SysTick rollover checking, done by an interrupt.
+
bool shared_module_bitbangio_onewire_reset(bitbangio_onewire_obj_t* self) {
common_hal_mcu_disable_interrupts();
common_hal_digitalio_digitalinout_switch_to_output(&self->pin, false, DRIVE_MODE_OPEN_DRAIN);
- common_hal_mcu_delay_us(480);
+ mp_hal_delay_us_loop(480);
common_hal_digitalio_digitalinout_switch_to_input(&self->pin, PULL_NONE);
- common_hal_mcu_delay_us(70);
+ mp_hal_delay_us_loop(70);
bool value = common_hal_digitalio_digitalinout_get_value(&self->pin);
- common_hal_mcu_delay_us(410);
+ mp_hal_delay_us_loop(410);
common_hal_mcu_enable_interrupts();
return value;
}
@@ -64,14 +69,14 @@ bool shared_module_bitbangio_onewire_reset(bitbangio_onewire_obj_t* self) {
bool shared_module_bitbangio_onewire_read_bit(bitbangio_onewire_obj_t* self) {
common_hal_mcu_disable_interrupts();
common_hal_digitalio_digitalinout_switch_to_output(&self->pin, false, DRIVE_MODE_OPEN_DRAIN);
- common_hal_mcu_delay_us(6);
+ mp_hal_delay_us_loop(6);
common_hal_digitalio_digitalinout_switch_to_input(&self->pin, PULL_NONE);
// TODO(tannewt): Test with more devices and maybe make the delays
// configurable. This should be 9 by the datasheet but all bits read as 1
// then.
- common_hal_mcu_delay_us(6);
+ mp_hal_delay_us_loop(6);
bool value = common_hal_digitalio_digitalinout_get_value(&self->pin);
- common_hal_mcu_delay_us(55);
+ mp_hal_delay_us_loop(55);
common_hal_mcu_enable_interrupts();
return value;
}
@@ -80,8 +85,8 @@ void shared_module_bitbangio_onewire_write_bit(bitbangio_onewire_obj_t* self,
bool bit) {
common_hal_mcu_disable_interrupts();
common_hal_digitalio_digitalinout_switch_to_output(&self->pin, false, DRIVE_MODE_OPEN_DRAIN);
- common_hal_mcu_delay_us(bit? 6 : 60);
+ mp_hal_delay_us_loop(bit? 6 : 60);
common_hal_digitalio_digitalinout_switch_to_input(&self->pin, PULL_NONE);
- common_hal_mcu_delay_us(bit? 64 : 10);
+ mp_hal_delay_us_loop(bit? 64 : 10);
common_hal_mcu_enable_interrupts();
}