ESPHome  2024.10.2
bme280_spi.cpp
Go to the documentation of this file.
1 #include <cstdint>
2 #include <cstddef>
3 
4 #include "bme280_spi.h"
6 
7 namespace esphome {
8 namespace bme280_spi {
9 
10 uint8_t set_bit(uint8_t num, int position) {
11  int mask = 1 << position;
12  return num | mask;
13 }
14 
15 uint8_t clear_bit(uint8_t num, int position) {
16  int mask = 1 << position;
17  return num & ~mask;
18 }
19 
20 void BME280SPIComponent::setup() {
21  this->spi_setup();
23 };
24 
25 // In SPI mode, only 7 bits of the register addresses are used; the MSB of register address is not used
26 // and replaced by a read/write bit (RW = ‘0’ for write and RW = ‘1’ for read).
27 // Example: address 0xF7 is accessed by using SPI register address 0x77. For write access, the byte
28 // 0x77 is transferred, for read access, the byte 0xF7 is transferred.
29 // https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme280-ds002.pdf
30 
31 bool BME280SPIComponent::read_byte(uint8_t a_register, uint8_t *data) {
32  this->enable();
33  this->transfer_byte(set_bit(a_register, 7));
34  *data = this->transfer_byte(0);
35  this->disable();
36  return true;
37 }
38 
39 bool BME280SPIComponent::write_byte(uint8_t a_register, uint8_t data) {
40  this->enable();
41  this->transfer_byte(clear_bit(a_register, 7));
42  this->transfer_byte(data);
43  this->disable();
44  return true;
45 }
46 
47 bool BME280SPIComponent::read_bytes(uint8_t a_register, uint8_t *data, size_t len) {
48  this->enable();
49  this->transfer_byte(set_bit(a_register, 7));
50  this->read_array(data, len);
51  this->disable();
52  return true;
53 }
54 
55 bool BME280SPIComponent::read_byte_16(uint8_t a_register, uint16_t *data) {
56  this->enable();
57  this->transfer_byte(set_bit(a_register, 7));
58  ((uint8_t *) data)[1] = this->transfer_byte(0);
59  ((uint8_t *) data)[0] = this->transfer_byte(0);
60  this->disable();
61  return true;
62 }
63 
64 } // namespace bme280_spi
65 } // namespace esphome
void setup()
uint8_t set_bit(uint8_t num, int position)
Definition: bme280_spi.cpp:10
uint8_t clear_bit(uint8_t num, int position)
Definition: bme280_spi.cpp:15
std::string size_t len
Definition: helpers.h:292
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
float position
Definition: cover.h:14