Skip to content

machine_i2c: Add I2C bus clear support. #13281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions extmod/machine_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,26 @@ int mp_machine_soft_i2c_transfer(mp_obj_base_t *self_in, uint16_t addr, size_t n
return transfer_ret;
}

// return value:
// 0 - success
// <0 - error, with errno being the negative of the return value
int mp_machine_soft_i2c_clear_bus(mp_obj_base_t *self_in) {
machine_i2c_obj_t *self = (machine_i2c_obj_t *)self_in;

mp_hal_i2c_sda_release(self);
for (int i = 0; i < 10; i++) {
mp_hal_i2c_delay(self);
mp_hal_i2c_scl_low(self);
mp_hal_i2c_delay(self);
int ret = mp_hal_i2c_scl_release(self);
if (ret != 0) {
mp_hal_i2c_stop(self);
return ret;
}
}
return mp_hal_i2c_stop(self);
}

#endif // MICROPY_PY_MACHINE_SOFTI2C

/******************************************************************************/
Expand Down Expand Up @@ -365,6 +385,20 @@ STATIC mp_obj_t machine_i2c_stop(mp_obj_t self_in) {
}
MP_DEFINE_CONST_FUN_OBJ_1(machine_i2c_stop_obj, machine_i2c_stop);

STATIC mp_obj_t machine_i2c_clear_bus(mp_obj_t self_in) {
mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(self_in);
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)MP_OBJ_TYPE_GET_SLOT(self->type, protocol);
if (i2c_p->clear_bus == NULL) {
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("I2C operation not supported"));
}
int ret = i2c_p->clear_bus(self);
if (ret != 0) {
mp_raise_OSError(-ret);
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(machine_i2c_clear_bus_obj, machine_i2c_clear_bus);

STATIC mp_obj_t machine_i2c_readinto(size_t n_args, const mp_obj_t *args) {
mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)MP_OBJ_TYPE_GET_SLOT(self->type, protocol);
Expand Down Expand Up @@ -638,6 +672,7 @@ STATIC const mp_rom_map_elem_t machine_i2c_locals_dict_table[] = {
// primitive I2C operations
{ MP_ROM_QSTR(MP_QSTR_start), MP_ROM_PTR(&machine_i2c_start_obj) },
{ MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&machine_i2c_stop_obj) },
{ MP_ROM_QSTR(MP_QSTR_clear_bus), MP_ROM_PTR(&machine_i2c_clear_bus_obj) },
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&machine_i2c_readinto_obj) },
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&machine_i2c_write_obj) },

Expand Down Expand Up @@ -726,6 +761,7 @@ STATIC const mp_machine_i2c_p_t mp_machine_soft_i2c_p = {
.init = mp_machine_soft_i2c_init,
.start = (int (*)(mp_obj_base_t *))mp_hal_i2c_start,
.stop = (int (*)(mp_obj_base_t *))mp_hal_i2c_stop,
.clear_bus = mp_machine_soft_i2c_clear_bus,
.read = mp_machine_soft_i2c_read,
.write = mp_machine_soft_i2c_write,
.transfer = mp_machine_soft_i2c_transfer,
Expand Down
2 changes: 2 additions & 0 deletions extmod/modmachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ typedef struct _mp_machine_i2c_p_t {
void (*init)(mp_obj_base_t *obj, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
int (*start)(mp_obj_base_t *obj);
int (*stop)(mp_obj_base_t *obj);
int (*clear_bus)(mp_obj_base_t *obj);
int (*read)(mp_obj_base_t *obj, uint8_t *dest, size_t len, bool nack);
int (*write)(mp_obj_base_t *obj, const uint8_t *src, size_t len);
int (*transfer)(mp_obj_base_t *obj, uint16_t addr, size_t n, mp_machine_i2c_buf_t *bufs, unsigned int flags);
Expand Down Expand Up @@ -252,6 +253,7 @@ MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_bitstream_obj);
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_time_pulse_us_obj);

#if MICROPY_PY_MACHINE_I2C
int mp_machine_soft_i2c_clear_bus(mp_obj_base_t *self);
int mp_machine_i2c_transfer_adaptor(mp_obj_base_t *self, uint16_t addr, size_t n, mp_machine_i2c_buf_t *bufs, unsigned int flags);
int mp_machine_soft_i2c_transfer(mp_obj_base_t *self, uint16_t addr, size_t n, mp_machine_i2c_buf_t *bufs, unsigned int flags);
#endif
Expand Down
19 changes: 19 additions & 0 deletions ports/rp2/machine_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,26 @@ STATIC int machine_i2c_transfer_single(mp_obj_base_t *self_in, uint16_t addr, si
}
}

STATIC int machine_i2c_clear_bus(mp_obj_base_t *self_in) {
machine_i2c_obj_t *self = (machine_i2c_obj_t *)self_in;
// Workaround issue with hardware I2C not supporting bus clear
mp_machine_soft_i2c_obj_t soft_i2c = {
.base = { &mp_machine_soft_i2c_type },
.us_delay = 500000 / self->freq + 1,
.us_timeout = self->timeout,
.scl = self->scl,
.sda = self->sda,
};
mp_hal_pin_open_drain(self->scl);
mp_hal_pin_open_drain(self->sda);
int ret = mp_machine_soft_i2c_clear_bus(&soft_i2c.base);
gpio_set_function(self->scl, GPIO_FUNC_I2C);
gpio_set_function(self->sda, GPIO_FUNC_I2C);
return ret;
}

STATIC const mp_machine_i2c_p_t machine_i2c_p = {
.clear_bus = machine_i2c_clear_bus,
.transfer = mp_machine_i2c_transfer_adaptor,
.transfer_single = machine_i2c_transfer_single,
};
Expand Down