4#include <zephyr/drivers/i2c.h>
9static const char *
const TAG =
"i2c.zephyr";
12 if (!device_is_ready(this->
i2c_dev_)) {
13 ESP_LOGE(TAG,
"I2C dev is not ready.");
20 ESP_LOGE(TAG,
"I2C: Failed to configure device");
28 ESP_LOGV(TAG,
"Scanning I2C bus for active devices...");
34 auto get_speed = [](uint32_t dev_config) {
35 switch (I2C_SPEED_GET(dev_config)) {
36 case I2C_SPEED_STANDARD:
40 case I2C_SPEED_FAST_PLUS:
60 ESP_LOGCONFIG(TAG,
" Recovery: bus successfully recovered");
63 ESP_LOGI(TAG,
"Results from I2C bus scan:");
65 ESP_LOGI(TAG,
"Found no I2C devices!");
69 ESP_LOGI(TAG,
"Found I2C device at address 0x%02X", s.first);
71 ESP_LOGE(TAG,
"Unknown error at address 0x%02X", s.first);
79 uint8_t *read_buffer,
size_t read_count) {
80 if (!device_is_ready(this->
i2c_dev_)) {
88 if (read_count == 0 && write_count == 0) {
91 msgs[cnt++].flags = I2C_MSG_WRITE;
95 msgs[cnt].buf =
const_cast<uint8_t *
>(write_buffer);
96 msgs[cnt].len = write_count;
97 msgs[cnt++].flags = I2C_MSG_WRITE;
100 msgs[cnt].buf =
const_cast<uint8_t *
>(read_buffer);
101 msgs[cnt].len = read_count;
102 msgs[cnt++].flags = I2C_MSG_READ | I2C_MSG_RESTART;
106 msgs[cnt - 1].flags |= I2C_MSG_STOP;
115 ESP_LOGE(TAG,
"i2c transfer error %d",
err);
125 this->
dev_config_ |= I2C_SPEED_SET(I2C_SPEED_FAST);
127 this->
dev_config_ |= I2C_SPEED_SET(I2C_SPEED_STANDARD);
virtual void mark_failed()
Mark this component as failed.
bool scan_
Should we scan ? Can be set in the yaml.
std::vector< std::pair< uint8_t, bool > > scan_results_
array containing scan results
void dump_config() override
void set_frequency(uint32_t frequency)
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
Error codes returned by I2CBus and I2CDevice methods.
@ 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