mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
drivers/lsm303dlhc: rework driver API and params definition
This commit is contained in:
parent
d1a19db350
commit
d81cf561b0
@ -99,19 +99,6 @@ typedef struct {
|
||||
int16_t z_axis; /**< surprise, holds the z axis value */
|
||||
} lsm303dlhc_3d_data_t;
|
||||
|
||||
/**
|
||||
* @brief Device descriptor for LSM303DLHC sensors
|
||||
*/
|
||||
typedef struct {
|
||||
i2c_t i2c; /**< I2C device */
|
||||
uint8_t acc_address; /**< accelerometer's I2C address */
|
||||
uint8_t mag_address; /**< magnetometer's I2C address */
|
||||
gpio_t acc_pin; /**< accelerometer's data ready pin */
|
||||
gpio_t mag_pin; /**< magnetometer's data ready pin */
|
||||
lsm303dlhc_acc_scale_t acc_scale; /**< accelerometer scale factor */
|
||||
lsm303dlhc_mag_gain_t mag_gain; /**< magnetometer gain */
|
||||
} lsm303dlhc_t;
|
||||
|
||||
/**
|
||||
* @brief Data structure holding all the information needed for initialization
|
||||
*/
|
||||
@ -127,30 +114,23 @@ typedef struct {
|
||||
lsm303dlhc_mag_gain_t mag_gain; /**< magnetometer gain */
|
||||
} lsm303dlhc_params_t;
|
||||
|
||||
/**
|
||||
* @brief Device descriptor for LSM303DLHC sensors
|
||||
*/
|
||||
typedef struct {
|
||||
lsm303dlhc_params_t params; /**< device initialization parameters */
|
||||
} lsm303dlhc_t;
|
||||
|
||||
/**
|
||||
* @brief Initialize a new LSM303DLHC device
|
||||
*
|
||||
* @param[in] dev device descriptor of an LSM303DLHC device
|
||||
* @param[in] i2c I2C device the sensor is connected to
|
||||
* @param[in] acc_pin GPIO pin connected to accelerometer
|
||||
* @param[in] mag_pin GPIO pin connected to magnetometer
|
||||
* @param[in] acc_address I2C address of the accelerometer
|
||||
* @param[in] acc_sample_rate accelerometer sample rate
|
||||
* @param[in] acc_scale accelerometer scale (from +- 2g to +-16g)
|
||||
* @param[in] mag_address I2C address of the magnetometer
|
||||
* @param[in] mag_sample_rate magnetometer sample rate
|
||||
* @param[in] mag_gain magnetometer gain
|
||||
* @param[in] dev device descriptor of an LSM303DLHC device
|
||||
* @param[in] params initialization parameters
|
||||
*
|
||||
* @return 0 on success
|
||||
* @return -1 on error
|
||||
*/
|
||||
int lsm303dlhc_init(lsm303dlhc_t *dev, i2c_t i2c, gpio_t acc_pin, gpio_t mag_pin,
|
||||
uint8_t acc_address,
|
||||
lsm303dlhc_acc_sample_rate_t acc_sample_rate,
|
||||
lsm303dlhc_acc_scale_t acc_scale,
|
||||
uint8_t mag_address,
|
||||
lsm303dlhc_mag_sample_rate_t mag_sample_rate,
|
||||
lsm303dlhc_mag_gain_t mag_gain);
|
||||
int lsm303dlhc_init(lsm303dlhc_t *dev, const lsm303dlhc_params_t *params);
|
||||
|
||||
/**
|
||||
* @brief Read a accelerometer value from the sensor.
|
||||
|
@ -59,15 +59,20 @@ extern "C" {
|
||||
#define LSM303DLHC_PARAM_MAG_GAIN (LSM303DLHC_MAG_GAIN_450_400_GAUSS)
|
||||
#endif
|
||||
|
||||
#define LSM303DLHC_PARAMS_DEFAULT { .i2c = LSM303DLHC_PARAM_I2C, \
|
||||
.acc_addr = LSM303DLHC_PARAM_ACC_ADDR, \
|
||||
.acc_pin = LSM303DLHC_PARAM_ACC_PIN, \
|
||||
.acc_rate = LSM303DLHC_PARAM_ACC_RATE, \
|
||||
#ifndef LSM303DLHC_PARAMS
|
||||
#define LSM303DLHC_PARAMS { .i2c = LSM303DLHC_PARAM_I2C, \
|
||||
.acc_addr = LSM303DLHC_PARAM_ACC_ADDR, \
|
||||
.acc_pin = LSM303DLHC_PARAM_ACC_PIN, \
|
||||
.acc_rate = LSM303DLHC_PARAM_ACC_RATE, \
|
||||
.acc_scale = LSM303DLHC_PARAM_ACC_SCALE, \
|
||||
.mag_addr = LSM303DLHC_PARAM_MAG_ADDR, \
|
||||
.mag_pin = LSM303DLHC_PARAM_MAG_PIN, \
|
||||
.mag_rate = LSM303DLHC_PARAM_MAG_RATE, \
|
||||
.mag_addr = LSM303DLHC_PARAM_MAG_ADDR, \
|
||||
.mag_pin = LSM303DLHC_PARAM_MAG_PIN, \
|
||||
.mag_rate = LSM303DLHC_PARAM_MAG_RATE, \
|
||||
.mag_gain = LSM303DLHC_PARAM_MAG_GAIN }
|
||||
#endif
|
||||
#ifndef LSM303DLHC_SAUL_INFO
|
||||
#define LSM303DLHC_SAUL_INFO { .name = "lsm303dlhc" }
|
||||
#endif
|
||||
/**@}*/
|
||||
|
||||
/**
|
||||
@ -75,11 +80,7 @@ extern "C" {
|
||||
*/
|
||||
static const lsm303dlhc_params_t lsm303dlhc_params[] =
|
||||
{
|
||||
#ifdef LSM303DLHC_PARAMS_CUSTOM
|
||||
LSM303DLHC_PARAMS_CUSTOM,
|
||||
#else
|
||||
LSM303DLHC_PARAMS_DEFAULT,
|
||||
#endif
|
||||
LSM303DLHC_PARAMS
|
||||
};
|
||||
|
||||
/**
|
||||
@ -87,7 +88,7 @@ static const lsm303dlhc_params_t lsm303dlhc_params[] =
|
||||
*/
|
||||
static const saul_reg_info_t lsm303dlhc_saul_info[] =
|
||||
{
|
||||
{ .name = "lsm303dlhc" }
|
||||
LSM303DLHC_SAUL_INFO
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -25,34 +25,32 @@
|
||||
#define ENABLE_DEBUG (0)
|
||||
#include "debug.h"
|
||||
|
||||
int lsm303dlhc_init(lsm303dlhc_t *dev, i2c_t i2c, gpio_t acc_pin, gpio_t mag_pin,
|
||||
uint8_t acc_address,
|
||||
lsm303dlhc_acc_sample_rate_t acc_sample_rate,
|
||||
lsm303dlhc_acc_scale_t acc_scale,
|
||||
uint8_t mag_address,
|
||||
lsm303dlhc_mag_sample_rate_t mag_sample_rate,
|
||||
lsm303dlhc_mag_gain_t mag_gain)
|
||||
#define DEV_I2C (dev->params.i2c)
|
||||
#define DEV_ACC_ADDR (dev->params.acc_addr)
|
||||
#define DEV_ACC_PIN (dev->params.acc_pin)
|
||||
#define DEV_ACC_RATE (dev->params.acc_rate)
|
||||
#define DEV_ACC_SCALE (dev->params.acc_scale)
|
||||
#define DEV_MAG_ADDR (dev->params.mag_addr)
|
||||
#define DEV_MAG_PIN (dev->params.mag_pin)
|
||||
#define DEV_MAG_RATE (dev->params.mag_rate)
|
||||
#define DEV_MAG_GAIN (dev->params.mag_gain)
|
||||
|
||||
int lsm303dlhc_init(lsm303dlhc_t *dev, const lsm303dlhc_params_t *params)
|
||||
{
|
||||
dev->params = *params;
|
||||
|
||||
int res;
|
||||
uint8_t tmp;
|
||||
|
||||
dev->i2c = i2c;
|
||||
dev->acc_address = acc_address;
|
||||
dev->mag_address = mag_address;
|
||||
dev->acc_pin = acc_pin;
|
||||
dev->mag_pin = mag_pin;
|
||||
dev->acc_scale = acc_scale;
|
||||
dev->mag_gain = mag_gain;
|
||||
|
||||
/* Acquire exclusive access to the bus. */
|
||||
i2c_acquire(dev->i2c);
|
||||
i2c_init_master(i2c, I2C_SPEED_NORMAL);
|
||||
i2c_acquire(DEV_I2C);
|
||||
i2c_init_master(DEV_I2C, I2C_SPEED_NORMAL);
|
||||
|
||||
DEBUG("lsm303dlhc reboot...");
|
||||
res = i2c_write_reg(dev->i2c, dev->acc_address,
|
||||
res = i2c_write_reg(DEV_I2C, DEV_ACC_ADDR,
|
||||
LSM303DLHC_REG_CTRL5_A, LSM303DLHC_REG_CTRL5_A_BOOT);
|
||||
/* Release the bus for other threads. */
|
||||
i2c_release(dev->i2c);
|
||||
i2c_release(DEV_I2C);
|
||||
DEBUG("[OK]\n");
|
||||
|
||||
/* configure accelerometer */
|
||||
@ -60,34 +58,34 @@ int lsm303dlhc_init(lsm303dlhc_t *dev, i2c_t i2c, gpio_t acc_pin, gpio_t mag_pin
|
||||
tmp = (LSM303DLHC_CTRL1_A_XEN
|
||||
| LSM303DLHC_CTRL1_A_YEN
|
||||
| LSM303DLHC_CTRL1_A_ZEN
|
||||
| acc_sample_rate);
|
||||
i2c_acquire(dev->i2c);
|
||||
res += i2c_write_reg(dev->i2c, dev->acc_address,
|
||||
| DEV_ACC_RATE);
|
||||
i2c_acquire(DEV_I2C);
|
||||
res += i2c_write_reg(DEV_I2C, DEV_ACC_ADDR,
|
||||
LSM303DLHC_REG_CTRL1_A, tmp);
|
||||
/* update on read, MSB @ low address, scale and high-resolution */
|
||||
tmp = (acc_scale | LSM303DLHC_CTRL4_A_HR);
|
||||
res += i2c_write_reg(dev->i2c, dev->acc_address,
|
||||
tmp = (DEV_ACC_SCALE | LSM303DLHC_CTRL4_A_HR);
|
||||
res += i2c_write_reg(DEV_I2C, DEV_ACC_ADDR,
|
||||
LSM303DLHC_REG_CTRL4_A, tmp);
|
||||
/* no interrupt generation */
|
||||
res += i2c_write_reg(dev->i2c, dev->acc_address,
|
||||
res += i2c_write_reg(DEV_I2C, DEV_ACC_ADDR,
|
||||
LSM303DLHC_REG_CTRL3_A, LSM303DLHC_CTRL3_A_I1_NONE);
|
||||
/* configure acc data ready pin */
|
||||
gpio_init(acc_pin, GPIO_IN);
|
||||
gpio_init(DEV_ACC_PIN, GPIO_IN);
|
||||
|
||||
/* configure magnetometer and temperature */
|
||||
/* enable temperature output and set sample rate */
|
||||
tmp = LSM303DLHC_TEMP_EN | mag_sample_rate;
|
||||
res += i2c_write_reg(dev->i2c, dev->mag_address,
|
||||
tmp = LSM303DLHC_TEMP_EN | DEV_MAG_RATE;
|
||||
res += i2c_write_reg(DEV_I2C, DEV_MAG_ADDR,
|
||||
LSM303DLHC_REG_CRA_M, tmp);
|
||||
/* configure z-axis gain */
|
||||
res += i2c_write_reg(dev->i2c, dev->mag_address,
|
||||
LSM303DLHC_REG_CRB_M, mag_gain);
|
||||
res += i2c_write_reg(DEV_I2C, DEV_MAG_ADDR,
|
||||
LSM303DLHC_REG_CRB_M, DEV_MAG_GAIN);
|
||||
/* set continuous mode */
|
||||
res += i2c_write_reg(dev->i2c, dev->mag_address,
|
||||
res += i2c_write_reg(DEV_I2C, DEV_MAG_ADDR,
|
||||
LSM303DLHC_REG_MR_M, LSM303DLHC_MAG_MODE_CONTINUOUS);
|
||||
i2c_release(dev->i2c);
|
||||
i2c_release(DEV_I2C);
|
||||
/* configure mag data ready pin */
|
||||
gpio_init(mag_pin, GPIO_IN);
|
||||
gpio_init(DEV_MAG_PIN, GPIO_IN);
|
||||
|
||||
return (res < 7) ? -1 : 0;
|
||||
}
|
||||
@ -97,30 +95,30 @@ int lsm303dlhc_read_acc(const lsm303dlhc_t *dev, lsm303dlhc_3d_data_t *data)
|
||||
int res;
|
||||
uint8_t tmp;
|
||||
|
||||
i2c_acquire(dev->i2c);
|
||||
i2c_read_reg(dev->i2c, dev->acc_address, LSM303DLHC_REG_STATUS_A, &tmp);
|
||||
i2c_acquire(DEV_I2C);
|
||||
i2c_read_reg(DEV_I2C, DEV_ACC_ADDR, LSM303DLHC_REG_STATUS_A, &tmp);
|
||||
DEBUG("lsm303dlhc status: %x\n", tmp);
|
||||
DEBUG("lsm303dlhc: wait for acc values ... ");
|
||||
|
||||
res = i2c_read_reg(dev->i2c, dev->acc_address,
|
||||
res = i2c_read_reg(DEV_I2C, DEV_ACC_ADDR,
|
||||
LSM303DLHC_REG_OUT_X_L_A, &tmp);
|
||||
data->x_axis = tmp;
|
||||
res += i2c_read_reg(dev->i2c, dev->acc_address,
|
||||
res += i2c_read_reg(DEV_I2C, DEV_ACC_ADDR,
|
||||
LSM303DLHC_REG_OUT_X_H_A, &tmp);
|
||||
data->x_axis |= tmp<<8;
|
||||
res += i2c_read_reg(dev->i2c, dev->acc_address,
|
||||
res += i2c_read_reg(DEV_I2C, DEV_ACC_ADDR,
|
||||
LSM303DLHC_REG_OUT_Y_L_A, &tmp);
|
||||
data->y_axis = tmp;
|
||||
res += i2c_read_reg(dev->i2c, dev->acc_address,
|
||||
res += i2c_read_reg(DEV_I2C, DEV_ACC_ADDR,
|
||||
LSM303DLHC_REG_OUT_Y_H_A, &tmp);
|
||||
data->y_axis |= tmp<<8;
|
||||
res += i2c_read_reg(dev->i2c, dev->acc_address,
|
||||
res += i2c_read_reg(DEV_I2C, DEV_ACC_ADDR,
|
||||
LSM303DLHC_REG_OUT_Z_L_A, &tmp);
|
||||
data->z_axis = tmp;
|
||||
res += i2c_read_reg(dev->i2c, dev->acc_address,
|
||||
res += i2c_read_reg(DEV_I2C, DEV_ACC_ADDR,
|
||||
LSM303DLHC_REG_OUT_Z_H_A, &tmp);
|
||||
data->z_axis |= tmp<<8;
|
||||
i2c_release(dev->i2c);
|
||||
i2c_release(DEV_I2C);
|
||||
DEBUG("read ... ");
|
||||
|
||||
data->x_axis = data->x_axis>>4;
|
||||
@ -141,14 +139,14 @@ int lsm303dlhc_read_mag(const lsm303dlhc_t *dev, lsm303dlhc_3d_data_t *data)
|
||||
int res;
|
||||
|
||||
DEBUG("lsm303dlhc: wait for mag values... ");
|
||||
while (gpio_read(dev->mag_pin) == 0){}
|
||||
while (gpio_read(DEV_MAG_PIN) == 0){}
|
||||
|
||||
DEBUG("read ... ");
|
||||
|
||||
i2c_acquire(dev->i2c);
|
||||
res = i2c_read_regs(dev->i2c, dev->mag_address,
|
||||
i2c_acquire(DEV_I2C);
|
||||
res = i2c_read_regs(DEV_I2C, DEV_MAG_ADDR,
|
||||
LSM303DLHC_REG_OUT_X_H_M, data, 6);
|
||||
i2c_release(dev->i2c);
|
||||
i2c_release(DEV_I2C);
|
||||
|
||||
if (res < 6) {
|
||||
DEBUG("[!!failed!!]\n");
|
||||
@ -173,9 +171,9 @@ int lsm303dlhc_read_temp(const lsm303dlhc_t *dev, int16_t *value)
|
||||
{
|
||||
int res;
|
||||
|
||||
i2c_acquire(dev->i2c);
|
||||
res = i2c_read_regs(dev->i2c, dev->mag_address, LSM303DLHC_REG_TEMP_OUT_H, value, 2);
|
||||
i2c_release(dev->i2c);
|
||||
i2c_acquire(DEV_I2C);
|
||||
res = i2c_read_regs(DEV_I2C, DEV_MAG_ADDR, LSM303DLHC_REG_TEMP_OUT_H, value, 2);
|
||||
i2c_release(DEV_I2C);
|
||||
|
||||
if (res < 2) {
|
||||
return -1;
|
||||
@ -192,14 +190,14 @@ int lsm303dlhc_disable(const lsm303dlhc_t *dev)
|
||||
{
|
||||
int res;
|
||||
|
||||
i2c_acquire(dev->i2c);
|
||||
res = i2c_write_reg(dev->i2c, dev->acc_address,
|
||||
i2c_acquire(DEV_I2C);
|
||||
res = i2c_write_reg(DEV_I2C, DEV_ACC_ADDR,
|
||||
LSM303DLHC_REG_CTRL1_A, LSM303DLHC_CTRL1_A_POWEROFF);
|
||||
res += i2c_write_reg(dev->i2c, dev->mag_address,
|
||||
res += i2c_write_reg(DEV_I2C, DEV_MAG_ADDR,
|
||||
LSM303DLHC_REG_MR_M, LSM303DLHC_MAG_MODE_SLEEP);
|
||||
res += i2c_write_reg(dev->i2c, dev->acc_address,
|
||||
res += i2c_write_reg(DEV_I2C, DEV_ACC_ADDR,
|
||||
LSM303DLHC_REG_CRA_M, LSM303DLHC_TEMP_DIS);
|
||||
i2c_release(dev->i2c);
|
||||
i2c_release(DEV_I2C);
|
||||
|
||||
return (res < 3) ? -1 : 0;
|
||||
}
|
||||
@ -211,25 +209,25 @@ int lsm303dlhc_enable(const lsm303dlhc_t *dev)
|
||||
| LSM303DLHC_CTRL1_A_YEN
|
||||
| LSM303DLHC_CTRL1_A_ZEN
|
||||
| LSM303DLHC_CTRL1_A_N1344HZ_L5376HZ);
|
||||
i2c_acquire(dev->i2c);
|
||||
res = i2c_write_reg(dev->i2c, dev->acc_address, LSM303DLHC_REG_CTRL1_A, tmp);
|
||||
i2c_acquire(DEV_I2C);
|
||||
res = i2c_write_reg(DEV_I2C, DEV_ACC_ADDR, LSM303DLHC_REG_CTRL1_A, tmp);
|
||||
|
||||
tmp = (LSM303DLHC_CTRL4_A_BDU| LSM303DLHC_CTRL4_A_SCALE_2G | LSM303DLHC_CTRL4_A_HR);
|
||||
res += i2c_write_reg(dev->i2c, dev->acc_address, LSM303DLHC_REG_CTRL4_A, tmp);
|
||||
res += i2c_write_reg(dev->i2c, dev->acc_address, LSM303DLHC_REG_CTRL3_A, LSM303DLHC_CTRL3_A_I1_DRDY1);
|
||||
gpio_init(dev->acc_pin, GPIO_IN);
|
||||
res += i2c_write_reg(DEV_I2C, DEV_ACC_ADDR, LSM303DLHC_REG_CTRL4_A, tmp);
|
||||
res += i2c_write_reg(DEV_I2C, DEV_ACC_ADDR, LSM303DLHC_REG_CTRL3_A, LSM303DLHC_CTRL3_A_I1_DRDY1);
|
||||
gpio_init(DEV_ACC_PIN, GPIO_IN);
|
||||
|
||||
tmp = LSM303DLHC_TEMP_EN | LSM303DLHC_TEMP_SAMPLE_75HZ;
|
||||
res += i2c_write_reg(dev->i2c, dev->mag_address, LSM303DLHC_REG_CRA_M, tmp);
|
||||
res += i2c_write_reg(DEV_I2C, DEV_MAG_ADDR, LSM303DLHC_REG_CRA_M, tmp);
|
||||
|
||||
res += i2c_write_reg(dev->i2c, dev->mag_address,
|
||||
res += i2c_write_reg(DEV_I2C, DEV_MAG_ADDR,
|
||||
LSM303DLHC_REG_CRB_M, LSM303DLHC_GAIN_5);
|
||||
|
||||
res += i2c_write_reg(dev->i2c, dev->mag_address,
|
||||
res += i2c_write_reg(DEV_I2C, DEV_MAG_ADDR,
|
||||
LSM303DLHC_REG_MR_M, LSM303DLHC_MAG_MODE_CONTINUOUS);
|
||||
i2c_release(dev->i2c);
|
||||
i2c_release(DEV_I2C);
|
||||
|
||||
gpio_init(dev->mag_pin, GPIO_IN);
|
||||
gpio_init(DEV_MAG_PIN, GPIO_IN);
|
||||
|
||||
return (res < 6) ? -1 : 0;
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ static int read_acc(const void *dev, phydat_t *res)
|
||||
lsm303dlhc_read_acc(d, (lsm303dlhc_3d_data_t *)res);
|
||||
|
||||
/* normalize result */
|
||||
int fac = (1 << (d->acc_scale >> 4));
|
||||
int fac = (1 << (d->params.acc_scale >> 4));
|
||||
for (int i = 0; i < 3; i++) {
|
||||
res->val[i] *= fac;
|
||||
}
|
||||
@ -47,7 +47,7 @@ static int read_mag(const void *dev, phydat_t *res)
|
||||
|
||||
/* normalize results */
|
||||
int gain;
|
||||
switch (d->mag_gain) {
|
||||
switch (d->params.mag_gain) {
|
||||
case LSM303DLHC_MAG_GAIN_1100_980_GAUSS: gain = 1100; break;
|
||||
case LSM303DLHC_MAG_GAIN_855_760_GAUSS: gain = 855; break;
|
||||
case LSM303DLHC_MAG_GAIN_670_600_GAUSS: gain = 670; break;
|
||||
|
Loading…
Reference in New Issue
Block a user