aboutsummaryrefslogtreecommitdiff
path: root/extmod/machine_i2c.c
diff options
context:
space:
mode:
authorRadomir Dopieralski <openstack@sheep.art.pl>2016-10-22 22:44:19 +0200
committerDamien George <damien.p.george@gmail.com>2016-11-17 12:43:12 +1100
commit702928915c49c2f7af7a5de289a05c646518900b (patch)
treec5a0e741c32da965311586efcb090fc11f5a601e /extmod/machine_i2c.c
parentb188d6e9db1842f9f66976ef1d67746239c8006c (diff)
extmod/machine_i2c: Make the clock stretching timeout configurable
Diffstat (limited to 'extmod/machine_i2c.c')
-rw-r--r--extmod/machine_i2c.c10
1 files changed, 5 insertions, 5 deletions
diff --git a/extmod/machine_i2c.c b/extmod/machine_i2c.c
index e201b2399..5b5a0ece5 100644
--- a/extmod/machine_i2c.c
+++ b/extmod/machine_i2c.c
@@ -34,12 +34,10 @@
#if MICROPY_PY_MACHINE_I2C
-// Clock stretching limit, so that we don't get stuck.
-#define I2C_STRETCH_LIMIT 255
-
typedef struct _machine_i2c_obj_t {
mp_obj_base_t base;
uint32_t us_delay;
+ uint32_t us_timeout;
mp_hal_pin_obj_t scl;
mp_hal_pin_obj_t sda;
} machine_i2c_obj_t;
@@ -58,7 +56,7 @@ STATIC void mp_hal_i2c_scl_release(machine_i2c_obj_t *self) {
mp_hal_pin_od_high(self->scl);
mp_hal_i2c_delay(self);
// For clock stretching, wait for the SCL pin to be released, with timeout.
- for (int count = I2C_STRETCH_LIMIT; mp_hal_pin_read(self->scl) == 0 && count; --count) {
+ for (uint32_t count = self->us_timeout; mp_hal_pin_read(self->scl) == 0 && count; --count) {
mp_hal_delay_us_fast(1);
}
}
@@ -237,16 +235,18 @@ STATIC void mp_hal_i2c_read(machine_i2c_obj_t *self, uint8_t addr, uint8_t *dest
// MicroPython bindings for I2C
STATIC void machine_i2c_obj_init_helper(machine_i2c_obj_t *self, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
- enum { ARG_scl, ARG_sda, ARG_freq };
+ enum { ARG_scl, ARG_sda, ARG_freq, ARG_timeout };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 400000} },
+ { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 255} },
};
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);
self->scl = mp_hal_get_pin_obj(args[ARG_scl].u_obj);
self->sda = mp_hal_get_pin_obj(args[ARG_sda].u_obj);
+ self->us_timeout = args[ARG_timeout].u_int;
mp_hal_i2c_init(self, args[ARG_freq].u_int);
}