ESPHome  2024.12.2
i2c.cpp
Go to the documentation of this file.
1 #include "i2c.h"
2 #include "esphome/core/log.h"
3 #include <memory>
4 
5 namespace esphome {
6 namespace i2c {
7 
8 static const char *const TAG = "i2c";
9 
10 ErrorCode I2CDevice::read_register(uint8_t a_register, uint8_t *data, size_t len, bool stop) {
11  ErrorCode err = this->write(&a_register, 1, stop);
12  if (err != ERROR_OK)
13  return err;
14  return bus_->read(address_, data, len);
15 }
16 
17 ErrorCode I2CDevice::read_register16(uint16_t a_register, uint8_t *data, size_t len, bool stop) {
18  a_register = convert_big_endian(a_register);
19  ErrorCode const err = this->write(reinterpret_cast<const uint8_t *>(&a_register), 2, stop);
20  if (err != ERROR_OK)
21  return err;
22  return bus_->read(address_, data, len);
23 }
24 
25 ErrorCode I2CDevice::write_register(uint8_t a_register, const uint8_t *data, size_t len, bool stop) {
26  WriteBuffer buffers[2];
27  buffers[0].data = &a_register;
28  buffers[0].len = 1;
29  buffers[1].data = data;
30  buffers[1].len = len;
31  return bus_->writev(address_, buffers, 2, stop);
32 }
33 
34 ErrorCode I2CDevice::write_register16(uint16_t a_register, const uint8_t *data, size_t len, bool stop) {
35  a_register = convert_big_endian(a_register);
36  WriteBuffer buffers[2];
37  buffers[0].data = reinterpret_cast<const uint8_t *>(&a_register);
38  buffers[0].len = 2;
39  buffers[1].data = data;
40  buffers[1].len = len;
41  return bus_->writev(address_, buffers, 2, stop);
42 }
43 
44 bool I2CDevice::read_bytes_16(uint8_t a_register, uint16_t *data, uint8_t len) {
45  if (read_register(a_register, reinterpret_cast<uint8_t *>(data), len * 2) != ERROR_OK)
46  return false;
47  for (size_t i = 0; i < len; i++)
48  data[i] = i2ctohs(data[i]);
49  return true;
50 }
51 
52 bool I2CDevice::write_bytes_16(uint8_t a_register, const uint16_t *data, uint8_t len) {
53  // we have to copy in order to be able to change byte order
54  std::unique_ptr<uint16_t[]> temp{new uint16_t[len]};
55  for (size_t i = 0; i < len; i++)
56  temp[i] = htoi2cs(data[i]);
57  return write_register(a_register, reinterpret_cast<const uint8_t *>(temp.get()), len * 2) == ERROR_OK;
58 }
59 
61  this->parent_->write_register(this->register_, &value, 1);
62  return *this;
63 }
64 I2CRegister &I2CRegister::operator&=(uint8_t value) {
65  value &= get();
66  this->parent_->write_register(this->register_, &value, 1);
67  return *this;
68 }
70  value |= get();
71  this->parent_->write_register(this->register_, &value, 1);
72  return *this;
73 }
74 
75 uint8_t I2CRegister::get() const {
76  uint8_t value = 0x00;
77  this->parent_->read_register(this->register_, &value, 1);
78  return value;
79 }
80 
82  this->parent_->write_register16(this->register_, &value, 1);
83  return *this;
84 }
86  value &= get();
87  this->parent_->write_register16(this->register_, &value, 1);
88  return *this;
89 }
91  value |= get();
92  this->parent_->write_register16(this->register_, &value, 1);
93  return *this;
94 }
95 
96 uint8_t I2CRegister16::get() const {
97  uint8_t value = 0x00;
98  this->parent_->read_register16(this->register_, &value, 1);
99  return value;
100 }
101 
102 } // namespace i2c
103 } // namespace esphome
the WriteBuffer structure stores a pointer to a write buffer and its length
Definition: i2c_bus.h:30
uint16_t i2ctohs(uint16_t i2cshort)
Definition: i2c.h:128
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len, bool stop=true)
reads an array of bytes from a specific register in the I²C device
Definition: i2c.cpp:10
uint8_t get() const
returns the register value
Definition: i2c.cpp:75
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
Definition: i2c.h:186
bool write_bytes_16(uint8_t a_register, const uint16_t *data, uint8_t len)
Definition: i2c.cpp:52
uint16_t htoi2cs(uint16_t hostshort)
Definition: i2c.h:129
I2CRegister & operator|=(uint8_t value)
overloads the compound |= operator.
Definition: i2c.cpp:69
virtual ErrorCode writev(uint8_t address, WriteBuffer *buffers, size_t cnt)
Definition: i2c_bus.h:80
No error found during execution of method.
Definition: i2c_bus.h:13
constexpr14 T convert_big_endian(T val)
Convert a value between host byte order and big endian (most significant byte first) order...
Definition: helpers.h:240
size_t len
length of the buffer
Definition: i2c_bus.h:32
ErrorCode write_register16(uint16_t a_register, const uint8_t *data, size_t len, bool stop=true)
write an array of bytes to a specific register in the I²C device
Definition: i2c.cpp:34
uint8_t address_
store the address of the device on the bus
Definition: i2c.h:269
I2CBus * bus_
pointer to I2CBus instance
Definition: i2c.h:270
std::string size_t len
Definition: helpers.h:293
virtual ErrorCode read(uint8_t address, uint8_t *buffer, size_t len)
Creates a ReadBuffer and calls the virtual readv() method to read bytes into this buffer...
Definition: i2c_bus.h:47
This class is used to create I2CRegister objects that act as proxies to read/write internal registers...
Definition: i2c.h:33
I2CRegister16 & operator=(uint8_t value)
overloads the = operator.
Definition: i2c.cpp:81
I2CRegister16 & operator &=(uint8_t value)
overloads the compound &= operator.
const uint8_t * data
pointer to the write buffer
Definition: i2c_bus.h:31
I2CRegister16 & operator|=(uint8_t value)
overloads the compound |= operator.
Definition: i2c.cpp:90
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
This class is used to create I2CRegister16 objects that act as proxies to read/write internal registe...
Definition: i2c.h:88
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a specific register in the I²C device
Definition: i2c.cpp:25
I2CRegister & operator &=(uint8_t value)
overloads the compound &= operator.
uint8_t get() const
returns the register value
Definition: i2c.cpp:96
ErrorCode read_register16(uint16_t a_register, uint8_t *data, size_t len, bool stop=true)
reads an array of bytes from a specific register in the I²C device
Definition: i2c.cpp:17
I2CRegister & operator=(uint8_t value)
overloads the = operator.
Definition: i2c.cpp:60
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition: i2c_bus.h:11
bool read_bytes_16(uint8_t a_register, uint16_t *data, uint8_t len)
Definition: i2c.cpp:44