diff options
| author | Scott Shawcroft <scott.shawcroft@gmail.com> | 2017-02-19 17:02:29 +0100 |
|---|---|---|
| committer | Scott Shawcroft <scott.shawcroft@gmail.com> | 2017-02-19 17:02:29 +0100 |
| commit | 710b5d8aff2de490c5531e5cbc9092d51fb39958 (patch) | |
| tree | aa3f15389b8ade8bbf5348a6ef21fd10a3c08a91 /shared-module | |
| parent | e9659e61f89fb9ef84011c42e89849f7fd9bc9c1 (diff) | |
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
Diffstat (limited to 'shared-module')
| -rw-r--r-- | shared-module/bitbangio/I2C.c | 39 |
1 files changed, 25 insertions, 14 deletions
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; } |
