diff --git a/drivers/sensor/st/CMakeLists.txt b/drivers/sensor/st/CMakeLists.txt index aea4b4c0c72e7..b1d6a31124148 100644 --- a/drivers/sensor/st/CMakeLists.txt +++ b/drivers/sensor/st/CMakeLists.txt @@ -20,6 +20,7 @@ add_subdirectory_ifdef(CONFIG_LIS2DUX12 lis2dux12) add_subdirectory_ifdef(CONFIG_LIS2DW12 lis2dw12) add_subdirectory_ifdef(CONFIG_LIS2MDL lis2mdl) add_subdirectory_ifdef(CONFIG_LIS3MDL lis3mdl) +add_subdirectory_ifdef(CONFIG_LIS3DSHTR lis3dshtr) add_subdirectory_ifdef(CONFIG_LPS22HB lps22hb) add_subdirectory_ifdef(CONFIG_LPS22HH lps22hh) add_subdirectory_ifdef(CONFIG_LPS25HB lps25hb) diff --git a/drivers/sensor/st/Kconfig b/drivers/sensor/st/Kconfig index 87d2cd665c5b4..9cd5af1943980 100644 --- a/drivers/sensor/st/Kconfig +++ b/drivers/sensor/st/Kconfig @@ -17,6 +17,7 @@ source "drivers/sensor/st/lis2ds12/Kconfig" source "drivers/sensor/st/lis2du12/Kconfig" source "drivers/sensor/st/lis2dux12/Kconfig" source "drivers/sensor/st/lis2dw12/Kconfig" +source "drivers/sensor/st/lis3dshtr/Kconfig" source "drivers/sensor/st/lis2mdl/Kconfig" source "drivers/sensor/st/lis3mdl/Kconfig" source "drivers/sensor/st/lps22hb/Kconfig" diff --git a/drivers/sensor/st/lis3dshtr/CMakeLists.txt b/drivers/sensor/st/lis3dshtr/CMakeLists.txt new file mode 100644 index 0000000000000..b5cf7fe00ea95 --- /dev/null +++ b/drivers/sensor/st/lis3dshtr/CMakeLists.txt @@ -0,0 +1,3 @@ +zephyr_library() + +zephyr_library_sources(lis3dshtr.c) diff --git a/drivers/sensor/st/lis3dshtr/Kconfig b/drivers/sensor/st/lis3dshtr/Kconfig new file mode 100644 index 0000000000000..5e11264810369 --- /dev/null +++ b/drivers/sensor/st/lis3dshtr/Kconfig @@ -0,0 +1,14 @@ +# Copyright (c) 2025, gaiaochos.com +# SPDX-License-Identifier: Apache 2.0 + +config LIS3DSHTR + bool "ST LIS3DSHTR 3 Axis Accelerometer" + default y + select SPI + depends on DT_HAS_ST_LIS3DSHTR_ENABLED + help + Enable driver for the LI3DSHTR SPI based accelemetor + with a temperature sensor. + + + diff --git a/drivers/sensor/st/lis3dshtr/lis3dshtr.c b/drivers/sensor/st/lis3dshtr/lis3dshtr.c new file mode 100644 index 0000000000000..f0f01f32daabc --- /dev/null +++ b/drivers/sensor/st/lis3dshtr/lis3dshtr.c @@ -0,0 +1,182 @@ +/* + * Copyright (c) 2025, gaiaochos.com + * SPDX-License-Identifier: Apache 2.0 + * + */ + +#define DT_DRV_COMPAT st_lis3dshtr + +#include +LOG_MODULE_REGISTER(LIS3DSHTR, CONFIG_SENSOR_LOG_LEVEL); + +#include +#include +#include + + +struct st_lis3dshtr_config { + const struct spi_dt_spec spi_controller; +}; + +struct st_lis3dshtr_data { + uint8_t sample_int; + uint8_t sample_dec; +}; + +static int lis3dshtr_write_register(const struct device *dev, uint8_t reg, uint8_t value) { + const struct st_lis3dshtr_config *cfg = dev->config; + + uint8_t tx_buf[2] = {reg,value}; + + const struct spi_buf buf = { + .buf = tx_buf, + .len = sizeof(tx_buf) + }; + const struct spi_buf_set tx = { + .buffers = &buf, + .count = 1 + }; + + + return spi_write_dt(&cfg->spi_controller, &tx); +} + +static int lis3dshtr_read_register(const struct device *dev, uint8_t reg, uint8_t *val) { + const struct st_lis3dshtr_config *cfg = dev->config; + int ret; + uint8_t tx_buf[2]; + uint8_t rx_buf[2]; + + /*Write address to read*/ + tx_buf[0] = 0x80 | reg; + tx_buf[1] = 0x00; + + const struct spi_buf tx_buf_arr = { + .buf = tx_buf, + .len = sizeof(tx_buf), + }; + const struct spi_buf_set tx_set = { + .buffers = &tx_buf_arr, + .count = 1, + }; + const struct spi_buf rx_buf_arr = { + .buf = rx_buf, + .len = sizeof(rx_buf), + }; + const struct spi_buf_set rx_set = { + .buffers = &rx_buf_arr, + .count = 1, + }; + + ret = spi_transceive_dt(&cfg->spi_controller, &tx_set, &rx_set); + + if (ret != 0) { + LOG_ERR("Error reading channel axis (%02X)", reg); + return ret; + } + + *val = rx_buf[1]; + + return ret; +} + +static int lis3dshtr_sample_fetch(const struct device *dev, enum sensor_channel chan) +{ + struct st_lis3dshtr_data *data = dev->data; + int ret; + + if (chan != SENSOR_CHAN_ACCEL_X && chan != SENSOR_CHAN_ALL) { + LOG_ERR("selected channel in fetch not supported"); + return -EINVAL; + } + + ret = lis3dshtr_read_register(dev, 0x29, &data->sample_int); + + + if (ret != 0) { + LOG_ERR("Could not read channel HIGH byte"); + return ret; + } + + ret = lis3dshtr_read_register(dev, 0x28, &data->sample_dec); + + if (ret != 0) { + LOG_ERR("Could not read channel LOW byte"); + return ret; + } + + return ret; +} + +static int lis3dshtr_channel_get(const struct device *dev, enum sensor_channel chan, struct sensor_value *val) +{ + struct st_lis3dshtr_data *data = dev->data; + int16_t raw; + int32_t micro_g; + + if(chan != SENSOR_CHAN_ACCEL_X) { + LOG_ERR("selected channel in get (%d) not supported \n", chan); + return -ENOTSUP; + } + + + /*Combine high and low byte*/ + raw = (int16_t)((data->sample_int << 8) | data->sample_dec); + + /*Apply 2G scaling*/ + micro_g = raw * 60; + + /*Fill sensor val with integer and fractional parts*/ + val->val1 = micro_g/1000000; + val->val2 = micro_g%1000000; + return 0; +} + + +static DEVICE_API(sensor, st_lis3dshtr_api) = { + .sample_fetch = lis3dshtr_sample_fetch, + .channel_get = lis3dshtr_channel_get, +}; + +static int st_lis3dshtr_init(const struct device *dev) +{ + const struct st_lis3dshtr_config *config = dev->config; + int ret = 0; + uint8_t config_byte; + + if(!spi_is_ready_dt(&config->spi_controller)) { + LOG_ERR("SPI Bus not ready"); + return -ENODEV; + } + + /*Static bring up*/ + config_byte = 0x60 | 0x0 | 0x7; + + ret = lis3dshtr_write_register(dev, 0x20, config_byte); + + if (ret != 0) { + LOG_ERR("Could not configure LIS3DSHTR with (%d)", ret); + return ret; + } + + LOG_INF("LIS3DSHTR initialized\n"); + + return ret; +} + +#define SENSOR_LIS3DSHTR_DEFINE(inst) \ + static struct st_lis3dshtr_data lis3dshtr_data_##inst; \ + static struct st_lis3dshtr_config lis3dshtr_config_##inst = { \ + .spi_controller = SPI_DT_SPEC_INST_GET(inst, SPI_WORD_SET(8), 0), \ + }; \ + \ + SENSOR_DEVICE_DT_INST_DEFINE(inst, \ + st_lis3dshtr_init, \ + NULL, \ + &lis3dshtr_data_##inst, \ + &lis3dshtr_config_##inst, \ + POST_KERNEL, \ + CONFIG_SENSOR_INIT_PRIORITY, \ + &st_lis3dshtr_api); + +DT_INST_FOREACH_STATUS_OKAY(SENSOR_LIS3DSHTR_DEFINE) diff --git a/dts/bindings/sensor/st,lis3dshtr.yaml b/dts/bindings/sensor/st,lis3dshtr.yaml new file mode 100644 index 0000000000000..7716a0d330263 --- /dev/null +++ b/dts/bindings/sensor/st,lis3dshtr.yaml @@ -0,0 +1,9 @@ +# Copyright (c) 2025 gaiaochos.com +# SPDX-License-Identifier: Apache 2.0 + +description: | + STMicroelectronics LIS3DSHTR 3-axis accelerometer accessed via SPI + +compatible: "st,lis3dshtr" + +include: [sensor-device.yaml, spi-device.yaml]