Skip to content

Commit 1e7328c

Browse files
robert-hhdpgeorge
authored andcommitted
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 <[email protected]>
1 parent b85ad4b commit 1e7328c

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

ports/mimxrt/machine_i2c.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
#define DEFAULT_I2C_FREQ (400000)
3838
#define DEFAULT_I2C_DRIVE (6)
39+
#define DEFAULT_I2C_TIMEOUT (50000)
3940

4041
typedef struct _machine_i2c_obj_t {
4142
mp_obj_base_t base;
@@ -82,16 +83,18 @@ bool lpi2c_set_iomux(int8_t hw_i2c, uint8_t drive) {
8283

8384
static void machine_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
8485
machine_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
85-
mp_printf(print, "I2C(%u, freq=%u)",
86-
self->i2c_id, self->master_config->baudRate_Hz);
86+
mp_printf(print, "I2C(%u, freq=%u, timeout=%u)",
87+
self->i2c_id, self->master_config->baudRate_Hz,
88+
self->master_config->pinLowTimeout_ns / 1000);
8789
}
8890

8991
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) {
90-
enum { ARG_id, ARG_freq, ARG_drive};
92+
enum { ARG_id, ARG_freq, ARG_drive, ARG_timeout};
9193
static const mp_arg_t allowed_args[] = {
9294
{ MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ },
9395
{ MP_QSTR_freq, MP_ARG_INT, {.u_int = DEFAULT_I2C_FREQ} },
9496
{ MP_QSTR_drive, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_I2C_DRIVE} },
97+
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_I2C_TIMEOUT} },
9598
};
9699

97100
// 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
121124
LPI2C_MasterGetDefaultConfig(self->master_config);
122125
// Initialise the I2C peripheral.
123126
self->master_config->baudRate_Hz = args[ARG_freq].u_int;
127+
if (args[ARG_timeout].u_int >= 0) {
128+
self->master_config->pinLowTimeout_ns = args[ARG_timeout].u_int * 1000; // to be set as ns
129+
}
124130
LPI2C_MasterInit(self->i2c_inst, self->master_config, BOARD_BOOTCLOCKRUN_LPI2C_CLK_ROOT);
125131

126132
return MP_OBJ_FROM_PTR(self);

0 commit comments

Comments
 (0)