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

drivers/srf08: refactor driver configuration

This commit is contained in:
Alexandre Abadie 2020-02-21 12:05:15 +01:00
parent a27b58fbba
commit 2b713f13cb
No known key found for this signature in database
GPG Key ID: 1C919A403CAE1405
4 changed files with 98 additions and 24 deletions

View File

@ -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

View File

@ -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.

View File

@ -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 <alexandre.abadie@inria.fr>
*/
#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 */
/** @} */

View File

@ -24,18 +24,22 @@
#include <stdint.h>
#include <stdio.h>
#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");