ESPHome 2026.3.0
Loading...
Searching...
No Matches
i2c_bus_arduino.cpp
Go to the documentation of this file.
1#if defined(USE_ARDUINO) && !defined(USE_ESP32)
2
3#include "i2c_bus_arduino.h"
4#include <Arduino.h>
5#include <cstring>
8#include "esphome/core/log.h"
9
10namespace esphome::i2c {
11
12static const char *const TAG = "i2c.arduino";
13
14// Maximum bytes to log in hex format (truncates larger transfers)
15static constexpr size_t I2C_MAX_LOG_BYTES = 32;
16
18 recover_();
19
20#if defined(USE_ESP8266)
21 wire_ = new TwoWire(); // NOLINT(cppcoreguidelines-owning-memory)
22#elif defined(USE_RP2040)
23 // Select Wire instance based on pin assignment, not definition order.
24 // I2C controller = (gpio / 2) % 2: even pairs (0-1,4-5,...) → I2C0, odd pairs (2-3,6-7,...) → I2C1
25 // RP2040 datasheet Table 2 (section 1.4.3): https://datasheets.raspberrypi.com/rp2040/rp2040-datasheet.pdf
26 // RP2350 datasheet Table 7 (section 9.4): https://datasheets.raspberrypi.com/rp2350/rp2350-datasheet.pdf
27 if ((this->sda_pin_ / 2) % 2 == 0) {
28 wire_ = &Wire;
29 } else {
30 wire_ = &Wire1;
31 }
32#endif
33
34 this->set_pins_and_clock_();
35
36 this->initialized_ = true;
37 if (this->scan_) {
38 ESP_LOGV(TAG, "Scanning bus for active devices");
39 this->i2c_scan_();
40 }
41}
42
43void ArduinoI2CBus::set_pins_and_clock_() {
44#ifdef USE_RP2040
45 wire_->setSDA(this->sda_pin_);
46 wire_->setSCL(this->scl_pin_);
47 wire_->begin();
48#else
49 wire_->begin(static_cast<int>(sda_pin_), static_cast<int>(scl_pin_));
50#endif
51 if (timeout_ > 0) { // if timeout specified in yaml
52#if defined(USE_ESP8266)
53 // https://github.com/esp8266/Arduino/blob/master/libraries/Wire/Wire.h
54 wire_->setClockStretchLimit(timeout_); // unit: us
55#elif defined(USE_RP2040)
56 // https://github.com/earlephilhower/ArduinoCore-API/blob/e37df85425e0ac020bfad226d927f9b00d2e0fb7/api/Stream.h
57 wire_->setTimeout(timeout_ / 1000); // unit: ms
58#endif
59 }
60 wire_->setClock(frequency_);
61}
62
64 ESP_LOGCONFIG(TAG, "I2C Bus:");
65 ESP_LOGCONFIG(TAG,
66 " SDA Pin: GPIO%u\n"
67 " SCL Pin: GPIO%u\n"
68 " Frequency: %u Hz",
69 this->sda_pin_, this->scl_pin_, this->frequency_);
70 if (timeout_ > 0) {
71#if defined(USE_ESP8266)
72 ESP_LOGCONFIG(TAG, " Timeout: %u us", this->timeout_);
73#elif defined(USE_RP2040)
74 ESP_LOGCONFIG(TAG, " Timeout: %u ms", this->timeout_ / 1000);
75#endif
76 }
77 switch (this->recovery_result_) {
79 ESP_LOGCONFIG(TAG, " Recovery: bus successfully recovered");
80 break;
82 ESP_LOGCONFIG(TAG, " Recovery: failed, SCL is held low on the bus");
83 break;
85 ESP_LOGCONFIG(TAG, " Recovery: failed, SDA is held low on the bus");
86 break;
87 }
88 if (this->scan_) {
89 ESP_LOGI(TAG, "Results from bus scan:");
90 if (scan_results_.empty()) {
91 ESP_LOGI(TAG, "Found no devices");
92 } else {
93 for (const auto &s : scan_results_) {
94 if (s.second) {
95 ESP_LOGI(TAG, "Found device at address 0x%02X", s.first);
96 } else {
97 ESP_LOGE(TAG, "Unknown error at address 0x%02X", s.first);
98 }
99 }
100 }
101 }
102}
103
104ErrorCode ArduinoI2CBus::write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count,
105 uint8_t *read_buffer, size_t read_count) {
106#if defined(USE_ESP8266)
107 this->set_pins_and_clock_(); // reconfigure Wire global state in case there are multiple instances
108#endif
109 if (!initialized_) {
110 ESP_LOGD(TAG, "i2c bus not initialized!");
112 }
113
114#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
115 char hex_buf[format_hex_pretty_size(I2C_MAX_LOG_BYTES)];
116 ESP_LOGV(TAG, "0x%02X TX %s", address, format_hex_pretty_to(hex_buf, write_buffer, write_count));
117#endif
118
119 uint8_t status = 0;
120 if (write_count != 0 || read_count == 0) {
121 wire_->beginTransmission(address);
122 size_t ret = wire_->write(write_buffer, write_count);
123 if (ret != write_count) {
124 ESP_LOGV(TAG, "TX failed");
125 return ERROR_UNKNOWN;
126 }
127 status = wire_->endTransmission(read_count == 0);
128 }
129 if (status == 0 && read_count != 0) {
130 size_t ret2 = wire_->requestFrom(address, read_count, true);
131 if (ret2 != read_count) {
132 ESP_LOGVV(TAG, "RX %u from %02X failed with error %u", read_count, address, ret2);
133 return ERROR_TIMEOUT;
134 }
135 for (size_t j = 0; j != read_count; j++)
136 read_buffer[j] = wire_->read();
137 }
138 // Avoid switch to prevent compiler-generated lookup table in RAM on ESP8266
139 if (status == 0)
140 return ERROR_OK;
141 if (status == 1) {
142 ESP_LOGVV(TAG, "TX failed: buffer not large enough");
143 return ERROR_UNKNOWN;
144 }
145 if (status == 2 || status == 3) {
146 ESP_LOGVV(TAG, "TX failed: not acknowledged: %u", status);
148 }
149 if (status == 5) {
150 ESP_LOGVV(TAG, "TX failed: timeout");
151 return ERROR_UNKNOWN;
152 }
153 ESP_LOGVV(TAG, "TX failed: unknown error %u", status);
154 return ERROR_UNKNOWN;
155}
156
160void ArduinoI2CBus::recover_() {
161 ESP_LOGI(TAG, "Performing bus recovery");
162
163 // For the upcoming operations, target for a 100kHz toggle frequency.
164 // This is the maximum frequency for I2C running in standard-mode.
165 // The actual frequency will be lower, because of the additional
166 // function calls that are done, but that is no problem.
167 const auto half_period_usec = 1000000 / 100000 / 2;
168
169 // Activate input and pull up resistor for the SCL pin.
170 pinMode(scl_pin_, INPUT_PULLUP); // NOLINT
171
172 // This should make the signal on the line HIGH. If SCL is pulled low
173 // on the I2C bus however, then some device is interfering with the SCL
174 // line. In that case, the I2C bus cannot be recovered.
175 delayMicroseconds(half_period_usec);
176 if (digitalRead(scl_pin_) == LOW) { // NOLINT
177 ESP_LOGE(TAG, "Recovery failed: SCL is held LOW on the bus");
178 recovery_result_ = RECOVERY_FAILED_SCL_LOW;
179 return;
180 }
181
182 // From the specification:
183 // "If the data line (SDA) is stuck LOW, send nine clock pulses. The
184 // device that held the bus LOW should release it sometime within
185 // those nine clocks."
186 // We don't really have to detect if SDA is stuck low. We'll simply send
187 // nine clock pulses here, just in case SDA is stuck. Actual checks on
188 // the SDA line status will be done after the clock pulses.
189
190 // Make sure that switching to output mode will make SCL low, just in
191 // case other code has setup the pin for a HIGH signal.
192 digitalWrite(scl_pin_, LOW); // NOLINT
193
194 delayMicroseconds(half_period_usec);
195 for (auto i = 0; i < 9; i++) {
196 // Release pull up resistor and switch to output to make the signal LOW.
197 pinMode(scl_pin_, INPUT); // NOLINT
198 pinMode(scl_pin_, OUTPUT); // NOLINT
199 delayMicroseconds(half_period_usec);
200
201 // Release output and activate pull up resistor to make the signal HIGH.
202 pinMode(scl_pin_, INPUT); // NOLINT
203 pinMode(scl_pin_, INPUT_PULLUP); // NOLINT
204 delayMicroseconds(half_period_usec);
205
206 // When SCL is kept LOW at this point, we might be looking at a device
207 // that applies clock stretching. Wait for the release of the SCL line,
208 // but not forever. There is no specification for the maximum allowed
209 // time. We yield and reset the WDT, so as to avoid triggering reset.
210 // No point in trying to recover the bus by forcing a uC reset. Bus
211 // should recover in a few ms or less else not likely to recovery at
212 // all.
213 auto wait = 250;
214 while (wait-- && digitalRead(scl_pin_) == LOW) { // NOLINT
215 App.feed_wdt();
216 delayMicroseconds(half_period_usec * 2);
217 }
218 if (digitalRead(scl_pin_) == LOW) { // NOLINT
219 ESP_LOGE(TAG, "Recovery failed: SCL is held LOW during clock pulse cycle");
220 recovery_result_ = RECOVERY_FAILED_SCL_LOW;
221 return;
222 }
223 }
224
225 // Activate input and pull resistor for the SDA pin, so we can verify
226 // that SDA is pulled HIGH in the following step.
227 pinMode(sda_pin_, INPUT_PULLUP); // NOLINT
228 digitalWrite(sda_pin_, LOW); // NOLINT
229
230 // By now, any stuck device ought to have sent all remaining bits of its
231 // transaction, meaning that it should have freed up the SDA line, resulting
232 // in SDA being pulled up.
233 if (digitalRead(sda_pin_) == LOW) { // NOLINT
234 ESP_LOGE(TAG, "Recovery failed: SDA is held LOW after clock pulse cycle");
235 recovery_result_ = RECOVERY_FAILED_SDA_LOW;
236 return;
237 }
238
239 // From the specification:
240 // "I2C-bus compatible devices must reset their bus logic on receipt of
241 // a START or repeated START condition such that they all anticipate
242 // the sending of a target address, even if these START conditions are
243 // not positioned according to the proper format."
244 // While the 9 clock pulses from above might have drained all bits of a
245 // single byte within a transaction, a device might have more bytes to
246 // transmit. So here we'll generate a START condition to snap the device
247 // out of this state.
248 // SCL and SDA are already high at this point, so we can generate a START
249 // condition by making the SDA signal LOW.
250 delayMicroseconds(half_period_usec);
251 pinMode(sda_pin_, INPUT); // NOLINT
252 pinMode(sda_pin_, OUTPUT); // NOLINT
253
254 // From the specification:
255 // "A START condition immediately followed by a STOP condition (void
256 // message) is an illegal format. Many devices however are designed to
257 // operate properly under this condition."
258 // Finally, we'll bring the I2C bus into a starting state by generating
259 // a STOP condition.
260 delayMicroseconds(half_period_usec);
261 pinMode(sda_pin_, INPUT); // NOLINT
262 pinMode(sda_pin_, INPUT_PULLUP); // NOLINT
263
264 recovery_result_ = RECOVERY_COMPLETED;
265}
266} // namespace esphome::i2c
267
268#endif // defined(USE_ARDUINO) && !defined(USE_ESP32)
uint8_t address
Definition bl0906.h:4
uint8_t status
Definition bl0942.h:8
void feed_wdt(uint32_t time=0)
ErrorCode write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count, uint8_t *read_buffer, size_t read_count) override
bool scan_
Should we scan ? Can be set in the yaml.
Definition i2c_bus.h:61
std::vector< std::pair< uint8_t, bool > > scan_results_
array containing scan results
Definition i2c_bus.h:60
void i2c_scan_()
Scans the I2C bus for devices.
Definition i2c.cpp:12
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:12
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14
@ ERROR_TIMEOUT
timeout while waiting to receive bytes
Definition i2c_bus.h:17
@ ERROR_NOT_ACKNOWLEDGED
I2C bus acknowledgment not received.
Definition i2c_bus.h:16
@ ERROR_NOT_INITIALIZED
call method to a not initialized bus
Definition i2c_bus.h:18
@ ERROR_UNKNOWN
miscellaneous I2C error during execution
Definition i2c_bus.h:20
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition core.cpp:30
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
Definition helpers.cpp:353
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".
Definition helpers.h:1200
Application App
Global storage of Application pointer - only one Application can exist.