Skip to content

Commit 8bb572c

Browse files
ivanwagnernashif
authored andcommitted
drivers: sensor: meas: ms5837 supporting 02 and 30 variants via dt
Fixes #87870 This patch adds support via dt compatible property. Signed-off-by: Ivan Wagner <[email protected]>
1 parent 6eda124 commit 8bb572c

File tree

8 files changed

+63
-52
lines changed

8 files changed

+63
-52
lines changed

drivers/sensor/meas/ms5837/Kconfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# MS5837 pressure sensor configuration options
22

33
# Copyright (c) 2018 Jan Van Winkel <[email protected]>
4+
# Copyright (c) 2025 Ivan Wagner <[email protected]>
45
# SPDX-License-Identifier: Apache-2.0
56

67
config MS5837
78
bool "MS5837 pressure and temperature sensor"
89
default y
9-
depends on DT_HAS_MEAS_MS5837_ENABLED
10+
depends on DT_HAS_MEAS_MS5837_02BA_ENABLED || DT_HAS_MEAS_MS5837_30BA_ENABLED
1011
select I2C
1112
help
1213
Enable driver for MS5837 pressure and temperature sensor.

drivers/sensor/meas/ms5837/ms5837.c

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
/* Driver for MS5837 pressure sensor
22
*
33
* Copyright (c) 2018 Jan Van Winkel <[email protected]>
4+
* Copyright (c) 2025 Ivan Wagner <[email protected]>
45
*
56
* SPDX-License-Identifier: Apache-2.0
67
*/
78

8-
#define DT_DRV_COMPAT meas_ms5837
9-
109
#include <zephyr/init.h>
1110
#include <zephyr/kernel.h>
1211
#include <zephyr/sys/byteorder.h>
@@ -45,6 +44,8 @@ static int ms5837_get_measurement(const struct device *dev, uint32_t *val,
4544
return 0;
4645
}
4746

47+
#if DT_HAS_COMPAT_STATUS_OKAY(meas_ms5837_30ba)
48+
4849
static void ms5837_compensate_30(const struct device *dev,
4950
const int32_t adc_temperature,
5051
const int32_t adc_pressure)
@@ -94,10 +95,14 @@ static void ms5837_compensate_30(const struct device *dev,
9495
SENS -= SENSi;
9596

9697
data->temperature -= Ti;
97-
data->pressure =
98-
(((SENS * adc_pressure) / (1ll << 21)) - OFF) / (1ll << 13);
98+
/* Result is in mbar * 10 but store result as mbar * 100 same as 02 sensor variant */
99+
data->pressure = ((((SENS * adc_pressure) / (1ll << 21)) - OFF) / (1ll << 13)) * 10;
99100
}
100101

102+
#endif
103+
104+
#if DT_HAS_COMPAT_STATUS_OKAY(meas_ms5837_02ba)
105+
101106
/*
102107
* First and second order pressure and temperature calculations, as per the flowchart in the
103108
* MS5837-02B datasheet. (see "Pressure and Temperature Calculation", pages 6 and 7, REV a8 12/2019)
@@ -135,12 +140,16 @@ static void ms5837_compensate_02(const struct device *dev,
135140
SENS -= SENSi;
136141

137142
data->temperature -= Ti;
143+
/* Result is in mbar * 100 */
138144
data->pressure = (((SENS * adc_pressure) / (1ll << 21)) - OFF) / (1ll << 15);
139145
}
140146

147+
#endif
148+
141149
static int ms5837_sample_fetch(const struct device *dev,
142150
enum sensor_channel channel)
143151
{
152+
const struct ms5837_config *cfg = dev->config;
144153
struct ms5837_data *data = dev->data;
145154
int err;
146155
uint32_t adc_pressure;
@@ -161,7 +170,7 @@ static int ms5837_sample_fetch(const struct device *dev,
161170
return err;
162171
}
163172

164-
data->comp_func(dev, adc_temperature, adc_pressure);
173+
cfg->comp_func(dev, adc_temperature, adc_pressure);
165174

166175
return 0;
167176
}
@@ -348,34 +357,25 @@ static int ms5837_init(const struct device *dev)
348357
return err;
349358
}
350359

351-
const int type_id = (data->factory >> 5) & 0x7f;
352-
353-
switch (type_id) {
354-
case MS5837_02BA01:
355-
case MS5837_02BA21:
356-
data->comp_func = ms5837_compensate_02;
357-
break;
358-
case MS5837_30BA26:
359-
data->comp_func = ms5837_compensate_30;
360-
break;
361-
default:
362-
LOG_WRN(" unrecognized type: '%2x', defaulting to MS5837-30", type_id);
363-
data->comp_func = ms5837_compensate_30;
364-
break;
365-
}
366-
367360
return 0;
368361
}
369362

370-
#define MS5837_DEFINE(inst) \
371-
static struct ms5837_data ms5837_data_##inst; \
372-
\
373-
static const struct ms5837_config ms5837_config_##inst = { \
374-
.i2c = I2C_DT_SPEC_INST_GET(inst), \
375-
}; \
376-
\
377-
SENSOR_DEVICE_DT_INST_DEFINE(inst, ms5837_init, NULL, \
378-
&ms5837_data_##inst, &ms5837_config_##inst, POST_KERNEL, \
379-
CONFIG_SENSOR_INIT_PRIORITY, &ms5837_api_funcs); \
380-
381-
DT_INST_FOREACH_STATUS_OKAY(MS5837_DEFINE)
363+
#define MS5837_DEFINE(inst, type) \
364+
static struct ms5837_data ms5837_##type##_data_##inst; \
365+
\
366+
static const struct ms5837_config ms5837_##type##_config_##inst = { \
367+
.i2c = I2C_DT_SPEC_INST_GET(inst), \
368+
.comp_func = ms5837_compensate_##type \
369+
}; \
370+
\
371+
SENSOR_DEVICE_DT_INST_DEFINE(inst, ms5837_init, NULL, &ms5837_##type##_data_##inst, \
372+
&ms5837_##type##_config_##inst, POST_KERNEL, \
373+
CONFIG_SENSOR_INIT_PRIORITY, &ms5837_api_funcs);
374+
375+
#undef DT_DRV_COMPAT
376+
#define DT_DRV_COMPAT meas_ms5837_02ba
377+
DT_INST_FOREACH_STATUS_OKAY_VARGS(MS5837_DEFINE, 02)
378+
379+
#undef DT_DRV_COMPAT
380+
#define DT_DRV_COMPAT meas_ms5837_30ba
381+
DT_INST_FOREACH_STATUS_OKAY_VARGS(MS5837_DEFINE, 30)

drivers/sensor/meas/ms5837/ms5837.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@
4444
#define MS5837_ADC_READ_DELAY_4086 10
4545
#define MS5837_ADC_READ_DELAY_8129 20
4646

47-
enum ms5837_type {
48-
MS5837_02BA01 = 0x00,
49-
MS5837_02BA21 = 0x15,
50-
MS5837_30BA26 = 0x1A
51-
};
52-
5347
typedef void (*ms5837_compensate_func)(const struct device *dev,
5448
const int32_t adc_temperature,
5549
const int32_t adc_pressure);
@@ -76,11 +70,11 @@ struct ms5837_data {
7670
uint8_t presure_conv_delay;
7771
uint8_t temperature_conv_delay;
7872

79-
ms5837_compensate_func comp_func;
8073
};
8174

8275
struct ms5837_config {
8376
struct i2c_dt_spec i2c;
77+
ms5837_compensate_func comp_func;
8478
};
8579

8680
#endif /* __SENSOR_MS5837_H__ */
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) 2025, Ivan Wagner <[email protected]>
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: TE Connectivity MS5837-02BA digital pressure sensor
5+
6+
compatible: "meas,ms5837-02ba"
7+
8+
include: [sensor-device.yaml, i2c-device.yaml]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) 2025, Ivan Wagner <[email protected]>
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: TE Connectivity MS5837-30BA digital pressure sensor
5+
6+
compatible: "meas,ms5837-30ba"
7+
8+
include: [sensor-device.yaml, i2c-device.yaml]

dts/bindings/sensor/meas,ms5837.yaml

Lines changed: 0 additions & 8 deletions
This file was deleted.

samples/sensor/ms5837/boards/nrf52840dk_nrf52840.overlay

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
status = "okay";
99
clock-frequency = <I2C_BITRATE_STANDARD>;
1010
ms5837@76 {
11-
compatible = "meas,ms5837";
11+
compatible = "meas,ms5837-30ba";
1212
reg = <0x76>;
13+
status = "okay";
1314
};
1415
};

tests/drivers/build_all/sensor/i2c.dtsi

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,10 @@ test_i2c_ms5607: ms5607@17 {
175175
reg = <0x17>;
176176
};
177177

178-
test_i2c_ms5837: ms5837@18 {
179-
compatible = "meas,ms5837";
178+
test_i2c_ms5837_02ba: ms5837@18 {
179+
compatible = "meas,ms5837-02ba";
180180
reg = <0x18>;
181+
status = "okay";
181182
};
182183

183184
test_i2c_mcp9808: mcp9808@19 {
@@ -1067,3 +1068,9 @@ test_i2c_sht21@90 {
10671068
compatible = "sensirion,sht21";
10681069
reg = <0x90>;
10691070
};
1071+
1072+
test_i2c_ms5837_30ba: ms5837@ae {
1073+
compatible = "meas,ms5837-30ba";
1074+
reg = <0xae>;
1075+
status = "okay";
1076+
};

0 commit comments

Comments
 (0)