2016-11-27 18:21:30 +08:00
|
|
|
/* lps25hb.c - Driver for LPS25HB pressure and temperature sensor */
|
2016-06-01 21:11:01 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Intel Corporation
|
|
|
|
*
|
2017-01-19 09:01:01 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-06-01 21:11:01 +08:00
|
|
|
*/
|
|
|
|
|
2020-03-27 05:47:47 +08:00
|
|
|
#define DT_DRV_COMPAT st_lps25hb_press
|
|
|
|
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/drivers/sensor.h>
|
|
|
|
#include <zephyr/kernel.h>
|
|
|
|
#include <zephyr/device.h>
|
|
|
|
#include <zephyr/init.h>
|
|
|
|
#include <zephyr/sys/byteorder.h>
|
|
|
|
#include <zephyr/sys/__assert.h>
|
|
|
|
#include <zephyr/logging/log.h>
|
2016-06-01 21:11:01 +08:00
|
|
|
|
2016-10-15 20:19:57 +08:00
|
|
|
#include "lps25hb.h"
|
2016-06-01 21:11:01 +08:00
|
|
|
|
2019-10-11 18:40:57 +08:00
|
|
|
LOG_MODULE_REGISTER(LPS25HB, CONFIG_SENSOR_LOG_LEVEL);
|
2018-10-10 14:38:54 +08:00
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static inline int lps25hb_power_ctrl(const struct device *dev, uint8_t value)
|
2016-06-01 21:11:01 +08:00
|
|
|
{
|
2020-05-29 02:44:16 +08:00
|
|
|
const struct lps25hb_config *config = dev->config;
|
2016-06-01 21:11:01 +08:00
|
|
|
|
2022-06-23 23:09:01 +08:00
|
|
|
return i2c_reg_update_byte_dt(&config->i2c, LPS25HB_REG_CTRL_REG1,
|
|
|
|
LPS25HB_MASK_CTRL_REG1_PD,
|
|
|
|
value << LPS25HB_SHIFT_CTRL_REG1_PD);
|
2016-06-01 21:11:01 +08:00
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static inline int lps25hb_set_odr_raw(const struct device *dev, uint8_t odr)
|
2016-06-01 21:11:01 +08:00
|
|
|
{
|
2020-05-29 02:44:16 +08:00
|
|
|
const struct lps25hb_config *config = dev->config;
|
2016-06-01 21:11:01 +08:00
|
|
|
|
2022-06-23 23:09:01 +08:00
|
|
|
return i2c_reg_update_byte_dt(&config->i2c, LPS25HB_REG_CTRL_REG1,
|
|
|
|
LPS25HB_MASK_CTRL_REG1_ODR,
|
|
|
|
odr << LPS25HB_SHIFT_CTRL_REG1_ODR);
|
2016-06-01 21:11:01 +08:00
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int lps25hb_sample_fetch(const struct device *dev,
|
2016-06-01 21:11:01 +08:00
|
|
|
enum sensor_channel chan)
|
|
|
|
{
|
2020-05-29 03:23:02 +08:00
|
|
|
struct lps25hb_data *data = dev->data;
|
2020-05-29 02:44:16 +08:00
|
|
|
const struct lps25hb_config *config = dev->config;
|
2020-05-28 00:26:57 +08:00
|
|
|
uint8_t out[5];
|
2016-06-01 21:11:01 +08:00
|
|
|
int offset;
|
|
|
|
|
2016-07-28 07:56:08 +08:00
|
|
|
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
|
2016-06-01 21:11:01 +08:00
|
|
|
|
|
|
|
for (offset = 0; offset < sizeof(out); ++offset) {
|
2022-06-23 23:09:01 +08:00
|
|
|
if (i2c_reg_read_byte_dt(&config->i2c,
|
|
|
|
LPS25HB_REG_PRESS_OUT_XL + offset,
|
|
|
|
out + offset) < 0) {
|
2018-10-10 14:38:54 +08:00
|
|
|
LOG_DBG("failed to read sample");
|
2016-06-01 21:11:01 +08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
data->sample_press = (int32_t)((uint32_t)(out[0]) |
|
|
|
|
((uint32_t)(out[1]) << 8) |
|
|
|
|
((uint32_t)(out[2]) << 16));
|
|
|
|
data->sample_temp = (int16_t)((uint16_t)(out[3]) |
|
|
|
|
((uint16_t)(out[4]) << 8));
|
2016-06-01 21:11:01 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void lps25hb_press_convert(struct sensor_value *val,
|
2020-05-28 00:26:57 +08:00
|
|
|
int32_t raw_val)
|
2016-06-01 21:11:01 +08:00
|
|
|
{
|
2016-12-08 21:08:51 +08:00
|
|
|
/* val = raw_val / 40960 */
|
|
|
|
val->val1 = raw_val / 40960;
|
2020-05-28 00:26:57 +08:00
|
|
|
val->val2 = ((int32_t)raw_val * 1000000 / 40960) % 1000000;
|
2016-06-01 21:11:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void lps25hb_temp_convert(struct sensor_value *val,
|
2020-05-28 00:26:57 +08:00
|
|
|
int16_t raw_val)
|
2016-06-01 21:11:01 +08:00
|
|
|
{
|
2020-05-28 00:26:57 +08:00
|
|
|
int32_t uval;
|
2016-12-08 21:08:51 +08:00
|
|
|
|
|
|
|
/* val = raw_val / 480 + 42.5 */
|
2020-05-28 00:26:57 +08:00
|
|
|
uval = (int32_t)raw_val * 1000000 / 480 + 42500000;
|
2016-12-08 21:08:51 +08:00
|
|
|
val->val1 = (raw_val * 10 / 480 + 425) / 10;
|
|
|
|
val->val2 = uval % 1000000;
|
2016-06-01 21:11:01 +08:00
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int lps25hb_channel_get(const struct device *dev,
|
2016-06-01 21:11:01 +08:00
|
|
|
enum sensor_channel chan,
|
|
|
|
struct sensor_value *val)
|
|
|
|
{
|
2020-05-29 03:23:02 +08:00
|
|
|
struct lps25hb_data *data = dev->data;
|
2016-06-01 21:11:01 +08:00
|
|
|
|
|
|
|
if (chan == SENSOR_CHAN_PRESS) {
|
|
|
|
lps25hb_press_convert(val, data->sample_press);
|
2018-03-23 16:41:09 +08:00
|
|
|
} else if (chan == SENSOR_CHAN_AMBIENT_TEMP) {
|
2016-06-01 21:11:01 +08:00
|
|
|
lps25hb_temp_convert(val, data->sample_temp);
|
|
|
|
} else {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-24 15:33:26 +08:00
|
|
|
static const struct sensor_driver_api lps25hb_api_funcs = {
|
2016-06-01 21:11:01 +08:00
|
|
|
.sample_fetch = lps25hb_sample_fetch,
|
|
|
|
.channel_get = lps25hb_channel_get,
|
|
|
|
};
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int lps25hb_init_chip(const struct device *dev)
|
2016-06-01 21:11:01 +08:00
|
|
|
{
|
2020-05-29 02:44:16 +08:00
|
|
|
const struct lps25hb_config *config = dev->config;
|
2020-05-28 00:26:57 +08:00
|
|
|
uint8_t chip_id;
|
2016-06-01 21:11:01 +08:00
|
|
|
|
|
|
|
lps25hb_power_ctrl(dev, 0);
|
2019-03-29 04:57:54 +08:00
|
|
|
k_busy_wait(USEC_PER_MSEC * 50U);
|
2016-06-01 21:11:01 +08:00
|
|
|
|
|
|
|
if (lps25hb_power_ctrl(dev, 1) < 0) {
|
2018-10-10 14:38:54 +08:00
|
|
|
LOG_DBG("failed to power on device");
|
2016-06-01 21:11:01 +08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2019-03-29 04:57:54 +08:00
|
|
|
k_busy_wait(USEC_PER_MSEC * 20U);
|
2016-06-01 21:11:01 +08:00
|
|
|
|
2022-06-23 23:09:01 +08:00
|
|
|
if (i2c_reg_read_byte_dt(&config->i2c, LPS25HB_REG_WHO_AM_I,
|
|
|
|
&chip_id) < 0) {
|
2018-10-10 14:38:54 +08:00
|
|
|
LOG_DBG("failed reading chip id");
|
2016-06-01 21:11:01 +08:00
|
|
|
goto err_poweroff;
|
|
|
|
}
|
|
|
|
if (chip_id != LPS25HB_VAL_WHO_AM_I) {
|
2018-10-10 14:38:54 +08:00
|
|
|
LOG_DBG("invalid chip id 0x%x", chip_id);
|
2016-06-01 21:11:01 +08:00
|
|
|
goto err_poweroff;
|
|
|
|
}
|
|
|
|
|
2018-10-10 14:38:54 +08:00
|
|
|
LOG_DBG("chip id 0x%x", chip_id);
|
2016-06-01 21:11:01 +08:00
|
|
|
|
|
|
|
if (lps25hb_set_odr_raw(dev, LPS25HB_DEFAULT_SAMPLING_RATE)
|
|
|
|
< 0) {
|
2018-10-10 14:38:54 +08:00
|
|
|
LOG_DBG("failed to set sampling rate");
|
2016-06-01 21:11:01 +08:00
|
|
|
goto err_poweroff;
|
|
|
|
}
|
|
|
|
|
2022-06-23 23:09:01 +08:00
|
|
|
if (i2c_reg_update_byte_dt(&config->i2c, LPS25HB_REG_CTRL_REG1,
|
|
|
|
LPS25HB_MASK_CTRL_REG1_BDU,
|
|
|
|
(1 << LPS25HB_SHIFT_CTRL_REG1_BDU)) < 0) {
|
2018-10-10 14:38:54 +08:00
|
|
|
LOG_DBG("failed to set BDU");
|
2016-06-01 21:11:01 +08:00
|
|
|
goto err_poweroff;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_poweroff:
|
|
|
|
lps25hb_power_ctrl(dev, 0);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int lps25hb_init(const struct device *dev)
|
2016-06-01 21:11:01 +08:00
|
|
|
{
|
2020-05-29 02:44:16 +08:00
|
|
|
const struct lps25hb_config * const config = dev->config;
|
2016-06-01 21:11:01 +08:00
|
|
|
|
2022-06-23 23:09:01 +08:00
|
|
|
if (!device_is_ready(config->i2c.bus)) {
|
|
|
|
LOG_ERR("I2C bus device not ready");
|
|
|
|
return -ENODEV;
|
2016-06-01 21:11:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lps25hb_init_chip(dev) < 0) {
|
2018-10-10 14:38:54 +08:00
|
|
|
LOG_DBG("failed to initialize chip");
|
2016-06-01 21:11:01 +08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-07-04 21:40:53 +08:00
|
|
|
#define LPS25HB_DEFINE(inst) \
|
|
|
|
static struct lps25hb_data lps25hb_data_##inst; \
|
|
|
|
\
|
|
|
|
static const struct lps25hb_config lps25hb_config_##inst = { \
|
|
|
|
.i2c = I2C_DT_SPEC_INST_GET(inst), \
|
|
|
|
}; \
|
|
|
|
\
|
2022-08-17 08:49:36 +08:00
|
|
|
SENSOR_DEVICE_DT_INST_DEFINE(inst, lps25hb_init, NULL, \
|
2022-07-04 21:40:53 +08:00
|
|
|
&lps25hb_data_##inst, &lps25hb_config_##inst, POST_KERNEL, \
|
|
|
|
CONFIG_SENSOR_INIT_PRIORITY, &lps25hb_api_funcs); \
|
|
|
|
|
|
|
|
DT_INST_FOREACH_STATUS_OKAY(LPS25HB_DEFINE)
|