Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers/mhz19: migrate to ztimer #17308

Merged
merged 1 commit into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions drivers/mhz19/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion drivers/mhz19/Makefile.dep
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
USEMODULE += xtimer
USEMODULE += ztimer
USEMODULE += ztimer_msec

ifneq (,$(filter mhz19_pwm,$(USEMODULE)))
FEATURES_REQUIRED += periph_gpio
Expand Down
16 changes: 8 additions & 8 deletions drivers/mhz19/mhz19_pwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#include "mhz19.h"
#include "mhz19_params.h"
#include "xtimer.h"
#include "ztimer.h"
#include "mutex.h"

#define ENABLE_DEBUG 0
Expand Down Expand Up @@ -61,34 +61,34 @@ 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 */
if (!timeout) {
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);
Expand Down
8 changes: 4 additions & 4 deletions drivers/mhz19/mhz19_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#include "mhz19.h"
#include "mhz19_params.h"
#include "xtimer.h"
#include "ztimer.h"
#include "mutex.h"

#define ENABLE_DEBUG 0
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion tests/driver_mhz19/Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion tests/driver_mhz19/app.config.test
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/driver_mhz19/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#endif

#include <stdio.h>
#include "xtimer.h"
#include "ztimer.h"
#include "mhz19.h"
#include "mhz19_params.h"

Expand Down Expand Up @@ -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;
Expand Down