1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2026-01-01 01:41:18 +01:00

drivers/l3g4200d: rework driver API and params definition

This commit is contained in:
Alexandre Abadie 2018-02-28 18:17:05 +01:00 committed by dylad
parent ec25c1f7b0
commit 2ace1a2dfe
3 changed files with 57 additions and 67 deletions

View File

@ -76,45 +76,35 @@ typedef enum {
} l3g4200d_mode_t;
/**
* @brief Device descriptor for L3G4200D sensors
* @brief Device initialization parameters
*/
typedef struct {
i2c_t i2c; /**< I2C device the sensor is connected to */
uint8_t addr; /**< the sensors slave address on the I2C bus */
gpio_t int1; /**< INT1 pin */
gpio_t int2; /**< INT2 (DRDY) pin */
int32_t scale; /**< scaling factor to normalize results */
} l3g4200d_t;
l3g4200d_mode_t mode; /**< sampling frequency and bandwidth mode */
l3g4200d_scale_t scale; /**< scaling factor to normalize results */
} l3g4200d_params_t;
/**
* @brief Data structure holding the device parameters needed for initialization
* @brief Device descriptor for L3G4200D sensors
*/
typedef struct {
i2c_t i2c; /**< I2C bus the device is connected to */
uint8_t addr; /**< the address on that bus */
gpio_t int1_pin; /**< GPIO pin connected to the INT1 line */
gpio_t int2_pin; /**< GPIO pin connected to the INT2 line */
l3g4200d_mode_t mode; /**< sampling mode to use */
l3g4200d_scale_t scale; /**< scaling to use */
} l3g4200d_params_t;
l3g4200d_params_t params; /**< device initialization parameters */
int scale; /**< internal scaling factor to normalize results */
} l3g4200d_t;
/**
* @brief Initialize a gyro
*
* @param[out] dev device descriptor of sensor to initialize
* @param[in] i2c I2C bus the gyro is connected to
* @param[in] address gyro's I2C slave address
* @param[in] int1_pin INT pin the gyro is connected to
* @param[in] int2_pin DRDY pin the gyro is connected to
* @param[in] mode bandwidth and sampling rate settings
* @param[in] scale scaling of results
* @param[in] params initialization parameters
*
* @return 0 on success
* @return -1 on error
*/
int l3g4200d_init(l3g4200d_t *dev, i2c_t i2c, uint8_t address,
gpio_t int1_pin, gpio_t int2_pin,
l3g4200d_mode_t mode, l3g4200d_scale_t scale);
int l3g4200d_init(l3g4200d_t *dev, const l3g4200d_params_t *params);
/**
* @brief Read angular speed value in degree per second from gyro

View File

@ -50,12 +50,17 @@ extern "C" {
#define L3G4200D_PARAM_SCALE (L3G4200D_SCALE_500DPS)
#endif
#define L3G4200D_PARAMS_DEFAULT { .i2c = L3G4200D_PARAM_I2C, \
.addr = L3G4200D_PARAM_ADDR, \
.int1_pin = L3G4200D_PARAM_INT1, \
.int2_pin = L3G4200D_PARAM_INT2, \
.mode = L3G4200D_PARAM_MODE, \
.scale = L3G4200D_PARAM_SCALE }
#ifndef L3G4200D_PARAMS
#define L3G4200D_PARAMS { .i2c = L3G4200D_PARAM_I2C, \
.addr = L3G4200D_PARAM_ADDR, \
.int1 = L3G4200D_PARAM_INT1, \
.int2 = L3G4200D_PARAM_INT2, \
.mode = L3G4200D_PARAM_MODE, \
.scale = L3G4200D_PARAM_SCALE }
#endif
#ifndef L3G4200D_SAUL_INFO
#define L3G4200D_SAUL_INFO { .name = "l3g4200d" }
#endif
/**@}*/
/**
@ -63,11 +68,7 @@ extern "C" {
*/
static const l3g4200d_params_t l3g4200d_params[] =
{
#ifdef L3G4200D_PARAMS_CUSTOM
L3G4200D_PARAMS_CUSTOM,
#else
L3G4200D_PARAMS_DEFAULT,
#endif
L3G4200D_PARAMS
};
/**
@ -75,7 +76,7 @@ static const l3g4200d_params_t l3g4200d_params[] =
*/
static const saul_reg_info_t l3g4200d_saul_info[] =
{
{ .name = "l3g4200d" }
L3G4200D_SAUL_INFO
};
#ifdef __cplusplus

View File

@ -33,20 +33,19 @@
#define MAX_VAL 0x7fff
int l3g4200d_init(l3g4200d_t *dev, i2c_t i2c, uint8_t address,
gpio_t int1_pin, gpio_t int2_pin,
l3g4200d_mode_t mode, l3g4200d_scale_t scale)
#define DEV_I2C (dev->params.i2c)
#define DEV_ADDR (dev->params.addr)
#define DEV_MODE (dev->params.mode)
#define DEV_SCALE (dev->params.scale)
int l3g4200d_init(l3g4200d_t *dev, const l3g4200d_params_t *params)
{
dev->params = *params;
uint8_t tmp;
/* write device descriptor */
dev->i2c = i2c;
dev->addr = address;
dev->int1 = int1_pin;
dev->int2 = int2_pin;
/* set scale */
switch (scale) {
switch (DEV_SCALE) {
case L3G4200D_SCALE_250DPS:
dev->scale = 250;
break;
@ -62,25 +61,25 @@ int l3g4200d_init(l3g4200d_t *dev, i2c_t i2c, uint8_t address,
}
/* acquire exclusive access to the bus. */
i2c_acquire(dev->i2c);
i2c_acquire(DEV_I2C);
/* initialize the I2C bus */
if (i2c_init_master(i2c, I2C_SPEED) < 0) {
if (i2c_init_master(DEV_I2C, I2C_SPEED) < 0) {
/* Release the bus for other threads. */
i2c_release(dev->i2c);
i2c_release(DEV_I2C);
return -1;
}
/* configure CTRL_REG1 */
tmp = ((mode & 0xf) << L3G4200D_CTRL1_MODE_POS) | L3G4200D_CTRL1_ALLON;
if (i2c_write_reg(dev->i2c, dev->addr, L3G4200D_REG_CTRL1, tmp) != 1) {
i2c_release(dev->i2c);
tmp = ((DEV_MODE & 0xf) << L3G4200D_CTRL1_MODE_POS) | L3G4200D_CTRL1_ALLON;
if (i2c_write_reg(DEV_I2C, DEV_ADDR, L3G4200D_REG_CTRL1, tmp) != 1) {
i2c_release(DEV_I2C);
return -1;
}
tmp = ((scale & 0x3) << L3G4200D_CTRL4_FS_POS) | L3G4200D_CTRL4_BDU;
if (i2c_write_reg(dev->i2c, dev->addr, L3G4200D_REG_CTRL4, tmp) != 1) {
i2c_release(dev->i2c);
tmp = ((DEV_SCALE & 0x3) << L3G4200D_CTRL4_FS_POS) | L3G4200D_CTRL4_BDU;
if (i2c_write_reg(DEV_I2C, DEV_ADDR, L3G4200D_REG_CTRL4, tmp) != 1) {
i2c_release(DEV_I2C);
return -1;
}
i2c_release(dev->i2c);
i2c_release(DEV_I2C);
return 0;
}
@ -89,10 +88,10 @@ int l3g4200d_read(const l3g4200d_t *dev, l3g4200d_data_t *data)
uint8_t tmp[6];
int16_t res;
i2c_acquire(dev->i2c);
i2c_acquire(DEV_I2C);
/* get acceleration in x direction */
i2c_read_regs(dev->i2c, dev->addr, L3G4200D_REG_OUT_X_L | L3G4200D_AUTOINC, tmp, 6);
i2c_release(dev->i2c);
i2c_read_regs(DEV_I2C, DEV_ADDR, L3G4200D_REG_OUT_X_L | L3G4200D_AUTOINC, tmp, 6);
i2c_release(DEV_I2C);
/* parse and normalize data into result vector */
res = (tmp[1] << 8) | tmp[0];
@ -109,18 +108,18 @@ int l3g4200d_enable(const l3g4200d_t *dev)
uint8_t tmp;
int res;
i2c_acquire(dev->i2c);
res = i2c_read_reg(dev->i2c, dev->addr, L3G4200D_REG_CTRL1, &tmp);
i2c_acquire(DEV_I2C);
res = i2c_read_reg(DEV_I2C, DEV_ADDR, L3G4200D_REG_CTRL1, &tmp);
if (res < 1) {
i2c_release(dev->i2c);
i2c_release(DEV_I2C);
return res;
}
tmp |= L3G4200D_CTRL1_PD;
if (i2c_write_reg(dev->i2c, dev->addr, L3G4200D_REG_CTRL1, tmp) != 1) {
i2c_release(dev->i2c);
if (i2c_write_reg(DEV_I2C, DEV_ADDR, L3G4200D_REG_CTRL1, tmp) != 1) {
i2c_release(DEV_I2C);
return -1;
}
i2c_release(dev->i2c);
i2c_release(DEV_I2C);
return 0;
}
@ -129,17 +128,17 @@ int l3g4200d_disable(const l3g4200d_t *dev)
uint8_t tmp;
int res;
i2c_acquire(dev->i2c);
res = i2c_read_reg(dev->i2c, dev->addr, L3G4200D_REG_CTRL1, &tmp);
i2c_acquire(DEV_I2C);
res = i2c_read_reg(DEV_I2C, DEV_ADDR, L3G4200D_REG_CTRL1, &tmp);
if (res < 1) {
i2c_release(dev->i2c);
i2c_release(DEV_I2C);
return res;
}
tmp &= ~L3G4200D_CTRL1_PD;
if (i2c_write_reg(dev->i2c, dev->addr, L3G4200D_REG_CTRL1, tmp) != 1) {
i2c_release(dev->i2c);
if (i2c_write_reg(DEV_I2C, DEV_ADDR, L3G4200D_REG_CTRL1, tmp) != 1) {
i2c_release(DEV_I2C);
return -1;
}
i2c_release(dev->i2c);
i2c_release(DEV_I2C);
return 0;
}