drivers: sensor: Added driver for the Würth Elektronik WSEN-PDUS sensor

Added driver for the Würth Elektronik WSEN-PDUS sensor

Signed-off-by: Matthias Hauser <Matthias.Hauser@we-online.de>
This commit is contained in:
Matthias Hauser 2023-04-18 08:44:50 +02:00 committed by Maureen Helm
parent d47120fad4
commit 73ed8ccb5f
8 changed files with 202 additions and 0 deletions

View file

@ -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)

View file

@ -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"

View file

@ -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)

View file

@ -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.

View file

@ -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 <stdlib.h>
#include <zephyr/sys/__assert.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/logging/log.h>
#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)

View file

@ -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 <zephyr/drivers/gpio.h>
#include <zephyr/drivers/sensor.h>
#include <weplatform.h>
#include "WSEN_PDUS_25131308XXX01.h"
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
#include <zephyr/drivers/i2c.h>
#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_ */

View file

@ -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).

View file

@ -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>;
};