diff options
| author | Dan Halbert <halbert@adafruit.com> | 2021-03-03 12:42:47 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-03 12:42:47 -0500 |
| commit | cd48c5ee8317fb94fed1fd6c430ab83d1b487ac9 (patch) | |
| tree | 14c123c138b8a5ac748adc31c8eedea172fb5f2b /ports/raspberrypi | |
| parent | 514b73bcf8e2978bc7a4cd8985618208b103283d (diff) | |
| parent | fb7a0f7efcdd639ff4a14a2985f9381c426b2321 (diff) | |
Merge pull request #4315 from dhalbert/rp2040-i2c-short-writes
RP2040: Implement short I2C writes (2 bytes or less) using bitbangio
Diffstat (limited to 'ports/raspberrypi')
| -rw-r--r-- | ports/raspberrypi/Makefile | 1 | ||||
| -rw-r--r-- | ports/raspberrypi/common-hal/busio/I2C.c | 75 | ||||
| -rw-r--r-- | ports/raspberrypi/common-hal/busio/I2C.h | 2 |
3 files changed, 62 insertions, 16 deletions
diff --git a/ports/raspberrypi/Makefile b/ports/raspberrypi/Makefile index 9d66338c7..ffd50299f 100644 --- a/ports/raspberrypi/Makefile +++ b/ports/raspberrypi/Makefile @@ -157,6 +157,7 @@ SRC_SDK := \ src/common/pico_sync/lock_core.c \ src/common/pico_sync/mutex.c \ src/common/pico_time/time.c \ + src/common/pico_time/timeout_helper.c \ src/common/pico_util/pheap.c \ src/rp2_common/hardware_adc/adc.c \ src/rp2_common/hardware_claim/claim.c \ diff --git a/ports/raspberrypi/common-hal/busio/I2C.c b/ports/raspberrypi/common-hal/busio/I2C.c index 26a7f3807..cb0d73e75 100644 --- a/ports/raspberrypi/common-hal/busio/I2C.c +++ b/ports/raspberrypi/common-hal/busio/I2C.c @@ -24,12 +24,13 @@ * THE SOFTWARE. */ -#include "shared-bindings/busio/I2C.h" #include "py/mperrno.h" +#include "py/mphal.h" +#include "shared-bindings/busio/I2C.h" #include "py/runtime.h" #include "shared-bindings/microcontroller/__init__.h" -#include "supervisor/shared/translate.h" +#include "shared-bindings/bitbangio/I2C.h" #include "src/rp2_common/hardware_gpio/include/hardware/gpio.h" @@ -37,6 +38,9 @@ #define NO_PIN 0xff +// One second +#define BUS_TIMEOUT_US 1000000 + STATIC bool never_reset_i2c[2]; STATIC i2c_inst_t* i2c[2] = {i2c0, i2c1}; @@ -94,15 +98,23 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, } #endif - gpio_set_function(sda->number, GPIO_FUNC_I2C); - gpio_set_function(scl->number, GPIO_FUNC_I2C); + // 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. + // + // Sets pins to open drain, high, and input. + shared_module_bitbangio_i2c_construct(&self->bitbangio_i2c, scl, sda, + frequency, timeout); self->baudrate = i2c_init(self->peripheral, frequency); - self->sda_pin = sda->number; self->scl_pin = scl->number; - claim_pin(sda); + self->sda_pin = sda->number; claim_pin(scl); + claim_pin(sda); + + gpio_set_function(self->scl_pin, GPIO_FUNC_I2C); + gpio_set_function(self->sda_pin, GPIO_FUNC_I2C); } bool common_hal_busio_i2c_deinited(busio_i2c_obj_t *self) { @@ -124,8 +136,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) { - uint8_t fake_read = 0; - return i2c_read_blocking(self->peripheral, addr, &fake_read, 1, false) != PICO_ERROR_GENERIC; + return common_hal_busio_i2c_write(self, addr, NULL, 0, true) == 0; } bool common_hal_busio_i2c_try_lock(busio_i2c_obj_t *self) { @@ -147,24 +158,56 @@ 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) { - int result = i2c_write_blocking(self->peripheral, addr, data, len, !transmit_stop_bit); + if (len <= 2) { + // The RP2040 I2C peripheral will not do writes 2 bytes or less long. + // So use bitbangio.I2C to do the write. + + gpio_set_function(self->scl_pin, GPIO_FUNC_SIO); + gpio_set_function(self->sda_pin, GPIO_FUNC_SIO); + gpio_set_dir(self->scl_pin, GPIO_IN); + gpio_set_dir(self->sda_pin, GPIO_IN); + gpio_put(self->scl_pin, false); + gpio_put(self->sda_pin, false); + + uint8_t status = shared_module_bitbangio_i2c_write(&self->bitbangio_i2c, + addr, data, len, transmit_stop_bit); + + // The pins must be set back to GPIO_FUNC_I2C in the order given here, + // SCL first, otherwise reads will hang. + gpio_set_function(self->scl_pin, GPIO_FUNC_I2C); + gpio_set_function(self->sda_pin, GPIO_FUNC_I2C); + + return status; + } + + int result = i2c_write_timeout_us(self->peripheral, addr, data, len, !transmit_stop_bit, BUS_TIMEOUT_US); if (result == len) { return 0; - } else if (result == PICO_ERROR_GENERIC) { - return MP_ENODEV; } - return MP_EIO; + switch (result) { + case PICO_ERROR_GENERIC: + return MP_ENODEV; + case PICO_ERROR_TIMEOUT: + return MP_ETIMEDOUT; + default: + return MP_EIO; + } } uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t addr, uint8_t *data, size_t len) { - int result = i2c_read_blocking(self->peripheral, addr, data, len, false); + int result = i2c_read_timeout_us(self->peripheral, addr, data, len, false, BUS_TIMEOUT_US); if (result == len) { return 0; - } else if (result == PICO_ERROR_GENERIC) { - return MP_ENODEV; } - return MP_EIO; + switch (result) { + case PICO_ERROR_GENERIC: + return MP_ENODEV; + case PICO_ERROR_TIMEOUT: + return MP_ETIMEDOUT; + default: + return MP_EIO; + } } void common_hal_busio_i2c_never_reset(busio_i2c_obj_t *self) { diff --git a/ports/raspberrypi/common-hal/busio/I2C.h b/ports/raspberrypi/common-hal/busio/I2C.h index d09f29e54..651054a04 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,6 +37,7 @@ typedef struct { mp_obj_base_t base; i2c_inst_t * peripheral; + bitbangio_i2c_obj_t bitbangio_i2c; bool has_lock; uint baudrate; uint8_t scl_pin; |
