wip; works on espressif

This commit is contained in:
Dan Halbert 2022-01-29 22:44:27 -05:00
parent 3fb5023b9e
commit 83e6e6690a
5 changed files with 108 additions and 95 deletions

View File

@ -99,7 +99,7 @@ msgstr ""
msgid "%q length must be %d-%d"
msgstr ""
#: shared-bindings/usb_hid/Device.c
#: shared-bindings/busio/I2C.c shared-bindings/usb_hid/Device.c
msgid "%q length must be >= 1"
msgstr ""
@ -631,7 +631,7 @@ msgstr ""
msgid "Buffer must be a multiple of 512 bytes"
msgstr ""
#: shared-bindings/bitbangio/I2C.c shared-bindings/busio/I2C.c
#: shared-bindings/bitbangio/I2C.c
msgid "Buffer must be at least length 1"
msgstr ""

View File

@ -136,13 +136,19 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
self->scl_pin = NULL;
}
bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) {
static esp_err_t i2c_zero_length_write(busio_i2c_obj_t *self, uint8_t addr, TickType_t timeout) {
// i2c_master_write_to_device() won't do zero-length writes, so we do it by hand.
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, addr << 1, true);
i2c_master_stop(cmd);
esp_err_t result = i2c_master_cmd_begin(self->i2c_num, cmd, 10);
esp_err_t result = i2c_master_cmd_begin(self->i2c_num, cmd, timeout);
i2c_cmd_link_delete(cmd);
return result;
}
bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) {
esp_err_t result = i2c_zero_length_write(self, addr, 1);
return result == ESP_OK;
}
@ -163,45 +169,36 @@ void common_hal_busio_i2c_unlock(busio_i2c_obj_t *self) {
self->has_lock = false;
}
uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addr,
const uint8_t *data, size_t len, bool transmit_stop_bit) {
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, addr << 1, true);
i2c_master_write(cmd, (uint8_t *)data, len, true);
if (transmit_stop_bit) {
i2c_master_stop(cmd);
static uint8_t convert_esp_err(esp_err_t result) {
switch (result) {
case ESP_OK:
return 0;
case ESP_FAIL:
return MP_ENODEV;
case ESP_ERR_TIMEOUT:
return MP_ETIMEDOUT;
default:
return MP_EIO;
}
esp_err_t result = i2c_master_cmd_begin(self->i2c_num, cmd, 100 /* wait in ticks */);
i2c_cmd_link_delete(cmd);
if (result == ESP_OK) {
return 0;
} else if (result == ESP_FAIL) {
return MP_ENODEV;
}
return MP_EIO;
}
uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t addr,
uint8_t *data, size_t len) {
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, addr << 1 | 1, true); // | 1 to indicate read
if (len > 1) {
i2c_master_read(cmd, data, len - 1, 0);
}
i2c_master_read_byte(cmd, data + len - 1, 1);
i2c_master_stop(cmd);
esp_err_t result = i2c_master_cmd_begin(self->i2c_num, cmd, 100 /* wait in ticks */);
i2c_cmd_link_delete(cmd);
uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint8_t addr, const uint8_t *data, size_t len) {
return convert_esp_err(len == 0
? i2c_zero_length_write(self, addr, 100)
: i2c_master_write_to_device(self->i2c_num, addr, data, len, 100 /* wait in ticks */)
);
}
if (result == ESP_OK) {
return 0;
} else if (result == ESP_FAIL) {
return MP_ENODEV;
}
return MP_EIO;
uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint8_t addr, uint8_t *data, size_t len) {
return convert_esp_err(
i2c_master_read_from_device(self->i2c_num, addr, data, len, 100 /* wait in ticks */));
}
uint8_t common_hal_busio_i2c_write_read(busio_i2c_obj_t *self, uint8_t addr,
uint8_t *out_data, size_t out_len, uint8_t *in_data, size_t in_len) {
return convert_esp_err(
i2c_master_write_read_device(self->i2c_num, (uint8_t)addr,
out_data, out_len, in_data, in_len, 100 /* wait in ticks */));
}
void common_hal_busio_i2c_never_reset(busio_i2c_obj_t *self) {

View File

@ -58,10 +58,6 @@
//| :param int frequency: The clock frequency in Hertz
//| :param int timeout: The maximum clock stretching timeut - (used only for
//| :class:`bitbangio.I2C`; ignored for :class:`busio.I2C`)
//|
//| .. note:: On the nRF52840, only one I2C object may be created,
//| except on the Circuit Playground Bluefruit, which allows two,
//| one for the onboard accelerometer, and one for offboard use."""
//| ...
//|
STATIC mp_obj_t busio_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
@ -191,23 +187,6 @@ MP_DEFINE_CONST_FUN_OBJ_1(busio_i2c_unlock_obj, busio_i2c_obj_unlock);
//| :param int end: end of buffer slice; if not specified, use ``len(buffer)``"""
//| ...
//|
// Shared arg parsing for readfrom_into and writeto_then_readfrom.
STATIC void readfrom(busio_i2c_obj_t *self, mp_int_t address, 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_busio_i2c_read(self, address, ((uint8_t *)bufinfo.buf) + start, length);
if (status != 0) {
mp_raise_OSError(status);
}
}
STATIC mp_obj_t busio_i2c_readfrom_into(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_address, ARG_buffer, ARG_start, ARG_end };
static const mp_arg_t allowed_args[] = {
@ -219,11 +198,27 @@ STATIC mp_obj_t busio_i2c_readfrom_into(size_t n_args, const mp_obj_t *pos_args,
busio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
check_for_deinit(self);
check_lock(self);
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);
readfrom(self, args[ARG_address].u_int, args[ARG_buffer].u_obj, args[ARG_start].u_int,
args[ARG_end].u_int);
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_WRITE);
size_t length = bufinfo.len;
int32_t start = args[ARG_start].u_int;
const int32_t end = args[ARG_end].u_int;
normalize_buffer_bounds(&start, end, &length);
if (length == 0) {
mp_raise_ValueError_varg(translate("%q length must be >= 1"), MP_QSTR_buffer);
}
uint8_t status =
common_hal_busio_i2c_read(self, args[ARG_address].u_int, ((uint8_t *)bufinfo.buf) + start, length);
if (status != 0) {
mp_raise_OSError(status);
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(busio_i2c_readfrom_into_obj, 1, busio_i2c_readfrom_into);
@ -247,23 +242,6 @@ MP_DEFINE_CONST_FUN_OBJ_KW(busio_i2c_readfrom_into_obj, 1, busio_i2c_readfrom_in
//| """
//| ...
//|
// Shared arg parsing for writeto and writeto_then_readfrom.
STATIC void writeto(busio_i2c_obj_t *self, mp_int_t address, mp_obj_t buffer, int32_t start, mp_int_t end, bool stop) {
// get the buffer to write the data from
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);
// do the transfer
uint8_t status = common_hal_busio_i2c_write(self, address, ((uint8_t *)bufinfo.buf) + start,
length, stop);
if (status != 0) {
mp_raise_OSError(status);
}
}
STATIC mp_obj_t busio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_address, ARG_buffer, ARG_start, ARG_end };
static const mp_arg_t allowed_args[] = {
@ -278,8 +256,23 @@ STATIC mp_obj_t busio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, mp_ma
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);
writeto(self, args[ARG_address].u_int, args[ARG_buffer].u_obj, args[ARG_start].u_int,
args[ARG_end].u_int, true);
// get the buffer to write the data from
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ);
size_t length = bufinfo.len;
int32_t start = args[ARG_start].u_int;
const int32_t end = args[ARG_end].u_int;
normalize_buffer_bounds(&start, end, &length);
// do the transfer
uint8_t status =
common_hal_busio_i2c_write(self, args[ARG_address].u_int, ((uint8_t *)bufinfo.buf) + start, length);
if (status != 0) {
mp_raise_OSError(status);
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(busio_i2c_writeto_obj, 1, busio_i2c_writeto);
@ -314,10 +307,10 @@ STATIC mp_obj_t busio_i2c_writeto_then_readfrom(size_t n_args, const mp_obj_t *p
{ MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_out_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_in_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_out_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_out_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} },
{ MP_QSTR_in_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_in_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} },
{ MP_QSTR_out_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_out_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} },
{ MP_QSTR_in_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_in_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} },
};
busio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
check_for_deinit(self);
@ -325,10 +318,30 @@ STATIC mp_obj_t busio_i2c_writeto_then_readfrom(size_t n_args, const mp_obj_t *p
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);
writeto(self, args[ARG_address].u_int, args[ARG_out_buffer].u_obj, args[ARG_out_start].u_int,
args[ARG_out_end].u_int, false);
readfrom(self, args[ARG_address].u_int, args[ARG_in_buffer].u_obj, args[ARG_in_start].u_int,
args[ARG_in_end].u_int);
mp_buffer_info_t out_bufinfo;
mp_get_buffer_raise(args[ARG_out_buffer].u_obj, &out_bufinfo, MP_BUFFER_READ);
size_t out_length = out_bufinfo.len;
int32_t out_start = args[ARG_out_start].u_int;
const int32_t out_end = args[ARG_out_end].u_int;
normalize_buffer_bounds(&out_start, out_end, &out_length);
mp_buffer_info_t in_bufinfo;
mp_get_buffer_raise(args[ARG_in_buffer].u_obj, &in_bufinfo, MP_BUFFER_WRITE);
size_t in_length = in_bufinfo.len;
int32_t in_start = args[ARG_in_start].u_int;
const int32_t in_end = args[ARG_in_end].u_int;
normalize_buffer_bounds(&in_start, in_end, &in_length);
if (in_length == 0) {
mp_raise_ValueError_varg(translate("%q length must be >= 1"), MP_QSTR_out_buffer);
}
uint8_t status = common_hal_busio_i2c_write_read(self, args[ARG_address].u_int,
((uint8_t *)out_bufinfo.buf) + out_start, out_length,((uint8_t *)in_bufinfo.buf) + in_start, in_length);
if (status != 0) {
mp_raise_OSError(status);
}
return mp_const_none;
}

View File

@ -60,15 +60,18 @@ extern void common_hal_busio_i2c_unlock(busio_i2c_obj_t *self);
extern bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr);
// Write to the device and return 0 on success or an appropriate error code from mperrno.h
extern uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t address,
const uint8_t *data, size_t len,
bool stop);
extern uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint8_t address,
const 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_busio_i2c_read(busio_i2c_obj_t *self, uint16_t address,
extern uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint8_t address,
uint8_t *data, size_t len);
// Do a write and then a read in the same I2C transaction.
uint8_t common_hal_busio_i2c_write_read(busio_i2c_obj_t *self, uint8_t address,
uint8_t *out_data, size_t out_len, uint8_t *in_data, size_t in_len);
// This is used by the supervisor to claim I2C devices indefinitely.
extern void common_hal_busio_i2c_never_reset(busio_i2c_obj_t *self);

View File

@ -112,12 +112,12 @@ void common_hal_displayio_i2cdisplay_send(mp_obj_t obj, display_byte_type_t data
command_bytes[2 * i] = 0x80;
command_bytes[2 * i + 1] = data[i];
}
common_hal_busio_i2c_write(self->bus, self->address, command_bytes, 2 * data_length, true);
common_hal_busio_i2c_write(self->bus, self->address, command_bytes, 2 * data_length);
} else {
uint8_t data_bytes[data_length + 1];
data_bytes[0] = 0x40;
memcpy(data_bytes + 1, data, data_length);
common_hal_busio_i2c_write(self->bus, self->address, data_bytes, data_length + 1, true);
common_hal_busio_i2c_write(self->bus, self->address, data_bytes, data_length + 1);
}
}