mirror of
https://github.com/esphome/esphome.git
synced 2025-10-03 02:22:25 +01:00
BME280 SPI (#5538)
* bme spi finally * linter * CO * tidy * lint * tidy [2] * tidy[-1] * final solution * Update test1.yaml remove failed test * Update test1.1.yaml add test to another file with free GPIO5 pin * fix spi read bytes * fix tests * rename bme280 to bme280_i2c
This commit is contained in:
1
esphome/components/bme280_spi/__init__.py
Normal file
1
esphome/components/bme280_spi/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
CODEOWNERS = ["@apbodrov"]
|
66
esphome/components/bme280_spi/bme280_spi.cpp
Normal file
66
esphome/components/bme280_spi/bme280_spi.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
|
||||
#include "bme280_spi.h"
|
||||
#include <esphome/components/bme280_base/bme280_base.h>
|
||||
|
||||
int set_bit(uint8_t num, int position) {
|
||||
int mask = 1 << position;
|
||||
return num | mask;
|
||||
}
|
||||
|
||||
int clear_bit(uint8_t num, int position) {
|
||||
int mask = 1 << position;
|
||||
return num & ~mask;
|
||||
}
|
||||
|
||||
namespace esphome {
|
||||
namespace bme280_spi {
|
||||
|
||||
void BME280SPIComponent::setup() {
|
||||
this->spi_setup();
|
||||
BME280Component::setup();
|
||||
};
|
||||
|
||||
// In SPI mode, only 7 bits of the register addresses are used; the MSB of register address is not used
|
||||
// and replaced by a read/write bit (RW = ‘0’ for write and RW = ‘1’ for read).
|
||||
// Example: address 0xF7 is accessed by using SPI register address 0x77. For write access, the byte
|
||||
// 0x77 is transferred, for read access, the byte 0xF7 is transferred.
|
||||
// https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme280-ds002.pdf
|
||||
|
||||
bool BME280SPIComponent::read_byte(uint8_t a_register, uint8_t *data) {
|
||||
this->enable();
|
||||
// cause: *data = this->delegate_->transfer(tmp) doesnt work
|
||||
this->delegate_->transfer(set_bit(a_register, 7));
|
||||
*data = this->delegate_->transfer(0);
|
||||
this->disable();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BME280SPIComponent::write_byte(uint8_t a_register, uint8_t data) {
|
||||
this->enable();
|
||||
this->delegate_->transfer(clear_bit(a_register, 7));
|
||||
this->delegate_->transfer(data);
|
||||
this->disable();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BME280SPIComponent::read_bytes(uint8_t a_register, uint8_t *data, size_t len) {
|
||||
this->enable();
|
||||
this->delegate_->transfer(set_bit(a_register, 7));
|
||||
this->delegate_->read_array(data, len);
|
||||
this->disable();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BME280SPIComponent::read_byte_16(uint8_t a_register, uint16_t *data) {
|
||||
this->enable();
|
||||
this->delegate_->transfer(set_bit(a_register, 7));
|
||||
((uint8_t *) data)[1] = this->delegate_->transfer(0);
|
||||
((uint8_t *) data)[0] = this->delegate_->transfer(0);
|
||||
this->disable();
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace bme280_spi
|
||||
} // namespace esphome
|
20
esphome/components/bme280_spi/bme280_spi.h
Normal file
20
esphome/components/bme280_spi/bme280_spi.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/components/bme280_base/bme280_base.h"
|
||||
#include "esphome/components/spi/spi.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace bme280_spi {
|
||||
|
||||
class BME280SPIComponent : public esphome::bme280_base::BME280Component,
|
||||
public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW,
|
||||
spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_200KHZ> {
|
||||
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 read_byte_16(uint8_t a_register, uint16_t *data) override;
|
||||
};
|
||||
|
||||
} // namespace bme280_spi
|
||||
} // namespace esphome
|
24
esphome/components/bme280_spi/sensor.py
Normal file
24
esphome/components/bme280_spi/sensor.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import spi
|
||||
from esphome.components.bme280_base.sensor import (
|
||||
to_code as to_code_base,
|
||||
cv,
|
||||
CONFIG_SCHEMA_BASE,
|
||||
)
|
||||
|
||||
DEPENDENCIES = ["spi"]
|
||||
AUTO_LOAD = ["bme280_base"]
|
||||
|
||||
|
||||
bme280_spi_ns = cg.esphome_ns.namespace("bme280_spi")
|
||||
BME280SPIComponent = bme280_spi_ns.class_(
|
||||
"BME280SPIComponent", cg.PollingComponent, spi.SPIDevice
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = CONFIG_SCHEMA_BASE.extend(spi.spi_device_schema()).extend(
|
||||
{cv.GenerateID(): cv.declare_id(BME280SPIComponent)}
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
await to_code_base(config, func=spi.register_spi_device)
|
Reference in New Issue
Block a user