From 9dbdd81abe22dcbc671ea066cfdfc041d71f3c83 Mon Sep 17 00:00:00 2001 From: Armando Visconti Date: Tue, 29 Jan 2019 11:06:35 +0100 Subject: [PATCH] driver/sensor: add LPS22HH sensor support Add support to STM LPS22HH pressure and temperature sensor. The driver support I2C and SPI bus communication and both polling and drdy trigger mode. Signed-off-by: Armando Visconti --- CODEOWNERS | 1 + drivers/sensor/CMakeLists.txt | 1 + drivers/sensor/Kconfig | 2 + drivers/sensor/lps22hh/CMakeLists.txt | 12 ++ drivers/sensor/lps22hh/Kconfig | 77 ++++++++ drivers/sensor/lps22hh/lps22hh.c | 233 +++++++++++++++++++++++ drivers/sensor/lps22hh/lps22hh.h | 88 +++++++++ drivers/sensor/lps22hh/lps22hh_i2c.c | 54 ++++++ drivers/sensor/lps22hh/lps22hh_spi.c | 128 +++++++++++++ drivers/sensor/lps22hh/lps22hh_trigger.c | 182 ++++++++++++++++++ dts/bindings/sensor/st,lps22hh-i2c.yaml | 25 +++ dts/bindings/sensor/st,lps22hh-spi.yaml | 25 +++ tests/drivers/build_all/dts_fixup.h | 19 ++ 13 files changed, 847 insertions(+) create mode 100644 drivers/sensor/lps22hh/CMakeLists.txt create mode 100644 drivers/sensor/lps22hh/Kconfig create mode 100644 drivers/sensor/lps22hh/lps22hh.c create mode 100644 drivers/sensor/lps22hh/lps22hh.h create mode 100644 drivers/sensor/lps22hh/lps22hh_i2c.c create mode 100644 drivers/sensor/lps22hh/lps22hh_spi.c create mode 100644 drivers/sensor/lps22hh/lps22hh_trigger.c create mode 100644 dts/bindings/sensor/st,lps22hh-i2c.yaml create mode 100644 dts/bindings/sensor/st,lps22hh-spi.yaml diff --git a/CODEOWNERS b/CODEOWNERS index b527f5281a4..27d22ff7633 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -197,6 +197,7 @@ /ext/hal/atmel/asf/sam/include/same70*/ @aurel32 /ext/hal/atmel/asf/sam0/include/samr21/ @benpicco /dts/bindings/sensor/*bme680* @BoschSensortec +/dts/bindings/sensor/st,* @avisconti /ext/hal/cmsis/ @MaureenHelm @galak /ext/hal/microchip/ @franciscomunoz @albertofloyd @scottwcpg /ext/hal/nordic/ @carlescufi @anangl diff --git a/drivers/sensor/CMakeLists.txt b/drivers/sensor/CMakeLists.txt index 6fe9ea83821..2519ef7dbf7 100644 --- a/drivers/sensor/CMakeLists.txt +++ b/drivers/sensor/CMakeLists.txt @@ -31,6 +31,7 @@ add_subdirectory_ifdef(CONFIG_LIS2DW12 lis2dw12) add_subdirectory_ifdef(CONFIG_LIS2MDL lis2mdl) add_subdirectory_ifdef(CONFIG_LIS3MDL lis3mdl) add_subdirectory_ifdef(CONFIG_LPS22HB lps22hb) +add_subdirectory_ifdef(CONFIG_LPS22HH lps22hh) add_subdirectory_ifdef(CONFIG_LPS25HB lps25hb) add_subdirectory_ifdef(CONFIG_LSM303DLHC_MAGN lsm303dlhc_magn) add_subdirectory_ifdef(CONFIG_LSM6DS0 lsm6ds0) diff --git a/drivers/sensor/Kconfig b/drivers/sensor/Kconfig index 228b3f30288..a2e518bc912 100644 --- a/drivers/sensor/Kconfig +++ b/drivers/sensor/Kconfig @@ -87,6 +87,8 @@ source "drivers/sensor/lis3mdl/Kconfig" source "drivers/sensor/lps22hb/Kconfig" +source "drivers/sensor/lps22hh/Kconfig" + source "drivers/sensor/lps25hb/Kconfig" source "drivers/sensor/lsm303dlhc_magn/Kconfig" diff --git a/drivers/sensor/lps22hh/CMakeLists.txt b/drivers/sensor/lps22hh/CMakeLists.txt new file mode 100644 index 00000000000..93e4ce5784e --- /dev/null +++ b/drivers/sensor/lps22hh/CMakeLists.txt @@ -0,0 +1,12 @@ +# ST Microelectronics LPS22HH pressure and temperature sensor +# +# Copyright (c) 2019 STMicroelectronics +# +# SPDX-License-Identifier: Apache-2.0 +# +zephyr_library() + +zephyr_library_sources_ifdef(CONFIG_LPS22HH lps22hh.c) +zephyr_library_sources_ifdef(CONFIG_LPS22HH lps22hh_i2c.c) +zephyr_library_sources_ifdef(CONFIG_LPS22HH lps22hh_spi.c) +zephyr_library_sources_ifdef(CONFIG_LPS22HH_TRIGGER lps22hh_trigger.c) diff --git a/drivers/sensor/lps22hh/Kconfig b/drivers/sensor/lps22hh/Kconfig new file mode 100644 index 00000000000..a2a3c82e742 --- /dev/null +++ b/drivers/sensor/lps22hh/Kconfig @@ -0,0 +1,77 @@ +# ST Microelectronics LPS22HH pressure and temperature sensor +# +# Copyright (c) 2019 STMicroelectronics +# +# SPDX-License-Identifier: Apache-2.0 +# + +menuconfig LPS22HH + bool "LPS22HH pressure and temperature" + depends on (I2C && HAS_DTS_I2C) || (SPI && HAS_DTS_SPI) + select HAS_STMEMSC + select USE_STDC_LPS22HH + help + Enable driver for LPS22HH I2C-based pressure and temperature + sensor. + +if LPS22HH + +choice LPS22HH_TRIGGER_MODE + prompt "Trigger mode" + default LPS22HH_TRIGGER_GLOBAL_THREAD + help + Specify the type of triggering to be used by the driver. + +config LPS22HH_TRIGGER_NONE + bool "No trigger" + +config LPS22HH_TRIGGER_GLOBAL_THREAD + bool "Use global thread" + depends on GPIO + select LPS22HH_TRIGGER + +config LPS22HH_TRIGGER_OWN_THREAD + bool "Use own thread" + depends on GPIO + select LPS22HH_TRIGGER + +endchoice # LPS22HH_TRIGGER_MODE + +config LPS22HH_TRIGGER + bool + +config LPS22HH_THREAD_PRIORITY + int "Thread priority" + depends on LPS22HH_TRIGGER_OWN_THREAD + default 10 + help + Priority of thread used by the driver to handle interrupts. + +config LPS22HH_THREAD_STACK_SIZE + int "Thread stack size" + depends on LPS22HH_TRIGGER_OWN_THREAD + default 1024 + help + Stack size of thread used by the driver to handle interrupts. + +menu "Attributes" + +config LPS22HH_SAMPLING_RATE + int "Output data rate" + range 0 7 + default 0 + help + Sensor output data rate expressed in samples per second. + Data rates supported by the chip are: + 0: ODR selected at runtime + 1: 1Hz + 2: 10Hz + 3: 25Hz + 4: 50Hz + 5: 75Hz + 6: 100Hz + 7: 200Hz + +endmenu + +endif # LPS22HH diff --git a/drivers/sensor/lps22hh/lps22hh.c b/drivers/sensor/lps22hh/lps22hh.c new file mode 100644 index 00000000000..53373fe62d2 --- /dev/null +++ b/drivers/sensor/lps22hh/lps22hh.c @@ -0,0 +1,233 @@ +/* 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 + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "lps22hh.h" + +#define LOG_LEVEL CONFIG_SENSOR_LOG_LEVEL +LOG_MODULE_REGISTER(LPS22HH); + +static inline int lps22hh_set_odr_raw(struct device *dev, u8_t odr) +{ + struct lps22hh_data *data = dev->driver_data; + + return lps22hh_data_rate_set(data->ctx, odr); +} + +static int lps22hh_sample_fetch(struct device *dev, + enum sensor_channel chan) +{ + struct lps22hh_data *data = dev->driver_data; + axis1bit32_t raw_press; + axis1bit16_t raw_temp; + + __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL); + + if (lps22hh_pressure_raw_get(data->ctx, raw_press.u8bit) < 0) { + LOG_DBG("Failed to read sample"); + return -EIO; + } + if (lps22hh_temperature_raw_get(data->ctx, raw_temp.u8bit) < 0) { + LOG_DBG("Failed to read sample"); + return -EIO; + } + + data->sample_press = raw_press.i32bit; + data->sample_temp = raw_temp.i16bit; + + return 0; +} + +static inline void lps22hh_press_convert(struct sensor_value *val, + s32_t raw_val) +{ + /* Pressure sensitivity is 4096 LSB/hPa */ + /* Convert raw_val to val in kPa */ + val->val1 = (raw_val >> 12) / 10; + val->val2 = (raw_val >> 12) % 10 * 100000 + + (((s32_t)((raw_val) & 0x0FFF) * 100000L) >> 12); +} + +static inline void lps22hh_temp_convert(struct sensor_value *val, + s16_t raw_val) +{ + /* Temperature sensitivity is 100 LSB/deg C */ + val->val1 = raw_val / 100; + val->val2 = ((s32_t)raw_val % 100) * 10000; +} + +static int lps22hh_channel_get(struct device *dev, + enum sensor_channel chan, + struct sensor_value *val) +{ + struct lps22hh_data *data = dev->driver_data; + + 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; +} + +static const u16_t lps22hh_map[] = {0, 1, 10, 25, 50, 75, 100, 200}; + +static int lps22hh_odr_set(struct device *dev, u16_t freq) +{ + 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; +} + +static int lps22hh_attr_set(struct device *dev, enum sensor_channel chan, + 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; +} + +static const struct sensor_driver_api lps22hh_api_funcs = { + .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 +}; + +static int lps22hh_init_chip(struct device *dev) +{ + struct lps22hh_data *data = dev->driver_data; + u8_t chip_id; + + if (lps22hh_device_id_get(data->ctx, &chip_id) < 0) { + LOG_DBG("Failed reading chip id"); + return -EIO; + } + + if (chip_id != LPS22HH_ID) { + LOG_DBG("Invalid chip id 0x%x", chip_id); + return -EIO; + } + + if (lps22hh_set_odr_raw(dev, CONFIG_LPS22HH_SAMPLING_RATE) < 0) { + LOG_DBG("Failed to set sampling rate"); + return -EIO; + } + + if (lps22hh_block_data_update_set(data->ctx, PROPERTY_ENABLE) < 0) { + LOG_DBG("Failed to set BDU"); + return -EIO; + } + + return 0; +} + +static int lps22hh_init(struct device *dev) +{ + const struct lps22hh_config * const config = dev->config->config_info; + struct lps22hh_data *data = dev->driver_data; + + data->bus = device_get_binding(config->master_dev_name); + if (!data->bus) { + LOG_DBG("bus master not found: %s", config->master_dev_name); + return -EINVAL; + } + + config->bus_init(dev); + + 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; +} + +static struct lps22hh_data lps22hh_data; + +static const struct lps22hh_config lps22hh_config = { + .master_dev_name = DT_INST_0_ST_LPS22HH_BUS_NAME, +#ifdef CONFIG_LPS22HH_TRIGGER + .drdy_port = DT_INST_0_ST_LPS22HH_DRDY_GPIOS_CONTROLLER, + .drdy_pin = DT_INST_0_ST_LPS22HH_DRDY_GPIOS_PIN, +#endif +#if defined(DT_ST_LPS22HH_BUS_SPI) + .bus_init = lps22hh_spi_init, + .spi_conf.frequency = DT_INST_0_ST_LPS22HH_SPI_MAX_FREQUENCY, + .spi_conf.operation = (SPI_OP_MODE_MASTER | SPI_MODE_CPOL | + SPI_MODE_CPHA | SPI_WORD_SET(8) | + SPI_LINES_SINGLE), + .spi_conf.slave = DT_INST_0_ST_LPS22HH_BASE_ADDRESS, +#if defined(DT_INST_0_ST_LPS22HH_CS_GPIO_CONTROLLER) + .gpio_cs_port = DT_INST_0_ST_LPS22HH_CS_GPIO_CONTROLLER, + .cs_gpio = DT_INST_0_ST_LPS22HH_CS_GPIO_PIN, + + .spi_conf.cs = &lps22hh_data.cs_ctrl, +#else + .spi_conf.cs = NULL, +#endif +#elif defined(DT_ST_LPS22HH_BUS_I2C) + .bus_init = lps22hh_i2c_init, + .i2c_slv_addr = DT_INST_0_ST_LPS22HH_BASE_ADDRESS, +#else +#error "BUS MACRO NOT DEFINED IN DTS" +#endif +}; + +DEVICE_AND_API_INIT(lps22hh, DT_INST_0_ST_LPS22HH_LABEL, lps22hh_init, + &lps22hh_data, &lps22hh_config, POST_KERNEL, + CONFIG_SENSOR_INIT_PRIORITY, &lps22hh_api_funcs); diff --git a/drivers/sensor/lps22hh/lps22hh.h b/drivers/sensor/lps22hh/lps22hh.h new file mode 100644 index 00000000000..64a32d73854 --- /dev/null +++ b/drivers/sensor/lps22hh/lps22hh.h @@ -0,0 +1,88 @@ +/* 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 + */ + +#ifndef ZEPHYR_DRIVERS_SENSOR_LPS22HH_LPS22HH_H_ +#define ZEPHYR_DRIVERS_SENSOR_LPS22HH_LPS22HH_H_ + +#include +#include +#include +#include +#include +#include +#include +#include "lps22hh_reg.h" + +struct lps22hh_config { + char *master_dev_name; + int (*bus_init)(struct device *dev); +#ifdef CONFIG_LPS22HH_TRIGGER + const char *drdy_port; + u8_t drdy_pin; +#endif +#ifdef DT_ST_LPS22HH_BUS_I2C + u16_t i2c_slv_addr; +#elif DT_ST_LPS22HH_BUS_SPI + struct spi_config spi_conf; +#if defined(DT_INST_0_ST_LPS22HH_CS_GPIO_CONTROLLER) + const char *gpio_cs_port; + u8_t cs_gpio; +#endif +#endif +}; + +struct lps22hh_data { + struct device *bus; + s32_t sample_press; + s16_t sample_temp; + + lps22hh_ctx_t *ctx; + +#ifdef DT_ST_LPS22HH_BUS_I2C + lps22hh_ctx_t ctx_i2c; +#elif DT_ST_LPS22HH_BUS_SPI + lps22hh_ctx_t ctx_spi; +#endif + +#ifdef CONFIG_LPS22HH_TRIGGER + struct device *gpio; + u32_t pin; + struct gpio_callback gpio_cb; + + struct sensor_trigger data_ready_trigger; + sensor_trigger_handler_t handler_drdy; + +#if defined(CONFIG_LPS22HH_TRIGGER_OWN_THREAD) + K_THREAD_STACK_MEMBER(thread_stack, CONFIG_LPS22HH_THREAD_STACK_SIZE); + struct k_thread thread; + struct k_sem gpio_sem; +#elif defined(CONFIG_LPS22HH_TRIGGER_GLOBAL_THREAD) + struct k_work work; + struct device *dev; +#endif + +#endif /* CONFIG_LPS22HH_TRIGGER */ +#if defined(DT_INST_0_ST_LPS22HH_CS_GPIO_CONTROLLER) + struct spi_cs_control cs_ctrl; +#endif +}; + +int lps22hh_i2c_init(struct device *dev); +int lps22hh_spi_init(struct device *dev); + +#ifdef CONFIG_LPS22HH_TRIGGER +int lps22hh_trigger_set(struct device *dev, + const struct sensor_trigger *trig, + sensor_trigger_handler_t handler); + +int lps22hh_init_interrupt(struct device *dev); +#endif + +#endif /* ZEPHYR_DRIVERS_SENSOR_LPS22HH_LPS22HH_H_ */ diff --git a/drivers/sensor/lps22hh/lps22hh_i2c.c b/drivers/sensor/lps22hh/lps22hh_i2c.c new file mode 100644 index 00000000000..08ff3ffcff3 --- /dev/null +++ b/drivers/sensor/lps22hh/lps22hh_i2c.c @@ -0,0 +1,54 @@ +/* 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 + */ + +#include +#include +#include + +#include "lps22hh.h" + +#ifdef DT_ST_LPS22HH_BUS_I2C + +#define LOG_LEVEL CONFIG_SENSOR_LOG_LEVEL +LOG_MODULE_DECLARE(LPS22HH); + +static int lps22hh_i2c_read(struct device *dev, u8_t reg_addr, + u8_t *value, u16_t len) +{ + struct lps22hh_data *data = dev->driver_data; + const struct lps22hh_config *cfg = dev->config->config_info; + + return i2c_burst_read(data->bus, cfg->i2c_slv_addr, + reg_addr, value, len); +} + +static int lps22hh_i2c_write(struct device *dev, u8_t reg_addr, + u8_t *value, u16_t len) +{ + struct lps22hh_data *data = dev->driver_data; + const struct lps22hh_config *cfg = dev->config->config_info; + + return i2c_burst_write(data->bus, cfg->i2c_slv_addr, + reg_addr, value, len); +} + +int lps22hh_i2c_init(struct device *dev) +{ + struct lps22hh_data *data = dev->driver_data; + + data->ctx_i2c.read_reg = (lps22hh_read_ptr) lps22hh_i2c_read; + data->ctx_i2c.write_reg = (lps22hh_write_ptr) lps22hh_i2c_write; + + data->ctx = &data->ctx_i2c; + data->ctx->handle = dev; + + return 0; +} +#endif /* DT_ST_LPS22HH_BUS_I2C */ diff --git a/drivers/sensor/lps22hh/lps22hh_spi.c b/drivers/sensor/lps22hh/lps22hh_spi.c new file mode 100644 index 00000000000..44e19192b80 --- /dev/null +++ b/drivers/sensor/lps22hh/lps22hh_spi.c @@ -0,0 +1,128 @@ +/* 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 + */ + + +#include +#include "lps22hh.h" +#include + +#ifdef DT_ST_LPS22HH_BUS_SPI + +#define LPS22HH_SPI_READ (1 << 7) + +#define LOG_LEVEL CONFIG_SENSOR_LOG_LEVEL +LOG_MODULE_DECLARE(LPS22HH); + +static int lps22hh_spi_read(struct device *dev, u8_t reg_addr, + u8_t *value, u8_t len) +{ + struct lps22hh_data *data = dev->driver_data; + const struct lps22hh_config *cfg = dev->config->config_info; + const struct spi_config *spi_cfg = &cfg->spi_conf; + u8_t buffer_tx[2] = { reg_addr | LPS22HH_SPI_READ, 0 }; + const struct spi_buf tx_buf = { + .buf = buffer_tx, + .len = 2, + }; + const struct spi_buf_set tx = { + .buffers = &tx_buf, + .count = 1 + }; + const struct spi_buf rx_buf[2] = { + { + .buf = NULL, + .len = 1, + }, + { + .buf = value, + .len = len, + } + }; + const struct spi_buf_set rx = { + .buffers = rx_buf, + .count = 2 + }; + + + if (len > 64) { + return -EIO; + } + + if (spi_transceive(data->bus, spi_cfg, &tx, &rx)) { + return -EIO; + } + + return 0; +} + +static int lps22hh_spi_write(struct device *dev, u8_t reg_addr, + u8_t *value, u8_t len) +{ + struct lps22hh_data *data = dev->driver_data; + const struct lps22hh_config *cfg = dev->config->config_info; + const struct spi_config *spi_cfg = &cfg->spi_conf; + u8_t buffer_tx[1] = { reg_addr & ~LPS22HH_SPI_READ }; + const struct spi_buf tx_buf[2] = { + { + .buf = buffer_tx, + .len = 1, + }, + { + .buf = value, + .len = len, + } + }; + const struct spi_buf_set tx = { + .buffers = tx_buf, + .count = 2 + }; + + + if (len > 64) { + return -EIO; + } + + if (spi_write(data->bus, spi_cfg, &tx)) { + return -EIO; + } + + return 0; +} + +int lps22hh_spi_init(struct device *dev) +{ + struct lps22hh_data *data = dev->driver_data; + + data->ctx_spi.read_reg = (lps22hh_read_ptr) lps22hh_spi_read; + data->ctx_spi.write_reg = (lps22hh_write_ptr) lps22hh_spi_write; + + data->ctx = &data->ctx_spi; + data->ctx->handle = dev; + +#if defined(DT_INST_0_ST_LPS22HH_CS_GPIO_CONTROLLER) + const struct lps22hh_config *cfg = dev->config->config_info; + + /* handle SPI CS thru GPIO if it is the case */ + data->cs_ctrl.gpio_dev = device_get_binding(cfg->gpio_cs_port); + if (!data->cs_ctrl.gpio_dev) { + LOG_ERR("Unable to get GPIO SPI CS device"); + return -ENODEV; + } + + data->cs_ctrl.gpio_pin = cfg->cs_gpio; + data->cs_ctrl.delay = 0; + + LOG_DBG("SPI GPIO CS configured on %s:%u", + cfg->gpio_cs_port, cfg->cs_gpio); +#endif + + return 0; +} +#endif /* DT_ST_LPS22HH_BUS_SPI */ diff --git a/drivers/sensor/lps22hh/lps22hh_trigger.c b/drivers/sensor/lps22hh/lps22hh_trigger.c new file mode 100644 index 00000000000..21eeae24746 --- /dev/null +++ b/drivers/sensor/lps22hh/lps22hh_trigger.c @@ -0,0 +1,182 @@ +/* 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 + */ + +#include +#include +#include +#include + +#include "lps22hh.h" + +#define LOG_LEVEL CONFIG_SENSOR_LOG_LEVEL +LOG_MODULE_DECLARE(LPS22HH); + +/** + * lps22hh_enable_int - enable selected int pin to generate interrupt + */ +static int lps22hh_enable_int(struct device *dev, int enable) +{ + struct lps22hh_data *lps22hh = dev->driver_data; + lps22hh_reg_t int_route; + + /* set interrupt */ + lps22hh_pin_int_route_get(lps22hh->ctx, + &int_route.ctrl_reg3); + int_route.ctrl_reg3.drdy = enable; + return lps22hh_pin_int_route_set(lps22hh->ctx, + &int_route.ctrl_reg3); +} + +/** + * lps22hh_trigger_set - link external trigger to event data ready + */ +int lps22hh_trigger_set(struct device *dev, + const struct sensor_trigger *trig, + sensor_trigger_handler_t handler) +{ + struct lps22hh_data *lps22hh = dev->driver_data; + axis1bit32_t raw_press; + + if (trig->chan == SENSOR_CHAN_ALL) { + lps22hh->handler_drdy = handler; + if (handler) { + /* dummy read: re-trigger interrupt */ + if (lps22hh_pressure_raw_get(lps22hh->ctx, + raw_press.u8bit) < 0) { + LOG_DBG("Failed to read sample"); + return -EIO; + } + return lps22hh_enable_int(dev, 1); + } else { + return lps22hh_enable_int(dev, 0); + } + } + + return -ENOTSUP; +} + +/** + * lps22hh_handle_interrupt - handle the drdy event + * read data and call handler if registered any + */ +static void lps22hh_handle_interrupt(void *arg) +{ + struct device *dev = arg; + struct lps22hh_data *lps22hh = dev->driver_data; + const struct lps22hh_config *cfg = dev->config->config_info; + struct sensor_trigger drdy_trigger = { + .type = SENSOR_TRIG_DATA_READY, + }; + + if (lps22hh->handler_drdy != NULL) { + lps22hh->handler_drdy(dev, &drdy_trigger); + } + + gpio_pin_enable_callback(lps22hh->gpio, cfg->drdy_pin); +} + +static void lps22hh_gpio_callback(struct device *dev, + struct gpio_callback *cb, u32_t pins) +{ + const struct lps22hh_config *cfg = dev->config->config_info; + struct lps22hh_data *lps22hh = + CONTAINER_OF(cb, struct lps22hh_data, gpio_cb); + + ARG_UNUSED(pins); + + gpio_pin_disable_callback(dev, cfg->drdy_pin); + +#if defined(CONFIG_LPS22HH_TRIGGER_OWN_THREAD) + k_sem_give(&lps22hh->gpio_sem); +#elif defined(CONFIG_LPS22HH_TRIGGER_GLOBAL_THREAD) + k_work_submit(&lps22hh->work); +#endif /* CONFIG_LPS22HH_TRIGGER_OWN_THREAD */ +} + +#ifdef CONFIG_LPS22HH_TRIGGER_OWN_THREAD +static void lps22hh_thread(int dev_ptr, int unused) +{ + struct device *dev = INT_TO_POINTER(dev_ptr); + struct lps22hh_data *lps22hh = dev->driver_data; + + ARG_UNUSED(unused); + + while (1) { + k_sem_take(&lps22hh->gpio_sem, K_FOREVER); + lps22hh_handle_interrupt(dev); + } +} +#endif /* CONFIG_LPS22HH_TRIGGER_OWN_THREAD */ + +#ifdef CONFIG_LPS22HH_TRIGGER_GLOBAL_THREAD +static void lps22hh_work_cb(struct k_work *work) +{ + struct lps22hh_data *lps22hh = + CONTAINER_OF(work, struct lps22hh_data, work); + + lps22hh_handle_interrupt(lps22hh->dev); +} +#endif /* CONFIG_LPS22HH_TRIGGER_GLOBAL_THREAD */ + +int lps22hh_init_interrupt(struct device *dev) +{ + struct lps22hh_data *lps22hh = dev->driver_data; + const struct lps22hh_config *cfg = dev->config->config_info; + int ret; + + /* setup data ready gpio interrupt */ + lps22hh->gpio = device_get_binding(cfg->drdy_port); + if (lps22hh->gpio == NULL) { + LOG_DBG("Cannot get pointer to %s device", cfg->drdy_port); + return -EINVAL; + } + +#if defined(CONFIG_LPS22HH_TRIGGER_OWN_THREAD) + k_sem_init(&lps22hh->gpio_sem, 0, UINT_MAX); + + k_thread_create(&lps22hh->thread, lps22hh->thread_stack, + CONFIG_LPS22HH_THREAD_STACK_SIZE, + (k_thread_entry_t)lps22hh_thread, dev, + 0, NULL, K_PRIO_COOP(CONFIG_LPS22HH_THREAD_PRIORITY), + 0, 0); +#elif defined(CONFIG_LPS22HH_TRIGGER_GLOBAL_THREAD) + lps22hh->work.handler = lps22hh_work_cb; + lps22hh->dev = dev; +#endif /* CONFIG_LPS22HH_TRIGGER_OWN_THREAD */ + + ret = gpio_pin_configure(lps22hh->gpio, cfg->drdy_pin, + GPIO_DIR_IN | GPIO_INT | GPIO_INT_EDGE | + GPIO_INT_ACTIVE_HIGH | GPIO_INT_DEBOUNCE); + if (ret < 0) { + LOG_DBG("Could not configure gpio"); + return ret; + } + + gpio_init_callback(&lps22hh->gpio_cb, lps22hh_gpio_callback, + BIT(cfg->drdy_pin)); + + if (gpio_add_callback(lps22hh->gpio, &lps22hh->gpio_cb) < 0) { + LOG_DBG("Could not set gpio callback"); + return -EIO; + } + + /* configure interrupt active high */ + if (lps22hh_pin_polarity_set(lps22hh->ctx, LPS22HH_ACTIVE_HIGH) < 0) { + return -EIO; + } + + /* enable interrupt in pulse mode */ + if (lps22hh_int_notification_set(lps22hh->ctx, + LPS22HH_INT_PULSED) < 0) { + return -EIO; + } + + return gpio_pin_enable_callback(lps22hh->gpio, cfg->drdy_pin); +} diff --git a/dts/bindings/sensor/st,lps22hh-i2c.yaml b/dts/bindings/sensor/st,lps22hh-i2c.yaml new file mode 100644 index 00000000000..338cdcb4d5b --- /dev/null +++ b/dts/bindings/sensor/st,lps22hh-i2c.yaml @@ -0,0 +1,25 @@ +# +# Copyright (c) 2019 STMicroelectronics +# +# SPDX-License-Identifier: Apache-2.0 +# + +title: STMicroelectronics MEMS sensors LPS22HH +version: 0.1 + +description: > + This binding gives a base representation of LPS22HH pressure and + temperature sensor connected to I2C bus + +inherits: + !include i2c-device.yaml + +properties: + compatible: + constraint: "st,lps22hh" + + drdy-gpios: + type: compound + category: optional + description: DRDY pin + generation: define diff --git a/dts/bindings/sensor/st,lps22hh-spi.yaml b/dts/bindings/sensor/st,lps22hh-spi.yaml new file mode 100644 index 00000000000..f09ab81be64 --- /dev/null +++ b/dts/bindings/sensor/st,lps22hh-spi.yaml @@ -0,0 +1,25 @@ +# +# Copyright (c) 2019 STMicroelectronics +# +# SPDX-License-Identifier: Apache-2.0 +# + +title: STMicroelectronics MEMS sensors LPS22HH +version: 0.1 + +description: > + This binding gives a base representation of LPS22HH pressure and + temperature sensor connected to SPI bus + +inherits: + !include spi-device.yaml + +properties: + compatible: + constraint: "st,lps22hh" + + drdy-gpios: + type: compound + category: optional + description: DRDY pin + generation: define diff --git a/tests/drivers/build_all/dts_fixup.h b/tests/drivers/build_all/dts_fixup.h index e82ad1c0aec..e4c320eb9a8 100644 --- a/tests/drivers/build_all/dts_fixup.h +++ b/tests/drivers/build_all/dts_fixup.h @@ -183,6 +183,15 @@ #define DT_ST_LIS2DW12_BUS_I2C 1 #endif +#ifndef DT_INST_0_ST_LPS22HH_LABEL +#define DT_INST_0_ST_LPS22HH_LABEL "" +#define DT_INST_0_ST_LPS22HH_BUS_NAME "" +#define DT_INST_0_ST_LPS22HH_BASE_ADDRESS 0x19 +#define DT_INST_0_ST_LPS22HH_DRDY_GPIOS_CONTROLLER "" +#define DT_INST_0_ST_LPS22HH_DRDY_GPIOS_PIN 0 +#define DT_ST_LPS22HH_BUS_I2C 1 +#endif + #ifndef DT_INST_0_ST_LSM9DS0_MFD_LABEL #define DT_INST_0_ST_LSM9DS0_MFD_LABEL "" #define DT_INST_0_ST_LSM9DS0_MFD_BUS_NAME "" @@ -276,6 +285,16 @@ #define DT_ST_LIS2DW12_BUS_SPI 1 #endif +#ifndef DT_INST_0_ST_LPS22HH_LABEL +#define DT_INST_0_ST_LPS22HH_LABEL "" +#define DT_INST_0_ST_LPS22HH_BUS_NAME "" +#define DT_INST_0_ST_LPS22HH_SPI_MAX_FREQUENCY 100000 +#define DT_INST_0_ST_LPS22HH_BASE_ADDRESS 1 +#define DT_INST_0_ST_LPS22HH_DRDY_GPIOS_CONTROLLER "" +#define DT_INST_0_ST_LPS22HH_DRDY_GPIOS_PIN 0 +#define DT_ST_LPS22HH_BUS_SPI 1 +#endif + #ifndef DT_INST_0_MICROCHIP_ENC28J60_LABEL #define DT_INST_0_MICROCHIP_ENC28J60_BASE_ADDRESS 0 #define DT_INST_0_MICROCHIP_ENC28J60_BUS_NAME ""