Skip to content

Commit

Permalink
Added basic BMP581 code (untested as FLOW3R platform uses another int…
Browse files Browse the repository at this point in the history
…erface!)

Signed-off-by: simonmicro <[email protected]>
  • Loading branch information
simonmicro committed Apr 6, 2024
1 parent 3b04e6c commit 070daf9
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 0 deletions.
41 changes: 41 additions & 0 deletions include/devices/bmp581.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once
#include OSW_TARGET_PLATFORM_HEADER
#if OSW_PLATFORM_HARDWARE_BMP581 == 1

#include <devices/interfaces/OswTemperatureProvider.h>
#include <devices/interfaces/OswPressureProvider.h>
#include <bmp5_defs.h>
#include <bmp5.h>

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
4 changes: 4 additions & 0 deletions include/hal/devices.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <devices/qmc5883l.h>
#include <devices/bme280.h>
#include <devices/bmi270.h>
#include <devices/bmp581.h>
#include <devices/ds3231mz.h>
#include <devices/esp32.h>
#include <devices/virtual.h>
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions include/platform/EMULATOR.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions include/platform/FLOW3R_C3CAMP_2023.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions include/platform/GPS_EDITION_V3_1.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions include/platform/LIGHT_EDITION_V3_3.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions include/platform/LIGHT_EDITION_V4_0.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions include/platform/MINIMAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
111 changes: 111 additions & 0 deletions src/devices/bmp581.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#ifndef OSW_EMULATOR
#include OSW_TARGET_PLATFORM_HEADER
#if OSW_PLATFORM_HARDWARE_BMP581 == 1
#include <stdexcept>
#include <Wire.h>
#include <OswLogger.h>

#include <devices/bmp581.h>

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
3 changes: 3 additions & 0 deletions src/hal/devices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 070daf9

Please sign in to comment.