From 710b5d8aff2de490c5531e5cbc9092d51fb39958 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Sun, 19 Feb 2017 17:02:29 +0100 Subject: Two I2C fixes: 1) Bus error will be thrown on read/write errors with errno set. (Read didn't used to fail at all.) 2) try_lock correctly returns boolean whether lock was grabbed. Fixes #87 --- shared-module/bitbangio/I2C.c | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) (limited to 'shared-module/bitbangio') diff --git a/shared-module/bitbangio/I2C.c b/shared-module/bitbangio/I2C.c index 9ba68262f..422f7778d 100644 --- a/shared-module/bitbangio/I2C.c +++ b/shared-module/bitbangio/I2C.c @@ -26,6 +26,7 @@ #include "shared-bindings/bitbangio/I2C.h" +#include "py/mperrno.h" #include "py/obj.h" #include "common-hal/microcontroller/types.h" @@ -192,38 +193,48 @@ bool shared_module_bitbangio_i2c_probe(bitbangio_i2c_obj_t *self, uint8_t addr) return ok; } -bool shared_module_bitbangio_i2c_write(bitbangio_i2c_obj_t *self, uint16_t addr, +uint8_t shared_module_bitbangio_i2c_write(bitbangio_i2c_obj_t *self, uint16_t addr, const uint8_t *data, size_t len, bool transmit_stop_bit) { // start the I2C transaction start(self); - bool ok = write_byte(self, addr << 1); + uint8_t status = 0; + if (!write_byte(self, addr << 1)) { + status = MP_ENODEV; + } - for (uint32_t i = 0; i < len; i++) { - ok = ok && write_byte(self, data[i]); - if (!ok) { - break; + if (status == 0) { + for (uint32_t i = 0; i < len; i++) { + if (!write_byte(self, data[i])) { + status = MP_EIO; + break; + } } } if (transmit_stop_bit) { stop(self); } - return ok; + return status; } -bool shared_module_bitbangio_i2c_read(bitbangio_i2c_obj_t *self, uint16_t addr, +uint8_t shared_module_bitbangio_i2c_read(bitbangio_i2c_obj_t *self, uint16_t addr, uint8_t * data, size_t len) { // start the I2C transaction start(self); - bool ok = write_byte(self, (addr << 1) | 1); + uint8_t status = 0; + if (!write_byte(self, (addr << 1) | 1)) { + status = MP_ENODEV; + } - for (uint32_t i = 0; i < len; i++) { - ok = ok && read_byte(self, data + i, i < len - 1); - if (!ok) { - break; + if (status == 0) { + for (uint32_t i = 0; i < len; i++) { + if (!read_byte(self, data + i, i < len - 1)) { + status = MP_EIO; + break; + } } } stop(self); - return ok; + return status; } -- cgit v1.2.3