summaryrefslogtreecommitdiff
path: root/shared-module
diff options
context:
space:
mode:
Diffstat (limited to 'shared-module')
-rw-r--r--shared-module/bitbangio/OneWire.c80
-rw-r--r--shared-module/bitbangio/types.h7
-rw-r--r--shared-module/nativeio/OneWire.c52
-rw-r--r--shared-module/nativeio/OneWire.h42
4 files changed, 180 insertions, 1 deletions
diff --git a/shared-module/bitbangio/OneWire.c b/shared-module/bitbangio/OneWire.c
new file mode 100644
index 000000000..5f45b3baf
--- /dev/null
+++ b/shared-module/bitbangio/OneWire.c
@@ -0,0 +1,80 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "common-hal/microcontroller/types.h"
+#include "shared-bindings/bitbangio/OneWire.h"
+#include "shared-bindings/microcontroller/__init__.h"
+#include "shared-bindings/nativeio/DigitalInOut.h"
+#include "shared-module/bitbangio/types.h"
+
+// Durations are taken from here: https://www.maximintegrated.com/en/app-notes/index.mvp/id/126
+
+void shared_module_bitbangio_onewire_construct(bitbangio_onewire_obj_t* self,
+ const mcu_pin_obj_t* pin) {
+ self->pin.base.type = &nativeio_digitalinout_type;
+ common_hal_nativeio_digitalinout_construct(&self->pin, pin);
+}
+
+void shared_module_bitbangio_onewire_deinit(bitbangio_onewire_obj_t* self) {
+ common_hal_nativeio_digitalinout_deinit(&self->pin);
+}
+
+bool shared_module_bitbangio_onewire_reset(bitbangio_onewire_obj_t* self) {
+ common_hal_mcu_disable_interrupts();
+ common_hal_nativeio_digitalinout_switch_to_output(&self->pin, false, DRIVE_MODE_OPEN_DRAIN);
+ common_hal_mcu_delay_us(480);
+ common_hal_nativeio_digitalinout_switch_to_input(&self->pin, PULL_NONE);
+ common_hal_mcu_delay_us(70);
+ bool value = common_hal_nativeio_digitalinout_get_value(&self->pin);
+ common_hal_mcu_delay_us(410);
+ common_hal_mcu_enable_interrupts();
+ return value;
+}
+
+bool shared_module_bitbangio_onewire_read_bit(bitbangio_onewire_obj_t* self) {
+ common_hal_mcu_disable_interrupts();
+ common_hal_nativeio_digitalinout_switch_to_output(&self->pin, false, DRIVE_MODE_OPEN_DRAIN);
+ common_hal_mcu_delay_us(6);
+ common_hal_nativeio_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);
+ bool value = common_hal_nativeio_digitalinout_get_value(&self->pin);
+ common_hal_mcu_delay_us(55);
+ common_hal_mcu_enable_interrupts();
+ return value;
+}
+
+void shared_module_bitbangio_onewire_write_bit(bitbangio_onewire_obj_t* self,
+ bool bit) {
+ common_hal_mcu_disable_interrupts();
+ common_hal_nativeio_digitalinout_switch_to_output(&self->pin, false, DRIVE_MODE_OPEN_DRAIN);
+ common_hal_mcu_delay_us(bit? 6 : 60);
+ common_hal_nativeio_digitalinout_switch_to_input(&self->pin, PULL_NONE);
+ common_hal_mcu_delay_us(bit? 64 : 10);
+ common_hal_mcu_enable_interrupts();
+}
diff --git a/shared-module/bitbangio/types.h b/shared-module/bitbangio/types.h
index 719f36a93..8fd4a48c4 100644
--- a/shared-module/bitbangio/types.h
+++ b/shared-module/bitbangio/types.h
@@ -27,7 +27,7 @@
#ifndef __MICROPY_INCLUDED_SHARED_MODULE_BITBANGIO_TYPES_H__
#define __MICROPY_INCLUDED_SHARED_MODULE_BITBANGIO_TYPES_H__
-#include "common-hal/nativeio/types.h"
+#include "common-hal/nativeio/DigitalInOut.h"
#include "py/obj.h"
@@ -41,6 +41,11 @@ typedef struct {
typedef struct {
mp_obj_base_t base;
+ nativeio_digitalinout_obj_t pin;
+} bitbangio_onewire_obj_t;
+
+typedef struct {
+ mp_obj_base_t base;
nativeio_digitalinout_obj_t clock;
nativeio_digitalinout_obj_t mosi;
nativeio_digitalinout_obj_t miso;
diff --git a/shared-module/nativeio/OneWire.c b/shared-module/nativeio/OneWire.c
new file mode 100644
index 000000000..490f9b109
--- /dev/null
+++ b/shared-module/nativeio/OneWire.c
@@ -0,0 +1,52 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+// Wraps the bitbangio implementation of OneWire for use in nativeio.
+#include "common-hal/microcontroller/types.h"
+#include "shared-bindings/bitbangio/OneWire.h"
+#include "common-hal/nativeio/types.h"
+
+void common_hal_nativeio_onewire_construct(nativeio_onewire_obj_t* self,
+ const mcu_pin_obj_t* pin) {
+ shared_module_bitbangio_onewire_construct(&self->bitbang, pin);
+}
+
+void common_hal_nativeio_onewire_deinit(nativeio_onewire_obj_t* self) {
+ shared_module_bitbangio_onewire_deinit(&self->bitbang);
+}
+
+bool common_hal_nativeio_onewire_reset(nativeio_onewire_obj_t* self) {
+ return shared_module_bitbangio_onewire_reset(&self->bitbang);
+}
+
+bool common_hal_nativeio_onewire_read_bit(nativeio_onewire_obj_t* self) {
+ return shared_module_bitbangio_onewire_read_bit(&self->bitbang);
+}
+
+void common_hal_nativeio_onewire_write_bit(nativeio_onewire_obj_t* self,
+ bool bit) {
+ shared_module_bitbangio_onewire_write_bit(&self->bitbang, bit);
+}
diff --git a/shared-module/nativeio/OneWire.h b/shared-module/nativeio/OneWire.h
new file mode 100644
index 000000000..3704c55a3
--- /dev/null
+++ b/shared-module/nativeio/OneWire.h
@@ -0,0 +1,42 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+// This defines the types used to underly the standard nativeio Python objects.
+// The shared API is defined in terms of these types.
+
+#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_NATIVEIO_ONEWIRE_H__
+#define __MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_NATIVEIO_ONEWIRE_H__
+
+#include "shared-module/bitbangio/types.h"
+
+#include "py/obj.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ bitbangio_onewire_obj_t bitbang;
+} nativeio_onewire_obj_t;
+
+#endif // __MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_NATIVEIO_ONEWIRE_H__