diff options
| -rw-r--r-- | atmel-samd/Makefile | 2 | ||||
| -rw-r--r-- | atmel-samd/common-hal/nativeio/DigitalInOut.c | 4 | ||||
| -rw-r--r-- | atmel-samd/common-hal/nativeio/DigitalInOut.h | 39 | ||||
| -rw-r--r-- | atmel-samd/common-hal/nativeio/types.h | 11 | ||||
| -rw-r--r-- | esp8266/Makefile | 2 | ||||
| -rw-r--r-- | esp8266/common-hal/nativeio/DigitalInOut.h | 39 | ||||
| -rw-r--r-- | esp8266/common-hal/nativeio/types.h | 11 | ||||
| -rw-r--r-- | shared-bindings/bitbangio/OneWire.c | 166 | ||||
| -rw-r--r-- | shared-bindings/bitbangio/OneWire.h | 42 | ||||
| -rw-r--r-- | shared-bindings/bitbangio/__init__.c | 5 | ||||
| -rw-r--r-- | shared-bindings/nativeio/OneWire.c | 169 | ||||
| -rw-r--r-- | shared-bindings/nativeio/OneWire.h | 42 | ||||
| -rw-r--r-- | shared-bindings/nativeio/__init__.c | 20 | ||||
| -rw-r--r-- | shared-module/bitbangio/OneWire.c | 80 | ||||
| -rw-r--r-- | shared-module/bitbangio/types.h | 7 | ||||
| -rw-r--r-- | shared-module/nativeio/OneWire.c | 52 | ||||
| -rw-r--r-- | shared-module/nativeio/OneWire.h | 42 |
17 files changed, 706 insertions, 27 deletions
diff --git a/atmel-samd/Makefile b/atmel-samd/Makefile index 93704eb08..fb950a440 100644 --- a/atmel-samd/Makefile +++ b/atmel-samd/Makefile @@ -253,7 +253,9 @@ SRC_SHARED_MODULE = \ help.c \ bitbangio/__init__.c \ bitbangio/I2C.c \ + bitbangio/OneWire.c \ bitbangio/SPI.c \ + nativeio/OneWire.c \ uheap/__init__.c \ SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_MODULE)) \ diff --git a/atmel-samd/common-hal/nativeio/DigitalInOut.c b/atmel-samd/common-hal/nativeio/DigitalInOut.c index 22e8fb179..e09b5af17 100644 --- a/atmel-samd/common-hal/nativeio/DigitalInOut.c +++ b/atmel-samd/common-hal/nativeio/DigitalInOut.c @@ -99,9 +99,7 @@ void common_hal_nativeio_digitalinout_set_value( port_base->OUTSET.reg = pin_mask; } } else { - if (!self->open_drain) { - port_base->DIRSET.reg = pin_mask; - } + port_base->DIRSET.reg = pin_mask; port_base->OUTCLR.reg = pin_mask; } } diff --git a/atmel-samd/common-hal/nativeio/DigitalInOut.h b/atmel-samd/common-hal/nativeio/DigitalInOut.h new file mode 100644 index 000000000..f517e0d5b --- /dev/null +++ b/atmel-samd/common-hal/nativeio/DigitalInOut.h @@ -0,0 +1,39 @@ +/* + * 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. + */ + +#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_DIGITALINOUT_H__ +#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_DIGITALINOUT_H__ + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + const mcu_pin_obj_t * pin; + bool output; + bool open_drain; +} nativeio_digitalinout_obj_t; + +#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_DIGITALINOUT_H__ diff --git a/atmel-samd/common-hal/nativeio/types.h b/atmel-samd/common-hal/nativeio/types.h index 5b26e86f6..9f8c19e9d 100644 --- a/atmel-samd/common-hal/nativeio/types.h +++ b/atmel-samd/common-hal/nativeio/types.h @@ -52,6 +52,10 @@ #include "py/obj.h" +// Import bitbang implemented types. Must be after DigitalInOut because they +// likely depend on it. +#include "shared-module/nativeio/OneWire.h" + typedef struct { mp_obj_base_t base; const mcu_pin_obj_t * pin; @@ -65,13 +69,6 @@ typedef struct { typedef struct { mp_obj_base_t base; - const mcu_pin_obj_t * pin; - bool output; - bool open_drain; -} nativeio_digitalinout_obj_t; - -typedef struct { - mp_obj_base_t base; struct i2c_master_module i2c_master_instance; bool has_lock; uint8_t scl_pin; diff --git a/esp8266/Makefile b/esp8266/Makefile index ec2475439..d7c8cef18 100644 --- a/esp8266/Makefile +++ b/esp8266/Makefile @@ -130,7 +130,9 @@ SRC_COMMON_HAL_EXPANDED = $(addprefix shared-bindings/, $(SRC_COMMON_HAL)) \ SRC_SHARED_MODULE = \ bitbangio/__init__.c \ bitbangio/I2C.c \ + bitbangio/OneWire.c \ bitbangio/SPI.c \ + nativeio/OneWire.c \ SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_MODULE)) \ $(addprefix shared-module/, $(SRC_SHARED_MODULE)) diff --git a/esp8266/common-hal/nativeio/DigitalInOut.h b/esp8266/common-hal/nativeio/DigitalInOut.h new file mode 100644 index 000000000..6325b978b --- /dev/null +++ b/esp8266/common-hal/nativeio/DigitalInOut.h @@ -0,0 +1,39 @@ +/* + * 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. + */ + +#ifndef __MICROPY_INCLUDED_ESP8266_COMMON_HAL_NATIVEIO_DIGITALINOUT_H__ +#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_NATIVEIO_DIGITALINOUT_H__ + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + const mcu_pin_obj_t * pin; + bool output; + bool open_drain; +} nativeio_digitalinout_obj_t; + +#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_NATIVEIO_DIGITALINOUT_H__ diff --git a/esp8266/common-hal/nativeio/types.h b/esp8266/common-hal/nativeio/types.h index bc8a3bdc1..0a45712e4 100644 --- a/esp8266/common-hal/nativeio/types.h +++ b/esp8266/common-hal/nativeio/types.h @@ -34,6 +34,10 @@ #include "py/obj.h" +// Use the bitbang wrapper for OneWire +// TODO(tannewt): Wrap bitbangio for I2C too. +#include "shared-module/nativeio/OneWire.h" + typedef struct { mp_obj_base_t base; const mcu_pin_obj_t * pin; @@ -44,13 +48,6 @@ typedef struct { mp_obj_base_t base; } nativeio_analogout_obj_t; -typedef struct { - mp_obj_base_t base; - const mcu_pin_obj_t * pin; - bool output; - bool open_drain; -} nativeio_digitalinout_obj_t; - // Not supported, throws error on construction. typedef struct { mp_obj_base_t base; diff --git a/shared-bindings/bitbangio/OneWire.c b/shared-bindings/bitbangio/OneWire.c new file mode 100644 index 000000000..14645444d --- /dev/null +++ b/shared-bindings/bitbangio/OneWire.c @@ -0,0 +1,166 @@ +/* + * 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 <stdint.h> + +#include "lib/utils/context_manager_helpers.h" +#include "py/objproperty.h" +#include "py/runtime.h" +#include "py/runtime0.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/bitbangio/OneWire.h" + +//| .. currentmodule:: bitbangio +//| +//| :class:`OneWire` -- Lowest-level of the Maxim OneWire protocol +//| =============================================================== +//| +//| :class:`~bitbangio.OneWire` implements the timing-sensitive foundation of +//| the Maxim (formerly Dallas Semi) OneWire protocol. +//| +//| Protocol definition is here: https://www.maximintegrated.com/en/app-notes/index.mvp/id/126 +//| +//| .. class:: OneWire(pin) +//| +//| Create a OneWire object associated with the given pin. The object +//| implements the lowest level timing-sensitive bits of the protocol. +//| +//| :param ~microcontroller.Pin pin: Pin to read pulses from. +//| +//| Read a short series of pulses:: +//| +//| import bitbangio +//| import board +//| +//| with bitbangio.OneWire(board.D7) as onewire: +//| onewire.reset() +//| onewire.write_bit(True) +//| onewire.write_bit(False) +//| print(onewire.read_bit()) +//| +STATIC mp_obj_t bitbangio_onewire_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { + mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); + mp_map_t kw_args; + mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); + enum { ARG_pin }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ }, + }; + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + assert_pin(args[ARG_pin].u_obj, false); + const mcu_pin_obj_t* pin = MP_OBJ_TO_PTR(args[ARG_pin].u_obj); + assert_pin_free(pin); + + bitbangio_onewire_obj_t *self = m_new_obj(bitbangio_onewire_obj_t); + self->base.type = &bitbangio_onewire_type; + + shared_module_bitbangio_onewire_construct(self, pin); + return MP_OBJ_FROM_PTR(self); +} + +//| .. method:: deinit() +//| +//| Deinitialize the OneWire bus and release any hardware resources for reuse. +//| +STATIC mp_obj_t bitbangio_onewire_deinit(mp_obj_t self_in) { + bitbangio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); + shared_module_bitbangio_onewire_deinit(self); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_onewire_deinit_obj, bitbangio_onewire_deinit); + +//| .. method:: __enter__() +//| +//| No-op used by Context Managers. +//| +// Provided by context manager helper. + +//| .. method:: __exit__() +//| +//| Automatically deinitializes the hardware when exiting a context. +//| +STATIC mp_obj_t bitbangio_onewire_obj___exit__(size_t n_args, const mp_obj_t *args) { + (void)n_args; + shared_module_bitbangio_onewire_deinit(args[0]); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bitbangio_onewire___exit___obj, 4, 4, bitbangio_onewire_obj___exit__); + +//| .. method:: reset() +//| +//| Reset the OneWire bus +//| +STATIC mp_obj_t bitbangio_onewire_obj_reset(mp_obj_t self_in) { + bitbangio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); + + return mp_obj_new_bool(shared_module_bitbangio_onewire_reset(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_onewire_reset_obj, bitbangio_onewire_obj_reset); + +//| .. method:: read_bit() +//| +//| Read in a bit +//| +//| :returns: bit state read +//| :rtype: bool +//| +STATIC mp_obj_t bitbangio_onewire_obj_read_bit(mp_obj_t self_in) { + bitbangio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); + + return mp_obj_new_bool(shared_module_bitbangio_onewire_read_bit(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_onewire_read_bit_obj, bitbangio_onewire_obj_read_bit); + +//| .. method:: write_bit(value) +//| +//| Write out a bit based on value. +//| +STATIC mp_obj_t bitbangio_onewire_obj_write_bit(mp_obj_t self_in, mp_obj_t bool_obj) { + bitbangio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); + + shared_module_bitbangio_onewire_write_bit(self, mp_obj_is_true(bool_obj)); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(bitbangio_onewire_write_bit_obj, bitbangio_onewire_obj_write_bit); + +STATIC const mp_rom_map_elem_t bitbangio_onewire_locals_dict_table[] = { + // Methods + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&bitbangio_onewire_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&bitbangio_onewire___exit___obj) }, + { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&bitbangio_onewire_reset_obj) }, + { MP_ROM_QSTR(MP_QSTR_read_bit), MP_ROM_PTR(&bitbangio_onewire_read_bit_obj) }, + { MP_ROM_QSTR(MP_QSTR_write_bit), MP_ROM_PTR(&bitbangio_onewire_write_bit_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(bitbangio_onewire_locals_dict, bitbangio_onewire_locals_dict_table); + +const mp_obj_type_t bitbangio_onewire_type = { + { &mp_type_type }, + .name = MP_QSTR_OneWire, + .make_new = bitbangio_onewire_make_new, + .locals_dict = (mp_obj_dict_t*)&bitbangio_onewire_locals_dict, +}; diff --git a/shared-bindings/bitbangio/OneWire.h b/shared-bindings/bitbangio/OneWire.h new file mode 100644 index 000000000..dcc0386d4 --- /dev/null +++ b/shared-bindings/bitbangio/OneWire.h @@ -0,0 +1,42 @@ +/* + * 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. + */ + +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ONEWIRE_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ONEWIRE_H__ + +#include "common-hal/microcontroller/types.h" +#include "shared-module/bitbangio/types.h" + +extern const mp_obj_type_t bitbangio_onewire_type; + +extern void shared_module_bitbangio_onewire_construct(bitbangio_onewire_obj_t* self, + const mcu_pin_obj_t* pin); +extern void shared_module_bitbangio_onewire_deinit(bitbangio_onewire_obj_t* self); +extern bool shared_module_bitbangio_onewire_reset(bitbangio_onewire_obj_t* self); +extern bool shared_module_bitbangio_onewire_read_bit(bitbangio_onewire_obj_t* self); +extern void shared_module_bitbangio_onewire_write_bit(bitbangio_onewire_obj_t* self, bool bit); + +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ONEWIRE_H__ diff --git a/shared-bindings/bitbangio/__init__.c b/shared-bindings/bitbangio/__init__.c index 6d864fd18..5fe469758 100644 --- a/shared-bindings/bitbangio/__init__.c +++ b/shared-bindings/bitbangio/__init__.c @@ -34,6 +34,7 @@ #include "shared-bindings/bitbangio/__init__.h" #include "shared-bindings/bitbangio/I2C.h" +#include "shared-bindings/bitbangio/OneWire.h" #include "shared-bindings/bitbangio/SPI.h" #include "shared-module/bitbangio/types.h" @@ -49,7 +50,7 @@ //| The `bitbangio` module contains classes to provide digital protocol support //| regardless of whether the underlying hardware exists to use the protocol. //| -//| First try to use `nativeio` module instead which utilizes peripheral +//| First try to use `nativeio` module instead which may utilize peripheral //| hardware to implement the protocols. Native implementations will be faster //| than bitbanged versions and have more capabilities. //| @@ -59,6 +60,7 @@ //| :maxdepth: 3 //| //| I2C +//| OneWire //| SPI //| //| All libraries change hardware state and should be deinitialized when they @@ -81,6 +83,7 @@ STATIC const mp_rom_map_elem_t bitbangio_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_bitbangio) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&bitbangio_i2c_type) }, + { MP_ROM_QSTR(MP_QSTR_OneWire), MP_ROM_PTR(&bitbangio_onewire_type) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&bitbangio_spi_type) }, }; diff --git a/shared-bindings/nativeio/OneWire.c b/shared-bindings/nativeio/OneWire.c new file mode 100644 index 000000000..f9fbbe63c --- /dev/null +++ b/shared-bindings/nativeio/OneWire.c @@ -0,0 +1,169 @@ +/* + * 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 <stdint.h> + +#include "lib/utils/context_manager_helpers.h" +#include "py/objproperty.h" +#include "py/runtime.h" +#include "py/runtime0.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/nativeio/OneWire.h" + +//| .. currentmodule:: nativeio +//| +//| :class:`OneWire` -- Lowest-level of the Maxim OneWire protocol +//| ================================================================= +//| +//| :class:`~nativeio.OneWire` implements the timing-sensitive foundation of the Maxim +//| (formerly Dallas Semi) OneWire protocol. +//| +//| Protocol definition is here: https://www.maximintegrated.com/en/app-notes/index.mvp/id/126 +//| +//| .. class:: OneWire(pin) +//| +//| Create a OneWire object associated with the given pin. The object +//| implements the lowest level timing-sensitive bits of the protocol. +//| +//| :param ~microcontroller.Pin pin: Pin connected to the OneWire bus +//| +//| Read a short series of pulses:: +//| +//| import nativeio +//| import board +//| +//| with nativeio.OneWire(board.D7) as onewire: +//| onewire.reset() +//| onewire.write_bit(True) +//| onewire.write_bit(False) +//| print(onewire.read_bit()) +//| +STATIC mp_obj_t nativeio_onewire_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { + mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); + mp_map_t kw_args; + mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); + enum { ARG_pin }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ }, + }; + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + assert_pin(args[ARG_pin].u_obj, false); + const mcu_pin_obj_t* pin = MP_OBJ_TO_PTR(args[ARG_pin].u_obj); + assert_pin_free(pin); + + nativeio_onewire_obj_t *self = m_new_obj(nativeio_onewire_obj_t); + self->base.type = &nativeio_onewire_type; + + common_hal_nativeio_onewire_construct(self, pin); + return MP_OBJ_FROM_PTR(self); +} + +//| .. method:: deinit() +//| +//| Deinitialize the OneWire bus and release any hardware resources for reuse. +//| +STATIC mp_obj_t nativeio_onewire_deinit(mp_obj_t self_in) { + nativeio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_nativeio_onewire_deinit(self); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_onewire_deinit_obj, nativeio_onewire_deinit); + +//| .. method:: __enter__() +//| +//| No-op used by Context Managers. +//| +// Provided by context manager helper. + +//| .. method:: __exit__() +//| +//| Automatically deinitializes the hardware when exiting a context. +//| +STATIC mp_obj_t nativeio_onewire_obj___exit__(size_t n_args, const mp_obj_t *args) { + (void)n_args; + common_hal_nativeio_onewire_deinit(args[0]); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_onewire___exit___obj, 4, 4, nativeio_onewire_obj___exit__); + +//| .. method:: reset() +//| +//| Reset the OneWire bus and read presence +//| +//| :returns: False when at least one device is present +//| :rtype: bool +//| +STATIC mp_obj_t nativeio_onewire_obj_reset(mp_obj_t self_in) { + nativeio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); + + return mp_obj_new_bool(common_hal_nativeio_onewire_reset(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(nativeio_onewire_reset_obj, nativeio_onewire_obj_reset); + +//| .. method:: read_bit() +//| +//| Read in a bit +//| +//| :returns: bit state read +//| :rtype: bool +//| +STATIC mp_obj_t nativeio_onewire_obj_read_bit(mp_obj_t self_in) { + nativeio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); + + return mp_obj_new_bool(common_hal_nativeio_onewire_read_bit(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(nativeio_onewire_read_bit_obj, nativeio_onewire_obj_read_bit); + +//| .. method:: write_bit(value) +//| +//| Write out a bit based on value. +//| +STATIC mp_obj_t nativeio_onewire_obj_write_bit(mp_obj_t self_in, mp_obj_t bool_obj) { + nativeio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); + + common_hal_nativeio_onewire_write_bit(self, mp_obj_is_true(bool_obj)); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(nativeio_onewire_write_bit_obj, nativeio_onewire_obj_write_bit); + +STATIC const mp_rom_map_elem_t nativeio_onewire_locals_dict_table[] = { + // Methods + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_onewire_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_onewire___exit___obj) }, + { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&nativeio_onewire_reset_obj) }, + { MP_ROM_QSTR(MP_QSTR_read_bit), MP_ROM_PTR(&nativeio_onewire_read_bit_obj) }, + { MP_ROM_QSTR(MP_QSTR_write_bit), MP_ROM_PTR(&nativeio_onewire_write_bit_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(nativeio_onewire_locals_dict, nativeio_onewire_locals_dict_table); + +const mp_obj_type_t nativeio_onewire_type = { + { &mp_type_type }, + .name = MP_QSTR_OneWire, + .make_new = nativeio_onewire_make_new, + .locals_dict = (mp_obj_dict_t*)&nativeio_onewire_locals_dict, +}; diff --git a/shared-bindings/nativeio/OneWire.h b/shared-bindings/nativeio/OneWire.h new file mode 100644 index 000000000..9d5c2c932 --- /dev/null +++ b/shared-bindings/nativeio/OneWire.h @@ -0,0 +1,42 @@ +/* + * 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. + */ + +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ONEWIRE_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ONEWIRE_H__ + +#include "common-hal/microcontroller/types.h" +#include "common-hal/nativeio/types.h" + +extern const mp_obj_type_t nativeio_onewire_type; + +extern void common_hal_nativeio_onewire_construct(nativeio_onewire_obj_t* self, + const mcu_pin_obj_t* pin); +extern void common_hal_nativeio_onewire_deinit(nativeio_onewire_obj_t* self); +extern bool common_hal_nativeio_onewire_reset(nativeio_onewire_obj_t* self); +extern bool common_hal_nativeio_onewire_read_bit(nativeio_onewire_obj_t* self); +extern void common_hal_nativeio_onewire_write_bit(nativeio_onewire_obj_t* self, bool bit); + +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ONEWIRE_H__ diff --git a/shared-bindings/nativeio/__init__.c b/shared-bindings/nativeio/__init__.c index 7395ce311..3287a46cd 100644 --- a/shared-bindings/nativeio/__init__.c +++ b/shared-bindings/nativeio/__init__.c @@ -39,6 +39,7 @@ #include "shared-bindings/nativeio/AnalogOut.h" #include "shared-bindings/nativeio/DigitalInOut.h" #include "shared-bindings/nativeio/I2C.h" +#include "shared-bindings/nativeio/OneWire.h" #include "shared-bindings/nativeio/PulseIn.h" #include "shared-bindings/nativeio/PulseOut.h" #include "shared-bindings/nativeio/PWMOut.h" @@ -57,15 +58,16 @@ //| :synopsis: Hardware accelerated behavior //| :platform: SAMD21 //| -//| The `nativeio` module contains classes to provide access to IO accelerated -//| by hardware on the onboard microcontroller. The classes are meant to align -//| with commonly hardware accelerated IO and not necessarily match up with -//| microcontroller structure (because it varies). +//| The `nativeio` module contains classes to provide access to IO typically +//| accelerated by hardware on the onboard microcontroller. The classes are +//| meant to align with commonly hardware accelerated IO and not necessarily +//| match up with microcontroller structure (because it varies). //| -//| If the microcontroller doesn't not support the behavior in a hardware -//| accelerated fashion it throws a NotImplementedError on construction. Use -//| `bitbangio` module instead which only depends on -//| :py:class:`~nativeio.DigitalInOut` and is shared across hardware ports. +//| When the microcontroller does not support the behavior in a hardware +//| accelerated fashion it may internally use a bitbang routine. However, if +//| hardware support is available on a subset of pins but not those provided, +//| then a RuntimeError will be raised. Use the `bitbangio` module to explicitly +//| bitbang a protocol on any general purpose pins. //| //| Libraries //| @@ -76,6 +78,7 @@ //| AnalogOut //| DigitalInOut //| I2C +//| OneWire //| PulseIn //| PulseOut //| PWMOut @@ -120,6 +123,7 @@ STATIC const mp_rom_map_elem_t nativeio_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_AnalogOut), MP_ROM_PTR(&nativeio_analogout_type) }, { MP_ROM_QSTR(MP_QSTR_DigitalInOut), MP_ROM_PTR(&nativeio_digitalinout_type) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&nativeio_i2c_type) }, + { MP_ROM_QSTR(MP_QSTR_OneWire), MP_ROM_PTR(&nativeio_onewire_type) }, { MP_ROM_QSTR(MP_QSTR_PulseIn), MP_ROM_PTR(&nativeio_pulsein_type) }, { MP_ROM_QSTR(MP_QSTR_PulseOut), MP_ROM_PTR(&nativeio_pulseout_type) }, { MP_ROM_QSTR(MP_QSTR_PWMOut), MP_ROM_PTR(&nativeio_pwmout_type) }, 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__ |
