Skip to content

Commit d0fcb0d

Browse files
authored
[driver] Add BMP581 pressure sensor driver and example
1 parent 03bfac6 commit d0fcb0d

File tree

8 files changed

+1424
-13
lines changed

8 files changed

+1424
-13
lines changed

README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -858,97 +858,98 @@ your specific needs.
858858
<td align="center"><a href="https://modm.io/reference/module/modm-driver-bmi088">BMI088</a></td>
859859
<td align="center"><a href="https://modm.io/reference/module/modm-driver-bmp085">BMP085</a></td>
860860
</tr><tr>
861+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-bmp581">BMP581</a></td>
861862
<td align="center"><a href="https://modm.io/reference/module/modm-driver-bno055">BNO055</a></td>
862863
<td align="center"><a href="https://modm.io/reference/module/modm-driver-cat24aa">CAT24AA</a></td>
863864
<td align="center"><a href="https://modm.io/reference/module/modm-driver-cycle_counter">CYCLE-COUNTER</a></td>
864865
<td align="center"><a href="https://modm.io/reference/module/modm-driver-drv832x_spi">DRV832X</a></td>
865866
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ds1302">DS1302</a></td>
866-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ds1631">DS1631</a></td>
867867
</tr><tr>
868+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ds1631">DS1631</a></td>
868869
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ds18b20">DS18B20</a></td>
869870
<td align="center"><a href="https://modm.io/reference/module/modm-driver-dw3110">DW3110</a></td>
870871
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ea_dog">EA-DOG</a></td>
871872
<td align="center"><a href="https://modm.io/reference/module/modm-driver-encoder_input">Encoder Input</a></td>
872873
<td align="center"><a href="https://modm.io/reference/module/modm-driver-encoder_input-bitbang">Encoder Input BitBang</a></td>
873-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-encoder_output-bitbang">Encoder Output BitBang</a></td>
874874
</tr><tr>
875+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-encoder_output-bitbang">Encoder Output BitBang</a></td>
875876
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ft245">FT245</a></td>
876877
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ft6x06">FT6x06</a></td>
877878
<td align="center"><a href="https://modm.io/reference/module/modm-driver-gpio_sampler">Gpio Sampler</a></td>
878879
<td align="center"><a href="https://modm.io/reference/module/modm-driver-hclax">HCLAx</a></td>
879880
<td align="center"><a href="https://modm.io/reference/module/modm-driver-hd44780">HD44780</a></td>
880-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-hmc58x">HMC58x</a></td>
881881
</tr><tr>
882+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-hmc58x">HMC58x</a></td>
882883
<td align="center"><a href="https://modm.io/reference/module/modm-driver-hmc6343">HMC6343</a></td>
883884
<td align="center"><a href="https://modm.io/reference/module/modm-driver-hx711">HX711</a></td>
884885
<td align="center"><a href="https://modm.io/reference/module/modm-driver-i2c-eeprom">I2C-EEPROM</a></td>
885886
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ili9341">ILI9341</a></td>
886887
<td align="center"><a href="https://modm.io/reference/module/modm-driver-is31fl3733">IS31FL3733</a></td>
887-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-itg3200">ITG3200</a></td>
888888
</tr><tr>
889+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-itg3200">ITG3200</a></td>
889890
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ixm42xxx">IXM42XXX</a></td>
890891
<td align="center"><a href="https://modm.io/reference/module/modm-driver-l3gd20">L3GD20</a></td>
891892
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lan8720a">LAN8720A</a></td>
892893
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lawicel">LAWICEL</a></td>
893894
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lis302dl">LIS302DL</a></td>
894-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lis3dsh">LIS3DSH</a></td>
895895
</tr><tr>
896+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lis3dsh">LIS3DSH</a></td>
896897
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lis3mdl">LIS3MDL</a></td>
897898
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lm75">LM75</a></td>
898899
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lp503x">LP503x</a></td>
899900
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lsm303a">LSM303A</a></td>
900901
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lsm6ds33">LSM6DS33</a></td>
901-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lsm6dso">LSM6DSO</a></td>
902902
</tr><tr>
903+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lsm6dso">LSM6DSO</a></td>
903904
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ltc2984">LTC2984</a></td>
904905
<td align="center"><a href="https://modm.io/reference/module/modm-driver-max31855">MAX31855</a></td>
905906
<td align="center"><a href="https://modm.io/reference/module/modm-driver-max31865">MAX31865</a></td>
906907
<td align="center"><a href="https://modm.io/reference/module/modm-driver-max6966">MAX6966</a></td>
907908
<td align="center"><a href="https://modm.io/reference/module/modm-driver-max7219">MAX7219</a></td>
908-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp23x17">MCP23x17</a></td>
909909
</tr><tr>
910+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp23x17">MCP23x17</a></td>
910911
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp2515">MCP2515</a></td>
911912
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp3008">MCP3008</a></td>
912913
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp7941x">MCP7941x</a></td>
913914
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp990x">MCP990X</a></td>
914915
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mmc5603">MMC5603</a></td>
915-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ms5611">MS5611</a></td>
916916
</tr><tr>
917+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ms5611">MS5611</a></td>
917918
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ms5837">MS5837</a></td>
918919
<td align="center"><a href="https://modm.io/reference/module/modm-driver-nokia5110">NOKIA5110</a></td>
919920
<td align="center"><a href="https://modm.io/reference/module/modm-driver-nrf24">NRF24</a></td>
920921
<td align="center"><a href="https://modm.io/reference/module/modm-driver-parallel_tft_display">TFT-DISPLAY</a></td>
921922
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pat9125el">PAT9125EL</a></td>
922-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca8574">PCA8574</a></td>
923923
</tr><tr>
924+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca8574">PCA8574</a></td>
924925
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca9535">PCA9535</a></td>
925926
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca9548a">PCA9548A</a></td>
926927
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca9685">PCA9685</a></td>
927928
<td align="center"><a href="https://modm.io/reference/module/modm-driver-qmc5883l">QMC5883L</a></td>
928929
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sh1106">SH1106</a></td>
929-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-siemens_s65">SIEMENS-S65</a></td>
930930
</tr><tr>
931+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-siemens_s65">SIEMENS-S65</a></td>
931932
<td align="center"><a href="https://modm.io/reference/module/modm-driver-siemens_s75">SIEMENS-S75</a></td>
932933
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sk6812">SK6812</a></td>
933934
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sk9822">SK9822</a></td>
934935
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ssd1306">SSD1306</a></td>
935936
<td align="center"><a href="https://modm.io/reference/module/modm-driver-st7586s">ST7586S</a></td>
936-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-st7789">ST7789</a></td>
937937
</tr><tr>
938+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-st7789">ST7789</a></td>
938939
<td align="center"><a href="https://modm.io/reference/module/modm-driver-stts22h">STTS22H</a></td>
939940
<td align="center"><a href="https://modm.io/reference/module/modm-driver-stusb4500">STUSB4500</a></td>
940941
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sx1276">SX1276</a></td>
941942
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sx128x">SX128X</a></td>
942943
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tcs3414">TCS3414</a></td>
943-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tcs3472">TCS3472</a></td>
944944
</tr><tr>
945+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tcs3472">TCS3472</a></td>
945946
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tlc594x">TLC594x</a></td>
946947
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp102">TMP102</a></td>
947948
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp12x">TMP12x</a></td>
948949
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp175">TMP175</a></td>
949950
<td align="center"><a href="https://modm.io/reference/module/modm-driver-touch2046">TOUCH2046</a></td>
950-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-vl53l0">VL53L0</a></td>
951951
</tr><tr>
952+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-vl53l0">VL53L0</a></td>
952953
<td align="center"><a href="https://modm.io/reference/module/modm-driver-vl6180">VL6180</a></td>
953954
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ws2812">WS2812</a></td>
954955
</tr>
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/*
2+
* Copyright (c) 2026, Joel Schulz-Andres
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#include <atomic>
13+
#include <modm/board.hpp>
14+
#include <modm/driver/pressure/bmp581.hpp>
15+
16+
using namespace Board;
17+
18+
// I2C2 Bus Configuration
19+
using I2c = I2cMaster2;
20+
using Scl = GpioB10;
21+
using Sda = GpioB11;
22+
23+
// Interrupt Pin
24+
using BaroInt = GpioA4;
25+
26+
// BMP581 Driver Setup
27+
using Transport = modm::Bmp581I2cTransport<I2c>;
28+
using Baro = modm::Bmp581<Transport>;
29+
30+
// BMP581 I2C address (0x46 with SDO to GND, 0x47 with SDO to VDDIO)
31+
constexpr uint8_t BaroAddress = 0x47;
32+
Baro baro{BaroAddress};
33+
std::atomic<bool> dataReady{false};
34+
35+
bool
36+
initializeBaro()
37+
{
38+
// Initialize the sensor
39+
if (!baro.initialize())
40+
{
41+
MODM_LOG_ERROR << "BMP581 initialization failed!\n";
42+
return false;
43+
}
44+
45+
// Configure oversampling: 4x for both pressure and temperature
46+
if (!baro.setOversampling(Baro::Osr::X4, Baro::Osr::X4, true))
47+
{
48+
MODM_LOG_ERROR << "Failed to set oversampling!\n";
49+
return false;
50+
}
51+
52+
// Configure output data rate: 50 Hz
53+
if (!baro.setOdr(Baro::Odr::Hz50))
54+
{
55+
MODM_LOG_ERROR << "Failed to set ODR!\n";
56+
return false;
57+
}
58+
59+
// Configure IIR filter: coefficient 3 for both
60+
if (!baro.setIirFilter(Baro::IirFilter::Coef3, Baro::IirFilter::Coef3))
61+
{
62+
MODM_LOG_ERROR << "Failed to set IIR filter!\n";
63+
return false;
64+
}
65+
66+
// Configure interrupt: active high, push-pull, pulsed mode
67+
const auto intConfig = Baro::IntConfig::Enable | Baro::IntConfig::Polarity; // Active high
68+
if (!baro.setIntConfig(intConfig))
69+
{
70+
MODM_LOG_ERROR << "Failed to set interrupt config!\n";
71+
return false;
72+
}
73+
74+
// Enable data ready interrupt
75+
if (!baro.setIntSource(Baro::IntSource::DataReadyEnable))
76+
{
77+
MODM_LOG_ERROR << "Failed to set interrupt source!\n";
78+
return false;
79+
}
80+
81+
/*
82+
* FIFO usage example:
83+
*
84+
* baro.setFifoSelect(Baro::FifoFrameSelect::PressureAndTemperature,
85+
* Baro::FifoDecimation::None);
86+
* baro.setFifoThreshold(8);
87+
* baro.setIntSource(Baro::IntSource::FifoThresholdEnable);
88+
* baro.setFifoEnabled(true);
89+
*
90+
* // On interrupt/main loop:
91+
* // const auto count = baro.getFifoCount();
92+
* // read up to `count` entries with readFifoFrames(...)
93+
*/
94+
95+
// Set power mode to normal (continuous measurement)
96+
if (!baro.setPowerMode(Baro::PowerMode::Normal))
97+
{
98+
MODM_LOG_ERROR << "Failed to set power mode!\n";
99+
return false;
100+
}
101+
102+
return true;
103+
}
104+
105+
int
106+
main()
107+
{
108+
Board::initialize();
109+
Leds::setOutput();
110+
111+
// Initialize I2C2
112+
I2c::connect<Scl::Scl, Sda::Sda>(I2c::PullUps::Internal);
113+
I2c::initialize<Board::SystemClock, 400_kHz>();
114+
115+
MODM_LOG_INFO << "BMP581 Barometric Pressure Sensor Example\n";
116+
MODM_LOG_INFO << "=========================================\n\n";
117+
// Configure interrupt pin
118+
BaroInt::setInput(BaroInt::InputType::PullDown);
119+
120+
// Initialize sensor with retries
121+
while (!initializeBaro())
122+
{
123+
LedRed::toggle();
124+
MODM_LOG_ERROR << "Retrying initialization...\n";
125+
modm::delay(250ms);
126+
}
127+
128+
MODM_LOG_INFO << "BMP581 initialized successfully!\n\n";
129+
130+
// Read chip ID for verification
131+
if (const auto chipId = baro.readChipId(); chipId)
132+
{
133+
MODM_LOG_INFO.printf("Chip ID: 0x%02X (expected 0x50)\n\n", *chipId);
134+
}
135+
136+
MODM_LOG_INFO << "Starting continuous measurement (DRDY EXTI)...\n\n";
137+
138+
Exti::connect<BaroInt>(Exti::Trigger::RisingEdge, [](auto) {
139+
dataReady = true;
140+
LedYellow::toggle();
141+
});
142+
143+
modm::bmp581::Data data;
144+
uint32_t sampleCount = 0;
145+
uint32_t readErrorCount = 0;
146+
while (true)
147+
{
148+
if (!dataReady) { continue; }
149+
dataReady = false;
150+
151+
if (baro.readData(data))
152+
{
153+
const float temperature = data.getTemperature();
154+
const float pressure = data.getPressure();
155+
const float pressureHpa = data.getPressureHpa();
156+
157+
// Calculate approximate altitude (simplified barometric formula)
158+
// Using standard sea level pressure of 1013.25 hPa
159+
constexpr float seaLevelPressure = 1013.25f;
160+
const float altitude =
161+
44330.0f * (1.0f - powf(pressureHpa / seaLevelPressure, 0.1903f));
162+
163+
sampleCount++;
164+
165+
// Print every 25th sample (~2 Hz at 50Hz sampling)
166+
if (sampleCount % 25 == 0)
167+
{
168+
MODM_LOG_INFO.printf("Sample #%lu\n", sampleCount);
169+
MODM_LOG_INFO.printf(" Temperature: %7.3f C\n", temperature);
170+
MODM_LOG_INFO.printf(" Pressure: %9.2f Pa (%7.2f hPa)\n", pressure,
171+
pressureHpa);
172+
MODM_LOG_INFO.printf(" Altitude: %7.1f m (approx)\n\n", altitude);
173+
}
174+
175+
LedGreen::toggle();
176+
readErrorCount = 0;
177+
} else
178+
{
179+
readErrorCount++;
180+
if (readErrorCount % 50 == 0) { MODM_LOG_ERROR << "Failed to read sensor data!\n"; }
181+
LedRed::set();
182+
}
183+
}
184+
185+
return 0;
186+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<library>
2+
<extends>modm:nucleo-h723zg</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../build/nucleo_h723zg/bmp581</option>
5+
</options>
6+
<modules>
7+
<module>modm:build:scons</module>
8+
<module>modm:platform:exti</module>
9+
<module>modm:platform:i2c:2</module>
10+
<module>modm:driver:bmp581</module>
11+
</modules>
12+
</library>

0 commit comments

Comments
 (0)