From 19f1b39d6ff723729e993c2559c5eb8d62215d39 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 8 Sep 2017 11:19:40 +1000 Subject: [PATCH] stm32/i2c: When scanning for I2C devices only do 1 probe per address. Previous to this patch the i2c.scan() method would do up to 100 probes per I2C address, to detect the devices on the bus. This repeated probing was a relic from when the code was copied from the accelerometer initialisation, which requires to do repeated probes while waiting for the accelerometer chip to turn on. But I2C devices shouldn't need more than 1 probe to detect their presence, and the generic software I2C implementation uses 1 probe successfully. So this patch changes the implementation to use 1 probe per address, which significantly speeds up the scan operation. --- ports/stm32/i2c.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/ports/stm32/i2c.c b/ports/stm32/i2c.c index d80301081a..fcf3cd9c35 100644 --- a/ports/stm32/i2c.c +++ b/ports/stm32/i2c.c @@ -699,12 +699,9 @@ STATIC mp_obj_t pyb_i2c_scan(mp_obj_t self_in) { mp_obj_t list = mp_obj_new_list(0, NULL); for (uint addr = 0x08; addr <= 0x77; addr++) { - for (int i = 0; i < 10; i++) { - HAL_StatusTypeDef status = HAL_I2C_IsDeviceReady(self->i2c, addr << 1, 10, 200); - if (status == HAL_OK) { - mp_obj_list_append(list, mp_obj_new_int(addr)); - break; - } + HAL_StatusTypeDef status = HAL_I2C_IsDeviceReady(self->i2c, addr << 1, 1, 200); + if (status == HAL_OK) { + mp_obj_list_append(list, MP_OBJ_NEW_SMALL_INT(addr)); } }