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-bindings/nativeio | |
| 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-bindings/nativeio')
| -rw-r--r-- | shared-bindings/nativeio/I2C.c | 15 | ||||
| -rw-r--r-- | shared-bindings/nativeio/I2C.h | 14 |
2 files changed, 17 insertions, 12 deletions
diff --git a/shared-bindings/nativeio/I2C.c b/shared-bindings/nativeio/I2C.c index bb827a95d..43cf7b63d 100644 --- a/shared-bindings/nativeio/I2C.c +++ b/shared-bindings/nativeio/I2C.c @@ -141,8 +141,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_i2c_scan_obj, nativeio_i2c_scan); //| Attempts to grab the I2C lock. Returns True on success. //| STATIC mp_obj_t nativeio_i2c_obj_try_lock(mp_obj_t self_in) { - common_hal_nativeio_i2c_try_lock(MP_OBJ_TO_PTR(self_in)); - return self_in; + return mp_obj_new_bool(common_hal_nativeio_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); @@ -196,7 +195,11 @@ 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; } - common_hal_nativeio_i2c_read(self, args[ARG_address].u_int, ((uint8_t*)bufinfo.buf) + start, len); + uint8_t status = common_hal_nativeio_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); @@ -248,10 +251,10 @@ STATIC mp_obj_t nativeio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, mp } // do the transfer - bool ok = common_hal_nativeio_i2c_write(self, args[ARG_address].u_int, + uint8_t status = common_hal_nativeio_i2c_write(self, args[ARG_address].u_int, ((uint8_t*) bufinfo.buf) + start, len, args[ARG_stop].u_bool); - if (!ok) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "I2C bus error")); + if (status != 0) { + mp_raise_OSError(status); } return mp_const_none; } diff --git a/shared-bindings/nativeio/I2C.h b/shared-bindings/nativeio/I2C.h index cf7a9fa60..5f3c89a77 100644 --- a/shared-bindings/nativeio/I2C.h +++ b/shared-bindings/nativeio/I2C.h @@ -57,12 +57,14 @@ extern void common_hal_nativeio_i2c_unlock(nativeio_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_nativeio_i2c_write(nativeio_i2c_obj_t *self, uint16_t address, - const uint8_t * data, size_t len, - bool stop); +// 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, + const uint8_t * data, size_t len, + bool stop); -// Reads memory of the i2c device picking up where it left off. -extern bool common_hal_nativeio_i2c_read(nativeio_i2c_obj_t *self, uint16_t address, - uint8_t * data, size_t len); +// 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, + uint8_t * data, size_t len); #endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_I2C_H__ |
