13static constexpr size_t I2C_MAX_LOG_BYTES = 32;
15static constexpr uint8_t RECOVERY_CLOCK_PULSES = 9;
32 ESP_LOGE(TAG,
"SDA or SCL is held low, the bus cannot be used");
38 ESP_LOGV(TAG,
"Scanning bus for active devices");
44 ESP_LOGCONFIG(TAG,
"I2C Bus (software):");
45 LOG_PIN(
" SDA Pin: ", this->
sda_pin_);
46 LOG_PIN(
" SCL Pin: ", this->
scl_pin_);
48 " Frequency: %" PRIu32
" Hz\n"
49 " Clock stretch timeout: %" PRIu32
" us",
52 ESP_LOGCONFIG(TAG,
" Recovery: bus successfully recovered");
62#ifdef I2C_PORT_FREQUENCY_COUNT
98 ESP_LOGW(TAG,
"Bus recovery failed, a line is held low");
154 for (uint8_t mask = 0x80; mask != 0 && !this->
timed_out_; mask >>= 1) {
180 for (uint8_t i = 0; i < 8 && !this->
timed_out_; i++) {
201 const uint8_t *write_buffer,
size_t write_count, uint8_t *read_buffer,
204 ESP_LOGD(TAG,
"i2c bus not initialized!");
214 ESP_LOGV_HEX(TAG, prefix, prefix_len, I2C_MAX_LOG_BYTES,
"0x%02X TX prefix %s",
address);
215 ESP_LOGV_HEX(TAG, write_buffer, write_count, I2C_MAX_LOG_BYTES,
"0x%02X TX %s",
address);
221 const size_t total = prefix_len + write_count;
222 if (total != 0 || read_count == 0) {
226 for (
size_t i = 0; err ==
ERROR_OK && i < total; i++) {
227 if (!this->
write_byte_(i < prefix_len ? prefix[i] : write_buffer[i - prefix_len])) {
228 ESP_LOGVV(TAG,
"TX of byte %u to 0x%02X not acknowledged", (
unsigned) i,
address);
233 if (err ==
ERROR_OK && read_count != 0) {
241 read_buffer[i] = this->
read_byte_(i + 1 != read_count);
247 ESP_LOGVV(TAG,
"Bus line held low during transfer to 0x%02X for more than %" PRIu32
" us",
address, this->
timeout_);
void mark_failed()
Mark this component as failed.
bool digital_read() override
void pin_mode(gpio::Flags flags) override
void digital_write(bool value) 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 and keeps each address that answered (ERROR_OK) or returned ERROR_UNKNOWN.
InternalGPIOPin * sda_pin_
ErrorCode switch_frequency(uint32_t frequency) override
uint8_t read_byte_(bool ack)
void update_half_period_()
void dump_config() override
ErrorCode transfer_(uint8_t address, const uint8_t *prefix, size_t prefix_len, const uint8_t *write_buffer, size_t write_count, uint8_t *read_buffer, size_t read_count)
Writes prefix then write_buffer in one transaction, then reads.
InternalGPIOPin * scl_pin_
bool write_byte_(uint8_t data)
Shift out one byte, returns true if the device acknowledged it.
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
@ ERROR_OK
No error found during execution of method.
@ ERROR_INVALID_ARGUMENT
method called invalid argument(s)
@ 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
ESPHOME_LOG_TAG(TAG, "i2c")
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
uint32_t IRAM_ATTR HOT micros()