ESPHome 2025.8.2
Loading...
Searching...
No Matches
i2c.cpp
Go to the documentation of this file.
1#include "i2c.h"
2
4#include "esphome/core/log.h"
5#include <memory>
6
7namespace esphome {
8namespace i2c {
9
10static const char *const TAG = "i2c";
11
12void I2CBus::i2c_scan_() {
13 // suppress logs from the IDF I2C library during the scan
14#if defined(USE_ESP32) && defined(USE_LOGGER)
15 auto previous = esp_log_level_get("*");
16 esp_log_level_set("*", ESP_LOG_NONE);
17#endif
18
19 for (uint8_t address = 8; address != 120; address++) {
20 auto err = write_readv(address, nullptr, 0, nullptr, 0);
21 if (err == ERROR_OK) {
22 scan_results_.emplace_back(address, true);
23 } else if (err == ERROR_UNKNOWN) {
24 scan_results_.emplace_back(address, false);
25 }
26 }
27#if defined(USE_ESP32) && defined(USE_LOGGER)
28 esp_log_level_set("*", previous);
29#endif
30}
31
32ErrorCode I2CDevice::read_register(uint8_t a_register, uint8_t *data, size_t len) {
33 return bus_->write_readv(this->address_, &a_register, 1, data, len);
34}
35
36ErrorCode I2CDevice::read_register16(uint16_t a_register, uint8_t *data, size_t len) {
37 a_register = convert_big_endian(a_register);
38 return bus_->write_readv(this->address_, reinterpret_cast<const uint8_t *>(&a_register), 2, data, len);
39}
40
41ErrorCode I2CDevice::write_register(uint8_t a_register, const uint8_t *data, size_t len) const {
42 std::vector<uint8_t> v{};
43 v.push_back(a_register);
44 v.insert(v.end(), data, data + len);
45 return bus_->write_readv(this->address_, v.data(), v.size(), nullptr, 0);
46}
47
48ErrorCode I2CDevice::write_register16(uint16_t a_register, const uint8_t *data, size_t len) const {
49 std::vector<uint8_t> v(len + 2);
50 v.push_back(a_register >> 8);
51 v.push_back(a_register);
52 v.insert(v.end(), data, data + len);
53 return bus_->write_readv(this->address_, v.data(), v.size(), nullptr, 0);
54}
55
56bool I2CDevice::read_bytes_16(uint8_t a_register, uint16_t *data, uint8_t len) {
57 if (read_register(a_register, reinterpret_cast<uint8_t *>(data), len * 2) != ERROR_OK)
58 return false;
59 for (size_t i = 0; i < len; i++)
60 data[i] = i2ctohs(data[i]);
61 return true;
62}
63
64bool I2CDevice::write_bytes_16(uint8_t a_register, const uint16_t *data, uint8_t len) const {
65 // we have to copy in order to be able to change byte order
66 std::unique_ptr<uint16_t[]> temp{new uint16_t[len]};
67 for (size_t i = 0; i < len; i++)
68 temp[i] = htoi2cs(data[i]);
69 return write_register(a_register, reinterpret_cast<const uint8_t *>(temp.get()), len * 2) == ERROR_OK;
70}
71
73 this->parent_->write_register(this->register_, &value, 1);
74 return *this;
75}
77 value &= get();
78 this->parent_->write_register(this->register_, &value, 1);
79 return *this;
80}
82 value |= get();
83 this->parent_->write_register(this->register_, &value, 1);
84 return *this;
85}
86
87uint8_t I2CRegister::get() const {
88 uint8_t value = 0x00;
89 this->parent_->read_register(this->register_, &value, 1);
90 return value;
91}
92
94 this->parent_->write_register16(this->register_, &value, 1);
95 return *this;
96}
98 value &= get();
99 this->parent_->write_register16(this->register_, &value, 1);
100 return *this;
101}
103 value |= get();
104 this->parent_->write_register16(this->register_, &value, 1);
105 return *this;
106}
107
108uint8_t I2CRegister16::get() const {
109 uint8_t value = 0x00;
110 this->parent_->read_register16(this->register_, &value, 1);
111 return value;
112}
113
114} // namespace i2c
115} // namespace esphome
uint8_t address
Definition bl0906.h:4
virtual ErrorCode write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count, uint8_t *read_buffer, size_t read_count)=0
This virtual method writes bytes to an I2CBus from an array, then reads bytes into an array of ReadBu...
std::vector< std::pair< uint8_t, bool > > scan_results_
array containing scan results
Definition i2c_bus.h:105
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) const
writes an array of bytes to a specific register in the I²C device
Definition i2c.cpp:41
ErrorCode write_register16(uint16_t a_register, const uint8_t *data, size_t len) const
write an array of bytes to a specific register in the I²C device
Definition i2c.cpp:48
bool write_bytes_16(uint8_t a_register, const uint16_t *data, uint8_t len) const
Definition i2c.cpp:64
I2CBus * bus_
pointer to I2CBus instance
Definition i2c.h:303
uint8_t address_
store the address of the device on the bus
Definition i2c.h:302
ErrorCode read_register16(uint16_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:36
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:32
bool read_bytes_16(uint8_t a_register, uint16_t *data, uint8_t len)
Definition i2c.cpp:56
uint8_t size_t len
Definition i2c.h:273
This class is used to create I2CRegister16 objects that act as proxies to read/write internal registe...
Definition i2c.h:88
uint16_t register_
the internal 16 bits address of the register
Definition i2c.h:123
I2CRegister16 & operator&=(uint8_t value)
overloads the compound &= operator.
Definition i2c.cpp:97
I2CRegister16 & operator|=(uint8_t value)
overloads the compound |= operator.
Definition i2c.cpp:102
I2CDevice * parent_
I2CDevice object pointer.
Definition i2c.h:122
uint8_t get() const
returns the register value
Definition i2c.cpp:108
I2CRegister16 & operator=(uint8_t value)
overloads the = operator.
Definition i2c.cpp:93
This class is used to create I2CRegister objects that act as proxies to read/write internal registers...
Definition i2c.h:33
I2CRegister & operator|=(uint8_t value)
overloads the compound |= operator.
Definition i2c.cpp:81
uint8_t get() const
returns the register value
Definition i2c.cpp:87
I2CRegister & operator&=(uint8_t value)
overloads the compound &= operator.
Definition i2c.cpp:76
I2CDevice * parent_
I2CDevice object pointer.
Definition i2c.h:67
uint8_t register_
the internal address of the register
Definition i2c.h:68
I2CRegister & operator=(uint8_t value)
overloads the = operator.
Definition i2c.cpp:72
uint16_t i2ctohs(uint16_t i2cshort)
Definition i2c.h:128
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:14
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:16
@ ERROR_UNKNOWN
miscellaneous I2C error during execution
Definition i2c_bus.h:22
uint16_t htoi2cs(uint16_t hostshort)
Definition i2c.h:129
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
constexpr T convert_big_endian(T val)
Convert a value between host byte order and big endian (most significant byte first) order.
Definition helpers.h:229
std::string size_t len
Definition helpers.h:279