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.
This commit is contained in:
Damien George 2017-09-08 11:19:40 +10:00
parent b8ee7ab5b9
commit 19f1b39d6f
1 changed files with 3 additions and 6 deletions

View File

@ -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));
}
}