diff --git a/README.md b/README.md index d66d98b8fe..1b35f4e2e4 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ project admins. Please join the [Gitter chat](https://gitter.im/adafruit/circuit * Port for Atmel SAMD21 (Commonly known as M0 in product names.) * No `machine` API on Atmel SAMD21 port. * Only supports Atmel SAMD21 and ESP8266 ports. -* Unified hardware API: [`nativeio`](https://circuitpython.readthedocs.io/en/latest/shared-bindings/nativeio/__init__.html), [`microcontroller`](https://circuitpython.readthedocs.io/en/latest/shared-bindings/microcontroller/__init__.html), [`board`](https://circuitpython.readthedocs.io/en/latest/shared-bindings/board/__init__.html), [`bitbangio`](https://circuitpython.readthedocs.io/en/latest/shared-bindings/bitbangio/__init__.html) (Only available on atmel-samd21 and ESP8266 currently.) +* Unified hardware APIs: [`analogio`](https://circuitpython.readthedocs.io/en/latest/shared-bindings/analogio/__init__.html), [`busio`](https://circuitpython.readthedocs.io/en/latest/shared-bindings/busio/__init__.html), [`digitalio`](https://circuitpython.readthedocs.io/en/latest/shared-bindings/digitalio/__init__.html), [`pulseio`](https://circuitpython.readthedocs.io/en/latest/shared-bindings/pulseio/__init__.html), [`touchio`](https://circuitpython.readthedocs.io/en/latest/shared-bindings/touchio/__init__.html), [`microcontroller`](https://circuitpython.readthedocs.io/en/latest/shared-bindings/microcontroller/__init__.html), [`board`](https://circuitpython.readthedocs.io/en/latest/shared-bindings/board/__init__.html), [`bitbangio`](https://circuitpython.readthedocs.io/en/latest/shared-bindings/bitbangio/__init__.html) (Only available on atmel-samd21 and ESP8266 currently.) * Tracks MicroPython's releases (not master). * No module aliasing. (`uos` and `utime` are not available as `os` and `time` respectively.) * Modules with a CPython counterpart, such as `time`, are strict [subsets](https://circuitpython.readthedocs.io/en/latest/shared-bindings/time/__init__.html) of their [CPython version](https://docs.python.org/3.4/library/time.html?highlight=time#module-time). Therefore, code from CircuitPython is runnable on CPython but not necessarily the reverse. diff --git a/atmel-samd/Makefile b/atmel-samd/Makefile index fb950a440f..792e930811 100644 --- a/atmel-samd/Makefile +++ b/atmel-samd/Makefile @@ -221,41 +221,42 @@ SRC_BINDINGS = \ board/__init__.c \ microcontroller/__init__.c \ microcontroller/Pin.c \ - nativeio/__init__.c \ - nativeio/AnalogIn.c \ - nativeio/AnalogOut.c \ - nativeio/DigitalInOut.c \ - nativeio/I2C.c \ - nativeio/PulseIn.c \ - nativeio/PulseOut.c \ - nativeio/PWMOut.c \ - nativeio/SPI.c \ - nativeio/UART.c \ + analogio/__init__.c \ + analogio/AnalogIn.c \ + analogio/AnalogOut.c \ + digitalio/__init__.c \ + digitalio/DigitalInOut.c \ + pulseio/__init__.c \ + pulseio/PulseIn.c \ + pulseio/PulseOut.c \ + pulseio/PWMOut.c \ + busio/__init__.c \ + busio/I2C.c \ + busio/SPI.c \ + busio/UART.c \ neopixel_write/__init__.c \ time/__init__.c \ usb_hid/__init__.c \ usb_hid/Device.c -SRC_BINDINGS_EXPANDED = $(addprefix shared-bindings/, $(SRC_BINDINGS)) \ - $(addprefix common-hal/, $(SRC_BINDINGS)) - # Handle touch support on its own since it only fits in the image when external # flash is used to store the file system. -SRC_BINDINGS_EXPANDED += shared-bindings/nativeio/TouchIn.c - -ifeq ($(FLASH_IMPL),internal_flash.c) -SRC_BINDINGS_EXPANDED += common-hal/nativeio/TouchInStub.c -else -SRC_BINDINGS_EXPANDED += common-hal/nativeio/TouchIn.c +# TODO(tannewt): Remove this after we switch to freetouch and rely on the linker +# to drop classes we don't need. +ifneq ($(FLASH_IMPL),internal_flash.c) +SRC_BINDINGS += touchio/__init__.c touchio/TouchIn.c endif +SRC_BINDINGS_EXPANDED = $(addprefix shared-bindings/, $(SRC_BINDINGS)) \ + $(addprefix common-hal/, $(SRC_BINDINGS)) + SRC_SHARED_MODULE = \ help.c \ bitbangio/__init__.c \ bitbangio/I2C.c \ bitbangio/OneWire.c \ bitbangio/SPI.c \ - nativeio/OneWire.c \ + busio/OneWire.c \ uheap/__init__.c \ SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_MODULE)) \ diff --git a/atmel-samd/common-hal/nativeio/AnalogIn.c b/atmel-samd/common-hal/analogio/AnalogIn.c similarity index 90% rename from atmel-samd/common-hal/nativeio/AnalogIn.c rename to atmel-samd/common-hal/analogio/AnalogIn.c index 2203fc2ca3..59ea3b8990 100644 --- a/atmel-samd/common-hal/nativeio/AnalogIn.c +++ b/atmel-samd/common-hal/analogio/AnalogIn.c @@ -24,7 +24,7 @@ * THE SOFTWARE. */ -#include "common-hal/nativeio/AnalogIn.h" +#include "common-hal/analogio/AnalogIn.h" #include @@ -33,7 +33,7 @@ #include "py/runtime.h" #include "py/binary.h" #include "py/mphal.h" -#include "shared-bindings/nativeio/AnalogIn.h" +#include "shared-bindings/analogio/AnalogIn.h" #include "asf/sam0/drivers/adc/adc.h" #include "samd21_pins.h" @@ -42,7 +42,7 @@ volatile uint8_t active_channel_count; struct adc_module *adc_instance = NULL; -void common_hal_nativeio_analogin_construct(nativeio_analogin_obj_t* self, +void common_hal_analogio_analogin_construct(analogio_analogin_obj_t* self, const mcu_pin_obj_t *pin) { if (!pin->has_adc) { // No ADC function on that pin @@ -72,7 +72,7 @@ void common_hal_nativeio_analogin_construct(nativeio_analogin_obj_t* self, active_channel_count++; } -void common_hal_nativeio_analogin_deinit(nativeio_analogin_obj_t *self) { +void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t *self) { active_channel_count--; if (active_channel_count == 0) { adc_reset(adc_instance); @@ -92,7 +92,7 @@ void analogin_reset() { active_channel_count = 0; } -uint16_t common_hal_nativeio_analogin_get_value(nativeio_analogin_obj_t *self) { +uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) { adc_set_positive_input(adc_instance, self->pin->adc_input); adc_enable(adc_instance); @@ -111,6 +111,6 @@ uint16_t common_hal_nativeio_analogin_get_value(nativeio_analogin_obj_t *self) { return data; } -float common_hal_nativeio_analogin_get_reference_voltage(nativeio_analogin_obj_t *self) { +float common_hal_analogio_analogin_get_reference_voltage(analogio_analogin_obj_t *self) { return 3.3f; } diff --git a/atmel-samd/common-hal/analogio/AnalogIn.h b/atmel-samd/common-hal/analogio/AnalogIn.h new file mode 100644 index 0000000000..78c7b82e8b --- /dev/null +++ b/atmel-samd/common-hal/analogio/AnalogIn.h @@ -0,0 +1,49 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft + * + * 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_ANALOGIO_ANALOGIN_H__ +#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ANALOGIO_ANALOGIN_H__ + +#include "common-hal/microcontroller/types.h" + +// Don't reorder these includes because they are dependencies of adc_feature.h. +// They should really be included by adc_feature.h. +#include +#include "asf/sam0/drivers/system/clock/gclk.h" +#include "asf/sam0/utils/cmsis/samd21/include/component/adc.h" +#include "asf/sam0/drivers/adc/adc_sam_d_r/adc_feature.h" + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + const mcu_pin_obj_t * pin; + struct adc_module * adc_instance; +} analogio_analogin_obj_t; + +void analogin_reset(void); + +#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ANALOGIO_ANALOGIN_H__ diff --git a/atmel-samd/common-hal/nativeio/AnalogOut.c b/atmel-samd/common-hal/analogio/AnalogOut.c similarity index 91% rename from atmel-samd/common-hal/nativeio/AnalogOut.c rename to atmel-samd/common-hal/analogio/AnalogOut.c index f36da19749..a618a6eeb5 100644 --- a/atmel-samd/common-hal/nativeio/AnalogOut.c +++ b/atmel-samd/common-hal/analogio/AnalogOut.c @@ -30,12 +30,12 @@ #include "py/mperrno.h" #include "py/runtime.h" -#include "shared-bindings/nativeio/AnalogOut.h" +#include "shared-bindings/analogio/AnalogOut.h" #include "asf/sam0/drivers/dac/dac.h" #include "samd21_pins.h" -void common_hal_nativeio_analogout_construct(nativeio_analogout_obj_t* self, +void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self, const mcu_pin_obj_t *pin) { if (pin->pin != PIN_PA02) { mp_raise_ValueError("AnalogOut not supported on given pin"); @@ -58,13 +58,13 @@ void common_hal_nativeio_analogout_construct(nativeio_analogout_obj_t* self, dac_enable(&self->dac_instance); } -void common_hal_nativeio_analogout_deinit(nativeio_analogout_obj_t *self) { +void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) { dac_disable(&self->dac_instance); dac_chan_disable(&self->dac_instance, DAC_CHANNEL_0); reset_pin(PIN_PA02); } -void common_hal_nativeio_analogout_set_value(nativeio_analogout_obj_t *self, +void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self, uint16_t value) { // Input is 16 bit but we only support 10 bit so we shift the input. dac_chan_write(&self->dac_instance, DAC_CHANNEL_0, value >> 6); diff --git a/atmel-samd/common-hal/analogio/AnalogOut.h b/atmel-samd/common-hal/analogio/AnalogOut.h new file mode 100644 index 0000000000..f74592d6a9 --- /dev/null +++ b/atmel-samd/common-hal/analogio/AnalogOut.h @@ -0,0 +1,41 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft + * + * 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_ANALOGIO_ANALOGOUT_H__ +#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ANALOGIO_ANALOGOUT_H__ + +#include "common-hal/microcontroller/types.h" + +#include "asf/sam0/drivers/dac/dac.h" + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + struct dac_module dac_instance; +} analogio_analogout_obj_t; + +#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ANALOGIO_ANALOGOUT_H__ diff --git a/atmel-samd/common-hal/analogio/__init__.c b/atmel-samd/common-hal/analogio/__init__.c new file mode 100644 index 0000000000..eea58c77d6 --- /dev/null +++ b/atmel-samd/common-hal/analogio/__init__.c @@ -0,0 +1 @@ +// No analogio module functions. diff --git a/atmel-samd/common-hal/nativeio/I2C.c b/atmel-samd/common-hal/busio/I2C.c similarity index 88% rename from atmel-samd/common-hal/nativeio/I2C.c rename to atmel-samd/common-hal/busio/I2C.c index fb5e765057..7b7c6a52cf 100644 --- a/atmel-samd/common-hal/nativeio/I2C.c +++ b/atmel-samd/common-hal/busio/I2C.c @@ -24,10 +24,7 @@ * THE SOFTWARE. */ - // This file contains all of the port specific HAL functions for the machine - // module. - -#include "shared-bindings/nativeio/I2C.h" +#include "shared-bindings/busio/I2C.h" #include "py/mperrno.h" #include "py/nlr.h" #include "py/runtime.h" @@ -41,7 +38,7 @@ // Number of times to try to send packet if failed. #define TIMEOUT 1 -void common_hal_nativeio_i2c_construct(nativeio_i2c_obj_t *self, +void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, const mcu_pin_obj_t* scl, const mcu_pin_obj_t* sda, uint32_t frequency) { struct i2c_master_config config_i2c_master; i2c_master_get_config_defaults(&config_i2c_master); @@ -85,7 +82,7 @@ void common_hal_nativeio_i2c_construct(nativeio_i2c_obj_t *self, sercom, &config_i2c_master); if (status != STATUS_OK) { - common_hal_nativeio_i2c_deinit(self); + common_hal_busio_i2c_deinit(self); if (status == STATUS_ERR_BAUDRATE_UNAVAILABLE) { mp_raise_ValueError("Unsupported baudrate"); } else { @@ -96,13 +93,13 @@ void common_hal_nativeio_i2c_construct(nativeio_i2c_obj_t *self, i2c_master_enable(&self->i2c_master_instance); } -void common_hal_nativeio_i2c_deinit(nativeio_i2c_obj_t *self) { +void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) { i2c_master_reset(&self->i2c_master_instance); reset_pin(self->sda_pin); reset_pin(self->scl_pin); } -bool common_hal_nativeio_i2c_probe(nativeio_i2c_obj_t *self, uint8_t addr) { +bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) { uint8_t buf; struct i2c_master_packet packet = { .address = addr, @@ -118,26 +115,26 @@ bool common_hal_nativeio_i2c_probe(nativeio_i2c_obj_t *self, uint8_t addr) { return status == STATUS_OK; } -void common_hal_nativeio_i2c_configure(nativeio_i2c_obj_t *self, +void common_hal_busio_i2c_configure(busio_i2c_obj_t *self, uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) { return; } -bool common_hal_nativeio_i2c_try_lock(nativeio_i2c_obj_t *self) { +bool common_hal_busio_i2c_try_lock(busio_i2c_obj_t *self) { self->has_lock = i2c_master_lock(&self->i2c_master_instance) == STATUS_OK; return self->has_lock; } -bool common_hal_nativeio_i2c_has_lock(nativeio_i2c_obj_t *self) { +bool common_hal_busio_i2c_has_lock(busio_i2c_obj_t *self) { return self->has_lock; } -void common_hal_nativeio_i2c_unlock(nativeio_i2c_obj_t *self) { +void common_hal_busio_i2c_unlock(busio_i2c_obj_t *self) { self->has_lock = false; i2c_master_unlock(&self->i2c_master_instance); } -uint8_t common_hal_nativeio_i2c_write(nativeio_i2c_obj_t *self, uint16_t addr, +uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addr, const uint8_t *data, size_t len, bool transmit_stop_bit) { struct i2c_master_packet packet = { .address = addr, @@ -171,7 +168,7 @@ uint8_t common_hal_nativeio_i2c_write(nativeio_i2c_obj_t *self, uint16_t addr, return MP_EIO; } -uint8_t common_hal_nativeio_i2c_read(nativeio_i2c_obj_t *self, uint16_t addr, +uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t addr, uint8_t *data, size_t len) { struct i2c_master_packet packet = { .address = addr, diff --git a/atmel-samd/common-hal/busio/I2C.h b/atmel-samd/common-hal/busio/I2C.h new file mode 100644 index 0000000000..8f09cb3433 --- /dev/null +++ b/atmel-samd/common-hal/busio/I2C.h @@ -0,0 +1,43 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft + * + * 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_BUSIO_I2C_H__ +#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_I2C_H__ + +#include "common-hal/microcontroller/types.h" + +#include "asf/sam0/drivers/sercom/i2c/i2c_master.h" +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + struct i2c_master_module i2c_master_instance; + bool has_lock; + uint8_t scl_pin; + uint8_t sda_pin; +} busio_i2c_obj_t; + +#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_I2C_H__ diff --git a/atmel-samd/common-hal/nativeio/PulseIn.h b/atmel-samd/common-hal/busio/OneWire.h similarity index 79% rename from atmel-samd/common-hal/nativeio/PulseIn.h rename to atmel-samd/common-hal/busio/OneWire.h index 9d6169414b..8b61260fcf 100644 --- a/atmel-samd/common-hal/nativeio/PulseIn.h +++ b/atmel-samd/common-hal/busio/OneWire.h @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries + * Copyright (c) 2016 Scott Shawcroft * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -24,9 +24,10 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_PULSEIN_H__ -#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_PULSEIN_H__ +#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_ONEWIRE_H__ +#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_ONEWIRE_H__ -void pulsein_reset(void); +// Use bitbangio. +#include "shared-module/busio/OneWire.h" -#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_PULSEIN_H__ +#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_ONEWIRE_H__ diff --git a/atmel-samd/common-hal/nativeio/SPI.c b/atmel-samd/common-hal/busio/SPI.c similarity index 91% rename from atmel-samd/common-hal/nativeio/SPI.c rename to atmel-samd/common-hal/busio/SPI.c index 3dec31a10d..a7175f3d5e 100644 --- a/atmel-samd/common-hal/nativeio/SPI.c +++ b/atmel-samd/common-hal/busio/SPI.c @@ -24,10 +24,7 @@ * THE SOFTWARE. */ - // This file contains all of the port specific HAL functions for the machine - // module. - -#include "shared-bindings/nativeio/SPI.h" +#include "shared-bindings/busio/SPI.h" #include "py/nlr.h" #include "py/runtime.h" #include "samd21_pins.h" @@ -38,7 +35,7 @@ // Number of times to try to send packet if failed. #define TIMEOUT 1 -void common_hal_nativeio_spi_construct(nativeio_spi_obj_t *self, +void common_hal_busio_spi_construct(busio_spi_obj_t *self, const mcu_pin_obj_t * clock, const mcu_pin_obj_t * mosi, const mcu_pin_obj_t * miso) { struct spi_config config_spi_master; @@ -146,14 +143,14 @@ void common_hal_nativeio_spi_construct(nativeio_spi_obj_t *self, spi_enable(&self->spi_master_instance); } -void common_hal_nativeio_spi_deinit(nativeio_spi_obj_t *self) { +void common_hal_busio_spi_deinit(busio_spi_obj_t *self) { spi_disable(&self->spi_master_instance); reset_pin(self->clock_pin); reset_pin(self->MOSI_pin); reset_pin(self->MISO_pin); } -bool common_hal_nativeio_spi_configure(nativeio_spi_obj_t *self, +bool common_hal_busio_spi_configure(busio_spi_obj_t *self, uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) { // TODO(tannewt): Check baudrate first before changing it. if (baudrate != self->current_baudrate) { @@ -194,21 +191,21 @@ bool common_hal_nativeio_spi_configure(nativeio_spi_obj_t *self, return true; } -bool common_hal_nativeio_spi_try_lock(nativeio_spi_obj_t *self) { +bool common_hal_busio_spi_try_lock(busio_spi_obj_t *self) { self->has_lock = spi_lock(&self->spi_master_instance) == STATUS_OK; return self->has_lock; } -bool common_hal_nativeio_spi_has_lock(nativeio_spi_obj_t *self) { +bool common_hal_busio_spi_has_lock(busio_spi_obj_t *self) { return self->has_lock; } -void common_hal_nativeio_spi_unlock(nativeio_spi_obj_t *self) { +void common_hal_busio_spi_unlock(busio_spi_obj_t *self) { self->has_lock = false; spi_unlock(&self->spi_master_instance); } -bool common_hal_nativeio_spi_write(nativeio_spi_obj_t *self, +bool common_hal_busio_spi_write(busio_spi_obj_t *self, const uint8_t *data, size_t len) { if (len == 0) { return true; @@ -220,7 +217,7 @@ bool common_hal_nativeio_spi_write(nativeio_spi_obj_t *self, return status == STATUS_OK; } -bool common_hal_nativeio_spi_read(nativeio_spi_obj_t *self, +bool common_hal_busio_spi_read(busio_spi_obj_t *self, uint8_t *data, size_t len, uint8_t write_value) { if (len == 0) { return true; diff --git a/atmel-samd/common-hal/busio/SPI.h b/atmel-samd/common-hal/busio/SPI.h new file mode 100644 index 0000000000..10dbe5734b --- /dev/null +++ b/atmel-samd/common-hal/busio/SPI.h @@ -0,0 +1,45 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft + * + * 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_BUSIO_SPI_H__ +#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_SPI_H__ + +#include "common-hal/microcontroller/types.h" + +#include "asf/sam0/drivers/sercom/spi/spi.h" +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + struct spi_module spi_master_instance; + bool has_lock; + uint8_t clock_pin; + uint8_t MOSI_pin; + uint8_t MISO_pin; + uint32_t current_baudrate; +} busio_spi_obj_t; + +#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_SPI_H__ diff --git a/atmel-samd/common-hal/nativeio/UART.c b/atmel-samd/common-hal/busio/UART.c similarity index 94% rename from atmel-samd/common-hal/nativeio/UART.c rename to atmel-samd/common-hal/busio/UART.c index cc42163484..8d39d53415 100644 --- a/atmel-samd/common-hal/nativeio/UART.c +++ b/atmel-samd/common-hal/busio/UART.c @@ -25,7 +25,7 @@ */ #include "shared-bindings/microcontroller/__init__.h" -#include "shared-bindings/nativeio/UART.h" +#include "shared-bindings/busio/UART.h" #include "mpconfigport.h" #include "py/gc.h" @@ -40,7 +40,7 @@ #undef ENABLE -nativeio_uart_obj_t *_uart_instances[SERCOM_INST_NUM]; +busio_uart_obj_t *_uart_instances[SERCOM_INST_NUM]; static void _sercom_default_handler( const uint8_t instance) @@ -48,7 +48,7 @@ static void _sercom_default_handler( Assert(false); } -static void _nativeio_uart_interrupt_handler(uint8_t instance) +static void _busio_uart_interrupt_handler(uint8_t instance) { /* Temporary variables */ uint16_t interrupt_status; @@ -58,7 +58,7 @@ static void _nativeio_uart_interrupt_handler(uint8_t instance) struct usart_module *module = (struct usart_module *)_sercom_instances[instance]; - nativeio_uart_obj_t *self = _uart_instances[instance]; + busio_uart_obj_t *self = _uart_instances[instance]; /* Pointer to the hardware module instance */ SercomUsart *const usart_hw = &(module->hw->USART); @@ -122,7 +122,7 @@ static void _nativeio_uart_interrupt_handler(uint8_t instance) } } -void common_hal_nativeio_uart_construct(nativeio_uart_obj_t *self, +void common_hal_busio_uart_construct(busio_uart_obj_t *self, const mcu_pin_obj_t * tx, const mcu_pin_obj_t * rx, uint32_t baudrate, uint8_t bits, uart_parity_t parity, uint8_t stop, uint32_t timeout, uint8_t receiver_buffer_size) { @@ -227,7 +227,7 @@ void common_hal_nativeio_uart_construct(nativeio_uart_obj_t *self, // We use our own interrupt handler because we want a circular buffer // instead of the jobs that ASF provides. uint8_t instance_index = _sercom_get_sercom_inst_index(self->uart_instance.hw); - _sercom_set_handler(instance_index, _nativeio_uart_interrupt_handler); + _sercom_set_handler(instance_index, _busio_uart_interrupt_handler); _sercom_instances[instance_index] = &self->uart_instance; _uart_instances[instance_index] = self; @@ -238,7 +238,7 @@ void common_hal_nativeio_uart_construct(nativeio_uart_obj_t *self, self->uart_instance.hw->USART.INTENSET.bit.RXC = true; } -void common_hal_nativeio_uart_deinit(nativeio_uart_obj_t *self) { +void common_hal_busio_uart_deinit(busio_uart_obj_t *self) { self->uart_instance.hw->USART.INTENCLR.bit.RXC = true; uint8_t instance_index = _sercom_get_sercom_inst_index(self->uart_instance.hw); @@ -254,7 +254,7 @@ void common_hal_nativeio_uart_deinit(nativeio_uart_obj_t *self) { } // Read characters. -size_t common_hal_nativeio_uart_read(nativeio_uart_obj_t *self, uint8_t *data, size_t len, int *errcode) { +size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t len, int *errcode) { size_t total_read = 0; uint64_t start_ticks = ticks_ms; while (total_read < len && ticks_ms - start_ticks < self->timeout_ms) { @@ -287,7 +287,7 @@ size_t common_hal_nativeio_uart_read(nativeio_uart_obj_t *self, uint8_t *data, s } // Write characters. -size_t common_hal_nativeio_uart_write(nativeio_uart_obj_t *self, const uint8_t *data, size_t len, int *errcode) { +size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data, size_t len, int *errcode) { /* Check that the transmitter is enabled */ if (!(self->uart_instance.transmitter_enabled)) { *errcode = MP_EIO; @@ -362,14 +362,14 @@ size_t common_hal_nativeio_uart_write(nativeio_uart_obj_t *self, const uint8_t * return tx_pos; } -uint32_t common_hal_nativeio_uart_rx_characters_available(nativeio_uart_obj_t *self) { +uint32_t common_hal_busio_uart_rx_characters_available(busio_uart_obj_t *self) { if (self->uart_instance.character_size == USART_CHARACTER_SIZE_9BIT) { return self->buffer_size / 2; } return self->buffer_size; } -bool common_hal_nativeio_uart_ready_to_tx(nativeio_uart_obj_t *self) { +bool common_hal_busio_uart_ready_to_tx(busio_uart_obj_t *self) { if (!(self->uart_instance.transmitter_enabled)) { return false; } diff --git a/atmel-samd/common-hal/busio/UART.h b/atmel-samd/common-hal/busio/UART.h new file mode 100644 index 0000000000..d9a001de4f --- /dev/null +++ b/atmel-samd/common-hal/busio/UART.h @@ -0,0 +1,50 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft + * + * 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_BUSIO_UART_H__ +#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_UART_H__ + +#include "common-hal/microcontroller/types.h" + +#include "asf/sam0/drivers/sercom/usart/usart.h" +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + struct usart_module uart_instance; + uint8_t rx_pin; + uint8_t tx_pin; + uint32_t timeout_ms; + bool rx_error; + // Index of the oldest received character. + uint32_t buffer_start; + // Index of the next available spot to store a character. + uint32_t buffer_size; + uint32_t buffer_length; + uint8_t* buffer; +} busio_uart_obj_t; + +#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_UART_H__ diff --git a/atmel-samd/common-hal/busio/__init__.c b/atmel-samd/common-hal/busio/__init__.c new file mode 100644 index 0000000000..41761b6743 --- /dev/null +++ b/atmel-samd/common-hal/busio/__init__.c @@ -0,0 +1 @@ +// No busio module functions. diff --git a/atmel-samd/common-hal/nativeio/DigitalInOut.c b/atmel-samd/common-hal/digitalio/DigitalInOut.c similarity index 74% rename from atmel-samd/common-hal/nativeio/DigitalInOut.c rename to atmel-samd/common-hal/digitalio/DigitalInOut.c index e09b5af172..391bf89926 100644 --- a/atmel-samd/common-hal/nativeio/DigitalInOut.c +++ b/atmel-samd/common-hal/digitalio/DigitalInOut.c @@ -31,13 +31,13 @@ #include "py/runtime.h" #include "py/mphal.h" -#include "shared-bindings/nativeio/DigitalInOut.h" +#include "shared-bindings/digitalio/DigitalInOut.h" #include "asf/sam0/drivers/port/port.h" #include "asf/sam0/drivers/system/pinmux/pinmux.h" -digitalinout_result_t common_hal_nativeio_digitalinout_construct( - nativeio_digitalinout_obj_t* self, const mcu_pin_obj_t* pin) { +digitalinout_result_t common_hal_digitalio_digitalinout_construct( + digitalio_digitalinout_obj_t* self, const mcu_pin_obj_t* pin) { self->pin = pin; struct port_config pin_conf; @@ -49,7 +49,7 @@ digitalinout_result_t common_hal_nativeio_digitalinout_construct( return DIGITALINOUT_OK; } -void common_hal_nativeio_digitalinout_deinit(nativeio_digitalinout_obj_t* self) { +void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t* self) { struct port_config pin_conf; port_get_config_defaults(&pin_conf); @@ -57,15 +57,15 @@ void common_hal_nativeio_digitalinout_deinit(nativeio_digitalinout_obj_t* self) port_pin_set_config(self->pin->pin, &pin_conf); } -void common_hal_nativeio_digitalinout_switch_to_input( - nativeio_digitalinout_obj_t* self, enum digitalinout_pull_t pull) { +void common_hal_digitalio_digitalinout_switch_to_input( + digitalio_digitalinout_obj_t* self, enum digitalinout_pull_t pull) { self->output = false; - common_hal_nativeio_digitalinout_set_pull(self, pull); + common_hal_digitalio_digitalinout_set_pull(self, pull); } -void common_hal_nativeio_digitalinout_switch_to_output( - nativeio_digitalinout_obj_t* self, bool value, +void common_hal_digitalio_digitalinout_switch_to_output( + digitalio_digitalinout_obj_t* self, bool value, enum digitalinout_drive_mode_t drive_mode) { struct port_config pin_conf; port_get_config_defaults(&pin_conf); @@ -76,16 +76,16 @@ void common_hal_nativeio_digitalinout_switch_to_output( self->output = true; self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN; - common_hal_nativeio_digitalinout_set_value(self, value); + common_hal_digitalio_digitalinout_set_value(self, value); } -enum digitalinout_direction_t common_hal_nativeio_digitalinout_get_direction( - nativeio_digitalinout_obj_t* self) { +enum digitalinout_direction_t common_hal_digitalio_digitalinout_get_direction( + digitalio_digitalinout_obj_t* self) { return self->output? DIRECTION_OUT : DIRECTION_IN; } -void common_hal_nativeio_digitalinout_set_value( - nativeio_digitalinout_obj_t* self, bool value) { +void common_hal_digitalio_digitalinout_set_value( + digitalio_digitalinout_obj_t* self, bool value) { uint32_t pin = self->pin->pin; PortGroup *const port_base = port_get_group_from_gpio_pin(pin); uint32_t pin_mask = (1UL << (pin % 32)); @@ -104,8 +104,8 @@ void common_hal_nativeio_digitalinout_set_value( } } -bool common_hal_nativeio_digitalinout_get_value( - nativeio_digitalinout_obj_t* self) { +bool common_hal_digitalio_digitalinout_get_value( + digitalio_digitalinout_obj_t* self) { uint32_t pin = self->pin->pin; PortGroup *const port_base = port_get_group_from_gpio_pin(pin); uint32_t pin_mask = (1UL << (pin % 32)); @@ -120,20 +120,20 @@ bool common_hal_nativeio_digitalinout_get_value( } } -void common_hal_nativeio_digitalinout_set_drive_mode( - nativeio_digitalinout_obj_t* self, +void common_hal_digitalio_digitalinout_set_drive_mode( + digitalio_digitalinout_obj_t* self, enum digitalinout_drive_mode_t drive_mode) { - bool value = common_hal_nativeio_digitalinout_get_value(self); + bool value = common_hal_digitalio_digitalinout_get_value(self); self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN; // True is implemented differently between modes so reset the value to make // sure its correct for the new mode. if (value) { - common_hal_nativeio_digitalinout_set_value(self, value); + common_hal_digitalio_digitalinout_set_value(self, value); } } -enum digitalinout_drive_mode_t common_hal_nativeio_digitalinout_get_drive_mode( - nativeio_digitalinout_obj_t* self) { +enum digitalinout_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode( + digitalio_digitalinout_obj_t* self) { if (self->open_drain) { return DRIVE_MODE_OPEN_DRAIN; } else { @@ -141,8 +141,8 @@ enum digitalinout_drive_mode_t common_hal_nativeio_digitalinout_get_drive_mode( } } -void common_hal_nativeio_digitalinout_set_pull( - nativeio_digitalinout_obj_t* self, enum digitalinout_pull_t pull) { +void common_hal_digitalio_digitalinout_set_pull( + digitalio_digitalinout_obj_t* self, enum digitalinout_pull_t pull) { enum port_pin_pull asf_pull = PORT_PIN_PULL_NONE; switch (pull) { case PULL_UP: @@ -163,8 +163,8 @@ void common_hal_nativeio_digitalinout_set_pull( port_pin_set_config(self->pin->pin, &pin_conf); } -enum digitalinout_pull_t common_hal_nativeio_digitalinout_get_pull( - nativeio_digitalinout_obj_t* self) { +enum digitalinout_pull_t common_hal_digitalio_digitalinout_get_pull( + digitalio_digitalinout_obj_t* self) { uint32_t pin = self->pin->pin; PortGroup *const port_base = port_get_group_from_gpio_pin(pin); uint32_t pin_mask = (1UL << (pin % 32)); diff --git a/esp8266/common-hal/nativeio/DigitalInOut.h b/atmel-samd/common-hal/digitalio/DigitalInOut.h similarity index 81% rename from esp8266/common-hal/nativeio/DigitalInOut.h rename to atmel-samd/common-hal/digitalio/DigitalInOut.h index 6325b978b9..1490765abe 100644 --- a/esp8266/common-hal/nativeio/DigitalInOut.h +++ b/atmel-samd/common-hal/digitalio/DigitalInOut.h @@ -24,9 +24,10 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_ESP8266_COMMON_HAL_NATIVEIO_DIGITALINOUT_H__ -#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_NATIVEIO_DIGITALINOUT_H__ +#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_DIGITALIO_DIGITALINOUT_H__ +#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_DIGITALIO_DIGITALINOUT_H__ +#include "common-hal/microcontroller/types.h" #include "py/obj.h" typedef struct { @@ -34,6 +35,6 @@ typedef struct { const mcu_pin_obj_t * pin; bool output; bool open_drain; -} nativeio_digitalinout_obj_t; +} digitalio_digitalinout_obj_t; -#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_NATIVEIO_DIGITALINOUT_H__ +#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_DIGITALIO_DIGITALINOUT_H__ diff --git a/atmel-samd/common-hal/digitalio/__init__.c b/atmel-samd/common-hal/digitalio/__init__.c new file mode 100644 index 0000000000..20fad45959 --- /dev/null +++ b/atmel-samd/common-hal/digitalio/__init__.c @@ -0,0 +1 @@ +// No digitalio module functions. diff --git a/atmel-samd/common-hal/nativeio/PulseOut.h b/atmel-samd/common-hal/nativeio/PulseOut.h deleted file mode 100644 index 721670befb..0000000000 --- a/atmel-samd/common-hal/nativeio/PulseOut.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * 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_PULSEOUT_H__ -#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_PULSEOUT_H__ - -void pulseout_reset(void); - -#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_PULSEOUT_H__ diff --git a/atmel-samd/common-hal/nativeio/TouchInStub.c b/atmel-samd/common-hal/nativeio/TouchInStub.c deleted file mode 100644 index cfdc1249fe..0000000000 --- a/atmel-samd/common-hal/nativeio/TouchInStub.c +++ /dev/null @@ -1,45 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2016 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 - -#include "py/nlr.h" -#include "py/runtime.h" -#include "py/binary.h" -#include "py/mphal.h" -#include "shared-bindings/nativeio/TouchIn.h" - -void common_hal_nativeio_touchin_construct(nativeio_touchin_obj_t* self, - const mcu_pin_obj_t *pin) { - mp_raise_NotImplementedError("No capacitive touch support"); -} - -void common_hal_nativeio_touchin_deinit(nativeio_touchin_obj_t* self) { -} - -bool common_hal_nativeio_touchin_get_value(nativeio_touchin_obj_t *self) { - return false; -} diff --git a/atmel-samd/common-hal/nativeio/__init__.c b/atmel-samd/common-hal/nativeio/__init__.c deleted file mode 100644 index e0ec8acfb1..0000000000 --- a/atmel-samd/common-hal/nativeio/__init__.c +++ /dev/null @@ -1 +0,0 @@ -// No nativeio module functions. diff --git a/atmel-samd/common-hal/nativeio/types.h b/atmel-samd/common-hal/nativeio/types.h deleted file mode 100644 index 9f8c19e9db..0000000000 --- a/atmel-samd/common-hal/nativeio/types.h +++ /dev/null @@ -1,142 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2016 Scott Shawcroft - * - * 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_COMMON_HAL_NATIVEIO_TYPES_H__ -#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_TYPES_H__ - -#include "common-hal/microcontroller/types.h" - -// Don't reorder these includes because they are dependencies of adc_feature.h. -// They should really be included by adc_feature.h. -#include -#include "asf/sam0/drivers/system/clock/gclk.h" -#include "asf/sam0/utils/cmsis/samd21/include/component/adc.h" -#include "asf/sam0/drivers/adc/adc_sam_d_r/adc_feature.h" - -#include "asf/sam0/drivers/dac/dac.h" -#include "asf/sam0/drivers/sercom/i2c/i2c_master.h" -#include "asf/sam0/drivers/sercom/spi/spi.h" -#include "asf/sam0/drivers/sercom/usart/usart.h" -#include "asf/sam0/drivers/tc/tc.h" -#include "asf/sam0/drivers/tcc/tcc.h" -// Only support TouchIn when external SPI flash is used. -#ifdef SPI_FLASH_SECTOR_SIZE -#include "QTouch/touch_api_ptc.h" -#endif - -#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; - struct adc_module * adc_instance; -} nativeio_analogin_obj_t; - -typedef struct { - mp_obj_base_t base; - struct dac_module dac_instance; -} nativeio_analogout_obj_t; - -typedef struct { - mp_obj_base_t base; - struct i2c_master_module i2c_master_instance; - bool has_lock; - uint8_t scl_pin; - uint8_t sda_pin; -} nativeio_i2c_obj_t; - -typedef struct { - mp_obj_base_t base; - struct spi_module spi_master_instance; - bool has_lock; - uint8_t clock_pin; - uint8_t MOSI_pin; - uint8_t MISO_pin; - uint32_t current_baudrate; -} nativeio_spi_obj_t; - -typedef struct { - mp_obj_base_t base; - uint8_t channel; - uint8_t pin; - uint16_t* buffer; - uint16_t maxlen; - bool idle_state; - volatile uint16_t start; - volatile uint16_t len; - volatile bool first_edge; - uint16_t ticks_per_ms; -} nativeio_pulsein_obj_t; - -typedef struct { - mp_obj_base_t base; - __IO PORT_PINCFG_Type *pincfg; - uint8_t pin; -} nativeio_pulseout_obj_t; - -typedef struct { - mp_obj_base_t base; - const mcu_pin_obj_t *pin; - const pin_timer_t* timer; - bool variable_frequency; - union { - struct tc_module tc_instance; - struct tcc_module tcc_instance; - }; -} nativeio_pwmout_obj_t; - -typedef struct { - mp_obj_base_t base; - // Only support TouchIn when external SPI flash is used. - #ifdef SPI_FLASH_SECTOR_SIZE - const mcu_pin_obj_t * pin; - sensor_id_t sensor_id; - #endif -} nativeio_touchin_obj_t; - -typedef struct { - mp_obj_base_t base; - struct usart_module uart_instance; - uint8_t rx_pin; - uint8_t tx_pin; - uint32_t timeout_ms; - bool rx_error; - // Index of the oldest received character. - uint32_t buffer_start; - // Index of the next available spot to store a character. - uint32_t buffer_size; - uint32_t buffer_length; - uint8_t* buffer; -} nativeio_uart_obj_t; - -#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_TYPES_H__ diff --git a/atmel-samd/common-hal/neopixel_write/__init__.c b/atmel-samd/common-hal/neopixel_write/__init__.c index ff0e99cd8b..27dc7d6ad0 100644 --- a/atmel-samd/common-hal/neopixel_write/__init__.c +++ b/atmel-samd/common-hal/neopixel_write/__init__.c @@ -29,8 +29,9 @@ #include "shared-bindings/neopixel_write/__init__.h" #include "asf/common2/services/delay/delay.h" +#include "asf/sam0/drivers/port/port.h" -void common_hal_neopixel_write(const nativeio_digitalinout_obj_t* digitalinout, uint8_t *pixels, uint32_t numBytes) { +void common_hal_neopixel_write(const digitalio_digitalinout_obj_t* digitalinout, uint8_t *pixels, uint32_t numBytes) { // This is adapted directly from the Adafruit NeoPixel library SAMD21G18A code: // https://github.com/adafruit/Adafruit_NeoPixel/blob/master/Adafruit_NeoPixel.cpp uint8_t *ptr, *end, p, bitMask; diff --git a/atmel-samd/common-hal/nativeio/PWMOut.c b/atmel-samd/common-hal/pulseio/PWMOut.c similarity index 92% rename from atmel-samd/common-hal/nativeio/PWMOut.c rename to atmel-samd/common-hal/pulseio/PWMOut.c index 73d83163a8..863f2084e2 100644 --- a/atmel-samd/common-hal/nativeio/PWMOut.c +++ b/atmel-samd/common-hal/pulseio/PWMOut.c @@ -27,8 +27,8 @@ #include #include "py/runtime.h" -#include "common-hal/nativeio/PWMOut.h" -#include "shared-bindings/nativeio/PWMOut.h" +#include "common-hal/pulseio/PWMOut.h" +#include "shared-bindings/pulseio/PWMOut.h" #include "samd21_pins.h" @@ -70,7 +70,7 @@ bool channel_ok(const pin_timer_t* t, uint8_t index) { t->is_tc; } -void common_hal_nativeio_pwmout_construct(nativeio_pwmout_obj_t* self, +void common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self, const mcu_pin_obj_t* pin, uint16_t duty, uint32_t frequency, @@ -198,10 +198,10 @@ void common_hal_nativeio_pwmout_construct(nativeio_pwmout_obj_t* self, pin_config.direction = SYSTEM_PINMUX_PIN_DIR_OUTPUT; system_pinmux_pin_set_config(pin->pin, &pin_config); - common_hal_nativeio_pwmout_set_duty_cycle(self, duty); + common_hal_pulseio_pwmout_set_duty_cycle(self, duty); } -extern void common_hal_nativeio_pwmout_deinit(nativeio_pwmout_obj_t* self) { +extern void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t* self) { const pin_timer_t* t = self->timer; uint8_t index = (((uint32_t) t->tcc) - ((uint32_t) TCC0)) / 0x400; timer_refcount[index]--; @@ -225,7 +225,7 @@ extern void common_hal_nativeio_pwmout_deinit(nativeio_pwmout_obj_t* self) { reset_pin(self->pin->pin); } -extern void common_hal_nativeio_pwmout_set_duty_cycle(nativeio_pwmout_obj_t* self, uint16_t duty) { +extern void common_hal_pulseio_pwmout_set_duty_cycle(pulseio_pwmout_obj_t* self, uint16_t duty) { const pin_timer_t* t = self->timer; if (t->is_tc) { uint32_t top = ((uint32_t) t->tc->COUNT16.CC[0].reg + 1); @@ -238,7 +238,7 @@ extern void common_hal_nativeio_pwmout_set_duty_cycle(nativeio_pwmout_obj_t* sel } } -uint16_t common_hal_nativeio_pwmout_get_duty_cycle(nativeio_pwmout_obj_t* self) { +uint16_t common_hal_pulseio_pwmout_get_duty_cycle(pulseio_pwmout_obj_t* self) { const pin_timer_t* t = self->timer; if (t->is_tc) { uint16_t top = t->tc->COUNT16.CC[0].reg; @@ -260,7 +260,7 @@ uint16_t common_hal_nativeio_pwmout_get_duty_cycle(nativeio_pwmout_obj_t* self) } -void common_hal_nativeio_pwmout_set_frequency(nativeio_pwmout_obj_t* self, +void common_hal_pulseio_pwmout_set_frequency(pulseio_pwmout_obj_t* self, uint32_t frequency) { if (frequency == 0 || frequency > 6000000) { mp_raise_ValueError("Invalid PWM frequency"); @@ -281,7 +281,7 @@ void common_hal_nativeio_pwmout_set_frequency(nativeio_pwmout_obj_t* self, break; } } - uint16_t old_duty = common_hal_nativeio_pwmout_get_duty_cycle(self); + uint16_t old_duty = common_hal_pulseio_pwmout_get_duty_cycle(self); uint8_t old_divisor; if (t->is_tc) { old_divisor = t->tc->COUNT16.CTRLA.bit.PRESCALER; @@ -308,10 +308,10 @@ void common_hal_nativeio_pwmout_set_frequency(nativeio_pwmout_obj_t* self, tcc_set_top_value(&self->tcc_instance, new_top); } - common_hal_nativeio_pwmout_set_duty_cycle(self, old_duty); + common_hal_pulseio_pwmout_set_duty_cycle(self, old_duty); } -uint32_t common_hal_nativeio_pwmout_get_frequency(nativeio_pwmout_obj_t* self) { +uint32_t common_hal_pulseio_pwmout_get_frequency(pulseio_pwmout_obj_t* self) { uint32_t system_clock = system_cpu_clock_get_hz(); const pin_timer_t* t = self->timer; uint32_t top; @@ -326,6 +326,6 @@ uint32_t common_hal_nativeio_pwmout_get_frequency(nativeio_pwmout_obj_t* self) { return (system_clock / prescaler[divisor]) / (top + 1); } -bool common_hal_nativeio_pwmout_get_variable_frequency(nativeio_pwmout_obj_t* self) { +bool common_hal_pulseio_pwmout_get_variable_frequency(pulseio_pwmout_obj_t* self) { return self->variable_frequency; } diff --git a/atmel-samd/common-hal/pulseio/PWMOut.h b/atmel-samd/common-hal/pulseio/PWMOut.h new file mode 100644 index 0000000000..b8e207f3ec --- /dev/null +++ b/atmel-samd/common-hal/pulseio/PWMOut.h @@ -0,0 +1,49 @@ +/* + * 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_PULSEIO_PWMOUT_H__ +#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PWMOUT_H__ + +#include "common-hal/microcontroller/types.h" +#include "asf/sam0/drivers/tc/tc.h" +#include "asf/sam0/drivers/tcc/tcc.h" + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + const mcu_pin_obj_t *pin; + const pin_timer_t* timer; + bool variable_frequency; + union { + struct tc_module tc_instance; + struct tcc_module tcc_instance; + }; +} pulseio_pwmout_obj_t; + +void pwmout_reset(void); + +#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PWMOUT_H__ diff --git a/atmel-samd/common-hal/nativeio/PulseIn.c b/atmel-samd/common-hal/pulseio/PulseIn.c similarity index 87% rename from atmel-samd/common-hal/nativeio/PulseIn.c rename to atmel-samd/common-hal/pulseio/PulseIn.c index f5df2e8cdb..69eff2a0bf 100644 --- a/atmel-samd/common-hal/nativeio/PulseIn.c +++ b/atmel-samd/common-hal/pulseio/PulseIn.c @@ -24,24 +24,25 @@ * THE SOFTWARE. */ -#include "common-hal/nativeio/PulseIn.h" +#include "common-hal/pulseio/PulseIn.h" #include #include "asf/common2/services/delay/delay.h" #include "asf/sam0/drivers/extint/extint.h" #include "asf/sam0/drivers/extint/extint_callback.h" +#include "asf/sam0/drivers/port/port.h" #include "mpconfigport.h" #include "py/gc.h" #include "py/runtime.h" #include "samd21_pins.h" #include "shared-bindings/microcontroller/__init__.h" -#include "shared-bindings/nativeio/PulseIn.h" +#include "shared-bindings/pulseio/PulseIn.h" #include "tick.h" -static nativeio_pulsein_obj_t *active_pulseins[EIC_NUMBER_OF_INTERRUPTS]; +static pulseio_pulsein_obj_t *active_pulseins[EIC_NUMBER_OF_INTERRUPTS]; static uint64_t last_ms[EIC_NUMBER_OF_INTERRUPTS]; static uint16_t last_us[EIC_NUMBER_OF_INTERRUPTS]; @@ -53,7 +54,7 @@ void pulsein_reset(void) { } } -static void pulsein_set_config(nativeio_pulsein_obj_t* self, bool first_edge) { +static void pulsein_set_config(pulseio_pulsein_obj_t* self, bool first_edge) { struct extint_chan_conf config; extint_chan_get_config_defaults(&config); config.gpio_pin = self->pin; @@ -78,7 +79,7 @@ static void pulsein_callback(void) { // Grab the current time first. uint16_t current_us = tc_get_count_value(&ms_timer); uint64_t current_ms = ticks_ms; - nativeio_pulsein_obj_t* self = active_pulseins[extint_get_current_channel()]; + pulseio_pulsein_obj_t* self = active_pulseins[extint_get_current_channel()]; current_us = current_us * 1000 / self->ticks_per_ms; if (self->first_edge) { self->first_edge = false; @@ -110,7 +111,7 @@ static void pulsein_callback(void) { last_us[self->channel] = current_us; } -void common_hal_nativeio_pulsein_construct(nativeio_pulsein_obj_t* self, +void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, const mcu_pin_obj_t* pin, uint16_t maxlen, bool idle_state) { if (!pin->has_extint) { mp_raise_RuntimeError("No hardware support on pin"); @@ -144,17 +145,17 @@ void common_hal_nativeio_pulsein_construct(nativeio_pulsein_obj_t* self, extint_chan_enable_callback(self->channel, EXTINT_CALLBACK_TYPE_DETECT); } -void common_hal_nativeio_pulsein_deinit(nativeio_pulsein_obj_t* self) { +void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) { extint_chan_disable_callback(self->channel, EXTINT_CALLBACK_TYPE_DETECT); active_pulseins[self->channel] = NULL; reset_pin(self->pin); } -void common_hal_nativeio_pulsein_pause(nativeio_pulsein_obj_t* self) { +void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self) { extint_chan_disable_callback(self->channel, EXTINT_CALLBACK_TYPE_DETECT); } -void common_hal_nativeio_pulsein_resume(nativeio_pulsein_obj_t* self, +void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, uint16_t trigger_duration) { // Send the trigger pulse. if (trigger_duration > 0) { @@ -176,14 +177,14 @@ void common_hal_nativeio_pulsein_resume(nativeio_pulsein_obj_t* self, pulsein_set_config(self, true); } -void common_hal_nativeio_pulsein_clear(nativeio_pulsein_obj_t* self) { +void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t* self) { common_hal_mcu_disable_interrupts(); self->start = 0; self->len = 0; common_hal_mcu_enable_interrupts(); } -uint16_t common_hal_nativeio_pulsein_popleft(nativeio_pulsein_obj_t* self) { +uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) { if (self->len == 0) { mp_raise_IndexError("pop from an empty PulseIn"); } @@ -196,15 +197,15 @@ uint16_t common_hal_nativeio_pulsein_popleft(nativeio_pulsein_obj_t* self) { return value; } -uint16_t common_hal_nativeio_pulsein_get_maxlen(nativeio_pulsein_obj_t* self) { +uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t* self) { return self->maxlen; } -uint16_t common_hal_nativeio_pulsein_get_len(nativeio_pulsein_obj_t* self) { +uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t* self) { return self->len; } -uint16_t common_hal_nativeio_pulsein_get_item(nativeio_pulsein_obj_t* self, +uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self, int16_t index) { common_hal_mcu_disable_interrupts(); if (index < 0) { diff --git a/atmel-samd/common-hal/pulseio/PulseIn.h b/atmel-samd/common-hal/pulseio/PulseIn.h new file mode 100644 index 0000000000..717b7267a3 --- /dev/null +++ b/atmel-samd/common-hal/pulseio/PulseIn.h @@ -0,0 +1,49 @@ +/* + * 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_PULSEIO_PULSEIN_H__ +#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEIN_H__ + +#include "common-hal/microcontroller/types.h" + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + uint8_t channel; + uint8_t pin; + uint16_t* buffer; + uint16_t maxlen; + bool idle_state; + volatile uint16_t start; + volatile uint16_t len; + volatile bool first_edge; + uint16_t ticks_per_ms; +} pulseio_pulsein_obj_t; + +void pulsein_reset(void); + +#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEIN_H__ diff --git a/atmel-samd/common-hal/nativeio/PulseOut.c b/atmel-samd/common-hal/pulseio/PulseOut.c similarity index 91% rename from atmel-samd/common-hal/nativeio/PulseOut.c rename to atmel-samd/common-hal/pulseio/PulseOut.c index 76ef849a1a..1df4e30db6 100644 --- a/atmel-samd/common-hal/nativeio/PulseOut.c +++ b/atmel-samd/common-hal/pulseio/PulseOut.c @@ -24,15 +24,16 @@ * THE SOFTWARE. */ -#include "common-hal/nativeio/PulseOut.h" +#include "common-hal/pulseio/PulseOut.h" #include #include "asf/sam0/drivers/tc/tc_interrupt.h" +#include "asf/sam0/drivers/port/port.h" #include "mpconfigport.h" #include "py/runtime.h" -#include "shared-bindings/nativeio/PulseOut.h" +#include "shared-bindings/pulseio/PulseOut.h" #undef ENABLE @@ -78,8 +79,8 @@ void pulseout_reset() { active_pincfg = NULL; } -void common_hal_nativeio_pulseout_construct(nativeio_pulseout_obj_t* self, - const nativeio_pwmout_obj_t* carrier) { +void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, + const pulseio_pwmout_obj_t* carrier) { if (refcount == 0) { // Find a spare timer. Tc *t = NULL; @@ -121,7 +122,7 @@ void common_hal_nativeio_pulseout_construct(nativeio_pulseout_obj_t* self, turn_off(self->pincfg); } -void common_hal_nativeio_pulseout_deinit(nativeio_pulseout_obj_t* self) { +void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self) { PortGroup *const port_base = port_get_group_from_gpio_pin(self->pin); port_base->DIRCLR.reg = 1 << (self->pin % 32); @@ -133,7 +134,7 @@ void common_hal_nativeio_pulseout_deinit(nativeio_pulseout_obj_t* self) { } } -void common_hal_nativeio_pulseout_send(nativeio_pulseout_obj_t* self, uint16_t* pulses, uint16_t length) { +void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t* self, uint16_t* pulses, uint16_t length) { if (active_pincfg != NULL) { mp_raise_RuntimeError("Another send is already active"); } diff --git a/atmel-samd/common-hal/pulseio/PulseOut.h b/atmel-samd/common-hal/pulseio/PulseOut.h new file mode 100644 index 0000000000..f06dcc44f3 --- /dev/null +++ b/atmel-samd/common-hal/pulseio/PulseOut.h @@ -0,0 +1,44 @@ +/* + * 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_PULSEIO_PULSEOUT_H__ +#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEOUT_H__ + +#include "common-hal/microcontroller/types.h" +#include "asf/sam0/drivers/tc/tc.h" +#include "asf/sam0/drivers/tcc/tcc.h" + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + __IO PORT_PINCFG_Type *pincfg; + uint8_t pin; +} pulseio_pulseout_obj_t; + +void pulseout_reset(void); + +#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEOUT_H__ diff --git a/atmel-samd/common-hal/pulseio/__init__.c b/atmel-samd/common-hal/pulseio/__init__.c new file mode 100644 index 0000000000..2bee925bc7 --- /dev/null +++ b/atmel-samd/common-hal/pulseio/__init__.c @@ -0,0 +1 @@ +// No pulseio module functions. diff --git a/atmel-samd/common-hal/nativeio/TouchIn.c b/atmel-samd/common-hal/touchio/TouchIn.c similarity index 96% rename from atmel-samd/common-hal/nativeio/TouchIn.c rename to atmel-samd/common-hal/touchio/TouchIn.c index a98d235ef3..a680294e27 100644 --- a/atmel-samd/common-hal/nativeio/TouchIn.c +++ b/atmel-samd/common-hal/touchio/TouchIn.c @@ -31,7 +31,7 @@ #include "py/runtime.h" #include "py/binary.h" #include "py/mphal.h" -#include "shared-bindings/nativeio/TouchIn.h" +#include "shared-bindings/touchio/TouchIn.h" #include "tick.h" @@ -182,9 +182,9 @@ touch_config_t touch_config = { DEF_TOUCH_PTC_ISR_LVL, /* PTC interrupt level. */ }; -nativeio_touchin_obj_t *active_touchin_obj[DEF_SELFCAP_NUM_CHANNELS]; +touchio_touchin_obj_t *active_touchin_obj[DEF_SELFCAP_NUM_CHANNELS]; -void common_hal_nativeio_touchin_construct(nativeio_touchin_obj_t* self, +void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self, const mcu_pin_obj_t *pin) { if (!pin->has_touch) { mp_raise_ValueError("Invalid pin"); @@ -245,13 +245,13 @@ void common_hal_nativeio_touchin_construct(nativeio_touchin_obj_t* self, } // Run a measurement to get calibrated. - common_hal_nativeio_touchin_get_value(self); + common_hal_touchio_touchin_get_value(self); } // This may get called twice, once on exit or deinit and once from the finaliser // (__del__). We need the finaliser to reduce the chance of us writing to memory // we no longer own because we keep track of Python pointers in active_touchin_obj. -void common_hal_nativeio_touchin_deinit(nativeio_touchin_obj_t* self) { +void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t* self) { touch_selfcap_sensor_disable(self->sensor_id); // Remove ourselves from the list of active objects. We don't change the // selfcap settings at all because we don't want to reinit now. Instead, @@ -273,7 +273,7 @@ void touch_selfcap_measure_complete_callback(void) touch_acq_status = p_selfcap_measure_data->acq_status; } -bool common_hal_nativeio_touchin_get_value(nativeio_touchin_obj_t *self) { +bool common_hal_touchio_touchin_get_value(touchio_touchin_obj_t *self) { if (p_selfcap_measure_data->acq_status & TOUCH_CC_CALIB_ERROR) { mp_raise_RuntimeError("Touch calibration error"); } diff --git a/atmel-samd/common-hal/touchio/TouchIn.h b/atmel-samd/common-hal/touchio/TouchIn.h new file mode 100644 index 0000000000..85799529e9 --- /dev/null +++ b/atmel-samd/common-hal/touchio/TouchIn.h @@ -0,0 +1,42 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft + * + * 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_TOUCHIO_TOUCHIN_H__ +#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_TOUCHIO_TOUCHIN_H__ + +#include "common-hal/microcontroller/types.h" + +#include "QTouch/touch_api_ptc.h" + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + const mcu_pin_obj_t * pin; + sensor_id_t sensor_id; +} touchio_touchin_obj_t; + +#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_TOUCHIO_TOUCHIN_H__ diff --git a/atmel-samd/common-hal/touchio/__init__.c b/atmel-samd/common-hal/touchio/__init__.c new file mode 100644 index 0000000000..d2290447c9 --- /dev/null +++ b/atmel-samd/common-hal/touchio/__init__.c @@ -0,0 +1 @@ +// No touchio module functions. diff --git a/atmel-samd/main.c b/atmel-samd/main.c index 8c22b6a6cd..613abb5a35 100644 --- a/atmel-samd/main.c +++ b/atmel-samd/main.c @@ -24,13 +24,13 @@ #include "asf/sam0/drivers/system/system.h" #include -#include "common-hal/nativeio/AnalogIn.h" -#include "common-hal/nativeio/PulseOut.h" -#include "common-hal/nativeio/PWMOut.h" +#include "common-hal/analogio/AnalogIn.h" +#include "common-hal/pulseio/PulseOut.h" +#include "common-hal/pulseio/PWMOut.h" #include "common-hal/usb_hid/__init__.h" #ifdef EXPRESS_BOARD -#include "common-hal/nativeio/types.h" +#include "common-hal/touchio/TouchIn.h" #include "QTouch/touch_api_ptc.h" #define INTERNAL_CIRCUITPY_CONFIG_START_ADDR (0x00040000 - 0x100) #else @@ -138,7 +138,7 @@ void reset_mp(void) { } #ifdef EXPRESS_BOARD -extern nativeio_touchin_obj_t *active_touchin_obj[DEF_SELFCAP_NUM_CHANNELS]; +extern touchio_touchin_obj_t *active_touchin_obj[DEF_SELFCAP_NUM_CHANNELS]; extern touch_selfcap_config_t selfcap_config; #endif extern volatile bool mp_msc_enabled; diff --git a/atmel-samd/mpconfigport.h b/atmel-samd/mpconfigport.h index 1d1abe21e6..b6b77d24fe 100644 --- a/atmel-samd/mpconfigport.h +++ b/atmel-samd/mpconfigport.h @@ -126,7 +126,10 @@ typedef long mp_off_t; // extra built in modules to add to the list of known ones extern const struct _mp_obj_module_t microcontroller_module; extern const struct _mp_obj_module_t bitbangio_module; -extern const struct _mp_obj_module_t nativeio_module; +extern const struct _mp_obj_module_t analogio_module; +extern const struct _mp_obj_module_t digitalio_module; +extern const struct _mp_obj_module_t pulseio_module; +extern const struct _mp_obj_module_t busio_module; extern const struct _mp_obj_module_t board_module; extern const struct _mp_obj_module_t uos_module; extern const struct _mp_obj_module_t time_module; @@ -137,8 +140,10 @@ extern const struct _mp_obj_module_t usb_hid_module; // Internal flash size dependent settings. #if BOARD_FLASH_SIZE > 192000 + extern const struct _mp_obj_module_t touchio_module; #define MICROPY_PY_MICROPYTHON_MEM_INFO (1) #define EXTRA_BUILTIN_MODULES \ + { MP_OBJ_NEW_QSTR(MP_QSTR_touchio), (mp_obj_t)&touchio_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_bitbangio), (mp_obj_t)&bitbangio_module } #define EXPRESS_BOARD #else @@ -148,7 +153,10 @@ extern const struct _mp_obj_module_t usb_hid_module; #define MICROPY_PORT_BUILTIN_MODULES \ { MP_OBJ_NEW_QSTR(MP_QSTR_microcontroller), (mp_obj_t)µcontroller_module }, \ - { MP_OBJ_NEW_QSTR(MP_QSTR_nativeio), (mp_obj_t)&nativeio_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_analogio), (mp_obj_t)&analogio_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_digitalio), (mp_obj_t)&digitalio_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_pulseio), (mp_obj_t)&pulseio_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_busio), (mp_obj_t)&busio_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_board), (mp_obj_t)&board_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_uos), (mp_obj_t)&uos_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_module }, \ diff --git a/atmel-samd/rgb_led_status.c b/atmel-samd/rgb_led_status.c index 7d77fc1eb5..e04f12339c 100644 --- a/atmel-samd/rgb_led_status.c +++ b/atmel-samd/rgb_led_status.c @@ -4,7 +4,7 @@ #include "mphalport.h" #include "shared-bindings/bitbangio/SPI.h" -#include "shared-bindings/nativeio/DigitalInOut.h" +#include "shared-bindings/digitalio/DigitalInOut.h" #include "shared-bindings/neopixel_write/__init__.h" #include "shared-module/bitbangio/types.h" #include "rgb_led_status.h" @@ -12,7 +12,7 @@ #ifdef MICROPY_HW_NEOPIXEL static uint8_t status_neopixel_color[3]; -static nativeio_digitalinout_obj_t status_neopixel; +static digitalio_digitalinout_obj_t status_neopixel; #endif #if defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK) @@ -26,8 +26,8 @@ static uint32_t current_status_color = 0; void rgb_led_status_init() { #ifdef MICROPY_HW_NEOPIXEL - common_hal_nativeio_digitalinout_construct(&status_neopixel, MICROPY_HW_NEOPIXEL); - common_hal_nativeio_digitalinout_switch_to_output(&status_neopixel, false, DRIVE_MODE_PUSH_PULL); + common_hal_digitalio_digitalinout_construct(&status_neopixel, MICROPY_HW_NEOPIXEL); + common_hal_digitalio_digitalinout_switch_to_output(&status_neopixel, false, DRIVE_MODE_PUSH_PULL); #endif #if defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK) shared_module_bitbangio_spi_construct(&status_apa102, diff --git a/docs/common_hal.md b/docs/common_hal.md index 9492699f40..22093c3baa 100644 --- a/docs/common_hal.md +++ b/docs/common_hal.md @@ -1,5 +1,7 @@ -# Adding `nativeio` support to other ports -`nativeio` provides a well-defined, cross-port hardware abstraction layer built to support different devices and their drivers. It's backed by the Common HAL, a C api suitable for supporting different hardware in a similar manner. By sharing this C api, developers can support new hardware easily and cross-port functionality to the new hardware. +# Adding `digitalio` support to other ports +`digitalio` provides a well-defined, cross-port hardware abstraction layer built to support different devices and their drivers. It's backed by the Common HAL, a C api suitable for supporting different hardware in a similar manner. By sharing this C api, developers can support new hardware easily and cross-port functionality to the new hardware. + +These instructions also apply to `analogio`, `busio`, `pulseio` and `touchio`. Most drivers depend on `analogio`, `digitalio` and `busio` so start with those. ## File layout Common HAL related files are found in these locations: @@ -20,15 +22,23 @@ SRC_BINDINGS = \ board/__init__.c \ microcontroller/__init__.c \ microcontroller/Pin.c \ - nativeio/__init__.c \ - nativeio/AnalogIn.c \ - nativeio/AnalogOut.c \ - nativeio/DigitalInOut.c \ - nativeio/I2C.c \ - nativeio/PWMOut.c \ - nativeio/SPI.c \ + analogio/__init__.c \ + analogio/AnalogIn.c \ + analogio/AnalogOut.c \ + digitalio/__init__.c \ + digitalio/DigitalInOut.c \ + pulseio/__init__.c \ + pulseio/PulseIn.c \ + pulseio/PulseOut.c \ + pulseio/PWMOut.c \ + busio/__init__.c \ + busio/I2C.c \ + busio/SPI.c \ + busio/UART.c \ neopixel_write/__init__.c \ - time/__init__.c + time/__init__.c \ + usb_hid/__init__.c \ + usb_hid/Device.c SRC_BINDINGS_EXPANDED = $(addprefix shared-bindings/, $(SRC_BINDINGS)) \ $(addprefix common-hal/, $(SRC_BINDINGS)) @@ -46,14 +56,20 @@ Built in modules are typically defined in `mpconfigport.h`. To add support you s ``` extern const struct _mp_obj_module_t microcontroller_module; -extern const struct _mp_obj_module_t nativeio_module; +extern const struct _mp_obj_module_t analogio_module; +extern const struct _mp_obj_module_t digitalio_module; +extern const struct _mp_obj_module_t pulseio_module; +extern const struct _mp_obj_module_t busio_module; extern const struct _mp_obj_module_t board_module; extern const struct _mp_obj_module_t time_module; extern const struct _mp_obj_module_t neopixel_write_module; #define MICROPY_PORT_BUILTIN_MODULES \ { MP_OBJ_NEW_QSTR(MP_QSTR_microcontroller), (mp_obj_t)µcontroller_module }, \ - { MP_OBJ_NEW_QSTR(MP_QSTR_nativeio), (mp_obj_t)&nativeio_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_analogio), (mp_obj_t)&analogio_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_digitalio), (mp_obj_t)&digitalio_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_pulseio), (mp_obj_t)&pulseio_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_busio), (mp_obj_t)&busio_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_board), (mp_obj_t)&board_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_neopixel_write),(mp_obj_t)&neopixel_write_module } \ diff --git a/docs/design_guide.rst b/docs/design_guide.rst index 8f56697fdb..69860b7dca 100644 --- a/docs/design_guide.rst +++ b/docs/design_guide.rst @@ -267,8 +267,8 @@ you can because you never know how much something will be called. Fewer allocations means less time spent cleaning up. So, where you can, prefer bytearray buffers that are created in ``__init__`` and used throughout the object with methods that read or write into the buffer instead of creating new -objects. `nativeio` classes are design to read and write to subsections of -buffers. +objects. Unified hardware API classes such as `busio.SPI` are design to read and +write to subsections of buffers. Its ok to allocate an object to return to the user. Just beware of causing more than one allocation per call due to internal logic. diff --git a/esp8266/Makefile b/esp8266/Makefile index eac6eddc51..93e424112a 100644 --- a/esp8266/Makefile +++ b/esp8266/Makefile @@ -79,7 +79,7 @@ SRC_C = \ esp_init_data.c \ gccollect.c \ lexerstr32.c \ - uart.c \ + espuart.c \ esppwm.c \ esponewire.c \ espneopixel.c \ @@ -109,16 +109,18 @@ SRC_C = \ SRC_COMMON_HAL = \ microcontroller/__init__.c \ microcontroller/Pin.c \ - nativeio/__init__.c \ - nativeio/AnalogIn.c \ - nativeio/AnalogOut.c \ - nativeio/DigitalInOut.c \ - nativeio/PulseIn.c \ - nativeio/PulseOut.c \ - nativeio/PWMOut.c \ - nativeio/SPI.c \ - nativeio/TouchIn.c \ - nativeio/UART.c \ + analogio/__init__.c \ + analogio/AnalogIn.c \ + analogio/AnalogOut.c \ + digitalio/__init__.c \ + digitalio/DigitalInOut.c \ + pulseio/__init__.c \ + pulseio/PulseIn.c \ + pulseio/PulseOut.c \ + pulseio/PWMOut.c \ + busio/__init__.c \ + busio/SPI.c \ + busio/UART.c \ neopixel_write/__init__.c \ time/__init__.c \ board/__init__.c @@ -131,8 +133,8 @@ SRC_SHARED_MODULE = \ bitbangio/I2C.c \ bitbangio/OneWire.c \ bitbangio/SPI.c \ - nativeio/I2C.c \ - nativeio/OneWire.c \ + busio/I2C.c \ + busio/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/AnalogIn.c b/esp8266/common-hal/analogio/AnalogIn.c similarity index 86% rename from esp8266/common-hal/nativeio/AnalogIn.c rename to esp8266/common-hal/analogio/AnalogIn.c index 57ff607a30..0f43a08791 100644 --- a/esp8266/common-hal/nativeio/AnalogIn.c +++ b/esp8266/common-hal/analogio/AnalogIn.c @@ -32,13 +32,13 @@ #include "py/binary.h" #include "py/mphal.h" #include "common-hal/microcontroller/__init__.h" -#include "shared-bindings/nativeio/AnalogIn.h" +#include "shared-bindings/analogio/AnalogIn.h" #include "user_interface.h" volatile bool adc_in_use __attribute__((aligned(4))) = false; -void common_hal_nativeio_analogin_construct(nativeio_analogin_obj_t* self, +void common_hal_analogio_analogin_construct(analogio_analogin_obj_t* self, const mcu_pin_obj_t *pin) { if (pin != &pin_TOUT) { nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Pin %q does not have ADC capabilities", pin->name)); @@ -46,15 +46,15 @@ void common_hal_nativeio_analogin_construct(nativeio_analogin_obj_t* self, adc_in_use = true; } -void common_hal_nativeio_analogin_deinit(nativeio_analogin_obj_t* self) { +void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t* self) { adc_in_use = false; } -uint16_t common_hal_nativeio_analogin_get_value(nativeio_analogin_obj_t *self) { +uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) { // ADC is 10 bit so shift by 6 to make it 16-bit. return system_adc_read() << 6; } -float common_hal_nativeio_analogin_get_reference_voltage(nativeio_analogin_obj_t *self) { +float common_hal_analogio_analogin_get_reference_voltage(analogio_analogin_obj_t *self) { return 1.0f; } diff --git a/esp8266/common-hal/analogio/AnalogIn.h b/esp8266/common-hal/analogio/AnalogIn.h new file mode 100644 index 0000000000..a4edd233d9 --- /dev/null +++ b/esp8266/common-hal/analogio/AnalogIn.h @@ -0,0 +1,39 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft + * + * 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_ANALOGIO_ANALOGIN_H__ +#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_ANALOGIO_ANALOGIN_H__ + +#include "common-hal/microcontroller/types.h" + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + const mcu_pin_obj_t * pin; +} analogio_analogin_obj_t; + +#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_ANALOGIO_ANALOGIN_H__ diff --git a/esp8266/common-hal/nativeio/AnalogOut.c b/esp8266/common-hal/analogio/AnalogOut.c similarity index 86% rename from esp8266/common-hal/nativeio/AnalogOut.c rename to esp8266/common-hal/analogio/AnalogOut.c index 06b1880c34..30dc448300 100644 --- a/esp8266/common-hal/nativeio/AnalogOut.c +++ b/esp8266/common-hal/analogio/AnalogOut.c @@ -30,18 +30,17 @@ #include "py/runtime.h" -#include "shared-bindings/nativeio/AnalogOut.h" +#include "shared-bindings/analogio/AnalogOut.h" - -void common_hal_nativeio_analogout_construct(nativeio_analogout_obj_t* self, +void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self, const mcu_pin_obj_t *pin) { nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "No hardware support for analog out.")); } -void common_hal_nativeio_analogout_deinit(nativeio_analogout_obj_t *self) { +void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) { } -void common_hal_nativeio_analogout_set_value(nativeio_analogout_obj_t *self, +void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self, uint16_t value) { } diff --git a/esp8266/common-hal/analogio/AnalogOut.h b/esp8266/common-hal/analogio/AnalogOut.h new file mode 100644 index 0000000000..146a6571c8 --- /dev/null +++ b/esp8266/common-hal/analogio/AnalogOut.h @@ -0,0 +1,37 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft + * + * 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_ANALOGIO_ANALOGOUT_H__ +#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_ANALOGIO_ANALOGOUT_H__ + +#include "py/obj.h" + +// Not supported, throws error on construction. +typedef struct { + mp_obj_base_t base; +} analogio_analogout_obj_t; + +#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_ANALOGIO_ANALOGOUT_H__ diff --git a/esp8266/common-hal/analogio/__init__.c b/esp8266/common-hal/analogio/__init__.c new file mode 100644 index 0000000000..eea58c77d6 --- /dev/null +++ b/esp8266/common-hal/analogio/__init__.c @@ -0,0 +1 @@ +// No analogio module functions. diff --git a/esp8266/common-hal/busio/I2C.h b/esp8266/common-hal/busio/I2C.h new file mode 100644 index 0000000000..5877e6c310 --- /dev/null +++ b/esp8266/common-hal/busio/I2C.h @@ -0,0 +1,33 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft + * + * 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_BUSIO_I2C_H__ +#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_BUSIO_I2C_H__ + +// Use the bitbang wrapper for I2C +#include "shared-module/busio/I2C.h" + +#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_BUSIO_I2C_H__ diff --git a/esp8266/common-hal/busio/OneWire.h b/esp8266/common-hal/busio/OneWire.h new file mode 100644 index 0000000000..3c52ced385 --- /dev/null +++ b/esp8266/common-hal/busio/OneWire.h @@ -0,0 +1,33 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft + * + * 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_BUSIO_ONEWIRE_H__ +#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_BUSIO_ONEWIRE_H__ + +// Use the bitbang wrapper for OneWire +#include "shared-module/busio/OneWire.h" + +#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_BUSIO_ONEWIRE_H__ diff --git a/esp8266/common-hal/nativeio/SPI.c b/esp8266/common-hal/busio/SPI.c similarity index 90% rename from esp8266/common-hal/nativeio/SPI.c rename to esp8266/common-hal/busio/SPI.c index 427c0d33c1..9ffb0294dd 100644 --- a/esp8266/common-hal/nativeio/SPI.c +++ b/esp8266/common-hal/busio/SPI.c @@ -27,7 +27,7 @@ #include "esp8266/ets_alt_task.h" #include "esp8266/hspi.h" #include "shared-bindings/microcontroller/__init__.h" -#include "shared-bindings/nativeio/SPI.h" +#include "common-hal/busio/SPI.h" #include "py/nlr.h" #include "eagle_soc.h" @@ -38,7 +38,7 @@ extern const mcu_pin_obj_t pin_MTMS; extern const mcu_pin_obj_t pin_MTCK; extern const mcu_pin_obj_t pin_MTDI; -void common_hal_nativeio_spi_construct(nativeio_spi_obj_t *self, +void common_hal_busio_spi_construct(busio_spi_obj_t *self, const mcu_pin_obj_t * clock, const mcu_pin_obj_t * mosi, const mcu_pin_obj_t * miso) { if (clock != &pin_MTMS || !((mosi == &pin_MTCK && miso == MP_OBJ_TO_PTR(mp_const_none)) || @@ -73,7 +73,7 @@ void common_hal_nativeio_spi_construct(nativeio_spi_obj_t *self, CLEAR_PERI_REG_MASK(SPI_USER(HSPI), SPI_FLASH_MODE); } -void common_hal_nativeio_spi_deinit(nativeio_spi_obj_t *self) { +void common_hal_busio_spi_deinit(busio_spi_obj_t *self) { PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, 0); PIN_PULLUP_DIS(PERIPHS_IO_MUX_MTDI_U); @@ -87,7 +87,7 @@ void common_hal_nativeio_spi_deinit(nativeio_spi_obj_t *self) { gpio_output_set(0x0, 0x0, 0x0, 0x7 << 12); } -bool common_hal_nativeio_spi_configure(nativeio_spi_obj_t *self, +bool common_hal_busio_spi_configure(busio_spi_obj_t *self, uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) { if (bits != 8) { return false; @@ -112,7 +112,7 @@ bool common_hal_nativeio_spi_configure(nativeio_spi_obj_t *self, return true; } -bool common_hal_nativeio_spi_try_lock(nativeio_spi_obj_t *self) { +bool common_hal_busio_spi_try_lock(busio_spi_obj_t *self) { bool success = false; common_hal_mcu_disable_interrupts(); if (!self->locked) { @@ -123,15 +123,15 @@ bool common_hal_nativeio_spi_try_lock(nativeio_spi_obj_t *self) { return success; } -bool common_hal_nativeio_spi_has_lock(nativeio_spi_obj_t *self) { +bool common_hal_busio_spi_has_lock(busio_spi_obj_t *self) { return self->locked; } -void common_hal_nativeio_spi_unlock(nativeio_spi_obj_t *self) { +void common_hal_busio_spi_unlock(busio_spi_obj_t *self) { self->locked = false; } -bool common_hal_nativeio_spi_write(nativeio_spi_obj_t *self, +bool common_hal_busio_spi_write(busio_spi_obj_t *self, const uint8_t * data, size_t len) { size_t chunk_size = 1024; size_t count = len / chunk_size; @@ -151,7 +151,7 @@ bool common_hal_nativeio_spi_write(nativeio_spi_obj_t *self, return true; } -bool common_hal_nativeio_spi_read(nativeio_spi_obj_t *self, +bool common_hal_busio_spi_read(busio_spi_obj_t *self, uint8_t * data, size_t len, uint8_t write_value) { // Process data in chunks, let the pending tasks run in between size_t chunk_size = 1024; // TODO this should depend on baudrate diff --git a/esp8266/common-hal/busio/SPI.h b/esp8266/common-hal/busio/SPI.h new file mode 100644 index 0000000000..baff10b70c --- /dev/null +++ b/esp8266/common-hal/busio/SPI.h @@ -0,0 +1,39 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft + * + * 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_BUSIO_SPI_H__ +#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_BUSIO_SPI_H__ + +#include "common-hal/microcontroller/types.h" + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + bool locked; +} busio_spi_obj_t; + +#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_BUSIO_SPI_H__ diff --git a/esp8266/common-hal/nativeio/UART.c b/esp8266/common-hal/busio/UART.c similarity index 85% rename from esp8266/common-hal/nativeio/UART.c rename to esp8266/common-hal/busio/UART.c index 8509dec64b..b2a1d69b8a 100644 --- a/esp8266/common-hal/nativeio/UART.c +++ b/esp8266/common-hal/busio/UART.c @@ -26,17 +26,17 @@ #include "common-hal/microcontroller/__init__.h" #include "shared-bindings/microcontroller/__init__.h" -#include "shared-bindings/nativeio/UART.h" +#include "shared-bindings/busio/UART.h" #include "ets_sys.h" -#include "uart.h" +#include "espuart.h" #include "py/nlr.h" // UartDev is defined and initialized in rom code. extern UartDevice UartDev; -void common_hal_nativeio_uart_construct(nativeio_uart_obj_t *self, +void common_hal_busio_uart_construct(busio_uart_obj_t *self, const mcu_pin_obj_t * tx, const mcu_pin_obj_t * rx, uint32_t baudrate, uint8_t bits, uart_parity_t parity, uint8_t stop, uint32_t timeout, uint8_t receiver_buffer_size) { @@ -92,16 +92,16 @@ void common_hal_nativeio_uart_construct(nativeio_uart_obj_t *self, uart_setup(UART1); } -void common_hal_nativeio_uart_deinit(nativeio_uart_obj_t *self) { +void common_hal_busio_uart_deinit(busio_uart_obj_t *self) { PIN_FUNC_SELECT(FUNC_U1TXD_BK, 0); } -size_t common_hal_nativeio_uart_read(nativeio_uart_obj_t *self, uint8_t *data, size_t len, int *errcode) { +size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t len, int *errcode) { return 0; } // Write characters. -size_t common_hal_nativeio_uart_write(nativeio_uart_obj_t *self, const uint8_t *data, size_t len, int *errcode) { +size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data, size_t len, int *errcode) { // write the data for (size_t i = 0; i < len; ++i) { uart_tx_one_char(UART1, *data++); @@ -111,10 +111,10 @@ size_t common_hal_nativeio_uart_write(nativeio_uart_obj_t *self, const uint8_t * return len; } -uint32_t common_hal_nativeio_uart_rx_characters_available(nativeio_uart_obj_t *self) { +uint32_t common_hal_busio_uart_rx_characters_available(busio_uart_obj_t *self) { return 0; } -bool common_hal_nativeio_uart_ready_to_tx(nativeio_uart_obj_t *self) { +bool common_hal_busio_uart_ready_to_tx(busio_uart_obj_t *self) { return true; } diff --git a/esp8266/common-hal/busio/UART.h b/esp8266/common-hal/busio/UART.h new file mode 100644 index 0000000000..a87e7c9b23 --- /dev/null +++ b/esp8266/common-hal/busio/UART.h @@ -0,0 +1,38 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft + * + * 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_BUSIO_UART_H__ +#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_BUSIO_UART_H__ + +#include "common-hal/microcontroller/types.h" + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; +} busio_uart_obj_t; + +#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_BUSIO_UART_H__ diff --git a/esp8266/common-hal/busio/__init__.c b/esp8266/common-hal/busio/__init__.c new file mode 100644 index 0000000000..41761b6743 --- /dev/null +++ b/esp8266/common-hal/busio/__init__.c @@ -0,0 +1 @@ +// No busio module functions. diff --git a/esp8266/common-hal/nativeio/DigitalInOut.c b/esp8266/common-hal/digitalio/DigitalInOut.c similarity index 76% rename from esp8266/common-hal/nativeio/DigitalInOut.c rename to esp8266/common-hal/digitalio/DigitalInOut.c index e6a28ed754..356d0f3b8a 100644 --- a/esp8266/common-hal/nativeio/DigitalInOut.c +++ b/esp8266/common-hal/digitalio/DigitalInOut.c @@ -32,16 +32,16 @@ #include "py/runtime.h" #include "py/mphal.h" -#include "shared-bindings/nativeio/DigitalInOut.h" +#include "shared-bindings/digitalio/DigitalInOut.h" -digitalinout_result_t common_hal_nativeio_digitalinout_construct( - nativeio_digitalinout_obj_t* self, const mcu_pin_obj_t* pin) { +digitalinout_result_t common_hal_digitalio_digitalinout_construct( + digitalio_digitalinout_obj_t* self, const mcu_pin_obj_t* pin) { self->pin = pin; PIN_FUNC_SELECT(self->pin->peripheral, self->pin->gpio_function); return DIGITALINOUT_OK; } -void common_hal_nativeio_digitalinout_deinit(nativeio_digitalinout_obj_t* self) { +void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t* self) { if (self->pin->gpio_number < 16) { uint32_t pin_mask = 1 << self->pin->gpio_number; gpio_output_set(0x0, 0x0, 0x0, pin_mask); @@ -50,8 +50,8 @@ void common_hal_nativeio_digitalinout_deinit(nativeio_digitalinout_obj_t* self) } } -void common_hal_nativeio_digitalinout_switch_to_input( - nativeio_digitalinout_obj_t* self, enum digitalinout_pull_t pull) { +void common_hal_digitalio_digitalinout_switch_to_input( + digitalio_digitalinout_obj_t* self, enum digitalinout_pull_t pull) { self->output = false; if (self->pin->gpio_number == 16) { @@ -62,11 +62,11 @@ void common_hal_nativeio_digitalinout_switch_to_input( PIN_PULLUP_DIS(self->pin->peripheral); gpio_output_set(0, 0, 0, 1 << self->pin->gpio_number); } - common_hal_nativeio_digitalinout_set_pull(self, pull); + common_hal_digitalio_digitalinout_set_pull(self, pull); } -void common_hal_nativeio_digitalinout_switch_to_output( - nativeio_digitalinout_obj_t* self, bool value, +void common_hal_digitalio_digitalinout_switch_to_output( + digitalio_digitalinout_obj_t* self, bool value, enum digitalinout_drive_mode_t drive_mode) { self->output = true; self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN; @@ -78,16 +78,16 @@ void common_hal_nativeio_digitalinout_switch_to_output( gpio_output_set(0, 0, 1 << self->pin->gpio_number, 0); PIN_PULLUP_DIS(self->pin->peripheral); } - common_hal_nativeio_digitalinout_set_value(self, value); + common_hal_digitalio_digitalinout_set_value(self, value); } -enum digitalinout_direction_t common_hal_nativeio_digitalinout_get_direction( - nativeio_digitalinout_obj_t* self) { +enum digitalinout_direction_t common_hal_digitalio_digitalinout_get_direction( + digitalio_digitalinout_obj_t* self) { return self->output? DIRECTION_OUT : DIRECTION_IN; } -void common_hal_nativeio_digitalinout_set_value( - nativeio_digitalinout_obj_t* self, bool value) { +void common_hal_digitalio_digitalinout_set_value( + digitalio_digitalinout_obj_t* self, bool value) { if (value) { if (self->open_drain) { // Disable output. @@ -109,8 +109,8 @@ void common_hal_nativeio_digitalinout_set_value( // Register addresses taken from: https://github.com/esp8266/esp8266-wiki/wiki/gpio-registers volatile uint32_t* PIN_DIR = (uint32_t *) 0x6000030C; volatile uint32_t* PIN_OUT = (uint32_t *) 0x60000300; -bool common_hal_nativeio_digitalinout_get_value( - nativeio_digitalinout_obj_t* self) { +bool common_hal_digitalio_digitalinout_get_value( + digitalio_digitalinout_obj_t* self) { if (!self->output) { if (self->pin->gpio_number == 16) { return READ_PERI_REG(RTC_GPIO_IN_DATA) & 1; @@ -126,20 +126,20 @@ bool common_hal_nativeio_digitalinout_get_value( } } -void common_hal_nativeio_digitalinout_set_drive_mode( - nativeio_digitalinout_obj_t* self, +void common_hal_digitalio_digitalinout_set_drive_mode( + digitalio_digitalinout_obj_t* self, enum digitalinout_drive_mode_t drive_mode) { - bool value = common_hal_nativeio_digitalinout_get_value(self); + bool value = common_hal_digitalio_digitalinout_get_value(self); self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN; // True is implemented differently between modes so reset the value to make // sure its correct for the new mode. if (value) { - common_hal_nativeio_digitalinout_set_value(self, value); + common_hal_digitalio_digitalinout_set_value(self, value); } } -enum digitalinout_drive_mode_t common_hal_nativeio_digitalinout_get_drive_mode( - nativeio_digitalinout_obj_t* self) { +enum digitalinout_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode( + digitalio_digitalinout_obj_t* self) { if (self->open_drain) { return DRIVE_MODE_OPEN_DRAIN; } else { @@ -147,8 +147,8 @@ enum digitalinout_drive_mode_t common_hal_nativeio_digitalinout_get_drive_mode( } } -void common_hal_nativeio_digitalinout_set_pull( - nativeio_digitalinout_obj_t* self, enum digitalinout_pull_t pull) { +void common_hal_digitalio_digitalinout_set_pull( + digitalio_digitalinout_obj_t* self, enum digitalinout_pull_t pull) { if (pull == PULL_DOWN) { nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "ESP8266 does not support pull down.")); @@ -166,8 +166,8 @@ void common_hal_nativeio_digitalinout_set_pull( } } -enum digitalinout_pull_t common_hal_nativeio_digitalinout_get_pull( - nativeio_digitalinout_obj_t* self) { +enum digitalinout_pull_t common_hal_digitalio_digitalinout_get_pull( + digitalio_digitalinout_obj_t* self) { if (self->pin->gpio_number < 16 && (READ_PERI_REG(self->pin->peripheral) & PERIPHS_IO_MUX_PULLUP) != 0) { return PULL_UP; diff --git a/atmel-samd/common-hal/nativeio/DigitalInOut.h b/esp8266/common-hal/digitalio/DigitalInOut.h similarity index 82% rename from atmel-samd/common-hal/nativeio/DigitalInOut.h rename to esp8266/common-hal/digitalio/DigitalInOut.h index f517e0d5b9..94de1bca55 100644 --- a/atmel-samd/common-hal/nativeio/DigitalInOut.h +++ b/esp8266/common-hal/digitalio/DigitalInOut.h @@ -24,9 +24,10 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_DIGITALINOUT_H__ -#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_DIGITALINOUT_H__ +#ifndef __MICROPY_INCLUDED_ESP8266_COMMON_HAL_DIGITALIO_DIGITALINOUT_H__ +#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_DIGITALIO_DIGITALINOUT_H__ +#include "common-hal/microcontroller/types.h" #include "py/obj.h" typedef struct { @@ -34,6 +35,6 @@ typedef struct { const mcu_pin_obj_t * pin; bool output; bool open_drain; -} nativeio_digitalinout_obj_t; +} digitalio_digitalinout_obj_t; -#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_DIGITALINOUT_H__ +#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_DIGITALIO_DIGITALINOUT_H__ diff --git a/esp8266/common-hal/digitalio/__init__.c b/esp8266/common-hal/digitalio/__init__.c new file mode 100644 index 0000000000..20fad45959 --- /dev/null +++ b/esp8266/common-hal/digitalio/__init__.c @@ -0,0 +1 @@ +// No digitalio module functions. diff --git a/esp8266/common-hal/nativeio/TouchIn.c b/esp8266/common-hal/nativeio/TouchIn.c deleted file mode 100644 index 1501a79362..0000000000 --- a/esp8266/common-hal/nativeio/TouchIn.c +++ /dev/null @@ -1,46 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2016 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 - -#include "py/nlr.h" -#include "py/runtime.h" -#include "py/binary.h" -#include "py/mphal.h" -#include "shared-bindings/nativeio/TouchIn.h" - -void common_hal_nativeio_touchin_construct(nativeio_touchin_obj_t* self, - const mcu_pin_obj_t *pin) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, - "No hardware support for capacitive touch.")); -} - -void common_hal_nativeio_touchin_deinit(nativeio_touchin_obj_t* self) { -} - -bool common_hal_nativeio_touchin_get_value(nativeio_touchin_obj_t *self) { - return false; -} diff --git a/esp8266/common-hal/nativeio/__init__.c b/esp8266/common-hal/nativeio/__init__.c deleted file mode 100644 index e0ec8acfb1..0000000000 --- a/esp8266/common-hal/nativeio/__init__.c +++ /dev/null @@ -1 +0,0 @@ -// No nativeio module functions. diff --git a/esp8266/common-hal/nativeio/types.h b/esp8266/common-hal/nativeio/types.h deleted file mode 100644 index 05b7d28c28..0000000000 --- a/esp8266/common-hal/nativeio/types.h +++ /dev/null @@ -1,78 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2016 Scott Shawcroft - * - * 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_COMMON_HAL_NATIVEIO_TYPES_H__ -#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_TYPES_H__ - -#include "common-hal/microcontroller/types.h" - -#include "py/obj.h" - -// Use the bitbang wrapper for I2C and OneWire -#include "shared-module/nativeio/I2C.h" -#include "shared-module/nativeio/OneWire.h" - -typedef struct { - mp_obj_base_t base; - const mcu_pin_obj_t * pin; -} nativeio_analogin_obj_t; - -// Not supported, throws error on construction. -typedef struct { - mp_obj_base_t base; -} nativeio_analogout_obj_t; - -typedef struct { - mp_obj_base_t base; - bool locked; -} nativeio_spi_obj_t; - -typedef struct { - mp_obj_base_t base; -} nativeio_pulsein_obj_t; - -typedef struct { - mp_obj_base_t base; -} nativeio_pulseout_obj_t; - -typedef struct { - mp_obj_base_t base; - int channel; - const mcu_pin_obj_t* pin; -} nativeio_pwmout_obj_t; - -typedef struct { - mp_obj_base_t base; -} nativeio_touchin_obj_t; - -typedef struct { - mp_obj_base_t base; -} nativeio_uart_obj_t; - -#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_TYPES_H__ diff --git a/esp8266/common-hal/neopixel_write/__init__.c b/esp8266/common-hal/neopixel_write/__init__.c index daaeead431..84a743d111 100644 --- a/esp8266/common-hal/neopixel_write/__init__.c +++ b/esp8266/common-hal/neopixel_write/__init__.c @@ -28,6 +28,6 @@ #include "espneopixel.h" -void common_hal_neopixel_write(const nativeio_digitalinout_obj_t* digitalinout, uint8_t *pixels, uint32_t numBytes) { +void common_hal_neopixel_write(const digitalio_digitalinout_obj_t* digitalinout, uint8_t *pixels, uint32_t numBytes) { esp_neopixel_write(digitalinout->pin->gpio_number, pixels, numBytes); } diff --git a/esp8266/common-hal/nativeio/PWMOut.c b/esp8266/common-hal/pulseio/PWMOut.c similarity index 83% rename from esp8266/common-hal/nativeio/PWMOut.c rename to esp8266/common-hal/pulseio/PWMOut.c index 234246d15f..40a46e3927 100644 --- a/esp8266/common-hal/nativeio/PWMOut.c +++ b/esp8266/common-hal/pulseio/PWMOut.c @@ -30,7 +30,7 @@ #include "esppwm.h" #include "py/runtime.h" -#include "shared-bindings/nativeio/PWMOut.h" +#include "shared-bindings/pulseio/PWMOut.h" #include "eagle_soc.h" #include "c_types.h" @@ -46,7 +46,7 @@ void pwmout_reset(void) { first_channel_variable = false; } -void common_hal_nativeio_pwmout_construct(nativeio_pwmout_obj_t* self, const mcu_pin_obj_t* pin, uint16_t duty, uint32_t frequency, +void common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self, const mcu_pin_obj_t* pin, uint16_t duty, uint32_t frequency, bool variable_frequency) { if (frequency > PWM_FREQ_MAX) { nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, @@ -77,7 +77,7 @@ void common_hal_nativeio_pwmout_construct(nativeio_pwmout_obj_t* self, const mcu } } -extern void common_hal_nativeio_pwmout_deinit(nativeio_pwmout_obj_t* self) { +extern void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t* self) { pwm_delete(self->channel); pwm_start(); if (self->pin->gpio_number < 16) { @@ -88,17 +88,17 @@ extern void common_hal_nativeio_pwmout_deinit(nativeio_pwmout_obj_t* self) { } } -extern void common_hal_nativeio_pwmout_set_duty_cycle(nativeio_pwmout_obj_t* self, uint16_t duty) { +extern void common_hal_pulseio_pwmout_set_duty_cycle(pulseio_pwmout_obj_t* self, uint16_t duty) { // We get 16 bits of duty in but the underlying code is only ten bit. pwm_set_duty(duty >> 6, self->channel); pwm_start(); } -uint16_t common_hal_nativeio_pwmout_get_duty_cycle(nativeio_pwmout_obj_t* self) { +uint16_t common_hal_pulseio_pwmout_get_duty_cycle(pulseio_pwmout_obj_t* self) { return pwm_get_duty(self->channel) << 6; } -void common_hal_nativeio_pwmout_set_frequency(nativeio_pwmout_obj_t* self, uint32_t frequency) { +void common_hal_pulseio_pwmout_set_frequency(pulseio_pwmout_obj_t* self, uint32_t frequency) { if (frequency > PWM_FREQ_MAX) { nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Maximum PWM frequency is %dhz.", PWM_FREQ_MAX)); @@ -109,10 +109,10 @@ void common_hal_nativeio_pwmout_set_frequency(nativeio_pwmout_obj_t* self, uint3 pwm_set_freq(frequency, 0); } -uint32_t common_hal_nativeio_pwmout_get_frequency(nativeio_pwmout_obj_t* self) { +uint32_t common_hal_pulseio_pwmout_get_frequency(pulseio_pwmout_obj_t* self) { return pwm_get_freq(0); } -bool common_hal_nativeio_pwmout_get_variable_frequency(nativeio_pwmout_obj_t* self) { +bool common_hal_pulseio_pwmout_get_variable_frequency(pulseio_pwmout_obj_t* self) { return first_channel_variable; } diff --git a/esp8266/common-hal/pulseio/PWMOut.h b/esp8266/common-hal/pulseio/PWMOut.h new file mode 100644 index 0000000000..7eb22e6e18 --- /dev/null +++ b/esp8266/common-hal/pulseio/PWMOut.h @@ -0,0 +1,40 @@ +/* + * 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_PULSEIO_PWMOUT_H__ +#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_PULSEIO_PWMOUT_H__ + +#include "common-hal/microcontroller/types.h" + +typedef struct { + mp_obj_base_t base; + int channel; + const mcu_pin_obj_t* pin; +} pulseio_pwmout_obj_t; + +void pwmout_reset(void); + +#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_PULSEIO_PWMOUT_H__ diff --git a/esp8266/common-hal/nativeio/PulseIn.c b/esp8266/common-hal/pulseio/PulseIn.c similarity index 69% rename from esp8266/common-hal/nativeio/PulseIn.c rename to esp8266/common-hal/pulseio/PulseIn.c index c4bd5a77c0..784116befe 100644 --- a/esp8266/common-hal/nativeio/PulseIn.c +++ b/esp8266/common-hal/pulseio/PulseIn.c @@ -30,40 +30,40 @@ #include "py/gc.h" #include "py/runtime.h" #include "shared-bindings/microcontroller/__init__.h" -#include "shared-bindings/nativeio/PulseIn.h" +#include "shared-bindings/pulseio/PulseIn.h" -void common_hal_nativeio_pulsein_construct(nativeio_pulsein_obj_t* self, +void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, const mcu_pin_obj_t* pin, uint16_t maxlen, bool idle_state) { mp_raise_NotImplementedError(""); } -void common_hal_nativeio_pulsein_deinit(nativeio_pulsein_obj_t* self) { +void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) { } -void common_hal_nativeio_pulsein_pause(nativeio_pulsein_obj_t* self) { +void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self) { } -void common_hal_nativeio_pulsein_resume(nativeio_pulsein_obj_t* self, +void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, uint16_t trigger_duration) { } -void common_hal_nativeio_pulsein_clear(nativeio_pulsein_obj_t* self) { +void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t* self) { } -uint16_t common_hal_nativeio_pulsein_popleft(nativeio_pulsein_obj_t* self) { +uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) { return 0; } -uint16_t common_hal_nativeio_pulsein_get_maxlen(nativeio_pulsein_obj_t* self) { +uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t* self) { return 0; } -uint16_t common_hal_nativeio_pulsein_get_len(nativeio_pulsein_obj_t* self) { +uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t* self) { return 0; } -uint16_t common_hal_nativeio_pulsein_get_item(nativeio_pulsein_obj_t* self, +uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self, int16_t index) { return 0; } diff --git a/esp8266/common-hal/nativeio/PWMOut.h b/esp8266/common-hal/pulseio/PulseIn.h similarity index 82% rename from esp8266/common-hal/nativeio/PWMOut.h rename to esp8266/common-hal/pulseio/PulseIn.h index 9406b49538..c3a27d9298 100644 --- a/esp8266/common-hal/nativeio/PWMOut.h +++ b/esp8266/common-hal/pulseio/PulseIn.h @@ -24,9 +24,15 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_ESP8266_COMMON_HAL_NATIVEIO_PWMOUT_H__ -#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_NATIVEIO_PWMOUT_H__ +#ifndef __MICROPY_INCLUDED_ESP8266_COMMON_HAL_PULSEIO_PULSEIN_H__ +#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_PULSEIO_PULSEIN_H__ + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; +} pulseio_pulsein_obj_t; void pwmout_reset(void); -#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_NATIVEIO_PWMOUT_H__ +#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_PULSEIO_PULSEIN_H__ diff --git a/esp8266/common-hal/nativeio/PulseOut.c b/esp8266/common-hal/pulseio/PulseOut.c similarity index 78% rename from esp8266/common-hal/nativeio/PulseOut.c rename to esp8266/common-hal/pulseio/PulseOut.c index d5d1e63661..1301abbf38 100644 --- a/esp8266/common-hal/nativeio/PulseOut.c +++ b/esp8266/common-hal/pulseio/PulseOut.c @@ -28,15 +28,15 @@ #include #include "py/runtime.h" -#include "shared-bindings/nativeio/PulseOut.h" +#include "shared-bindings/pulseio/PulseOut.h" -void common_hal_nativeio_pulseout_construct(nativeio_pulseout_obj_t* self, - const nativeio_pwmout_obj_t* carrier) { +void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, + const pulseio_pwmout_obj_t* carrier) { nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "No hardware support for PulseOut.")); } -void common_hal_nativeio_pulseout_deinit(nativeio_pulseout_obj_t* self) { +void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self) { } -void common_hal_nativeio_pulseout_send(nativeio_pulseout_obj_t* self, uint16_t* pulses, uint16_t length) { +void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t* self, uint16_t* pulses, uint16_t length) { } diff --git a/atmel-samd/common-hal/nativeio/PWMOut.h b/esp8266/common-hal/pulseio/PulseOut.h similarity index 81% rename from atmel-samd/common-hal/nativeio/PWMOut.h rename to esp8266/common-hal/pulseio/PulseOut.h index 56994e519a..c4c6ef6160 100644 --- a/atmel-samd/common-hal/nativeio/PWMOut.h +++ b/esp8266/common-hal/pulseio/PulseOut.h @@ -24,9 +24,15 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_PWMOUT_H__ -#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_PWMOUT_H__ +#ifndef __MICROPY_INCLUDED_ESP8266_COMMON_HAL_PULSEIO_PULSEOUT_H__ +#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_PULSEIO_PULSEOUT_H__ + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; +} pulseio_pulseout_obj_t; void pwmout_reset(void); -#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_PWMOUT_H__ +#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_PULSEIO_PULSEOUT_H__ diff --git a/esp8266/common-hal/pulseio/__init__.c b/esp8266/common-hal/pulseio/__init__.c new file mode 100644 index 0000000000..2bee925bc7 --- /dev/null +++ b/esp8266/common-hal/pulseio/__init__.c @@ -0,0 +1 @@ +// No pulseio module functions. diff --git a/esp8266/esp_mphal.c b/esp8266/esp_mphal.c index f5e284fde6..5db4f1968a 100644 --- a/esp8266/esp_mphal.c +++ b/esp8266/esp_mphal.c @@ -27,7 +27,7 @@ #include #include "ets_sys.h" #include "etshal.h" -#include "uart.h" +#include "espuart.h" #include "esp_mphal.h" #include "user_interface.h" #include "ets_alt_task.h" diff --git a/esp8266/uart.c b/esp8266/espuart.c similarity index 99% rename from esp8266/uart.c rename to esp8266/espuart.c index 001a9c673c..ee893f4ec8 100644 --- a/esp8266/uart.c +++ b/esp8266/espuart.c @@ -11,7 +11,7 @@ *******************************************************************************/ #include "ets_sys.h" #include "osapi.h" -#include "uart.h" +#include "espuart.h" #include "osapi.h" #include "uart_register.h" #include "etshal.h" diff --git a/esp8266/uart.h b/esp8266/espuart.h similarity index 100% rename from esp8266/uart.h rename to esp8266/espuart.h diff --git a/esp8266/machine_uart.c b/esp8266/machine_uart.c index 9bc6422cc7..0ae9c6e6c6 100644 --- a/esp8266/machine_uart.c +++ b/esp8266/machine_uart.c @@ -29,7 +29,7 @@ #include #include "ets_sys.h" -#include "uart.h" +#include "espuart.h" #include "py/runtime.h" #include "py/stream.h" diff --git a/esp8266/main.c b/esp8266/main.c index 0c1ad1bab8..9ac728e381 100644 --- a/esp8266/main.c +++ b/esp8266/main.c @@ -40,7 +40,7 @@ #include "gccollect.h" #include "user_interface.h" #include "common-hal/microcontroller/Pin.h" -#include "common-hal/nativeio/PWMOut.h" +#include "common-hal/pulseio/PWMOut.h" STATIC char heap[36 * 1024]; diff --git a/esp8266/modesp.c b/esp8266/modesp.c index f13d94b32f..77f8d1332e 100644 --- a/esp8266/modesp.c +++ b/esp8266/modesp.c @@ -38,7 +38,7 @@ #include "netutils.h" #include "queue.h" #include "ets_sys.h" -#include "uart.h" +#include "espuart.h" #include "user_interface.h" #include "espconn.h" #include "spi_flash.h" diff --git a/esp8266/mpconfigport.h b/esp8266/mpconfigport.h index fa2e8bc1b7..c4a3752002 100644 --- a/esp8266/mpconfigport.h +++ b/esp8266/mpconfigport.h @@ -156,7 +156,10 @@ extern const struct _mp_obj_module_t mp_module_machine; extern const struct _mp_obj_module_t onewire_module; extern const struct _mp_obj_module_t microcontroller_module; extern const struct _mp_obj_module_t board_module; -extern const struct _mp_obj_module_t nativeio_module; +extern const struct _mp_obj_module_t analogio_module; +extern const struct _mp_obj_module_t digitalio_module; +extern const struct _mp_obj_module_t pulseio_module; +extern const struct _mp_obj_module_t busio_module; extern const struct _mp_obj_module_t bitbangio_module; extern const struct _mp_obj_module_t time_module; @@ -172,7 +175,10 @@ extern const struct _mp_obj_module_t time_module; { MP_OBJ_NEW_QSTR(MP_QSTR__onewire), (mp_obj_t)&onewire_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_microcontroller), (mp_obj_t)µcontroller_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_board), (mp_obj_t)&board_module }, \ - { MP_OBJ_NEW_QSTR(MP_QSTR_nativeio), (mp_obj_t)&nativeio_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_analogio), (mp_obj_t)&analogio_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_digitalio), (mp_obj_t)&digitalio_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_pulseio), (mp_obj_t)&pulseio_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_busio), (mp_obj_t)&busio_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_bitbangio), (mp_obj_t)&bitbangio_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_module }, \ diff --git a/shared-bindings/nativeio/AnalogIn.c b/shared-bindings/analogio/AnalogIn.c similarity index 61% rename from shared-bindings/nativeio/AnalogIn.c rename to shared-bindings/analogio/AnalogIn.c index 4513cd8afe..f073022501 100644 --- a/shared-bindings/nativeio/AnalogIn.c +++ b/shared-bindings/analogio/AnalogIn.c @@ -33,19 +33,19 @@ #include "py/objproperty.h" #include "py/runtime.h" #include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/AnalogIn.h" +#include "shared-bindings/analogio/AnalogIn.h" -//| .. currentmodule:: nativeio +//| .. currentmodule:: analogio //| //| :class:`AnalogIn` -- read analog voltage //| ============================================ //| //| Usage:: //| -//| import nativeio +//| import analogio //| from board import * //| -//| with nativeio.AnalogIn(A1) as adc: +//| with analogio.AnalogIn(A1) as adc: //| val = adc.value //| @@ -56,7 +56,7 @@ //| //| :param ~microcontroller.Pin pin: the pin to read from //| -STATIC mp_obj_t nativeio_analogin_make_new(const mp_obj_type_t *type, +STATIC mp_obj_t analogio_analogin_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { // check number of arguments mp_arg_check_num(n_args, n_kw, 1, 1, false); @@ -65,11 +65,11 @@ STATIC mp_obj_t nativeio_analogin_make_new(const mp_obj_type_t *type, mp_obj_t pin_obj = args[0]; assert_pin(pin_obj, false); - nativeio_analogin_obj_t *self = m_new_obj(nativeio_analogin_obj_t); - self->base.type = &nativeio_analogin_type; + analogio_analogin_obj_t *self = m_new_obj(analogio_analogin_obj_t); + self->base.type = &analogio_analogin_type; const mcu_pin_obj_t *pin = MP_OBJ_TO_PTR(pin_obj); assert_pin_free(pin); - common_hal_nativeio_analogin_construct(self, pin); + common_hal_analogio_analogin_construct(self, pin); return (mp_obj_t) self; } @@ -78,12 +78,12 @@ STATIC mp_obj_t nativeio_analogin_make_new(const mp_obj_type_t *type, //| //| Turn off the AnalogIn and release the pin for other use. //| -STATIC mp_obj_t nativeio_analogin_deinit(mp_obj_t self_in) { - nativeio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_analogin_deinit(self); +STATIC mp_obj_t analogio_analogin_deinit(mp_obj_t self_in) { + analogio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_analogio_analogin_deinit(self); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogin_deinit_obj, nativeio_analogin_deinit); +MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogin_deinit_obj, analogio_analogin_deinit); //| .. method:: __enter__() //| @@ -95,12 +95,12 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogin_deinit_obj, nativeio_analogin_deinit //| //| Automatically deinitializes the hardware when exiting a context. //| -STATIC mp_obj_t nativeio_analogin___exit__(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t analogio_analogin___exit__(size_t n_args, const mp_obj_t *args) { (void)n_args; - common_hal_nativeio_analogin_deinit(args[0]); + common_hal_analogio_analogin_deinit(args[0]); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_analogin___exit___obj, 4, 4, nativeio_analogin___exit__); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(analogio_analogin___exit___obj, 4, 4, analogio_analogin___exit__); //| .. attribute:: value //| @@ -112,15 +112,15 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_analogin___exit___obj, 4, 4, //| :return: the data read //| :rtype: int //| -STATIC mp_obj_t nativeio_analogin_obj_get_value(mp_obj_t self_in) { - nativeio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in); - return MP_OBJ_NEW_SMALL_INT(common_hal_nativeio_analogin_get_value(self)); +STATIC mp_obj_t analogio_analogin_obj_get_value(mp_obj_t self_in) { + analogio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in); + return MP_OBJ_NEW_SMALL_INT(common_hal_analogio_analogin_get_value(self)); } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogin_get_value_obj, nativeio_analogin_obj_get_value); +MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogin_get_value_obj, analogio_analogin_obj_get_value); -const mp_obj_property_t nativeio_analogin_value_obj = { +const mp_obj_property_t analogio_analogin_value_obj = { .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_analogin_get_value_obj, + .proxy = {(mp_obj_t)&analogio_analogin_get_value_obj, (mp_obj_t)&mp_const_none_obj, (mp_obj_t)&mp_const_none_obj}, }; @@ -132,33 +132,33 @@ const mp_obj_property_t nativeio_analogin_value_obj = { //| :return: the reference voltage //| :rtype: float //| -STATIC mp_obj_t nativeio_analogin_obj_get_reference_voltage(mp_obj_t self_in) { - nativeio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in); - return mp_obj_new_float(common_hal_nativeio_analogin_get_reference_voltage(self)); +STATIC mp_obj_t analogio_analogin_obj_get_reference_voltage(mp_obj_t self_in) { + analogio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in); + return mp_obj_new_float(common_hal_analogio_analogin_get_reference_voltage(self)); } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogin_get_reference_voltage_obj, - nativeio_analogin_obj_get_reference_voltage); +MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogin_get_reference_voltage_obj, + analogio_analogin_obj_get_reference_voltage); -const mp_obj_property_t nativeio_analogin_reference_voltage_obj = { +const mp_obj_property_t analogio_analogin_reference_voltage_obj = { .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_analogin_get_reference_voltage_obj, + .proxy = {(mp_obj_t)&analogio_analogin_get_reference_voltage_obj, (mp_obj_t)&mp_const_none_obj, (mp_obj_t)&mp_const_none_obj}, }; -STATIC const mp_rom_map_elem_t nativeio_analogin_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_analogin_deinit_obj) }, +STATIC const mp_rom_map_elem_t analogio_analogin_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&analogio_analogin_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, - { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_analogin___exit___obj) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_value), MP_ROM_PTR(&nativeio_analogin_value_obj)}, - { MP_OBJ_NEW_QSTR(MP_QSTR_reference_voltage), MP_ROM_PTR(&nativeio_analogin_reference_voltage_obj)}, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&analogio_analogin___exit___obj) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_value), MP_ROM_PTR(&analogio_analogin_value_obj)}, + { MP_OBJ_NEW_QSTR(MP_QSTR_reference_voltage), MP_ROM_PTR(&analogio_analogin_reference_voltage_obj)}, }; -STATIC MP_DEFINE_CONST_DICT(nativeio_analogin_locals_dict, nativeio_analogin_locals_dict_table); +STATIC MP_DEFINE_CONST_DICT(analogio_analogin_locals_dict, analogio_analogin_locals_dict_table); -const mp_obj_type_t nativeio_analogin_type = { +const mp_obj_type_t analogio_analogin_type = { { &mp_type_type }, .name = MP_QSTR_AnalogIn, - .make_new = nativeio_analogin_make_new, - .locals_dict = (mp_obj_t)&nativeio_analogin_locals_dict, + .make_new = analogio_analogin_make_new, + .locals_dict = (mp_obj_t)&analogio_analogin_locals_dict, }; diff --git a/shared-bindings/nativeio/AnalogIn.h b/shared-bindings/analogio/AnalogIn.h similarity index 71% rename from shared-bindings/nativeio/AnalogIn.h rename to shared-bindings/analogio/AnalogIn.h index ea5cfb84e1..64ddaab0fe 100644 --- a/shared-bindings/nativeio/AnalogIn.h +++ b/shared-bindings/analogio/AnalogIn.h @@ -24,17 +24,17 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGIN_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGIN_H__ +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGIN_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGIN_H__ #include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" +#include "common-hal/analogio/AnalogIn.h" -extern const mp_obj_type_t nativeio_analogin_type; +extern const mp_obj_type_t analogio_analogin_type; -void common_hal_nativeio_analogin_construct(nativeio_analogin_obj_t* self, const mcu_pin_obj_t *pin); -void common_hal_nativeio_analogin_deinit(nativeio_analogin_obj_t* self); -uint16_t common_hal_nativeio_analogin_get_value(nativeio_analogin_obj_t *self); -float common_hal_nativeio_analogin_get_reference_voltage(nativeio_analogin_obj_t *self); +void common_hal_analogio_analogin_construct(analogio_analogin_obj_t* self, const mcu_pin_obj_t *pin); +void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t* self); +uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self); +float common_hal_analogio_analogin_get_reference_voltage(analogio_analogin_obj_t *self); -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGIN_H__ +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGIN_H__ diff --git a/shared-bindings/nativeio/AnalogOut.c b/shared-bindings/analogio/AnalogOut.c similarity index 66% rename from shared-bindings/nativeio/AnalogOut.c rename to shared-bindings/analogio/AnalogOut.c index c34fa48f35..6b02f8c0ad 100644 --- a/shared-bindings/nativeio/AnalogOut.c +++ b/shared-bindings/analogio/AnalogOut.c @@ -31,9 +31,9 @@ #include "py/objproperty.h" #include "py/runtime.h" #include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/AnalogOut.h" +#include "shared-bindings/analogio/AnalogOut.h" -//| .. currentmodule:: nativeio +//| .. currentmodule:: analogio //| //| :class:`AnalogOut` -- output analog voltage //| ============================================ @@ -42,10 +42,10 @@ //| //| Example usage:: //| -//| import nativeio +//| import analogio //| from microcontroller import pin //| -//| with nativeio.AnalogOut(pin.PA02) as dac: # output on pin PA02 +//| with analogio.AnalogOut(pin.PA02) as dac: # output on pin PA02 //| dac.value = 32768 # makes PA02 1.65V //| @@ -55,17 +55,17 @@ //| //| :param ~microcontroller.Pin pin: the pin to output to //| -STATIC mp_obj_t nativeio_analogout_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { +STATIC mp_obj_t analogio_analogout_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { // check arguments mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); assert_pin(args[0], false); const mcu_pin_obj_t *pin = MP_OBJ_TO_PTR(args[0]); - nativeio_analogout_obj_t *self = m_new_obj(nativeio_analogout_obj_t); - self->base.type = &nativeio_analogout_type; + analogio_analogout_obj_t *self = m_new_obj(analogio_analogout_obj_t); + self->base.type = &analogio_analogout_type; assert_pin_free(pin); - common_hal_nativeio_analogout_construct(self, pin); + common_hal_analogio_analogout_construct(self, pin); return self; } @@ -74,14 +74,14 @@ STATIC mp_obj_t nativeio_analogout_make_new(const mp_obj_type_t *type, mp_uint_t //| //| Turn off the AnalogOut and release the pin for other use. //| -STATIC mp_obj_t nativeio_analogout_deinit(mp_obj_t self_in) { - nativeio_analogout_obj_t *self = self_in; +STATIC mp_obj_t analogio_analogout_deinit(mp_obj_t self_in) { + analogio_analogout_obj_t *self = self_in; - common_hal_nativeio_analogout_deinit(self); + common_hal_analogio_analogout_deinit(self); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogout_deinit_obj, nativeio_analogout_deinit); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogout_deinit_obj, analogio_analogout_deinit); //| .. method:: __enter__() //| @@ -93,12 +93,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogout_deinit_obj, nativeio_analogo //| //| Automatically deinitializes the hardware when exiting a context. //| -STATIC mp_obj_t nativeio_analogout___exit__(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t analogio_analogout___exit__(size_t n_args, const mp_obj_t *args) { (void)n_args; - common_hal_nativeio_analogout_deinit(args[0]); + common_hal_analogio_analogout_deinit(args[0]); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_analogout___exit___obj, 4, 4, nativeio_analogout___exit__); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(analogio_analogout___exit___obj, 4, 4, analogio_analogout___exit__); //| .. attribute:: value //| @@ -109,39 +109,39 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_analogout___exit___obj, 4, 4 //| :return: the last value written //| :rtype: int //| -STATIC mp_obj_t nativeio_analogout_obj_set_value(mp_obj_t self_in, mp_obj_t value) { - nativeio_analogout_obj_t *self = MP_OBJ_TO_PTR(self_in); +STATIC mp_obj_t analogio_analogout_obj_set_value(mp_obj_t self_in, mp_obj_t value) { + analogio_analogout_obj_t *self = MP_OBJ_TO_PTR(self_in); uint32_t v = mp_obj_get_int(value); if (v >= (1 << 16)) { mp_raise_ValueError("AnalogOut is only 16 bits. Value must be less than 65536."); } - common_hal_nativeio_analogout_set_value(self, v); + common_hal_analogio_analogout_set_value(self, v); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_2(nativeio_analogout_set_value_obj, nativeio_analogout_obj_set_value); +MP_DEFINE_CONST_FUN_OBJ_2(analogio_analogout_set_value_obj, analogio_analogout_obj_set_value); -const mp_obj_property_t nativeio_analogout_value_obj = { +const mp_obj_property_t analogio_analogout_value_obj = { .base.type = &mp_type_property, .proxy = {(mp_obj_t)&mp_const_none_obj, - (mp_obj_t)&nativeio_analogout_set_value_obj, + (mp_obj_t)&analogio_analogout_set_value_obj, (mp_obj_t)&mp_const_none_obj}, }; -STATIC const mp_rom_map_elem_t nativeio_analogout_locals_dict_table[] = { +STATIC const mp_rom_map_elem_t analogio_analogout_locals_dict_table[] = { // instance methods - { MP_OBJ_NEW_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_analogout_deinit_obj) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&analogio_analogout_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, - { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_analogout___exit___obj) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&analogio_analogout___exit___obj) }, // Properties - { MP_OBJ_NEW_QSTR(MP_QSTR_value), (mp_obj_t)&nativeio_analogout_value_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_value), (mp_obj_t)&analogio_analogout_value_obj }, }; -STATIC MP_DEFINE_CONST_DICT(nativeio_analogout_locals_dict, nativeio_analogout_locals_dict_table); +STATIC MP_DEFINE_CONST_DICT(analogio_analogout_locals_dict, analogio_analogout_locals_dict_table); -const mp_obj_type_t nativeio_analogout_type = { +const mp_obj_type_t analogio_analogout_type = { { &mp_type_type }, .name = MP_QSTR_AnalogOut, - .make_new = nativeio_analogout_make_new, - .locals_dict = (mp_obj_t)&nativeio_analogout_locals_dict, + .make_new = analogio_analogout_make_new, + .locals_dict = (mp_obj_t)&analogio_analogout_locals_dict, }; diff --git a/shared-bindings/nativeio/AnalogOut.h b/shared-bindings/analogio/AnalogOut.h similarity index 74% rename from shared-bindings/nativeio/AnalogOut.h rename to shared-bindings/analogio/AnalogOut.h index 195e4c313f..3aa19ac294 100644 --- a/shared-bindings/nativeio/AnalogOut.h +++ b/shared-bindings/analogio/AnalogOut.h @@ -24,16 +24,16 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGOUT_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGOUT_H__ +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGOUT_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGOUT_H__ #include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" +#include "common-hal/analogio/AnalogOut.h" -extern const mp_obj_type_t nativeio_analogout_type; +extern const mp_obj_type_t analogio_analogout_type; -void common_hal_nativeio_analogout_construct(nativeio_analogout_obj_t* self, const mcu_pin_obj_t *pin); -void common_hal_nativeio_analogout_deinit(nativeio_analogout_obj_t *self); -void common_hal_nativeio_analogout_set_value(nativeio_analogout_obj_t *self, uint16_t value); +void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self, const mcu_pin_obj_t *pin); +void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self); +void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self, uint16_t value); -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGOUT_H__ +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGOUT_H__ diff --git a/shared-bindings/analogio/__init__.c b/shared-bindings/analogio/__init__.c new file mode 100644 index 0000000000..39d6bdcda4 --- /dev/null +++ b/shared-bindings/analogio/__init__.c @@ -0,0 +1,84 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 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 + +#include "py/obj.h" +#include "py/runtime.h" + +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/analogio/__init__.h" +#include "shared-bindings/analogio/AnalogIn.h" +#include "shared-bindings/analogio/AnalogOut.h" + +//| :mod:`analogio` --- Analog hardware support +//| ================================================= +//| +//| .. module:: analogio +//| :synopsis: Analog hardware support +//| :platform: SAMD21, ESP8266 +//| +//| The `analogio` module contains classes to provide access to analog IO +//| typically implemented with digital-to-analog (DAC) and analog-to-digital +//| (ADC) converters. +//| +//| Libraries +//| +//| .. toctree:: +//| :maxdepth: 3 +//| +//| AnalogIn +//| AnalogOut +//| +//| All libraries change hardware state and should be deinitialized when they +//| are no longer needed. To do so, either call :py:meth:`!deinit` or use a +//| context manager. +//| +//| For example:: +//| +//| import analogio +//| from board import * +//| +//| with analogio.AnalogIn(A0) as pin: +//| print(pin.value) +//| +//| This example will initialize the the device, read +//| :py:data:`~analogio.AnalogIn.value` and then +//| :py:meth:`~analogio.AnalogIn.deinit` the hardware. +//| + +STATIC const mp_rom_map_elem_t analogio_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_analogio) }, + { MP_ROM_QSTR(MP_QSTR_AnalogIn), MP_ROM_PTR(&analogio_analogin_type) }, + { MP_ROM_QSTR(MP_QSTR_AnalogOut), MP_ROM_PTR(&analogio_analogout_type) }, +}; + +STATIC MP_DEFINE_CONST_DICT(analogio_module_globals, analogio_module_globals_table); + +const mp_obj_module_t analogio_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&analogio_module_globals, +}; diff --git a/shared-bindings/nativeio/__init__.h b/shared-bindings/analogio/__init__.h similarity index 86% rename from shared-bindings/nativeio/__init__.h rename to shared-bindings/analogio/__init__.h index 7f4c01e051..9a0c77cb82 100644 --- a/shared-bindings/nativeio/__init__.h +++ b/shared-bindings/analogio/__init__.h @@ -24,11 +24,11 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO___INIT___H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO___INIT___H__ +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO___INIT___H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO___INIT___H__ #include "py/obj.h" // Nothing now. -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO___INIT___H__ +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO___INIT___H__ diff --git a/shared-bindings/bitbangio/OneWire.h b/shared-bindings/bitbangio/OneWire.h index dcc0386d4a..a888dae04c 100644 --- a/shared-bindings/bitbangio/OneWire.h +++ b/shared-bindings/bitbangio/OneWire.h @@ -24,8 +24,8 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ONEWIRE_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ONEWIRE_H__ +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_BITBANGIO_ONEWIRE_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_BITBANGIO_ONEWIRE_H__ #include "common-hal/microcontroller/types.h" #include "shared-module/bitbangio/types.h" @@ -39,4 +39,4 @@ 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__ +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_BITBANGIO_ONEWIRE_H__ diff --git a/shared-bindings/bitbangio/__init__.c b/shared-bindings/bitbangio/__init__.c index 5fe4697588..85acab0d36 100644 --- a/shared-bindings/bitbangio/__init__.c +++ b/shared-bindings/bitbangio/__init__.c @@ -25,7 +25,7 @@ */ // bitbangio implements some standard protocols in the processor. Its only -// dependency is nativeio.DigitalInOut. +// dependency is digitalio. #include @@ -45,12 +45,13 @@ //| //| .. module:: bitbangio //| :synopsis: Digital protocols implemented by the CPU -//| :platform: SAMD21 +//| :platform: SAMD21, ESP8266 //| -//| The `bitbangio` module contains classes to provide digital protocol support -//| regardless of whether the underlying hardware exists to use the protocol. +//| The `bitbangio` module contains classes to provide digital bus protocol +//| support regardless of whether the underlying hardware exists to use the +//| protocol. //| -//| First try to use `nativeio` module instead which may utilize peripheral +//| First try to use `busio` module instead which may utilize peripheral //| hardware to implement the protocols. Native implementations will be faster //| than bitbanged versions and have more capabilities. //| diff --git a/shared-bindings/nativeio/I2C.c b/shared-bindings/busio/I2C.c similarity index 72% rename from shared-bindings/nativeio/I2C.c rename to shared-bindings/busio/I2C.c index fe3e206b29..7383cb104e 100644 --- a/shared-bindings/nativeio/I2C.c +++ b/shared-bindings/busio/I2C.c @@ -25,14 +25,14 @@ */ // This file contains all of the Python API definitions for the -// nativeio.I2C class. +// busio.I2C class. #include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/I2C.h" +#include "shared-bindings/busio/I2C.h" #include "lib/utils/context_manager_helpers.h" #include "py/runtime.h" -//| .. currentmodule:: nativeio +//| .. currentmodule:: busio //| //| :class:`I2C` --- Two wire serial protocol //| ------------------------------------------ @@ -55,10 +55,10 @@ //| :param ~microcontroller.Pin sda: The data pin //| :param int frequency: The clock frequency in Hertz //| -STATIC mp_obj_t nativeio_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { +STATIC mp_obj_t busio_i2c_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, 0, MP_OBJ_FUN_ARGS_MAX, true); - nativeio_i2c_obj_t *self = m_new_obj(nativeio_i2c_obj_t); - self->base.type = &nativeio_i2c_type; + busio_i2c_obj_t *self = m_new_obj(busio_i2c_obj_t); + self->base.type = &busio_i2c_type; mp_map_t kw_args; mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); enum { ARG_scl, ARG_sda, ARG_frequency }; @@ -75,7 +75,7 @@ STATIC mp_obj_t nativeio_i2c_make_new(const mp_obj_type_t *type, size_t n_args, assert_pin_free(scl); const mcu_pin_obj_t* sda = MP_OBJ_TO_PTR(args[ARG_sda].u_obj); assert_pin_free(sda); - common_hal_nativeio_i2c_construct(self, scl, sda, args[ARG_frequency].u_int); + common_hal_busio_i2c_construct(self, scl, sda, args[ARG_frequency].u_int); return (mp_obj_t)self; } @@ -83,12 +83,12 @@ STATIC mp_obj_t nativeio_i2c_make_new(const mp_obj_type_t *type, size_t n_args, //| //| Releases control of the underlying hardware so other classes can use it. //| -STATIC mp_obj_t nativeio_i2c_obj_deinit(mp_obj_t self_in) { - nativeio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_i2c_deinit(self); +STATIC mp_obj_t busio_i2c_obj_deinit(mp_obj_t self_in) { + busio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_busio_i2c_deinit(self); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_i2c_deinit_obj, nativeio_i2c_obj_deinit); +MP_DEFINE_CONST_FUN_OBJ_1(busio_i2c_deinit_obj, busio_i2c_obj_deinit); //| .. method:: I2C.__enter__() //| @@ -100,15 +100,15 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_i2c_deinit_obj, nativeio_i2c_obj_deinit); //| //| Automatically deinitializes the hardware on context exit. //| -STATIC mp_obj_t nativeio_i2c_obj___exit__(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t busio_i2c_obj___exit__(size_t n_args, const mp_obj_t *args) { (void)n_args; - common_hal_nativeio_i2c_deinit(args[0]); + common_hal_busio_i2c_deinit(args[0]); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_i2c___exit___obj, 4, 4, nativeio_i2c_obj___exit__); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busio_i2c___exit___obj, 4, 4, busio_i2c_obj___exit__); -static void check_lock(nativeio_i2c_obj_t *self) { - if (!common_hal_nativeio_i2c_has_lock(self)) { +static void check_lock(busio_i2c_obj_t *self) { + if (!common_hal_busio_i2c_has_lock(self)) { mp_raise_RuntimeError("Function requires lock."); } } @@ -121,20 +121,20 @@ static void check_lock(nativeio_i2c_obj_t *self) { //| :return: List of device ids on the I2C bus //| :rtype: list //| -STATIC mp_obj_t nativeio_i2c_scan(mp_obj_t self_in) { - nativeio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in); +STATIC mp_obj_t busio_i2c_scan(mp_obj_t self_in) { + busio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in); check_lock(self); mp_obj_t list = mp_obj_new_list(0, NULL); // 7-bit addresses 0b0000xxx and 0b1111xxx are reserved for (int addr = 0x08; addr < 0x78; ++addr) { - bool success = common_hal_nativeio_i2c_probe(self, addr); + bool success = common_hal_busio_i2c_probe(self, addr); if (success) { mp_obj_list_append(list, MP_OBJ_NEW_SMALL_INT(addr)); } } return list; } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_i2c_scan_obj, nativeio_i2c_scan); +MP_DEFINE_CONST_FUN_OBJ_1(busio_i2c_scan_obj, busio_i2c_scan); //| .. method:: I2C.try_lock() //| @@ -143,20 +143,20 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_i2c_scan_obj, nativeio_i2c_scan); //| :return: True when lock has been grabbed //| :rtype: bool //| -STATIC mp_obj_t nativeio_i2c_obj_try_lock(mp_obj_t self_in) { - return mp_obj_new_bool(common_hal_nativeio_i2c_try_lock(MP_OBJ_TO_PTR(self_in))); +STATIC mp_obj_t busio_i2c_obj_try_lock(mp_obj_t self_in) { + return mp_obj_new_bool(common_hal_busio_i2c_try_lock(MP_OBJ_TO_PTR(self_in))); } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_i2c_try_lock_obj, nativeio_i2c_obj_try_lock); +MP_DEFINE_CONST_FUN_OBJ_1(busio_i2c_try_lock_obj, busio_i2c_obj_try_lock); //| .. method:: I2C.unlock() //| //| Releases the I2C lock. //| -STATIC mp_obj_t nativeio_i2c_obj_unlock(mp_obj_t self_in) { - common_hal_nativeio_i2c_unlock(MP_OBJ_TO_PTR(self_in)); +STATIC mp_obj_t busio_i2c_obj_unlock(mp_obj_t self_in) { + common_hal_busio_i2c_unlock(MP_OBJ_TO_PTR(self_in)); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_i2c_unlock_obj, nativeio_i2c_obj_unlock); +MP_DEFINE_CONST_FUN_OBJ_1(busio_i2c_unlock_obj, busio_i2c_obj_unlock); //| .. method:: I2C.readfrom_into(address, buffer, \*, start=0, end=len(buffer)) //| @@ -172,7 +172,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_i2c_unlock_obj, nativeio_i2c_obj_unlock); //| :param int start: Index to start writing at //| :param int end: Index to write up to but not include //| -STATIC mp_obj_t nativeio_i2c_readfrom_into(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +STATIC mp_obj_t busio_i2c_readfrom_into(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_address, ARG_buffer, ARG_start, ARG_end }; static const mp_arg_t allowed_args[] = { { MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} }, @@ -180,7 +180,7 @@ STATIC mp_obj_t nativeio_i2c_readfrom_into(size_t n_args, const mp_obj_t *pos_ar { MP_QSTR_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, { MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} }, }; - nativeio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + busio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); check_lock(self); mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); @@ -198,14 +198,14 @@ STATIC mp_obj_t nativeio_i2c_readfrom_into(size_t n_args, const mp_obj_t *pos_ar } else if (len > bufinfo.len) { len = bufinfo.len; } - uint8_t status = common_hal_nativeio_i2c_read(self, args[ARG_address].u_int, ((uint8_t*)bufinfo.buf) + start, len); + uint8_t status = common_hal_busio_i2c_read(self, args[ARG_address].u_int, ((uint8_t*)bufinfo.buf) + start, len); if (status != 0) { mp_raise_OSError(status); } return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_i2c_readfrom_into_obj, 3, nativeio_i2c_readfrom_into); +MP_DEFINE_CONST_FUN_OBJ_KW(busio_i2c_readfrom_into_obj, 3, busio_i2c_readfrom_into); //| .. method:: I2C.writeto(address, buffer, \*, start=0, end=len(buffer), stop=True) //| @@ -223,7 +223,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_i2c_readfrom_into_obj, 3, nativeio_i2c_readf //| :param bool stop: If true, output an I2C stop condition after the //| buffer is written //| -STATIC mp_obj_t nativeio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +STATIC mp_obj_t busio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_address, ARG_buffer, ARG_start, ARG_end, ARG_stop }; static const mp_arg_t allowed_args[] = { { MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} }, @@ -232,7 +232,7 @@ STATIC mp_obj_t nativeio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, mp { MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} }, { MP_QSTR_stop, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} }, }; - nativeio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + busio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); check_lock(self); mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); @@ -254,33 +254,33 @@ STATIC mp_obj_t nativeio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, mp } // do the transfer - uint8_t status = common_hal_nativeio_i2c_write(self, args[ARG_address].u_int, + uint8_t status = common_hal_busio_i2c_write(self, args[ARG_address].u_int, ((uint8_t*) bufinfo.buf) + start, len, args[ARG_stop].u_bool); if (status != 0) { mp_raise_OSError(status); } return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_i2c_writeto_obj, 1, nativeio_i2c_writeto); +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(busio_i2c_writeto_obj, 1, busio_i2c_writeto); -STATIC const mp_rom_map_elem_t nativeio_i2c_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_i2c_deinit_obj) }, +STATIC const mp_rom_map_elem_t busio_i2c_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&busio_i2c_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, - { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_i2c___exit___obj) }, - { MP_ROM_QSTR(MP_QSTR_scan), MP_ROM_PTR(&nativeio_i2c_scan_obj) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&busio_i2c___exit___obj) }, + { MP_ROM_QSTR(MP_QSTR_scan), MP_ROM_PTR(&busio_i2c_scan_obj) }, - { MP_ROM_QSTR(MP_QSTR_try_lock), MP_ROM_PTR(&nativeio_i2c_try_lock_obj) }, - { MP_ROM_QSTR(MP_QSTR_unlock), MP_ROM_PTR(&nativeio_i2c_unlock_obj) }, + { MP_ROM_QSTR(MP_QSTR_try_lock), MP_ROM_PTR(&busio_i2c_try_lock_obj) }, + { MP_ROM_QSTR(MP_QSTR_unlock), MP_ROM_PTR(&busio_i2c_unlock_obj) }, - { MP_ROM_QSTR(MP_QSTR_readfrom_into), MP_ROM_PTR(&nativeio_i2c_readfrom_into_obj) }, - { MP_ROM_QSTR(MP_QSTR_writeto), MP_ROM_PTR(&nativeio_i2c_writeto_obj) }, + { MP_ROM_QSTR(MP_QSTR_readfrom_into), MP_ROM_PTR(&busio_i2c_readfrom_into_obj) }, + { MP_ROM_QSTR(MP_QSTR_writeto), MP_ROM_PTR(&busio_i2c_writeto_obj) }, }; -STATIC MP_DEFINE_CONST_DICT(nativeio_i2c_locals_dict, nativeio_i2c_locals_dict_table); +STATIC MP_DEFINE_CONST_DICT(busio_i2c_locals_dict, busio_i2c_locals_dict_table); -const mp_obj_type_t nativeio_i2c_type = { +const mp_obj_type_t busio_i2c_type = { { &mp_type_type }, .name = MP_QSTR_I2C, - .make_new = nativeio_i2c_make_new, - .locals_dict = (mp_obj_dict_t*)&nativeio_i2c_locals_dict, + .make_new = busio_i2c_make_new, + .locals_dict = (mp_obj_dict_t*)&busio_i2c_locals_dict, }; diff --git a/shared-bindings/nativeio/I2C.h b/shared-bindings/busio/I2C.h similarity index 73% rename from shared-bindings/nativeio/I2C.h rename to shared-bindings/busio/I2C.h index 5f3c89a77b..0d260a309b 100644 --- a/shared-bindings/nativeio/I2C.h +++ b/shared-bindings/busio/I2C.h @@ -31,40 +31,40 @@ // This file includes externs for all functions a port should implement to // support the machine module. -#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_I2C_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_I2C_H__ +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_I2C_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_I2C_H__ #include "py/obj.h" #include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" +#include "common-hal/busio/I2C.h" // Type object used in Python. Should be shared between ports. -extern const mp_obj_type_t nativeio_i2c_type; +extern const mp_obj_type_t busio_i2c_type; // Initializes the hardware peripheral. -extern void common_hal_nativeio_i2c_construct(nativeio_i2c_obj_t *self, +extern void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, const mcu_pin_obj_t * scl, const mcu_pin_obj_t * sda, uint32_t frequency); -extern void common_hal_nativeio_i2c_deinit(nativeio_i2c_obj_t *self); +extern void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self); -extern bool common_hal_nativeio_i2c_try_lock(nativeio_i2c_obj_t *self); -extern bool common_hal_nativeio_i2c_has_lock(nativeio_i2c_obj_t *self); -extern void common_hal_nativeio_i2c_unlock(nativeio_i2c_obj_t *self); +extern bool common_hal_busio_i2c_try_lock(busio_i2c_obj_t *self); +extern bool common_hal_busio_i2c_has_lock(busio_i2c_obj_t *self); +extern void common_hal_busio_i2c_unlock(busio_i2c_obj_t *self); // Probe the bus to see if a device acknowledges the given address. -extern bool common_hal_nativeio_i2c_probe(nativeio_i2c_obj_t *self, uint8_t addr); +extern bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr); // Write to the device and return 0 on success or an appropriate error code from mperrno.h -extern uint8_t common_hal_nativeio_i2c_write(nativeio_i2c_obj_t *self, uint16_t address, +extern uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t address, const uint8_t * data, size_t len, bool stop); // Reads memory of the i2c device picking up where it left off and return 0 on // success or an appropriate error code from mperrno.h -extern uint8_t common_hal_nativeio_i2c_read(nativeio_i2c_obj_t *self, uint16_t address, +extern uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t address, uint8_t * data, size_t len); -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_I2C_H__ +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_I2C_H__ diff --git a/shared-bindings/nativeio/OneWire.c b/shared-bindings/busio/OneWire.c similarity index 59% rename from shared-bindings/nativeio/OneWire.c rename to shared-bindings/busio/OneWire.c index f9fbbe63cc..52aed21351 100644 --- a/shared-bindings/nativeio/OneWire.c +++ b/shared-bindings/busio/OneWire.c @@ -31,14 +31,14 @@ #include "py/runtime.h" #include "py/runtime0.h" #include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/OneWire.h" +#include "shared-bindings/busio/OneWire.h" -//| .. currentmodule:: nativeio +//| .. currentmodule:: busio //| //| :class:`OneWire` -- Lowest-level of the Maxim OneWire protocol //| ================================================================= //| -//| :class:`~nativeio.OneWire` implements the timing-sensitive foundation of the Maxim +//| :class:`~busio.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 @@ -52,16 +52,16 @@ //| //| Read a short series of pulses:: //| -//| import nativeio +//| import busio //| import board //| -//| with nativeio.OneWire(board.D7) as onewire: +//| with busio.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) { +STATIC mp_obj_t busio_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); @@ -75,10 +75,10 @@ STATIC mp_obj_t nativeio_onewire_make_new(const mp_obj_type_t *type, size_t n_ar 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; + busio_onewire_obj_t *self = m_new_obj(busio_onewire_obj_t); + self->base.type = &busio_onewire_type; - common_hal_nativeio_onewire_construct(self, pin); + common_hal_busio_onewire_construct(self, pin); return MP_OBJ_FROM_PTR(self); } @@ -86,12 +86,12 @@ STATIC mp_obj_t nativeio_onewire_make_new(const mp_obj_type_t *type, size_t n_ar //| //| 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); +STATIC mp_obj_t busio_onewire_deinit(mp_obj_t self_in) { + busio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_busio_onewire_deinit(self); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_onewire_deinit_obj, nativeio_onewire_deinit); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(busio_onewire_deinit_obj, busio_onewire_deinit); //| .. method:: __enter__() //| @@ -103,12 +103,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_onewire_deinit_obj, nativeio_onewire_d //| //| 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) { +STATIC mp_obj_t busio_onewire_obj___exit__(size_t n_args, const mp_obj_t *args) { (void)n_args; - common_hal_nativeio_onewire_deinit(args[0]); + common_hal_busio_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__); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busio_onewire___exit___obj, 4, 4, busio_onewire_obj___exit__); //| .. method:: reset() //| @@ -117,12 +117,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_onewire___exit___obj, 4, 4, //| :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); +STATIC mp_obj_t busio_onewire_obj_reset(mp_obj_t self_in) { + busio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); - return mp_obj_new_bool(common_hal_nativeio_onewire_reset(self)); + return mp_obj_new_bool(common_hal_busio_onewire_reset(self)); } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_onewire_reset_obj, nativeio_onewire_obj_reset); +MP_DEFINE_CONST_FUN_OBJ_1(busio_onewire_reset_obj, busio_onewire_obj_reset); //| .. method:: read_bit() //| @@ -131,39 +131,39 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_onewire_reset_obj, nativeio_onewire_obj_reset //| :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); +STATIC mp_obj_t busio_onewire_obj_read_bit(mp_obj_t self_in) { + busio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); - return mp_obj_new_bool(common_hal_nativeio_onewire_read_bit(self)); + return mp_obj_new_bool(common_hal_busio_onewire_read_bit(self)); } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_onewire_read_bit_obj, nativeio_onewire_obj_read_bit); +MP_DEFINE_CONST_FUN_OBJ_1(busio_onewire_read_bit_obj, busio_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); +STATIC mp_obj_t busio_onewire_obj_write_bit(mp_obj_t self_in, mp_obj_t bool_obj) { + busio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_onewire_write_bit(self, mp_obj_is_true(bool_obj)); + common_hal_busio_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); +MP_DEFINE_CONST_FUN_OBJ_2(busio_onewire_write_bit_obj, busio_onewire_obj_write_bit); -STATIC const mp_rom_map_elem_t nativeio_onewire_locals_dict_table[] = { +STATIC const mp_rom_map_elem_t busio_onewire_locals_dict_table[] = { // Methods - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_onewire_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&busio_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) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&busio_onewire___exit___obj) }, + { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&busio_onewire_reset_obj) }, + { MP_ROM_QSTR(MP_QSTR_read_bit), MP_ROM_PTR(&busio_onewire_read_bit_obj) }, + { MP_ROM_QSTR(MP_QSTR_write_bit), MP_ROM_PTR(&busio_onewire_write_bit_obj) }, }; -STATIC MP_DEFINE_CONST_DICT(nativeio_onewire_locals_dict, nativeio_onewire_locals_dict_table); +STATIC MP_DEFINE_CONST_DICT(busio_onewire_locals_dict, busio_onewire_locals_dict_table); -const mp_obj_type_t nativeio_onewire_type = { +const mp_obj_type_t busio_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, + .make_new = busio_onewire_make_new, + .locals_dict = (mp_obj_dict_t*)&busio_onewire_locals_dict, }; diff --git a/shared-bindings/nativeio/OneWire.h b/shared-bindings/busio/OneWire.h similarity index 65% rename from shared-bindings/nativeio/OneWire.h rename to shared-bindings/busio/OneWire.h index 9d5c2c9324..df5cecb7c9 100644 --- a/shared-bindings/nativeio/OneWire.h +++ b/shared-bindings/busio/OneWire.h @@ -24,19 +24,19 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ONEWIRE_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ONEWIRE_H__ +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_ONEWIRE_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_ONEWIRE_H__ #include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" +#include "common-hal/busio/OneWire.h" -extern const mp_obj_type_t nativeio_onewire_type; +extern const mp_obj_type_t busio_onewire_type; -extern void common_hal_nativeio_onewire_construct(nativeio_onewire_obj_t* self, +extern void common_hal_busio_onewire_construct(busio_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); +extern void common_hal_busio_onewire_deinit(busio_onewire_obj_t* self); +extern bool common_hal_busio_onewire_reset(busio_onewire_obj_t* self); +extern bool common_hal_busio_onewire_read_bit(busio_onewire_obj_t* self); +extern void common_hal_busio_onewire_write_bit(busio_onewire_obj_t* self, bool bit); -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ONEWIRE_H__ +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_ONEWIRE_H__ diff --git a/shared-bindings/nativeio/SPI.c b/shared-bindings/busio/SPI.c similarity index 73% rename from shared-bindings/nativeio/SPI.c rename to shared-bindings/busio/SPI.c index 8d545a127b..d4d9ec345c 100644 --- a/shared-bindings/nativeio/SPI.c +++ b/shared-bindings/busio/SPI.c @@ -25,25 +25,25 @@ */ // This file contains all of the Python API definitions for the -// nativeio.SPI class. +// busio.SPI class. #include #include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/SPI.h" +#include "shared-bindings/busio/SPI.h" #include "lib/utils/context_manager_helpers.h" #include "py/mperrno.h" #include "py/nlr.h" #include "py/runtime.h" -//| .. currentmodule:: nativeio +//| .. currentmodule:: busio //| //| :class:`SPI` -- a 3-4 wire serial protocol //| ----------------------------------------------- //| //| SPI is a serial protocol that has exclusive pins for data in and out of the -//| master. It is typically faster than :py:class:`~nativeio.I2C` because a +//| master. It is typically faster than :py:class:`~busio.I2C` because a //| separate pin is used to control the active slave rather than a transitted //| address. This class only manages three of the four SPI lines: `!clock`, //| `!MOSI`, `!MISO`. Its up to the client to manage the appropriate slave @@ -68,10 +68,10 @@ //| // TODO(tannewt): Support LSB SPI. -STATIC mp_obj_t nativeio_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { +STATIC mp_obj_t busio_spi_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, 0, MP_OBJ_FUN_ARGS_MAX, true); - nativeio_spi_obj_t *self = m_new_obj(nativeio_spi_obj_t); - self->base.type = &nativeio_spi_type; + busio_spi_obj_t *self = m_new_obj(busio_spi_obj_t); + self->base.type = &busio_spi_type; mp_map_t kw_args; mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); enum { ARG_clock, ARG_MOSI, ARG_MISO }; @@ -91,7 +91,7 @@ STATIC mp_obj_t nativeio_spi_make_new(const mp_obj_type_t *type, size_t n_args, assert_pin_free(mosi); const mcu_pin_obj_t* miso = MP_OBJ_TO_PTR(args[ARG_MISO].u_obj); assert_pin_free(miso); - common_hal_nativeio_spi_construct(self, clock, mosi, miso); + common_hal_busio_spi_construct(self, clock, mosi, miso); return (mp_obj_t)self; } @@ -99,12 +99,12 @@ STATIC mp_obj_t nativeio_spi_make_new(const mp_obj_type_t *type, size_t n_args, //| //| Turn off the SPI bus. //| -STATIC mp_obj_t nativeio_spi_obj_deinit(mp_obj_t self_in) { - nativeio_spi_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_spi_deinit(self); +STATIC mp_obj_t busio_spi_obj_deinit(mp_obj_t self_in) { + busio_spi_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_busio_spi_deinit(self); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_spi_deinit_obj, nativeio_spi_obj_deinit); +MP_DEFINE_CONST_FUN_OBJ_1(busio_spi_deinit_obj, busio_spi_obj_deinit); //| .. method:: SPI.__enter__() //| @@ -116,15 +116,15 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_spi_deinit_obj, nativeio_spi_obj_deinit); //| //| Automatically deinitializes the hardware when exiting a context. //| -STATIC mp_obj_t nativeio_spi_obj___exit__(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t busio_spi_obj___exit__(size_t n_args, const mp_obj_t *args) { (void)n_args; - common_hal_nativeio_spi_deinit(args[0]); + common_hal_busio_spi_deinit(args[0]); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_spi_obj___exit___obj, 4, 4, nativeio_spi_obj___exit__); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busio_spi_obj___exit___obj, 4, 4, busio_spi_obj___exit__); -static void check_lock(nativeio_spi_obj_t *self) { - if (!common_hal_nativeio_spi_has_lock(self)) { +static void check_lock(busio_spi_obj_t *self) { + if (!common_hal_busio_spi_has_lock(self)) { mp_raise_RuntimeError("Function requires lock"); } } @@ -139,7 +139,7 @@ static void check_lock(nativeio_spi_obj_t *self) { //| or second (1). Rising or falling depends on clock polarity. //| :param int bits: the number of bits per word //| -STATIC mp_obj_t nativeio_spi_configure(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +STATIC mp_obj_t busio_spi_configure(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits }; static const mp_arg_t allowed_args[] = { { MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 100000} }, @@ -147,7 +147,7 @@ STATIC mp_obj_t nativeio_spi_configure(size_t n_args, const mp_obj_t *pos_args, { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} }, }; - nativeio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + busio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); check_lock(self); mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); @@ -165,13 +165,13 @@ STATIC mp_obj_t nativeio_spi_configure(size_t n_args, const mp_obj_t *pos_args, mp_raise_ValueError("Invalid number of bits"); } - if (!common_hal_nativeio_spi_configure(self, args[ARG_baudrate].u_int, + if (!common_hal_busio_spi_configure(self, args[ARG_baudrate].u_int, polarity, phase, bits)) { mp_raise_OSError(MP_EIO); } return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_spi_configure_obj, 1, nativeio_spi_configure); +MP_DEFINE_CONST_FUN_OBJ_KW(busio_spi_configure_obj, 1, busio_spi_configure); //| .. method:: SPI.try_lock() //| @@ -180,20 +180,20 @@ MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_spi_configure_obj, 1, nativeio_spi_configure //| :return: True when lock has been grabbed //| :rtype: bool //| -STATIC mp_obj_t nativeio_spi_obj_try_lock(mp_obj_t self_in) { - return mp_obj_new_bool(common_hal_nativeio_spi_try_lock(MP_OBJ_TO_PTR(self_in))); +STATIC mp_obj_t busio_spi_obj_try_lock(mp_obj_t self_in) { + return mp_obj_new_bool(common_hal_busio_spi_try_lock(MP_OBJ_TO_PTR(self_in))); } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_spi_try_lock_obj, nativeio_spi_obj_try_lock); +MP_DEFINE_CONST_FUN_OBJ_1(busio_spi_try_lock_obj, busio_spi_obj_try_lock); //| .. method:: SPI.unlock() //| //| Releases the SPI lock. //| -STATIC mp_obj_t nativeio_spi_obj_unlock(mp_obj_t self_in) { - common_hal_nativeio_spi_unlock(MP_OBJ_TO_PTR(self_in)); +STATIC mp_obj_t busio_spi_obj_unlock(mp_obj_t self_in) { + common_hal_busio_spi_unlock(MP_OBJ_TO_PTR(self_in)); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_spi_unlock_obj, nativeio_spi_obj_unlock); +MP_DEFINE_CONST_FUN_OBJ_1(busio_spi_unlock_obj, busio_spi_obj_unlock); //| .. method:: SPI.write(buffer, \*, start=0, end=len(buffer)) //| @@ -203,14 +203,14 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_spi_unlock_obj, nativeio_spi_obj_unlock); //| :param int start: Index to start writing from //| :param int end: Index to read up to but not include //| -STATIC mp_obj_t nativeio_spi_write(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +STATIC mp_obj_t busio_spi_write(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_buffer, ARG_start, ARG_end }; static const mp_arg_t allowed_args[] = { { MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, { MP_QSTR_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, { MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} }, }; - nativeio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + busio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); check_lock(self); mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); @@ -229,13 +229,13 @@ STATIC mp_obj_t nativeio_spi_write(size_t n_args, const mp_obj_t *pos_args, mp_m len = bufinfo.len; } - bool ok = common_hal_nativeio_spi_write(self, ((uint8_t*)bufinfo.buf) + start, len); + bool ok = common_hal_busio_spi_write(self, ((uint8_t*)bufinfo.buf) + start, len); if (!ok) { mp_raise_OSError(MP_EIO); } return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_spi_write_obj, 2, nativeio_spi_write); +MP_DEFINE_CONST_FUN_OBJ_KW(busio_spi_write_obj, 2, busio_spi_write); //| .. method:: SPI.readinto(buffer, \*, start=0, end=len(buffer), write_value=0) @@ -247,7 +247,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_spi_write_obj, 2, nativeio_spi_write); //| :param int end: Index to write up to but not include //| :param int write_value: Value to write reading. (Usually ignored.) //| -STATIC mp_obj_t nativeio_spi_readinto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +STATIC mp_obj_t busio_spi_readinto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_buffer, ARG_start, ARG_end, ARG_write_value }; static const mp_arg_t allowed_args[] = { { MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, @@ -255,7 +255,7 @@ STATIC mp_obj_t nativeio_spi_readinto(size_t n_args, const mp_obj_t *pos_args, m { MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} }, { MP_QSTR_write_value,MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, }; - nativeio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + busio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); check_lock(self); mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); @@ -274,31 +274,31 @@ STATIC mp_obj_t nativeio_spi_readinto(size_t n_args, const mp_obj_t *pos_args, m len = bufinfo.len; } - bool ok = common_hal_nativeio_spi_read(self, ((uint8_t*)bufinfo.buf) + start, len, args[ARG_write_value].u_int); + bool ok = common_hal_busio_spi_read(self, ((uint8_t*)bufinfo.buf) + start, len, args[ARG_write_value].u_int); if (!ok) { mp_raise_OSError(MP_EIO); } return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_spi_readinto_obj, 2, nativeio_spi_readinto); +MP_DEFINE_CONST_FUN_OBJ_KW(busio_spi_readinto_obj, 2, busio_spi_readinto); -STATIC const mp_rom_map_elem_t nativeio_spi_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_spi_deinit_obj) }, +STATIC const mp_rom_map_elem_t busio_spi_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&busio_spi_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, - { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_spi_obj___exit___obj) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&busio_spi_obj___exit___obj) }, - { MP_ROM_QSTR(MP_QSTR_configure), MP_ROM_PTR(&nativeio_spi_configure_obj) }, - { MP_ROM_QSTR(MP_QSTR_try_lock), MP_ROM_PTR(&nativeio_spi_try_lock_obj) }, - { MP_ROM_QSTR(MP_QSTR_unlock), MP_ROM_PTR(&nativeio_spi_unlock_obj) }, + { MP_ROM_QSTR(MP_QSTR_configure), MP_ROM_PTR(&busio_spi_configure_obj) }, + { MP_ROM_QSTR(MP_QSTR_try_lock), MP_ROM_PTR(&busio_spi_try_lock_obj) }, + { MP_ROM_QSTR(MP_QSTR_unlock), MP_ROM_PTR(&busio_spi_unlock_obj) }, - { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&nativeio_spi_readinto_obj) }, - { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&nativeio_spi_write_obj) }, + { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&busio_spi_readinto_obj) }, + { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&busio_spi_write_obj) }, }; -STATIC MP_DEFINE_CONST_DICT(nativeio_spi_locals_dict, nativeio_spi_locals_dict_table); +STATIC MP_DEFINE_CONST_DICT(busio_spi_locals_dict, busio_spi_locals_dict_table); -const mp_obj_type_t nativeio_spi_type = { +const mp_obj_type_t busio_spi_type = { { &mp_type_type }, .name = MP_QSTR_SPI, - .make_new = nativeio_spi_make_new, - .locals_dict = (mp_obj_dict_t*)&nativeio_spi_locals_dict, + .make_new = busio_spi_make_new, + .locals_dict = (mp_obj_dict_t*)&busio_spi_locals_dict, }; diff --git a/shared-bindings/nativeio/SPI.h b/shared-bindings/busio/SPI.h similarity index 61% rename from shared-bindings/nativeio/SPI.h rename to shared-bindings/busio/SPI.h index b997ddf671..e3deb0e72b 100644 --- a/shared-bindings/nativeio/SPI.h +++ b/shared-bindings/busio/SPI.h @@ -24,34 +24,34 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_SPI_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_SPI_H__ +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_SPI_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_SPI_H__ #include "py/obj.h" #include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" +#include "common-hal/busio/SPI.h" // Type object used in Python. Should be shared between ports. -extern const mp_obj_type_t nativeio_spi_type; +extern const mp_obj_type_t busio_spi_type; // Construct an underlying SPI object. -extern void common_hal_nativeio_spi_construct(nativeio_spi_obj_t *self, +extern void common_hal_busio_spi_construct(busio_spi_obj_t *self, const mcu_pin_obj_t * clock, const mcu_pin_obj_t * mosi, const mcu_pin_obj_t * miso); -extern void common_hal_nativeio_spi_deinit(nativeio_spi_obj_t *self); +extern void common_hal_busio_spi_deinit(busio_spi_obj_t *self); -extern bool common_hal_nativeio_spi_configure(nativeio_spi_obj_t *self, uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits); +extern bool common_hal_busio_spi_configure(busio_spi_obj_t *self, uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits); -extern bool common_hal_nativeio_spi_try_lock(nativeio_spi_obj_t *self); -extern bool common_hal_nativeio_spi_has_lock(nativeio_spi_obj_t *self); -extern void common_hal_nativeio_spi_unlock(nativeio_spi_obj_t *self); +extern bool common_hal_busio_spi_try_lock(busio_spi_obj_t *self); +extern bool common_hal_busio_spi_has_lock(busio_spi_obj_t *self); +extern void common_hal_busio_spi_unlock(busio_spi_obj_t *self); // Writes out the given data. -extern bool common_hal_nativeio_spi_write(nativeio_spi_obj_t *self, const uint8_t *data, size_t len); +extern bool common_hal_busio_spi_write(busio_spi_obj_t *self, const uint8_t *data, size_t len); // Reads in len bytes while outputting zeroes. -extern bool common_hal_nativeio_spi_read(nativeio_spi_obj_t *self, uint8_t *data, size_t len, uint8_t write_value); +extern bool common_hal_busio_spi_read(busio_spi_obj_t *self, uint8_t *data, size_t len, uint8_t write_value); -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_SPI_H__ +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_SPI_H__ diff --git a/shared-bindings/nativeio/UART.c b/shared-bindings/busio/UART.c similarity index 66% rename from shared-bindings/nativeio/UART.c rename to shared-bindings/busio/UART.c index c561305938..11ee91355e 100644 --- a/shared-bindings/nativeio/UART.c +++ b/shared-bindings/busio/UART.c @@ -26,7 +26,7 @@ #include -#include "shared-bindings/nativeio/UART.h" +#include "shared-bindings/busio/UART.h" #include "lib/utils/context_manager_helpers.h" @@ -36,7 +36,7 @@ #include "shared-bindings/microcontroller/Pin.h" -//| .. currentmodule:: nativeio +//| .. currentmodule:: busio //| //| :class:`UART` -- a bidirectional serial protocol //| ================================================= @@ -58,14 +58,14 @@ //| typedef struct { mp_obj_base_t base; -} nativeio_uart_parity_obj_t; -extern const nativeio_uart_parity_obj_t nativeio_uart_parity_even_obj; -extern const nativeio_uart_parity_obj_t nativeio_uart_parity_odd_obj; +} busio_uart_parity_obj_t; +extern const busio_uart_parity_obj_t busio_uart_parity_even_obj; +extern const busio_uart_parity_obj_t busio_uart_parity_odd_obj; -STATIC mp_obj_t nativeio_uart_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { +STATIC mp_obj_t busio_uart_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, 0, MP_OBJ_FUN_ARGS_MAX, true); - nativeio_uart_obj_t *self = m_new_obj(nativeio_uart_obj_t); - self->base.type = &nativeio_uart_type; + busio_uart_obj_t *self = m_new_obj(busio_uart_obj_t); + self->base.type = &busio_uart_type; mp_map_t kw_args; mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); enum { ARG_tx, ARG_rx, ARG_baudrate, ARG_bits, ARG_parity, ARG_stop, ARG_timeout, ARG_receiver_buffer_size}; @@ -96,9 +96,9 @@ STATIC mp_obj_t nativeio_uart_make_new(const mp_obj_type_t *type, size_t n_args, } uart_parity_t parity = PARITY_NONE; - if (args[ARG_parity].u_obj == &nativeio_uart_parity_even_obj) { + if (args[ARG_parity].u_obj == &busio_uart_parity_even_obj) { parity = PARITY_EVEN; - } else if (args[ARG_parity].u_obj == &nativeio_uart_parity_odd_obj) { + } else if (args[ARG_parity].u_obj == &busio_uart_parity_odd_obj) { parity = PARITY_ODD; } @@ -107,7 +107,7 @@ STATIC mp_obj_t nativeio_uart_make_new(const mp_obj_type_t *type, size_t n_args, mp_raise_ValueError("stop must be 1 or 2"); } - common_hal_nativeio_uart_construct(self, tx, rx, + common_hal_busio_uart_construct(self, tx, rx, args[ARG_baudrate].u_int, bits, parity, stop, args[ARG_timeout].u_int, args[ARG_receiver_buffer_size].u_int); return (mp_obj_t)self; @@ -117,12 +117,12 @@ STATIC mp_obj_t nativeio_uart_make_new(const mp_obj_type_t *type, size_t n_args, //| //| Deinitialises the UART and releases any hardware resources for reuse. //| -STATIC mp_obj_t nativeio_uart_obj_deinit(mp_obj_t self_in) { - nativeio_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_uart_deinit(self); +STATIC mp_obj_t busio_uart_obj_deinit(mp_obj_t self_in) { + busio_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_busio_uart_deinit(self); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_uart_deinit_obj, nativeio_uart_obj_deinit); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(busio_uart_deinit_obj, busio_uart_obj_deinit); //| .. method:: __enter__() //| @@ -134,12 +134,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_uart_deinit_obj, nativeio_uart_obj_dei //| //| Automatically deinitializes the hardware when exiting a context. //| -STATIC mp_obj_t nativeio_uart_obj___exit__(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t busio_uart_obj___exit__(size_t n_args, const mp_obj_t *args) { (void)n_args; - common_hal_nativeio_uart_deinit(args[0]); + common_hal_busio_uart_deinit(args[0]); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_uart___exit___obj, 4, 4, nativeio_uart_obj___exit__); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busio_uart___exit___obj, 4, 4, busio_uart_obj___exit__); // These are standard stream methods. Code is in py/stream.c. // @@ -175,8 +175,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_uart___exit___obj, 4, 4, nat //| // These three methods are used by the shared stream methods. -STATIC mp_uint_t nativeio_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) { - nativeio_uart_obj_t *self = self_in; +STATIC mp_uint_t busio_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) { + busio_uart_obj_t *self = self_in; byte *buf = buf_in; // make sure we want at least 1 char @@ -184,26 +184,26 @@ STATIC mp_uint_t nativeio_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t si return 0; } - return common_hal_nativeio_uart_read(self, buf, size, errcode); + return common_hal_busio_uart_read(self, buf, size, errcode); } -STATIC mp_uint_t nativeio_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) { - nativeio_uart_obj_t *self = self_in; +STATIC mp_uint_t busio_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) { + busio_uart_obj_t *self = self_in; const byte *buf = buf_in; - return common_hal_nativeio_uart_write(self, buf, size, errcode); + return common_hal_busio_uart_write(self, buf, size, errcode); } -STATIC mp_uint_t nativeio_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) { - nativeio_uart_obj_t *self = self_in; +STATIC mp_uint_t busio_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) { + busio_uart_obj_t *self = self_in; mp_uint_t ret; if (request == MP_IOCTL_POLL) { mp_uint_t flags = arg; ret = 0; - if ((flags & MP_IOCTL_POLL_RD) && common_hal_nativeio_uart_rx_characters_available(self) > 0) { + if ((flags & MP_IOCTL_POLL_RD) && common_hal_busio_uart_rx_characters_available(self) > 0) { ret |= MP_IOCTL_POLL_RD; } - if ((flags & MP_IOCTL_POLL_WR) && common_hal_nativeio_uart_ready_to_tx(self)) { + if ((flags & MP_IOCTL_POLL_WR) && common_hal_busio_uart_ready_to_tx(self)) { ret |= MP_IOCTL_POLL_WR; } } else { @@ -213,7 +213,7 @@ STATIC mp_uint_t nativeio_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uin return ret; } -//| .. class:: nativeio.UART.Parity +//| .. class:: busio.UART.Parity //| //| Enum-like class to define the parity used to verify correct data transfer. //| @@ -225,41 +225,41 @@ STATIC mp_uint_t nativeio_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uin //| //| Total number of ones should be even. //| -const mp_obj_type_t nativeio_uart_parity_type; +const mp_obj_type_t busio_uart_parity_type; -const nativeio_uart_parity_obj_t nativeio_uart_parity_odd_obj = { - { &nativeio_uart_parity_type }, +const busio_uart_parity_obj_t busio_uart_parity_odd_obj = { + { &busio_uart_parity_type }, }; -const nativeio_uart_parity_obj_t nativeio_uart_parity_even_obj = { - { &nativeio_uart_parity_type }, +const busio_uart_parity_obj_t busio_uart_parity_even_obj = { + { &busio_uart_parity_type }, }; -STATIC const mp_rom_map_elem_t nativeio_uart_parity_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_ODD), MP_ROM_PTR(&nativeio_uart_parity_odd_obj) }, - { MP_ROM_QSTR(MP_QSTR_EVEN), MP_ROM_PTR(&nativeio_uart_parity_even_obj) }, +STATIC const mp_rom_map_elem_t busio_uart_parity_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_ODD), MP_ROM_PTR(&busio_uart_parity_odd_obj) }, + { MP_ROM_QSTR(MP_QSTR_EVEN), MP_ROM_PTR(&busio_uart_parity_even_obj) }, }; -STATIC MP_DEFINE_CONST_DICT(nativeio_uart_parity_locals_dict, nativeio_uart_parity_locals_dict_table); +STATIC MP_DEFINE_CONST_DICT(busio_uart_parity_locals_dict, busio_uart_parity_locals_dict_table); -STATIC void nativeio_uart_parity_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { +STATIC void busio_uart_parity_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { qstr parity = MP_QSTR_ODD; - if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&nativeio_uart_parity_even_obj)) { + if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&busio_uart_parity_even_obj)) { parity = MP_QSTR_EVEN; } - mp_printf(print, "%q.%q.%q.%q", MP_QSTR_nativeio, MP_QSTR_UART, MP_QSTR_Parity, parity); + mp_printf(print, "%q.%q.%q.%q", MP_QSTR_busio, MP_QSTR_UART, MP_QSTR_Parity, parity); } -const mp_obj_type_t nativeio_uart_parity_type = { +const mp_obj_type_t busio_uart_parity_type = { { &mp_type_type }, .name = MP_QSTR_Parity, - .print = nativeio_uart_parity_print, - .locals_dict = (mp_obj_t)&nativeio_uart_parity_locals_dict, + .print = busio_uart_parity_print, + .locals_dict = (mp_obj_t)&busio_uart_parity_locals_dict, }; -STATIC const mp_rom_map_elem_t nativeio_uart_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_uart_deinit_obj) }, +STATIC const mp_rom_map_elem_t busio_uart_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&busio_uart_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, - { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_uart___exit___obj) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&busio_uart___exit___obj) }, // Standard stream methods. { MP_OBJ_NEW_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) }, @@ -268,23 +268,23 @@ STATIC const mp_rom_map_elem_t nativeio_uart_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) }, // Nested Enum-like Classes. - { MP_ROM_QSTR(MP_QSTR_Parity), MP_ROM_PTR(&nativeio_uart_parity_type) }, + { MP_ROM_QSTR(MP_QSTR_Parity), MP_ROM_PTR(&busio_uart_parity_type) }, }; -STATIC MP_DEFINE_CONST_DICT(nativeio_uart_locals_dict, nativeio_uart_locals_dict_table); +STATIC MP_DEFINE_CONST_DICT(busio_uart_locals_dict, busio_uart_locals_dict_table); STATIC const mp_stream_p_t uart_stream_p = { - .read = nativeio_uart_read, - .write = nativeio_uart_write, - .ioctl = nativeio_uart_ioctl, + .read = busio_uart_read, + .write = busio_uart_write, + .ioctl = busio_uart_ioctl, .is_text = false, }; -const mp_obj_type_t nativeio_uart_type = { +const mp_obj_type_t busio_uart_type = { { &mp_type_type }, .name = MP_QSTR_UART, - .make_new = nativeio_uart_make_new, + .make_new = busio_uart_make_new, .getiter = mp_identity, .iternext = mp_stream_unbuffered_iter, .protocol = &uart_stream_p, - .locals_dict = (mp_obj_dict_t*)&nativeio_uart_locals_dict, + .locals_dict = (mp_obj_dict_t*)&busio_uart_locals_dict, }; diff --git a/shared-bindings/nativeio/UART.h b/shared-bindings/busio/UART.h similarity index 71% rename from shared-bindings/nativeio/UART.h rename to shared-bindings/busio/UART.h index bafc5b730e..e0a8a0b09c 100644 --- a/shared-bindings/nativeio/UART.h +++ b/shared-bindings/busio/UART.h @@ -24,13 +24,13 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_UART_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_UART_H__ +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_UART_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_UART_H__ #include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" +#include "common-hal/busio/UART.h" -extern const mp_obj_type_t nativeio_uart_type; +extern const mp_obj_type_t busio_uart_type; typedef enum { PARITY_NONE, @@ -39,22 +39,22 @@ typedef enum { } uart_parity_t; // Construct an underlying UART object. -extern void common_hal_nativeio_uart_construct(nativeio_uart_obj_t *self, +extern void common_hal_busio_uart_construct(busio_uart_obj_t *self, const mcu_pin_obj_t * tx, const mcu_pin_obj_t * rx, uint32_t baudrate, uint8_t bits, uart_parity_t parity, uint8_t stop, uint32_t timeout, uint8_t receiver_buffer_size); -extern void common_hal_nativeio_uart_deinit(nativeio_uart_obj_t *self); +extern void common_hal_busio_uart_deinit(busio_uart_obj_t *self); // Read characters. len is in characters NOT bytes! -extern size_t common_hal_nativeio_uart_read(nativeio_uart_obj_t *self, +extern size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t len, int *errcode); // Write characters. len is in characters NOT bytes! -extern size_t common_hal_nativeio_uart_write(nativeio_uart_obj_t *self, +extern size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data, size_t len, int *errcode); -extern uint32_t common_hal_nativeio_uart_rx_characters_available(nativeio_uart_obj_t *self); -extern bool common_hal_nativeio_uart_ready_to_tx(nativeio_uart_obj_t *self); +extern uint32_t common_hal_busio_uart_rx_characters_available(busio_uart_obj_t *self); +extern bool common_hal_busio_uart_ready_to_tx(busio_uart_obj_t *self); -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_UART_H__ +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_UART_H__ diff --git a/shared-bindings/busio/__init__.c b/shared-bindings/busio/__init__.c new file mode 100644 index 0000000000..9e075752d1 --- /dev/null +++ b/shared-bindings/busio/__init__.c @@ -0,0 +1,98 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 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 + +#include "py/obj.h" +#include "py/runtime.h" + +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/busio/__init__.h" +#include "shared-bindings/busio/I2C.h" +#include "shared-bindings/busio/OneWire.h" +#include "shared-bindings/busio/SPI.h" +#include "shared-bindings/busio/UART.h" +#include "shared-bindings/busio/__init__.h" + +#include "py/runtime.h" + +//| :mod:`busio` --- Hardware accelerated behavior +//| ================================================= +//| +//| .. module:: busio +//| :synopsis: Hardware accelerated behavior +//| :platform: SAMD21 +//| +//| The `busio` module contains classes to support a variety of serial +//| protocols. +//| +//| 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 serial protocol on any general purpose pins. +//| +//| Libraries +//| +//| .. toctree:: +//| :maxdepth: 3 +//| +//| I2C +//| OneWire +//| SPI +//| UART +//| +//| All libraries change hardware state and should be deinitialized when they +//| are no longer needed. To do so, either call :py:meth:`!deinit` or use a +//| context manager. +//| +//| For example:: +//| +//| import busio +//| from board import * +//| +//| with busio.I2C(SCL, SDA) as i2c: +//| i2c.scan() +//| +//| This example will initialize the the device, run +//| :py:meth:`~busio.I2C.scan` and then :py:meth:`~busio.I2C.deinit` the +//| hardware. +//| + +STATIC const mp_rom_map_elem_t busio_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_busio) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&busio_i2c_type) }, + { MP_ROM_QSTR(MP_QSTR_OneWire), MP_ROM_PTR(&busio_onewire_type) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&busio_spi_type) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&busio_uart_type) }, +}; + +STATIC MP_DEFINE_CONST_DICT(busio_module_globals, busio_module_globals_table); + +const mp_obj_module_t busio_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&busio_module_globals, +}; diff --git a/atmel-samd/common-hal/nativeio/AnalogIn.h b/shared-bindings/busio/__init__.h similarity index 83% rename from atmel-samd/common-hal/nativeio/AnalogIn.h rename to shared-bindings/busio/__init__.h index d975f6cb42..214c2b1442 100644 --- a/atmel-samd/common-hal/nativeio/AnalogIn.h +++ b/shared-bindings/busio/__init__.h @@ -24,9 +24,11 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_ANALOGIN_H__ -#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_ANALOGIN_H__ +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO___INIT___H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO___INIT___H__ -void analogin_reset(void); +#include "py/obj.h" -#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_ANALOGIN_H__ +// Nothing now. + +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO___INIT___H__ diff --git a/shared-bindings/digitalio/DigitalInOut.c b/shared-bindings/digitalio/DigitalInOut.c new file mode 100644 index 0000000000..65ace83579 --- /dev/null +++ b/shared-bindings/digitalio/DigitalInOut.c @@ -0,0 +1,486 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013, 2014 Damien P. George + * + * 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 +#include + +#include "lib/utils/context_manager_helpers.h" + +#include "py/nlr.h" +#include "py/objtype.h" +#include "py/objproperty.h" +#include "py/runtime.h" +#include "py/mphal.h" + +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/digitalio/DigitalInOut.h" + +//| .. currentmodule:: digitalio +//| +//| :class:`DigitalInOut` -- digital input and output +//| ========================================================= +//| +//| A DigitalInOut is used to digitally control I/O pins. For analog control of +//| a pin, see the :py:class:`~digitalio.AnalogIn` and +//| :py:class:`~digitalio.AnalogOut` classes. +//| + +//| .. class:: DigitalInOut(pin) +//| +//| Create a new DigitalInOut object associated with the pin. Defaults to input +//| with no pull. Use :py:meth:`switch_to_input` and +//| :py:meth:`switch_to_output` to change the direction. +//| +//| :param ~microcontroller.Pin pin: The pin to control +//| +STATIC mp_obj_t digitalio_digitalinout_make_new(const mp_obj_type_t *type, + mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { + mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); + + digitalio_digitalinout_obj_t *self = m_new_obj(digitalio_digitalinout_obj_t); + self->base.type = &digitalio_digitalinout_type; + + assert_pin(args[0], false); + mcu_pin_obj_t *pin = MP_OBJ_TO_PTR(args[0]); + assert_pin_free(pin); + common_hal_digitalio_digitalinout_construct(self, pin); + + return (mp_obj_t)self; +} + +//| .. method:: deinit() +//| +//| Turn off the DigitalInOut and release the pin for other use. +//| +STATIC mp_obj_t digitalio_digitalinout_obj_deinit(mp_obj_t self_in) { + digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_digitalio_digitalinout_deinit(self); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_deinit_obj, digitalio_digitalinout_obj_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 digitalio_digitalinout_obj___exit__(size_t n_args, const mp_obj_t *args) { + (void)n_args; + common_hal_digitalio_digitalinout_deinit(args[0]); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(digitalio_digitalinout_obj___exit___obj, 4, 4, digitalio_digitalinout_obj___exit__); + +//| +//| .. method:: switch_to_output(value=False, drive_mode=DriveMode.PUSH_PULL) +//| +//| Switch to writing out digital values. +//| +//| :param bool value: default value to set upon switching +//| :param DriveMode drive_mode: drive mode for the output +//| +typedef struct { + mp_obj_base_t base; +} digitalio_digitalinout_drive_mode_obj_t; +extern const digitalio_digitalinout_drive_mode_obj_t digitalio_digitalinout_drive_mode_push_pull_obj; +extern const digitalio_digitalinout_drive_mode_obj_t digitalio_digitalinout_drive_mode_open_drain_obj; + +STATIC mp_obj_t digitalio_digitalinout_switch_to_output(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_value, ARG_drive_mode }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_value, MP_ARG_BOOL, {.u_bool = false} }, + { MP_QSTR_drive_mode, MP_ARG_OBJ, {.u_rom_obj = &digitalio_digitalinout_drive_mode_push_pull_obj} }, + }; + digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + enum digitalinout_drive_mode_t drive_mode = DRIVE_MODE_PUSH_PULL; + if (args[ARG_drive_mode].u_rom_obj == &digitalio_digitalinout_drive_mode_open_drain_obj) { + drive_mode = DRIVE_MODE_OPEN_DRAIN; + } + // do the transfer + common_hal_digitalio_digitalinout_switch_to_output(self, args[ARG_value].u_bool, drive_mode); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_KW(digitalio_digitalinout_switch_to_output_obj, 1, digitalio_digitalinout_switch_to_output); + +//| .. method:: switch_to_input(pull=None) +//| +//| Switch to read in digital values. +//| +//| :param Pull pull: pull configuration for the input +//| +//| Example usage:: +//| +//| import digitalio +//| import board +//| +//| with digitalio.DigitalInOut(board.SLIDE_SWITCH) as switch: +//| switch.switch_to_input(pull=digitalio.DigitalInOut.Pull.UP) +//| # Or, after switch_to_input +//| switch.pull = digitalio.DigitalInOut.Pull.UP +//| print(switch.value) +//| +typedef struct { + mp_obj_base_t base; +} digitalio_digitalinout_pull_obj_t; +extern const digitalio_digitalinout_pull_obj_t digitalio_digitalinout_pull_up_obj; +extern const digitalio_digitalinout_pull_obj_t digitalio_digitalinout_pull_down_obj; + +STATIC mp_obj_t digitalio_digitalinout_switch_to_input(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_pull }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_pull, MP_ARG_OBJ, {.u_rom_obj = mp_const_none} }, + }; + digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + enum digitalinout_pull_t pull = PULL_NONE; + if (args[ARG_pull].u_rom_obj == &digitalio_digitalinout_pull_up_obj) { + pull = PULL_UP; + }else if (args[ARG_pull].u_rom_obj == &digitalio_digitalinout_pull_down_obj) { + pull = PULL_DOWN; + } + // do the transfer + common_hal_digitalio_digitalinout_switch_to_input(self, pull); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_KW(digitalio_digitalinout_switch_to_input_obj, 1, digitalio_digitalinout_switch_to_input); + +//| .. attribute:: direction +//| +//| Get the direction of the pin. +//| +//| :raises AttributeError: when set. Use :py:meth:`switch_to_input` and :py:meth:`switch_to_output` to change the direction. +//| +typedef struct { + mp_obj_base_t base; +} digitalio_digitalinout_direction_obj_t; +extern const digitalio_digitalinout_direction_obj_t digitalio_digitalinout_direction_in_obj; +extern const digitalio_digitalinout_direction_obj_t digitalio_digitalinout_direction_out_obj; + +STATIC mp_obj_t digitalio_digitalinout_obj_get_direction(mp_obj_t self_in) { + digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); + enum digitalinout_direction_t direction = common_hal_digitalio_digitalinout_get_direction(self); + if (direction == DIRECTION_IN) { + return (mp_obj_t)&digitalio_digitalinout_direction_in_obj; + } + return (mp_obj_t)&digitalio_digitalinout_direction_out_obj; +} +MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_get_direction_obj, digitalio_digitalinout_obj_get_direction); + +const mp_obj_property_t digitalio_digitalinout_direction_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&digitalio_digitalinout_get_direction_obj, + (mp_obj_t)&mp_const_none_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +//| .. attribute:: value +//| +//| The digital logic level of the pin. +//| +STATIC mp_obj_t digitalio_digitalinout_obj_get_value(mp_obj_t self_in) { + digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); + bool value = common_hal_digitalio_digitalinout_get_value(self); + return mp_obj_new_bool(value); +} +MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_get_value_obj, digitalio_digitalinout_obj_get_value); + +STATIC mp_obj_t digitalio_digitalinout_obj_set_value(mp_obj_t self_in, mp_obj_t value) { + digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); + if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_IN) { + mp_raise_AttributeError("Cannot set value when direction is input."); + return mp_const_none; + } + common_hal_digitalio_digitalinout_set_value(self, mp_obj_is_true(value)); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(digitalio_digitalinout_set_value_obj, digitalio_digitalinout_obj_set_value); + +const mp_obj_property_t digitalio_digitalinout_value_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&digitalio_digitalinout_get_value_obj, + (mp_obj_t)&digitalio_digitalinout_set_value_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +//| .. attribute:: drive_mode +//| +//| Get or set the pin drive mode. +//| +STATIC mp_obj_t digitalio_digitalinout_obj_get_drive_mode(mp_obj_t self_in) { + digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); + if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_IN) { + mp_raise_AttributeError("Drive mode not used when direction is input."); + return mp_const_none; + } + enum digitalinout_drive_mode_t drive_mode = common_hal_digitalio_digitalinout_get_drive_mode(self); + if (drive_mode == DRIVE_MODE_PUSH_PULL) { + return (mp_obj_t)&digitalio_digitalinout_drive_mode_push_pull_obj; + } + return (mp_obj_t)&digitalio_digitalinout_drive_mode_open_drain_obj; +} +MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_get_drive_mode_obj, digitalio_digitalinout_obj_get_drive_mode); + +STATIC mp_obj_t digitalio_digitalinout_obj_set_drive_mode(mp_obj_t self_in, mp_obj_t drive_mode) { + digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); + if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_IN) { + mp_raise_AttributeError("Drive mode not used when direction is input."); + return mp_const_none; + } + enum digitalinout_drive_mode_t c_drive_mode = DRIVE_MODE_PUSH_PULL; + if (drive_mode == &digitalio_digitalinout_drive_mode_open_drain_obj) { + c_drive_mode = DRIVE_MODE_OPEN_DRAIN; + } + common_hal_digitalio_digitalinout_set_drive_mode(self, c_drive_mode); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(digitalio_digitalinout_set_drive_mode_obj, digitalio_digitalinout_obj_set_drive_mode); + +const mp_obj_property_t digitalio_digitalinout_drive_mode_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&digitalio_digitalinout_get_drive_mode_obj, + (mp_obj_t)&digitalio_digitalinout_set_drive_mode_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +//| .. attribute:: pull +//| +//| Get or set the pin pull. +//| +//| :raises AttributeError: if the direction is ~`Direction.OUT`. +//| +STATIC mp_obj_t digitalio_digitalinout_obj_get_pull(mp_obj_t self_in) { + digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); + if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_OUT) { + mp_raise_AttributeError("Pull not used when direction is output."); + return mp_const_none; + } + enum digitalinout_pull_t pull = common_hal_digitalio_digitalinout_get_pull(self); + if (pull == PULL_UP) { + return (mp_obj_t)&digitalio_digitalinout_pull_up_obj; + } else if (pull == PULL_DOWN) { + return (mp_obj_t)&digitalio_digitalinout_pull_down_obj; + } + return (mp_obj_t)&mp_const_none_obj; +} +MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_get_pull_obj, digitalio_digitalinout_obj_get_pull); + +STATIC mp_obj_t digitalio_digitalinout_obj_set_pull(mp_obj_t self_in, mp_obj_t pull_obj) { + digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); + if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_OUT) { + mp_raise_AttributeError("Pull not used when direction is output."); + return mp_const_none; + } + enum digitalinout_pull_t pull = PULL_NONE; + if (pull_obj == &digitalio_digitalinout_pull_up_obj) { + pull = PULL_UP; + } else if (pull_obj == &digitalio_digitalinout_pull_down_obj) { + pull = PULL_DOWN; + } + common_hal_digitalio_digitalinout_set_pull(self, pull); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(digitalio_digitalinout_set_pull_obj, digitalio_digitalinout_obj_set_pull); + +const mp_obj_property_t digitalio_digitalinout_pull_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&digitalio_digitalinout_get_pull_obj, + (mp_obj_t)&digitalio_digitalinout_set_pull_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +//| .. class:: digitalio.DigitalInOut.Direction +//| +//| Enum-like class to define which direction the digital values are +//| going. +//| +//| .. data:: IN +//| +//| Read digital data in +//| +//| .. data:: OUT +//| +//| Write digital data out +//| +const mp_obj_type_t digitalio_digitalinout_direction_type; + +const digitalio_digitalinout_direction_obj_t digitalio_digitalinout_direction_in_obj = { + { &digitalio_digitalinout_direction_type }, +}; + +const digitalio_digitalinout_direction_obj_t digitalio_digitalinout_direction_out_obj = { + { &digitalio_digitalinout_direction_type }, +}; + +STATIC const mp_rom_map_elem_t digitalio_digitalinout_direction_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_IN), MP_ROM_PTR(&digitalio_digitalinout_direction_in_obj) }, + { MP_ROM_QSTR(MP_QSTR_OUT), MP_ROM_PTR(&digitalio_digitalinout_direction_out_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(digitalio_digitalinout_direction_locals_dict, digitalio_digitalinout_direction_locals_dict_table); + +STATIC void digitalio_digitalinout_direction_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + qstr direction = MP_QSTR_IN; + if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&digitalio_digitalinout_direction_out_obj)) { + direction = MP_QSTR_OUT; + } + mp_printf(print, "%q.%q.%q.%q", MP_QSTR_digitalio, MP_QSTR_DigitalInOut, MP_QSTR_Direction, direction); +} + +const mp_obj_type_t digitalio_digitalinout_direction_type = { + { &mp_type_type }, + .name = MP_QSTR_Direction, + .print = digitalio_digitalinout_direction_print, + .locals_dict = (mp_obj_t)&digitalio_digitalinout_direction_locals_dict, +}; + +//| .. class:: digitalio.DigitalInOut.DriveMode +//| +//| Enum-like class to define the drive mode used when outputting +//| digital values. +//| +//| .. data:: PUSH_PULL +//| +//| Output both high and low digital values +//| +//| .. data:: OPEN_DRAIN +//| +//| Output low digital values but go into high z for digital high. This is +//| useful for i2c and other protocols that share a digital line. +//| +const mp_obj_type_t digitalio_digitalinout_drive_mode_type; + +const digitalio_digitalinout_drive_mode_obj_t digitalio_digitalinout_drive_mode_push_pull_obj = { + { &digitalio_digitalinout_drive_mode_type }, +}; + +const digitalio_digitalinout_drive_mode_obj_t digitalio_digitalinout_drive_mode_open_drain_obj = { + { &digitalio_digitalinout_drive_mode_type }, +}; + +STATIC const mp_rom_map_elem_t digitalio_digitalinout_drive_mode_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_PUSH_PULL), MP_ROM_PTR(&digitalio_digitalinout_drive_mode_push_pull_obj) }, + { MP_ROM_QSTR(MP_QSTR_OPEN_DRAIN), MP_ROM_PTR(&digitalio_digitalinout_drive_mode_open_drain_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(digitalio_digitalinout_drive_mode_locals_dict, digitalio_digitalinout_drive_mode_locals_dict_table); + +STATIC void digitalio_digitalinout_drive_mode_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + qstr drive_mode = MP_QSTR_PUSH_PULL; + if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&digitalio_digitalinout_drive_mode_open_drain_obj)) { + drive_mode = MP_QSTR_OPEN_DRAIN; + } + mp_printf(print, "%q.%q.%q.%q", MP_QSTR_digitalio, MP_QSTR_DigitalInOut, MP_QSTR_DriveMode, drive_mode); +} + +const mp_obj_type_t digitalio_digitalinout_drive_mode_type = { + { &mp_type_type }, + .name = MP_QSTR_DriveMode, + .print = digitalio_digitalinout_drive_mode_print, + .locals_dict = (mp_obj_t)&digitalio_digitalinout_drive_mode_locals_dict, +}; + +//| .. class:: digitalio.DigitalInOut.Pull +//| +//| Enum-like class to define the pull value, if any, used while reading +//| digital values in. +//| +//| .. data:: UP +//| +//| When the input line isn't being driven the pull up can pull the state +//| of the line high so it reads as true. +//| +//| .. data:: DOWN +//| +//| When the input line isn't being driven the pull down can pull the +//| state of the line low so it reads as false. +//| +const mp_obj_type_t digitalio_digitalinout_pull_type; + +const digitalio_digitalinout_pull_obj_t digitalio_digitalinout_pull_up_obj = { + { &digitalio_digitalinout_pull_type }, +}; + +const digitalio_digitalinout_pull_obj_t digitalio_digitalinout_pull_down_obj = { + { &digitalio_digitalinout_pull_type }, +}; + +STATIC const mp_rom_map_elem_t digitalio_digitalinout_pull_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_UP), MP_ROM_PTR(&digitalio_digitalinout_pull_up_obj) }, + { MP_ROM_QSTR(MP_QSTR_DOWN), MP_ROM_PTR(&digitalio_digitalinout_pull_down_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(digitalio_digitalinout_pull_locals_dict, digitalio_digitalinout_pull_locals_dict_table); + +STATIC void digitalio_digitalinout_pull_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + qstr pull = MP_QSTR_UP; + if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&digitalio_digitalinout_pull_down_obj)) { + pull = MP_QSTR_DOWN; + } + mp_printf(print, "%q.%q.%q.%q", MP_QSTR_digitalio, MP_QSTR_DigitalInOut, MP_QSTR_Pull, pull); +} + +const mp_obj_type_t digitalio_digitalinout_pull_type = { + { &mp_type_type }, + .name = MP_QSTR_Pull, + .print = digitalio_digitalinout_pull_print, + .locals_dict = (mp_obj_t)&digitalio_digitalinout_pull_locals_dict, +}; + +STATIC const mp_rom_map_elem_t digitalio_digitalinout_locals_dict_table[] = { + // instance methods + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&digitalio_digitalinout_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&digitalio_digitalinout_obj___exit___obj) }, + { MP_ROM_QSTR(MP_QSTR_switch_to_output), MP_ROM_PTR(&digitalio_digitalinout_switch_to_output_obj) }, + { MP_ROM_QSTR(MP_QSTR_switch_to_input), MP_ROM_PTR(&digitalio_digitalinout_switch_to_input_obj) }, + + // Properties + { MP_ROM_QSTR(MP_QSTR_direction), MP_ROM_PTR(&digitalio_digitalinout_direction_obj) }, + { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&digitalio_digitalinout_value_obj) }, + { MP_ROM_QSTR(MP_QSTR_drive_mode), MP_ROM_PTR(&digitalio_digitalinout_drive_mode_obj) }, + { MP_ROM_QSTR(MP_QSTR_pull), MP_ROM_PTR(&digitalio_digitalinout_pull_obj) }, + + // Nested Enum-like Classes. + { MP_ROM_QSTR(MP_QSTR_Direction), MP_ROM_PTR(&digitalio_digitalinout_direction_type) }, + { MP_ROM_QSTR(MP_QSTR_DriveMode), MP_ROM_PTR(&digitalio_digitalinout_drive_mode_type) }, + { MP_ROM_QSTR(MP_QSTR_Pull), MP_ROM_PTR(&digitalio_digitalinout_pull_type) }, +}; + +STATIC MP_DEFINE_CONST_DICT(digitalio_digitalinout_locals_dict, digitalio_digitalinout_locals_dict_table); + +const mp_obj_type_t digitalio_digitalinout_type = { + { &mp_type_type }, + .name = MP_QSTR_DigitalInOut, + .make_new = digitalio_digitalinout_make_new, + .locals_dict = (mp_obj_t)&digitalio_digitalinout_locals_dict, +}; diff --git a/shared-bindings/nativeio/DigitalInOut.h b/shared-bindings/digitalio/DigitalInOut.h similarity index 50% rename from shared-bindings/nativeio/DigitalInOut.h rename to shared-bindings/digitalio/DigitalInOut.h index b712a811cd..7fd6810f1e 100644 --- a/shared-bindings/nativeio/DigitalInOut.h +++ b/shared-bindings/digitalio/DigitalInOut.h @@ -24,13 +24,13 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_DIGITALINOUT_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_DIGITALINOUT_H__ +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_DIGITALIO_DIGITALINOUT_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_DIGITALIO_DIGITALINOUT_H__ #include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" +#include "common-hal/digitalio/DigitalInOut.h" -extern const mp_obj_type_t nativeio_digitalinout_type; +extern const mp_obj_type_t digitalio_digitalinout_type; enum digitalinout_direction_t { DIRECTION_IN, @@ -53,16 +53,16 @@ typedef enum { DIGITALINOUT_PIN_BUSY } digitalinout_result_t; -digitalinout_result_t common_hal_nativeio_digitalinout_construct(nativeio_digitalinout_obj_t* self, const mcu_pin_obj_t* pin); -void common_hal_nativeio_digitalinout_deinit(nativeio_digitalinout_obj_t* self); -void common_hal_nativeio_digitalinout_switch_to_input(nativeio_digitalinout_obj_t* self, enum digitalinout_pull_t pull); -void common_hal_nativeio_digitalinout_switch_to_output(nativeio_digitalinout_obj_t* self, bool value, enum digitalinout_drive_mode_t drive_mode); -enum digitalinout_direction_t common_hal_nativeio_digitalinout_get_direction(nativeio_digitalinout_obj_t* self); -void common_hal_nativeio_digitalinout_set_value(nativeio_digitalinout_obj_t* self, bool value); -bool common_hal_nativeio_digitalinout_get_value(nativeio_digitalinout_obj_t* self); -void common_hal_nativeio_digitalinout_set_drive_mode(nativeio_digitalinout_obj_t* self, enum digitalinout_drive_mode_t drive_mode); -enum digitalinout_drive_mode_t common_hal_nativeio_digitalinout_get_drive_mode(nativeio_digitalinout_obj_t* self); -void common_hal_nativeio_digitalinout_set_pull(nativeio_digitalinout_obj_t* self, enum digitalinout_pull_t pull); -enum digitalinout_pull_t common_hal_nativeio_digitalinout_get_pull(nativeio_digitalinout_obj_t* self); +digitalinout_result_t common_hal_digitalio_digitalinout_construct(digitalio_digitalinout_obj_t* self, const mcu_pin_obj_t* pin); +void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t* self); +void common_hal_digitalio_digitalinout_switch_to_input(digitalio_digitalinout_obj_t* self, enum digitalinout_pull_t pull); +void common_hal_digitalio_digitalinout_switch_to_output(digitalio_digitalinout_obj_t* self, bool value, enum digitalinout_drive_mode_t drive_mode); +enum digitalinout_direction_t common_hal_digitalio_digitalinout_get_direction(digitalio_digitalinout_obj_t* self); +void common_hal_digitalio_digitalinout_set_value(digitalio_digitalinout_obj_t* self, bool value); +bool common_hal_digitalio_digitalinout_get_value(digitalio_digitalinout_obj_t* self); +void common_hal_digitalio_digitalinout_set_drive_mode(digitalio_digitalinout_obj_t* self, enum digitalinout_drive_mode_t drive_mode); +enum digitalinout_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode(digitalio_digitalinout_obj_t* self); +void common_hal_digitalio_digitalinout_set_pull(digitalio_digitalinout_obj_t* self, enum digitalinout_pull_t pull); +enum digitalinout_pull_t common_hal_digitalio_digitalinout_get_pull(digitalio_digitalinout_obj_t* self); -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_DIGITALINOUT_H__ +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_DIGITALIO_DIGITALINOUT_H__ diff --git a/shared-bindings/digitalio/__init__.c b/shared-bindings/digitalio/__init__.c new file mode 100644 index 0000000000..6871106711 --- /dev/null +++ b/shared-bindings/digitalio/__init__.c @@ -0,0 +1,95 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 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 + +#include "py/obj.h" +#include "py/runtime.h" + +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/digitalio/__init__.h" +#include "shared-bindings/digitalio/DigitalInOut.h" + +#include "py/runtime.h" + +//| :mod:`digitalio` --- Basic digital pin support +//| ================================================= +//| +//| .. module:: digitalio +//| :synopsis: Basic digital pin support +//| :platform: SAMD21, ESP8266 +//| +//| The `digitalio` module contains classes to provide access to basic digital IO. +//| +//| Libraries +//| +//| .. toctree:: +//| :maxdepth: 3 +//| +//| DigitalInOut +//| +//| All classes change hardware state and should be deinitialized when they +//| are no longer needed. To do so, either call :py:meth:`!deinit` or use a +//| context manager. +//| +//| For example:: +//| +//| import digitalio +//| from board import * +//| +//| with digitalio.DigitalInOut(D13) as pin: +//| print(pin.value) +//| +//| This example will initialize the the device, read +//| :py:data:`~digitalio.DigitalInOut.value` and then +//| :py:meth:`~digitalio.DigitalInOut.deinit` the hardware. +//| +//| Here is blinky:: +//| +//| import digitalio +//| from board import * +//| import time +//| +//| with digitalio.DigitalInOut(D13) as led: +//| led.switch_to_output() +//| while True: +//| led.value = True +//| time.sleep(0.1) +//| led.value = False +//| time.sleep(0.1) +//| + +STATIC const mp_rom_map_elem_t digitalio_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_digitalio) }, + { MP_ROM_QSTR(MP_QSTR_DigitalInOut), MP_ROM_PTR(&digitalio_digitalinout_type) }, +}; + +STATIC MP_DEFINE_CONST_DICT(digitalio_module_globals, digitalio_module_globals_table); + +const mp_obj_module_t digitalio_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&digitalio_module_globals, +}; diff --git a/shared-bindings/digitalio/__init__.h b/shared-bindings/digitalio/__init__.h new file mode 100644 index 0000000000..ed7cb51818 --- /dev/null +++ b/shared-bindings/digitalio/__init__.h @@ -0,0 +1,34 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft + * + * 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_DIGITALIO___INIT___H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_DIGITALIO___INIT___H__ + +#include "py/obj.h" + +// Nothing now. + +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_DIGITALIO___INIT___H__ diff --git a/shared-bindings/nativeio/DigitalInOut.c b/shared-bindings/nativeio/DigitalInOut.c deleted file mode 100644 index 421e142771..0000000000 --- a/shared-bindings/nativeio/DigitalInOut.c +++ /dev/null @@ -1,486 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2013, 2014 Damien P. George - * - * 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 -#include - -#include "lib/utils/context_manager_helpers.h" - -#include "py/nlr.h" -#include "py/objtype.h" -#include "py/objproperty.h" -#include "py/runtime.h" -#include "py/mphal.h" - -#include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/DigitalInOut.h" - -//| .. currentmodule:: nativeio -//| -//| :class:`DigitalInOut` -- digital input and output -//| ========================================================= -//| -//| A DigitalInOut is used to digitally control I/O pins. For analog control of -//| a pin, see the :py:class:`~nativeio.AnalogIn` and -//| :py:class:`~nativeio.AnalogOut` classes. -//| - -//| .. class:: DigitalInOut(pin) -//| -//| Create a new DigitalInOut object associated with the pin. Defaults to input -//| with no pull. Use :py:meth:`switch_to_input` and -//| :py:meth:`switch_to_output` to change the direction. -//| -//| :param ~microcontroller.Pin pin: The pin to control -//| -STATIC mp_obj_t nativeio_digitalinout_make_new(const mp_obj_type_t *type, - mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { - mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); - - nativeio_digitalinout_obj_t *self = m_new_obj(nativeio_digitalinout_obj_t); - self->base.type = &nativeio_digitalinout_type; - - assert_pin(args[0], false); - mcu_pin_obj_t *pin = MP_OBJ_TO_PTR(args[0]); - assert_pin_free(pin); - common_hal_nativeio_digitalinout_construct(self, pin); - - return (mp_obj_t)self; -} - -//| .. method:: deinit() -//| -//| Turn off the DigitalInOut and release the pin for other use. -//| -STATIC mp_obj_t nativeio_digitalinout_obj_deinit(mp_obj_t self_in) { - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_digitalinout_deinit(self); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_digitalinout_deinit_obj, nativeio_digitalinout_obj_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_digitalinout_obj___exit__(size_t n_args, const mp_obj_t *args) { - (void)n_args; - common_hal_nativeio_digitalinout_deinit(args[0]); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_digitalinout_obj___exit___obj, 4, 4, nativeio_digitalinout_obj___exit__); - -//| -//| .. method:: switch_to_output(value=False, drive_mode=DriveMode.PUSH_PULL) -//| -//| Switch to writing out digital values. -//| -//| :param bool value: default value to set upon switching -//| :param DriveMode drive_mode: drive mode for the output -//| -typedef struct { - mp_obj_base_t base; -} nativeio_digitalinout_drive_mode_obj_t; -extern const nativeio_digitalinout_drive_mode_obj_t nativeio_digitalinout_drive_mode_push_pull_obj; -extern const nativeio_digitalinout_drive_mode_obj_t nativeio_digitalinout_drive_mode_open_drain_obj; - -STATIC mp_obj_t nativeio_digitalinout_switch_to_output(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_value, ARG_drive_mode }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_value, MP_ARG_BOOL, {.u_bool = false} }, - { MP_QSTR_drive_mode, MP_ARG_OBJ, {.u_rom_obj = &nativeio_digitalinout_drive_mode_push_pull_obj} }, - }; - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - - enum digitalinout_drive_mode_t drive_mode = DRIVE_MODE_PUSH_PULL; - if (args[ARG_drive_mode].u_rom_obj == &nativeio_digitalinout_drive_mode_open_drain_obj) { - drive_mode = DRIVE_MODE_OPEN_DRAIN; - } - // do the transfer - common_hal_nativeio_digitalinout_switch_to_output(self, args[ARG_value].u_bool, drive_mode); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_digitalinout_switch_to_output_obj, 1, nativeio_digitalinout_switch_to_output); - -//| .. method:: switch_to_input(pull=None) -//| -//| Switch to read in digital values. -//| -//| :param Pull pull: pull configuration for the input -//| -//| Example usage:: -//| -//| import nativeio -//| import board -//| -//| with nativeio.DigitalInOut(board.SLIDE_SWITCH) as switch: -//| switch.switch_to_input(pull=nativeio.DigitalInOut.Pull.UP) -//| # Or, after switch_to_input -//| switch.pull = nativeio.DigitalInOut.Pull.UP -//| print(switch.value) -//| -typedef struct { - mp_obj_base_t base; -} nativeio_digitalinout_pull_obj_t; -extern const nativeio_digitalinout_pull_obj_t nativeio_digitalinout_pull_up_obj; -extern const nativeio_digitalinout_pull_obj_t nativeio_digitalinout_pull_down_obj; - -STATIC mp_obj_t nativeio_digitalinout_switch_to_input(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_pull }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_pull, MP_ARG_OBJ, {.u_rom_obj = mp_const_none} }, - }; - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - - enum digitalinout_pull_t pull = PULL_NONE; - if (args[ARG_pull].u_rom_obj == &nativeio_digitalinout_pull_up_obj) { - pull = PULL_UP; - }else if (args[ARG_pull].u_rom_obj == &nativeio_digitalinout_pull_down_obj) { - pull = PULL_DOWN; - } - // do the transfer - common_hal_nativeio_digitalinout_switch_to_input(self, pull); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_digitalinout_switch_to_input_obj, 1, nativeio_digitalinout_switch_to_input); - -//| .. attribute:: direction -//| -//| Get the direction of the pin. -//| -//| :raises AttributeError: when set. Use :py:meth:`switch_to_input` and :py:meth:`switch_to_output` to change the direction. -//| -typedef struct { - mp_obj_base_t base; -} nativeio_digitalinout_direction_obj_t; -extern const nativeio_digitalinout_direction_obj_t nativeio_digitalinout_direction_in_obj; -extern const nativeio_digitalinout_direction_obj_t nativeio_digitalinout_direction_out_obj; - -STATIC mp_obj_t nativeio_digitalinout_obj_get_direction(mp_obj_t self_in) { - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); - enum digitalinout_direction_t direction = common_hal_nativeio_digitalinout_get_direction(self); - if (direction == DIRECTION_IN) { - return (mp_obj_t)&nativeio_digitalinout_direction_in_obj; - } - return (mp_obj_t)&nativeio_digitalinout_direction_out_obj; -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_digitalinout_get_direction_obj, nativeio_digitalinout_obj_get_direction); - -const mp_obj_property_t nativeio_digitalinout_direction_obj = { - .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_digitalinout_get_direction_obj, - (mp_obj_t)&mp_const_none_obj, - (mp_obj_t)&mp_const_none_obj}, -}; - -//| .. attribute:: value -//| -//| The digital logic level of the pin. -//| -STATIC mp_obj_t nativeio_digitalinout_obj_get_value(mp_obj_t self_in) { - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); - bool value = common_hal_nativeio_digitalinout_get_value(self); - return mp_obj_new_bool(value); -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_digitalinout_get_value_obj, nativeio_digitalinout_obj_get_value); - -STATIC mp_obj_t nativeio_digitalinout_obj_set_value(mp_obj_t self_in, mp_obj_t value) { - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (common_hal_nativeio_digitalinout_get_direction(self) == DIRECTION_IN) { - mp_raise_AttributeError("Cannot set value when direction is input."); - return mp_const_none; - } - common_hal_nativeio_digitalinout_set_value(self, mp_obj_is_true(value)); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_2(nativeio_digitalinout_set_value_obj, nativeio_digitalinout_obj_set_value); - -const mp_obj_property_t nativeio_digitalinout_value_obj = { - .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_digitalinout_get_value_obj, - (mp_obj_t)&nativeio_digitalinout_set_value_obj, - (mp_obj_t)&mp_const_none_obj}, -}; - -//| .. attribute:: drive_mode -//| -//| Get or set the pin drive mode. -//| -STATIC mp_obj_t nativeio_digitalinout_obj_get_drive_mode(mp_obj_t self_in) { - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (common_hal_nativeio_digitalinout_get_direction(self) == DIRECTION_IN) { - mp_raise_AttributeError("Drive mode not used when direction is input."); - return mp_const_none; - } - enum digitalinout_drive_mode_t drive_mode = common_hal_nativeio_digitalinout_get_drive_mode(self); - if (drive_mode == DRIVE_MODE_PUSH_PULL) { - return (mp_obj_t)&nativeio_digitalinout_drive_mode_push_pull_obj; - } - return (mp_obj_t)&nativeio_digitalinout_drive_mode_open_drain_obj; -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_digitalinout_get_drive_mode_obj, nativeio_digitalinout_obj_get_drive_mode); - -STATIC mp_obj_t nativeio_digitalinout_obj_set_drive_mode(mp_obj_t self_in, mp_obj_t drive_mode) { - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (common_hal_nativeio_digitalinout_get_direction(self) == DIRECTION_IN) { - mp_raise_AttributeError("Drive mode not used when direction is input."); - return mp_const_none; - } - enum digitalinout_drive_mode_t c_drive_mode = DRIVE_MODE_PUSH_PULL; - if (drive_mode == &nativeio_digitalinout_drive_mode_open_drain_obj) { - c_drive_mode = DRIVE_MODE_OPEN_DRAIN; - } - common_hal_nativeio_digitalinout_set_drive_mode(self, c_drive_mode); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_2(nativeio_digitalinout_set_drive_mode_obj, nativeio_digitalinout_obj_set_drive_mode); - -const mp_obj_property_t nativeio_digitalinout_drive_mode_obj = { - .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_digitalinout_get_drive_mode_obj, - (mp_obj_t)&nativeio_digitalinout_set_drive_mode_obj, - (mp_obj_t)&mp_const_none_obj}, -}; - -//| .. attribute:: pull -//| -//| Get or set the pin pull. -//| -//| :raises AttributeError: if the direction is ~`Direction.OUT`. -//| -STATIC mp_obj_t nativeio_digitalinout_obj_get_pull(mp_obj_t self_in) { - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (common_hal_nativeio_digitalinout_get_direction(self) == DIRECTION_OUT) { - mp_raise_AttributeError("Pull not used when direction is output."); - return mp_const_none; - } - enum digitalinout_pull_t pull = common_hal_nativeio_digitalinout_get_pull(self); - if (pull == PULL_UP) { - return (mp_obj_t)&nativeio_digitalinout_pull_up_obj; - } else if (pull == PULL_DOWN) { - return (mp_obj_t)&nativeio_digitalinout_pull_down_obj; - } - return (mp_obj_t)&mp_const_none_obj; -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_digitalinout_get_pull_obj, nativeio_digitalinout_obj_get_pull); - -STATIC mp_obj_t nativeio_digitalinout_obj_set_pull(mp_obj_t self_in, mp_obj_t pull_obj) { - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (common_hal_nativeio_digitalinout_get_direction(self) == DIRECTION_OUT) { - mp_raise_AttributeError("Pull not used when direction is output."); - return mp_const_none; - } - enum digitalinout_pull_t pull = PULL_NONE; - if (pull_obj == &nativeio_digitalinout_pull_up_obj) { - pull = PULL_UP; - } else if (pull_obj == &nativeio_digitalinout_pull_down_obj) { - pull = PULL_DOWN; - } - common_hal_nativeio_digitalinout_set_pull(self, pull); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_2(nativeio_digitalinout_set_pull_obj, nativeio_digitalinout_obj_set_pull); - -const mp_obj_property_t nativeio_digitalinout_pull_obj = { - .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_digitalinout_get_pull_obj, - (mp_obj_t)&nativeio_digitalinout_set_pull_obj, - (mp_obj_t)&mp_const_none_obj}, -}; - -//| .. class:: microcontroller.DigitalInOut.Direction -//| -//| Enum-like class to define which direction the digital values are -//| going. -//| -//| .. data:: IN -//| -//| Read digital data in -//| -//| .. data:: OUT -//| -//| Write digital data out -//| -const mp_obj_type_t nativeio_digitalinout_direction_type; - -const nativeio_digitalinout_direction_obj_t nativeio_digitalinout_direction_in_obj = { - { &nativeio_digitalinout_direction_type }, -}; - -const nativeio_digitalinout_direction_obj_t nativeio_digitalinout_direction_out_obj = { - { &nativeio_digitalinout_direction_type }, -}; - -STATIC const mp_rom_map_elem_t nativeio_digitalinout_direction_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_IN), MP_ROM_PTR(&nativeio_digitalinout_direction_in_obj) }, - { MP_ROM_QSTR(MP_QSTR_OUT), MP_ROM_PTR(&nativeio_digitalinout_direction_out_obj) }, -}; -STATIC MP_DEFINE_CONST_DICT(nativeio_digitalinout_direction_locals_dict, nativeio_digitalinout_direction_locals_dict_table); - -STATIC void nativeio_digitalinout_direction_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { - qstr direction = MP_QSTR_IN; - if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&nativeio_digitalinout_direction_out_obj)) { - direction = MP_QSTR_OUT; - } - mp_printf(print, "%q.%q.%q.%q", MP_QSTR_nativeio, MP_QSTR_DigitalInOut, MP_QSTR_Direction, direction); -} - -const mp_obj_type_t nativeio_digitalinout_direction_type = { - { &mp_type_type }, - .name = MP_QSTR_Direction, - .print = nativeio_digitalinout_direction_print, - .locals_dict = (mp_obj_t)&nativeio_digitalinout_direction_locals_dict, -}; - -//| .. class:: nativeio.DigitalInOut.DriveMode -//| -//| Enum-like class to define the drive mode used when outputting -//| digital values. -//| -//| .. data:: PUSH_PULL -//| -//| Output both high and low digital values -//| -//| .. data:: OPEN_DRAIN -//| -//| Output low digital values but go into high z for digital high. This is -//| useful for i2c and other protocols that share a digital line. -//| -const mp_obj_type_t nativeio_digitalinout_drive_mode_type; - -const nativeio_digitalinout_drive_mode_obj_t nativeio_digitalinout_drive_mode_push_pull_obj = { - { &nativeio_digitalinout_drive_mode_type }, -}; - -const nativeio_digitalinout_drive_mode_obj_t nativeio_digitalinout_drive_mode_open_drain_obj = { - { &nativeio_digitalinout_drive_mode_type }, -}; - -STATIC const mp_rom_map_elem_t nativeio_digitalinout_drive_mode_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_PUSH_PULL), MP_ROM_PTR(&nativeio_digitalinout_drive_mode_push_pull_obj) }, - { MP_ROM_QSTR(MP_QSTR_OPEN_DRAIN), MP_ROM_PTR(&nativeio_digitalinout_drive_mode_open_drain_obj) }, -}; -STATIC MP_DEFINE_CONST_DICT(nativeio_digitalinout_drive_mode_locals_dict, nativeio_digitalinout_drive_mode_locals_dict_table); - -STATIC void nativeio_digitalinout_drive_mode_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { - qstr drive_mode = MP_QSTR_PUSH_PULL; - if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&nativeio_digitalinout_drive_mode_open_drain_obj)) { - drive_mode = MP_QSTR_OPEN_DRAIN; - } - mp_printf(print, "%q.%q.%q.%q", MP_QSTR_nativeio, MP_QSTR_DigitalInOut, MP_QSTR_DriveMode, drive_mode); -} - -const mp_obj_type_t nativeio_digitalinout_drive_mode_type = { - { &mp_type_type }, - .name = MP_QSTR_DriveMode, - .print = nativeio_digitalinout_drive_mode_print, - .locals_dict = (mp_obj_t)&nativeio_digitalinout_drive_mode_locals_dict, -}; - -//| .. class:: nativeio.DigitalInOut.Pull -//| -//| Enum-like class to define the pull value, if any, used while reading -//| digital values in. -//| -//| .. data:: UP -//| -//| When the input line isn't being driven the pull up can pull the state -//| of the line high so it reads as true. -//| -//| .. data:: DOWN -//| -//| When the input line isn't being driven the pull down can pull the -//| state of the line low so it reads as false. -//| -const mp_obj_type_t nativeio_digitalinout_pull_type; - -const nativeio_digitalinout_pull_obj_t nativeio_digitalinout_pull_up_obj = { - { &nativeio_digitalinout_pull_type }, -}; - -const nativeio_digitalinout_pull_obj_t nativeio_digitalinout_pull_down_obj = { - { &nativeio_digitalinout_pull_type }, -}; - -STATIC const mp_rom_map_elem_t nativeio_digitalinout_pull_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_UP), MP_ROM_PTR(&nativeio_digitalinout_pull_up_obj) }, - { MP_ROM_QSTR(MP_QSTR_DOWN), MP_ROM_PTR(&nativeio_digitalinout_pull_down_obj) }, -}; -STATIC MP_DEFINE_CONST_DICT(nativeio_digitalinout_pull_locals_dict, nativeio_digitalinout_pull_locals_dict_table); - -STATIC void nativeio_digitalinout_pull_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { - qstr pull = MP_QSTR_UP; - if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&nativeio_digitalinout_pull_down_obj)) { - pull = MP_QSTR_DOWN; - } - mp_printf(print, "%q.%q.%q.%q", MP_QSTR_nativeio, MP_QSTR_DigitalInOut, MP_QSTR_Pull, pull); -} - -const mp_obj_type_t nativeio_digitalinout_pull_type = { - { &mp_type_type }, - .name = MP_QSTR_Pull, - .print = nativeio_digitalinout_pull_print, - .locals_dict = (mp_obj_t)&nativeio_digitalinout_pull_locals_dict, -}; - -STATIC const mp_rom_map_elem_t nativeio_digitalinout_locals_dict_table[] = { - // instance methods - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_digitalinout_deinit_obj) }, - { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, - { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_digitalinout_obj___exit___obj) }, - { MP_ROM_QSTR(MP_QSTR_switch_to_output), MP_ROM_PTR(&nativeio_digitalinout_switch_to_output_obj) }, - { MP_ROM_QSTR(MP_QSTR_switch_to_input), MP_ROM_PTR(&nativeio_digitalinout_switch_to_input_obj) }, - - // Properties - { MP_ROM_QSTR(MP_QSTR_direction), MP_ROM_PTR(&nativeio_digitalinout_direction_obj) }, - { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&nativeio_digitalinout_value_obj) }, - { MP_ROM_QSTR(MP_QSTR_drive_mode), MP_ROM_PTR(&nativeio_digitalinout_drive_mode_obj) }, - { MP_ROM_QSTR(MP_QSTR_pull), MP_ROM_PTR(&nativeio_digitalinout_pull_obj) }, - - // Nested Enum-like Classes. - { MP_ROM_QSTR(MP_QSTR_Direction), MP_ROM_PTR(&nativeio_digitalinout_direction_type) }, - { MP_ROM_QSTR(MP_QSTR_DriveMode), MP_ROM_PTR(&nativeio_digitalinout_drive_mode_type) }, - { MP_ROM_QSTR(MP_QSTR_Pull), MP_ROM_PTR(&nativeio_digitalinout_pull_type) }, -}; - -STATIC MP_DEFINE_CONST_DICT(nativeio_digitalinout_locals_dict, nativeio_digitalinout_locals_dict_table); - -const mp_obj_type_t nativeio_digitalinout_type = { - { &mp_type_type }, - .name = MP_QSTR_DigitalInOut, - .make_new = nativeio_digitalinout_make_new, - .locals_dict = (mp_obj_t)&nativeio_digitalinout_locals_dict, -}; diff --git a/shared-bindings/nativeio/__init__.c b/shared-bindings/nativeio/__init__.c deleted file mode 100644 index 3287a46cde..0000000000 --- a/shared-bindings/nativeio/__init__.c +++ /dev/null @@ -1,140 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2016 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. - */ - -// nativeio is the HAL for low-level, hardware accelerated classes. It -// is not meant to simplify APIs, its only meant to unify them so that other -// libraries do not require port specific logic. - -#include - -#include "py/obj.h" -#include "py/runtime.h" - -#include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/__init__.h" -#include "shared-bindings/nativeio/AnalogIn.h" -#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" -#include "shared-bindings/nativeio/SPI.h" -#include "shared-bindings/nativeio/TouchIn.h" -#include "shared-bindings/nativeio/UART.h" -#include "common-hal/nativeio/types.h" -#include "shared-bindings/nativeio/__init__.h" - -#include "py/runtime.h" - -//| :mod:`nativeio` --- Hardware accelerated behavior -//| ================================================= -//| -//| .. module:: nativeio -//| :synopsis: Hardware accelerated behavior -//| :platform: SAMD21 -//| -//| 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). -//| -//| 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 -//| -//| .. toctree:: -//| :maxdepth: 3 -//| -//| AnalogIn -//| AnalogOut -//| DigitalInOut -//| I2C -//| OneWire -//| PulseIn -//| PulseOut -//| PWMOut -//| SPI -//| TouchIn -//| UART -//| -//| All libraries change hardware state and should be deinitialized when they -//| are no longer needed. To do so, either call :py:meth:`!deinit` or use a -//| context manager. -//| -//| For example:: -//| -//| import nativeio -//| from board import * -//| -//| with nativeio.I2C(SCL, SDA) as i2c: -//| i2c.scan() -//| -//| This example will initialize the the device, run -//| :py:meth:`~nativeio.I2C.scan` and then :py:meth:`~nativeio.I2C.deinit` the -//| hardware. -//| -//| Here is blinky:: -//| -//| import nativeio -//| from board import * -//| import time -//| -//| with nativeio.DigitalInOut(D13) as led: -//| led.switch_to_output() -//| while True: -//| led.value = True -//| time.sleep(0.1) -//| led.value = False -//| time.sleep(0.1) -//| - -STATIC const mp_rom_map_elem_t nativeio_module_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_nativeio) }, - { MP_ROM_QSTR(MP_QSTR_AnalogIn), MP_ROM_PTR(&nativeio_analogin_type) }, - { 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) }, - { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&nativeio_spi_type) }, - { MP_ROM_QSTR(MP_QSTR_TouchIn), MP_ROM_PTR(&nativeio_touchin_type) }, - { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&nativeio_uart_type) }, -}; - -STATIC MP_DEFINE_CONST_DICT(nativeio_module_globals, nativeio_module_globals_table); - -const mp_obj_module_t nativeio_module = { - .base = { &mp_type_module }, - .globals = (mp_obj_dict_t*)&nativeio_module_globals, -}; diff --git a/shared-bindings/neopixel_write/__init__.c b/shared-bindings/neopixel_write/__init__.c index b0490e1ae4..faf2433d08 100644 --- a/shared-bindings/neopixel_write/__init__.c +++ b/shared-bindings/neopixel_write/__init__.c @@ -28,9 +28,7 @@ #include "py/mphal.h" #include "py/runtime.h" -#include "common-hal/nativeio/types.h" - -#include "shared-bindings/nativeio/DigitalInOut.h" +#include "shared-bindings/digitalio/DigitalInOut.h" #include "shared-bindings/neopixel_write/__init__.h" //| :mod:`neopixel_write` --- Low-level neopixel implementation @@ -47,15 +45,15 @@ //| //| Write buf out on the given DigitalInOut. //| -//| :param ~nativeio.DigitalInOut gpio: the DigitalInOut to output with +//| :param ~digitalio.DigitalInOut gpio: the DigitalInOut to output with //| :param bytearray buf: The bytes to clock out. No assumption is made about color order //| STATIC mp_obj_t neopixel_write_neopixel_write_(mp_obj_t digitalinout_obj, mp_obj_t buf) { - if (!MP_OBJ_IS_TYPE(digitalinout_obj, &nativeio_digitalinout_type)) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "Expected a %q", nativeio_digitalinout_type.name)); + if (!MP_OBJ_IS_TYPE(digitalinout_obj, &digitalio_digitalinout_type)) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "Expected a %q", digitalio_digitalinout_type.name)); } // Convert parameters into expected types. - const nativeio_digitalinout_obj_t *digitalinout = MP_OBJ_TO_PTR(digitalinout_obj); + const digitalio_digitalinout_obj_t *digitalinout = MP_OBJ_TO_PTR(digitalinout_obj); mp_buffer_info_t bufinfo; mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ); // Call platform's neopixel write function with provided buffer and options. diff --git a/shared-bindings/neopixel_write/__init__.h b/shared-bindings/neopixel_write/__init__.h index 87f85ee015..39b565ee2f 100644 --- a/shared-bindings/neopixel_write/__init__.h +++ b/shared-bindings/neopixel_write/__init__.h @@ -30,8 +30,8 @@ #include #include -#include "common-hal/nativeio/types.h" +#include "common-hal/digitalio/DigitalInOut.h" -extern void common_hal_neopixel_write(const nativeio_digitalinout_obj_t* gpio, uint8_t *pixels, uint32_t numBytes); +extern void common_hal_neopixel_write(const digitalio_digitalinout_obj_t* gpio, uint8_t *pixels, uint32_t numBytes); #endif diff --git a/shared-bindings/nativeio/PWMOut.c b/shared-bindings/pulseio/PWMOut.c similarity index 62% rename from shared-bindings/nativeio/PWMOut.c rename to shared-bindings/pulseio/PWMOut.c index 6d5710f8e1..73f3bfd3e8 100644 --- a/shared-bindings/nativeio/PWMOut.c +++ b/shared-bindings/pulseio/PWMOut.c @@ -30,9 +30,9 @@ #include "py/objproperty.h" #include "py/runtime.h" #include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/PWMOut.h" +#include "shared-bindings/pulseio/PWMOut.h" -//| .. currentmodule:: nativeio +//| .. currentmodule:: pulseio //| //| :class:`PWMOut` -- Output a Pulse Width Modulated signal //| ======================================================== @@ -57,32 +57,32 @@ //| //| Simple LED fade:: //| -//| import nativeio +//| import pulseio //| import board //| -//| with nativeio.PWMOut(board.D13) as pwm: # output on D13 +//| with pulseio.PWMOut(board.D13) as pwm: # output on D13 //| pwm.duty_cycle = 2 ** 15 # Cycles the pin with 50% duty cycle (half of 2 ** 16) at the default 500hz //| //| PWM at specific frequency (servos and motors):: //| -//| import nativeio +//| import pulseio //| import board //| -//| with nativeio.PWMOut(board.D13, frequency=50) as pwm: +//| with pulseio.PWMOut(board.D13, frequency=50) as pwm: //| pwm.duty_cycle = 2 ** 15 # Cycles the pin with 50% duty cycle (half of 2 ** 16) at 50hz //| //| Variable frequency (usually tones):: //| -//| import nativeio +//| import pulseio //| import board //| import time //| -//| with nativeio.PWMOut(board.D13, duty_cycle=2 ** 15, frequency=440, variable_frequency=True) as pwm: +//| with pulseio.PWMOut(board.D13, duty_cycle=2 ** 15, frequency=440, variable_frequency=True) as pwm: //| time.sleep(0.2) //| pwm.frequency = 880 //| time.sleep(0.1) //| -STATIC mp_obj_t nativeio_pwmout_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { +STATIC mp_obj_t pulseio_pwmout_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); mp_obj_t pin_obj = args[0]; assert_pin(pin_obj, false); @@ -90,8 +90,8 @@ STATIC mp_obj_t nativeio_pwmout_make_new(const mp_obj_type_t *type, size_t n_arg assert_pin_free(pin); // create PWM object from the given pin - nativeio_pwmout_obj_t *self = m_new_obj(nativeio_pwmout_obj_t); - self->base.type = &nativeio_pwmout_type; + pulseio_pwmout_obj_t *self = m_new_obj(pulseio_pwmout_obj_t); + self->base.type = &pulseio_pwmout_type; mp_map_t kw_args; mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); @@ -107,7 +107,7 @@ STATIC mp_obj_t nativeio_pwmout_make_new(const mp_obj_type_t *type, size_t n_arg uint32_t frequency = parsed_args[ARG_frequency].u_int; bool variable_frequency = parsed_args[ARG_variable_frequency].u_int; - common_hal_nativeio_pwmout_construct(self, pin, duty_cycle, frequency, variable_frequency); + common_hal_pulseio_pwmout_construct(self, pin, duty_cycle, frequency, variable_frequency); return MP_OBJ_FROM_PTR(self); } @@ -116,12 +116,12 @@ STATIC mp_obj_t nativeio_pwmout_make_new(const mp_obj_type_t *type, size_t n_arg //| //| Deinitialises the PWMOut and releases any hardware resources for reuse. //| -STATIC mp_obj_t nativeio_pwmout_deinit(mp_obj_t self_in) { - nativeio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_pwmout_deinit(self); +STATIC mp_obj_t pulseio_pwmout_deinit(mp_obj_t self_in) { + pulseio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_pulseio_pwmout_deinit(self); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pwmout_deinit_obj, nativeio_pwmout_deinit); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pwmout_deinit_obj, pulseio_pwmout_deinit); //| .. method:: __enter__() //| @@ -133,39 +133,39 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pwmout_deinit_obj, nativeio_pwmout_dei //| //| Automatically deinitializes the hardware when exiting a context. //| -STATIC mp_obj_t nativeio_pwmout_obj___exit__(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t pulseio_pwmout_obj___exit__(size_t n_args, const mp_obj_t *args) { (void)n_args; - common_hal_nativeio_pwmout_deinit(args[0]); + common_hal_pulseio_pwmout_deinit(args[0]); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_pwmout___exit___obj, 4, 4, nativeio_pwmout_obj___exit__); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pulseio_pwmout___exit___obj, 4, 4, pulseio_pwmout_obj___exit__); //| .. attribute:: duty_cycle //| //| 16 bit value that dictates how much of one cycle is high (1) versus low //| (0). 0xffff will always be high, 0 will always be low and 0x7fff will //| be half high and then half low. -STATIC mp_obj_t nativeio_pwmout_obj_get_duty_cycle(mp_obj_t self_in) { - nativeio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); - return MP_OBJ_NEW_SMALL_INT(common_hal_nativeio_pwmout_get_duty_cycle(self)); +STATIC mp_obj_t pulseio_pwmout_obj_get_duty_cycle(mp_obj_t self_in) { + pulseio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); + return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pwmout_get_duty_cycle(self)); } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pwmout_get_duty_cycle_obj, nativeio_pwmout_obj_get_duty_cycle); +MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pwmout_get_duty_cycle_obj, pulseio_pwmout_obj_get_duty_cycle); -STATIC mp_obj_t nativeio_pwmout_obj_set_duty_cycle(mp_obj_t self_in, mp_obj_t duty_cycle) { - nativeio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); +STATIC mp_obj_t pulseio_pwmout_obj_set_duty_cycle(mp_obj_t self_in, mp_obj_t duty_cycle) { + pulseio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); mp_int_t duty = mp_obj_get_int(duty_cycle); if (duty < 0 || duty > 0xffff) { mp_raise_ValueError("PWM duty must be between 0 and 65536 (16 bit resolution)"); } - common_hal_nativeio_pwmout_set_duty_cycle(self, duty); + common_hal_pulseio_pwmout_set_duty_cycle(self, duty); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_2(nativeio_pwmout_set_duty_cycle_obj, nativeio_pwmout_obj_set_duty_cycle); +MP_DEFINE_CONST_FUN_OBJ_2(pulseio_pwmout_set_duty_cycle_obj, pulseio_pwmout_obj_set_duty_cycle); -const mp_obj_property_t nativeio_pwmout_duty_cycle_obj = { +const mp_obj_property_t pulseio_pwmout_duty_cycle_obj = { .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_pwmout_get_duty_cycle_obj, - (mp_obj_t)&nativeio_pwmout_set_duty_cycle_obj, + .proxy = {(mp_obj_t)&pulseio_pwmout_get_duty_cycle_obj, + (mp_obj_t)&pulseio_pwmout_set_duty_cycle_obj, (mp_obj_t)&mp_const_none_obj}, }; @@ -174,48 +174,48 @@ const mp_obj_property_t nativeio_pwmout_duty_cycle_obj = { //| 32 bit value that dictates the PWM frequency in Hertz (cycles per //| second). Only writeable when constructed with ``variable_frequency=True``. //| -STATIC mp_obj_t nativeio_pwmout_obj_get_frequency(mp_obj_t self_in) { - nativeio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); - return MP_OBJ_NEW_SMALL_INT(common_hal_nativeio_pwmout_get_frequency(self)); +STATIC mp_obj_t pulseio_pwmout_obj_get_frequency(mp_obj_t self_in) { + pulseio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); + return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pwmout_get_frequency(self)); } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pwmout_get_frequency_obj, nativeio_pwmout_obj_get_frequency); +MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pwmout_get_frequency_obj, pulseio_pwmout_obj_get_frequency); -STATIC mp_obj_t nativeio_pwmout_obj_set_frequency(mp_obj_t self_in, mp_obj_t frequency) { - nativeio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (!common_hal_nativeio_pwmout_get_variable_frequency(self)) { +STATIC mp_obj_t pulseio_pwmout_obj_set_frequency(mp_obj_t self_in, mp_obj_t frequency) { + pulseio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); + if (!common_hal_pulseio_pwmout_get_variable_frequency(self)) { mp_raise_AttributeError( "PWM frequency not writeable when variable_frequency is False on " "construction."); } - common_hal_nativeio_pwmout_set_frequency(self, mp_obj_get_int(frequency)); + common_hal_pulseio_pwmout_set_frequency(self, mp_obj_get_int(frequency)); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_2(nativeio_pwmout_set_frequency_obj, nativeio_pwmout_obj_set_frequency); +MP_DEFINE_CONST_FUN_OBJ_2(pulseio_pwmout_set_frequency_obj, pulseio_pwmout_obj_set_frequency); -const mp_obj_property_t nativeio_pwmout_frequency_obj = { +const mp_obj_property_t pulseio_pwmout_frequency_obj = { .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_pwmout_get_frequency_obj, - (mp_obj_t)&nativeio_pwmout_set_frequency_obj, + .proxy = {(mp_obj_t)&pulseio_pwmout_get_frequency_obj, + (mp_obj_t)&pulseio_pwmout_set_frequency_obj, (mp_obj_t)&mp_const_none_obj}, }; -STATIC const mp_rom_map_elem_t nativeio_pwmout_locals_dict_table[] = { +STATIC const mp_rom_map_elem_t pulseio_pwmout_locals_dict_table[] = { // Methods - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_pwmout_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&pulseio_pwmout_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, - { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_pwmout___exit___obj) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&pulseio_pwmout___exit___obj) }, // Properties - { MP_ROM_QSTR(MP_QSTR_duty_cycle), MP_ROM_PTR(&nativeio_pwmout_duty_cycle_obj) }, - { MP_ROM_QSTR(MP_QSTR_frequency), MP_ROM_PTR(&nativeio_pwmout_frequency_obj) }, + { MP_ROM_QSTR(MP_QSTR_duty_cycle), MP_ROM_PTR(&pulseio_pwmout_duty_cycle_obj) }, + { MP_ROM_QSTR(MP_QSTR_frequency), MP_ROM_PTR(&pulseio_pwmout_frequency_obj) }, // TODO(tannewt): Add enabled to determine whether the signal is output // without giving up the resources. Useful for IR output. }; -STATIC MP_DEFINE_CONST_DICT(nativeio_pwmout_locals_dict, nativeio_pwmout_locals_dict_table); +STATIC MP_DEFINE_CONST_DICT(pulseio_pwmout_locals_dict, pulseio_pwmout_locals_dict_table); -const mp_obj_type_t nativeio_pwmout_type = { +const mp_obj_type_t pulseio_pwmout_type = { { &mp_type_type }, .name = MP_QSTR_PWMOut, - .make_new = nativeio_pwmout_make_new, - .locals_dict = (mp_obj_dict_t*)&nativeio_pwmout_locals_dict, + .make_new = pulseio_pwmout_make_new, + .locals_dict = (mp_obj_dict_t*)&pulseio_pwmout_locals_dict, }; diff --git a/shared-bindings/nativeio/PWMOut.h b/shared-bindings/pulseio/PWMOut.h similarity index 60% rename from shared-bindings/nativeio/PWMOut.h rename to shared-bindings/pulseio/PWMOut.h index 588992ceba..f7a0bff222 100644 --- a/shared-bindings/nativeio/PWMOut.h +++ b/shared-bindings/pulseio/PWMOut.h @@ -24,22 +24,22 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PWMOUT_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PWMOUT_H__ +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PWMOUT_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PWMOUT_H__ #include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" +#include "common-hal/pulseio/PWMOut.h" -extern const mp_obj_type_t nativeio_pwmout_type; +extern const mp_obj_type_t pulseio_pwmout_type; -extern void common_hal_nativeio_pwmout_construct(nativeio_pwmout_obj_t* self, +extern void common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self, const mcu_pin_obj_t* pin, uint16_t duty, uint32_t frequency, bool variable_frequency); -extern void common_hal_nativeio_pwmout_deinit(nativeio_pwmout_obj_t* self); -extern void common_hal_nativeio_pwmout_set_duty_cycle(nativeio_pwmout_obj_t* self, uint16_t duty); -extern uint16_t common_hal_nativeio_pwmout_get_duty_cycle(nativeio_pwmout_obj_t* self); -extern void common_hal_nativeio_pwmout_set_frequency(nativeio_pwmout_obj_t* self, uint32_t frequency); -extern uint32_t common_hal_nativeio_pwmout_get_frequency(nativeio_pwmout_obj_t* self); -extern bool common_hal_nativeio_pwmout_get_variable_frequency(nativeio_pwmout_obj_t* self); +extern void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t* self); +extern void common_hal_pulseio_pwmout_set_duty_cycle(pulseio_pwmout_obj_t* self, uint16_t duty); +extern uint16_t common_hal_pulseio_pwmout_get_duty_cycle(pulseio_pwmout_obj_t* self); +extern void common_hal_pulseio_pwmout_set_frequency(pulseio_pwmout_obj_t* self, uint32_t frequency); +extern uint32_t common_hal_pulseio_pwmout_get_frequency(pulseio_pwmout_obj_t* self); +extern bool common_hal_pulseio_pwmout_get_variable_frequency(pulseio_pwmout_obj_t* self); -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PWMOUT_H__ +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PWMOUT_H__ diff --git a/shared-bindings/nativeio/PulseIn.c b/shared-bindings/pulseio/PulseIn.c similarity index 66% rename from shared-bindings/nativeio/PulseIn.c rename to shared-bindings/pulseio/PulseIn.c index 78e0c84e4d..e87020cd93 100644 --- a/shared-bindings/nativeio/PulseIn.c +++ b/shared-bindings/pulseio/PulseIn.c @@ -31,9 +31,9 @@ #include "py/runtime.h" #include "py/runtime0.h" #include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/PulseIn.h" +#include "shared-bindings/pulseio/PulseIn.h" -//| .. currentmodule:: nativeio +//| .. currentmodule:: pulseio //| //| :class:`PulseIn` -- Read a series of pulse durations //| ======================================================== @@ -58,10 +58,10 @@ //| //| Read a short series of pulses:: //| -//| import nativeio +//| import pulseio //| import board //| -//| with nativeio.PulseIn(board.D7) as pulses: +//| with pulseio.PulseIn(board.D7) as pulses: //| # Wait for an active pulse //| while len(pulses) == 0: //| pass @@ -78,7 +78,7 @@ //| # Resume with an 80 microsecond active pulse //| pulse_in.resume(80) //| -STATIC mp_obj_t nativeio_pulsein_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { +STATIC mp_obj_t pulseio_pulsein_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); @@ -94,10 +94,10 @@ STATIC mp_obj_t nativeio_pulsein_make_new(const mp_obj_type_t *type, size_t n_ar const mcu_pin_obj_t* pin = MP_OBJ_TO_PTR(args[ARG_pin].u_obj); assert_pin_free(pin); - nativeio_pulsein_obj_t *self = m_new_obj(nativeio_pulsein_obj_t); - self->base.type = &nativeio_pulsein_type; + pulseio_pulsein_obj_t *self = m_new_obj(pulseio_pulsein_obj_t); + self->base.type = &pulseio_pulsein_type; - common_hal_nativeio_pulsein_construct(self, pin, args[ARG_maxlen].u_int, + common_hal_pulseio_pulsein_construct(self, pin, args[ARG_maxlen].u_int, args[ARG_idle_state].u_bool); return MP_OBJ_FROM_PTR(self); @@ -107,12 +107,12 @@ STATIC mp_obj_t nativeio_pulsein_make_new(const mp_obj_type_t *type, size_t n_ar //| //| Deinitialises the PulseIn and releases any hardware resources for reuse. //| -STATIC mp_obj_t nativeio_pulsein_deinit(mp_obj_t self_in) { - nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_pulsein_deinit(self); +STATIC mp_obj_t pulseio_pulsein_deinit(mp_obj_t self_in) { + pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_pulseio_pulsein_deinit(self); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulsein_deinit_obj, nativeio_pulsein_deinit); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulsein_deinit_obj, pulseio_pulsein_deinit); //| .. method:: __enter__() //| @@ -124,24 +124,24 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulsein_deinit_obj, nativeio_pulsein_d //| //| Automatically deinitializes the hardware when exiting a context. //| -STATIC mp_obj_t nativeio_pulsein_obj___exit__(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t pulseio_pulsein_obj___exit__(size_t n_args, const mp_obj_t *args) { (void)n_args; - common_hal_nativeio_pulsein_deinit(args[0]); + common_hal_pulseio_pulsein_deinit(args[0]); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_pulsein___exit___obj, 4, 4, nativeio_pulsein_obj___exit__); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pulseio_pulsein___exit___obj, 4, 4, pulseio_pulsein_obj___exit__); //| .. method:: pause() //| //| Pause pulse capture //| -STATIC mp_obj_t nativeio_pulsein_obj_pause(mp_obj_t self_in) { - nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); +STATIC mp_obj_t pulseio_pulsein_obj_pause(mp_obj_t self_in) { + pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_pulsein_pause(self); + common_hal_pulseio_pulsein_pause(self); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulsein_pause_obj, nativeio_pulsein_obj_pause); +MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulsein_pause_obj, pulseio_pulsein_obj_pause); //| .. method:: resume(trigger_duration=0) //| @@ -154,8 +154,8 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulsein_pause_obj, nativeio_pulsein_obj_pause //| //| :param int trigger_duration: trigger pulse duration in microseconds //| -STATIC mp_obj_t nativeio_pulsein_obj_resume(mp_obj_t self_in, mp_obj_t duration_obj) { - nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); +STATIC mp_obj_t pulseio_pulsein_obj_resume(mp_obj_t self_in, mp_obj_t duration_obj) { + pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); uint16_t trigger_duration = 0; if (MP_OBJ_IS_SMALL_INT(duration_obj)) { trigger_duration = MP_OBJ_SMALL_INT_VALUE(duration_obj); @@ -163,47 +163,47 @@ STATIC mp_obj_t nativeio_pulsein_obj_resume(mp_obj_t self_in, mp_obj_t duration_ mp_raise_TypeError("trigger_duration must be int"); } - common_hal_nativeio_pulsein_resume(self, trigger_duration); + common_hal_pulseio_pulsein_resume(self, trigger_duration); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_2(nativeio_pulsein_resume_obj, nativeio_pulsein_obj_resume); +MP_DEFINE_CONST_FUN_OBJ_2(pulseio_pulsein_resume_obj, pulseio_pulsein_obj_resume); //| .. method:: clear() //| //| Clears all captured pulses //| -STATIC mp_obj_t nativeio_pulsein_obj_clear(mp_obj_t self_in) { - nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); +STATIC mp_obj_t pulseio_pulsein_obj_clear(mp_obj_t self_in) { + pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_pulsein_clear(self); + common_hal_pulseio_pulsein_clear(self); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulsein_clear_obj, nativeio_pulsein_obj_clear); +MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulsein_clear_obj, pulseio_pulsein_obj_clear); //| .. method:: popleft() //| //| Removes and returns the oldest read pulse. //| -STATIC mp_obj_t nativeio_pulsein_obj_popleft(mp_obj_t self_in) { - nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); - return MP_OBJ_NEW_SMALL_INT(common_hal_nativeio_pulsein_popleft(self)); +STATIC mp_obj_t pulseio_pulsein_obj_popleft(mp_obj_t self_in) { + pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pulsein_popleft(self)); } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulsein_popleft_obj, nativeio_pulsein_obj_popleft); +MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulsein_popleft_obj, pulseio_pulsein_obj_popleft); //| .. attribute:: maxlen //| //| Returns the maximum length of the PulseIn. When len() is equal to maxlen, //| it is unclear which pulses are active and which are idle. //| -STATIC mp_obj_t nativeio_pulsein_obj_get_maxlen(mp_obj_t self_in) { - nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); - return MP_OBJ_NEW_SMALL_INT(common_hal_nativeio_pulsein_get_maxlen(self)); +STATIC mp_obj_t pulseio_pulsein_obj_get_maxlen(mp_obj_t self_in) { + pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pulsein_get_maxlen(self)); } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulsein_get_maxlen_obj, nativeio_pulsein_obj_get_maxlen); +MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulsein_get_maxlen_obj, pulseio_pulsein_obj_get_maxlen); -const mp_obj_property_t nativeio_pulsein_maxlen_obj = { +const mp_obj_property_t pulseio_pulsein_maxlen_obj = { .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_pulsein_get_maxlen_obj, + .proxy = {(mp_obj_t)&pulseio_pulsein_get_maxlen_obj, (mp_obj_t)&mp_const_none_obj, (mp_obj_t)&mp_const_none_obj}, }; @@ -214,12 +214,12 @@ const mp_obj_property_t nativeio_pulsein_maxlen_obj = { //| //| This allows you to:: //| -//| pulses = nativeio.PulseIn(pin) +//| pulses = pulseio.PulseIn(pin) //| print(len(pulses)) //| STATIC mp_obj_t pulsein_unary_op(mp_uint_t op, mp_obj_t self_in) { - nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); - uint16_t len = common_hal_nativeio_pulsein_get_len(self); + pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + uint16_t len = common_hal_pulseio_pulsein_get_len(self); switch (op) { case MP_UNARY_OP_BOOL: return mp_obj_new_bool(len != 0); case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(len); @@ -233,7 +233,7 @@ STATIC mp_obj_t pulsein_unary_op(mp_uint_t op, mp_obj_t self_in) { //| //| This allows you to:: //| -//| pulses = nativeio.PulseIn(pin) +//| pulses = pulseio.PulseIn(pin) //| print(pulses[0]) //| STATIC mp_obj_t pulsein_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t value) { @@ -241,7 +241,7 @@ STATIC mp_obj_t pulsein_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t va // delete item mp_raise_AttributeError("Cannot delete values"); } else { - nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); if (MP_OBJ_IS_TYPE(index_obj, &mp_type_slice)) { mp_raise_NotImplementedError("Slices not supported"); } else { @@ -253,7 +253,7 @@ STATIC mp_obj_t pulsein_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t va } if (value == MP_OBJ_SENTINEL) { // load - return MP_OBJ_NEW_SMALL_INT(common_hal_nativeio_pulsein_get_item(self, index)); + return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pulsein_get_item(self, index)); } else { mp_raise_AttributeError("Read-only"); } @@ -262,24 +262,24 @@ STATIC mp_obj_t pulsein_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t va return mp_const_none; } -STATIC const mp_rom_map_elem_t nativeio_pulsein_locals_dict_table[] = { +STATIC const mp_rom_map_elem_t pulseio_pulsein_locals_dict_table[] = { // Methods - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_pulsein_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&pulseio_pulsein_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, - { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_pulsein___exit___obj) }, - { MP_ROM_QSTR(MP_QSTR_pause), MP_ROM_PTR(&nativeio_pulsein_pause_obj) }, - { MP_ROM_QSTR(MP_QSTR_resume), MP_ROM_PTR(&nativeio_pulsein_resume_obj) }, - { MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&nativeio_pulsein_clear_obj) }, - { MP_ROM_QSTR(MP_QSTR_popleft), MP_ROM_PTR(&nativeio_pulsein_popleft_obj) }, - { MP_ROM_QSTR(MP_QSTR_maxlen), MP_ROM_PTR(&nativeio_pulsein_maxlen_obj) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&pulseio_pulsein___exit___obj) }, + { MP_ROM_QSTR(MP_QSTR_pause), MP_ROM_PTR(&pulseio_pulsein_pause_obj) }, + { MP_ROM_QSTR(MP_QSTR_resume), MP_ROM_PTR(&pulseio_pulsein_resume_obj) }, + { MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&pulseio_pulsein_clear_obj) }, + { MP_ROM_QSTR(MP_QSTR_popleft), MP_ROM_PTR(&pulseio_pulsein_popleft_obj) }, + { MP_ROM_QSTR(MP_QSTR_maxlen), MP_ROM_PTR(&pulseio_pulsein_maxlen_obj) }, }; -STATIC MP_DEFINE_CONST_DICT(nativeio_pulsein_locals_dict, nativeio_pulsein_locals_dict_table); +STATIC MP_DEFINE_CONST_DICT(pulseio_pulsein_locals_dict, pulseio_pulsein_locals_dict_table); -const mp_obj_type_t nativeio_pulsein_type = { +const mp_obj_type_t pulseio_pulsein_type = { { &mp_type_type }, .name = MP_QSTR_PulseIn, - .make_new = nativeio_pulsein_make_new, + .make_new = pulseio_pulsein_make_new, .subscr = pulsein_subscr, .unary_op = pulsein_unary_op, - .locals_dict = (mp_obj_dict_t*)&nativeio_pulsein_locals_dict, + .locals_dict = (mp_obj_dict_t*)&pulseio_pulsein_locals_dict, }; diff --git a/shared-bindings/nativeio/PulseIn.h b/shared-bindings/pulseio/PulseIn.h similarity index 56% rename from shared-bindings/nativeio/PulseIn.h rename to shared-bindings/pulseio/PulseIn.h index cdb5e95c50..5ffab1d67d 100644 --- a/shared-bindings/nativeio/PulseIn.h +++ b/shared-bindings/pulseio/PulseIn.h @@ -24,23 +24,23 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PULSEIN_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PULSEIN_H__ +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PULSEIN_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PULSEIN_H__ #include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" +#include "common-hal/pulseio/PulseIn.h" -extern const mp_obj_type_t nativeio_pulsein_type; +extern const mp_obj_type_t pulseio_pulsein_type; -extern void common_hal_nativeio_pulsein_construct(nativeio_pulsein_obj_t* self, +extern void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, const mcu_pin_obj_t* pin, uint16_t maxlen, bool idle_state); -extern void common_hal_nativeio_pulsein_deinit(nativeio_pulsein_obj_t* self); -extern void common_hal_nativeio_pulsein_pause(nativeio_pulsein_obj_t* self); -extern void common_hal_nativeio_pulsein_resume(nativeio_pulsein_obj_t* self, uint16_t trigger_duration); -extern void common_hal_nativeio_pulsein_clear(nativeio_pulsein_obj_t* self); -extern uint16_t common_hal_nativeio_pulsein_popleft(nativeio_pulsein_obj_t* self); -extern uint16_t common_hal_nativeio_pulsein_get_maxlen(nativeio_pulsein_obj_t* self); -extern uint16_t common_hal_nativeio_pulsein_get_len(nativeio_pulsein_obj_t* self); -extern uint16_t common_hal_nativeio_pulsein_get_item(nativeio_pulsein_obj_t* self, int16_t index); +extern void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self); +extern void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self); +extern void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, uint16_t trigger_duration); +extern void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t* self); +extern uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self); +extern uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t* self); +extern uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t* self); +extern uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self, int16_t index); -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PULSEIN_H__ +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PULSEIN_H__ diff --git a/shared-bindings/nativeio/PulseOut.c b/shared-bindings/pulseio/PulseOut.c similarity index 63% rename from shared-bindings/nativeio/PulseOut.c rename to shared-bindings/pulseio/PulseOut.c index 79f189293f..20e04e8fba 100644 --- a/shared-bindings/nativeio/PulseOut.c +++ b/shared-bindings/pulseio/PulseOut.c @@ -30,10 +30,10 @@ #include "py/objproperty.h" #include "py/runtime.h" #include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/PulseOut.h" -#include "shared-bindings/nativeio/PWMOut.h" +#include "shared-bindings/pulseio/PulseOut.h" +#include "shared-bindings/pulseio/PWMOut.h" -//| .. currentmodule:: nativeio +//| .. currentmodule:: pulseio //| //| :class:`PulseOut` -- Output a pulse train //| ======================================================== @@ -46,16 +46,16 @@ //| //| Create a PulseOut object associated with the given PWM out experience. //| -//| :param ~nativeio.PWMOut carrier: PWMOut that is set to output on the desired pin. +//| :param ~pulseio.PWMOut carrier: PWMOut that is set to output on the desired pin. //| //| Send a short series of pulses:: //| //| import array -//| import nativeio +//| import pulseio //| import board //| -//| with nativeio.PWMOut(board.D13, duty_cycle=2 ** 15) as pwm: -//| pulse = nativeio.PulseOut(pwm) +//| with pulseio.PWMOut(board.D13, duty_cycle=2 ** 15) as pwm: +//| pulse = pulseio.PulseOut(pwm) //| # on off on off on //| pulses = array.array('h', [65000, 1000, 65000, 65000, 1000]) //| pulse.send(pulses) @@ -64,19 +64,19 @@ //| pulses[0] = 200 //| pulse.send(pulses) //| -STATIC mp_obj_t nativeio_pulseout_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { +STATIC mp_obj_t pulseio_pulseout_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 1, 1, true); mp_obj_t carrier_obj = args[0]; - if (!MP_OBJ_IS_TYPE(carrier_obj, &nativeio_pwmout_type)) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "Expected a %q", nativeio_pwmout_type.name)); + if (!MP_OBJ_IS_TYPE(carrier_obj, &pulseio_pwmout_type)) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "Expected a %q", pulseio_pwmout_type.name)); } // create Pulse object from the given pin - nativeio_pulseout_obj_t *self = m_new_obj(nativeio_pulseout_obj_t); - self->base.type = &nativeio_pulseout_type; + pulseio_pulseout_obj_t *self = m_new_obj(pulseio_pulseout_obj_t); + self->base.type = &pulseio_pulseout_type; - common_hal_nativeio_pulseout_construct(self, (nativeio_pwmout_obj_t *)MP_OBJ_TO_PTR(carrier_obj)); + common_hal_pulseio_pulseout_construct(self, (pulseio_pwmout_obj_t *)MP_OBJ_TO_PTR(carrier_obj)); return MP_OBJ_FROM_PTR(self); } @@ -85,12 +85,12 @@ STATIC mp_obj_t nativeio_pulseout_make_new(const mp_obj_type_t *type, size_t n_a //| //| Deinitialises the PulseOut and releases any hardware resources for reuse. //| -STATIC mp_obj_t nativeio_pulseout_deinit(mp_obj_t self_in) { - nativeio_pulseout_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_pulseout_deinit(self); +STATIC mp_obj_t pulseio_pulseout_deinit(mp_obj_t self_in) { + pulseio_pulseout_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_pulseio_pulseout_deinit(self); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulseout_deinit_obj, nativeio_pulseout_deinit); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulseout_deinit_obj, pulseio_pulseout_deinit); //| .. method:: __enter__() //| @@ -102,12 +102,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulseout_deinit_obj, nativeio_pulseout //| //| Automatically deinitializes the hardware when exiting a context. //| -STATIC mp_obj_t nativeio_pulseout_obj___exit__(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t pulseio_pulseout_obj___exit__(size_t n_args, const mp_obj_t *args) { (void)n_args; - common_hal_nativeio_pulseout_deinit(args[0]); + common_hal_pulseio_pulseout_deinit(args[0]); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_pulseout___exit___obj, 4, 4, nativeio_pulseout_obj___exit__); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pulseio_pulseout___exit___obj, 4, 4, pulseio_pulseout_obj___exit__); //| .. method:: send(pulses) //| @@ -120,31 +120,31 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_pulseout___exit___obj, 4, 4, //| //| :param array.array pulses: pulse durations in microseconds //| -STATIC mp_obj_t nativeio_pulseout_obj_send(mp_obj_t self_in, mp_obj_t pulses) { - nativeio_pulseout_obj_t *self = MP_OBJ_TO_PTR(self_in); +STATIC mp_obj_t pulseio_pulseout_obj_send(mp_obj_t self_in, mp_obj_t pulses) { + pulseio_pulseout_obj_t *self = MP_OBJ_TO_PTR(self_in); mp_buffer_info_t bufinfo; mp_get_buffer_raise(pulses, &bufinfo, MP_BUFFER_READ); if (bufinfo.typecode != 'H') { mp_raise_TypeError("Array must contain halfwords (type 'H')"); } - common_hal_nativeio_pulseout_send(self, (uint16_t *)bufinfo.buf, bufinfo.len / 2); + common_hal_pulseio_pulseout_send(self, (uint16_t *)bufinfo.buf, bufinfo.len / 2); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_2(nativeio_pulseout_send_obj, nativeio_pulseout_obj_send); +MP_DEFINE_CONST_FUN_OBJ_2(pulseio_pulseout_send_obj, pulseio_pulseout_obj_send); -STATIC const mp_rom_map_elem_t nativeio_pulseout_locals_dict_table[] = { +STATIC const mp_rom_map_elem_t pulseio_pulseout_locals_dict_table[] = { // Methods - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_pulseout_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&pulseio_pulseout_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, - { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_pulseout___exit___obj) }, - { MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&nativeio_pulseout_send_obj) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&pulseio_pulseout___exit___obj) }, + { MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&pulseio_pulseout_send_obj) }, }; -STATIC MP_DEFINE_CONST_DICT(nativeio_pulseout_locals_dict, nativeio_pulseout_locals_dict_table); +STATIC MP_DEFINE_CONST_DICT(pulseio_pulseout_locals_dict, pulseio_pulseout_locals_dict_table); -const mp_obj_type_t nativeio_pulseout_type = { +const mp_obj_type_t pulseio_pulseout_type = { { &mp_type_type }, .name = MP_QSTR_PulseOut, - .make_new = nativeio_pulseout_make_new, - .locals_dict = (mp_obj_dict_t*)&nativeio_pulseout_locals_dict, + .make_new = pulseio_pulseout_make_new, + .locals_dict = (mp_obj_dict_t*)&pulseio_pulseout_locals_dict, }; diff --git a/shared-bindings/nativeio/PulseOut.h b/shared-bindings/pulseio/PulseOut.h similarity index 68% rename from shared-bindings/nativeio/PulseOut.h rename to shared-bindings/pulseio/PulseOut.h index 23fb8efed9..5365fd1f55 100644 --- a/shared-bindings/nativeio/PulseOut.h +++ b/shared-bindings/pulseio/PulseOut.h @@ -24,18 +24,19 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PULSEOUT_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PULSEOUT_H__ +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PULSEOUT_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PULSEOUT_H__ #include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" +#include "common-hal/pulseio/PulseOut.h" +#include "common-hal/pulseio/PWMOut.h" -extern const mp_obj_type_t nativeio_pulseout_type; +extern const mp_obj_type_t pulseio_pulseout_type; -extern void common_hal_nativeio_pulseout_construct(nativeio_pulseout_obj_t* self, - const nativeio_pwmout_obj_t* carrier); -extern void common_hal_nativeio_pulseout_deinit(nativeio_pulseout_obj_t* self); -extern void common_hal_nativeio_pulseout_send(nativeio_pulseout_obj_t* self, +extern void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, + const pulseio_pwmout_obj_t* carrier); +extern void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self); +extern void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t* self, uint16_t* pulses, uint16_t len); -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PULSEOUT_H__ +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PULSEOUT_H__ diff --git a/shared-bindings/pulseio/__init__.c b/shared-bindings/pulseio/__init__.c new file mode 100644 index 0000000000..27d978ec58 --- /dev/null +++ b/shared-bindings/pulseio/__init__.c @@ -0,0 +1,87 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 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 + +#include "py/obj.h" +#include "py/runtime.h" + +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/pulseio/__init__.h" +#include "shared-bindings/pulseio/PulseIn.h" +#include "shared-bindings/pulseio/PulseOut.h" +#include "shared-bindings/pulseio/PWMOut.h" + +//| :mod:`pulseio` --- Support for pulse based protocols +//| ================================================= +//| +//| .. module:: pulseio +//| :synopsis: Support for pulse based protocols +//| :platform: SAMD21, ESP8266 +//| +//| The `pulseio` module contains classes to provide access to basic pulse IO. +//| +//| Libraries +//| +//| .. toctree:: +//| :maxdepth: 3 +//| +//| PulseIn +//| PulseOut +//| PWMOut +//| +//| All libraries change hardware state and should be deinitialized when they +//| are no longer needed. To do so, either call :py:meth:`!deinit` or use a +//| context manager. +//| +//| For example:: +//| +//| import pulseio +//| import time +//| from board import * +//| +//| with pulseio.PWMOut(D13) as pin: +//| pin.duty_cycle = 2 ** 15 +//| time.sleep(0.1) +//| +//| This example will initialize the the device, set +//| :py:data:`~pulseio.PWMOut.duty_cycle`, sleep 0.1 seconds and then +//| :py:meth:`~pulseio.PWMOut.deinit` the hardware. +//| + +STATIC const mp_rom_map_elem_t pulseio_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_pulseio) }, + { MP_ROM_QSTR(MP_QSTR_PulseIn), MP_ROM_PTR(&pulseio_pulsein_type) }, + { MP_ROM_QSTR(MP_QSTR_PulseOut), MP_ROM_PTR(&pulseio_pulseout_type) }, + { MP_ROM_QSTR(MP_QSTR_PWMOut), MP_ROM_PTR(&pulseio_pwmout_type) }, +}; + +STATIC MP_DEFINE_CONST_DICT(pulseio_module_globals, pulseio_module_globals_table); + +const mp_obj_module_t pulseio_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&pulseio_module_globals, +}; diff --git a/shared-bindings/pulseio/__init__.h b/shared-bindings/pulseio/__init__.h new file mode 100644 index 0000000000..26bb9f7b4e --- /dev/null +++ b/shared-bindings/pulseio/__init__.h @@ -0,0 +1,34 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft + * + * 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_PULSEIO___INIT___H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO___INIT___H__ + +#include "py/obj.h" + +// Nothing now. + +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO___INIT___H__ diff --git a/shared-bindings/nativeio/TouchIn.c b/shared-bindings/touchio/TouchIn.c similarity index 62% rename from shared-bindings/nativeio/TouchIn.c rename to shared-bindings/touchio/TouchIn.c index 7b391499c6..f4e82994ce 100644 --- a/shared-bindings/nativeio/TouchIn.c +++ b/shared-bindings/touchio/TouchIn.c @@ -33,19 +33,19 @@ #include "py/objproperty.h" #include "py/runtime.h" #include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/TouchIn.h" +#include "shared-bindings/touchio/TouchIn.h" -//| .. currentmodule:: nativeio +//| .. currentmodule:: touchio //| //| :class:`TouchIn` -- Read the state of a capacitive touch sensor //| =================================================================== //| //| Usage:: //| -//| import nativeio +//| import touchio //| from board import * //| -//| with nativeio.TouchIn(A1) as touch: +//| with touchio.TouchIn(A1) as touch: //| if touch.value: //| print("touched!") //| @@ -56,7 +56,7 @@ //| //| :param ~microcontroller.Pin pin: the pin to read from //| -STATIC mp_obj_t nativeio_touchin_make_new(const mp_obj_type_t *type, +STATIC mp_obj_t touchio_touchin_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { // check number of arguments mp_arg_check_num(n_args, n_kw, 1, 1, false); @@ -65,10 +65,10 @@ STATIC mp_obj_t nativeio_touchin_make_new(const mp_obj_type_t *type, mp_obj_t pin_obj = args[0]; assert_pin(pin_obj, false); - nativeio_touchin_obj_t *self = m_new_obj(nativeio_touchin_obj_t); - self->base.type = &nativeio_touchin_type; + touchio_touchin_obj_t *self = m_new_obj(touchio_touchin_obj_t); + self->base.type = &touchio_touchin_type; const mcu_pin_obj_t *pin = MP_OBJ_TO_PTR(pin_obj); - common_hal_nativeio_touchin_construct(self, pin); + common_hal_touchio_touchin_construct(self, pin); return (mp_obj_t) self; } @@ -77,12 +77,12 @@ STATIC mp_obj_t nativeio_touchin_make_new(const mp_obj_type_t *type, //| //| Deinitialises the TouchIn and releases any hardware resources for reuse. //| -STATIC mp_obj_t nativeio_touchin_deinit(mp_obj_t self_in) { - nativeio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_touchin_deinit(self); +STATIC mp_obj_t touchio_touchin_deinit(mp_obj_t self_in) { + touchio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_touchio_touchin_deinit(self); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_touchin_deinit_obj, nativeio_touchin_deinit); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(touchio_touchin_deinit_obj, touchio_touchin_deinit); //| .. method:: __enter__() //| @@ -94,12 +94,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_touchin_deinit_obj, nativeio_touchin_d //| //| Automatically deinitializes the hardware when exiting a context. //| -STATIC mp_obj_t nativeio_touchin_obj___exit__(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t touchio_touchin_obj___exit__(size_t n_args, const mp_obj_t *args) { (void)n_args; - common_hal_nativeio_touchin_deinit(args[0]); + common_hal_touchio_touchin_deinit(args[0]); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_touchin___exit___obj, 4, 4, nativeio_touchin_obj___exit__); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(touchio_touchin___exit___obj, 4, 4, touchio_touchin_obj___exit__); //| .. attribute:: value //| @@ -108,32 +108,32 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_touchin___exit___obj, 4, 4, //| :return: True when touched, False otherwise. //| :rtype: bool //| -STATIC mp_obj_t nativeio_touchin_obj_get_value(mp_obj_t self_in) { - nativeio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in); - return mp_obj_new_bool(common_hal_nativeio_touchin_get_value(self)); +STATIC mp_obj_t touchio_touchin_obj_get_value(mp_obj_t self_in) { + touchio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in); + return mp_obj_new_bool(common_hal_touchio_touchin_get_value(self)); } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_touchin_get_value_obj, nativeio_touchin_obj_get_value); +MP_DEFINE_CONST_FUN_OBJ_1(touchio_touchin_get_value_obj, touchio_touchin_obj_get_value); -const mp_obj_property_t nativeio_touchin_value_obj = { +const mp_obj_property_t touchio_touchin_value_obj = { .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_touchin_get_value_obj, + .proxy = {(mp_obj_t)&touchio_touchin_get_value_obj, (mp_obj_t)&mp_const_none_obj, (mp_obj_t)&mp_const_none_obj}, }; -STATIC const mp_rom_map_elem_t nativeio_touchin_locals_dict_table[] = { +STATIC const mp_rom_map_elem_t touchio_touchin_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, - { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_touchin___exit___obj) }, - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_touchin_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&touchio_touchin___exit___obj) }, + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&touchio_touchin_deinit_obj) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_value), MP_ROM_PTR(&nativeio_touchin_value_obj)}, + { MP_OBJ_NEW_QSTR(MP_QSTR_value), MP_ROM_PTR(&touchio_touchin_value_obj)}, }; -STATIC MP_DEFINE_CONST_DICT(nativeio_touchin_locals_dict, nativeio_touchin_locals_dict_table); +STATIC MP_DEFINE_CONST_DICT(touchio_touchin_locals_dict, touchio_touchin_locals_dict_table); -const mp_obj_type_t nativeio_touchin_type = { +const mp_obj_type_t touchio_touchin_type = { { &mp_type_type }, .name = MP_QSTR_TouchIn, - .make_new = nativeio_touchin_make_new, - .locals_dict = (mp_obj_t)&nativeio_touchin_locals_dict, + .make_new = touchio_touchin_make_new, + .locals_dict = (mp_obj_t)&touchio_touchin_locals_dict, }; diff --git a/shared-bindings/nativeio/TouchIn.h b/shared-bindings/touchio/TouchIn.h similarity index 70% rename from shared-bindings/nativeio/TouchIn.h rename to shared-bindings/touchio/TouchIn.h index b22f655e57..04a72835ed 100644 --- a/shared-bindings/nativeio/TouchIn.h +++ b/shared-bindings/touchio/TouchIn.h @@ -24,16 +24,16 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_TOUCHIN_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_TOUCHIN_H__ +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_TOUCHIO_TOUCHIN_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_TOUCHIO_TOUCHIN_H__ #include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" +#include "common-hal/touchio/TouchIn.h" -extern const mp_obj_type_t nativeio_touchin_type; +extern const mp_obj_type_t touchio_touchin_type; -void common_hal_nativeio_touchin_construct(nativeio_touchin_obj_t* self, const mcu_pin_obj_t *pin); -void common_hal_nativeio_touchin_deinit(nativeio_touchin_obj_t* self); -bool common_hal_nativeio_touchin_get_value(nativeio_touchin_obj_t *self); +void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self, const mcu_pin_obj_t *pin); +void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t* self); +bool common_hal_touchio_touchin_get_value(touchio_touchin_obj_t *self); -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_TOUCHIN_H__ +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_TOUCHIO_TOUCHIN_H__ diff --git a/shared-bindings/touchio/__init__.c b/shared-bindings/touchio/__init__.c new file mode 100644 index 0000000000..a930c1af70 --- /dev/null +++ b/shared-bindings/touchio/__init__.c @@ -0,0 +1,82 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 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 + +#include "py/obj.h" +#include "py/runtime.h" + +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/touchio/__init__.h" +#include "shared-bindings/touchio/TouchIn.h" + +#include "py/runtime.h" + +//| :mod:`touchio` --- Touch related IO +//| ================================================= +//| +//| .. module:: touchio +//| :synopsis: Hardware accelerated behavior +//| :platform: SAMD21 +//| +//| The `touchio` module contains classes to provide access to touch IO typically +//| accelerated by hardware on the onboard microcontroller. +//| +//| Libraries +//| +//| .. toctree:: +//| :maxdepth: 3 +//| +//| TouchIn +//| +//| All libraries change hardware state and should be deinitialized when they +//| are no longer needed. To do so, either call :py:meth:`!deinit` or use a +//| context manager. +//| +//| For example:: +//| +//| import touchio +//| from board import * +//| +//| with touchio.TouchIn(D6) as touch_pin: +//| print(touch_pin.value) +//| +//| This example will initialize the the device, run +//| :py:data:`~touchio.TouchIn.value` and then :py:meth:`~touchio.TouchIn.deinit` +//| the hardware. +//| + +STATIC const mp_rom_map_elem_t touchio_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_touchio) }, + { MP_ROM_QSTR(MP_QSTR_TouchIn), MP_ROM_PTR(&touchio_touchin_type) }, +}; + +STATIC MP_DEFINE_CONST_DICT(touchio_module_globals, touchio_module_globals_table); + +const mp_obj_module_t touchio_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&touchio_module_globals, +}; diff --git a/shared-bindings/touchio/__init__.h b/shared-bindings/touchio/__init__.h new file mode 100644 index 0000000000..fc40e462b5 --- /dev/null +++ b/shared-bindings/touchio/__init__.h @@ -0,0 +1,34 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft + * + * 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_TOUCHIO___INIT___H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_TOUCHIO___INIT___H__ + +#include "py/obj.h" + +// Nothing now. + +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_TOUCHIO___INIT___H__ diff --git a/shared-bindings/usb_hid/Device.h b/shared-bindings/usb_hid/Device.h index 08713874e1..234d7ebc2e 100644 --- a/shared-bindings/usb_hid/Device.h +++ b/shared-bindings/usb_hid/Device.h @@ -24,8 +24,8 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGIN_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGIN_H__ +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_USB_HID_DEVICE_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_USB_HID_DEVICE_H__ #include "common-hal/usb_hid/types.h" @@ -35,4 +35,4 @@ void common_hal_usb_hid_device_send_report(usb_hid_device_obj_t *self, uint8_t* uint8_t common_hal_usb_hid_device_get_usage_page(usb_hid_device_obj_t *self); uint8_t common_hal_usb_hid_device_get_usage(usb_hid_device_obj_t *self); -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGIN_H__ +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_USB_HID_DEVICE_H__ diff --git a/shared-bindings/usb_hid/__init__.h b/shared-bindings/usb_hid/__init__.h index 576a3301b8..ff478d0836 100644 --- a/shared-bindings/usb_hid/__init__.h +++ b/shared-bindings/usb_hid/__init__.h @@ -30,8 +30,6 @@ #include #include -#include "common-hal/nativeio/types.h" - // TODO(tannewt): Make this a mp_obj_tuple_t when it is dynamically allocated. // until then we hard code it to two entries so LTO is happy. extern mp_obj_tuple2_t common_hal_usb_hid_devices; diff --git a/shared-module/bitbangio/I2C.c b/shared-module/bitbangio/I2C.c index 422f7778dc..4c47629c75 100644 --- a/shared-module/bitbangio/I2C.c +++ b/shared-module/bitbangio/I2C.c @@ -31,7 +31,7 @@ #include "common-hal/microcontroller/types.h" #include "shared-bindings/microcontroller/__init__.h" -#include "shared-bindings/nativeio/DigitalInOut.h" +#include "shared-bindings/digitalio/DigitalInOut.h" #include "shared-module/bitbangio/types.h" #define I2C_STRETCH_LIMIT 255 @@ -43,30 +43,30 @@ STATIC void delay(bitbangio_i2c_obj_t *self) { } STATIC void scl_low(bitbangio_i2c_obj_t *self) { - common_hal_nativeio_digitalinout_set_value(&self->scl, false); + common_hal_digitalio_digitalinout_set_value(&self->scl, false); } STATIC void scl_release(bitbangio_i2c_obj_t *self) { - common_hal_nativeio_digitalinout_set_value(&self->scl, true); + common_hal_digitalio_digitalinout_set_value(&self->scl, true); delay(self); // For clock stretching, wait for the SCL pin to be released, with timeout. - for (int count = I2C_STRETCH_LIMIT; !common_hal_nativeio_digitalinout_get_value(&self->scl) && count; --count) { + for (int count = I2C_STRETCH_LIMIT; !common_hal_digitalio_digitalinout_get_value(&self->scl) && count; --count) { common_hal_mcu_delay_us(1); } } STATIC void sda_low(bitbangio_i2c_obj_t *self) { - common_hal_nativeio_digitalinout_set_value(&self->sda, false); + common_hal_digitalio_digitalinout_set_value(&self->sda, false); } STATIC void sda_release(bitbangio_i2c_obj_t *self) { - common_hal_nativeio_digitalinout_set_value(&self->sda, true); + common_hal_digitalio_digitalinout_set_value(&self->sda, true); } STATIC bool sda_read(bitbangio_i2c_obj_t *self) { - common_hal_nativeio_digitalinout_switch_to_input(&self->sda, PULL_UP); - bool value = common_hal_nativeio_digitalinout_get_value(&self->sda); - common_hal_nativeio_digitalinout_switch_to_output(&self->sda, true, DRIVE_MODE_OPEN_DRAIN); + common_hal_digitalio_digitalinout_switch_to_input(&self->sda, PULL_UP); + bool value = common_hal_digitalio_digitalinout_get_value(&self->sda); + common_hal_digitalio_digitalinout_switch_to_output(&self->sda, true, DRIVE_MODE_OPEN_DRAIN); return value; } @@ -147,24 +147,24 @@ void shared_module_bitbangio_i2c_construct(bitbangio_i2c_obj_t *self, if (self->us_delay == 0) { self->us_delay = 1; } - digitalinout_result_t result = common_hal_nativeio_digitalinout_construct(&self->scl, scl); + digitalinout_result_t result = common_hal_digitalio_digitalinout_construct(&self->scl, scl); if (result != DIGITALINOUT_OK) { return; } - result = common_hal_nativeio_digitalinout_construct(&self->sda, sda); + result = common_hal_digitalio_digitalinout_construct(&self->sda, sda); if (result != DIGITALINOUT_OK) { - common_hal_nativeio_digitalinout_deinit(&self->scl); + common_hal_digitalio_digitalinout_deinit(&self->scl); return; } - common_hal_nativeio_digitalinout_switch_to_output(&self->scl, true, DRIVE_MODE_OPEN_DRAIN); - common_hal_nativeio_digitalinout_switch_to_output(&self->sda, true, DRIVE_MODE_OPEN_DRAIN); + common_hal_digitalio_digitalinout_switch_to_output(&self->scl, true, DRIVE_MODE_OPEN_DRAIN); + common_hal_digitalio_digitalinout_switch_to_output(&self->sda, true, DRIVE_MODE_OPEN_DRAIN); stop(self); } void shared_module_bitbangio_i2c_deinit(bitbangio_i2c_obj_t *self) { - common_hal_nativeio_digitalinout_deinit(&self->scl); - common_hal_nativeio_digitalinout_deinit(&self->sda); + common_hal_digitalio_digitalinout_deinit(&self->scl); + common_hal_digitalio_digitalinout_deinit(&self->sda); } bool shared_module_bitbangio_i2c_try_lock(bitbangio_i2c_obj_t *self) { diff --git a/shared-module/bitbangio/OneWire.c b/shared-module/bitbangio/OneWire.c index 5f45b3bafc..b78acc8ecf 100644 --- a/shared-module/bitbangio/OneWire.c +++ b/shared-module/bitbangio/OneWire.c @@ -27,28 +27,28 @@ #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-bindings/digitalio/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); + self->pin.base.type = &digitalio_digitalinout_type; + common_hal_digitalio_digitalinout_construct(&self->pin, pin); } void shared_module_bitbangio_onewire_deinit(bitbangio_onewire_obj_t* self) { - common_hal_nativeio_digitalinout_deinit(&self->pin); + common_hal_digitalio_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_digitalio_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_digitalio_digitalinout_switch_to_input(&self->pin, PULL_NONE); common_hal_mcu_delay_us(70); - bool value = common_hal_nativeio_digitalinout_get_value(&self->pin); + bool value = common_hal_digitalio_digitalinout_get_value(&self->pin); common_hal_mcu_delay_us(410); common_hal_mcu_enable_interrupts(); return value; @@ -56,14 +56,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_nativeio_digitalinout_switch_to_output(&self->pin, false, DRIVE_MODE_OPEN_DRAIN); + common_hal_digitalio_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); + 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); - bool value = common_hal_nativeio_digitalinout_get_value(&self->pin); + bool value = common_hal_digitalio_digitalinout_get_value(&self->pin); common_hal_mcu_delay_us(55); common_hal_mcu_enable_interrupts(); return value; @@ -72,9 +72,9 @@ bool shared_module_bitbangio_onewire_read_bit(bitbangio_onewire_obj_t* self) { 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_digitalio_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_digitalio_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/SPI.c b/shared-module/bitbangio/SPI.c index 5e8d73122d..be0deda8ff 100644 --- a/shared-module/bitbangio/SPI.c +++ b/shared-module/bitbangio/SPI.c @@ -31,7 +31,7 @@ #include "common-hal/microcontroller/types.h" #include "shared-bindings/microcontroller/__init__.h" -#include "shared-bindings/nativeio/DigitalInOut.h" +#include "shared-bindings/digitalio/DigitalInOut.h" #include "shared-module/bitbangio/types.h" #define MAX_BAUDRATE (common_hal_mcu_get_clock_frequency() / 48) @@ -39,26 +39,26 @@ void shared_module_bitbangio_spi_construct(bitbangio_spi_obj_t *self, const mcu_pin_obj_t * clock, const mcu_pin_obj_t * mosi, const mcu_pin_obj_t * miso) { - digitalinout_result_t result = common_hal_nativeio_digitalinout_construct(&self->clock, clock); + digitalinout_result_t result = common_hal_digitalio_digitalinout_construct(&self->clock, clock); if (result != DIGITALINOUT_OK) { nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Clock pin init failed.")); } if (mosi != mp_const_none) { - result = common_hal_nativeio_digitalinout_construct(&self->mosi, mosi); + result = common_hal_digitalio_digitalinout_construct(&self->mosi, mosi); if (result != DIGITALINOUT_OK) { - common_hal_nativeio_digitalinout_deinit(&self->clock); + common_hal_digitalio_digitalinout_deinit(&self->clock); nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "MOSI pin init failed.")); } self->has_mosi = true; } if (miso != mp_const_none) { - result = common_hal_nativeio_digitalinout_construct(&self->miso, miso); + result = common_hal_digitalio_digitalinout_construct(&self->miso, miso); if (result != DIGITALINOUT_OK) { - common_hal_nativeio_digitalinout_deinit(&self->clock); + common_hal_digitalio_digitalinout_deinit(&self->clock); if (mosi != mp_const_none) { - common_hal_nativeio_digitalinout_deinit(&self->mosi); + common_hal_digitalio_digitalinout_deinit(&self->mosi); } nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "MISO pin init failed.")); @@ -71,12 +71,12 @@ void shared_module_bitbangio_spi_construct(bitbangio_spi_obj_t *self, } void shared_module_bitbangio_spi_deinit(bitbangio_spi_obj_t *self) { - common_hal_nativeio_digitalinout_deinit(&self->clock); + common_hal_digitalio_digitalinout_deinit(&self->clock); if (self->has_mosi) { - common_hal_nativeio_digitalinout_deinit(&self->mosi); + common_hal_digitalio_digitalinout_deinit(&self->mosi); } if (self->has_miso) { - common_hal_nativeio_digitalinout_deinit(&self->miso); + common_hal_digitalio_digitalinout_deinit(&self->miso); } } @@ -129,9 +129,9 @@ bool shared_module_bitbangio_spi_write(bitbangio_spi_obj_t *self, const uint8_t for (size_t i = 0; i < len; ++i) { uint8_t data_out = data[i]; for (int j = 0; j < 8; ++j, data_out <<= 1) { - common_hal_nativeio_digitalinout_set_value(&self->mosi, (data_out >> 7) & 1); - common_hal_nativeio_digitalinout_set_value(&self->clock, 1 - self->polarity); - common_hal_nativeio_digitalinout_set_value(&self->clock, self->polarity); + common_hal_digitalio_digitalinout_set_value(&self->mosi, (data_out >> 7) & 1); + common_hal_digitalio_digitalinout_set_value(&self->clock, 1 - self->polarity); + common_hal_digitalio_digitalinout_set_value(&self->clock, self->polarity); } if (dest != NULL) { dest[i] = data_in; @@ -144,16 +144,16 @@ bool shared_module_bitbangio_spi_write(bitbangio_spi_obj_t *self, const uint8_t for (size_t i = 0; i < len; ++i) { uint8_t data_out = data[i]; for (int j = 0; j < 8; ++j, data_out <<= 1) { - common_hal_nativeio_digitalinout_set_value(&self->mosi, (data_out >> 7) & 1); + common_hal_digitalio_digitalinout_set_value(&self->mosi, (data_out >> 7) & 1); if (self->phase == 0) { common_hal_mcu_delay_us(delay_half); - common_hal_nativeio_digitalinout_set_value(&self->clock, 1 - self->polarity); + common_hal_digitalio_digitalinout_set_value(&self->clock, 1 - self->polarity); common_hal_mcu_delay_us(delay_half); - common_hal_nativeio_digitalinout_set_value(&self->clock, self->polarity); + common_hal_digitalio_digitalinout_set_value(&self->clock, self->polarity); } else { - common_hal_nativeio_digitalinout_set_value(&self->clock, 1 - self->polarity); + common_hal_digitalio_digitalinout_set_value(&self->clock, 1 - self->polarity); common_hal_mcu_delay_us(delay_half); - common_hal_nativeio_digitalinout_set_value(&self->clock, self->polarity); + common_hal_digitalio_digitalinout_set_value(&self->clock, self->polarity); common_hal_mcu_delay_us(delay_half); } } @@ -185,14 +185,14 @@ bool shared_module_bitbangio_spi_read(bitbangio_spi_obj_t *self, uint8_t *data, if (delay_half <= MICROPY_PY_MACHINE_SPI_MIN_DELAY) { // Clock out zeroes while we read. if (self->has_mosi) { - common_hal_nativeio_digitalinout_set_value(&self->mosi, false); + common_hal_digitalio_digitalinout_set_value(&self->mosi, false); } for (size_t i = 0; i < len; ++i) { uint8_t data_in = 0; for (int j = 0; j < 8; ++j, data_out <<= 1) { - common_hal_nativeio_digitalinout_set_value(&self->clock, 1 - self->polarity); - data_in = (data_in << 1) | common_hal_nativeio_digitalinout_get_value(&self->miso); - common_hal_nativeio_digitalinout_set_value(&self->clock, self->polarity); + common_hal_digitalio_digitalinout_set_value(&self->clock, 1 - self->polarity); + data_in = (data_in << 1) | common_hal_digitalio_digitalinout_get_value(&self->miso); + common_hal_digitalio_digitalinout_set_value(&self->clock, self->polarity); } data[i] = data_in; } @@ -200,24 +200,24 @@ bool shared_module_bitbangio_spi_read(bitbangio_spi_obj_t *self, uint8_t *data, } #endif if (self->has_mosi) { - common_hal_nativeio_digitalinout_set_value(&self->mosi, false); + common_hal_digitalio_digitalinout_set_value(&self->mosi, false); } for (size_t i = 0; i < len; ++i) { uint8_t data_in = 0; for (int j = 0; j < 8; ++j) { if (self->phase == 0) { common_hal_mcu_delay_us(delay_half); - common_hal_nativeio_digitalinout_set_value(&self->clock, 1 - self->polarity); + common_hal_digitalio_digitalinout_set_value(&self->clock, 1 - self->polarity); } else { - common_hal_nativeio_digitalinout_set_value(&self->clock, 1 - self->polarity); + common_hal_digitalio_digitalinout_set_value(&self->clock, 1 - self->polarity); common_hal_mcu_delay_us(delay_half); } - data_in = (data_in << 1) | common_hal_nativeio_digitalinout_get_value(&self->miso); + data_in = (data_in << 1) | common_hal_digitalio_digitalinout_get_value(&self->miso); if (self->phase == 0) { common_hal_mcu_delay_us(delay_half); - common_hal_nativeio_digitalinout_set_value(&self->clock, self->polarity); + common_hal_digitalio_digitalinout_set_value(&self->clock, self->polarity); } else { - common_hal_nativeio_digitalinout_set_value(&self->clock, self->polarity); + common_hal_digitalio_digitalinout_set_value(&self->clock, self->polarity); common_hal_mcu_delay_us(delay_half); } } diff --git a/shared-module/bitbangio/types.h b/shared-module/bitbangio/types.h index 8fd4a48c4c..46723a9585 100644 --- a/shared-module/bitbangio/types.h +++ b/shared-module/bitbangio/types.h @@ -27,28 +27,28 @@ #ifndef __MICROPY_INCLUDED_SHARED_MODULE_BITBANGIO_TYPES_H__ #define __MICROPY_INCLUDED_SHARED_MODULE_BITBANGIO_TYPES_H__ -#include "common-hal/nativeio/DigitalInOut.h" +#include "common-hal/digitalio/DigitalInOut.h" #include "py/obj.h" typedef struct { mp_obj_base_t base; - nativeio_digitalinout_obj_t scl; - nativeio_digitalinout_obj_t sda; + digitalio_digitalinout_obj_t scl; + digitalio_digitalinout_obj_t sda; uint32_t us_delay; volatile bool locked; } bitbangio_i2c_obj_t; typedef struct { mp_obj_base_t base; - nativeio_digitalinout_obj_t pin; + digitalio_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; + digitalio_digitalinout_obj_t clock; + digitalio_digitalinout_obj_t mosi; + digitalio_digitalinout_obj_t miso; uint32_t delay_half; bool has_miso:1; bool has_mosi:1; diff --git a/shared-module/nativeio/I2C.c b/shared-module/busio/I2C.c similarity index 77% rename from shared-module/nativeio/I2C.c rename to shared-module/busio/I2C.c index 345a3825a3..f8f154e7ff 100644 --- a/shared-module/nativeio/I2C.c +++ b/shared-module/busio/I2C.c @@ -24,43 +24,43 @@ * THE SOFTWARE. */ -#include "shared-bindings/nativeio/I2C.h" +#include "shared-bindings/busio/I2C.h" #include "shared-bindings/bitbangio/I2C.h" #include "py/mperrno.h" #include "py/nlr.h" -void common_hal_nativeio_i2c_construct(nativeio_i2c_obj_t *self, +void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, const mcu_pin_obj_t* scl, const mcu_pin_obj_t* sda, uint32_t freq) { shared_module_bitbangio_i2c_construct(&self->bitbang, scl, sda, freq); } -void common_hal_nativeio_i2c_deinit(nativeio_i2c_obj_t *self) { +void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) { shared_module_bitbangio_i2c_deinit(&self->bitbang); } -bool common_hal_nativeio_i2c_probe(nativeio_i2c_obj_t *self, uint8_t addr) { +bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) { return shared_module_bitbangio_i2c_probe(&self->bitbang, addr); } -bool common_hal_nativeio_i2c_try_lock(nativeio_i2c_obj_t *self) { +bool common_hal_busio_i2c_try_lock(busio_i2c_obj_t *self) { return shared_module_bitbangio_i2c_try_lock(&self->bitbang); } -bool common_hal_nativeio_i2c_has_lock(nativeio_i2c_obj_t *self) { +bool common_hal_busio_i2c_has_lock(busio_i2c_obj_t *self) { return shared_module_bitbangio_i2c_has_lock(&self->bitbang); } -void common_hal_nativeio_i2c_unlock(nativeio_i2c_obj_t *self) { +void common_hal_busio_i2c_unlock(busio_i2c_obj_t *self) { shared_module_bitbangio_i2c_unlock(&self->bitbang); } -uint8_t common_hal_nativeio_i2c_write(nativeio_i2c_obj_t *self, uint16_t addr, +uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addr, const uint8_t * data, size_t len, bool transmit_stop_bit) { return shared_module_bitbangio_i2c_write(&self->bitbang, addr, data, len, transmit_stop_bit); } -uint8_t common_hal_nativeio_i2c_read(nativeio_i2c_obj_t *self, uint16_t addr, +uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t addr, uint8_t * data, size_t len) { return shared_module_bitbangio_i2c_read(&self->bitbang, addr, data, len); } diff --git a/shared-module/nativeio/I2C.h b/shared-module/busio/I2C.h similarity index 79% rename from shared-module/nativeio/I2C.h rename to shared-module/busio/I2C.h index b26ba4642c..9706124c27 100644 --- a/shared-module/nativeio/I2C.h +++ b/shared-module/busio/I2C.h @@ -24,11 +24,8 @@ * 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_I2C_H__ -#define __MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_NATIVEIO_I2C_H__ +#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_BUSIO_I2C_H__ +#define __MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_BUSIO_I2C_H__ #include "shared-module/bitbangio/types.h" @@ -37,6 +34,6 @@ typedef struct { mp_obj_base_t base; bitbangio_i2c_obj_t bitbang; -} nativeio_i2c_obj_t; +} busio_i2c_obj_t; -#endif // __MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_NATIVEIO_I2C_H__ +#endif // __MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_BUSIO_I2C_H__ diff --git a/shared-module/nativeio/OneWire.c b/shared-module/busio/OneWire.c similarity index 78% rename from shared-module/nativeio/OneWire.c rename to shared-module/busio/OneWire.c index 490f9b1095..0e3c2c3840 100644 --- a/shared-module/nativeio/OneWire.c +++ b/shared-module/busio/OneWire.c @@ -24,29 +24,29 @@ * THE SOFTWARE. */ -// Wraps the bitbangio implementation of OneWire for use in nativeio. +// Wraps the bitbangio implementation of OneWire for use in busio. #include "common-hal/microcontroller/types.h" #include "shared-bindings/bitbangio/OneWire.h" -#include "common-hal/nativeio/types.h" +#include "shared-module/busio/OneWire.h" -void common_hal_nativeio_onewire_construct(nativeio_onewire_obj_t* self, +void common_hal_busio_onewire_construct(busio_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) { +void common_hal_busio_onewire_deinit(busio_onewire_obj_t* self) { shared_module_bitbangio_onewire_deinit(&self->bitbang); } -bool common_hal_nativeio_onewire_reset(nativeio_onewire_obj_t* self) { +bool common_hal_busio_onewire_reset(busio_onewire_obj_t* self) { return shared_module_bitbangio_onewire_reset(&self->bitbang); } -bool common_hal_nativeio_onewire_read_bit(nativeio_onewire_obj_t* self) { +bool common_hal_busio_onewire_read_bit(busio_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, +void common_hal_busio_onewire_write_bit(busio_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/busio/OneWire.h similarity index 78% rename from shared-module/nativeio/OneWire.h rename to shared-module/busio/OneWire.h index 3704c55a39..9404a7206e 100644 --- a/shared-module/nativeio/OneWire.h +++ b/shared-module/busio/OneWire.h @@ -24,11 +24,8 @@ * 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__ +#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_BUSIO_ONEWIRE_H__ +#define __MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_BUSIO_ONEWIRE_H__ #include "shared-module/bitbangio/types.h" @@ -37,6 +34,6 @@ typedef struct { mp_obj_base_t base; bitbangio_onewire_obj_t bitbang; -} nativeio_onewire_obj_t; +} busio_onewire_obj_t; -#endif // __MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_NATIVEIO_ONEWIRE_H__ +#endif // __MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_BUSIO_ONEWIRE_H__