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 uint8_t *read_buffer,
size_t read_count) {
203 ESP_LOGD(TAG,
"i2c bus not initialized!");
212 ESP_LOGV_HEX(TAG, write_buffer, write_count, I2C_MAX_LOG_BYTES,
"0x%02X TX %s",
address);
218 if (write_count != 0 || read_count == 0) {
222 for (
size_t i = 0; err ==
ERROR_OK && i < write_count; i++) {
224 ESP_LOGVV(TAG,
"TX of byte %u to 0x%02X not acknowledged", (
unsigned) i,
address);
229 if (err ==
ERROR_OK && read_count != 0) {
230 if (write_count != 0) {
237 read_buffer[i] = this->
read_byte_(i + 1 != read_count);
243 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 for devices.
InternalGPIOPin * sda_pin_
ErrorCode write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count, uint8_t *read_buffer, size_t read_count) override
ErrorCode switch_frequency(uint32_t frequency) override
uint8_t read_byte_(bool ack)
void update_half_period_()
void dump_config() override
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()