Skip to content

Commit d8dfb2e

Browse files
committed
driver_icm20x48: respect timing requirements
1 parent e65ba56 commit d8dfb2e

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

modules/driver_icm20x48/driver_icm20x48.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,38 @@ static uint8_t icm20x48_get_whoami(enum icm20x48_imu_type_t imu_type);
1313

1414
bool icm20x48_init(struct icm20x48_instance_s* instance, uint8_t spi_idx, uint32_t select_line, enum icm20x48_imu_type_t imu_type) {
1515
// Ensure sufficient power-up time has elapsed
16-
chThdSleep(MS2ST(100));
16+
chThdSleep(LL_MS2ST(100));
1717

1818
instance->curr_bank = 99;
1919

20-
spi_device_init(&instance->spi_dev, spi_idx, select_line, 8000000, 16, SPI_DEVICE_FLAG_CPHA|SPI_DEVICE_FLAG_CPOL);
20+
spi_device_init(&instance->spi_dev, spi_idx, select_line, 7000000, 16, SPI_DEVICE_FLAG_CPHA|SPI_DEVICE_FLAG_CPOL);
2121

2222
if (icm20x48_read_reg(instance, ICM20948_REG_WHO_AM_I) != icm20x48_get_whoami(imu_type)) {
2323
return false;
2424
}
2525

2626
// Read USER_CTRL, disable MST_I2C, write USER_CTRL, and wait long enough for any active I2C transaction to complete
2727
icm20x48_write_reg(instance, ICM20948_REG_USER_CTRL, icm20x48_read_reg(instance, ICM20948_REG_USER_CTRL) & ~(1<<5));
28-
chThdSleep(MS2ST(10));
28+
chThdSleep(LL_MS2ST(10));
2929
// Perform a device reset, wait for completion, then wake the device
3030
// Datasheet is unclear on time required for wait time after reset, but mentions 100ms under "start-up time for register read/write from power-up"
3131
icm20x48_write_reg(instance, ICM20948_REG_PWR_MGMT_1, 1<<7);
32-
usleep(10000);
32+
chThdSleep(LL_MS2ST(100));
3333

3434
icm20x48_write_reg(instance, ICM20948_REG_PWR_MGMT_1, 1);
3535
// Wait for reset to complete
3636
{
3737
uint32_t tbegin = chVTGetSystemTimeX();
3838
while (icm20x48_read_reg(instance, ICM20948_REG_PWR_MGMT_1) & 1<<7) {
3939
uint32_t tnow = chVTGetSystemTimeX();
40-
if (tnow-tbegin > MS2ST(100)) {
40+
if (tnow-tbegin > LL_MS2ST(100)) {
4141
return false;
4242
}
4343
}
4444
}
4545

46+
chThdSleep(LL_MS2ST(10));
47+
4648
return true;
4749
}
4850

@@ -88,6 +90,7 @@ uint8_t icm20x48_read_reg(struct icm20x48_instance_s* instance, uint16_t reg){
8890
}
8991
spi_device_begin(&instance->spi_dev);
9092
spi_device_exchange(&instance->spi_dev, 1, &_reg, &ret);
93+
chThdSleepMicroseconds(2);
9194
spi_device_end(&instance->spi_dev);
9295
return ret;
9396
}
@@ -100,6 +103,7 @@ void icm20x48_write_reg(struct icm20x48_instance_s* instance, uint16_t reg, uint
100103
}
101104
spi_device_begin(&instance->spi_dev);
102105
spi_device_send(&instance->spi_dev, 1 , &data);
106+
chThdSleepMicroseconds(2);
103107
spi_device_end(&instance->spi_dev);
104108
}
105109

0 commit comments

Comments
 (0)