1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-25 14:33:52 +01:00

drivers/lis3dh: rework driver API and params definition

This commit is contained in:
Alexandre Abadie 2018-02-28 18:18:20 +01:00
parent 291922be34
commit 1a0cb4264c
3 changed files with 35 additions and 30 deletions

View File

@ -736,10 +736,8 @@ typedef struct {
* @brief Device descriptor for LIS3DH sensors
*/
typedef struct {
spi_t spi; /**< SPI device the sensor is connected to */
spi_clk_t clk; /**< clock speed of the SPI bus */
gpio_t cs; /**< Chip select pin */
int16_t scale; /**< Current scale setting of the sensor */
lis3dh_params_t params; /**< Device initialization parameters */
uint16_t scale; /**< Internal sensor scale */
} lis3dh_t;
/**

View File

@ -37,6 +37,9 @@ extern "C" {
#ifndef LIS3DH_PARAM_CS
#define LIS3DH_PARAM_CS (GPIO_PIN(0, 0))
#endif
#ifndef LIS3DH_PARAM_CLK
#define LIS3DH_PARAM_CLK (SPI_CLK_5MHZ)
#endif
#ifndef LIS3DH_PARAM_INT1
#define LIS3DH_PARAM_INT1 (GPIO_PIN(0, 1))
#endif
@ -50,12 +53,18 @@ extern "C" {
#define LIS3DH_PARAM_ODR (LIS3DH_ODR_100Hz)
#endif
#define LIS3DH_PARAMS_DEFAULT { .spi = LIS3DH_PARAM_SPI, \
.cs = LIS3DH_PARAM_CS, \
.int1 = LIS3DH_PARAM_INT1, \
.int2 = LIS3DH_PARAM_INT2, \
#ifndef LIS3DH_PARAMS
#define LIS3DH_PARAMS { .spi = LIS3DH_PARAM_SPI, \
.cs = LIS3DH_PARAM_CS, \
.clk = LIS3DH_PARAM_CLK, \
.int1 = LIS3DH_PARAM_INT1, \
.int2 = LIS3DH_PARAM_INT2, \
.scale = LIS3DH_PARAM_SCALE, \
.odr = LIS3DH_PARAM_ODR }
#endif
#ifndef LIS3DH_SAUL_INFO
#define LIS3DH_SAUL_INFO { .name = "lis3dh" }
#endif
/**@}*/
/**
@ -63,11 +72,7 @@ extern "C" {
*/
static const lis3dh_params_t lis3dh_params[] =
{
#ifdef LIS3DH_PARAMS_CUSTOM
LIS3DH_PARAMS_CUSTOM,
#else
LIS3DH_PARAMS_DEFAULT,
#endif
LIS3DH_PARAMS
};
/**
@ -75,7 +80,7 @@ static const lis3dh_params_t lis3dh_params[] =
*/
static const saul_reg_info_t lis3dh_saul_info[] =
{
{ .name = "lis3dh" }
LIS3DH_SAUL_INFO
};
#ifdef __cplusplus

View File

@ -29,6 +29,11 @@
#define SPI_MODE SPI_MODE_3
#define DEV_SPI (dev->params.spi)
#define DEV_CS (dev->params.cs)
#define DEV_CLK (dev->params.clk)
#define DEV_SCALE (dev->params.scale)
static inline int lis3dh_write_bits(const lis3dh_t *dev, const uint8_t reg,
const uint8_t mask, const uint8_t values);
static int lis3dh_write_reg(const lis3dh_t *dev, const uint8_t reg,
@ -38,15 +43,12 @@ static int lis3dh_read_regs(const lis3dh_t *dev, const uint8_t reg,
int lis3dh_init(lis3dh_t *dev, const lis3dh_params_t *params)
{
dev->params = *params;
uint8_t test;
dev->spi = params->spi;
dev->clk = params->clk;
dev->cs = params->cs;
dev->scale = params->scale;
/* initialize the chip select line */
if (spi_init_cs(dev->spi, dev->cs) != SPI_OK) {
if (spi_init_cs(DEV_SPI, DEV_CS) != SPI_OK) {
DEBUG("[lis3dh] error while initializing CS pin\n");
return -1;
}
@ -76,7 +78,7 @@ int lis3dh_init(lis3dh_t *dev, const lis3dh_params_t *params)
lis3dh_write_reg(dev, LIS3DH_REG_CTRL_REG6, 0);
/* Configure scale */
lis3dh_set_scale(dev, dev->scale);
lis3dh_set_scale(dev, DEV_SCALE);
return 0;
}
@ -89,12 +91,12 @@ int lis3dh_read_xyz(const lis3dh_t *dev, lis3dh_data_t *acc_data)
LIS3DH_SPI_MULTI_MASK);
/* Acquire exclusive access to the bus. */
spi_acquire(dev->spi, dev->cs, SPI_MODE, dev->clk);
spi_acquire(DEV_SPI, DEV_CS, SPI_MODE, DEV_CLK);
/* Perform the transaction */
spi_transfer_regs(dev->spi, dev->cs, addr,
spi_transfer_regs(DEV_SPI, DEV_CS, addr,
NULL, acc_data, sizeof(lis3dh_data_t));
/* Release the bus for other threads. */
spi_release(dev->spi);
spi_release(DEV_SPI);
/* Scale to milli-G */
for (i = 0; i < 3; ++i) {
@ -236,11 +238,11 @@ static int lis3dh_read_regs(const lis3dh_t *dev, const uint8_t reg,
LIS3DH_SPI_MULTI_MASK;
/* Acquire exclusive access to the bus. */
spi_acquire(dev->spi, dev->cs, SPI_MODE, dev->clk);
spi_acquire(DEV_SPI, DEV_CS, SPI_MODE, DEV_CLK);
/* Perform the transaction */
spi_transfer_regs(dev->spi, dev->cs, addr, NULL, buf, (size_t)len);
spi_transfer_regs(DEV_SPI, DEV_CS, addr, NULL, buf, (size_t)len);
/* Release the bus for other threads. */
spi_release(dev->spi);
spi_release(DEV_SPI);
return 0;
}
@ -262,11 +264,11 @@ static int lis3dh_write_reg(const lis3dh_t *dev, const uint8_t reg,
LIS3DH_SPI_SINGLE_MASK);
/* Acquire exclusive access to the bus. */
spi_acquire(dev->spi, dev->cs, SPI_MODE, dev->clk);
spi_acquire(DEV_SPI, DEV_CS, SPI_MODE, DEV_CLK);
/* Perform the transaction */
spi_transfer_reg(dev->spi, dev->cs, addr, value);
spi_transfer_reg(DEV_SPI, DEV_CS, addr, value);
/* Release the bus for other threads. */
spi_release(dev->spi);
spi_release(DEV_SPI);
return 0;
}