Skip to content

Commit

Permalink
Merge pull request #17358 from aabadie/pr/drivers/ztimer_bis
Browse files Browse the repository at this point in the history
drivers: migrate mpu9x50 and si70xx to ztimer
  • Loading branch information
fjmolinas committed Dec 9, 2021
2 parents d959ce7 + 3a8a543 commit e8d8759
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 30 deletions.
3 changes: 2 additions & 1 deletion drivers/mpu9x50/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ config MODULE_MPU9X50
bool
depends on HAS_PERIPH_I2C
select MODULE_PERIPH_I2C
select MODULE_XTIMER
select MODULE_ZTIMER
select MODULE_ZTIMER_MSEC

endif # TEST_KCONFIG
3 changes: 2 additions & 1 deletion drivers/mpu9x50/Makefile.dep
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
FEATURES_REQUIRED += periph_i2c
USEMODULE += xtimer
USEMODULE += ztimer
USEMODULE += ztimer_msec
8 changes: 4 additions & 4 deletions drivers/mpu9x50/include/mpu9x50_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ extern "C" {
* @name Sleep times in microseconds
* @{
*/
#define MPU9X50_COMP_MODE_SLEEP_US (1000)
#define MPU9X50_BYPASS_SLEEP_US (3000)
#define MPU9X50_PWR_CHANGE_SLEEP_US (50000)
#define MPU9X50_RESET_SLEEP_US (100000)
#define MPU9X50_COMP_MODE_SLEEP_MS (1) /**< 1ms sleep for comp mode */
#define MPU9X50_BYPASS_SLEEP_MS (3) /**< 3ms sleep for bypass */
#define MPU9X50_PWR_CHANGE_SLEEP_MS (50) /**< 50ms sleep to change power */
#define MPU9X50_RESET_SLEEP_MS (100) /**< 100ms sleep during driver reset */
/** @} */

/**
Expand Down
22 changes: 11 additions & 11 deletions drivers/mpu9x50/mpu9x50.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "mpu9x50_regs.h"
#include "mpu9x50_internal.h"
#include "periph/i2c.h"
#include "xtimer.h"
#include "ztimer.h"
#include "byteorder.h"

#define ENABLE_DEBUG 0
Expand Down Expand Up @@ -73,7 +73,7 @@ int mpu9x50_init(mpu9x50_t *dev, const mpu9x50_params_t *params)

/* Reset MPU9X50 registers and afterwards wake up the chip */
i2c_write_reg(DEV_I2C, DEV_ADDR, MPU9X50_PWR_MGMT_1_REG, MPU9X50_PWR_RESET, 0);
xtimer_usleep(MPU9X50_RESET_SLEEP_US);
ztimer_sleep(ZTIMER_MSEC, MPU9X50_RESET_SLEEP_MS);
i2c_write_reg(DEV_I2C, DEV_ADDR, MPU9X50_PWR_MGMT_1_REG, MPU9X50_PWR_WAKEUP, 0);

/* Release the bus, it is acquired again inside each function */
Expand Down Expand Up @@ -103,7 +103,7 @@ int mpu9x50_init(mpu9x50_t *dev, const mpu9x50_params_t *params)
temp &= ~(MPU9X50_PWR_ACCEL | MPU9X50_PWR_GYRO);
i2c_write_reg(DEV_I2C, DEV_ADDR, MPU9X50_PWR_MGMT_2_REG, temp, 0);
i2c_release(DEV_I2C);
xtimer_usleep(MPU9X50_PWR_CHANGE_SLEEP_US);
ztimer_sleep(ZTIMER_MSEC, MPU9X50_PWR_CHANGE_SLEEP_MS);

return 0;
}
Expand Down Expand Up @@ -142,7 +142,7 @@ int mpu9x50_set_accel_power(mpu9x50_t *dev, mpu9x50_pwr_t pwr_conf)
i2c_release(DEV_I2C);

dev->conf.accel_pwr = pwr_conf;
xtimer_usleep(MPU9X50_PWR_CHANGE_SLEEP_US);
ztimer_sleep(ZTIMER_MSEC, MPU9X50_PWR_CHANGE_SLEEP_MS);

return 0;
}
Expand Down Expand Up @@ -188,7 +188,7 @@ int mpu9x50_set_gyro_power(mpu9x50_t *dev, mpu9x50_pwr_t pwr_conf)
i2c_release(DEV_I2C);

dev->conf.gyro_pwr = pwr_conf;
xtimer_usleep(MPU9X50_PWR_CHANGE_SLEEP_US);
ztimer_sleep(ZTIMER_MSEC, MPU9X50_PWR_CHANGE_SLEEP_MS);

return 0;
}
Expand Down Expand Up @@ -231,7 +231,7 @@ int mpu9x50_set_compass_power(mpu9x50_t *dev, mpu9x50_pwr_t pwr_conf)
i2c_release(DEV_I2C);

dev->conf.compass_pwr = pwr_conf;
xtimer_usleep(MPU9X50_PWR_CHANGE_SLEEP_US);
ztimer_sleep(ZTIMER_MSEC, MPU9X50_PWR_CHANGE_SLEEP_MS);

return 0;
}
Expand Down Expand Up @@ -492,18 +492,18 @@ static int compass_init(mpu9x50_t *dev)

/* Configure Power Down mode */
i2c_write_reg(DEV_I2C, DEV_COMP_ADDR, COMPASS_CNTL_REG, MPU9X50_COMP_POWER_DOWN, 0);
xtimer_usleep(MPU9X50_COMP_MODE_SLEEP_US);
ztimer_sleep(ZTIMER_MSEC, MPU9X50_COMP_MODE_SLEEP_MS);
/* Configure Fuse ROM access */
i2c_write_reg(DEV_I2C, DEV_COMP_ADDR, COMPASS_CNTL_REG, MPU9X50_COMP_FUSE_ROM, 0);
xtimer_usleep(MPU9X50_COMP_MODE_SLEEP_US);
ztimer_sleep(ZTIMER_MSEC, MPU9X50_COMP_MODE_SLEEP_MS);
/* Read sensitivity adjustment values from Fuse ROM */
i2c_read_regs(DEV_I2C, DEV_COMP_ADDR, COMPASS_ASAX_REG, data, 3, 0);
dev->conf.compass_x_adj = data[0];
dev->conf.compass_y_adj = data[1];
dev->conf.compass_z_adj = data[2];
/* Configure Power Down mode again */
i2c_write_reg(DEV_I2C, DEV_COMP_ADDR, COMPASS_CNTL_REG, MPU9X50_COMP_POWER_DOWN, 0);
xtimer_usleep(MPU9X50_COMP_MODE_SLEEP_US);
ztimer_sleep(ZTIMER_MSEC, MPU9X50_COMP_MODE_SLEEP_MS);

/* Disable Bypass Mode to configure MPU as master to the compass */
conf_bypass(dev, 0);
Expand Down Expand Up @@ -553,13 +553,13 @@ static void conf_bypass(const mpu9x50_t *dev, uint8_t bypass_enable)
if (bypass_enable) {
data &= ~(BIT_I2C_MST_EN);
i2c_write_reg(DEV_I2C, DEV_ADDR, MPU9X50_USER_CTRL_REG, data, 0);
xtimer_usleep(MPU9X50_BYPASS_SLEEP_US);
ztimer_sleep(ZTIMER_MSEC, MPU9X50_BYPASS_SLEEP_MS);
i2c_write_reg(DEV_I2C, DEV_ADDR, MPU9X50_INT_PIN_CFG_REG, BIT_I2C_BYPASS_EN, 0);
}
else {
data |= BIT_I2C_MST_EN;
i2c_write_reg(DEV_I2C, DEV_ADDR, MPU9X50_USER_CTRL_REG, data, 0);
xtimer_usleep(MPU9X50_BYPASS_SLEEP_US);
ztimer_sleep(ZTIMER_MSEC, MPU9X50_BYPASS_SLEEP_MS);
i2c_write_reg(DEV_I2C, DEV_ADDR, MPU9X50_INT_PIN_CFG_REG, REG_RESET, 0);
}
}
Expand Down
5 changes: 3 additions & 2 deletions drivers/si70xx/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ config MODULE_SI70XX
depends on TEST_KCONFIG
depends on HAS_PERIPH_I2C
select MODULE_PERIPH_I2C
select MODULE_XTIMER
select MODULE_ZTIMER
select MODULE_ZTIMER_MSEC

choice
bool "Sensor variant"
Expand Down Expand Up @@ -56,4 +57,4 @@ config HAVE_SI7021
bool
select MODULE_SI70XX if MODULE_SAUL_DEFAULT
help
Indicates that a si7021 sensor is present.
Indicates that a si7021 sensor is present.
3 changes: 2 additions & 1 deletion drivers/si70xx/Makefile.dep
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
USEMODULE += xtimer
USEMODULE += ztimer
USEMODULE += ztimer_msec
FEATURES_REQUIRED += periph_i2c
4 changes: 2 additions & 2 deletions drivers/si70xx/si70xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/

#include <string.h>
#include "xtimer.h"
#include "ztimer.h"

#include "si70xx_internals.h"
#include "si70xx.h"
Expand Down Expand Up @@ -162,7 +162,7 @@ int si70xx_init(si70xx_t *dev, const si70xx_params_t *params)
i2c_release(SI70XX_I2C);

/* sensor is ready after at most 25 ms */
xtimer_msleep(25);
ztimer_sleep(ZTIMER_MSEC, 25);

DEBUG("[DEBUG] Device initialized with success.\n");
return SI70XX_OK;
Expand Down
3 changes: 2 additions & 1 deletion tests/driver_mpu9x50/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include ../Makefile.tests_common
DRIVER ?= mpu9150

USEMODULE += $(DRIVER)
USEMODULE += xtimer
USEMODULE += ztimer
USEMODULE += ztimer_msec

include $(RIOTBASE)/Makefile.include
3 changes: 2 additions & 1 deletion tests/driver_mpu9x50/app.config.test
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# this file enables modules defined in Kconfig. Do not use this file for
# application configuration. This is only needed during migration.
CONFIG_MODULE_MPU9150=y
CONFIG_MODULE_XTIMER=y
CONFIG_MODULE_ZTIMER=y
CONFIG_MODULE_ZTIMER_MSEC=y
7 changes: 3 additions & 4 deletions tests/driver_mpu9x50/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@
#include <stdio.h>
#include <inttypes.h>

#include "xtimer.h"
#include "timex.h"
#include "ztimer.h"
#include "board.h"

#include "mpu9x50.h"
#include "mpu9x50_params.h"

#define SLEEP_USEC (1000 * 1000u)

int main(void)
{
mpu9x50_t dev;
Expand Down Expand Up @@ -92,7 +91,7 @@ int main(void)
printf("Temperature [milli deg] : %"PRId32"\n", temperature);
printf("\n+-------------------------------------+\n");

xtimer_usleep(SLEEP_USEC);
ztimer_sleep(ZTIMER_MSEC, MS_PER_SEC); /* 1s delay */
}

return 0;
Expand Down
3 changes: 3 additions & 0 deletions tests/driver_si70xx/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ SI70XX_VARIANT ?= si7021

USEMODULE += $(SI70XX_VARIANT)

USEMODULE += ztimer
USEMODULE += ztimer_msec

include $(RIOTBASE)/Makefile.include
3 changes: 3 additions & 0 deletions tests/driver_si70xx/app.config.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
# This test should also work with Si7006, Si7013 and Si7020 variants.
CONFIG_MODULE_SI70XX=y
CONFIG_MODULE_SI7021=y

CONFIG_MODULE_ZTIMER=y
CONFIG_MODULE_ZTIMER_MSEC=y
5 changes: 3 additions & 2 deletions tests/driver_si70xx/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

#include <stdio.h>

#include "xtimer.h"
#include "timex.h"
#include "ztimer.h"

#include "si70xx_params.h"
#include "si70xx.h"
Expand Down Expand Up @@ -74,7 +75,7 @@ int main(void)
temperature % 100);

/* sleep between measurements */
xtimer_msleep(1000);
ztimer_sleep(ZTIMER_MSEC, MS_PER_SEC); /* 1s delay */
}

return 0;
Expand Down

0 comments on commit e8d8759

Please sign in to comment.