From 2e3f85f76b751a089843c16aebaafc349c44e578 Mon Sep 17 00:00:00 2001 From: ladyada Date: Sat, 9 Aug 2025 10:56:15 -0400 Subject: [PATCH] Add SPI example and interrupt configuration functionality MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add bmp5xx_spi_test example demonstrating SPI communication - Add configureInterrupt() method with polarity/drive/mode options - Add interrupt configuration enums to header file - Update I2C example to show interrupt configuration - Change default interrupt mode from pulsed to latched 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- examples/bmp5xx_spi_test/bmp5xx_spi_test.ino | 87 ++++++++++++++++++++ examples/bmp5xx_test/bmp5xx_test.ino | 8 ++ src/Adafruit_BMP5xx.cpp | 27 +++++- src/Adafruit_BMP5xx.h | 29 +++++++ 4 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 examples/bmp5xx_spi_test/bmp5xx_spi_test.ino diff --git a/examples/bmp5xx_spi_test/bmp5xx_spi_test.ino b/examples/bmp5xx_spi_test/bmp5xx_spi_test.ino new file mode 100644 index 0000000..ad98d69 --- /dev/null +++ b/examples/bmp5xx_spi_test/bmp5xx_spi_test.ino @@ -0,0 +1,87 @@ +/*! + * @file bmp5xx_spi_test.ino + * + * This is a test sketch for the BMP5xx pressure and temperature sensor using SPI. + * It demonstrates basic SPI communication and sensor readings. + * + * Adafruit invests time and resources providing this open source code, + * please support Adafruit and open-source hardware by purchasing + * products from Adafruit! + * + * Written by Limor "ladyada" Fried for Adafruit Industries. + * BSD license, all text above must be included in any redistribution + */ + +#include +#include +#include "Adafruit_BMP5xx.h" + +#define BMP5XX_CS_PIN 10 +#define SEALEVELPRESSURE_HPA (1013.25) + +Adafruit_BMP5xx bmp; // Create BMP5xx object + +void setup() { + Serial.begin(115200); + while (!Serial) delay(10); // Wait for Serial Monitor to open + + Serial.println(F("Adafruit BMP5xx SPI Test!")); + + // Try to initialize the sensor using SPI + // bmp.begin(CS_PIN, &SPI) - CS pin and SPI peripheral + if (!bmp.begin(BMP5XX_CS_PIN, &SPI)) { + Serial.println(F("Could not find a valid BMP5xx sensor, check wiring or " + "SPI connections!")); + while (1) delay(10); + } + + Serial.println(F("BMP5xx found via SPI!")); + Serial.print(F("Using CS pin: ")); + Serial.println(BMP5XX_CS_PIN); + Serial.println(); + + // Set up basic sensor configuration + Serial.println(F("=== Configuring Sensor ===")); + + Serial.println(F("Setting temperature oversampling to 2X...")); + bmp.setTemperatureOversampling(BMP5XX_OVERSAMPLING_2X); + + Serial.println(F("Setting pressure oversampling to 16X...")); + bmp.setPressureOversampling(BMP5XX_OVERSAMPLING_16X); + + Serial.println(F("Setting IIR filter to coefficient 3...")); + bmp.setIIRFilterCoeff(BMP5XX_IIR_FILTER_COEFF_3); + + Serial.println(F("Setting output data rate to 50 Hz...")); + bmp.setOutputDataRate(BMP5XX_ODR_50_HZ); + + Serial.println(F("Setting power mode to normal...")); + bmp.setPowerMode(BMP5XX_POWERMODE_NORMAL); + + Serial.println(); + Serial.println(F("=== Starting Continuous Readings ===")); + Serial.println(); +} + +void loop() { + if (!bmp.performReading()) { + Serial.println(F("Failed to perform reading")); + return; + } + + Serial.print(F("Temperature = ")); + Serial.print(bmp.temperature); + Serial.println(F(" °C")); + + Serial.print(F("Pressure = ")); + Serial.print(bmp.pressure); + Serial.println(F(" hPa")); + + Serial.print(F("Approx. Altitude = ")); + Serial.print(bmp.readAltitude(SEALEVELPRESSURE_HPA)); + Serial.println(F(" m")); + + Serial.println(F("---")); + + delay(1000); // Read every second +} \ No newline at end of file diff --git a/examples/bmp5xx_test/bmp5xx_test.ino b/examples/bmp5xx_test/bmp5xx_test.ino index d63af0f..6cdb066 100644 --- a/examples/bmp5xx_test/bmp5xx_test.ino +++ b/examples/bmp5xx_test/bmp5xx_test.ino @@ -106,6 +106,14 @@ void setup() { Serial.println(F("Enabling pressure measurement...")); bmp.enablePressure(true); + /* Interrupt Configuration: + * BMP5XX_INTERRUPT_PULSED / BMP5XX_INTERRUPT_LATCHED - Interrupt mode + * BMP5XX_INTERRUPT_ACTIVE_LOW / BMP5XX_INTERRUPT_ACTIVE_HIGH - Interrupt polarity + * BMP5XX_INTERRUPT_PUSH_PULL / BMP5XX_INTERRUPT_OPEN_DRAIN - Interrupt drive + */ + Serial.println(F("Configuring interrupt pin...")); + bmp.configureInterrupt(BMP5XX_INTERRUPT_LATCHED, BMP5XX_INTERRUPT_ACTIVE_HIGH, BMP5XX_INTERRUPT_PUSH_PULL, true); + Serial.println(); Serial.println(F("=== Current Sensor Configuration ===")); diff --git a/src/Adafruit_BMP5xx.cpp b/src/Adafruit_BMP5xx.cpp index febf3dc..5e4dc06 100644 --- a/src/Adafruit_BMP5xx.cpp +++ b/src/Adafruit_BMP5xx.cpp @@ -164,8 +164,8 @@ bool Adafruit_BMP5xx::_init(void) { return false; } - // Configure interrupt pin as push-pull, active high, pulsed mode - rslt = bmp5_configure_interrupt(BMP5_PULSED, BMP5_ACTIVE_HIGH, BMP5_INTR_PUSH_PULL, BMP5_INTR_ENABLE, &_bmp5_dev); + // Configure interrupt pin as push-pull, active high, latched mode + rslt = bmp5_configure_interrupt(BMP5_LATCHED, BMP5_ACTIVE_HIGH, BMP5_INTR_PUSH_PULL, BMP5_INTR_ENABLE, &_bmp5_dev); return rslt == BMP5_OK; } @@ -361,6 +361,29 @@ bool Adafruit_BMP5xx::dataReady(void) { return (int_status & BMP5_INT_ASSERTED_DRDY) != 0; } +/*! + * @brief Configure interrupt pin settings + * @param mode Interrupt mode (pulsed or latched) + * @param polarity Interrupt polarity (active high or low) + * @param drive Interrupt drive (push-pull or open-drain) + * @param enable Enable or disable interrupt pin + * @return True if configuration was successful, false otherwise + */ +bool Adafruit_BMP5xx::configureInterrupt(bmp5xx_interrupt_mode_t mode, + bmp5xx_interrupt_polarity_t polarity, + bmp5xx_interrupt_drive_t drive, + bool enable) { + enum bmp5_intr_en_dis int_enable = enable ? BMP5_INTR_ENABLE : BMP5_INTR_DISABLE; + + int8_t rslt = bmp5_configure_interrupt((enum bmp5_intr_mode)mode, + (enum bmp5_intr_polarity)polarity, + (enum bmp5_intr_drive)drive, + int_enable, + &_bmp5_dev); + + return rslt == BMP5_OK; +} + /**************************************************************************/ /*! @brief I2C read callback for Bosch API diff --git a/src/Adafruit_BMP5xx.h b/src/Adafruit_BMP5xx.h index 556b2af..aadef28 100644 --- a/src/Adafruit_BMP5xx.h +++ b/src/Adafruit_BMP5xx.h @@ -121,6 +121,30 @@ typedef enum { BMP5XX_POWERMODE_DEEP_STANDBY = BMP5_POWERMODE_DEEP_STANDBY, ///< Deep standby mode } bmp5xx_powermode_t; +/** + * @brief Interrupt polarity settings + */ +typedef enum { + BMP5XX_INTERRUPT_ACTIVE_LOW = BMP5_ACTIVE_LOW, ///< Interrupt active low + BMP5XX_INTERRUPT_ACTIVE_HIGH = BMP5_ACTIVE_HIGH ///< Interrupt active high +} bmp5xx_interrupt_polarity_t; + +/** + * @brief Interrupt drive settings + */ +typedef enum { + BMP5XX_INTERRUPT_PUSH_PULL = BMP5_INTR_PUSH_PULL, ///< Push-pull output + BMP5XX_INTERRUPT_OPEN_DRAIN = BMP5_INTR_OPEN_DRAIN ///< Open-drain output +} bmp5xx_interrupt_drive_t; + +/** + * @brief Interrupt mode settings + */ +typedef enum { + BMP5XX_INTERRUPT_PULSED = BMP5_PULSED, ///< Pulsed interrupt + BMP5XX_INTERRUPT_LATCHED = BMP5_LATCHED ///< Latched interrupt +} bmp5xx_interrupt_mode_t; + /** * @brief Adafruit Unified Sensor interface for temperature component of BMP5xx */ @@ -196,6 +220,11 @@ public: bool enablePressure(bool enable = true); bool dataReady(void); + + bool configureInterrupt(bmp5xx_interrupt_mode_t mode, + bmp5xx_interrupt_polarity_t polarity, + bmp5xx_interrupt_drive_t drive, + bool enable = true); /**! Temperature (Celsius) assigned after calling performReading() */ float temperature;