summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--shared-bindings/adafruit_bus_device/I2CDevice.c81
-rw-r--r--shared-bindings/adafruit_bus_device/I2CDevice.h4
-rw-r--r--shared-module/adafruit_bus_device/I2CDevice.c67
-rw-r--r--shared-module/adafruit_bus_device/I2CDevice.h2
4 files changed, 89 insertions, 65 deletions
diff --git a/shared-bindings/adafruit_bus_device/I2CDevice.c b/shared-bindings/adafruit_bus_device/I2CDevice.c
index a4c04e198..d3b6fbec4 100644
--- a/shared-bindings/adafruit_bus_device/I2CDevice.c
+++ b/shared-bindings/adafruit_bus_device/I2CDevice.c
@@ -31,6 +31,7 @@
#include "shared-bindings/adafruit_bus_device/I2CDevice.h"
#include "shared-bindings/util.h"
#include "shared-module/adafruit_bus_device/I2CDevice.h"
+#include "shared-bindings/busio/I2C.h"
#include "lib/utils/buffer_helper.h"
#include "lib/utils/context_manager_helpers.h"
@@ -76,7 +77,7 @@ STATIC mp_obj_t adafruit_bus_device_i2cdevice_make_new(const mp_obj_type_t *type
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
- busio_i2c_obj_t* i2c = args[ARG_i2c].u_obj;
+ mp_obj_t* i2c = args[ARG_i2c].u_obj;
common_hal_adafruit_bus_device_i2cdevice_construct(MP_OBJ_TO_PTR(self), i2c, args[ARG_device_address].u_int);
if (args[ARG_probe].u_bool == true) {
@@ -107,7 +108,7 @@ STATIC mp_obj_t adafruit_bus_device_i2cdevice_obj___exit__(size_t n_args, const
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(adafruit_bus_device_i2cdevice___exit___obj, 4, 4, adafruit_bus_device_i2cdevice_obj___exit__);
-//| def readinto(self, buf: WriteableBuffer, *, start: int = 0, end: int = 0) -> None:
+//| def readinto(self, buf: WriteableBuffer, *, start: int = 0, end: Optional[int] = None) -> None:
//| """Read into ``buf`` from the device. The number of bytes read will be the
//| length of ``buf``.
//| If ``start`` or ``end`` is provided, then the buffer will be sliced
@@ -118,22 +119,6 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(adafruit_bus_device_i2cdevice___exit_
//| :param int end: Index to write up to but not include; if None, use ``len(buf)``"""
//| ...
//|
-STATIC void readinto(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t buffer, int32_t start, mp_int_t end) {
- mp_buffer_info_t bufinfo;
- mp_get_buffer_raise(buffer, &bufinfo, MP_BUFFER_WRITE);
-
- size_t length = bufinfo.len;
- normalize_buffer_bounds(&start, end, &length);
- if (length == 0) {
- mp_raise_ValueError(translate("Buffer must be at least length 1"));
- }
-
- uint8_t status = common_hal_adafruit_bus_device_i2cdevice_readinto(MP_OBJ_TO_PTR(self), ((uint8_t*)bufinfo.buf) + start, length);
- if (status != 0) {
- mp_raise_OSError(status);
- }
-}
-
STATIC mp_obj_t adafruit_bus_device_i2cdevice_readinto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_buffer, ARG_start, ARG_end };
static const mp_arg_t allowed_args[] = {
@@ -147,12 +132,21 @@ STATIC mp_obj_t adafruit_bus_device_i2cdevice_readinto(size_t n_args, const mp_o
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
- readinto(self, args[ARG_buffer].u_obj, args[ARG_start].u_int, args[ARG_end].u_int);
+ mp_obj_t dest[8];
+ mp_load_method(self->i2c, MP_QSTR_readfrom_into, dest);
+ dest[2] = mp_obj_new_int_from_ull(self->device_address);
+ dest[3] = args[ARG_buffer].u_obj;
+ dest[4] = mp_obj_new_str("start", 5);
+ dest[5] = mp_obj_new_int(args[ARG_start].u_int);
+ dest[6] = mp_obj_new_str("end", 3);
+ dest[7] = mp_obj_new_int(args[ARG_end].u_int);
+ mp_call_method_n_kw(2, 2, dest);
+
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(adafruit_bus_device_i2cdevice_readinto_obj, 2, adafruit_bus_device_i2cdevice_readinto);
-//| def write(self, buf: ReadableBuffer, *, start: int = 0, end: int = 0) -> None:
+//| def write(self, buf: ReadableBuffer, *, start: int = 0, end: Optional[int] = None) -> None:
//| """Write the bytes from ``buffer`` to the device, then transmit a stop bit.
//| If ``start`` or ``end`` is provided, then the buffer will be sliced
//| as if ``buffer[start:end]``. This will not cause an allocation like
@@ -163,22 +157,6 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(adafruit_bus_device_i2cdevice_readinto_obj, 2,
//| """
//| ...
//|
-STATIC void write(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t buffer, int32_t start, mp_int_t end, bool transmit_stop_bit) {
- mp_buffer_info_t bufinfo;
- mp_get_buffer_raise(buffer, &bufinfo, MP_BUFFER_READ);
-
- size_t length = bufinfo.len;
- normalize_buffer_bounds(&start, end, &length);
- if (length == 0) {
- mp_raise_ValueError(translate("Buffer must be at least length 1"));
- }
-
- uint8_t status = common_hal_adafruit_bus_device_i2cdevice_write(MP_OBJ_TO_PTR(self), ((uint8_t*)bufinfo.buf) + start, length, transmit_stop_bit);
- if (status != 0) {
- mp_raise_OSError(status);
- }
-}
-
STATIC mp_obj_t adafruit_bus_device_i2cdevice_write(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_buffer, ARG_start, ARG_end };
static const mp_arg_t allowed_args[] = {
@@ -191,13 +169,22 @@ STATIC mp_obj_t adafruit_bus_device_i2cdevice_write(size_t n_args, const mp_obj_
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
- write(self, args[ARG_buffer].u_obj, args[ARG_start].u_int, args[ARG_end].u_int, true);
+ mp_obj_t dest[8];
+ mp_load_method(self->i2c, MP_QSTR_writeto, dest);
+ dest[2] = mp_obj_new_int_from_ull(self->device_address);
+ dest[3] = args[ARG_buffer].u_obj;
+ dest[4] = mp_obj_new_str("start", 5);
+ dest[5] = mp_obj_new_int(args[ARG_start].u_int);
+ dest[6] = mp_obj_new_str("end", 3);
+ dest[7] = mp_obj_new_int(args[ARG_end].u_int);
+ mp_call_method_n_kw(2, 2, dest);
+
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(adafruit_bus_device_i2cdevice_write_obj, 2, adafruit_bus_device_i2cdevice_write);
-//| def write_then_readinto(self, out_buffer: WriteableBuffer, in_buffer: ReadableBuffer, *, out_start: int = 0, out_end: int = 0, in_start: int = 0, in_end: int = 0) -> None:
+//| def write_then_readinto(self, out_buffer: WriteableBuffer, in_buffer: ReadableBuffer, *, out_start: int = 0, out_end: Optional[int] = None, in_start: int = 0, in_end: Optional[int] = None) -> None:
//| """Write the bytes from ``out_buffer`` to the device, then immediately
//| reads into ``in_buffer`` from the device. The number of bytes read
//| will be the length of ``in_buffer``.
@@ -233,9 +220,21 @@ STATIC mp_obj_t adafruit_bus_device_i2cdevice_write_then_readinto(size_t n_args,
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
- write(self, args[ARG_out_buffer].u_obj, args[ARG_out_start].u_int, args[ARG_out_end].u_int, false);
-
- readinto(self, args[ARG_in_buffer].u_obj, args[ARG_in_start].u_int, args[ARG_in_end].u_int);
+ mp_obj_t dest[13];
+ mp_load_method(self->i2c, MP_QSTR_writeto_then_readfrom, dest);
+ dest[2] = mp_obj_new_int_from_ull(self->device_address);
+ dest[3] = args[ARG_out_buffer].u_obj;
+ dest[4] = args[ARG_in_buffer].u_obj;
+ dest[5] = mp_obj_new_str("out_start", 9);
+ dest[6] = mp_obj_new_int(args[ARG_out_start].u_int);
+ dest[7] = mp_obj_new_str("out_end", 7);
+ dest[8] = mp_obj_new_int(args[ARG_out_end].u_int);
+ dest[9] = mp_obj_new_str("in_start", 8);
+ dest[10] = mp_obj_new_int(args[ARG_in_start].u_int);
+ dest[11] = mp_obj_new_str("in_end", 6);
+ dest[12] = mp_obj_new_int(args[ARG_in_end].u_int);
+
+ mp_call_method_n_kw(3, 4, dest);
return mp_const_none;
}
diff --git a/shared-bindings/adafruit_bus_device/I2CDevice.h b/shared-bindings/adafruit_bus_device/I2CDevice.h
index cf7b1321a..82ef1feb8 100644
--- a/shared-bindings/adafruit_bus_device/I2CDevice.h
+++ b/shared-bindings/adafruit_bus_device/I2CDevice.h
@@ -43,9 +43,7 @@
extern const mp_obj_type_t adafruit_bus_device_i2cdevice_type;
// Initializes the hardware peripheral.
-extern void common_hal_adafruit_bus_device_i2cdevice_construct(adafruit_bus_device_i2cdevice_obj_t *self, busio_i2c_obj_t *i2c, uint8_t device_address);
-extern uint8_t common_hal_adafruit_bus_device_i2cdevice_readinto(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t buffer, size_t length);
-extern uint8_t common_hal_adafruit_bus_device_i2cdevice_write(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t buffer, size_t length, bool transmit_stop_bit);
+extern void common_hal_adafruit_bus_device_i2cdevice_construct(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t *i2c, uint8_t device_address);
extern void common_hal_adafruit_bus_device_i2cdevice_lock(adafruit_bus_device_i2cdevice_obj_t *self);
extern void common_hal_adafruit_bus_device_i2cdevice_unlock(adafruit_bus_device_i2cdevice_obj_t *self);
extern void common_hal_adafruit_bus_device_i2cdevice_probe_for_device(adafruit_bus_device_i2cdevice_obj_t *self);
diff --git a/shared-module/adafruit_bus_device/I2CDevice.c b/shared-module/adafruit_bus_device/I2CDevice.c
index 83abe80d6..53811d491 100644
--- a/shared-module/adafruit_bus_device/I2CDevice.c
+++ b/shared-module/adafruit_bus_device/I2CDevice.c
@@ -31,48 +31,75 @@
#include "py/runtime.h"
#include "lib/utils/interrupt_char.h"
-void common_hal_adafruit_bus_device_i2cdevice_construct(adafruit_bus_device_i2cdevice_obj_t *self, busio_i2c_obj_t *i2c, uint8_t device_address) {
+void common_hal_adafruit_bus_device_i2cdevice_construct(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t *i2c, uint8_t device_address) {
self->i2c = i2c;
self->device_address = device_address;
}
void common_hal_adafruit_bus_device_i2cdevice_lock(adafruit_bus_device_i2cdevice_obj_t *self) {
- bool success = common_hal_busio_i2c_try_lock(self->i2c);
+ mp_obj_t dest[2];
+ mp_load_method(self->i2c, MP_QSTR_try_lock, dest);
- while (!success) {
+ mp_obj_t success = mp_call_method_n_kw(0, 0, dest);
+
+ while (!mp_obj_is_true(success)) {
RUN_BACKGROUND_TASKS;
if (mp_hal_is_interrupted()) {
break;
}
- success = common_hal_busio_i2c_try_lock(self->i2c);
+ success = mp_call_method_n_kw(0, 0, dest);
}
}
void common_hal_adafruit_bus_device_i2cdevice_unlock(adafruit_bus_device_i2cdevice_obj_t *self) {
- common_hal_busio_i2c_unlock(self->i2c);
-}
-
-uint8_t common_hal_adafruit_bus_device_i2cdevice_readinto(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t buffer, size_t length) {
- return common_hal_busio_i2c_read(self->i2c, self->device_address, buffer, length);
-}
-
-uint8_t common_hal_adafruit_bus_device_i2cdevice_write(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t buffer, size_t length, bool transmit_stop_bit) {
- return common_hal_busio_i2c_write(self->i2c, self->device_address, buffer, length, transmit_stop_bit);
+ mp_obj_t dest[2];
+ mp_load_method(self->i2c, MP_QSTR_unlock, dest);
+ mp_call_method_n_kw(0, 0, dest);
}
void common_hal_adafruit_bus_device_i2cdevice_probe_for_device(adafruit_bus_device_i2cdevice_obj_t *self) {
common_hal_adafruit_bus_device_i2cdevice_lock(self);
- mp_buffer_info_t bufinfo;
- mp_obj_t buffer = mp_obj_new_bytearray_of_zeros(1);
+ mp_buffer_info_t write_bufinfo;
+ mp_obj_t write_buffer = mp_obj_new_bytearray_of_zeros(0);
+ mp_get_buffer_raise(write_buffer, &write_bufinfo, MP_BUFFER_READ);
+
+ mp_obj_t dest[4];
- mp_get_buffer_raise(buffer, &bufinfo, MP_BUFFER_WRITE);
+ /* catch exceptions that may be thrown while probing for the device */
+ nlr_buf_t write_nlr;
+ if (nlr_push(&write_nlr) == 0) {
+ mp_load_method(self->i2c, MP_QSTR_writeto, dest);
+ dest[2] = mp_obj_new_int_from_ull(self->device_address);
+ dest[3] = write_buffer;
+ mp_call_method_n_kw(2, 0, dest);
+ nlr_pop();
+ } else {
+ /* some OS's don't like writing an empty bytestring... retry by reading a byte */
+ mp_buffer_info_t read_bufinfo;
+ mp_obj_t read_buffer = mp_obj_new_bytearray_of_zeros(1);
+ mp_get_buffer_raise(read_buffer, &read_bufinfo, MP_BUFFER_WRITE);
- uint8_t status = common_hal_adafruit_bus_device_i2cdevice_readinto(self, (uint8_t*)bufinfo.buf, 1);
- if (status != 0) {
- common_hal_adafruit_bus_device_i2cdevice_unlock(self);
- mp_raise_ValueError_varg(translate("No I2C device at address: %x"), self->device_address);
+ mp_load_method(self->i2c, MP_QSTR_readfrom_into, dest);
+ dest[2] = mp_obj_new_int_from_ull(self->device_address);
+ dest[3] = read_buffer;
+
+ nlr_buf_t read_nlr;
+ if (nlr_push(&read_nlr) == 0) {
+ mp_call_method_n_kw(2, 0, dest);
+ nlr_pop();
+ } else {
+ /* At this point we tried two methods and only got exceptions */
+ if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(((mp_obj_base_t*)read_nlr.ret_val)->type), MP_OBJ_FROM_PTR(&mp_type_OSError))) {
+ common_hal_adafruit_bus_device_i2cdevice_unlock(self);
+ mp_raise_ValueError_varg(translate("No I2C device at address: %x"), self->device_address);
+ }
+ else {
+ /* In case we receive an unrelated exception pass it up */
+ nlr_raise(MP_OBJ_FROM_PTR(read_nlr.ret_val));
+ }
+ }
}
common_hal_adafruit_bus_device_i2cdevice_unlock(self);
diff --git a/shared-module/adafruit_bus_device/I2CDevice.h b/shared-module/adafruit_bus_device/I2CDevice.h
index d06adb9f5..b76bafb2c 100644
--- a/shared-module/adafruit_bus_device/I2CDevice.h
+++ b/shared-module/adafruit_bus_device/I2CDevice.h
@@ -32,7 +32,7 @@
typedef struct {
mp_obj_base_t base;
- busio_i2c_obj_t *i2c;
+ mp_obj_t *i2c;
uint8_t device_address;
} adafruit_bus_device_i2cdevice_obj_t;