From 19018760a56f3505a904947cad4c1b4c7e940eeb Mon Sep 17 00:00:00 2001 From: Alexandre Abadie Date: Wed, 1 Dec 2021 14:32:41 +0100 Subject: [PATCH] drivers/mhz19: migrate to ztimer --- drivers/mhz19/Kconfig | 6 ++++-- drivers/mhz19/Makefile.dep | 3 ++- drivers/mhz19/mhz19_pwm.c | 16 ++++++++-------- drivers/mhz19/mhz19_uart.c | 8 ++++---- tests/driver_mhz19/Makefile | 3 ++- tests/driver_mhz19/app.config.test | 3 ++- tests/driver_mhz19/main.c | 4 ++-- 7 files changed, 24 insertions(+), 19 deletions(-) diff --git a/drivers/mhz19/Kconfig b/drivers/mhz19/Kconfig index 3b1f7c34b2..c57d19de1e 100644 --- a/drivers/mhz19/Kconfig +++ b/drivers/mhz19/Kconfig @@ -15,14 +15,16 @@ config MODULE_MHZ19_UART depends on HAS_PERIPH_UART select MODULE_PERIPH_UART select MODULE_MHZ19 - select MODULE_XTIMER + select MODULE_ZTIMER + select MODULE_ZTIMER_MSEC config MODULE_MHZ19_PWM bool "MH-Z19 over PWM" depends on HAS_PERIPH_GPIO select MODULE_PERIPH_GPIO select MODULE_MHZ19 - select MODULE_XTIMER + select MODULE_ZTIMER + select MODULE_ZTIMER_MSEC config MODULE_MHZ19 bool diff --git a/drivers/mhz19/Makefile.dep b/drivers/mhz19/Makefile.dep index 1798ce1a09..0d36787168 100644 --- a/drivers/mhz19/Makefile.dep +++ b/drivers/mhz19/Makefile.dep @@ -1,4 +1,5 @@ -USEMODULE += xtimer +USEMODULE += ztimer +USEMODULE += ztimer_msec ifneq (,$(filter mhz19_pwm,$(USEMODULE))) FEATURES_REQUIRED += periph_gpio diff --git a/drivers/mhz19/mhz19_pwm.c b/drivers/mhz19/mhz19_pwm.c index 34a0fb4bc4..b7dfb021e0 100644 --- a/drivers/mhz19/mhz19_pwm.c +++ b/drivers/mhz19/mhz19_pwm.c @@ -22,7 +22,7 @@ #include "mhz19.h" #include "mhz19_params.h" -#include "xtimer.h" +#include "ztimer.h" #include "mutex.h" #define ENABLE_DEBUG 0 @@ -61,26 +61,26 @@ int mhz19_get_ppm(mhz19_t *dev, int16_t *ppm) DEBUG("%s: Waiting for high level to end\n", __func__); while (gpio_read(dev->pin) && timeout) { timeout--; - xtimer_msleep(1); + ztimer_sleep(ZTIMER_MSEC, 1); } DEBUG("%s: Waiting for initial rising edge\n", __func__); while (!gpio_read(dev->pin) && timeout) { timeout--; - xtimer_msleep(1); + ztimer_sleep(ZTIMER_MSEC, 1); } - start = xtimer_now_usec() / US_PER_MS; + start = ztimer_now(ZTIMER_MSEC); DEBUG("%s: Waiting for falling edge\n", __func__); while (gpio_read(dev->pin) && timeout) { timeout--; - xtimer_msleep(1); + ztimer_sleep(ZTIMER_MSEC, 1); } - middle = xtimer_now_usec() / US_PER_MS; + middle = ztimer_now(ZTIMER_MSEC); DEBUG("%s: Waiting for rising edge\n", __func__); while (!gpio_read(dev->pin) && timeout) { timeout--; - xtimer_msleep(1); + ztimer_sleep(ZTIMER_MSEC, 1); } /* If we waited too long for flanks, something went wrong */ @@ -88,7 +88,7 @@ int mhz19_get_ppm(mhz19_t *dev, int16_t *ppm) DEBUG("%s: Measurement timed out\n", __func__); return MHZ19_ERR_TIMEOUT; } - end = xtimer_now_usec() / US_PER_MS; + end = ztimer_now(ZTIMER_MSEC); th = (middle - start); tl = (end - middle); diff --git a/drivers/mhz19/mhz19_uart.c b/drivers/mhz19/mhz19_uart.c index 887bb2b778..1b4e1d38c6 100644 --- a/drivers/mhz19/mhz19_uart.c +++ b/drivers/mhz19/mhz19_uart.c @@ -24,7 +24,7 @@ #include "mhz19.h" #include "mhz19_params.h" -#include "xtimer.h" +#include "ztimer.h" #include "mutex.h" #define ENABLE_DEBUG 0 @@ -137,7 +137,7 @@ static void mhz19_cmd(mhz19_t *dev, const uint8_t *in) uart_write(dev->params->uart, in, MHZ19_BUF_SIZE + 1); /* Add some delay after executing command */ - xtimer_msleep(MHZ19_TIMEOUT_CMD); + ztimer_sleep(ZTIMER_MSEC, MHZ19_TIMEOUT_CMD); /* Unlock concurrency guard mutex */ mutex_unlock(&dev->mutex); @@ -165,10 +165,10 @@ static void mhz19_xmit(mhz19_t *dev, const uint8_t *in) /* By locking the same mutex another time, this thread blocks until * the UART ISR received all bytes and unlocks the mutex. If that does not - * happen, then xtimer_mutex_lock_timeout unlocks the mutex after as well + * happen, then ztimer_mutex_lock_timeout unlocks the mutex after as well * after the timeout expired. */ - xtimer_mutex_lock_timeout(&dev->sync, MHZ19_TIMEOUT_CMD * US_PER_MS); + ztimer_mutex_lock_timeout(ZTIMER_MSEC, &dev->sync, MHZ19_TIMEOUT_CMD); /* Unlock synchronization for next transmission */ mutex_unlock(&dev->sync); diff --git a/tests/driver_mhz19/Makefile b/tests/driver_mhz19/Makefile index 0516e545b7..00fc32c2bd 100644 --- a/tests/driver_mhz19/Makefile +++ b/tests/driver_mhz19/Makefile @@ -1,6 +1,7 @@ include ../Makefile.tests_common -USEMODULE += xtimer +USEMODULE += ztimer +USEMODULE += ztimer_msec # set default device parameters in case they are undefined TEST_MODE ?= 1 diff --git a/tests/driver_mhz19/app.config.test b/tests/driver_mhz19/app.config.test index c53768432f..84b8d1a002 100644 --- a/tests/driver_mhz19/app.config.test +++ b/tests/driver_mhz19/app.config.test @@ -1,6 +1,7 @@ # this file enables modules defined in Kconfig. Do not use this file for # application configuration. This is only needed during migration. -CONFIG_MODULE_XTIMER=y +CONFIG_MODULE_ZTIMER=y +CONFIG_MODULE_ZTIMER_MSEC=y # Use UART mode by default CONFIG_MODULE_MHZ19_UART=y diff --git a/tests/driver_mhz19/main.c b/tests/driver_mhz19/main.c index 7a17717fde..9f80d1659d 100644 --- a/tests/driver_mhz19/main.c +++ b/tests/driver_mhz19/main.c @@ -30,7 +30,7 @@ #endif #include -#include "xtimer.h" +#include "ztimer.h" #include "mhz19.h" #include "mhz19_params.h" @@ -79,7 +79,7 @@ int main(void) printf("CO2: %d ppm\n", ppm); /* sleep between measurements */ - xtimer_sleep(1); + ztimer_sleep(ZTIMER_MSEC, 1000); } return 0;