2019-01-29 11:06:35 +01:00
|
|
|
/* ST Microelectronics LPS22HH pressure and temperature sensor
|
|
|
|
*
|
|
|
|
* Copyright (c) 2019 STMicroelectronics
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Datasheet:
|
|
|
|
* https://www.st.com/resource/en/datasheet/lps22hh.pdf
|
|
|
|
*/
|
|
|
|
|
2020-03-26 16:47:47 -05:00
|
|
|
#define DT_DRV_COMPAT st_lps22hh
|
|
|
|
|
2020-01-25 05:35:02 -06:00
|
|
|
#include <drivers/sensor.h>
|
2019-01-29 11:06:35 +01:00
|
|
|
#include <kernel.h>
|
|
|
|
#include <device.h>
|
|
|
|
#include <init.h>
|
|
|
|
#include <sys/byteorder.h>
|
|
|
|
#include <sys/__assert.h>
|
|
|
|
#include <logging/log.h>
|
|
|
|
|
|
|
|
#include "lps22hh.h"
|
|
|
|
|
2019-10-11 12:40:57 +02:00
|
|
|
LOG_MODULE_REGISTER(LPS22HH, CONFIG_SENSOR_LOG_LEVEL);
|
2019-01-29 11:06:35 +01:00
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static inline int lps22hh_set_odr_raw(const struct device *dev, uint8_t odr)
|
2019-01-29 11:06:35 +01:00
|
|
|
{
|
2021-12-13 16:26:11 +01:00
|
|
|
const struct lps22hh_config * const cfg = dev->config;
|
|
|
|
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
|
2019-01-29 11:06:35 +01:00
|
|
|
|
2021-12-13 16:26:11 +01:00
|
|
|
return lps22hh_data_rate_set(ctx, odr);
|
2019-01-29 11:06:35 +01:00
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int lps22hh_sample_fetch(const struct device *dev,
|
2019-01-29 11:06:35 +01:00
|
|
|
enum sensor_channel chan)
|
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
struct lps22hh_data *data = dev->data;
|
2021-12-13 16:26:11 +01:00
|
|
|
const struct lps22hh_config * const cfg = dev->config;
|
|
|
|
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
|
2020-10-05 18:06:27 +02:00
|
|
|
uint32_t raw_press;
|
|
|
|
int16_t raw_temp;
|
2019-01-29 11:06:35 +01:00
|
|
|
|
|
|
|
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
|
|
|
|
|
2021-12-13 16:26:11 +01:00
|
|
|
if (lps22hh_pressure_raw_get(ctx, &raw_press) < 0) {
|
2019-01-29 11:06:35 +01:00
|
|
|
LOG_DBG("Failed to read sample");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2021-12-13 16:26:11 +01:00
|
|
|
if (lps22hh_temperature_raw_get(ctx, &raw_temp) < 0) {
|
2019-01-29 11:06:35 +01:00
|
|
|
LOG_DBG("Failed to read sample");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2020-10-05 18:06:27 +02:00
|
|
|
data->sample_press = raw_press;
|
|
|
|
data->sample_temp = raw_temp;
|
2019-01-29 11:06:35 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void lps22hh_press_convert(struct sensor_value *val,
|
2020-05-27 11:26:57 -05:00
|
|
|
int32_t raw_val)
|
2019-01-29 11:06:35 +01:00
|
|
|
{
|
2021-06-03 11:43:12 +02:00
|
|
|
int32_t press_tmp = raw_val >> 8; /* raw value is left aligned (24 msb) */
|
|
|
|
|
2019-01-29 11:06:35 +01:00
|
|
|
/* Pressure sensitivity is 4096 LSB/hPa */
|
2021-06-03 11:43:12 +02:00
|
|
|
/* Also convert hPa into kPa */
|
|
|
|
|
|
|
|
val->val1 = press_tmp / 40960;
|
2021-09-03 18:17:05 +02:00
|
|
|
|
|
|
|
/* For the decimal part use (3125 / 128) as a factor instead of
|
|
|
|
* (1000000 / 40960) to avoid int32 overflow
|
|
|
|
*/
|
|
|
|
val->val2 = (press_tmp % 40960) * 3125 / 128;
|
2019-01-29 11:06:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void lps22hh_temp_convert(struct sensor_value *val,
|
2020-05-27 11:26:57 -05:00
|
|
|
int16_t raw_val)
|
2019-01-29 11:06:35 +01:00
|
|
|
{
|
|
|
|
/* Temperature sensitivity is 100 LSB/deg C */
|
|
|
|
val->val1 = raw_val / 100;
|
2020-05-27 11:26:57 -05:00
|
|
|
val->val2 = ((int32_t)raw_val % 100) * 10000;
|
2019-01-29 11:06:35 +01:00
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int lps22hh_channel_get(const struct device *dev,
|
2019-01-29 11:06:35 +01:00
|
|
|
enum sensor_channel chan,
|
|
|
|
struct sensor_value *val)
|
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
struct lps22hh_data *data = dev->data;
|
2019-01-29 11:06:35 +01:00
|
|
|
|
|
|
|
if (chan == SENSOR_CHAN_PRESS) {
|
|
|
|
lps22hh_press_convert(val, data->sample_press);
|
|
|
|
} else if (chan == SENSOR_CHAN_AMBIENT_TEMP) {
|
|
|
|
lps22hh_temp_convert(val, data->sample_temp);
|
|
|
|
} else {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:26:57 -05:00
|
|
|
static const uint16_t lps22hh_map[] = {0, 1, 10, 25, 50, 75, 100, 200};
|
2019-01-29 11:06:35 +01:00
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int lps22hh_odr_set(const struct device *dev, uint16_t freq)
|
2019-01-29 11:06:35 +01:00
|
|
|
{
|
|
|
|
int odr;
|
|
|
|
|
|
|
|
for (odr = 0; odr < ARRAY_SIZE(lps22hh_map); odr++) {
|
|
|
|
if (freq == lps22hh_map[odr]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (odr == ARRAY_SIZE(lps22hh_map)) {
|
|
|
|
LOG_DBG("bad frequency");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lps22hh_set_odr_raw(dev, odr) < 0) {
|
|
|
|
LOG_DBG("failed to set sampling rate");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int lps22hh_attr_set(const struct device *dev,
|
|
|
|
enum sensor_channel chan,
|
2019-01-29 11:06:35 +01:00
|
|
|
enum sensor_attribute attr,
|
|
|
|
const struct sensor_value *val)
|
|
|
|
{
|
|
|
|
if (chan != SENSOR_CHAN_ALL) {
|
|
|
|
LOG_WRN("attr_set() not supported on this channel.");
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (attr) {
|
|
|
|
case SENSOR_ATTR_SAMPLING_FREQUENCY:
|
|
|
|
return lps22hh_odr_set(dev, val->val1);
|
|
|
|
default:
|
|
|
|
LOG_DBG("operation not supported.");
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-13 16:26:11 +01:00
|
|
|
static const struct sensor_driver_api lps22hh_driver_api = {
|
2019-01-29 11:06:35 +01:00
|
|
|
.attr_set = lps22hh_attr_set,
|
|
|
|
.sample_fetch = lps22hh_sample_fetch,
|
|
|
|
.channel_get = lps22hh_channel_get,
|
|
|
|
#if CONFIG_LPS22HH_TRIGGER
|
|
|
|
.trigger_set = lps22hh_trigger_set,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int lps22hh_init_chip(const struct device *dev)
|
2019-01-29 11:06:35 +01:00
|
|
|
{
|
2021-12-13 16:26:11 +01:00
|
|
|
const struct lps22hh_config * const cfg = dev->config;
|
|
|
|
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
|
2020-05-27 11:26:57 -05:00
|
|
|
uint8_t chip_id;
|
2021-12-13 16:26:11 +01:00
|
|
|
int ret;
|
2019-01-29 11:06:35 +01:00
|
|
|
|
2021-12-13 16:26:11 +01:00
|
|
|
if (lps22hh_device_id_get(ctx, &chip_id) < 0) {
|
|
|
|
LOG_ERR("%s: Not able to read dev id", dev->name);
|
2019-01-29 11:06:35 +01:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chip_id != LPS22HH_ID) {
|
2021-12-13 16:26:11 +01:00
|
|
|
LOG_ERR("%s: Invalid chip ID 0x%02x", dev->name, chip_id);
|
2019-01-29 11:06:35 +01:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2021-12-13 16:26:11 +01:00
|
|
|
LOG_DBG("%s: chip id 0x%x", dev->name, chip_id);
|
|
|
|
|
|
|
|
ret = lps22hh_set_odr_raw(dev, CONFIG_LPS22HH_SAMPLING_RATE);
|
|
|
|
if (ret < 0) {
|
|
|
|
LOG_ERR("%s: Failed to set sampling rate", dev->name);
|
|
|
|
return ret;
|
2019-01-29 11:06:35 +01:00
|
|
|
}
|
|
|
|
|
2021-12-13 16:26:11 +01:00
|
|
|
if (lps22hh_block_data_update_set(ctx, PROPERTY_ENABLE) < 0) {
|
|
|
|
LOG_ERR("%s: Failed to set BDU", dev->name);
|
|
|
|
return ret;
|
2019-01-29 11:06:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int lps22hh_init(const struct device *dev)
|
2019-01-29 11:06:35 +01:00
|
|
|
{
|
|
|
|
if (lps22hh_init_chip(dev) < 0) {
|
|
|
|
LOG_DBG("Failed to initialize chip");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_LPS22HH_TRIGGER
|
|
|
|
if (lps22hh_init_interrupt(dev) < 0) {
|
|
|
|
LOG_ERR("Failed to initialize interrupt.");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-13 16:26:11 +01:00
|
|
|
#if DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) == 0
|
|
|
|
#warning "LPS22HH driver enabled without any devices"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Instantiation macros used when a device is on a SPI bus.
|
|
|
|
*/
|
2019-01-29 11:06:35 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_LPS22HH_TRIGGER
|
2021-12-13 16:26:11 +01:00
|
|
|
#define LPS22HH_CFG_IRQ(inst) \
|
|
|
|
.gpio_int = GPIO_DT_SPEC_INST_GET(inst, drdy_gpios),
|
2019-01-29 11:06:35 +01:00
|
|
|
#else
|
2021-12-13 16:26:11 +01:00
|
|
|
#define LPS22HH_CFG_IRQ(inst)
|
|
|
|
#endif /* CONFIG_LPS22HH_TRIGGER */
|
|
|
|
|
|
|
|
#define LPS22HH_SPI_OPERATION (SPI_WORD_SET(8) | \
|
|
|
|
SPI_OP_MODE_MASTER | \
|
|
|
|
SPI_MODE_CPOL | \
|
|
|
|
SPI_MODE_CPHA) \
|
|
|
|
|
|
|
|
#define LPS22HH_CONFIG_SPI(inst) \
|
|
|
|
{ \
|
|
|
|
.ctx = { \
|
|
|
|
.read_reg = \
|
|
|
|
(stmdev_read_ptr) stmemsc_spi_read, \
|
|
|
|
.write_reg = \
|
|
|
|
(stmdev_write_ptr) stmemsc_spi_write, \
|
|
|
|
.handle = \
|
|
|
|
(void *)&lps22hh_config_##inst.stmemsc_cfg, \
|
|
|
|
}, \
|
|
|
|
.stmemsc_cfg = { \
|
|
|
|
.spi = SPI_DT_SPEC_INST_GET(inst, \
|
|
|
|
LPS22HH_SPI_OPERATION, \
|
|
|
|
0), \
|
|
|
|
}, \
|
|
|
|
COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, drdy_gpios), \
|
|
|
|
(LPS22HH_CFG_IRQ(inst)), ()) \
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Instantiation macros used when a device is on an I2C bus.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LPS22HH_CONFIG_I2C(inst) \
|
|
|
|
{ \
|
|
|
|
.ctx = { \
|
|
|
|
.read_reg = \
|
|
|
|
(stmdev_read_ptr) stmemsc_i2c_read, \
|
|
|
|
.write_reg = \
|
|
|
|
(stmdev_write_ptr) stmemsc_i2c_write, \
|
|
|
|
.handle = \
|
|
|
|
(void *)&lps22hh_config_##inst.stmemsc_cfg, \
|
|
|
|
}, \
|
|
|
|
.stmemsc_cfg = { \
|
|
|
|
.i2c = I2C_DT_SPEC_INST_GET(inst), \
|
|
|
|
}, \
|
|
|
|
COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, drdy_gpios), \
|
|
|
|
(LPS22HH_CFG_IRQ(inst)), ()) \
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Main instantiation macro. Use of COND_CODE_1() selects the right
|
|
|
|
* bus-specific macro at preprocessor time.
|
|
|
|
*/
|
2019-01-29 11:06:35 +01:00
|
|
|
|
2021-12-13 16:26:11 +01:00
|
|
|
#define LPS22HH_DEFINE(inst) \
|
|
|
|
static struct lps22hh_data lps22hh_data_##inst; \
|
|
|
|
static const struct lps22hh_config lps22hh_config_##inst = \
|
|
|
|
COND_CODE_1(DT_INST_ON_BUS(inst, spi), \
|
|
|
|
(LPS22HH_CONFIG_SPI(inst)), \
|
|
|
|
(LPS22HH_CONFIG_I2C(inst))); \
|
|
|
|
DEVICE_DT_INST_DEFINE(inst, lps22hh_init, NULL, &lps22hh_data_##inst, \
|
|
|
|
&lps22hh_config_##inst, POST_KERNEL, \
|
|
|
|
CONFIG_SENSOR_INIT_PRIORITY, &lps22hh_driver_api);
|
|
|
|
|
|
|
|
DT_INST_FOREACH_STATUS_OKAY(LPS22HH_DEFINE)
|