mimxrt/machine_i2c: Support the timeout keyword argument.
Set the default timeout to 50000 us. The default used to be 0, causing the NXP I2C driver to silently stop working in case of a non-responding device. Signed-off-by: robert-hh <robert@hammelrath.com>
This commit is contained in:
parent
b85ad4bd41
commit
1e7328ca28
1 changed files with 9 additions and 3 deletions
|
|
@ -36,6 +36,7 @@
|
||||||
|
|
||||||
#define DEFAULT_I2C_FREQ (400000)
|
#define DEFAULT_I2C_FREQ (400000)
|
||||||
#define DEFAULT_I2C_DRIVE (6)
|
#define DEFAULT_I2C_DRIVE (6)
|
||||||
|
#define DEFAULT_I2C_TIMEOUT (50000)
|
||||||
|
|
||||||
typedef struct _machine_i2c_obj_t {
|
typedef struct _machine_i2c_obj_t {
|
||||||
mp_obj_base_t base;
|
mp_obj_base_t base;
|
||||||
|
|
@ -82,16 +83,18 @@ bool lpi2c_set_iomux(int8_t hw_i2c, uint8_t drive) {
|
||||||
|
|
||||||
static void machine_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
static void machine_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||||
machine_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
machine_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||||
mp_printf(print, "I2C(%u, freq=%u)",
|
mp_printf(print, "I2C(%u, freq=%u, timeout=%u)",
|
||||||
self->i2c_id, self->master_config->baudRate_Hz);
|
self->i2c_id, self->master_config->baudRate_Hz,
|
||||||
|
self->master_config->pinLowTimeout_ns / 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
|
mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
|
||||||
enum { ARG_id, ARG_freq, ARG_drive};
|
enum { ARG_id, ARG_freq, ARG_drive, ARG_timeout};
|
||||||
static const mp_arg_t allowed_args[] = {
|
static const mp_arg_t allowed_args[] = {
|
||||||
{ MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
{ MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||||
{ MP_QSTR_freq, MP_ARG_INT, {.u_int = DEFAULT_I2C_FREQ} },
|
{ MP_QSTR_freq, MP_ARG_INT, {.u_int = DEFAULT_I2C_FREQ} },
|
||||||
{ MP_QSTR_drive, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_I2C_DRIVE} },
|
{ MP_QSTR_drive, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_I2C_DRIVE} },
|
||||||
|
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_I2C_TIMEOUT} },
|
||||||
};
|
};
|
||||||
|
|
||||||
// Parse args.
|
// Parse args.
|
||||||
|
|
@ -121,6 +124,9 @@ mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
|
||||||
LPI2C_MasterGetDefaultConfig(self->master_config);
|
LPI2C_MasterGetDefaultConfig(self->master_config);
|
||||||
// Initialise the I2C peripheral.
|
// Initialise the I2C peripheral.
|
||||||
self->master_config->baudRate_Hz = args[ARG_freq].u_int;
|
self->master_config->baudRate_Hz = args[ARG_freq].u_int;
|
||||||
|
if (args[ARG_timeout].u_int >= 0) {
|
||||||
|
self->master_config->pinLowTimeout_ns = args[ARG_timeout].u_int * 1000; // to be set as ns
|
||||||
|
}
|
||||||
LPI2C_MasterInit(self->i2c_inst, self->master_config, BOARD_BOOTCLOCKRUN_LPI2C_CLK_ROOT);
|
LPI2C_MasterInit(self->i2c_inst, self->master_config, BOARD_BOOTCLOCKRUN_LPI2C_CLK_ROOT);
|
||||||
|
|
||||||
return MP_OBJ_FROM_PTR(self);
|
return MP_OBJ_FROM_PTR(self);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue