1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

drivers/mhz19: migrate to ztimer

This commit is contained in:
Alexandre Abadie 2021-12-01 14:32:41 +01:00
parent 7a8f840627
commit 19018760a5
No known key found for this signature in database
GPG Key ID: 1C919A403CAE1405
7 changed files with 24 additions and 19 deletions

View File

@ -15,14 +15,16 @@ config MODULE_MHZ19_UART
depends on HAS_PERIPH_UART depends on HAS_PERIPH_UART
select MODULE_PERIPH_UART select MODULE_PERIPH_UART
select MODULE_MHZ19 select MODULE_MHZ19
select MODULE_XTIMER select MODULE_ZTIMER
select MODULE_ZTIMER_MSEC
config MODULE_MHZ19_PWM config MODULE_MHZ19_PWM
bool "MH-Z19 over PWM" bool "MH-Z19 over PWM"
depends on HAS_PERIPH_GPIO depends on HAS_PERIPH_GPIO
select MODULE_PERIPH_GPIO select MODULE_PERIPH_GPIO
select MODULE_MHZ19 select MODULE_MHZ19
select MODULE_XTIMER select MODULE_ZTIMER
select MODULE_ZTIMER_MSEC
config MODULE_MHZ19 config MODULE_MHZ19
bool bool

View File

@ -1,4 +1,5 @@
USEMODULE += xtimer USEMODULE += ztimer
USEMODULE += ztimer_msec
ifneq (,$(filter mhz19_pwm,$(USEMODULE))) ifneq (,$(filter mhz19_pwm,$(USEMODULE)))
FEATURES_REQUIRED += periph_gpio FEATURES_REQUIRED += periph_gpio

View File

@ -22,7 +22,7 @@
#include "mhz19.h" #include "mhz19.h"
#include "mhz19_params.h" #include "mhz19_params.h"
#include "xtimer.h" #include "ztimer.h"
#include "mutex.h" #include "mutex.h"
#define ENABLE_DEBUG 0 #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__); DEBUG("%s: Waiting for high level to end\n", __func__);
while (gpio_read(dev->pin) && timeout) { while (gpio_read(dev->pin) && timeout) {
timeout--; timeout--;
xtimer_msleep(1); ztimer_sleep(ZTIMER_MSEC, 1);
} }
DEBUG("%s: Waiting for initial rising edge\n", __func__); DEBUG("%s: Waiting for initial rising edge\n", __func__);
while (!gpio_read(dev->pin) && timeout) { while (!gpio_read(dev->pin) && timeout) {
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__); DEBUG("%s: Waiting for falling edge\n", __func__);
while (gpio_read(dev->pin) && timeout) { while (gpio_read(dev->pin) && timeout) {
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__); DEBUG("%s: Waiting for rising edge\n", __func__);
while (!gpio_read(dev->pin) && timeout) { while (!gpio_read(dev->pin) && timeout) {
timeout--; timeout--;
xtimer_msleep(1); ztimer_sleep(ZTIMER_MSEC, 1);
} }
/* If we waited too long for flanks, something went wrong */ /* 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__); DEBUG("%s: Measurement timed out\n", __func__);
return MHZ19_ERR_TIMEOUT; return MHZ19_ERR_TIMEOUT;
} }
end = xtimer_now_usec() / US_PER_MS; end = ztimer_now(ZTIMER_MSEC);
th = (middle - start); th = (middle - start);
tl = (end - middle); tl = (end - middle);

View File

@ -24,7 +24,7 @@
#include "mhz19.h" #include "mhz19.h"
#include "mhz19_params.h" #include "mhz19_params.h"
#include "xtimer.h" #include "ztimer.h"
#include "mutex.h" #include "mutex.h"
#define ENABLE_DEBUG 0 #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); uart_write(dev->params->uart, in, MHZ19_BUF_SIZE + 1);
/* Add some delay after executing command */ /* Add some delay after executing command */
xtimer_msleep(MHZ19_TIMEOUT_CMD); ztimer_sleep(ZTIMER_MSEC, MHZ19_TIMEOUT_CMD);
/* Unlock concurrency guard mutex */ /* Unlock concurrency guard mutex */
mutex_unlock(&dev->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 /* 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 * 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. * 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 */ /* Unlock synchronization for next transmission */
mutex_unlock(&dev->sync); mutex_unlock(&dev->sync);

View File

@ -1,6 +1,7 @@
include ../Makefile.tests_common include ../Makefile.tests_common
USEMODULE += xtimer USEMODULE += ztimer
USEMODULE += ztimer_msec
# set default device parameters in case they are undefined # set default device parameters in case they are undefined
TEST_MODE ?= 1 TEST_MODE ?= 1

View File

@ -1,6 +1,7 @@
# this file enables modules defined in Kconfig. Do not use this file for # this file enables modules defined in Kconfig. Do not use this file for
# application configuration. This is only needed during migration. # 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 # Use UART mode by default
CONFIG_MODULE_MHZ19_UART=y CONFIG_MODULE_MHZ19_UART=y

View File

@ -30,7 +30,7 @@
#endif #endif
#include <stdio.h> #include <stdio.h>
#include "xtimer.h" #include "ztimer.h"
#include "mhz19.h" #include "mhz19.h"
#include "mhz19_params.h" #include "mhz19_params.h"
@ -79,7 +79,7 @@ int main(void)
printf("CO2: %d ppm\n", ppm); printf("CO2: %d ppm\n", ppm);
/* sleep between measurements */ /* sleep between measurements */
xtimer_sleep(1); ztimer_sleep(ZTIMER_MSEC, 1000);
} }
return 0; return 0;