From 9939c59caa4e13011e9d1f63079cb4a7942cfcc4 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 2 Mar 2021 15:16:55 -0500 Subject: [PATCH] wip --- ports/raspberrypi/common-hal/busio/I2C.c | 36 ++++++++-------- ports/raspberrypi/common-hal/busio/I2C.h | 6 +-- .../common-hal/digitalio/DigitalInOut.c | 30 +++++++++---- shared-bindings/bitbangio/I2C.h | 2 +- shared-bindings/bitbangio/OneWire.h | 2 +- shared-bindings/bitbangio/SPI.h | 2 +- shared-bindings/bitbangio/__init__.c | 1 - shared-module/bitbangio/I2C.c | 1 - shared-module/bitbangio/I2C.h | 43 +++++++++++++++++++ shared-module/bitbangio/OneWire.c | 1 - shared-module/bitbangio/OneWire.h | 39 +++++++++++++++++ shared-module/bitbangio/SPI.c | 4 +- shared-module/bitbangio/{types.h => SPI.h} | 20 ++------- shared-module/busio/I2C.h | 2 +- shared-module/busio/OneWire.h | 2 +- supervisor/shared/rgb_led_status.c | 1 - 16 files changed, 133 insertions(+), 59 deletions(-) create mode 100644 shared-module/bitbangio/I2C.h create mode 100644 shared-module/bitbangio/OneWire.h rename shared-module/bitbangio/{types.h => SPI.h} (76%) diff --git a/ports/raspberrypi/common-hal/busio/I2C.c b/ports/raspberrypi/common-hal/busio/I2C.c index e4f4033cf4..970b6de896 100644 --- a/ports/raspberrypi/common-hal/busio/I2C.c +++ b/ports/raspberrypi/common-hal/busio/I2C.c @@ -24,6 +24,7 @@ * THE SOFTWARE. */ +#include "py/mperrno.h" #include "py/mphal.h" #include "shared-bindings/busio/I2C.h" #include "py/runtime.h" @@ -94,17 +95,17 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, } #endif + // Create a bitbangio.I2C object to do short writes. + // Must be done before setting up the I2C pins, since they will be + // set up as GPIO by the bitbangio.I2C object. + shared_module_bitbangio_i2c_construct(&self->bitbangio_i2c, scl, sda, + frequency, timeout); + gpio_set_function(sda->number, GPIO_FUNC_I2C); gpio_set_function(scl->number, GPIO_FUNC_I2C); self->baudrate = i2c_init(self->peripheral, frequency); - // Remember these in case we need to use bitbangio to do short writes. - self->sda = sda; - self->scl = scl; - self->frequency = frequency; - self->timeout = timeout; - self->sda_pin = sda->number; self->scl_pin = scl->number; claim_pin(sda); @@ -130,7 +131,7 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) { } bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) { - return common_hal_busio_i2c_write(self, addr, NULL, 0, false) == 0; + return common_hal_busio_i2c_write(self, addr, NULL, 0, true) == 0; } bool common_hal_busio_i2c_try_lock(busio_i2c_obj_t *self) { @@ -153,20 +154,19 @@ void common_hal_busio_i2c_unlock(busio_i2c_obj_t *self) { 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) { if (len <= 2) { - // Give up pin ownership temporarily and use a bitbangio.I2C to do the write. + // Use a bitbangio.I2C to do the write. - reset_pin_number(self->sda_pin); - reset_pin_number(self->scl_pin); - bitbangio_i2c_obj_t bitbangio_i2c; - shared_module_bitbangio_i2c_construct(&bitbangio_i2c, self->sda, self->scl, - self->frequency, self->timeout); - uint8_t status = shared_module_bitbangio_i2c_write(&bitbangio_i2c, addr, data, len, transmit_stop_bit); - shared_module_bitbangio_i2c_deinit(&bitbangio_i2c); + gpio_set_function(self->sda_pin, GPIO_FUNC_SIO); + gpio_set_function(self->scl_pin, GPIO_FUNC_SIO); + gpio_set_dir(self->sda_pin, GPIO_IN); + gpio_put(self->sda_pin, true); + gpio_set_dir(self->scl_pin, GPIO_IN); + gpio_put(self->scl_pin, true); - // Take back the pins and restore them to hardware I2C functionality. - claim_pin(self->sda); - claim_pin(self->scl); + uint8_t status = shared_module_bitbangio_i2c_write(&self->bitbangio_i2c, + addr, data, len, transmit_stop_bit); + mp_hal_delay_us(20); gpio_set_function(self->sda_pin, GPIO_FUNC_I2C); gpio_set_function(self->scl_pin, GPIO_FUNC_I2C); diff --git a/ports/raspberrypi/common-hal/busio/I2C.h b/ports/raspberrypi/common-hal/busio/I2C.h index efa31ce635..651054a048 100644 --- a/ports/raspberrypi/common-hal/busio/I2C.h +++ b/ports/raspberrypi/common-hal/busio/I2C.h @@ -28,6 +28,7 @@ #define MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_BUSIO_I2C_H #include "common-hal/microcontroller/Pin.h" +#include "shared-module/bitbangio/I2C.h" #include "py/obj.h" @@ -36,10 +37,7 @@ typedef struct { mp_obj_base_t base; i2c_inst_t * peripheral; - const mcu_pin_obj_t *scl; - const mcu_pin_obj_t *sda; - uint32_t frequency; - uint32_t timeout; + bitbangio_i2c_obj_t bitbangio_i2c; bool has_lock; uint baudrate; uint8_t scl_pin; diff --git a/ports/raspberrypi/common-hal/digitalio/DigitalInOut.c b/ports/raspberrypi/common-hal/digitalio/DigitalInOut.c index b0bc1b96e8..a1a23b9ab9 100644 --- a/ports/raspberrypi/common-hal/digitalio/DigitalInOut.c +++ b/ports/raspberrypi/common-hal/digitalio/DigitalInOut.c @@ -43,6 +43,7 @@ digitalinout_result_t common_hal_digitalio_digitalinout_construct( self->output = false; self->open_drain = false; + // Set to input. No output value. gpio_init(pin->number); return DIGITALINOUT_OK; } @@ -75,11 +76,17 @@ digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_output( digitalio_digitalinout_obj_t* self, bool value, digitalio_drive_mode_t drive_mode) { const uint8_t pin = self->pin->number; - gpio_set_dir(pin, GPIO_OUT); - // TODO: Turn on "strong" pin driving (more current available). + gpio_disable_pulls(pin); + + // Turn on "strong" pin driving (more current available). + hw_write_masked(&padsbank0_hw->io[pin], + PADS_BANK0_GPIO0_DRIVE_VALUE_12MA << PADS_BANK0_GPIO0_DRIVE_LSB, + PADS_BANK0_GPIO0_DRIVE_BITS); self->output = true; - common_hal_digitalio_digitalinout_set_drive_mode(self, drive_mode); + self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN; + + // Pin direction is ultimately set in set_value. We don't need to do it here. common_hal_digitalio_digitalinout_set_value(self, value); return DIGITALINOUT_OK; } @@ -92,10 +99,18 @@ digitalio_direction_t common_hal_digitalio_digitalinout_get_direction( void common_hal_digitalio_digitalinout_set_value( digitalio_digitalinout_obj_t* self, bool value) { const uint8_t pin = self->pin->number; - if (self->open_drain) { - gpio_set_dir(pin, value ? GPIO_IN : GPIO_OUT); - } else { + if (self->open_drain && value) { + // If true and open-drain, set the direction -before- setting + // the pin value, to to avoid a high glitch on the pin before + // switching from output to input for open-drain. + gpio_set_dir(pin, GPIO_IN); gpio_put(pin, value); + } else { + // Otherwise set the direction -after- setting the pin value, + // to avoid a glitch which might occur if the old value was + // different and the pin was previously set to input. + gpio_put(pin, value); + gpio_set_dir(pin, GPIO_OUT); } } @@ -110,9 +125,6 @@ digitalinout_result_t common_hal_digitalio_digitalinout_set_drive_mode( const uint8_t pin = self->pin->number; bool value = common_hal_digitalio_digitalinout_get_value(self); self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN; - if (self->open_drain) { - gpio_put(pin, false); - } // True is implemented differently between modes so reset the value to make // sure it's correct for the new mode. if (value) { diff --git a/shared-bindings/bitbangio/I2C.h b/shared-bindings/bitbangio/I2C.h index 1ce4d21e91..97dd4580de 100644 --- a/shared-bindings/bitbangio/I2C.h +++ b/shared-bindings/bitbangio/I2C.h @@ -30,7 +30,7 @@ #include "py/obj.h" #include "common-hal/microcontroller/Pin.h" -#include "shared-module/bitbangio/types.h" +#include "shared-module/bitbangio/I2C.h" // Type object used in Python. Should be shared between ports. extern const mp_obj_type_t bitbangio_i2c_type; diff --git a/shared-bindings/bitbangio/OneWire.h b/shared-bindings/bitbangio/OneWire.h index ef50db737b..da665b50d5 100644 --- a/shared-bindings/bitbangio/OneWire.h +++ b/shared-bindings/bitbangio/OneWire.h @@ -28,7 +28,7 @@ #define MICROPY_INCLUDED_SHARED_BINDINGS_BITBANGIO_ONEWIRE_H #include "common-hal/microcontroller/Pin.h" -#include "shared-module/bitbangio/types.h" +#include "shared-module/bitbangio/OneWire.h" extern const mp_obj_type_t bitbangio_onewire_type; diff --git a/shared-bindings/bitbangio/SPI.h b/shared-bindings/bitbangio/SPI.h index c4b10b6663..645f1a39c8 100644 --- a/shared-bindings/bitbangio/SPI.h +++ b/shared-bindings/bitbangio/SPI.h @@ -30,7 +30,7 @@ #include "py/obj.h" #include "common-hal/microcontroller/Pin.h" -#include "shared-module/bitbangio/types.h" +#include "shared-module/bitbangio/SPI.h" // Type object used in Python. Should be shared between ports. extern const mp_obj_type_t bitbangio_spi_type; diff --git a/shared-bindings/bitbangio/__init__.c b/shared-bindings/bitbangio/__init__.c index e04bdf7011..3e4d2c08ef 100644 --- a/shared-bindings/bitbangio/__init__.c +++ b/shared-bindings/bitbangio/__init__.c @@ -36,7 +36,6 @@ #include "shared-bindings/bitbangio/I2C.h" #include "shared-bindings/bitbangio/OneWire.h" #include "shared-bindings/bitbangio/SPI.h" -#include "shared-module/bitbangio/types.h" #include "py/runtime.h" diff --git a/shared-module/bitbangio/I2C.c b/shared-module/bitbangio/I2C.c index d44aec0e75..170630df63 100644 --- a/shared-module/bitbangio/I2C.c +++ b/shared-module/bitbangio/I2C.c @@ -32,7 +32,6 @@ #include "common-hal/microcontroller/Pin.h" #include "shared-bindings/microcontroller/__init__.h" #include "shared-bindings/digitalio/DigitalInOut.h" -#include "shared-module/bitbangio/types.h" #include "supervisor/shared/translate.h" STATIC void delay(bitbangio_i2c_obj_t *self) { diff --git a/shared-module/bitbangio/I2C.h b/shared-module/bitbangio/I2C.h new file mode 100644 index 0000000000..763c03adc6 --- /dev/null +++ b/shared-module/bitbangio/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_SHARED_MODULE_BITBANGIO_I2C_H +#define MICROPY_INCLUDED_SHARED_MODULE_BITBANGIO_I2C_H + +#include "common-hal/digitalio/DigitalInOut.h" + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + digitalio_digitalinout_obj_t scl; + digitalio_digitalinout_obj_t sda; + uint32_t us_delay; + uint32_t us_timeout; + volatile bool locked; +} bitbangio_i2c_obj_t; + +#endif // MICROPY_INCLUDED_SHARED_MODULE_BITBANGIO_I2C_H diff --git a/shared-module/bitbangio/OneWire.c b/shared-module/bitbangio/OneWire.c index f5f4790876..3e4ec8426e 100644 --- a/shared-module/bitbangio/OneWire.c +++ b/shared-module/bitbangio/OneWire.c @@ -28,7 +28,6 @@ #include "shared-bindings/bitbangio/OneWire.h" #include "shared-bindings/microcontroller/__init__.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 diff --git a/shared-module/bitbangio/OneWire.h b/shared-module/bitbangio/OneWire.h new file mode 100644 index 0000000000..bc4cb2096f --- /dev/null +++ b/shared-module/bitbangio/OneWire.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_SHARED_MODULE_BITBANGIO_ONEWIRE_H +#define MICROPY_INCLUDED_SHARED_MODULE_BITBANGIO_ONEWIRE_H + +#include "common-hal/digitalio/DigitalInOut.h" + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + digitalio_digitalinout_obj_t pin; +} bitbangio_onewire_obj_t; + +#endif // MICROPY_INCLUDED_SHARED_MODULE_BITBANGIO_ONEWIRE_H diff --git a/shared-module/bitbangio/SPI.c b/shared-module/bitbangio/SPI.c index f3fe16029a..9ed3d660a5 100644 --- a/shared-module/bitbangio/SPI.c +++ b/shared-module/bitbangio/SPI.c @@ -29,9 +29,9 @@ #include "py/runtime.h" #include "common-hal/microcontroller/Pin.h" -#include "shared-bindings/microcontroller/__init__.h" +#include "shared-bindings/bitbangio/SPI.h" #include "shared-bindings/digitalio/DigitalInOut.h" -#include "shared-module/bitbangio/types.h" +#include "shared-bindings/microcontroller/__init__.h" #include "supervisor/shared/translate.h" #define MAX_BAUDRATE (common_hal_mcu_get_clock_frequency() / 48) diff --git a/shared-module/bitbangio/types.h b/shared-module/bitbangio/SPI.h similarity index 76% rename from shared-module/bitbangio/types.h rename to shared-module/bitbangio/SPI.h index 6d17234741..f3e02c206c 100644 --- a/shared-module/bitbangio/types.h +++ b/shared-module/bitbangio/SPI.h @@ -24,27 +24,13 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_SHARED_MODULE_BITBANGIO_TYPES_H -#define MICROPY_INCLUDED_SHARED_MODULE_BITBANGIO_TYPES_H +#ifndef MICROPY_INCLUDED_SHARED_MODULE_BITBANGIO_SPI_H +#define MICROPY_INCLUDED_SHARED_MODULE_BITBANGIO_SPI_H #include "common-hal/digitalio/DigitalInOut.h" #include "py/obj.h" -typedef struct { - mp_obj_base_t base; - digitalio_digitalinout_obj_t scl; - digitalio_digitalinout_obj_t sda; - uint32_t us_delay; - uint32_t us_timeout; - volatile bool locked; -} bitbangio_i2c_obj_t; - -typedef struct { - mp_obj_base_t base; - digitalio_digitalinout_obj_t pin; -} bitbangio_onewire_obj_t; - typedef struct { mp_obj_base_t base; digitalio_digitalinout_obj_t clock; @@ -58,4 +44,4 @@ typedef struct { volatile bool locked:1; } bitbangio_spi_obj_t; -#endif // MICROPY_INCLUDED_SHARED_MODULE_BITBANGIO_TYPES_H +#endif // MICROPY_INCLUDED_SHARED_MODULE_BITBANGIO_SPI_H diff --git a/shared-module/busio/I2C.h b/shared-module/busio/I2C.h index 8e3525f196..e089ea367b 100644 --- a/shared-module/busio/I2C.h +++ b/shared-module/busio/I2C.h @@ -27,7 +27,7 @@ #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" +#include "shared-module/bitbangio/I2C.h" #include "py/obj.h" diff --git a/shared-module/busio/OneWire.h b/shared-module/busio/OneWire.h index 9ffb89d497..5d80bab17c 100644 --- a/shared-module/busio/OneWire.h +++ b/shared-module/busio/OneWire.h @@ -27,7 +27,7 @@ #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" +#include "shared-module/bitbangio/OneWire.h" #include "py/obj.h" diff --git a/supervisor/shared/rgb_led_status.c b/supervisor/shared/rgb_led_status.c index 006bb1b34c..60470e057e 100644 --- a/supervisor/shared/rgb_led_status.c +++ b/supervisor/shared/rgb_led_status.c @@ -47,7 +47,6 @@ static uint8_t status_apa102_color[APA102_BUFFER_LENGTH] = {0, 0, 0, 0, 0xff, 0, #if CIRCUITPY_BITBANG_APA102 #include "shared-bindings/bitbangio/SPI.h" -#include "shared-module/bitbangio/types.h" static bitbangio_spi_obj_t status_apa102 = { .base = { .type = &bitbangio_spi_type,