2018-05-17 15:32:05 +02:00
|
|
|
/* ST Microelectronics LIS2DW12 3-axis accelerometer driver
|
|
|
|
*
|
|
|
|
* Copyright (c) 2019 STMicroelectronics
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Datasheet:
|
|
|
|
* https://www.st.com/resource/en/datasheet/lis2dw12.pdf
|
|
|
|
*/
|
|
|
|
|
2020-03-26 16:47:47 -05:00
|
|
|
#define DT_DRV_COMPAT st_lis2dw12
|
|
|
|
|
2018-05-17 15:32:05 +02:00
|
|
|
#include <init.h>
|
2019-06-26 10:33:39 -04:00
|
|
|
#include <sys/__assert.h>
|
2019-06-26 10:33:41 -04:00
|
|
|
#include <sys/byteorder.h>
|
2018-05-17 15:32:05 +02:00
|
|
|
#include <logging/log.h>
|
2019-06-25 15:54:00 -04:00
|
|
|
#include <drivers/sensor.h>
|
2018-05-17 15:32:05 +02:00
|
|
|
|
2020-05-06 11:23:07 -07:00
|
|
|
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
2019-06-25 15:54:01 -04:00
|
|
|
#include <drivers/spi.h>
|
2020-05-06 11:23:07 -07:00
|
|
|
#elif DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
|
2019-06-25 15:53:54 -04:00
|
|
|
#include <drivers/i2c.h>
|
2018-05-17 15:32:05 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "lis2dw12.h"
|
|
|
|
|
2019-10-11 12:40:57 +02:00
|
|
|
LOG_MODULE_REGISTER(LIS2DW12, CONFIG_SENSOR_LOG_LEVEL);
|
2018-05-17 15:32:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* lis2dw12_set_range - set full scale range for acc
|
|
|
|
* @dev: Pointer to instance of struct device (I2C or SPI)
|
|
|
|
* @range: Full scale range (2, 4, 8 and 16 G)
|
|
|
|
*/
|
2021-05-31 17:14:23 +02:00
|
|
|
static int lis2dw12_set_range(const struct device *dev, uint8_t fs)
|
2018-05-17 15:32:05 +02:00
|
|
|
{
|
|
|
|
int err;
|
2020-05-28 21:23:02 +02:00
|
|
|
struct lis2dw12_data *lis2dw12 = dev->data;
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct lis2dw12_device_config *cfg = dev->config;
|
2020-05-27 11:26:57 -05:00
|
|
|
uint8_t shift_gain = 0U;
|
2018-05-17 15:32:05 +02:00
|
|
|
|
2019-04-29 16:13:49 +02:00
|
|
|
err = lis2dw12_full_scale_set(lis2dw12->ctx, fs);
|
2018-05-17 15:32:05 +02:00
|
|
|
|
2019-04-29 16:13:49 +02:00
|
|
|
if (cfg->pm == LIS2DW12_CONT_LOW_PWR_12bit) {
|
2018-05-17 15:32:05 +02:00
|
|
|
shift_gain = LIS2DW12_SHFT_GAIN_NOLP1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!err) {
|
|
|
|
/* save internally gain for optimization */
|
|
|
|
lis2dw12->gain =
|
2021-05-31 17:14:23 +02:00
|
|
|
LIS2DW12_FS_TO_GAIN(fs, shift_gain);
|
2018-05-17 15:32:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* lis2dw12_set_odr - set new sampling frequency
|
|
|
|
* @dev: Pointer to instance of struct device (I2C or SPI)
|
|
|
|
* @odr: Output data rate
|
|
|
|
*/
|
2020-04-30 20:33:38 +02:00
|
|
|
static int lis2dw12_set_odr(const struct device *dev, uint16_t odr)
|
2018-05-17 15:32:05 +02:00
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
struct lis2dw12_data *lis2dw12 = dev->data;
|
2020-05-27 11:26:57 -05:00
|
|
|
uint8_t val;
|
2018-05-17 15:32:05 +02:00
|
|
|
|
|
|
|
/* check if power off */
|
2019-03-26 19:57:45 -06:00
|
|
|
if (odr == 0U) {
|
2019-04-29 16:13:49 +02:00
|
|
|
return lis2dw12_data_rate_set(lis2dw12->ctx,
|
|
|
|
LIS2DW12_XL_ODR_OFF);
|
2018-05-17 15:32:05 +02:00
|
|
|
}
|
|
|
|
|
2019-04-29 16:13:49 +02:00
|
|
|
val = LIS2DW12_ODR_TO_REG(odr);
|
|
|
|
if (val > LIS2DW12_XL_ODR_1k6Hz) {
|
2018-05-17 15:32:05 +02:00
|
|
|
LOG_ERR("ODR too high");
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2019-04-29 16:13:49 +02:00
|
|
|
return lis2dw12_data_rate_set(lis2dw12->ctx, val);
|
2018-05-17 15:32:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void lis2dw12_convert(struct sensor_value *val, int raw_val,
|
|
|
|
float gain)
|
|
|
|
{
|
2020-05-27 11:26:57 -05:00
|
|
|
int64_t dval;
|
2018-05-17 15:32:05 +02:00
|
|
|
|
|
|
|
/* Gain is in ug/LSB */
|
|
|
|
/* Convert to m/s^2 */
|
2020-05-27 11:26:57 -05:00
|
|
|
dval = ((int64_t)raw_val * gain * SENSOR_G) / 1000000LL;
|
2018-05-17 15:32:05 +02:00
|
|
|
val->val1 = dval / 1000000LL;
|
|
|
|
val->val2 = dval % 1000000LL;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static inline void lis2dw12_channel_get_acc(const struct device *dev,
|
2018-05-17 15:32:05 +02:00
|
|
|
enum sensor_channel chan,
|
|
|
|
struct sensor_value *val)
|
|
|
|
{
|
|
|
|
int i;
|
2020-05-27 11:26:57 -05:00
|
|
|
uint8_t ofs_start, ofs_stop;
|
2020-05-28 21:23:02 +02:00
|
|
|
struct lis2dw12_data *lis2dw12 = dev->data;
|
2018-05-17 15:32:05 +02:00
|
|
|
struct sensor_value *pval = val;
|
|
|
|
|
|
|
|
switch (chan) {
|
|
|
|
case SENSOR_CHAN_ACCEL_X:
|
2019-03-26 19:57:45 -06:00
|
|
|
ofs_start = ofs_stop = 0U;
|
2018-05-17 15:32:05 +02:00
|
|
|
break;
|
|
|
|
case SENSOR_CHAN_ACCEL_Y:
|
2019-03-26 19:57:45 -06:00
|
|
|
ofs_start = ofs_stop = 1U;
|
2018-05-17 15:32:05 +02:00
|
|
|
break;
|
|
|
|
case SENSOR_CHAN_ACCEL_Z:
|
2019-03-26 19:57:45 -06:00
|
|
|
ofs_start = ofs_stop = 2U;
|
2018-05-17 15:32:05 +02:00
|
|
|
break;
|
|
|
|
default:
|
2019-03-26 19:57:45 -06:00
|
|
|
ofs_start = 0U; ofs_stop = 2U;
|
2018-05-17 15:32:05 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = ofs_start; i <= ofs_stop ; i++) {
|
|
|
|
lis2dw12_convert(pval++, lis2dw12->acc[i], lis2dw12->gain);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int lis2dw12_channel_get(const struct device *dev,
|
2018-05-17 15:32:05 +02:00
|
|
|
enum sensor_channel chan,
|
|
|
|
struct sensor_value *val)
|
|
|
|
{
|
|
|
|
switch (chan) {
|
|
|
|
case SENSOR_CHAN_ACCEL_X:
|
|
|
|
case SENSOR_CHAN_ACCEL_Y:
|
|
|
|
case SENSOR_CHAN_ACCEL_Z:
|
|
|
|
case SENSOR_CHAN_ACCEL_XYZ:
|
|
|
|
lis2dw12_channel_get_acc(dev, chan, val);
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
LOG_DBG("Channel not supported");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int lis2dw12_config(const struct device *dev, enum sensor_channel chan,
|
2018-05-17 15:32:05 +02:00
|
|
|
enum sensor_attribute attr,
|
|
|
|
const struct sensor_value *val)
|
|
|
|
{
|
|
|
|
switch (attr) {
|
|
|
|
case SENSOR_ATTR_FULL_SCALE:
|
2021-05-31 17:14:23 +02:00
|
|
|
return lis2dw12_set_range(dev,
|
|
|
|
LIS2DW12_FS_TO_REG(sensor_ms2_to_g(val)));
|
2018-05-17 15:32:05 +02:00
|
|
|
case SENSOR_ATTR_SAMPLING_FREQUENCY:
|
|
|
|
return lis2dw12_set_odr(dev, val->val1);
|
|
|
|
default:
|
|
|
|
LOG_DBG("Acc attribute not supported");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int lis2dw12_attr_set(const struct device *dev,
|
|
|
|
enum sensor_channel chan,
|
2018-05-17 15:32:05 +02:00
|
|
|
enum sensor_attribute attr,
|
|
|
|
const struct sensor_value *val)
|
|
|
|
{
|
|
|
|
switch (chan) {
|
|
|
|
case SENSOR_CHAN_ACCEL_X:
|
|
|
|
case SENSOR_CHAN_ACCEL_Y:
|
|
|
|
case SENSOR_CHAN_ACCEL_Z:
|
|
|
|
case SENSOR_CHAN_ACCEL_XYZ:
|
|
|
|
return lis2dw12_config(dev, chan, attr, val);
|
|
|
|
default:
|
|
|
|
LOG_DBG("Attr not supported on %d channel", chan);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int lis2dw12_sample_fetch(const struct device *dev,
|
|
|
|
enum sensor_channel chan)
|
2018-05-17 15:32:05 +02:00
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
struct lis2dw12_data *lis2dw12 = dev->data;
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct lis2dw12_device_config *cfg = dev->config;
|
2020-05-27 11:26:57 -05:00
|
|
|
uint8_t shift;
|
2020-10-05 18:06:27 +02:00
|
|
|
int16_t buf[3];
|
2018-05-17 15:32:05 +02:00
|
|
|
|
|
|
|
/* fetch raw data sample */
|
2020-10-05 18:06:27 +02:00
|
|
|
if (lis2dw12_acceleration_raw_get(lis2dw12->ctx, buf) < 0) {
|
2018-05-17 15:32:05 +02:00
|
|
|
LOG_DBG("Failed to fetch raw data sample");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* adjust to resolution */
|
2019-04-29 16:13:49 +02:00
|
|
|
if (cfg->pm == LIS2DW12_CONT_LOW_PWR_12bit) {
|
2018-05-17 15:32:05 +02:00
|
|
|
shift = LIS2DW12_SHIFT_PM1;
|
|
|
|
} else {
|
|
|
|
shift = LIS2DW12_SHIFT_PMOTHER;
|
|
|
|
}
|
|
|
|
|
2020-10-05 18:06:27 +02:00
|
|
|
lis2dw12->acc[0] = sys_le16_to_cpu(buf[0]) >> shift;
|
|
|
|
lis2dw12->acc[1] = sys_le16_to_cpu(buf[1]) >> shift;
|
|
|
|
lis2dw12->acc[2] = sys_le16_to_cpu(buf[2]) >> shift;
|
2018-05-17 15:32:05 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct sensor_driver_api lis2dw12_driver_api = {
|
|
|
|
.attr_set = lis2dw12_attr_set,
|
|
|
|
#if CONFIG_LIS2DW12_TRIGGER
|
|
|
|
.trigger_set = lis2dw12_trigger_set,
|
|
|
|
#endif /* CONFIG_LIS2DW12_TRIGGER */
|
|
|
|
.sample_fetch = lis2dw12_sample_fetch,
|
|
|
|
.channel_get = lis2dw12_channel_get,
|
|
|
|
};
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int lis2dw12_init_interface(const struct device *dev)
|
2018-05-17 15:32:05 +02:00
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
struct lis2dw12_data *lis2dw12 = dev->data;
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct lis2dw12_device_config *cfg = dev->config;
|
2018-05-17 15:32:05 +02:00
|
|
|
|
|
|
|
lis2dw12->bus = device_get_binding(cfg->bus_name);
|
|
|
|
if (!lis2dw12->bus) {
|
|
|
|
LOG_DBG("master bus not found: %s", cfg->bus_name);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-05-06 11:23:07 -07:00
|
|
|
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
2018-05-17 15:32:05 +02:00
|
|
|
lis2dw12_spi_init(dev);
|
2020-05-06 11:23:07 -07:00
|
|
|
#elif DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
|
2018-05-17 15:32:05 +02:00
|
|
|
lis2dw12_i2c_init(dev);
|
|
|
|
#else
|
|
|
|
#error "BUS MACRO NOT DEFINED IN DTS"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lis2dw12_set_power_mode(struct lis2dw12_data *lis2dw12,
|
2019-04-29 16:13:49 +02:00
|
|
|
lis2dw12_mode_t pm)
|
2018-05-17 15:32:05 +02:00
|
|
|
{
|
2020-05-27 11:26:57 -05:00
|
|
|
uint8_t regval = LIS2DW12_CONT_LOW_PWR_12bit;
|
2018-05-17 15:32:05 +02:00
|
|
|
|
|
|
|
switch (pm) {
|
2019-04-29 16:13:49 +02:00
|
|
|
case LIS2DW12_CONT_LOW_PWR_2:
|
|
|
|
case LIS2DW12_CONT_LOW_PWR_3:
|
|
|
|
case LIS2DW12_CONT_LOW_PWR_4:
|
|
|
|
case LIS2DW12_HIGH_PERFORMANCE:
|
|
|
|
regval = pm;
|
2018-05-17 15:32:05 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG_DBG("Apply default Power Mode");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-04-29 16:13:49 +02:00
|
|
|
return lis2dw12_write_reg(lis2dw12->ctx, LIS2DW12_CTRL1, ®val, 1);
|
2018-05-17 15:32:05 +02:00
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int lis2dw12_init(const struct device *dev)
|
2018-05-17 15:32:05 +02:00
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
struct lis2dw12_data *lis2dw12 = dev->data;
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct lis2dw12_device_config *cfg = dev->config;
|
2020-05-27 11:26:57 -05:00
|
|
|
uint8_t wai;
|
2018-05-17 15:32:05 +02:00
|
|
|
|
|
|
|
if (lis2dw12_init_interface(dev)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check chip ID */
|
2019-04-29 16:13:49 +02:00
|
|
|
if (lis2dw12_device_id_get(lis2dw12->ctx, &wai) < 0) {
|
2018-05-17 15:32:05 +02:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2019-04-29 16:13:49 +02:00
|
|
|
if (wai != LIS2DW12_ID) {
|
2018-05-17 15:32:05 +02:00
|
|
|
LOG_ERR("Invalid chip ID");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reset device */
|
2019-04-29 16:13:49 +02:00
|
|
|
if (lis2dw12_reset_set(lis2dw12->ctx, PROPERTY_ENABLE) < 0) {
|
2018-05-17 15:32:05 +02:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
k_busy_wait(100);
|
|
|
|
|
2019-04-29 16:13:49 +02:00
|
|
|
if (lis2dw12_block_data_update_set(lis2dw12->ctx,
|
|
|
|
PROPERTY_ENABLE) < 0) {
|
2018-05-17 15:32:05 +02:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set power mode */
|
2021-05-31 16:42:40 +02:00
|
|
|
LOG_DBG("power-mode is %d", cfg->pm);
|
|
|
|
if (lis2dw12_set_power_mode(lis2dw12, cfg->pm)) {
|
2018-05-17 15:32:05 +02:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2021-05-31 17:20:41 +02:00
|
|
|
/* set default odr to 12.5Hz acc */
|
|
|
|
if (lis2dw12_set_odr(dev, 12) < 0) {
|
|
|
|
LOG_ERR("odr init error (12.5 Hz)");
|
2018-05-17 15:32:05 +02:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2021-05-31 17:14:23 +02:00
|
|
|
LOG_DBG("range is %d", cfg->range);
|
|
|
|
if (lis2dw12_set_range(dev, LIS2DW12_FS_TO_REG(cfg->range)) < 0) {
|
|
|
|
LOG_ERR("range init error %d", cfg->range);
|
2018-05-17 15:32:05 +02:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_LIS2DW12_TRIGGER
|
|
|
|
if (lis2dw12_init_interrupt(dev) < 0) {
|
|
|
|
LOG_ERR("Failed to initialize interrupts");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_LIS2DW12_TRIGGER */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct lis2dw12_device_config lis2dw12_cfg = {
|
2020-03-26 16:47:47 -05:00
|
|
|
.bus_name = DT_INST_BUS_LABEL(0),
|
2021-05-31 16:42:40 +02:00
|
|
|
.pm = DT_INST_PROP(0, power_mode),
|
2021-05-31 17:14:23 +02:00
|
|
|
.range = DT_INST_PROP(0, range),
|
2018-05-17 15:32:05 +02:00
|
|
|
#ifdef CONFIG_LIS2DW12_TRIGGER
|
2021-05-27 15:03:19 +02:00
|
|
|
.gpio_int = GPIO_DT_SPEC_INST_GET_OR(0, irq_gpios, {0}),
|
2021-05-27 13:23:09 +02:00
|
|
|
.int_pin = DT_INST_PROP(0, int_pin),
|
2018-05-17 15:32:05 +02:00
|
|
|
|
2021-05-28 10:46:20 +02:00
|
|
|
#ifdef CONFIG_LIS2DW12_TAP
|
|
|
|
.tap_mode = DT_INST_PROP(0, tap_mode),
|
|
|
|
.tap_threshold = DT_INST_PROP(0, tap_threshold),
|
|
|
|
.tap_shock = DT_INST_PROP(0, tap_shock),
|
|
|
|
.tap_latency = DT_INST_PROP(0, tap_latency),
|
|
|
|
.tap_quiet = DT_INST_PROP(0, tap_quiet),
|
|
|
|
#endif /* CONFIG_LIS2DW12_TAP */
|
2018-05-17 15:32:05 +02:00
|
|
|
#endif /* CONFIG_LIS2DW12_TRIGGER */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct lis2dw12_data lis2dw12_data;
|
|
|
|
|
2021-04-28 11:51:35 +02:00
|
|
|
DEVICE_DT_INST_DEFINE(0, lis2dw12_init, NULL,
|
2018-05-17 15:32:05 +02:00
|
|
|
&lis2dw12_data, &lis2dw12_cfg, POST_KERNEL,
|
|
|
|
CONFIG_SENSOR_INIT_PRIORITY, &lis2dw12_driver_api);
|