diff --git a/drivers/sensor/CMakeLists.txt b/drivers/sensor/CMakeLists.txt index 20893716ec9..fbb6f287333 100644 --- a/drivers/sensor/CMakeLists.txt +++ b/drivers/sensor/CMakeLists.txt @@ -118,6 +118,7 @@ add_subdirectory_ifdef(CONFIG_TACH_XEC mchp_tach_xec) add_subdirectory_ifdef(CONFIG_WSEN_HIDS wsen_hids) add_subdirectory_ifdef(CONFIG_ITDS wsen_itds) add_subdirectory_ifdef(CONFIG_WSEN_PADS wsen_pads) +add_subdirectory_ifdef(CONFIG_WSEN_PDUS wsen_pdus) add_subdirectory_ifdef(CONFIG_WSEN_TIDS wsen_tids) add_subdirectory_ifdef(CONFIG_MCUX_ACMP mcux_acmp) add_subdirectory_ifdef(CONFIG_TACH_NPCX nuvoton_tach_npcx) diff --git a/drivers/sensor/Kconfig b/drivers/sensor/Kconfig index f3bbe499190..c51d6e5aae8 100644 --- a/drivers/sensor/Kconfig +++ b/drivers/sensor/Kconfig @@ -275,6 +275,8 @@ source "drivers/sensor/wsen_itds/Kconfig" source "drivers/sensor/wsen_pads/Kconfig" +source "drivers/sensor/wsen_pdus/Kconfig" + source "drivers/sensor/wsen_tids/Kconfig" source "drivers/sensor/mcux_acmp/Kconfig" diff --git a/drivers/sensor/wsen_pdus/CMakeLists.txt b/drivers/sensor/wsen_pdus/CMakeLists.txt new file mode 100644 index 00000000000..ab0d66fb23a --- /dev/null +++ b/drivers/sensor/wsen_pdus/CMakeLists.txt @@ -0,0 +1,6 @@ +# Copyright (c) 2023 Würth Elektronik eiSos GmbH & Co. KG +# SPDX-License-Identifier: Apache-2.0 + +zephyr_library() + +zephyr_library_sources(wsen_pdus.c) diff --git a/drivers/sensor/wsen_pdus/Kconfig b/drivers/sensor/wsen_pdus/Kconfig new file mode 100644 index 00000000000..a43cbe41144 --- /dev/null +++ b/drivers/sensor/wsen_pdus/Kconfig @@ -0,0 +1,11 @@ +# Copyright (c) 2023 Würth Elektronik eiSos GmbH & Co. KG +# SPDX-License-Identifier: Apache-2.0 + +config WSEN_PDUS + bool "WSEN-PDUS differential pressure sensor" + default y + depends on DT_HAS_WE_WSEN_PDUS_ENABLED + select I2C if $(dt_compat_on_bus,$(DT_COMPAT_WE_WSEN_PDUS),i2c) + select HAS_WESENSORS + help + Enable driver for the WSEN-PDUS I2C-based differential pressure sensor. diff --git a/drivers/sensor/wsen_pdus/wsen_pdus.c b/drivers/sensor/wsen_pdus/wsen_pdus.c new file mode 100644 index 00000000000..70e319602d0 --- /dev/null +++ b/drivers/sensor/wsen_pdus/wsen_pdus.c @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2023 Würth Elektronik eiSos GmbH & Co. KG + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#define DT_DRV_COMPAT we_wsen_pdus + +#include + +#include +#include +#include + +#include "wsen_pdus.h" + +LOG_MODULE_REGISTER(WSEN_PDUS, CONFIG_SENSOR_LOG_LEVEL); + +static int pdus_sample_fetch(const struct device *dev, enum sensor_channel chan) +{ + const struct pdus_config *const config = dev->config; + struct pdus_data *data = dev->data; + float pressure; + float temperature; + + __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL); + + if (PDUS_getPressureAndTemperature_float(&data->sensor_interface, + config->sensor_type, &pressure, + &temperature) != WE_SUCCESS) { + LOG_ERR("Failed to fetch data sample"); + return -EIO; + } + + data->pressure_k_pa = pressure; + data->temperature_deg_c = temperature; + + return 0; +} + +static int pdus_channel_get(const struct device *dev, enum sensor_channel chan, + struct sensor_value *value) +{ + struct pdus_data *data = dev->data; + + if (chan == SENSOR_CHAN_PRESS) { + value->val1 = (int32_t)data->pressure_k_pa; + value->val2 = (((int32_t)(data->pressure_k_pa * 1000)) % 1000) * 1000; + } else if (chan == SENSOR_CHAN_AMBIENT_TEMP) { + value->val1 = (int32_t)data->temperature_deg_c; + value->val2 = + (((int32_t)(data->temperature_deg_c * 1000)) % 1000) * 1000; + } else { + return -ENOTSUP; + } + + return 0; +} + +static const struct sensor_driver_api pdus_driver_api = { .sample_fetch = pdus_sample_fetch, + .channel_get = pdus_channel_get }; + +static int pdus_init(const struct device *dev) +{ + const struct pdus_config *const config = dev->config; + struct pdus_data *data = dev->data; + + /* Initialize WE sensor interface */ + PDUS_getDefaultInterface(&data->sensor_interface); + data->sensor_interface.interfaceType = WE_i2c; + + switch (data->sensor_interface.interfaceType) { +#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) + case WE_i2c: + data->sensor_interface.handle = (void *)&config->bus_cfg.i2c; + break; +#endif + default: + LOG_ERR("Invalid interface type"); + return -EINVAL; + } + + return 0; +} + +#if DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) == 0 +#warning "PDUS driver enabled without any devices" +#endif + +/* + * Main instantiation macro. + */ +#define PDUS_DEFINE(inst) \ + static struct pdus_data pdus_data_##inst; \ + static const struct pdus_config pdus_config_##inst = \ + { \ + .bus_cfg = { \ + .i2c = I2C_DT_SPEC_INST_GET(inst), \ + }, \ + .sensor_type = (PDUS_SensorType_t) DT_INST_ENUM_IDX(inst, sensor_type) \ + }; \ + SENSOR_DEVICE_DT_INST_DEFINE(inst, \ + pdus_init, \ + NULL, \ + &pdus_data_##inst, \ + &pdus_config_##inst, \ + POST_KERNEL, \ + CONFIG_SENSOR_INIT_PRIORITY, \ + &pdus_driver_api); + +DT_INST_FOREACH_STATUS_OKAY(PDUS_DEFINE) diff --git a/drivers/sensor/wsen_pdus/wsen_pdus.h b/drivers/sensor/wsen_pdus/wsen_pdus.h new file mode 100644 index 00000000000..6fb9f1f0595 --- /dev/null +++ b/drivers/sensor/wsen_pdus/wsen_pdus.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 Würth Elektronik eiSos GmbH & Co. KG + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef ZEPHYR_DRIVERS_SENSOR_WSEN_PDUS_WSEN_PDUS_H_ +#define ZEPHYR_DRIVERS_SENSOR_WSEN_PDUS_WSEN_PDUS_H_ + +#include +#include + +#include + +#include "WSEN_PDUS_25131308XXX01.h" + +#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) +#include +#endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */ + +struct pdus_data { + /* WE sensor interface configuration */ + WE_sensorInterface_t sensor_interface; + + /* Last pressure sample */ + float pressure_k_pa; + + /* Last temperature sample */ + float temperature_deg_c; +}; + +struct pdus_config { + union { +#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) + const struct i2c_dt_spec i2c; +#endif + } bus_cfg; + + PDUS_SensorType_t sensor_type; +}; + +int pdus_i2c_init(const struct device *dev); + +#endif /* ZEPHYR_DRIVERS_SENSOR_WSEN_PDUS_WSEN_PDUS_H_ */ diff --git a/dts/bindings/sensor/we,wsen-pdus.yaml b/dts/bindings/sensor/we,wsen-pdus.yaml new file mode 100644 index 00000000000..526f1fa450f --- /dev/null +++ b/dts/bindings/sensor/we,wsen-pdus.yaml @@ -0,0 +1,21 @@ +# Copyright (c) 2023 Würth Elektronik eiSos GmbH & Co. KG +# SPDX-License-Identifier: Apache-2.0 + +description: | + Würth Elektronik WSEN-PDUS differential pressure sensor + +compatible: "we,wsen-pdus" + +include: [sensor-device.yaml, i2c-device.yaml] + +properties: + sensor-type: + type: int + required: true + enum: + - 0 # order code 2513130810001, range = -0.1 to +0.1 kPa + - 1 # order code 2513130810101, range = -1 to +1 kPa + - 2 # order code 2513130810201, range = -10 to +10 kPa + - 3 # order code 2513130810301, range = 0 to 100 kPa + - 4 # order code 2513130810401, range = -100 to +1000 kPa + description: PDUS sensor product variant (pressure measurement range). diff --git a/tests/drivers/build_all/sensor/i2c.dtsi b/tests/drivers/build_all/sensor/i2c.dtsi index b49e1d4e802..b97f73f4926 100644 --- a/tests/drivers/build_all/sensor/i2c.dtsi +++ b/tests/drivers/build_all/sensor/i2c.dtsi @@ -700,3 +700,9 @@ test_i2c_s11059: s11059@6b { reg = <0x6b>; integration-time = <546000>; }; + +test_i2c_wsen_pdus: wsen_pdus@6C { + compatible = "we,wsen-pdus"; + reg = <0x6C>; + sensor-type = <3>; +};