diff --git a/include/devices/bmp581.h b/include/devices/bmp581.h new file mode 100644 index 000000000..4ac7f02b7 --- /dev/null +++ b/include/devices/bmp581.h @@ -0,0 +1,41 @@ +#pragma once +#include OSW_TARGET_PLATFORM_HEADER +#if OSW_PLATFORM_HARDWARE_BMP581 == 1 + +#include +#include +#include +#include + +namespace OswDevices { +class BMP581 : public OswPressureProvider, public OswTemperatureProvider { + public: + BMP581() : OswPressureProvider(), OswTemperatureProvider() {}; + virtual ~BMP581() {}; + + virtual void setup() override; + virtual void update() override; + virtual void reset() override {}; + virtual void stop() override {}; + + virtual inline const char* getName() override { + return "BMP581"; + }; + + virtual float getPressure() override; + virtual unsigned char getPressureProviderPriority() override { + return 100; + }; // This is a specialized device! + + virtual float getTemperature() override; + virtual inline unsigned char getTemperatureProviderPriority() override { + return 20; + }; // This sensor is not sooo good... + private: + bmp5_dev bmp5 = {}; + bmp5_osr_odr_press_config osr_odr_press_cfg = {}; + float pressure = 0; + float temperature = 0; +}; +}; +#endif \ No newline at end of file diff --git a/include/hal/devices.h b/include/hal/devices.h index 3ccee31c5..4b27457a8 100644 --- a/include/hal/devices.h +++ b/include/hal/devices.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -21,6 +22,9 @@ class OswHal::Devices { #if OSW_PLATFORM_HARDWARE_BMI270 == 1 OswDevices::BMI270* bmi270; #endif +#if OSW_PLATFORM_HARDWARE_BMP581 == 1 + OswDevices::BMP581* bmp581; +#endif #if OSW_PLATFORM_HARDWARE_QMC5883L == 1 OswDevices::QMC5883L* qmc5883l; #endif diff --git a/include/platform/EMULATOR.h b/include/platform/EMULATOR.h index de12fa54b..75b35e1d2 100644 --- a/include/platform/EMULATOR.h +++ b/include/platform/EMULATOR.h @@ -10,6 +10,7 @@ //#define OSW_PLATFORM_HARDWARE_DS3231MZ 0 //#define OSW_PLATFORM_HARDWARE_BMA400 0 //#define OSW_PLATFORM_HARDWARE_BMI270 0 +//#define OSW_PLATFORM_HARDWARE_BMP581 0 //#define OSW_PLATFORM_HARDWARE_BME280 0 //#define OSW_PLATFORM_HARDWARE_QMC5883L 0 #define OSW_PLATFORM_HARDWARE_VIRTUAL 1 diff --git a/include/platform/FLOW3R_C3CAMP_2023.h b/include/platform/FLOW3R_C3CAMP_2023.h index 92ec72b57..1c5ad61c1 100644 --- a/include/platform/FLOW3R_C3CAMP_2023.h +++ b/include/platform/FLOW3R_C3CAMP_2023.h @@ -10,6 +10,7 @@ //#define OSW_PLATFORM_HARDWARE_DS3231MZ 0 //#define OSW_PLATFORM_HARDWARE_BMA400 0 #define OSW_PLATFORM_HARDWARE_BMI270 1 +//#define OSW_PLATFORM_HARDWARE_BMP581 0 //#define OSW_PLATFORM_HARDWARE_QMC5883L 0 //#define OSW_PLATFORM_HARDWARE_BME280 0 //#define OSW_PLATFORM_HARDWARE_VIRTUAL 0 diff --git a/include/platform/GPS_EDITION_V3_1.h b/include/platform/GPS_EDITION_V3_1.h index 3523bf63b..51dfd1e91 100644 --- a/include/platform/GPS_EDITION_V3_1.h +++ b/include/platform/GPS_EDITION_V3_1.h @@ -13,6 +13,7 @@ #define OSW_PLATFORM_HARDWARE_DS3231MZ 1 #define OSW_PLATFORM_HARDWARE_BMA400 1 //#define OSW_PLATFORM_HARDWARE_BMI270 0 +//#define OSW_PLATFORM_HARDWARE_BMP581 0 #define OSW_PLATFORM_HARDWARE_BME280 1 #define OSW_PLATFORM_HARDWARE_QMC5883L 1 //#define OSW_PLATFORM_HARDWARE_VIRTUAL 0 diff --git a/include/platform/LIGHT_EDITION_V3_3.h b/include/platform/LIGHT_EDITION_V3_3.h index 7e5537c9f..4d438dac8 100644 --- a/include/platform/LIGHT_EDITION_V3_3.h +++ b/include/platform/LIGHT_EDITION_V3_3.h @@ -10,6 +10,7 @@ #define OSW_PLATFORM_HARDWARE_DS3231MZ 1 #define OSW_PLATFORM_HARDWARE_BMA400 1 //#define OSW_PLATFORM_HARDWARE_BMI270 0 +//#define OSW_PLATFORM_HARDWARE_BMP581 0 //#define OSW_PLATFORM_HARDWARE_QMC5883L 0 //#define OSW_PLATFORM_HARDWARE_BME280 0 //#define OSW_PLATFORM_HARDWARE_VIRTUAL 0 diff --git a/include/platform/LIGHT_EDITION_V4_0.h b/include/platform/LIGHT_EDITION_V4_0.h index a6eae2a8e..63f00e93d 100644 --- a/include/platform/LIGHT_EDITION_V4_0.h +++ b/include/platform/LIGHT_EDITION_V4_0.h @@ -10,6 +10,7 @@ #define OSW_PLATFORM_HARDWARE_DS3231MZ 1 #define OSW_PLATFORM_HARDWARE_BMA400 1 //#define OSW_PLATFORM_HARDWARE_BMI270 0 +//#define OSW_PLATFORM_HARDWARE_BMP581 0 //#define OSW_PLATFORM_HARDWARE_BME280 0 //#define OSW_PLATFORM_HARDWARE_QMC5883L 0 //#define OSW_PLATFORM_HARDWARE_VIRTUAL 0 diff --git a/include/platform/MINIMAL.h b/include/platform/MINIMAL.h index 0a93419ec..11b25f4af 100644 --- a/include/platform/MINIMAL.h +++ b/include/platform/MINIMAL.h @@ -10,6 +10,7 @@ //#define OSW_PLATFORM_HARDWARE_DS3231MZ 0 //#define OSW_PLATFORM_HARDWARE_BMA400 0 //#define OSW_PLATFORM_HARDWARE_BMI270 0 +//#define OSW_PLATFORM_HARDWARE_BMP581 0 //#define OSW_PLATFORM_HARDWARE_BME280 0 //#define OSW_PLATFORM_HARDWARE_QMC5883L 0 //#define OSW_PLATFORM_HARDWARE_VIRTUAL 0 diff --git a/src/devices/bmp581.cpp b/src/devices/bmp581.cpp new file mode 100644 index 000000000..e58dee748 --- /dev/null +++ b/src/devices/bmp581.cpp @@ -0,0 +1,111 @@ +#ifndef OSW_EMULATOR +#include OSW_TARGET_PLATFORM_HEADER +#if OSW_PLATFORM_HARDWARE_BMP581 == 1 +#include +#include +#include + +#include + +static int8_t bmp5_i2c_read(uint8_t reg_addr, uint8_t* reg_data, uint32_t len, void* intf_ptr) { + if ((reg_data == NULL) || (len == 0) || (len > 32)) { + return -1; + } + uint8_t bytes_received; + + Wire.beginTransmission(BMP5_I2C_ADDR_PRIM); + Wire.write(reg_addr); + if (Wire.endTransmission() == 0) { + bytes_received = Wire.requestFrom(BMP5_I2C_ADDR_PRIM, len); + // Optionally, throw an error if bytes_received != len + for (uint16_t i = 0; i < bytes_received; i++) { + reg_data[i] = Wire.read(); + } + } else { + return -1; + } + + return 0; +} + +static int8_t bmp5_i2c_write(uint8_t reg_addr, const uint8_t* reg_data, uint32_t len, void* intf_ptr) { + if ((reg_data == NULL) || (len == 0) || (len > 32)) { + return -1; + } + + Wire.beginTransmission(BMP5_I2C_ADDR_PRIM); + Wire.write(reg_addr); + for (uint16_t i = 0; i < len; i++) { + Wire.write(reg_data[i]); + } + if (Wire.endTransmission() != 0) { + return -1; + } + + return 0; +} + +static void bmp5_delay_us(uint32_t period, void* intf_ptr) { + delayMicroseconds(period); +} + +void OswDevices::BMP581::setup() { + int8_t rslt; + { + this->bmp5.chip_id = BMP5_I2C_ADDR_PRIM; + this->bmp5.read = bmp5_i2c_read; + this->bmp5.write = bmp5_i2c_write; + this->bmp5.delay_us = bmp5_delay_us; + this->bmp5.intf = BMP5_I2C_INTF; + this->bmp5.intf_ptr = nullptr; // we are using the default address anyways + + rslt = bmp5_init(&this->bmp5); + if(rslt != BMP5_OK) + throw std::runtime_error("Failed to initialize BMP581!"); + } + { + rslt = bmp5_set_power_mode(BMP5_POWERMODE_STANDBY, &this->bmp5); + if(rslt != BMP5_OK) + throw std::runtime_error("Failed to reconfigure BMP581 power mode!"); + + struct bmp5_iir_config set_iir_cfg; + rslt = bmp5_get_osr_odr_press_config(&this->osr_odr_press_cfg, &this->bmp5); + + osr_odr_press_cfg.odr = BMP5_ODR_50_HZ; + osr_odr_press_cfg.press_en = BMP5_ENABLE; + osr_odr_press_cfg.osr_t = BMP5_OVERSAMPLING_64X; + osr_odr_press_cfg.osr_p = BMP5_OVERSAMPLING_4X; + rslt = bmp5_set_osr_odr_press_config(&this->osr_odr_press_cfg, &this->bmp5); + + set_iir_cfg.set_iir_t = BMP5_IIR_FILTER_COEFF_1; + set_iir_cfg.set_iir_p = BMP5_IIR_FILTER_COEFF_1; + set_iir_cfg.shdw_set_iir_t = BMP5_ENABLE; + set_iir_cfg.shdw_set_iir_p = BMP5_ENABLE; + rslt = bmp5_set_iir_config(&set_iir_cfg, &this->bmp5); + + rslt = bmp5_set_power_mode(BMP5_POWERMODE_NORMAL, &this->bmp5); + if(rslt != BMP5_OK) + throw std::runtime_error("Failed to reconfigure BMP581 power mode!"); + } + // TODO proper sensor reading configuration +} + +void OswDevices::BMP581::update() { + int8_t rslt; + struct bmp5_sensor_data sensor_data; + + rslt = bmp5_get_sensor_data(&sensor_data, &this->osr_odr_press_cfg, &this->bmp5); + + this->pressure = sensor_data.pressure; + this->temperature = sensor_data.temperature; +} + +float OswDevices::BMP581::getPressure() { + return this->pressure; +} + +float OswDevices::BMP581::getTemperature() { + return this->temperature; +} +#endif +#endif \ No newline at end of file diff --git a/src/hal/devices.cpp b/src/hal/devices.cpp index 8862c43bb..8e23c8d6a 100644 --- a/src/hal/devices.cpp +++ b/src/hal/devices.cpp @@ -10,6 +10,9 @@ OswHal::Devices::Devices() { #if OSW_PLATFORM_HARDWARE_BMI270 == 1 this->bmi270 = new OswDevices::BMI270(); #endif +#if OSW_PLATFORM_HARDWARE_BMP581 == 1 + this->bmp581 = new OswDevices::BMP581(); +#endif #if OSW_PLATFORM_HARDWARE_QMC5883L == 1 this->qmc5883l = new OswDevices::QMC5883L(); #endif