ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
i2c.cpp
Go to the documentation of this file.
1#include "i2c.h"
2
4#include "esphome/core/hal.h"
5#include "esphome/core/log.h"
6
7namespace esphome::i2c {
8
9ESPHOME_LOG_TAG(TAG, "i2c");
10
12 for (uint8_t address = 8; address != 120; address++) {
13 auto err = write_readv(address, nullptr, 0, nullptr, 0);
14 if (err == ERROR_OK) {
15 scan_results_.emplace_back(address, true);
16 } else if (err == ERROR_UNKNOWN) {
17 scan_results_.emplace_back(address, false);
18 }
19 // it takes 16sec to scan on nrf52. It prevents board reset.
21 }
22}
23
25 if (!this->scan_)
26 return;
27 ESP_LOGCONFIG(TAG, "Results from bus scan:");
28 if (this->scan_results_.empty()) {
29 ESP_LOGCONFIG(TAG, "Found no devices");
30 return;
31 }
32 for (const auto &s : this->scan_results_) {
33 if (s.second) {
34 ESP_LOGCONFIG(TAG, "Found device at address 0x%02X", s.first);
35 } else {
36 ESP_LOGE(TAG, "Unknown error at address 0x%02X", s.first);
37 }
38 }
39}
40
41void log_wrong_chip_id(uint8_t id, uint8_t expected) {
42 ESP_LOGE(TAG, "Wrong chip ID 0x%02X, expected 0x%02X", id, expected);
43}
44
45ErrorCode I2CDevice::read_register(uint8_t a_register, uint8_t *data, size_t len) {
46 return bus_->write_readv(this->address_, &a_register, 1, data, len);
47}
48
49ErrorCode I2CDevice::read_register16(uint16_t a_register, uint8_t *data, size_t len) {
50 a_register = convert_big_endian(a_register);
51 return bus_->write_readv(this->address_, reinterpret_cast<const uint8_t *>(&a_register), 2, data, len);
52}
53
54ErrorCode I2CDevice::write_register(uint8_t a_register, const uint8_t *data, size_t len) const {
55 SmallBufferWithHeapFallback<17> buffer_alloc(len + 1); // Most I2C writes are <= 16 bytes
56 uint8_t *buffer = buffer_alloc.get();
57
58 buffer[0] = a_register;
59 std::copy(data, data + len, buffer + 1);
60 return this->bus_->write_readv(this->address_, buffer, len + 1, nullptr, 0);
61}
62
63ErrorCode I2CDevice::write_register16(uint16_t a_register, const uint8_t *data, size_t len) const {
64 SmallBufferWithHeapFallback<18> buffer_alloc(len + 2); // Most I2C writes are <= 16 bytes + 2 for register
65 uint8_t *buffer = buffer_alloc.get();
66
67 buffer[0] = a_register >> 8;
68 buffer[1] = a_register;
69 std::copy(data, data + len, buffer + 2);
70 return this->bus_->write_readv(this->address_, buffer, len + 2, nullptr, 0);
71}
72
73bool I2CDevice::write_registers(const I2CRegValue *table, size_t count) const {
74 for (size_t i = 0; i < count; i++) {
75 I2CRegValue entry;
76 progmem_memcpy(&entry, &table[i], sizeof(entry));
77 if (this->bus_->write_readv(this->address_, reinterpret_cast<const uint8_t *>(&entry), sizeof(entry), nullptr, 0) !=
78 ERROR_OK) {
79 ESP_LOGW(TAG, "Writing register 0x%02X (table entry %u) to 0x%02X failed", entry.reg, static_cast<unsigned>(i),
80 this->address_);
81 return false;
82 }
83 }
84 return true;
85}
86
87bool I2CDevice::read_bytes_16(uint8_t a_register, uint16_t *data, uint8_t len) {
88 if (read_register(a_register, reinterpret_cast<uint8_t *>(data), len * 2) != ERROR_OK)
89 return false;
90 for (size_t i = 0; i < len; i++)
91 data[i] = i2ctohs(data[i]);
92 return true;
93}
94
95bool I2CDevice::update_register(uint8_t a_register, uint8_t mask, uint8_t value) {
96 uint8_t data;
97 if (!this->read_byte(a_register, &data))
98 return false;
99 const uint8_t updated = (data & ~mask) | (value & mask);
100 ESP_LOGV(TAG, "Register 0x%02X: 0x%02X -> 0x%02X", a_register, data, updated);
101 return this->write_byte(a_register, updated);
102}
103
104bool I2CDevice::write_bytes_16(uint8_t a_register, const uint16_t *data, uint8_t len) const {
105 const size_t size = 1 + len * 2;
106 SmallBufferWithHeapFallback<17> buffer_alloc(size); // Register plus up to 8 words
107 uint8_t *buffer = buffer_alloc.get();
108
109 buffer[0] = a_register;
110 for (size_t i = 0; i < len; i++) {
111 const uint16_t word = data[i];
112 buffer[1 + i * 2] = word >> 8;
113 buffer[2 + i * 2] = word;
114 }
115 return this->bus_->write_readv(this->address_, buffer, size, nullptr, 0) == ERROR_OK;
116}
117
118bool I2CDevice::write_byte_16(uint8_t a_register, uint16_t data) const {
119 const uint8_t buffer[3] = {a_register, static_cast<uint8_t>(data >> 8), static_cast<uint8_t>(data)};
120 return this->bus_->write_readv(this->address_, buffer, sizeof(buffer), nullptr, 0) == ERROR_OK;
121}
122
123bool I2CDevice::write_byte_16_le(uint8_t a_register, uint16_t data) const {
124 const uint8_t buffer[3] = {a_register, static_cast<uint8_t>(data), static_cast<uint8_t>(data >> 8)};
125 return this->bus_->write_readv(this->address_, buffer, sizeof(buffer), nullptr, 0) == ERROR_OK;
126}
127
129 this->parent_->write_register(this->register_, &value, 1);
130 return *this;
131}
133 value &= get();
134 this->parent_->write_register(this->register_, &value, 1);
135 return *this;
136}
138 value |= get();
139 this->parent_->write_register(this->register_, &value, 1);
140 return *this;
141}
142
143uint8_t I2CRegister::get() const {
144 uint8_t value = 0x00;
145 this->parent_->read_register(this->register_, &value, 1);
146 return value;
147}
148
150 this->parent_->write_register16(this->register_, &value, 1);
151 return *this;
152}
154 value &= get();
155 this->parent_->write_register16(this->register_, &value, 1);
156 return *this;
157}
159 value |= get();
160 this->parent_->write_register16(this->register_, &value, 1);
161 return *this;
162}
163
164uint8_t I2CRegister16::get() const {
165 uint8_t value = 0x00;
166 this->parent_->read_register16(this->register_, &value, 1);
167 return value;
168}
169
170} // namespace esphome::i2c
Helper class for efficient buffer allocation - uses stack for small sizes, heap for large This is use...
Definition helpers.h:872
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...
bool scan_
Should we scan ? Can be set in the yaml.
Definition i2c_bus.h:73
std::vector< std::pair< uint8_t, bool > > scan_results_
array containing scan results
Definition i2c_bus.h:72
void dump_scan_results_()
Logs the scan results at config level, if the bus was scanned.
Definition i2c.cpp:24
void i2c_scan_()
Scans the I2C bus for devices.
Definition i2c.cpp:11
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:54
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:63
bool write_byte_16_le(uint8_t a_register, uint16_t data) const
Write a 16-bit value least significant byte first.
Definition i2c.cpp:123
bool write_bytes_16(uint8_t a_register, const uint16_t *data, uint8_t len) const
Definition i2c.cpp:104
I2CBus * bus_
pointer to I2CBus instance
Definition i2c.h:325
uint8_t address_
store the address of the device on the bus
Definition i2c.h:327
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:272
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:49
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:45
bool read_bytes_16(uint8_t a_register, uint16_t *data, uint8_t len)
Definition i2c.cpp:87
bool write_byte_16(uint8_t a_register, uint16_t data) const
Definition i2c.cpp:118
bool update_register(uint8_t a_register, uint8_t mask, uint8_t value)
Read a register, replace the bits in mask with those of value and write it back.
Definition i2c.cpp:95
bool write_registers(const I2CRegValue *table, size_t count) const
Write each register and value of table in order, stopping at the first failure.
Definition i2c.cpp:73
This class is used to create I2CRegister16 objects that act as proxies to read/write internal registe...
Definition i2c.h:99
uint16_t register_
the internal 16 bits address of the register
Definition i2c.h:134
I2CRegister16 & operator&=(uint8_t value)
overloads the compound &= operator.
Definition i2c.cpp:153
I2CRegister16 & operator|=(uint8_t value)
overloads the compound |= operator.
Definition i2c.cpp:158
I2CDevice * parent_
I2CDevice object pointer.
Definition i2c.h:133
uint8_t get() const
returns the register value
Definition i2c.cpp:164
I2CRegister16 & operator=(uint8_t value)
overloads the = operator.
Definition i2c.cpp:149
This class is used to create I2CRegister objects that act as proxies to read/write internal registers...
Definition i2c.h:44
I2CRegister & operator|=(uint8_t value)
overloads the compound |= operator.
Definition i2c.cpp:137
uint8_t get() const
returns the register value
Definition i2c.cpp:143
I2CRegister & operator&=(uint8_t value)
overloads the compound &= operator.
Definition i2c.cpp:132
I2CDevice * parent_
I2CDevice object pointer.
Definition i2c.h:78
uint8_t register_
the internal address of the register
Definition i2c.h:79
I2CRegister & operator=(uint8_t value)
overloads the = operator.
Definition i2c.cpp:128
unsigned count
Definition gif.cpp:51
uint32_t buffer
Definition gif.cpp:50
uint16_t i2ctohs(uint16_t i2cshort)
Definition i2c.h:139
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:13
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
@ ERROR_UNKNOWN
miscellaneous I2C error during execution
Definition i2c_bus.h:21
void log_wrong_chip_id(uint8_t id, uint8_t expected)
Out of line so each check_chip_id() caller only pays for a call.
Definition i2c.cpp:41
ESPHOME_LOG_TAG(TAG, "i2c")
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:1176
void progmem_memcpy(void *dst, const void *src, size_t len)
Definition hal.h:56
uint32_t value
Definition hal.cpp:41
const void size_t len
Definition hal.h:68
uint16_t size
Definition helpers.cpp:34
void arch_feed_wdt()
Definition hal.cpp:115
One register write for I2CDevice::write_registers(); tables of these may live in PROGMEM.
Definition i2c.h:18