Skip to content

Commit 2d4fe16

Browse files
wm-eisosHardevsinh-Palaniya
authored andcommitted
drivers: sensor: wsen_pdms_25131308XXX05: add sensor driver
Add wsen_pdms_25131308XXX05 driver. Signed-off-by: Wajdi ELMuhtadi <wajdi.elmuhtadi@we-online.com>
1 parent 8a12ea7 commit 2d4fe16

File tree

10 files changed

+416
-0
lines changed

10 files changed

+416
-0
lines changed

drivers/sensor/wsen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ add_subdirectory_ifdef(CONFIG_WSEN_HIDS_2525020210002 wsen_hids_2525020210002)
77
add_subdirectory_ifdef(CONFIG_WSEN_ISDS_2536030320001 wsen_isds_2536030320001)
88
add_subdirectory_ifdef(CONFIG_WSEN_ITDS_2533020201601 wsen_itds_2533020201601)
99
add_subdirectory_ifdef(CONFIG_WSEN_PADS_2511020213301 wsen_pads_2511020213301)
10+
add_subdirectory_ifdef(CONFIG_WSEN_PDMS_25131308XXX05 wsen_pdms_25131308XXX05)
1011
add_subdirectory_ifdef(CONFIG_WSEN_PDUS_25131308XXXXX wsen_pdus_25131308XXXXX)
1112
add_subdirectory_ifdef(CONFIG_WSEN_TIDS_2521020222501 wsen_tids_2521020222501)
1213
# zephyr-keep-sorted-stop

drivers/sensor/wsen/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ source "drivers/sensor/wsen/wsen_hids_2525020210002/Kconfig"
77
source "drivers/sensor/wsen/wsen_isds_2536030320001/Kconfig"
88
source "drivers/sensor/wsen/wsen_itds_2533020201601/Kconfig"
99
source "drivers/sensor/wsen/wsen_pads_2511020213301/Kconfig"
10+
source "drivers/sensor/wsen/wsen_pdms_25131308XXX05/Kconfig"
1011
source "drivers/sensor/wsen/wsen_pdus_25131308XXXXX/Kconfig"
1112
source "drivers/sensor/wsen/wsen_tids_2521020222501/Kconfig"
1213
# zephyr-keep-sorted-stop
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright (c) 2025 Würth Elektronik eiSos GmbH & Co. KG
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
zephyr_library()
5+
6+
zephyr_library_sources(wsen_pdms_25131308XXX05.c)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright (c) 2025 Würth Elektronik eiSos GmbH & Co. KG
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
config WSEN_PDMS_25131308XXX05
5+
bool "WSEN-PDMS-25131308XXX05 differential pressure sensor"
6+
default y
7+
depends on DT_HAS_WE_WSEN_PDMS_25131308XXX05_ENABLED
8+
select I2C if $(dt_compat_on_bus,$(DT_COMPAT_WE_WSEN_PDMS_25131308XXX05),i2c)
9+
select SPI if $(dt_compat_on_bus,$(DT_COMPAT_WE_WSEN_PDMS_25131308XXX05),spi)
10+
select HAS_WESENSORS
11+
help
12+
Enable driver for the WSEN-PDMS-25131308XXX05 I2C/SPI-based differential pressure sensor.
Lines changed: 283 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,283 @@
1+
/*
2+
* Copyright (c) 2025 Würth Elektronik eiSos GmbH & Co. KG
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#define DT_DRV_COMPAT we_wsen_pdms_25131308xxx05
8+
9+
#include <zephyr/sys/__assert.h>
10+
#include <zephyr/sys/byteorder.h>
11+
#include <zephyr/logging/log.h>
12+
13+
#include <string.h>
14+
15+
#include "wsen_pdms_25131308XXX05.h"
16+
17+
LOG_MODULE_REGISTER(WSEN_PDMS_25131308XXX05, CONFIG_SENSOR_LOG_LEVEL);
18+
19+
static int pdms_25131308XXX05_sample_fetch(const struct device *dev, enum sensor_channel chan)
20+
{
21+
switch (chan) {
22+
case SENSOR_CHAN_ALL:
23+
case SENSOR_CHAN_AMBIENT_TEMP:
24+
case SENSOR_CHAN_PRESS: {
25+
break;
26+
}
27+
default:
28+
LOG_ERR("Invalid channel.");
29+
return -ENOTSUP;
30+
}
31+
32+
const struct pdms_25131308XXX05_config *const config = dev->config;
33+
struct pdms_25131308XXX05_data *data = dev->data;
34+
35+
uint16_t status;
36+
37+
switch (data->sensor_interface.interfaceType) {
38+
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
39+
case WE_i2c: {
40+
if (config->crc) {
41+
if (PDMS_I2C_GetRawPressureAndTemperature_WithCRC(
42+
&data->sensor_interface, &data->pressure_data,
43+
&data->temperature_data, &status) != WE_SUCCESS) {
44+
LOG_ERR("Failed to retrieve data from the sensor.");
45+
return -EIO;
46+
}
47+
} else {
48+
if (PDMS_I2C_GetRawPressureAndTemperature(
49+
&data->sensor_interface, &data->pressure_data,
50+
&data->temperature_data, &status) != WE_SUCCESS) {
51+
LOG_ERR("Failed to retrieve data from the sensor.");
52+
return -EIO;
53+
}
54+
}
55+
break;
56+
}
57+
#endif
58+
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
59+
case WE_spi: {
60+
if (config->crc) {
61+
if (PDMS_SPI_getRawPressureAndTemperature_WithCRC(
62+
&data->sensor_interface, &data->pressure_data,
63+
&data->temperature_data, &status) != WE_SUCCESS) {
64+
LOG_ERR("Failed to retrieve data from the sensor.");
65+
return -EIO;
66+
}
67+
} else {
68+
if (PDMS_SPI_GetRawPressureAndTemperature(
69+
&data->sensor_interface, &data->pressure_data,
70+
&data->temperature_data, &status) != WE_SUCCESS) {
71+
LOG_ERR("Failed to retrieve data from the sensor.");
72+
return -EIO;
73+
}
74+
}
75+
break;
76+
}
77+
#endif
78+
default:
79+
return -EIO;
80+
}
81+
82+
return 0;
83+
}
84+
85+
static int pdms_25131308XXX05_channel_get(const struct device *dev, enum sensor_channel chan,
86+
struct sensor_value *val)
87+
{
88+
const struct pdms_25131308XXX05_config *const config = dev->config;
89+
struct pdms_25131308XXX05_data *data = dev->data;
90+
91+
int status = 0;
92+
93+
switch (chan) {
94+
case SENSOR_CHAN_AMBIENT_TEMP: {
95+
int64_t temp = ((int64_t)(data->temperature_data - T_MIN_TYP_VAL_PDMS)) * 4272;
96+
97+
status = sensor_value_from_micro(val, temp);
98+
break;
99+
}
100+
case SENSOR_CHAN_PRESS: {
101+
int64_t pascal = (int64_t)(data->pressure_data - P_MIN_TYP_VAL_PDMS);
102+
103+
/*
104+
* these values are conversion factors based on the sensor type defined in the user
105+
* manual of the respective sensor
106+
*/
107+
switch (config->sensor_type) {
108+
case PDMS_pdms0:
109+
pascal = ((pascal * 763) / 10000) - 1000;
110+
break;
111+
case PDMS_pdms1:
112+
pascal = ((pascal * 763) / 1000) - 10000;
113+
break;
114+
case PDMS_pdms2:
115+
pascal = (((pascal * 2670) / 1000) - 35000);
116+
break;
117+
case PDMS_pdms3:
118+
pascal = ((pascal * 381) / 100);
119+
break;
120+
case PDMS_pdms4:
121+
pascal = ((pascal * 4190) / 100) - 100000;
122+
break;
123+
default:
124+
LOG_ERR("Sensor type doesn't exist");
125+
return -ENOTSUP;
126+
}
127+
128+
status = sensor_value_from_milli(val, pascal);
129+
break;
130+
}
131+
default:
132+
LOG_ERR("Invalid channel.");
133+
return -ENOTSUP;
134+
}
135+
136+
return status;
137+
}
138+
139+
static int pdms_25131308XXX05_init(const struct device *dev)
140+
{
141+
const struct pdms_25131308XXX05_config *const config = dev->config;
142+
struct pdms_25131308XXX05_data *data = dev->data;
143+
144+
/* Initialize WE sensor interface */
145+
WE_sensorInterfaceType_t interface_type = data->sensor_interface.interfaceType;
146+
147+
if (PDMS_getDefaultInterface(&data->sensor_interface) != WE_SUCCESS) {
148+
return -EIO;
149+
}
150+
151+
data->sensor_interface.interfaceType = interface_type;
152+
153+
switch (data->sensor_interface.interfaceType) {
154+
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
155+
case WE_i2c: {
156+
if (!i2c_is_ready_dt(&config->bus_cfg.i2c)) {
157+
LOG_ERR("I2C bus device not ready");
158+
return -ENODEV;
159+
}
160+
161+
switch (config->bus_cfg.i2c.addr) {
162+
case PDMS_I2C_ADDRESS_CRC: {
163+
if (!config->crc) {
164+
LOG_ERR("I2C with CRC disabled but the wrong I2C address is "
165+
"chosen.");
166+
return -ENODEV;
167+
}
168+
break;
169+
}
170+
case PDMS_I2C_ADDRESS: {
171+
if (config->crc) {
172+
LOG_ERR("I2C with CRC enabled but the wrong I2C address is "
173+
"chosen.");
174+
return -ENODEV;
175+
}
176+
break;
177+
}
178+
default:
179+
LOG_ERR("Invalid I2C address.");
180+
return -ENODEV;
181+
}
182+
183+
data->sensor_interface.options.i2c.address = config->bus_cfg.i2c.addr;
184+
data->sensor_interface.handle = (void *)&config->bus_cfg.i2c;
185+
break;
186+
}
187+
#endif
188+
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
189+
case WE_spi: {
190+
if (!spi_is_ready_dt(&config->bus_cfg.spi)) {
191+
LOG_ERR("SPI bus device not ready");
192+
return -ENODEV;
193+
}
194+
195+
data->spi_crc = (config->crc ? PDMS_SPI_withCRC : PDMS_SPI_withoutCRC);
196+
data->sensor_interface.options.spi.sensorSpecificSettings = (void *)&data->spi_crc;
197+
data->sensor_interface.options.spi.duplexMode = 1;
198+
data->sensor_interface.options.spi.burstMode = 1;
199+
data->sensor_interface.handle = (void *)&config->bus_cfg.spi;
200+
break;
201+
}
202+
#endif
203+
default:
204+
LOG_ERR("Invalid interface type");
205+
return -EINVAL;
206+
}
207+
208+
return 0;
209+
}
210+
211+
static DEVICE_API(sensor, pdms_25131308XXX05_driver_api) = {
212+
.sample_fetch = pdms_25131308XXX05_sample_fetch,
213+
.channel_get = pdms_25131308XXX05_channel_get,
214+
};
215+
216+
/* clang-format off */
217+
218+
#define PDMS_25131308XXX05_CONFIG_WE_INTERFACE(inst) \
219+
COND_CODE_1(DT_INST_ON_BUS(inst, i2c), \
220+
(.sensor_interface = { \
221+
.interfaceType = WE_i2c \
222+
}), \
223+
()) \
224+
COND_CODE_1(DT_INST_ON_BUS(inst, spi), \
225+
(.sensor_interface = { \
226+
.interfaceType = WE_spi \
227+
}), \
228+
())
229+
230+
#define PDMS_25131308XXX05_SPI_OPERATION \
231+
(SPI_WORD_SET(8) | SPI_OP_MODE_MASTER)
232+
233+
#define PDMS_25131308XXX05_CONFIG_BUS(inst) \
234+
COND_CODE_1(DT_INST_ON_BUS(inst, i2c), \
235+
(.bus_cfg = { \
236+
.i2c = I2C_DT_SPEC_INST_GET(inst) \
237+
},), \
238+
()) \
239+
COND_CODE_1(DT_INST_ON_BUS(inst, spi), \
240+
(.bus_cfg = { \
241+
.spi = SPI_DT_SPEC_INST_GET( \
242+
inst, \
243+
PDMS_25131308XXX05_SPI_OPERATION, \
244+
0 \
245+
) \
246+
},), \
247+
())
248+
249+
#define PDMS_25131308XXX05_CONFIG_SENSOR_TYPE(inst) \
250+
.sensor_type = (PDMS_SensorType_t)DT_INST_ENUM_IDX(inst, sensor_type),
251+
252+
#define PDMS_25131308XXX05_CONFIG_CRC(inst) \
253+
.crc = DT_INST_PROP(inst, crc),
254+
255+
#define PDMS_25131308XXX05_CONFIG(inst) \
256+
PDMS_25131308XXX05_CONFIG_BUS(inst) \
257+
PDMS_25131308XXX05_CONFIG_SENSOR_TYPE(inst) \
258+
PDMS_25131308XXX05_CONFIG_CRC(inst)
259+
260+
/*
261+
* Main instantiation macro.
262+
*/
263+
#define PDMS_25131308XXX05_DEFINE(inst) \
264+
static struct pdms_25131308XXX05_data pdms_25131308XXX05_data_##inst = { \
265+
PDMS_25131308XXX05_CONFIG_WE_INTERFACE(inst) \
266+
}; \
267+
static const struct pdms_25131308XXX05_config pdms_25131308XXX05_config_##inst = { \
268+
PDMS_25131308XXX05_CONFIG(inst) \
269+
}; \
270+
SENSOR_DEVICE_DT_INST_DEFINE( \
271+
inst, \
272+
pdms_25131308XXX05_init, \
273+
NULL, \
274+
&pdms_25131308XXX05_data_##inst, \
275+
&pdms_25131308XXX05_config_##inst, \
276+
POST_KERNEL, \
277+
CONFIG_SENSOR_INIT_PRIORITY, \
278+
&pdms_25131308XXX05_driver_api \
279+
)
280+
281+
DT_INST_FOREACH_STATUS_OKAY(PDMS_25131308XXX05_DEFINE)
282+
283+
/* clang-format on */
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2025 Würth Elektronik eiSos GmbH & Co. KG
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_DRIVERS_SENSOR_WSEN_PDMS_25131308XXX05_H_
8+
#define ZEPHYR_DRIVERS_SENSOR_WSEN_PDMS_25131308XXX05_H_
9+
10+
#include <stdbool.h>
11+
12+
#include <zephyr/drivers/sensor.h>
13+
14+
#include <platform.h>
15+
16+
#include <WSEN_PDMS_25131308XXX05.h>
17+
18+
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
19+
#include <zephyr/drivers/spi.h>
20+
#endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) */
21+
22+
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
23+
#include <zephyr/drivers/i2c.h>
24+
#endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */
25+
26+
struct pdms_25131308XXX05_data {
27+
/* WE sensor interface configuration */
28+
WE_sensorInterface_t sensor_interface;
29+
30+
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
31+
PDMS_Spi_CrcSelect_t spi_crc;
32+
#endif
33+
34+
uint16_t pressure_data;
35+
uint16_t temperature_data;
36+
};
37+
38+
struct pdms_25131308XXX05_config {
39+
union {
40+
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
41+
const struct i2c_dt_spec i2c;
42+
#endif
43+
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
44+
const struct spi_dt_spec spi;
45+
#endif
46+
} bus_cfg;
47+
48+
const PDMS_SensorType_t sensor_type;
49+
const bool crc;
50+
};
51+
52+
/* GCDS Sensor API functions*/
53+
static int pdms_25131308XXX05_sample_fetch(const struct device *dev, enum sensor_channel chan);
54+
static int pdms_25131308XXX05_channel_get(const struct device *dev, enum sensor_channel chan,
55+
struct sensor_value *val);
56+
57+
static int pdms_25131308XXX05_init(const struct device *dev);
58+
59+
#endif /* ZEPHYR_DRIVERS_SENSOR_WSEN_PDMS_25131308XXX05_H_ */

0 commit comments

Comments
 (0)