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

drivers/lis2dh12: general cleanup

Don't expose raw register values for the user to figure out
This commit is contained in:
Benjamin Valentin 2021-04-23 17:20:32 +02:00 committed by Benjamin Valentin
parent e8264d3ea9
commit 28148b7dc3
4 changed files with 210 additions and 165 deletions

View File

@ -63,10 +63,10 @@ extern "C" {
* @brief Available scale values * @brief Available scale values
*/ */
typedef enum { typedef enum {
LIS2DH12_SCALE_2G = 0x00, /**< +- 2g */ LIS2DH12_SCALE_2G = 0x0, /**< +- 2g */
LIS2DH12_SCALE_4G = 0x10, /**< +- 4g */ LIS2DH12_SCALE_4G = 0x1, /**< +- 4g */
LIS2DH12_SCALE_8G = 0x20, /**< +- 8g */ LIS2DH12_SCALE_8G = 0x2, /**< +- 8g */
LIS2DH12_SCALE_16G = 0x30, /**< +- 16g */ LIS2DH12_SCALE_16G = 0x3, /**< +- 16g */
} lis2dh12_scale_t; } lis2dh12_scale_t;
/** /**
@ -177,7 +177,6 @@ typedef struct {
*/ */
typedef struct { typedef struct {
const lis2dh12_params_t *p; /**< device configuration */ const lis2dh12_params_t *p; /**< device configuration */
uint8_t comp; /**< scale compensation factor */
} lis2dh12_t; } lis2dh12_t;
/** /**
@ -215,10 +214,13 @@ typedef struct {
/** /**
* @brief LIS2DH12 FIFO data struct * @brief LIS2DH12 FIFO data struct
*/ */
typedef struct { typedef union {
int16_t X_AXIS; /**< X raw data in FIFO */ struct {
int16_t Y_AXIS; /**< Y raw data in FIFO */ int16_t x; /**< X data in mili-g */
int16_t Z_AXIS; /**< Z raw data in FIFO */ int16_t y; /**< Y data in mili-g */
int16_t z; /**< Z data in mili-g */
} axis; /**< named axis access */
int16_t data[3]; /**< x, y, z data in mili-g */
} lis2dh12_fifo_data_t; } lis2dh12_fifo_data_t;
/** /**
@ -332,16 +334,6 @@ int lis2dh12_set_fifo(const lis2dh12_t *dev, const lis2dh12_fifo_t *config);
*/ */
int lis2dh12_restart_fifo(const lis2dh12_t *dev); int lis2dh12_restart_fifo(const lis2dh12_t *dev);
/**
* @brief Read the FIFO source register
*
* @param[in] dev device descriptor
* @param[out] data LIS2DH12_FIFO_SRC_REG_t content, allocate one byte
*
* @return LIS2DH12_OK on success
*/
int lis2dh12_read_fifo_src(const lis2dh12_t *dev, LIS2DH12_FIFO_SRC_REG_t *data);
/** /**
* @brief This function will read a given number of data from FIFO * @brief This function will read a given number of data from FIFO
* reads amount of data that is available in FIFO * reads amount of data that is available in FIFO
@ -371,12 +363,12 @@ int lis2dh12_init(lis2dh12_t *dev, const lis2dh12_params_t *params);
* @brief Read acceleration data from the given device * @brief Read acceleration data from the given device
* *
* @param[in] dev device descriptor * @param[in] dev device descriptor
* @param[out] data acceleration data in mili-g, **MUST** hold 3 values * @param[out] data acceleration data in mili-g
* *
* @return LIS2DH12_OK on success * @return LIS2DH12_OK on success
* @return LIS2DH12_NOBUS on bus error * @return LIS2DH12_NOBUS on bus error
*/ */
int lis2dh12_read(const lis2dh12_t *dev, int16_t *data); int lis2dh12_read(const lis2dh12_t *dev, lis2dh12_fifo_data_t *data);
/** /**
* @brief Clear the LIS2DH12 memory, clears all sampled data * @brief Clear the LIS2DH12 memory, clears all sampled data
@ -388,7 +380,7 @@ int lis2dh12_read(const lis2dh12_t *dev, int16_t *data);
int lis2dh12_clear_data(const lis2dh12_t *dev); int lis2dh12_clear_data(const lis2dh12_t *dev);
/** /**
* @brief Change device scale value * @brief Change device measuring range
* *
* @param[in] dev device descriptor * @param[in] dev device descriptor
* @param[in] scale change to given scale value * @param[in] scale change to given scale value
@ -397,6 +389,15 @@ int lis2dh12_clear_data(const lis2dh12_t *dev);
*/ */
int lis2dh12_set_scale(lis2dh12_t *dev, lis2dh12_scale_t scale); int lis2dh12_set_scale(lis2dh12_t *dev, lis2dh12_scale_t scale);
/**
* @brief Get device measuring range
*
* @param[in] dev device descriptor
*
* @return Current device range
*/
lis2dh12_scale_t lis2dh12_get_scale(lis2dh12_t *dev);
/** /**
* @brief Change device sampling rate * @brief Change device sampling rate
* *
@ -408,24 +409,13 @@ int lis2dh12_set_scale(lis2dh12_t *dev, lis2dh12_scale_t scale);
int lis2dh12_set_datarate(const lis2dh12_t *dev, lis2dh12_rate_t rate); int lis2dh12_set_datarate(const lis2dh12_t *dev, lis2dh12_rate_t rate);
/** /**
* @brief Change device power mode * @brief Get device sampling rate in Hz
* *
* @param[in] dev device descriptor * @param[in] dev device descriptor
* @param[in] powermode change to given power mode
* *
* @return LIS2DH12_OK on success * @return current sampling rate in Hz
*/ */
int lis2dh12_set_powermode(const lis2dh12_t *dev, lis2dh12_powermode_t powermode); uint16_t lis2dh12_get_datarate(const lis2dh12_t *dev);
/**
* @brief Configures the high pass filter
*
* @param[in] dev device descriptor
* @param[in] config device high pass configuration
*
* @return LIS2DH12_OK on success
*/
int lis2dh12_set_highpass(const lis2dh12_t *dev, const lis2dh12_highpass_t *config);
/** /**
* @brief Change device resolution * @brief Change device resolution
@ -447,27 +437,30 @@ int lis2dh12_set_resolution(const lis2dh12_t *dev, lis2dh12_resolution_t resolut
lis2dh12_resolution_t lis2dh12_get_resolution(const lis2dh12_t *dev); lis2dh12_resolution_t lis2dh12_get_resolution(const lis2dh12_t *dev);
/** /**
* @brief Set click configuration * @brief Configures the high pass filter
* *
* @param[in] dev device descriptor * @param[in] dev device descriptor
* @param[in] config device click configuration * @param[in] config device high pass configuration
* *
* @return LIS2DH12_OK on success * @return LIS2DH12_OK on success
*/ */
int lis2dh12_set_click(const lis2dh12_t *dev, const lis2dh12_click_t *config); int lis2dh12_set_highpass(const lis2dh12_t *dev, const lis2dh12_highpass_t *config);
/** /**
* @brief Read click source register * @brief Set the reference value to control the high-pass reference.
* In LIS2DH12_HP_MODE_REFERENCE the reference value is used to filter data
* on all axis. Subtracts reference value from acceleration.
* Note: LSB changes according to LIS2DH12_SCALE
* *
* @param[in] dev device descriptor * @param[in] dev device descriptor
* @param[out] data LIS2DH12_CLICK_SRC_t content, allocate one byte * @param[in] reference reference value [8 Bit]
* *
* @return LIS2DH12_OK on success * @return LIS2DH12_OK on success
*/ */
int lis2dh12_read_click_src(const lis2dh12_t *dev, LIS2DH12_CLICK_SRC_t *data); int lis2dh12_set_reference(const lis2dh12_t *dev, uint8_t reference);
/** /**
* @brief Power on the given device and resets power mode and sampling rate * @brief Power on the given device and resets resolution and sampling rate
* to default values in the device descriptor parameters * to default values in the device descriptor parameters
* *
* @param[in] dev device descriptor * @param[in] dev device descriptor

View File

@ -94,8 +94,36 @@ enum {
LIS2DH12_INT_TYPE_I2_IA2 = 0x20, /**< IA2 on INT2 */ LIS2DH12_INT_TYPE_I2_IA2 = 0x20, /**< IA2 on INT2 */
LIS2DH12_INT_TYPE_I2_IA1 = 0x40, /**< IA1 on INT2 */ LIS2DH12_INT_TYPE_I2_IA1 = 0x40, /**< IA1 on INT2 */
LIS2DH12_INT_TYPE_I2_CLICK = 0x80, /**< click interrupt on INT2 */ LIS2DH12_INT_TYPE_I2_CLICK = 0x80, /**< click interrupt on INT2 */
LIS2DH12_INT_TYPE_IA2 = 0x20, /**< Event 2 */
LIS2DH12_INT_TYPE_IA1 = 0x40, /**< Event 1 */
LIS2DH12_INT_TYPE_CLICK = 0x80, /**< click interrupt */
}; };
/**
* @brief Event slots
*/
enum {
LIS2DH12_EVENT_1 = 0x1, /**< first event slot */
LIS2DH12_EVENT_2 = 0x2, /**< second event slot */
LIS2DH12_EVENT_CLICK = 0x3, /**< click event */
};
/**
* @brief Extract interrupt flags for Event Slot 1
*/
#define LIS2DH12_INT_SRC_1(ret) (((uint32_t)(ret) >> 0) & 0x7F)
/**
* @brief Extract interrupt flags for Event Slot 2
*/
#define LIS2DH12_INT_SRC_2(ret) (((uint32_t)(ret) >> 8) & 0x7F)
/**
* @brief Extract interrupt flags for Click Event
*/
#define LIS2DH12_INT_SRC_CLICK(ret) (((uint32_t)(ret) >> 16) & 0x7F)
/** /**
* @brief CLICK_SRC definitions * @brief CLICK_SRC definitions
*/ */
@ -169,6 +197,13 @@ typedef union {
uint8_t reg; /**< Type used for register access */ uint8_t reg; /**< Type used for register access */
} LIS2DH12_CTRL_REG1_t; } LIS2DH12_CTRL_REG1_t;
#define LIS2DH12_CTRL_REG2_HP_IA1 (1 << 0)
#define LIS2DH12_CTRL_REG2_HP_IA2 (1 << 1)
#define LIS2DH12_CTRL_REG2_HPCLICK (1 << 2)
#define LIS2DH12_CTRL_REG2_FDS (1 << 3)
#define LIS2DH12_CLICK_THS_LIR (0x80)
/** /**
* @brief CTRL_REG2 definitions * @brief CTRL_REG2 definitions
*/ */

View File

@ -118,8 +118,7 @@ static uint8_t _read(const lis2dh12_t *dev, uint8_t reg)
return tmp; return tmp;
} }
static void _read_burst(const lis2dh12_t *dev, uint8_t reg, static void _read_burst(const lis2dh12_t *dev, uint8_t reg, void *data, size_t len)
void *data, size_t len)
{ {
i2c_read_regs(BUS, ADDR, (FLAG_AINC | reg), data, len, 0); i2c_read_regs(BUS, ADDR, (FLAG_AINC | reg), data, len, 0);
} }
@ -132,13 +131,17 @@ static void _write(const lis2dh12_t *dev, uint8_t reg, uint8_t data)
#endif /* MODULE_LIS2DH12_SPI */ #endif /* MODULE_LIS2DH12_SPI */
static void _write_or(const lis2dh12_t *dev, uint8_t reg, uint8_t data)
{
data |= _read(dev, reg);
_write(dev, reg, data);
}
int lis2dh12_init(lis2dh12_t *dev, const lis2dh12_params_t *params) int lis2dh12_init(lis2dh12_t *dev, const lis2dh12_params_t *params)
{ {
assert(dev && params); assert(dev && params);
dev->p = params; dev->p = params;
/* calculate shift amount to convert raw acceleration data */
dev->comp = 4 - (dev->p->scale >> 4);
/* initialize the chip select line */ /* initialize the chip select line */
if (_init_bus(dev) != LIS2DH12_OK) { if (_init_bus(dev) != LIS2DH12_OK) {
@ -146,6 +149,15 @@ int lis2dh12_init(lis2dh12_t *dev, const lis2dh12_params_t *params)
return LIS2DH12_NOBUS; return LIS2DH12_NOBUS;
} }
/* set resolution */
lis2dh12_set_resolution(dev, dev->p->resolution);
/* clear stale data */
lis2dh12_clear_data(dev);
/* set data range */
lis2dh12_set_scale(dev, dev->p->scale);
/* acquire the bus and verify that our parameters are valid */ /* acquire the bus and verify that our parameters are valid */
if (_acquire(dev) != BUS_OK) { if (_acquire(dev) != BUS_OK) {
DEBUG("[lis2dh12] error: unable to acquire the bus\n"); DEBUG("[lis2dh12] error: unable to acquire the bus\n");
@ -159,6 +171,13 @@ int lis2dh12_init(lis2dh12_t *dev, const lis2dh12_params_t *params)
return LIS2DH12_NODEV; return LIS2DH12_NODEV;
} }
/* clear events */
_write(dev, REG_CTRL_REG3, 0);
_write(dev, REG_CTRL_REG6, 0);
/* disable fifo */
_write(dev, REG_FIFO_CTRL_REG, 0);
/* enable all axes, set sampling rate and scale */ /* enable all axes, set sampling rate and scale */
LIS2DH12_CTRL_REG1_t reg1 = {0}; LIS2DH12_CTRL_REG1_t reg1 = {0};
@ -167,7 +186,6 @@ int lis2dh12_init(lis2dh12_t *dev, const lis2dh12_params_t *params)
reg1.bit.Yen = 1; reg1.bit.Yen = 1;
reg1.bit.Zen = 1; reg1.bit.Zen = 1;
_write(dev, REG_CTRL_REG4, dev->p->scale);
_write(dev, REG_CTRL_REG1, reg1.reg); _write(dev, REG_CTRL_REG1, reg1.reg);
_release(dev); _release(dev);
@ -176,30 +194,33 @@ int lis2dh12_init(lis2dh12_t *dev, const lis2dh12_params_t *params)
return LIS2DH12_OK; return LIS2DH12_OK;
} }
int lis2dh12_read(const lis2dh12_t *dev, int16_t *data) static void _get_fifo_data(const lis2dh12_t *dev, lis2dh12_fifo_data_t *dst, uint8_t comp)
{
_read_burst(dev, REG_OUT_X_L, dst, sizeof(*dst));
for (unsigned i = 0; i < 3; ++i) {
dst->data[i] >>= comp;
}
}
int lis2dh12_read(const lis2dh12_t *dev, lis2dh12_fifo_data_t *data)
{ {
assert(dev && data); assert(dev && data);
/* allocate 6 byte to save the 6 RAW data registers */
uint8_t raw[6];
/* read sampled data from the device */ /* read sampled data from the device */
_acquire(dev); _acquire(dev);
uint8_t comp = 4 - ((_read(dev, REG_CTRL_REG4) >> 4) & 0x3);
/* first check if valid data is available */ /* first check if valid data is available */
if ((_read(dev, REG_STATUS_REG) & LIS2DH12_STATUS_REG_ZYXDA) == 0) { if ((_read(dev, REG_STATUS_REG) & LIS2DH12_STATUS_REG_ZYXDA) == 0) {
_release(dev); _release(dev);
return LIS2DH12_NODATA; return LIS2DH12_NODATA;
} }
_read_burst(dev, REG_OUT_X_L, raw, 6); _get_fifo_data(dev, data, comp);
_release(dev); _release(dev);
/* calculate the actual g-values for the x, y, and z dimension */
for (int i = 0; i < 3; i++) {
data[i] = (int16_t)((raw[i*2 + 1] << 8) | raw[i*2]) >> dev->comp;
}
return LIS2DH12_OK; return LIS2DH12_OK;
} }
@ -246,7 +267,7 @@ void lis2dh12_cfg_threshold_event(const lis2dh12_t *dev,
int_reg = _read(dev, REG_CTRL_REG6); int_reg = _read(dev, REG_CTRL_REG6);
} }
DEBUG("[%u] threshold: %lu mg\n", event, mg); DEBUG("[%u] threshold: %"PRIu32" mg\n", event, mg);
/* read reference to set it to current data */ /* read reference to set it to current data */
_read(dev, REG_REFERENCE); _read(dev, REG_REFERENCE);
@ -302,7 +323,7 @@ void lis2dh12_cfg_click_event(const lis2dh12_t *dev, uint32_t mg,
uint8_t odr = _read(dev, REG_CTRL_REG1) >> 4; uint8_t odr = _read(dev, REG_CTRL_REG1) >> 4;
uint8_t scale = (_read(dev, REG_CTRL_REG4) >> 4) & 0x3; uint8_t scale = (_read(dev, REG_CTRL_REG4) >> 4) & 0x3;
DEBUG("click threshold: %lu mg\n", mg); DEBUG("click threshold: %"PRIu32" mg\n", mg);
/* read reference to set it to current data */ /* read reference to set it to current data */
_read(dev, REG_REFERENCE); _read(dev, REG_REFERENCE);
@ -392,23 +413,23 @@ static uint32_t _merge_int_flags(const lis2dh12_t *dev, uint8_t events)
/* merge interrupt flags (7 bit per event) into one word */ /* merge interrupt flags (7 bit per event) into one word */
if (events & LIS2DH12_INT_TYPE_IA1) { if (events & LIS2DH12_INT_TYPE_IA1) {
int_src |= _read(dev, REG_INT1_SRC); int_src |= (uint32_t)_read(dev, REG_INT1_SRC);
} }
if (events & LIS2DH12_INT_TYPE_IA2) { if (events & LIS2DH12_INT_TYPE_IA2) {
int_src |= _read(dev, REG_INT2_SRC) << 8; int_src |= (uint32_t)_read(dev, REG_INT2_SRC) << 8;
} }
if (events & LIS2DH12_INT_TYPE_CLICK) { if (events & LIS2DH12_INT_TYPE_CLICK) {
int_src |= _read(dev, REG_CLICK_SRC) << 16; int_src |= (uint32_t)_read(dev, REG_CLICK_SRC) << 16;
} }
DEBUG("int_src: %lx\n", int_src); DEBUG("int_src: %"PRIx32"\n", int_src);
return int_src; return int_src;
} }
#define LIS2DH12_INT_SRC_ANY ((LIS2DH12_INT_SRC_IA << 0) | \ #define LIS2DH12_INT_SRC_ANY (((uint32_t)LIS2DH12_INT_SRC_IA << 0) | \
(LIS2DH12_INT_SRC_IA << 8) | \ ((uint32_t)LIS2DH12_INT_SRC_IA << 8) | \
(LIS2DH12_INT_SRC_IA << 16)) ((uint32_t)LIS2DH12_INT_SRC_IA << 16))
int lis2dh12_wait_event(const lis2dh12_t *dev, uint8_t line, bool stale_events) int lis2dh12_wait_event(const lis2dh12_t *dev, uint8_t line, bool stale_events)
{ {
@ -458,15 +479,21 @@ int lis2dh12_wait_event(const lis2dh12_t *dev, uint8_t line, bool stale_events)
} }
#endif /* MODULE_LIS2DH12_INT */ #endif /* MODULE_LIS2DH12_INT */
int lis2dh12_set_fifo(const lis2dh12_t *dev, const lis2dh12_fifo_t *config) { int lis2dh12_set_fifo(const lis2dh12_t *dev, const lis2dh12_fifo_t *config)
{
assert(dev && config); assert(dev && config);
LIS2DH12_CTRL_REG5_t reg5 = {0}; LIS2DH12_CTRL_REG5_t reg5 = {0};
LIS2DH12_FIFO_CTRL_REG_t fifo_reg = {0}; LIS2DH12_FIFO_CTRL_REG_t fifo_reg = {0};
reg5.bit.LIR_INT1 = 1;
reg5.bit.LIR_INT2 = 1;
if (config->FIFO_mode != LIS2DH12_FIFO_MODE_BYPASS) { if (config->FIFO_mode != LIS2DH12_FIFO_MODE_BYPASS) {
reg5.bit.FIFO_EN = 1; reg5.bit.FIFO_EN = 1;
} else {
reg5.bit.FIFO_EN = 0;
} }
fifo_reg.bit.TR = config->FIFO_set_INT2; fifo_reg.bit.TR = config->FIFO_set_INT2;
fifo_reg.bit.FM = config->FIFO_mode; fifo_reg.bit.FM = config->FIFO_mode;
@ -480,13 +507,14 @@ int lis2dh12_set_fifo(const lis2dh12_t *dev, const lis2dh12_fifo_t *config) {
return LIS2DH12_OK; return LIS2DH12_OK;
} }
int lis2dh12_restart_fifo(const lis2dh12_t *dev) { int lis2dh12_restart_fifo(const lis2dh12_t *dev)
{
assert(dev); assert(dev);
_acquire(dev); _acquire(dev);
uint8_t reg5 = _read(dev, REG_CTRL_REG5); uint8_t reg5 = _read(dev, REG_CTRL_REG5);
LIS2DH12_FIFO_CTRL_REG_t fifo_reg = {0}; LIS2DH12_FIFO_CTRL_REG_t fifo_reg;
fifo_reg.reg = _read(dev, REG_FIFO_CTRL_REG); fifo_reg.reg = _read(dev, REG_FIFO_CTRL_REG);
uint8_t fifo_mode_old = fifo_reg.bit.FM; uint8_t fifo_mode_old = fifo_reg.bit.FM;
@ -504,45 +532,28 @@ int lis2dh12_restart_fifo(const lis2dh12_t *dev) {
return LIS2DH12_OK; return LIS2DH12_OK;
} }
int lis2dh12_read_fifo_src(const lis2dh12_t *dev, LIS2DH12_FIFO_SRC_REG_t *data) {
assert(dev && data);
_acquire(dev);
data->reg = _read(dev, REG_FIFO_SRC_REG);
_release(dev);
return LIS2DH12_OK;
}
uint8_t lis2dh12_read_fifo_data(const lis2dh12_t *dev, lis2dh12_fifo_data_t *fifo_data, uint8_t lis2dh12_read_fifo_data(const lis2dh12_t *dev, lis2dh12_fifo_data_t *fifo_data,
uint8_t number) { uint8_t number)
{
assert(dev && fifo_data); assert(dev && fifo_data);
/* check max FIFO length */ /* check max FIFO length */
assert(number <= 32); assert(number <= 32);
_acquire(dev); _acquire(dev);
/* check if number is available */ /* check if number is available */
LIS2DH12_FIFO_SRC_REG_t src_reg = {0}; LIS2DH12_FIFO_SRC_REG_t src_reg;
src_reg.reg = _read(dev, REG_FIFO_SRC_REG); src_reg.reg = _read(dev, REG_FIFO_SRC_REG);
if (src_reg.bit.FSS <= number) { if (src_reg.bit.FSS < number) {
number = src_reg.bit.FSS; number = src_reg.bit.FSS;
} }
if (src_reg.bit.EMPTY) { uint8_t comp = 4 - ((_read(dev, REG_CTRL_REG4) >> 4) & 0x3);
return 0;
}
/* calculate X, Y and Z values */ /* calculate X, Y and Z values */
for (uint8_t i = 0; i < number; i++){ for (uint8_t i = 0; i < number; i++) {
fifo_data[i].X_AXIS = (int16_t)(_read(dev, REG_OUT_X_L) | (_read(dev, REG_OUT_X_H) << 8)) _get_fifo_data(dev, &fifo_data[i], comp);
>> dev->comp;
fifo_data[i].Y_AXIS = (int16_t)(_read(dev, REG_OUT_Y_L) | (_read(dev, REG_OUT_Y_H) << 8))
>> dev->comp;
fifo_data[i].Z_AXIS = (int16_t)(_read(dev, REG_OUT_Z_L) | (_read(dev, REG_OUT_Z_H) << 8))
>> dev->comp;
} }
_release(dev); _release(dev);
@ -550,12 +561,16 @@ uint8_t lis2dh12_read_fifo_data(const lis2dh12_t *dev, lis2dh12_fifo_data_t *fif
return number; return number;
} }
int lis2dh12_clear_data(const lis2dh12_t *dev) { int lis2dh12_clear_data(const lis2dh12_t *dev)
{
assert(dev); assert(dev);
LIS2DH12_CTRL_REG5_t ctrl_reg5 = {0}; LIS2DH12_CTRL_REG5_t ctrl_reg5 = {
ctrl_reg5.bit.BOOT = 1; .bit.BOOT = 1,
.bit.LIR_INT1 = 1,
.bit.LIR_INT2 = 1,
};
_acquire(dev); _acquire(dev);
_write(dev, REG_CTRL_REG5, ctrl_reg5.reg); _write(dev, REG_CTRL_REG5, ctrl_reg5.reg);
@ -564,7 +579,8 @@ int lis2dh12_clear_data(const lis2dh12_t *dev) {
return LIS2DH12_OK; return LIS2DH12_OK;
} }
int lis2dh12_set_reference(const lis2dh12_t *dev, uint8_t reference) { int lis2dh12_set_reference(const lis2dh12_t *dev, uint8_t reference)
{
assert(dev); assert(dev);
@ -575,18 +591,8 @@ int lis2dh12_set_reference(const lis2dh12_t *dev, uint8_t reference) {
return LIS2DH12_OK; return LIS2DH12_OK;
} }
int lis2dh12_read_reference(const lis2dh12_t *dev, uint8_t *data) { int lis2dh12_set_highpass(const lis2dh12_t *dev, const lis2dh12_highpass_t *config)
{
assert(dev);
_acquire(dev);
*data = _read(dev, REG_REFERENCE);
_release(dev);
return LIS2DH12_OK;
}
int lis2dh12_set_highpass(const lis2dh12_t *dev, const lis2dh12_highpass_t *config) {
assert(dev && config); assert(dev && config);
@ -606,46 +612,6 @@ int lis2dh12_set_highpass(const lis2dh12_t *dev, const lis2dh12_highpass_t *conf
return LIS2DH12_OK; return LIS2DH12_OK;
} }
int lis2dh12_set_click(const lis2dh12_t *dev, const lis2dh12_click_t *config) {
assert(dev);
LIS2DH12_CLICK_CFG_t click_CFG = {0};
if (config->enable_DOUBLE) {
click_CFG.bit.XD = config->enable_X_CLICK;
click_CFG.bit.YD = config->enable_Y_CLICK;
click_CFG.bit.ZD = config->enable_Z_CLICK;
}
else {
click_CFG.bit.XS = config->enable_X_CLICK;
click_CFG.bit.YS = config->enable_Y_CLICK;
click_CFG.bit.ZS = config->enable_Z_CLICK;
}
LIS2DH12_CLICK_THS_t click_thold = {0};
click_thold.bit.LIR_CLICK = config->noINT_latency;
click_thold.bit.THS = config->CLICK_thold;
_acquire(dev);
_write(dev, REG_CLICK_CFG, click_CFG.reg);
_write(dev, REG_CLICK_THS, click_thold.reg);
_write(dev, REG_TIME_LIMIT, config->TIME_limit);
_write(dev, REG_TIME_LATENCY, config->TIME_latency);
_write(dev, REG_TIME_WINDOW, config->TIME_window);
_release(dev);
return LIS2DH12_OK;
}
int lis2dh12_read_click_src(const lis2dh12_t *dev, LIS2DH12_CLICK_SRC_t *data) {
assert(dev && data);
_acquire(dev);
data->reg = _read(dev, REG_CLICK_SRC);
_release(dev);
return LIS2DH12_OK;
}
int lis2dh12_set_resolution(const lis2dh12_t *dev, lis2dh12_resolution_t resolution) int lis2dh12_set_resolution(const lis2dh12_t *dev, lis2dh12_resolution_t resolution)
{ {
assert(dev); assert(dev);
@ -704,44 +670,95 @@ lis2dh12_resolution_t lis2dh12_get_resolution(const lis2dh12_t *dev)
} }
return LIS2DH12_POWER_NORMAL; return LIS2DH12_POWER_NORMAL;
} }
int lis2dh12_set_datarate(const lis2dh12_t *dev, lis2dh12_rate_t rate) {
int lis2dh12_set_datarate(const lis2dh12_t *dev, lis2dh12_rate_t rate)
{
assert(dev); assert(dev);
assert(rate <= 0x9); assert(rate <= 0x9);
LIS2DH12_CTRL_REG1_t reg1 = {0}; LIS2DH12_CTRL_REG1_t reg1;
_acquire(dev); _acquire(dev);
reg1.reg = _read(dev, REG_CTRL_REG1); reg1.reg = _read(dev, REG_CTRL_REG1);
reg1.bit.ODR = rate; reg1.bit.ODR = rate;
_write(dev, REG_CTRL_REG1, reg1.reg); _write(dev, REG_CTRL_REG1, reg1.reg);
_release(dev); _release(dev);
return LIS2DH12_OK; return LIS2DH12_OK;
} }
int lis2dh12_set_scale(lis2dh12_t *dev, lis2dh12_scale_t scale) { uint16_t lis2dh12_get_datarate(const lis2dh12_t *dev)
{
const uint16_t rates_hz[] = {
0,
1,
10,
25,
50,
100,
200,
400,
};
assert(dev); assert(dev);
assert((scale>>4) <= 0x3);
LIS2DH12_CTRL_REG4_t reg4 = {0}; LIS2DH12_CTRL_REG1_t reg1;
_acquire(dev);
reg1.reg = _read(dev, REG_CTRL_REG1);
_release(dev);
if (reg1.bit.ODR < ARRAY_SIZE(rates_hz)) {
return rates_hz[reg1.bit.ODR];
}
if (reg1.bit.LPen) {
if (reg1.bit.ODR == 8) {
return 1620;
}
if (reg1.bit.ODR == 9) {
return 5376;
}
}
if (reg1.bit.ODR == 9) {
return 1344;
}
return 0;
}
int lis2dh12_set_scale(lis2dh12_t *dev, lis2dh12_scale_t scale)
{
assert(dev);
assert(scale <= LIS2DH12_SCALE_16G);
LIS2DH12_CTRL_REG4_t reg4;
_acquire(dev); _acquire(dev);
reg4.reg = _read(dev, REG_CTRL_REG4); reg4.reg = _read(dev, REG_CTRL_REG4);
reg4.bit.FS = scale;
reg4.bit.FS = scale >> 4;
_write(dev, REG_CTRL_REG4, reg4.reg); _write(dev, REG_CTRL_REG4, reg4.reg);
_release(dev); _release(dev);
dev->comp = 4 - (scale >> 4);
return LIS2DH12_OK; return LIS2DH12_OK;
} }
lis2dh12_scale_t lis2dh12_get_scale(lis2dh12_t *dev)
{
assert(dev);
LIS2DH12_CTRL_REG4_t reg4;
_acquire(dev);
reg4.reg = _read(dev, REG_CTRL_REG4);
_release(dev);
return reg4.bit.FS;
}
int lis2dh12_poweron(const lis2dh12_t *dev) int lis2dh12_poweron(const lis2dh12_t *dev)
{ {
assert(dev); assert(dev);

View File

@ -23,7 +23,7 @@
static int read_accelerometer(const void *dev, phydat_t *res) static int read_accelerometer(const void *dev, phydat_t *res)
{ {
if (lis2dh12_read((const lis2dh12_t *)dev, res->val) != LIS2DH12_OK) { if (lis2dh12_read(dev, (lis2dh12_fifo_data_t*)res->val) != LIS2DH12_OK) {
return 0; return 0;
} }
res->unit = UNIT_G; res->unit = UNIT_G;