summaryrefslogtreecommitdiff
path: root/shared-bindings
diff options
context:
space:
mode:
authorScott Shawcroft <scott.shawcroft@gmail.com>2017-02-19 17:02:29 +0100
committerScott Shawcroft <scott.shawcroft@gmail.com>2017-02-19 17:02:29 +0100
commit710b5d8aff2de490c5531e5cbc9092d51fb39958 (patch)
treeaa3f15389b8ade8bbf5348a6ef21fd10a3c08a91 /shared-bindings
parente9659e61f89fb9ef84011c42e89849f7fd9bc9c1 (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')
-rw-r--r--shared-bindings/bitbangio/I2C.c18
-rw-r--r--shared-bindings/bitbangio/I2C.h14
-rw-r--r--shared-bindings/nativeio/I2C.c15
-rw-r--r--shared-bindings/nativeio/I2C.h14
4 files changed, 36 insertions, 25 deletions
diff --git a/shared-bindings/bitbangio/I2C.c b/shared-bindings/bitbangio/I2C.c
index 2b547d65c..5b772c9bf 100644
--- a/shared-bindings/bitbangio/I2C.c
+++ b/shared-bindings/bitbangio/I2C.c
@@ -31,6 +31,7 @@
#include "shared-bindings/microcontroller/Pin.h"
#include "lib/utils/context_manager_helpers.h"
+#include "py/mperrno.h"
#include "py/runtime.h"
//| .. currentmodule:: bitbangio
//|
@@ -129,8 +130,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_i2c_scan_obj, bitbangio_i2c_scan);
//| Attempts to grab the I2C lock. Returns True on success.
//|
STATIC mp_obj_t bitbangio_i2c_obj_try_lock(mp_obj_t self_in) {
- shared_module_bitbangio_i2c_try_lock(MP_OBJ_TO_PTR(self_in));
- return self_in;
+ return mp_obj_new_bool(shared_module_bitbangio_i2c_try_lock(MP_OBJ_TO_PTR(self_in)));
}
MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_i2c_try_lock_obj, bitbangio_i2c_obj_try_lock);
@@ -183,7 +183,13 @@ STATIC mp_obj_t bitbangio_i2c_readfrom_into(size_t n_args, const mp_obj_t *pos_a
} else if (len > bufinfo.len) {
len = bufinfo.len;
}
- shared_module_bitbangio_i2c_read(self, args[ARG_address].u_int, ((uint8_t*)bufinfo.buf) + start, len);
+ uint8_t status = shared_module_bitbangio_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(bitbangio_i2c_readfrom_into_obj, 3, bitbangio_i2c_readfrom_into);
@@ -235,10 +241,10 @@ STATIC mp_obj_t bitbangio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, m
}
// do the transfer
- bool ok = shared_module_bitbangio_i2c_write(self, args[ARG_address].u_int,
+ uint8_t status = shared_module_bitbangio_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/bitbangio/I2C.h b/shared-bindings/bitbangio/I2C.h
index 451fa4d94..4d01230af 100644
--- a/shared-bindings/bitbangio/I2C.h
+++ b/shared-bindings/bitbangio/I2C.h
@@ -50,14 +50,14 @@ extern void shared_module_bitbangio_i2c_unlock(bitbangio_i2c_obj_t *self);
// Probe the bus to see if a device acknowledges the given address.
extern bool shared_module_bitbangio_i2c_probe(bitbangio_i2c_obj_t *self, uint8_t addr);
-extern bool shared_module_bitbangio_i2c_write(bitbangio_i2c_obj_t *self,
- uint16_t address,
- const uint8_t * data, size_t len,
- bool stop);
+extern uint8_t shared_module_bitbangio_i2c_write(bitbangio_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 shared_module_bitbangio_i2c_read(bitbangio_i2c_obj_t *self,
- uint16_t address,
- uint8_t * data, size_t len);
+extern uint8_t shared_module_bitbangio_i2c_read(bitbangio_i2c_obj_t *self,
+ uint16_t address,
+ uint8_t * data, size_t len);
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_BITBANGIO_I2C_H__
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__