From 2b713f13cb26e2911fd92eb7a7f9e37f3df7e694 Mon Sep 17 00:00:00 2001 From: Alexandre Abadie Date: Fri, 21 Feb 2020 12:05:15 +0100 Subject: [PATCH] drivers/srf08: refactor driver configuration --- drivers/Makefile.include | 4 ++ drivers/include/srf08.h | 19 +++++---- drivers/srf08/include/srf08_params.h | 63 ++++++++++++++++++++++++++++ drivers/srf08/srf08.c | 36 +++++++++------- 4 files changed, 98 insertions(+), 24 deletions(-) create mode 100644 drivers/srf08/include/srf08_params.h diff --git a/drivers/Makefile.include b/drivers/Makefile.include index 55fd9b9e82..e0c2867ea7 100644 --- a/drivers/Makefile.include +++ b/drivers/Makefile.include @@ -320,6 +320,10 @@ ifneq (,$(filter srf04,$(USEMODULE))) USEMODULE_INCLUDES += $(RIOTBASE)/drivers/srf04/include endif +ifneq (,$(filter srf08,$(USEMODULE))) + USEMODULE_INCLUDES += $(RIOTBASE)/drivers/srf08/include +endif + ifneq (,$(filter stmpe811,$(USEMODULE))) USEMODULE_INCLUDES += $(RIOTBASE)/drivers/stmpe811/include endif diff --git a/drivers/include/srf08.h b/drivers/include/srf08.h index 53f432daf1..133beacf23 100644 --- a/drivers/include/srf08.h +++ b/drivers/include/srf08.h @@ -35,9 +35,6 @@ extern "C" { #endif -/** @brief The sensors default I2C address */ -#define SRF08_DEFAULT_ADDR 112 - /** @brief The sensors command register (write) */ #define SRF08_COMMAND_REG 0x0 @@ -65,12 +62,19 @@ extern "C" { /** @brief Maximum gain of the sensor (1025)*/ #define SRF08_MAX_GAIN 0x1F +/** + * @brief Device initialization parameters + */ +typedef struct { + i2c_t i2c; /**< I2C device the sensor is connected to */ + uint8_t addr; /**< I2C bus address of the sensor */ +} srf08_params_t; + /** * @brief Device descriptor for SRF08 sensors */ typedef struct { - i2c_t i2c; /**< I2C device the sensor is connected to */ - uint8_t addr; /**< I2C bus address of the sensor */ + srf08_params_t params; /**< Initialization parameters */ } srf08_t; /** @@ -89,15 +93,14 @@ typedef enum { * @brief Initialize the SRF08 ultrasonic sensor * * @param[in] dev device descriptor of an SRF08 sensor - * @param[in] i2c I2C device the sensor is connected to - * @param[in] addr I2C address of the sensor + * @param[in] params initialization parameters * * @return 0 on successful initialization * @return -3 on max. range error * @return -4 on max. gain error * */ -int srf08_init(srf08_t *dev, i2c_t i2c, uint8_t addr); +int srf08_init(srf08_t *dev, const srf08_params_t *params); /** * @brief Set the maximum range of the SRF08. diff --git a/drivers/srf08/include/srf08_params.h b/drivers/srf08/include/srf08_params.h new file mode 100644 index 0000000000..0fe4c1160f --- /dev/null +++ b/drivers/srf08/include/srf08_params.h @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2020 Inria + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup drivers_srf08 + * + * @{ + * @file + * @brief Default configuration for srf08 devices + * + * @author Alexandre Abadie + */ + +#ifndef SRF08_PARAMS_H +#define SRF08_PARAMS_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "board.h" +#include "srf08.h" + +/** + * @name Default configuration parameters for SRF08 device + * @{ + */ +#ifndef SRF08_PARAM_I2C +#define SRF08_PARAM_I2C I2C_DEV(0) +#endif +#ifndef SRF08_PARAM_ADDR +#define SRF08_PARAM_ADDR (0x70) /* 0xE0 shifted by 1 */ +#endif + +#ifndef SRF08_PARAMS +#define SRF08_PARAMS { .i2c = SRF08_PARAM_I2C, \ + .addr = SRF08_PARAM_ADDR } +#endif +/** @} */ + +/** + * @brief SRF08 configuration + */ +static const srf08_params_t srf08_params[] = { + SRF08_PARAMS +}; + +/** + * @brief Number of SRF08 devices + */ +#define SRF08_NUMOF ARRAY_SIZE(srf08_params) + +#ifdef __cplusplus +} +#endif + +#endif /* SRF08_PARAMS_H */ +/** @} */ diff --git a/drivers/srf08/srf08.c b/drivers/srf08/srf08.c index 3e8081eb31..bc986e7376 100644 --- a/drivers/srf08/srf08.c +++ b/drivers/srf08/srf08.c @@ -24,18 +24,22 @@ #include #include + #include "xtimer.h" -#include "srf08.h" #include "periph/i2c.h" +#include "srf08.h" +#include "srf08_params.h" + #define ENABLE_DEBUG (0) #include "debug.h" +#define SRF08_DEV_I2C (dev->params.i2c) +#define SRF08_DEV_ADDR (dev->params.addr) -int srf08_init(srf08_t *dev, i2c_t i2c, uint8_t addr) +int srf08_init(srf08_t *dev, const srf08_params_t *params) { - dev->i2c = i2c; - dev->addr = addr; + dev->params = *params; /* set the maximum range */ if (srf08_set_max_range(dev, SRF08_MAX_RANGE_6M) < 0) { @@ -56,10 +60,10 @@ int srf08_set_max_range(const srf08_t *dev, uint8_t max_range) int status; /* Acquire exclusive access to the bus. */ - i2c_acquire(dev->i2c); - status = i2c_write_reg(dev->i2c, dev->addr, SRF08_RANGE_REG, max_range, 0); + i2c_acquire(SRF08_DEV_I2C); + status = i2c_write_reg(SRF08_DEV_I2C, SRF08_DEV_ADDR, SRF08_RANGE_REG, max_range, 0); /* Release the bus for other threads. */ - i2c_release(dev->i2c); + i2c_release(SRF08_DEV_I2C); return status; } @@ -70,10 +74,10 @@ int srf08_set_max_gain(const srf08_t *dev, uint8_t gain) int status; /* Acquire exclusive access to the bus. */ - i2c_acquire(dev->i2c); - status = i2c_write_reg(dev->i2c, dev->addr, SRF08_GAIN_REG, gain, 0); + i2c_acquire(SRF08_DEV_I2C); + status = i2c_write_reg(SRF08_DEV_I2C, SRF08_DEV_ADDR, SRF08_GAIN_REG, gain, 0); /* Release the bus for other threads. */ - i2c_release(dev->i2c); + i2c_release(SRF08_DEV_I2C); return status; } @@ -89,12 +93,12 @@ int srf08_get_distances(const srf08_t *dev, uint16_t *range_array, char max_reg_no_read = (num_echos * sizeof(range_bytes)) +1; /* Acquire exclusive access to the bus. */ - i2c_acquire(dev->i2c); + i2c_acquire(SRF08_DEV_I2C); /* set ranging mode */ - status = i2c_write_reg(dev->i2c, dev->addr, SRF08_COMMAND_REG, + status = i2c_write_reg(SRF08_DEV_I2C, SRF08_DEV_ADDR, SRF08_COMMAND_REG, ranging_mode, 0); /* Release the bus for other threads. */ - i2c_release(dev->i2c); + i2c_release(SRF08_DEV_I2C); if (status != 0) { DEBUG("Write the ranging command to the i2c-interface is failed\n"); @@ -113,12 +117,12 @@ int srf08_get_distances(const srf08_t *dev, uint16_t *range_array, register_location += sizeof(range_bytes)) { /* Acquire exclusive access to the bus. */ - i2c_acquire(dev->i2c); + i2c_acquire(SRF08_DEV_I2C); /* read the echo bytes */ - status = i2c_read_regs(dev->i2c, dev->addr, register_location, + status = i2c_read_regs(SRF08_DEV_I2C, SRF08_DEV_ADDR, register_location, range_bytes, sizeof(range_bytes), 0); /* Release the bus for other threads. */ - i2c_release(dev->i2c); + i2c_release(SRF08_DEV_I2C); if (status != 0) { DEBUG("Read the echo bytes from the i2c-interface is failed\n");