From 9ed7d2dc18f540185a739b065f0ae25e4ec59f63 Mon Sep 17 00:00:00 2001 From: Kees Bakker Date: Sun, 6 Jan 2019 16:23:58 +0100 Subject: [PATCH 1/2] drivers/ds18: refactor functions with const dev pointer All DS18 functions have a dev argument. All except the init function use it as an IN parameter, so we can prototype it as const ds18_t*. As a consequence we can drop the cast in read_temperature() in ds18_saul.c which was the primary trigger for the changes. The commit also follows the preferred convention that "params" is a field in the device struct. Only the init function needs to write it. --- drivers/ds18/ds18.c | 40 ++++++++++++++++++++-------------------- drivers/ds18/ds18_saul.c | 2 +- drivers/include/ds18.h | 25 ++++++++++++------------- 3 files changed, 33 insertions(+), 34 deletions(-) diff --git a/drivers/ds18/ds18.c b/drivers/ds18/ds18.c index f2bae940fe..43b0d71b1d 100644 --- a/drivers/ds18/ds18.c +++ b/drivers/ds18/ds18.c @@ -30,20 +30,20 @@ #define ENABLE_DEBUG (0) #include "debug.h" -static void ds18_low(ds18_t *dev) +static void ds18_low(const ds18_t *dev) { /* Set gpio as output and clear pin */ - gpio_init(dev->pin, GPIO_OUT); - gpio_clear(dev->pin); + gpio_init(dev->params.pin, GPIO_OUT); + gpio_clear(dev->params.pin); } -static void ds18_release(ds18_t *dev) +static void ds18_release(const ds18_t *dev) { /* Init pin as input */ - gpio_init(dev->pin, dev->in_mode); + gpio_init(dev->params.pin, dev->params.in_mode); } -static void ds18_write_bit(ds18_t *dev, uint8_t bit) +static void ds18_write_bit(const ds18_t *dev, uint8_t bit) { /* Initiate write slot */ ds18_low(dev); @@ -59,7 +59,7 @@ static void ds18_write_bit(ds18_t *dev, uint8_t bit) xtimer_usleep(1); } -static int ds18_read_bit(ds18_t *dev, uint8_t *bit) +static int ds18_read_bit(const ds18_t *dev, uint8_t *bit) { /* Initiate read slot */ ds18_low(dev); @@ -67,7 +67,7 @@ static int ds18_read_bit(ds18_t *dev, uint8_t *bit) #if defined(MODULE_DS18_OPTIMIZED) xtimer_usleep(DS18_SAMPLE_TIME); - *bit = gpio_read(dev->pin); + *bit = gpio_read(dev->params.pin); xtimer_usleep(DS18_DELAY_R_RECOVER); return DS18_OK; #else @@ -75,7 +75,7 @@ static int ds18_read_bit(ds18_t *dev, uint8_t *bit) /* Measure time low of device pin, timeout after slot time*/ start = xtimer_now_usec(); - while (!gpio_read(dev->pin) && measurement < DS18_DELAY_SLOT) { + while (!gpio_read(dev->params.pin) && measurement < DS18_DELAY_SLOT) { measurement = xtimer_now_usec() - start; } @@ -94,7 +94,7 @@ static int ds18_read_bit(ds18_t *dev, uint8_t *bit) #endif } -static int ds18_read_byte(ds18_t *dev, uint8_t *byte) +static int ds18_read_byte(const ds18_t *dev, uint8_t *byte) { uint8_t bit = 0; *byte = 0; @@ -111,14 +111,14 @@ static int ds18_read_byte(ds18_t *dev, uint8_t *byte) return DS18_OK; } -static void ds18_write_byte(ds18_t *dev, uint8_t byte) +static void ds18_write_byte(const ds18_t *dev, uint8_t byte) { for (int i = 0; i < 8; i++) { ds18_write_bit(dev, byte & (0x01 << i)); } } -static int ds18_reset(ds18_t *dev) +static int ds18_reset(const ds18_t *dev) { int res; @@ -131,7 +131,7 @@ static int ds18_reset(ds18_t *dev) xtimer_usleep(DS18_DELAY_PRESENCE); /* Check device presence */ - res = gpio_read(dev->pin); + res = gpio_read(dev->params.pin); /* Sleep for reset delay */ xtimer_usleep(DS18_DELAY_RESET); @@ -139,7 +139,7 @@ static int ds18_reset(ds18_t *dev) return res; } -int ds18_trigger(ds18_t *dev) +int ds18_trigger(const ds18_t *dev) { int res; @@ -156,7 +156,7 @@ int ds18_trigger(ds18_t *dev) return DS18_OK; } -int ds18_read(ds18_t *dev, int16_t *temperature) +int ds18_read(const ds18_t *dev, int16_t *temperature) { int res; uint8_t b1 = 0, b2 = 0; @@ -190,7 +190,7 @@ int ds18_read(ds18_t *dev, int16_t *temperature) return DS18_OK; } -int ds18_get_temperature(ds18_t *dev, int16_t *temperature) +int ds18_get_temperature(const ds18_t *dev, int16_t *temperature) { DEBUG("[DS18] Convert T\n"); @@ -208,14 +208,14 @@ int ds18_init(ds18_t *dev, const ds18_params_t *params) { int res; + dev->params = *params; + /* Deduct the input mode from the output mode. If pull-up resistors are * used for output then will be used for input as well. */ - dev->in_mode = (params->out_mode == GPIO_OD_PU) ? GPIO_IN_PU : GPIO_IN; + dev->params.in_mode = (dev->params.out_mode == GPIO_OD_PU) ? GPIO_IN_PU : GPIO_IN; /* Initialize the device and the pin */ - dev->pin = params->pin; - dev->out_mode = params->out_mode; - res = gpio_init(dev->pin, dev->in_mode) == 0 ? DS18_OK : DS18_ERROR; + res = gpio_init(dev->params.pin, dev->params.in_mode) == 0 ? DS18_OK : DS18_ERROR; return res; } diff --git a/drivers/ds18/ds18_saul.c b/drivers/ds18/ds18_saul.c index 996d3bd295..ffab3cd4fd 100644 --- a/drivers/ds18/ds18_saul.c +++ b/drivers/ds18/ds18_saul.c @@ -26,7 +26,7 @@ static int read_temperature(const void *dev, phydat_t *res) { - if (ds18_get_temperature((ds18_t *)dev, &res->val[0]) == DS18_ERROR) { + if (ds18_get_temperature(dev, &res->val[0]) == DS18_ERROR) { return -ECANCELED; } diff --git a/drivers/include/ds18.h b/drivers/include/ds18.h index 8840c3dccb..2b54902d45 100644 --- a/drivers/include/ds18.h +++ b/drivers/include/ds18.h @@ -53,23 +53,22 @@ extern "C" { /** @} */ -/** - * @brief Device descriptor for a ds18 device - */ -typedef struct { - gpio_t pin; /**< Pin the sensor is connected to */ - gpio_mode_t out_mode; /**< Pin output mode */ - gpio_mode_t in_mode; /**< Pin input mode */ -} ds18_t; - /** * @brief Device initialization parameters */ typedef struct { gpio_t pin; /**< Pin the sensor is connected to */ - gpio_mode_t out_mode; /**< Pin output mode */ + gpio_mode_t out_mode; /**< Pin output mode */ + gpio_mode_t in_mode; /**< Pin input mode (usually deduced from output mode) */ } ds18_params_t; +/** + * @brief Device descriptor for a ds18 device + */ +typedef struct { + ds18_params_t params; /**< Device Parameters */ +} ds18_t; + /** * @brief Initialize a ds18 device * @@ -91,7 +90,7 @@ int ds18_init(ds18_t *dev, const ds18_params_t *params); * @return 0 on success * @return -1 on error */ -int ds18_trigger(ds18_t *dev); +int ds18_trigger(const ds18_t *dev); /** * @brief Reads the scratchpad for the last conversion @@ -102,7 +101,7 @@ int ds18_trigger(ds18_t *dev); * @return 0 on success * @return -1 on error */ -int ds18_read(ds18_t *dev, int16_t *temperature); +int ds18_read(const ds18_t *dev, int16_t *temperature); /** * @brief convenience fuction for triggering a conversion and reading the @@ -117,7 +116,7 @@ int ds18_read(ds18_t *dev, int16_t *temperature); * @return 0 on success * @return -1 on error */ -int ds18_get_temperature(ds18_t *dev, int16_t *temperature); +int ds18_get_temperature(const ds18_t *dev, int16_t *temperature); #ifdef __cplusplus } From b8472550a49985d2ea860aee91806dba8e0e034d Mon Sep 17 00:00:00 2001 From: Kees Bakker Date: Sun, 6 Jan 2019 20:35:27 +0100 Subject: [PATCH 2/2] drivers/ds18: eliminate cast in auto_init_ds18 --- sys/auto_init/saul/auto_init_ds18.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sys/auto_init/saul/auto_init_ds18.c b/sys/auto_init/saul/auto_init_ds18.c index ea12fbac1c..a40dbeb4d0 100644 --- a/sys/auto_init/saul/auto_init_ds18.c +++ b/sys/auto_init/saul/auto_init_ds18.c @@ -55,7 +55,7 @@ void auto_init_ds18(void) LOG_DEBUG("[auto_init_saul] initializing ds18 #%u\n", i); - if (ds18_init(&ds18_devs[i], (ds18_params_t *) p) < 0) { + if (ds18_init(&ds18_devs[i], p) < 0) { LOG_ERROR("[auto_init_saul] error initializing ds18 #%u\n", i); return; }