Skip to content

Commit bc7f0c9

Browse files
committed
bmp581: Add dts-properties to set default configuration
The existing driver requires setting multiple attributes in order to work basic fetch/get reads. Simplify this by allowing the user to set dts node properties based on the use-case. As a result, basic settings results in the driver being up and running from the start, one can just get sensor readings out of the box. These still can be overriden at run-time if need be. Signed-off-by: Luis Ubieda <luisf@croxel.com>
1 parent 322da1d commit bc7f0c9

File tree

5 files changed

+312
-84
lines changed

5 files changed

+312
-84
lines changed

drivers/sensor/bosch/bmp581/bmp581.c

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -254,15 +254,42 @@ static int get_osr_odr_press_config(struct bmp581_osr_odr_press_config *osr_odr_
254254
rslt = i2c_burst_read_dt(&conf->i2c, BMP5_REG_OSR_CONFIG, reg_data, 2);
255255

256256
if (rslt == BMP5_OK) {
257-
osr_odr_press_cfg->osr_t = BMP5_GET_BITS_POS_0(reg_data[0], BMP5_TEMP_OS);
258-
osr_odr_press_cfg->osr_p = BMP5_GET_BITSLICE(reg_data[0], BMP5_PRESS_OS);
257+
osr_odr_press_cfg->osr_t = BMP5_GET_BITS_POS_0(reg_data[0], BMP5_TEMP_OSR);
258+
osr_odr_press_cfg->osr_p = BMP5_GET_BITSLICE(reg_data[0], BMP5_PRESS_OSR);
259259
osr_odr_press_cfg->press_en = BMP5_GET_BITSLICE(reg_data[0], BMP5_PRESS_EN);
260-
osr_odr_press_cfg->odr = BMP5_GET_BITSLICE(reg_data[1], BMP5_ODR);
261260
}
262261

263262
return rslt;
264263
}
265264

265+
static int set_osr_odr_press_config(const struct bmp581_osr_odr_press_config *osr_odr_press_cfg,
266+
const struct device *dev)
267+
{
268+
const struct bmp581_config *cfg = (const struct bmp581_config *)dev->config;
269+
uint8_t reg_data[2] = {0};
270+
271+
reg_data[0] = BMP5_SET_BITSLICE(reg_data[0], BMP5_TEMP_OSR, osr_odr_press_cfg->osr_t);
272+
reg_data[0] = BMP5_SET_BITSLICE(reg_data[0], BMP5_PRESS_OSR, osr_odr_press_cfg->osr_p);
273+
reg_data[0] = BMP5_SET_BITSLICE(reg_data[0], BMP5_PRESS_EN, osr_odr_press_cfg->press_en);
274+
275+
reg_data[1] = BMP5_SET_BITSLICE(reg_data[1], BMP5_POWERMODE, osr_odr_press_cfg->power_mode);
276+
reg_data[1] = BMP5_SET_BITSLICE(reg_data[1], BMP5_ODR, osr_odr_press_cfg->odr);
277+
278+
return i2c_burst_write_dt(&cfg->i2c, BMP5_REG_OSR_CONFIG, reg_data, sizeof(reg_data));
279+
}
280+
281+
static int set_iir_filters_config(const struct bmp581_osr_odr_press_config *osr_odr_press_cfg,
282+
const struct device *dev)
283+
{
284+
const struct bmp581_config *cfg = (const struct bmp581_config *)dev->config;
285+
uint8_t reg_data = 0;
286+
287+
reg_data = BMP5_SET_BITSLICE(reg_data, BMP5_SET_IIR_TEMP, osr_odr_press_cfg->iir_t);
288+
reg_data = BMP5_SET_BITSLICE(reg_data, BMP5_SET_IIR_PRESS, osr_odr_press_cfg->iir_p);
289+
290+
return i2c_burst_write_dt(&cfg->i2c, BMP5_REG_DSP_IIR, &reg_data, 1);
291+
}
292+
266293
static int set_osr_config(const struct sensor_value *osr, enum sensor_channel chan,
267294
const struct device *dev)
268295
{
@@ -282,16 +309,16 @@ static int set_osr_config(const struct sensor_value *osr, enum sensor_channel ch
282309
if (ret == BMP5_OK) {
283310
switch (chan) {
284311
case SENSOR_CHAN_ALL:
285-
osr_val = BMP5_SET_BITS_POS_0(osr_val, BMP5_TEMP_OS, oversampling);
286-
osr_val = BMP5_SET_BITSLICE(osr_val, BMP5_PRESS_OS, oversampling);
312+
osr_val = BMP5_SET_BITS_POS_0(osr_val, BMP5_TEMP_OSR, oversampling);
313+
osr_val = BMP5_SET_BITSLICE(osr_val, BMP5_PRESS_OSR, oversampling);
287314
osr_val = BMP5_SET_BITSLICE(osr_val, BMP5_PRESS_EN, press_en);
288315
break;
289316
case SENSOR_CHAN_PRESS:
290-
osr_val = BMP5_SET_BITSLICE(osr_val, BMP5_PRESS_OS, oversampling);
317+
osr_val = BMP5_SET_BITSLICE(osr_val, BMP5_PRESS_OSR, oversampling);
291318
osr_val = BMP5_SET_BITSLICE(osr_val, BMP5_PRESS_EN, press_en);
292319
break;
293320
case SENSOR_CHAN_AMBIENT_TEMP:
294-
osr_val = BMP5_SET_BITS_POS_0(osr_val, BMP5_TEMP_OS, oversampling);
321+
osr_val = BMP5_SET_BITS_POS_0(osr_val, BMP5_TEMP_OSR, oversampling);
295322
break;
296323
default:
297324
ret = -ENOTSUP;
@@ -519,7 +546,6 @@ static int bmp581_init(const struct device *dev)
519546

520547
/* Reset the chip id. */
521548
drv->chip_id = 0;
522-
memset(&drv->osr_odr_press_config, 0, sizeof(drv->osr_odr_press_config));
523549
memset(&drv->last_sample, 0, sizeof(drv->last_sample));
524550

525551
soft_reset(dev);
@@ -544,6 +570,19 @@ static int bmp581_init(const struct device *dev)
544570
BMP5_CHIP_ID_PRIM, BMP5_CHIP_ID_SEC);
545571
return -EINVAL;
546572
}
573+
574+
ret = set_iir_filters_config(&drv->osr_odr_press_config, dev);
575+
if (ret != 0) {
576+
LOG_ERR("Failed to set initial IIR settings: %d", ret);
577+
return ret;
578+
}
579+
580+
ret = set_osr_odr_press_config(&drv->osr_odr_press_config, dev);
581+
if (ret != 0) {
582+
LOG_ERR("Failed to set initial ODR OSR settings: %d", ret);
583+
return ret;
584+
}
585+
547586
return ret;
548587
}
549588

@@ -559,7 +598,17 @@ static DEVICE_API(sensor, bmp581_driver_api) = {
559598
}
560599

561600
#define BMP581_INIT(i) \
562-
static struct bmp581_data bmp581_data_##i; \
601+
static struct bmp581_data bmp581_data_##i = { \
602+
.osr_odr_press_config = { \
603+
.press_en = 1, \
604+
.odr = DT_INST_PROP(i, odr), \
605+
.osr_t = DT_INST_PROP(i, temp_osr), \
606+
.osr_p = DT_INST_PROP(i, press_osr), \
607+
.iir_t = DT_INST_PROP(i, temp_iir), \
608+
.iir_p = DT_INST_PROP(i, press_iir), \
609+
.power_mode = DT_INST_PROP(i, power_mode), \
610+
}, \
611+
}; \
563612
BMP581_CONFIG(i); \
564613
\
565614
SENSOR_DEVICE_DT_INST_DEFINE(i, bmp581_init, NULL, &bmp581_data_##i, &bmp581_config_##i, \

drivers/sensor/bosch/bmp581/bmp581.h

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -175,17 +175,19 @@
175175
#define BMP5_ODR_POS 2
176176

177177
/* OSR configurations */
178-
#define BMP5_TEMP_OS_MSK 0x07
178+
#define BMP5_TEMP_OSR_MSK 0x07
179+
#define BMP5_TEMP_OSR_POS 0
179180

180-
#define BMP5_PRESS_OS_MSK 0x38
181-
#define BMP5_PRESS_OS_POS 3
181+
#define BMP5_PRESS_OSR_MSK 0x38
182+
#define BMP5_PRESS_OSR_POS 3
182183

183184
/* Pressure enable */
184185
#define BMP5_PRESS_EN_MSK 0x40
185186
#define BMP5_PRESS_EN_POS 6
186187

187188
/* IIR configurations */
188189
#define BMP5_SET_IIR_TEMP_MSK 0x07
190+
#define BMP5_SET_IIR_TEMP_POS 0
189191

190192
#define BMP5_SET_IIR_PRESS_MSK 0x38
191193
#define BMP5_SET_IIR_PRESS_POS 3
@@ -219,6 +221,7 @@
219221

220222
/* Powermode */
221223
#define BMP5_POWERMODE_MSK 0x03
224+
#define BMP5_POWERMODE_POS 0
222225

223226
#define BMP5_DEEP_DISABLE_MSK 0x80
224227
#define BMP5_DEEP_DISABLE_POS 7
@@ -266,27 +269,27 @@
266269
struct bmp581_osr_odr_press_config {
267270
/*! Temperature oversampling
268271
* Assignable macros :
269-
* - BMP5_OVERSAMPLING_1X
270-
* - BMP5_OVERSAMPLING_2X
271-
* - BMP5_OVERSAMPLING_4X
272-
* - BMP5_OVERSAMPLING_8X
273-
* - BMP5_OVERSAMPLING_16X
274-
* - BMP5_OVERSAMPLING_32X
275-
* - BMP5_OVERSAMPLING_64X
276-
* - BMP5_OVERSAMPLING_128X
272+
* - BMP581_DT_OVERSAMPLING_1X
273+
* - BMP581_DT_OVERSAMPLING_2X
274+
* - BMP581_DT_OVERSAMPLING_4X
275+
* - BMP581_DT_OVERSAMPLING_8X
276+
* - BMP581_DT_OVERSAMPLING_16X
277+
* - BMP581_DT_OVERSAMPLING_32X
278+
* - BMP581_DT_OVERSAMPLING_64X
279+
* - BMP581_DT_OVERSAMPLING_128X
277280
*/
278281
uint8_t osr_t;
279282

280283
/*! Pressure oversampling
281284
* Assignable macros :
282-
* - BMP5_OVERSAMPLING_1X
283-
* - BMP5_OVERSAMPLING_2X
284-
* - BMP5_OVERSAMPLING_4X
285-
* - BMP5_OVERSAMPLING_8X
286-
* - BMP5_OVERSAMPLING_16X
287-
* - BMP5_OVERSAMPLING_32X
288-
* - BMP5_OVERSAMPLING_64X
289-
* - BMP5_OVERSAMPLING_128X
285+
* - BMP581_DT_OVERSAMPLING_1X
286+
* - BMP581_DT_OVERSAMPLING_2X
287+
* - BMP581_DT_OVERSAMPLING_4X
288+
* - BMP581_DT_OVERSAMPLING_8X
289+
* - BMP581_DT_OVERSAMPLING_16X
290+
* - BMP581_DT_OVERSAMPLING_32X
291+
* - BMP581_DT_OVERSAMPLING_64X
292+
* - BMP581_DT_OVERSAMPLING_128X
290293
*/
291294
uint8_t osr_p;
292295

@@ -298,6 +301,10 @@ struct bmp581_osr_odr_press_config {
298301

299302
/*! Output Data Rate */
300303
uint8_t odr;
304+
305+
uint8_t iir_t;
306+
uint8_t iir_p;
307+
uint8_t power_mode;
301308
};
302309

303310
struct bmp581_sample {

dts/bindings/sensor/bosch,bmp581.yaml

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@ description: |
22
The BMP581 is a Barometric pressure sensor. See more info at:
33
https://www.bosch-sensortec.com/products/environmental-sensors/pressure-sensors/bmp581/
44
5+
When setting the sensor DTS properties, make sure to include
6+
bmp581.h and use the macros defined there.
7+
8+
Example:
9+
#include <zephyr/dt-bindings/sensor/bmp581.h>
10+
11+
bmp581@46 {
12+
...
13+
odr = <BMP581_DT_ODR_50_HZ>;
14+
press-osr = <BMP581_DT_OVERSAMPLING_8X>;
15+
temp-osr = <BMP581_DT_OVERSAMPLING_4X>;
16+
press-iir = <BMP581_DT_IIR_FILTER_COEFF_7>;
17+
temp-iir = <BMP581_DT_IIR_FILTER_COEFF_3>;
18+
power-mode = <BMP581_DT_MODE_NORMAL>;
19+
};
20+
521
compatible: "bosch,bmp581"
622

723
include: [sensor-device.yaml, i2c-device.yaml]
@@ -13,3 +29,120 @@ properties:
1329

1430
The interrupt pin of BMP581 is open-drain, active low. If connected directly to the MCU,
1531
the pin should be configured as pull-up, active low.
32+
33+
odr:
34+
type: int
35+
default: 0x1C # BMP581_DT_ODR_1_HZ
36+
description: |
37+
Output data rate. Please note this is only valid on BMP581_DT_MODE_NORMAL.
38+
Default is power-on reset.
39+
enum:
40+
- 0x00 # BMP581_DT_ODR_240_HZ
41+
- 0x01 # BMP581_DT_ODR_218_5_HZ
42+
- 0x02 # BMP581_DT_ODR_199_1_HZ
43+
- 0x03 # BMP581_DT_ODR_179_2_HZ
44+
- 0x04 # BMP581_DT_ODR_160_HZ
45+
- 0x05 # BMP581_DT_ODR_149_3_HZ
46+
- 0x06 # BMP581_DT_ODR_140_HZ
47+
- 0x07 # BMP581_DT_ODR_129_8_HZ
48+
- 0x08 # BMP581_DT_ODR_120_HZ
49+
- 0x09 # BMP581_DT_ODR_110_1_HZ
50+
- 0x0A # BMP581_DT_ODR_100_2_HZ
51+
- 0x0B # BMP581_DT_ODR_89_6_HZ
52+
- 0x0C # BMP581_DT_ODR_80_HZ
53+
- 0x0D # BMP581_DT_ODR_70_HZ
54+
- 0x0E # BMP581_DT_ODR_60_HZ
55+
- 0x0F # BMP581_DT_ODR_50_HZ
56+
- 0x10 # BMP581_DT_ODR_45_HZ
57+
- 0x11 # BMP581_DT_ODR_40_HZ
58+
- 0x12 # BMP581_DT_ODR_35_HZ
59+
- 0x13 # BMP581_DT_ODR_30_HZ
60+
- 0x14 # BMP581_DT_ODR_25_HZ
61+
- 0x15 # BMP581_DT_ODR_20_HZ
62+
- 0x16 # BMP581_DT_ODR_15_HZ
63+
- 0x17 # BMP581_DT_ODR_10_HZ
64+
- 0x18 # BMP581_DT_ODR_5_HZ
65+
- 0x19 # BMP581_DT_ODR_4_HZ
66+
- 0x1A # BMP581_DT_ODR_3_HZ
67+
- 0x1B # BMP581_DT_ODR_2_HZ
68+
- 0x1C # BMP581_DT_ODR_1_HZ
69+
- 0x1D # BMP581_DT_ODR_0_5_HZ
70+
- 0x1E # BMP581_DT_ODR_0_250_HZ
71+
- 0x1F # BMP581_DT_ODR_0_125_HZ
72+
73+
press-osr:
74+
type: int
75+
default: 0x00 # BMP581_DT_OVERSAMPLING_1X
76+
description: |
77+
Pressure oversampling rate.
78+
Default is power-on reset.
79+
enum:
80+
- 0x00 # BMP581_DT_OVERSAMPLING_1X
81+
- 0x01 # BMP581_DT_OVERSAMPLING_2X
82+
- 0x02 # BMP581_DT_OVERSAMPLING_4X
83+
- 0x03 # BMP581_DT_OVERSAMPLING_8X
84+
- 0x04 # BMP581_DT_OVERSAMPLING_16X
85+
- 0x05 # BMP581_DT_OVERSAMPLING_32X
86+
- 0x06 # BMP581_DT_OVERSAMPLING_64X
87+
- 0x07 # BMP581_DT_OVERSAMPLING_128X
88+
89+
temp-osr:
90+
type: int
91+
default: 0x00 # BMP581_DT_OVERSAMPLING_1X
92+
description: |
93+
Temperature oversampling rate.
94+
Default is power-on reset.
95+
enum:
96+
- 0x00 # BMP581_DT_OVERSAMPLING_1X
97+
- 0x01 # BMP581_DT_OVERSAMPLING_2X
98+
- 0x02 # BMP581_DT_OVERSAMPLING_4X
99+
- 0x03 # BMP581_DT_OVERSAMPLING_8X
100+
- 0x04 # BMP581_DT_OVERSAMPLING_16X
101+
- 0x05 # BMP581_DT_OVERSAMPLING_32X
102+
- 0x06 # BMP581_DT_OVERSAMPLING_64X
103+
- 0x07 # BMP581_DT_OVERSAMPLING_128X
104+
105+
power-mode:
106+
type: int
107+
default: 1 # BMP581_DT_MODE_NORMAL
108+
description: |
109+
Power mode.
110+
Default favors ease of use by simply setting ODR and OSR. Otherwise user
111+
needs to look into driver details, as the other modes require some quirks
112+
(e.g: Forced mode does not just work).
113+
enum:
114+
- 1 # BMP581_DT_MODE_NORMAL
115+
- 2 # BMP581_DT_MODE_FORCED
116+
- 3 # BMP581_DT_MODE_CONTINUOUS
117+
118+
press-iir:
119+
type: int
120+
default: 0x00 # BMP581_DT_IIR_FILTER_BYPASS
121+
description: |
122+
Pressure IIR filter coefficient.
123+
Default is power-on reset (bypass).
124+
enum:
125+
- 0x00 # BMP581_DT_IIR_FILTER_BYPASS
126+
- 0x01 # BMP581_DT_IIR_FILTER_COEFF_1
127+
- 0x02 # BMP581_DT_IIR_FILTER_COEFF_3
128+
- 0x03 # BMP581_DT_IIR_FILTER_COEFF_7
129+
- 0x04 # BMP581_DT_IIR_FILTER_COEFF_15
130+
- 0x05 # BMP581_DT_IIR_FILTER_COEFF_31
131+
- 0x06 # BMP581_DT_IIR_FILTER_COEFF_63
132+
- 0x07 # BMP581_DT_IIR_FILTER_COEFF_127
133+
134+
temp-iir:
135+
type: int
136+
default: 0x00 # BMP581_DT_IIR_FILTER_BYPASS
137+
description: |
138+
Temperature IIR filter coefficient.
139+
Default is power-on reset (bypass).
140+
enum:
141+
- 0x00 # BMP581_DT_IIR_FILTER_BYPASS
142+
- 0x01 # BMP581_DT_IIR_FILTER_COEFF_1
143+
- 0x02 # BMP581_DT_IIR_FILTER_COEFF_3
144+
- 0x03 # BMP581_DT_IIR_FILTER_COEFF_7
145+
- 0x04 # BMP581_DT_IIR_FILTER_COEFF_15
146+
- 0x05 # BMP581_DT_IIR_FILTER_COEFF_31
147+
- 0x06 # BMP581_DT_IIR_FILTER_COEFF_63
148+
- 0x07 # BMP581_DT_IIR_FILTER_COEFF_127

0 commit comments

Comments
 (0)