1#if defined(USE_ARDUINO) && !defined(USE_ESP32) && !defined(USE_LIBRETINY)
15static constexpr size_t I2C_MAX_LOG_BYTES = 32;
20#if defined(USE_ESP8266)
21 wire_ =
new TwoWire();
34 this->set_pins_and_clock_();
38 ESP_LOGV(TAG,
"Scanning bus for active devices");
43void ArduinoI2CBus::set_pins_and_clock_() {
52#if defined(USE_ESP8266)
64 ESP_LOGCONFIG(TAG,
"I2C Bus:");
71#if defined(USE_ESP8266)
72 ESP_LOGCONFIG(TAG,
" Timeout: %u us", this->
timeout_);
74 ESP_LOGCONFIG(TAG,
" Timeout: %u ms", this->
timeout_ / 1000);
77 switch (this->recovery_result_) {
79 ESP_LOGCONFIG(TAG,
" Recovery: bus successfully recovered");
82 ESP_LOGCONFIG(TAG,
" Recovery: failed, SCL is held low on the bus");
85 ESP_LOGCONFIG(TAG,
" Recovery: failed, SDA is held low on the bus");
92 uint8_t *read_buffer,
size_t read_count) {
93#if defined(USE_ESP8266)
94 this->set_pins_and_clock_();
97 ESP_LOGD(TAG,
"i2c bus not initialized!");
101 ESP_LOGV_HEX(TAG, write_buffer, write_count, I2C_MAX_LOG_BYTES,
"0x%02X TX %s",
address);
104 if (write_count != 0 || read_count == 0) {
108 if (write_count > WIRE_BUFFER_SIZE) {
109 ESP_LOGVV(TAG,
"TX of %zu bytes exceeds the %u byte Wire buffer", write_count, WIRE_BUFFER_SIZE);
113 this->
wire_->beginTransmission(address);
114 size_t ret = this->
wire_->write(write_buffer, write_count);
115 if (
ret != write_count) {
116 ESP_LOGV(TAG,
"TX failed");
119 status = this->
wire_->endTransmission(read_count == 0);
121 if (
status == 0 && read_count != 0) {
122 size_t ret2 = this->
wire_->requestFrom(address, read_count,
true);
123 if (ret2 != read_count) {
124 ESP_LOGVV(TAG,
"RX %u from %02X failed with error %u", read_count,
address, ret2);
127 for (
size_t j = 0; j != read_count; j++)
128 read_buffer[j] = this->
wire_->read();
134 ESP_LOGVV(TAG,
"TX failed: buffer not large enough");
138 ESP_LOGVV(TAG,
"TX failed: not acknowledged: %u",
status);
142 ESP_LOGVV(TAG,
"TX failed: timeout");
145 ESP_LOGVV(TAG,
"TX failed: unknown error %u",
status);
149#ifdef I2C_PORT_FREQUENCY_COUNT
165void ArduinoI2CBus::recover_() {
166 ESP_LOGI(TAG,
"Performing bus recovery");
172 const auto half_period_usec = 1000000 / 100000 / 2;
175 pinMode(this->
scl_pin_, INPUT_PULLUP);
181 if (digitalRead(this->
scl_pin_) == LOW) {
182 ESP_LOGE(TAG,
"Recovery failed: SCL is held LOW on the bus");
200 for (
auto i = 0; i < 9; i++) {
208 pinMode(this->
scl_pin_, INPUT_PULLUP);
219 while (wait-- && digitalRead(this->
scl_pin_) == LOW) {
223 if (digitalRead(this->
scl_pin_) == LOW) {
224 ESP_LOGE(TAG,
"Recovery failed: SCL is held LOW during clock pulse cycle");
232 pinMode(this->
sda_pin_, INPUT_PULLUP);
238 if (digitalRead(this->
sda_pin_) == LOW) {
239 ESP_LOGE(TAG,
"Recovery failed: SDA is held LOW after clock pulse cycle");
267 pinMode(this->
sda_pin_, INPUT_PULLUP);
void feed_wdt()
Feed the task watchdog.
void dump_config() override
ErrorCode switch_frequency(uint32_t frequency) override
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.
void dump_scan_results_()
Logs the scan results at config level, if the bus was scanned.
void i2c_scan_()
Scans the I2C bus for devices.
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
@ ERROR_OK
No error found during execution of method.
@ ERROR_TOO_LARGE
requested a transfer larger than buffers can hold
@ ERROR_TIMEOUT
timeout while waiting to receive bytes
@ ERROR_NOT_ACKNOWLEDGED
I2C bus acknowledgment not received.
@ ERROR_NOT_INITIALIZED
call method to a not initialized bus
@ ERROR_UNKNOWN
miscellaneous I2C error during execution
@ RECOVERY_FAILED_SDA_LOW
@ RECOVERY_FAILED_SCL_LOW
ESPHOME_LOG_TAG(TAG, "i2c")
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Application App
Global storage of Application pointer - only one Application can exist.