SPI and I2C for BMP390 and BMP380 (#6652)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Anton Viktorov 2024-05-02 03:49:01 +02:00 committed by GitHub
parent bc65e6e914
commit c7c0d97a5e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
34 changed files with 375 additions and 201 deletions

View File

@ -63,7 +63,10 @@ esphome/components/bme280_base/* @esphome/core
esphome/components/bme280_spi/* @apbodrov
esphome/components/bme680_bsec/* @trvrnrth
esphome/components/bmi160/* @flaviut
esphome/components/bmp3xx/* @martgras
esphome/components/bmp3xx/* @latonita
esphome/components/bmp3xx_base/* @latonita @martgras
esphome/components/bmp3xx_i2c/* @latonita
esphome/components/bmp3xx_spi/* @latonita
esphome/components/bmp581/* @kahrendt
esphome/components/bp1658cj/* @Cossid
esphome/components/bp5758d/* @Cossid

View File

@ -1,102 +1,7 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import i2c, sensor
from esphome.const import (
CONF_ID,
CONF_IIR_FILTER,
CONF_OVERSAMPLING,
CONF_PRESSURE,
CONF_TEMPERATURE,
DEVICE_CLASS_PRESSURE,
DEVICE_CLASS_TEMPERATURE,
STATE_CLASS_MEASUREMENT,
UNIT_CELSIUS,
UNIT_HECTOPASCAL,
CODEOWNERS = ["@latonita"]
CONFIG_SCHEMA = CONFIG_SCHEMA = cv.invalid(
"The bmp3xx sensor component has been renamed to bmp3xx_i2c."
)
CODEOWNERS = ["@martgras"]
DEPENDENCIES = ["i2c"]
bmp3xx_ns = cg.esphome_ns.namespace("bmp3xx")
Oversampling = bmp3xx_ns.enum("Oversampling")
OVERSAMPLING_OPTIONS = {
"NONE": Oversampling.OVERSAMPLING_NONE,
"2X": Oversampling.OVERSAMPLING_X2,
"4X": Oversampling.OVERSAMPLING_X4,
"8X": Oversampling.OVERSAMPLING_X8,
"16X": Oversampling.OVERSAMPLING_X16,
"32X": Oversampling.OVERSAMPLING_X32,
}
IIRFilter = bmp3xx_ns.enum("IIRFilter")
IIR_FILTER_OPTIONS = {
"OFF": IIRFilter.IIR_FILTER_OFF,
"2X": IIRFilter.IIR_FILTER_2,
"4X": IIRFilter.IIR_FILTER_4,
"8X": IIRFilter.IIR_FILTER_8,
"16X": IIRFilter.IIR_FILTER_16,
"32X": IIRFilter.IIR_FILTER_32,
"64X": IIRFilter.IIR_FILTER_64,
"128X": IIRFilter.IIR_FILTER_128,
}
BMP3XXComponent = bmp3xx_ns.class_(
"BMP3XXComponent", cg.PollingComponent, i2c.I2CDevice
)
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(BMP3XXComponent),
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
).extend(
{
cv.Optional(CONF_OVERSAMPLING, default="2X"): cv.enum(
OVERSAMPLING_OPTIONS, upper=True
),
}
),
cv.Optional(CONF_PRESSURE): sensor.sensor_schema(
unit_of_measurement=UNIT_HECTOPASCAL,
accuracy_decimals=1,
device_class=DEVICE_CLASS_PRESSURE,
state_class=STATE_CLASS_MEASUREMENT,
).extend(
{
cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum(
OVERSAMPLING_OPTIONS, upper=True
),
}
),
cv.Optional(CONF_IIR_FILTER, default="OFF"): cv.enum(
IIR_FILTER_OPTIONS, upper=True
),
}
)
.extend(cv.polling_component_schema("60s"))
.extend(i2c.i2c_device_schema(0x77))
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)
cg.add(var.set_iir_filter_config(config[CONF_IIR_FILTER]))
if temperature_config := config.get(CONF_TEMPERATURE):
sens = await sensor.new_sensor(temperature_config)
cg.add(var.set_temperature_sensor(sens))
cg.add(
var.set_temperature_oversampling_config(
temperature_config[CONF_OVERSAMPLING]
)
)
if pressure_config := config.get(CONF_PRESSURE):
sens = await sensor.new_sensor(pressure_config)
cg.add(var.set_pressure_sensor(sens))
cg.add(var.set_pressure_oversampling_config(pressure_config[CONF_OVERSAMPLING]))

View File

@ -0,0 +1,95 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor
from esphome.const import (
CONF_ID,
CONF_IIR_FILTER,
CONF_OVERSAMPLING,
CONF_PRESSURE,
CONF_TEMPERATURE,
DEVICE_CLASS_PRESSURE,
DEVICE_CLASS_TEMPERATURE,
STATE_CLASS_MEASUREMENT,
UNIT_CELSIUS,
UNIT_HECTOPASCAL,
)
CODEOWNERS = ["@martgras", "@latonita"]
bmp3xx_ns = cg.esphome_ns.namespace("bmp3xx_base")
Oversampling = bmp3xx_ns.enum("Oversampling")
OVERSAMPLING_OPTIONS = {
"NONE": Oversampling.OVERSAMPLING_NONE,
"2X": Oversampling.OVERSAMPLING_X2,
"4X": Oversampling.OVERSAMPLING_X4,
"8X": Oversampling.OVERSAMPLING_X8,
"16X": Oversampling.OVERSAMPLING_X16,
"32X": Oversampling.OVERSAMPLING_X32,
}
IIRFilter = bmp3xx_ns.enum("IIRFilter")
IIR_FILTER_OPTIONS = {
"OFF": IIRFilter.IIR_FILTER_OFF,
"2X": IIRFilter.IIR_FILTER_2,
"4X": IIRFilter.IIR_FILTER_4,
"8X": IIRFilter.IIR_FILTER_8,
"16X": IIRFilter.IIR_FILTER_16,
"32X": IIRFilter.IIR_FILTER_32,
"64X": IIRFilter.IIR_FILTER_64,
"128X": IIRFilter.IIR_FILTER_128,
}
CONFIG_SCHEMA_BASE = cv.Schema(
{
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
).extend(
{
cv.Optional(CONF_OVERSAMPLING, default="2X"): cv.enum(
OVERSAMPLING_OPTIONS, upper=True
),
}
),
cv.Optional(CONF_PRESSURE): sensor.sensor_schema(
unit_of_measurement=UNIT_HECTOPASCAL,
accuracy_decimals=1,
device_class=DEVICE_CLASS_PRESSURE,
state_class=STATE_CLASS_MEASUREMENT,
).extend(
{
cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum(
OVERSAMPLING_OPTIONS, upper=True
),
}
),
cv.Optional(CONF_IIR_FILTER, default="OFF"): cv.enum(
IIR_FILTER_OPTIONS, upper=True
),
}
).extend(cv.polling_component_schema("60s"))
async def to_code_base(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
cg.add(var.set_iir_filter_config(config[CONF_IIR_FILTER]))
if temperature_config := config.get(CONF_TEMPERATURE):
sens = await sensor.new_sensor(temperature_config)
cg.add(var.set_temperature_sensor(sens))
cg.add(
var.set_temperature_oversampling_config(
temperature_config[CONF_OVERSAMPLING]
)
)
if pressure_config := config.get(CONF_PRESSURE):
sens = await sensor.new_sensor(pressure_config)
cg.add(var.set_pressure_sensor(sens))
cg.add(var.set_pressure_oversampling_config(pressure_config[CONF_OVERSAMPLING]))
return var

View File

@ -5,13 +5,13 @@
http://github.com/MartinL1/BMP388_DEV
*/
#include "bmp3xx.h"
#include "bmp3xx_base.h"
#include "esphome/core/log.h"
#include "esphome/core/hal.h"
#include <cinttypes>
namespace esphome {
namespace bmp3xx {
namespace bmp3xx_base {
static const char *const TAG = "bmp3xx.sensor";
@ -150,7 +150,6 @@ void BMP3XXComponent::setup() {
void BMP3XXComponent::dump_config() {
ESP_LOGCONFIG(TAG, "BMP3XX:");
ESP_LOGCONFIG(TAG, " Type: %s (0x%X)", LOG_STR_ARG(chip_type_to_str(this->chip_id_.reg)), this->chip_id_.reg);
LOG_I2C_DEVICE(this);
switch (this->error_code_) {
case NONE:
break;
@ -386,5 +385,5 @@ float BMP3XXComponent::bmp388_compensate_pressure_(float uncomp_press, float t_l
return partial_out1 + partial_out2 + partial_data4;
}
} // namespace bmp3xx
} // namespace bmp3xx_base
} // namespace esphome

View File

@ -9,10 +9,9 @@
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/i2c/i2c.h"
namespace esphome {
namespace bmp3xx {
namespace bmp3xx_base {
static const uint8_t BMP388_ID = 0x50; // The BMP388 device ID
static const uint8_t BMP390_ID = 0x60; // The BMP390 device ID
@ -69,8 +68,8 @@ enum IIRFilter {
IIR_FILTER_128 = 0x07
};
/// This class implements support for the BMP3XX Temperature+Pressure i2c sensor.
class BMP3XXComponent : public PollingComponent, public i2c::I2CDevice {
/// This class implements support for the BMP3XX Temperature+Pressure sensor.
class BMP3XXComponent : public PollingComponent {
public:
void setup() override;
void dump_config() override;
@ -231,7 +230,13 @@ class BMP3XXComponent : public PollingComponent, public i2c::I2CDevice {
float bmp388_compensate_temperature_(float uncomp_temp);
// Bosch pressure compensation function
float bmp388_compensate_pressure_(float uncomp_press, float t_lin);
// interface specific functions
virtual bool read_byte(uint8_t a_register, uint8_t *data) = 0;
virtual bool write_byte(uint8_t a_register, uint8_t data) = 0;
virtual bool read_bytes(uint8_t a_register, uint8_t *data, size_t len) = 0;
virtual bool write_bytes(uint8_t a_register, uint8_t *data, size_t len) = 0;
};
} // namespace bmp3xx
} // namespace bmp3xx_base
} // namespace esphome

View File

@ -0,0 +1,29 @@
#include "esphome/components/i2c/i2c.h"
#include "bmp3xx_i2c.h"
#include <cinttypes>
namespace esphome {
namespace bmp3xx_i2c {
static const char *const TAG = "bmp3xx_i2c.sensor";
bool BMP3XXI2CComponent::read_byte(uint8_t a_register, uint8_t *data) {
return I2CDevice::read_byte(a_register, data);
};
bool BMP3XXI2CComponent::write_byte(uint8_t a_register, uint8_t data) {
return I2CDevice::write_byte(a_register, data);
};
bool BMP3XXI2CComponent::read_bytes(uint8_t a_register, uint8_t *data, size_t len) {
return I2CDevice::read_bytes(a_register, data, len);
};
bool BMP3XXI2CComponent::write_bytes(uint8_t a_register, uint8_t *data, size_t len) {
return I2CDevice::write_bytes(a_register, data, len);
};
void BMP3XXI2CComponent::dump_config() {
LOG_I2C_DEVICE(this);
BMP3XXComponent::dump_config();
}
} // namespace bmp3xx_i2c
} // namespace esphome

View File

@ -0,0 +1,17 @@
#pragma once
#include "esphome/components/i2c/i2c.h"
#include "esphome/components/bmp3xx_base/bmp3xx_base.h"
namespace esphome {
namespace bmp3xx_i2c {
class BMP3XXI2CComponent : public bmp3xx_base::BMP3XXComponent, public i2c::I2CDevice {
bool read_byte(uint8_t a_register, uint8_t *data) override;
bool write_byte(uint8_t a_register, uint8_t data) override;
bool read_bytes(uint8_t a_register, uint8_t *data, size_t len) override;
bool write_bytes(uint8_t a_register, uint8_t *data, size_t len) override;
void dump_config() override;
};
} // namespace bmp3xx_i2c
} // namespace esphome

View File

@ -0,0 +1,22 @@
import esphome.codegen as cg
from esphome.components import i2c
from ..bmp3xx_base import to_code_base, cv, CONFIG_SCHEMA_BASE
AUTO_LOAD = ["bmp3xx_base"]
CODEOWNERS = ["@latonita"]
DEPENDENCIES = ["i2c"]
bmp3xx_ns = cg.esphome_ns.namespace("bmp3xx_i2c")
BMP3XXI2CComponent = bmp3xx_ns.class_(
"BMP3XXI2CComponent", cg.PollingComponent, i2c.I2CDevice
)
CONFIG_SCHEMA = CONFIG_SCHEMA_BASE.extend(
i2c.i2c_device_schema(default_address=0x77)
).extend({cv.GenerateID(): cv.declare_id(BMP3XXI2CComponent)})
async def to_code(config):
var = await to_code_base(config)
await i2c.register_i2c_device(var, config)

View File

@ -0,0 +1,57 @@
#include "bmp3xx_spi.h"
#include <cinttypes>
namespace esphome {
namespace bmp3xx_spi {
static const char *const TAG = "bmp3xx_spi.sensor";
uint8_t set_bit(uint8_t num, int position) {
int mask = 1 << position;
return num | mask;
}
uint8_t clear_bit(uint8_t num, int position) {
int mask = 1 << position;
return num & ~mask;
}
void BMP3XXSPIComponent::setup() {
this->spi_setup();
BMP3XXComponent::setup();
}
bool BMP3XXSPIComponent::read_byte(uint8_t a_register, uint8_t *data) {
this->enable();
this->transfer_byte(set_bit(a_register, 7));
*data = this->transfer_byte(0);
this->disable();
return true;
}
bool BMP3XXSPIComponent::write_byte(uint8_t a_register, uint8_t data) {
this->enable();
this->transfer_byte(clear_bit(a_register, 7));
this->transfer_byte(data);
this->disable();
return true;
}
bool BMP3XXSPIComponent::read_bytes(uint8_t a_register, uint8_t *data, size_t len) {
this->enable();
this->transfer_byte(set_bit(a_register, 7));
this->read_array(data, len);
this->disable();
return true;
}
bool BMP3XXSPIComponent::write_bytes(uint8_t a_register, uint8_t *data, size_t len) {
this->enable();
this->transfer_byte(clear_bit(a_register, 7));
this->transfer_array(data, len);
this->disable();
return true;
}
} // namespace bmp3xx_spi
} // namespace esphome

View File

@ -0,0 +1,19 @@
#pragma once
#include "esphome/components/bmp3xx_base/bmp3xx_base.h"
#include "esphome/components/spi/spi.h"
namespace esphome {
namespace bmp3xx_spi {
class BMP3XXSPIComponent : public bmp3xx_base::BMP3XXComponent,
public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW,
spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_1MHZ> {
void setup() override;
bool read_byte(uint8_t a_register, uint8_t *data) override;
bool write_byte(uint8_t a_register, uint8_t data) override;
bool read_bytes(uint8_t a_register, uint8_t *data, size_t len) override;
bool write_bytes(uint8_t a_register, uint8_t *data, size_t len) override;
};
} // namespace bmp3xx_spi
} // namespace esphome

View File

@ -0,0 +1,22 @@
import esphome.codegen as cg
from esphome.components import spi
from ..bmp3xx_base import to_code_base, cv, CONFIG_SCHEMA_BASE
AUTO_LOAD = ["bmp3xx_base"]
CODEOWNERS = ["@latonita"]
DEPENDENCIES = ["spi"]
bmp3xx_ns = cg.esphome_ns.namespace("bmp3xx_spi")
BMP3XXSPIComponent = bmp3xx_ns.class_(
"BMP3XXSPIComponent", cg.PollingComponent, spi.SPIDevice
)
CONFIG_SCHEMA = CONFIG_SCHEMA_BASE.extend(spi.spi_device_schema()).extend(
{cv.GenerateID(): cv.declare_id(BMP3XXSPIComponent)}
)
async def to_code(config):
var = await to_code_base(config)
await spi.register_spi_device(var, config)

View File

@ -1,14 +0,0 @@
i2c:
- id: i2c_bmp3xx
scl: 5
sda: 4
sensor:
- platform: bmp3xx
address: 0x77
temperature:
name: BMP Temperature
oversampling: 16x
pressure:
name: BMP Pressure
iir_filter: 2X

View File

@ -1,14 +0,0 @@
i2c:
- id: i2c_bmp3xx
scl: 5
sda: 4
sensor:
- platform: bmp3xx
address: 0x77
temperature:
name: BMP Temperature
oversampling: 16x
pressure:
name: BMP Pressure
iir_filter: 2X

View File

@ -1,14 +0,0 @@
i2c:
- id: i2c_bmp3xx
scl: 16
sda: 17
sensor:
- platform: bmp3xx
address: 0x77
temperature:
name: BMP Temperature
oversampling: 16x
pressure:
name: BMP Pressure
iir_filter: 2X

View File

@ -1,14 +0,0 @@
i2c:
- id: i2c_bmp3xx
scl: 5
sda: 4
sensor:
- platform: bmp3xx
address: 0x77
temperature:
name: BMP Temperature
oversampling: 16x
pressure:
name: BMP Pressure
iir_filter: 2X

View File

@ -1,14 +0,0 @@
i2c:
- id: i2c_bmp3xx
scl: 5
sda: 4
sensor:
- platform: bmp3xx
address: 0x77
temperature:
name: BMP Temperature
oversampling: 16x
pressure:
name: BMP Pressure
iir_filter: 2X

View File

@ -1,10 +1,11 @@
i2c:
- id: i2c_bmp3xx
scl: 16
sda: 17
scl: ${scl_pin}
sda: ${sda_pin}
sensor:
- platform: bmp3xx
- platform: bmp3xx_i2c
i2c_id: i2c_bmp3xx
address: 0x77
temperature:
name: BMP Temperature

View File

@ -0,0 +1,5 @@
substitutions:
scl_pin: GPIO5
sda_pin: GPIO4
<<: !include common.yaml

View File

@ -0,0 +1,5 @@
substitutions:
scl_pin: GPIO5
sda_pin: GPIO4
<<: !include common.yaml

View File

@ -0,0 +1,5 @@
substitutions:
scl_pin: GPIO16
sda_pin: GPIO17
<<: !include common.yaml

View File

@ -0,0 +1,5 @@
substitutions:
scl_pin: GPIO16
sda_pin: GPIO17
<<: !include common.yaml

View File

@ -0,0 +1,5 @@
substitutions:
scl_pin: GPIO5
sda_pin: GPIO4
<<: !include common.yaml

View File

@ -0,0 +1,5 @@
substitutions:
scl_pin: GPIO5
sda_pin: GPIO4
<<: !include common.yaml

View File

@ -0,0 +1,16 @@
spi:
- id: spi_bmp3xx
clk_pin: ${clk_pin}
mosi_pin: ${mosi_pin}
miso_pin: ${miso_pin}
sensor:
- platform: bmp3xx_spi
spi_id: spi_bmp3xx
cs_pin: ${cs_pin}
temperature:
name: BMP Temperature
oversampling: 16x
pressure:
name: BMP Pressure
iir_filter: 2X

View File

@ -0,0 +1,7 @@
substitutions:
clk_pin: GPIO6
mosi_pin: GPIO7
miso_pin: GPIO5
cs_pin: GPIO8
<<: !include common.yaml

View File

@ -0,0 +1,7 @@
substitutions:
clk_pin: GPIO6
mosi_pin: GPIO7
miso_pin: GPIO5
cs_pin: GPIO8
<<: !include common.yaml

View File

@ -0,0 +1,7 @@
substitutions:
clk_pin: GPIO16
mosi_pin: GPIO17
miso_pin: GPIO15
cs_pin: GPIO5
<<: !include common.yaml

View File

@ -0,0 +1,7 @@
substitutions:
clk_pin: GPIO16
mosi_pin: GPIO17
miso_pin: GPIO15
cs_pin: GPIO5
<<: !include common.yaml

View File

@ -0,0 +1,7 @@
substitutions:
clk_pin: GPIO14
mosi_pin: GPIO13
miso_pin: GPIO12
cs_pin: GPIO15
<<: !include common.yaml

View File

@ -0,0 +1,7 @@
substitutions:
clk_pin: GPIO2
mosi_pin: GPIO3
miso_pin: GPIO4
cs_pin: GPIO5
<<: !include common.yaml

View File

@ -498,15 +498,6 @@ sensor:
co2:
name: CO2 Sensor
- platform: bmp3xx
temperature:
name: BMP Temperature
oversampling: 16x
pressure:
name: BMP Pressure
address: 0x77
iir_filter: 2X
- platform: sen5x
id: sen54
temperature:

View File

@ -474,15 +474,6 @@ sensor:
co2:
name: CO2 Sensor
- platform: bmp3xx
temperature:
name: BMP Temperature
oversampling: 16x
pressure:
name: BMP Pressure
address: 0x77
iir_filter: 2X
- platform: ms8607
temperature:
name: Temperature