mirror of
https://github.com/esphome/esphome.git
synced 2026-11-19 03:25:57 +00:00
Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ebc9807ffe | ||
|
|
49a6f7133f | ||
|
|
c20c445188 | ||
|
|
ca6cfc6469 | ||
|
|
0f90578733 | ||
|
|
f7eb1c811e | ||
|
|
8e95c9b0a6 | ||
|
|
c09d677ed2 | ||
|
|
b6d5777876 | ||
|
|
b59e822cda |
No files matched your search
@@ -5,6 +5,8 @@
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
namespace esphome::i2c {
|
||||
|
||||
ESPHOME_LOG_TAG(TAG, "i2c");
|
||||
@@ -52,22 +54,26 @@ ErrorCode I2CDevice::read_register16(uint16_t a_register, uint8_t *data, size_t
|
||||
return bus_->write_readv(this->address_, reinterpret_cast<const uint8_t *>(&a_register), 2, data, len);
|
||||
}
|
||||
|
||||
ErrorCode I2CDevice::write_register(uint8_t a_register, const uint8_t *data, size_t len) const {
|
||||
SmallBufferWithHeapFallback<19> buffer_alloc(len + 1); // Up to 18 data bytes stay on the stack
|
||||
ErrorCode I2CBus::write_prefixed(uint8_t address, const uint8_t *prefix, size_t prefix_len, const uint8_t *data,
|
||||
size_t len) {
|
||||
SmallBufferWithHeapFallback<20> buffer_alloc(prefix_len + len);
|
||||
uint8_t *buffer = buffer_alloc.get();
|
||||
// memcpy needs valid pointers even for a zero length
|
||||
if (prefix_len != 0)
|
||||
std::memcpy(buffer, prefix, prefix_len);
|
||||
if (len != 0)
|
||||
std::memcpy(buffer + prefix_len, data, len);
|
||||
return this->write_readv(address, buffer, prefix_len + len, nullptr, 0);
|
||||
}
|
||||
|
||||
buffer[0] = a_register;
|
||||
std::copy(data, data + len, buffer + 1);
|
||||
return this->bus_->write_readv(this->address_, buffer, len + 1, nullptr, 0);
|
||||
ErrorCode I2CDevice::write_register(uint8_t a_register, const uint8_t *data, size_t len) const {
|
||||
return this->bus_->write_prefixed(this->address_, &a_register, 1, data, len);
|
||||
}
|
||||
|
||||
ErrorCode I2CDevice::write_register16(uint16_t a_register, const uint8_t *data, size_t len) const {
|
||||
SmallBufferWithHeapFallback<20> buffer_alloc(len + 2); // Up to 18 data bytes stay on the stack
|
||||
uint8_t *buffer = buffer_alloc.get();
|
||||
|
||||
put_be16(buffer, a_register);
|
||||
std::copy(data, data + len, buffer + 2);
|
||||
return this->bus_->write_readv(this->address_, buffer, len + 2, nullptr, 0);
|
||||
uint8_t reg[2];
|
||||
put_be16(reg, a_register);
|
||||
return this->bus_->write_prefixed(this->address_, reg, sizeof(reg), data, len);
|
||||
}
|
||||
|
||||
bool I2CDevice::write_registers(const I2CRegValue *table, size_t count) const {
|
||||
|
||||
@@ -44,6 +44,12 @@ class I2CBus {
|
||||
virtual ErrorCode write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count, uint8_t *read_buffer,
|
||||
size_t read_count) = 0;
|
||||
|
||||
/// @brief Writes `prefix` then `data` in one transaction, as for a register address followed by its data.
|
||||
/// The built-in buses send the two parts without copying them; the default joins them in a buffer for
|
||||
/// write_readv(), on the stack for up to 20 bytes.
|
||||
virtual ErrorCode write_prefixed(uint8_t address, const uint8_t *prefix, size_t prefix_len, const uint8_t *data,
|
||||
size_t len);
|
||||
|
||||
// Legacy functions for compatibility
|
||||
|
||||
ErrorCode read(uint8_t address, uint8_t *buffer, size_t len) {
|
||||
|
||||
@@ -84,8 +84,9 @@ void ArduinoI2CBus::dump_config() {
|
||||
this->dump_scan_results_();
|
||||
}
|
||||
|
||||
ErrorCode ArduinoI2CBus::write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count,
|
||||
uint8_t *read_buffer, size_t read_count) {
|
||||
ErrorCode ArduinoI2CBus::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) {
|
||||
#if defined(USE_ESP8266)
|
||||
this->set_pins_and_clock_(); // reconfigure Wire global state in case there are multiple instances
|
||||
#endif
|
||||
@@ -94,21 +95,23 @@ ErrorCode ArduinoI2CBus::write_readv(uint8_t address, const uint8_t *write_buffe
|
||||
return ERROR_NOT_INITIALIZED;
|
||||
}
|
||||
|
||||
if (prefix_len != 0)
|
||||
ESP_LOGV_HEX(TAG, prefix, prefix_len, I2C_MAX_LOG_BYTES, "0x%02X TX prefix %s", address);
|
||||
ESP_LOGV_HEX(TAG, write_buffer, write_count, I2C_MAX_LOG_BYTES, "0x%02X TX %s", address);
|
||||
|
||||
uint8_t status = 0;
|
||||
if (write_count != 0 || read_count == 0) {
|
||||
if (prefix_len != 0 || write_count != 0 || read_count == 0) {
|
||||
#ifdef USE_RP2
|
||||
// arduino-pico leaves a transmission open when a write overflows its buffer, and every later
|
||||
// beginTransmission() is then ignored, so refuse the write before starting it
|
||||
if (write_count > WIRE_BUFFER_SIZE) {
|
||||
ESP_LOGVV(TAG, "TX of %zu bytes exceeds the %u byte Wire buffer", write_count, WIRE_BUFFER_SIZE);
|
||||
if (prefix_len + write_count > WIRE_BUFFER_SIZE) {
|
||||
ESP_LOGVV(TAG, "TX of %zu bytes exceeds the %u byte Wire buffer", prefix_len + write_count, WIRE_BUFFER_SIZE);
|
||||
return ERROR_TOO_LARGE;
|
||||
}
|
||||
#endif
|
||||
this->wire_->beginTransmission(address);
|
||||
size_t ret = this->wire_->write(write_buffer, write_count);
|
||||
if (ret != write_count) {
|
||||
if ((prefix_len != 0 && this->wire_->write(prefix, prefix_len) != prefix_len) ||
|
||||
this->wire_->write(write_buffer, write_count) != write_count) {
|
||||
ESP_LOGV(TAG, "TX failed");
|
||||
return ERROR_UNKNOWN;
|
||||
}
|
||||
|
||||
@@ -19,7 +19,13 @@ class ArduinoI2CBus final : public InternalI2CBus, public Component {
|
||||
void setup() override;
|
||||
void dump_config() 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;
|
||||
size_t read_count) override {
|
||||
return this->transfer_(address, nullptr, 0, write_buffer, write_count, read_buffer, read_count);
|
||||
}
|
||||
ErrorCode write_prefixed(uint8_t address, const uint8_t *prefix, size_t prefix_len, const uint8_t *data,
|
||||
size_t len) override {
|
||||
return this->transfer_(address, prefix, prefix_len, data, len, nullptr, 0);
|
||||
}
|
||||
float get_setup_priority() const override { return setup_priority::BUS; }
|
||||
|
||||
void set_scan(bool scan) { this->scan_ = scan; }
|
||||
@@ -35,6 +41,9 @@ class ArduinoI2CBus final : public InternalI2CBus, public Component {
|
||||
#endif
|
||||
|
||||
private:
|
||||
/// Writes `prefix` then `write_buffer` in one transaction, then reads
|
||||
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);
|
||||
void recover_();
|
||||
void set_pins_and_clock_();
|
||||
RecoveryCode recovery_result_;
|
||||
|
||||
@@ -200,8 +200,8 @@ void IDFI2CBus::dump_config() {
|
||||
this->dump_scan_results_();
|
||||
}
|
||||
|
||||
ErrorCode IDFI2CBus::write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count, uint8_t *read_buffer,
|
||||
size_t read_count) {
|
||||
ErrorCode IDFI2CBus::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) {
|
||||
// logging is only enabled with v level, if warnings are shown the caller
|
||||
// should log them
|
||||
if (!this->initialized_) {
|
||||
@@ -209,50 +209,44 @@ ErrorCode IDFI2CBus::write_readv(uint8_t address, const uint8_t *write_buffer, s
|
||||
return ERROR_NOT_INITIALIZED;
|
||||
}
|
||||
|
||||
i2c_operation_job_t jobs[8]{};
|
||||
// START, address, prefix, data, START, address, two reads, STOP
|
||||
i2c_operation_job_t jobs[9]{};
|
||||
size_t num_jobs = 0;
|
||||
uint8_t write_addr = (address << 1) | I2C_MASTER_WRITE;
|
||||
uint8_t read_addr = (address << 1) | I2C_MASTER_READ;
|
||||
ESP_LOGV(TAG, "Writing %zu bytes, reading %zu bytes", write_count, read_count);
|
||||
if (read_count == 0 && write_count == 0) {
|
||||
// basically just a bus probe. Send a start, address and stop
|
||||
ESP_LOGV(TAG, "0x%02X BUS PROBE", address);
|
||||
jobs[num_jobs++].command = I2C_MASTER_CMD_START;
|
||||
auto add_write = [&jobs, &num_jobs](const uint8_t *data, size_t len) ESPHOME_ALWAYS_INLINE {
|
||||
jobs[num_jobs].command = I2C_MASTER_CMD_WRITE;
|
||||
jobs[num_jobs].write.ack_check = true;
|
||||
jobs[num_jobs].write.data = &write_addr;
|
||||
jobs[num_jobs++].write.total_bytes = 1;
|
||||
} else {
|
||||
if (write_count != 0) {
|
||||
ESP_LOGV_HEX(TAG, write_buffer, write_count, I2C_MAX_LOG_BYTES, "0x%02X TX %s", address);
|
||||
jobs[num_jobs++].command = I2C_MASTER_CMD_START;
|
||||
jobs[num_jobs].command = I2C_MASTER_CMD_WRITE;
|
||||
jobs[num_jobs].write.ack_check = true;
|
||||
jobs[num_jobs].write.data = &write_addr;
|
||||
jobs[num_jobs++].write.total_bytes = 1;
|
||||
jobs[num_jobs].command = I2C_MASTER_CMD_WRITE;
|
||||
jobs[num_jobs].write.ack_check = true;
|
||||
jobs[num_jobs].write.data = (uint8_t *) write_buffer;
|
||||
jobs[num_jobs++].write.total_bytes = write_count;
|
||||
}
|
||||
if (read_count != 0) {
|
||||
ESP_LOGV(TAG, "0x%02X RX bytes %zu", address, read_count);
|
||||
jobs[num_jobs++].command = I2C_MASTER_CMD_START;
|
||||
jobs[num_jobs].command = I2C_MASTER_CMD_WRITE;
|
||||
jobs[num_jobs].write.ack_check = true;
|
||||
jobs[num_jobs].write.data = &read_addr;
|
||||
jobs[num_jobs++].write.total_bytes = 1;
|
||||
if (read_count > 1) {
|
||||
jobs[num_jobs].command = I2C_MASTER_CMD_READ;
|
||||
jobs[num_jobs].read.ack_value = I2C_ACK_VAL;
|
||||
jobs[num_jobs].read.data = read_buffer;
|
||||
jobs[num_jobs++].read.total_bytes = read_count - 1;
|
||||
}
|
||||
jobs[num_jobs].write.data = const_cast<uint8_t *>(data);
|
||||
jobs[num_jobs++].write.total_bytes = len;
|
||||
};
|
||||
uint8_t write_addr = (address << 1) | I2C_MASTER_WRITE;
|
||||
uint8_t read_addr = (address << 1) | I2C_MASTER_READ;
|
||||
ESP_LOGV(TAG, "Writing %zu bytes, reading %zu bytes", prefix_len + write_count, read_count);
|
||||
// With nothing to write or read this only addresses the device (the bus scan probe)
|
||||
if (prefix_len != 0 || write_count != 0 || read_count == 0) {
|
||||
if (prefix_len != 0)
|
||||
ESP_LOGV_HEX(TAG, prefix, prefix_len, I2C_MAX_LOG_BYTES, "0x%02X TX prefix %s", address);
|
||||
ESP_LOGV_HEX(TAG, write_buffer, write_count, I2C_MAX_LOG_BYTES, "0x%02X TX %s", address);
|
||||
jobs[num_jobs++].command = I2C_MASTER_CMD_START;
|
||||
add_write(&write_addr, 1);
|
||||
if (prefix_len != 0)
|
||||
add_write(prefix, prefix_len);
|
||||
if (write_count != 0)
|
||||
add_write(write_buffer, write_count);
|
||||
}
|
||||
if (read_count != 0) {
|
||||
ESP_LOGV(TAG, "0x%02X RX bytes %zu", address, read_count);
|
||||
jobs[num_jobs++].command = I2C_MASTER_CMD_START;
|
||||
add_write(&read_addr, 1);
|
||||
if (read_count > 1) {
|
||||
jobs[num_jobs].command = I2C_MASTER_CMD_READ;
|
||||
jobs[num_jobs].read.ack_value = I2C_NACK_VAL;
|
||||
jobs[num_jobs].read.data = read_buffer + read_count - 1;
|
||||
jobs[num_jobs++].read.total_bytes = 1;
|
||||
jobs[num_jobs].read.ack_value = I2C_ACK_VAL;
|
||||
jobs[num_jobs].read.data = read_buffer;
|
||||
jobs[num_jobs++].read.total_bytes = read_count - 1;
|
||||
}
|
||||
jobs[num_jobs].command = I2C_MASTER_CMD_READ;
|
||||
jobs[num_jobs].read.ack_value = I2C_NACK_VAL;
|
||||
jobs[num_jobs].read.data = read_buffer + read_count - 1;
|
||||
jobs[num_jobs++].read.total_bytes = 1;
|
||||
}
|
||||
jobs[num_jobs++].command = I2C_MASTER_CMD_STOP;
|
||||
ESP_LOGV(TAG, "Sending %zu jobs", num_jobs);
|
||||
|
||||
@@ -19,7 +19,13 @@ class IDFI2CBus final : public InternalI2CBus, public Component {
|
||||
void setup() override;
|
||||
void dump_config() 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;
|
||||
size_t read_count) override {
|
||||
return this->transfer_(address, nullptr, 0, write_buffer, write_count, read_buffer, read_count);
|
||||
}
|
||||
ErrorCode write_prefixed(uint8_t address, const uint8_t *prefix, size_t prefix_len, const uint8_t *data,
|
||||
size_t len) override {
|
||||
return this->transfer_(address, prefix, prefix_len, data, len, nullptr, 0);
|
||||
}
|
||||
float get_setup_priority() const override { return setup_priority::BUS; }
|
||||
|
||||
void set_scan(bool scan) { this->scan_ = scan; }
|
||||
@@ -42,6 +48,9 @@ class IDFI2CBus final : public InternalI2CBus, public Component {
|
||||
#endif
|
||||
|
||||
private:
|
||||
/// Writes `prefix` then `write_buffer` in one transaction, then reads
|
||||
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);
|
||||
void recover_();
|
||||
i2c_master_dev_handle_t add_device_(uint32_t frequency);
|
||||
RecoveryCode recovery_result_{};
|
||||
|
||||
@@ -197,8 +197,9 @@ uint8_t SoftwareI2CBus::read_byte_(bool ack) {
|
||||
return data;
|
||||
}
|
||||
|
||||
ErrorCode SoftwareI2CBus::write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count,
|
||||
uint8_t *read_buffer, size_t read_count) {
|
||||
ErrorCode SoftwareI2CBus::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) {
|
||||
if (!this->initialized_) {
|
||||
ESP_LOGD(TAG, "i2c bus not initialized!");
|
||||
return ERROR_NOT_INITIALIZED;
|
||||
@@ -209,25 +210,28 @@ ErrorCode SoftwareI2CBus::write_readv(uint8_t address, const uint8_t *write_buff
|
||||
}
|
||||
this->bus_stuck_ = false;
|
||||
|
||||
if (prefix_len != 0)
|
||||
ESP_LOGV_HEX(TAG, prefix, prefix_len, I2C_MAX_LOG_BYTES, "0x%02X TX prefix %s", address);
|
||||
ESP_LOGV_HEX(TAG, write_buffer, write_count, I2C_MAX_LOG_BYTES, "0x%02X TX %s", address);
|
||||
|
||||
this->timed_out_ = false;
|
||||
ErrorCode err = ERROR_OK;
|
||||
this->start_();
|
||||
// An empty transfer only addresses the device (used by the bus scan)
|
||||
if (write_count != 0 || read_count == 0) {
|
||||
const size_t total = prefix_len + write_count;
|
||||
if (total != 0 || read_count == 0) {
|
||||
if (!this->write_byte_(address << 1)) {
|
||||
err = ERROR_NOT_ACKNOWLEDGED;
|
||||
}
|
||||
for (size_t i = 0; err == ERROR_OK && i < write_count; i++) {
|
||||
if (!this->write_byte_(write_buffer[i])) {
|
||||
for (size_t i = 0; err == ERROR_OK && i < total; i++) {
|
||||
if (!this->write_byte_(i < prefix_len ? prefix[i] : write_buffer[i - prefix_len])) {
|
||||
ESP_LOGVV(TAG, "TX of byte %u to 0x%02X not acknowledged", (unsigned) i, address);
|
||||
err = ERROR_NOT_ACKNOWLEDGED;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (err == ERROR_OK && read_count != 0) {
|
||||
if (write_count != 0) {
|
||||
if (total != 0) {
|
||||
this->start_(); // repeated start
|
||||
}
|
||||
if (!this->write_byte_((address << 1) | 1)) {
|
||||
|
||||
@@ -16,7 +16,13 @@ class SoftwareI2CBus final : public I2CBus, public Component {
|
||||
void setup() override;
|
||||
void dump_config() 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;
|
||||
size_t read_count) override {
|
||||
return this->transfer_(address, nullptr, 0, write_buffer, write_count, read_buffer, read_count);
|
||||
}
|
||||
ErrorCode write_prefixed(uint8_t address, const uint8_t *prefix, size_t prefix_len, const uint8_t *data,
|
||||
size_t len) override {
|
||||
return this->transfer_(address, prefix, prefix_len, data, len, nullptr, 0);
|
||||
}
|
||||
float get_setup_priority() const override { return setup_priority::BUS; }
|
||||
|
||||
void set_scan(bool scan) { this->scan_ = scan; }
|
||||
@@ -31,6 +37,9 @@ class SoftwareI2CBus final : public I2CBus, public Component {
|
||||
void set_timeout(uint32_t timeout) { this->timeout_ = timeout; }
|
||||
|
||||
protected:
|
||||
/// Writes `prefix` then `write_buffer` in one transaction, then reads
|
||||
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);
|
||||
bool recover_();
|
||||
void update_half_period_();
|
||||
void start_();
|
||||
|
||||
@@ -9,21 +9,39 @@ ESPHOME_LOG_TAG(TAG, "tca9548a");
|
||||
|
||||
i2c::ErrorCode TCA9548AChannel::write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count,
|
||||
uint8_t *read_buffer, size_t read_count) {
|
||||
return this->run_({address, nullptr, 0, write_buffer, write_count, read_buffer, read_count});
|
||||
}
|
||||
|
||||
// Forwarded so the upstream bus writes the two parts without joining them
|
||||
i2c::ErrorCode TCA9548AChannel::write_prefixed(uint8_t address, const uint8_t *prefix, size_t prefix_len,
|
||||
const uint8_t *data, size_t len) {
|
||||
return this->run_({address, prefix, prefix_len, data, len, nullptr, 0});
|
||||
}
|
||||
|
||||
i2c::ErrorCode TCA9548AChannel::run_(const Transfer &transfer) {
|
||||
#ifdef I2C_PORT_FREQUENCY_COUNT
|
||||
const uint32_t select_frequency = this->select_frequency_();
|
||||
if (select_frequency != 0) {
|
||||
return this->write_readv_at_port_frequency_(select_frequency, address, write_buffer, write_count, read_buffer,
|
||||
read_count);
|
||||
return this->run_at_port_frequency_(select_frequency, transfer);
|
||||
}
|
||||
#endif
|
||||
auto err = this->parent_->switch_to_channel(this->channel_);
|
||||
if (err != i2c::ERROR_OK)
|
||||
return err;
|
||||
err = this->parent_->bus_->write_readv(address, write_buffer, write_count, read_buffer, read_count);
|
||||
err = this->forward_(transfer);
|
||||
this->parent_->disable_all_channels();
|
||||
return err;
|
||||
}
|
||||
|
||||
i2c::ErrorCode TCA9548AChannel::forward_(const Transfer &transfer) {
|
||||
if (transfer.prefix_len != 0) {
|
||||
return this->parent_->bus_->write_prefixed(transfer.address, transfer.prefix, transfer.prefix_len,
|
||||
transfer.write_buffer, transfer.write_count);
|
||||
}
|
||||
return this->parent_->bus_->write_readv(transfer.address, transfer.write_buffer, transfer.write_count,
|
||||
transfer.read_buffer, transfer.read_count);
|
||||
}
|
||||
|
||||
#ifdef I2C_PORT_FREQUENCY_COUNT
|
||||
// The multiplexer's own frequency, else the slower of the bus and the port
|
||||
uint32_t TCA9548AChannel::select_frequency_() const {
|
||||
@@ -41,9 +59,7 @@ uint32_t TCA9548AChannel::transfer_frequency_() const {
|
||||
return this->frequency_ != 0 ? this->frequency_ : this->parent_->frequency_;
|
||||
}
|
||||
|
||||
i2c::ErrorCode TCA9548AChannel::write_readv_at_port_frequency_(uint32_t select_frequency, uint8_t address,
|
||||
const uint8_t *write_buffer, size_t write_count,
|
||||
uint8_t *read_buffer, size_t read_count) {
|
||||
i2c::ErrorCode TCA9548AChannel::run_at_port_frequency_(uint32_t select_frequency, const Transfer &transfer) {
|
||||
const uint32_t original_frequency = this->parent_->bus_->get_frequency();
|
||||
// A failed switch leaves the bus as it was
|
||||
auto err = this->switch_bus_(select_frequency);
|
||||
@@ -52,7 +68,7 @@ i2c::ErrorCode TCA9548AChannel::write_readv_at_port_frequency_(uint32_t select_f
|
||||
|
||||
err = this->parent_->switch_to_channel(this->channel_);
|
||||
if (err == i2c::ERROR_OK) {
|
||||
err = this->transfer_(select_frequency, address, write_buffer, write_count, read_buffer, read_count);
|
||||
err = this->transfer_(select_frequency, transfer);
|
||||
this->parent_->disable_all_channels();
|
||||
}
|
||||
const i2c::ErrorCode restored = this->switch_bus_(original_frequency);
|
||||
@@ -60,15 +76,14 @@ i2c::ErrorCode TCA9548AChannel::write_readv_at_port_frequency_(uint32_t select_f
|
||||
}
|
||||
|
||||
// Transfer at the port frequency, back to the select frequency for the deselect
|
||||
i2c::ErrorCode TCA9548AChannel::transfer_(uint32_t select_frequency, uint8_t address, const uint8_t *write_buffer,
|
||||
size_t write_count, uint8_t *read_buffer, size_t read_count) {
|
||||
i2c::ErrorCode TCA9548AChannel::transfer_(uint32_t select_frequency, const Transfer &transfer) {
|
||||
const uint32_t transfer_frequency = this->transfer_frequency_();
|
||||
if (transfer_frequency == select_frequency) {
|
||||
return this->parent_->bus_->write_readv(address, write_buffer, write_count, read_buffer, read_count);
|
||||
return this->forward_(transfer);
|
||||
}
|
||||
auto err = this->switch_bus_(transfer_frequency);
|
||||
if (err == i2c::ERROR_OK) {
|
||||
err = this->parent_->bus_->write_readv(address, write_buffer, write_count, read_buffer, read_count);
|
||||
err = this->forward_(transfer);
|
||||
}
|
||||
const i2c::ErrorCode restored = this->switch_bus_(select_frequency);
|
||||
return err != i2c::ERROR_OK ? err : restored;
|
||||
|
||||
@@ -15,6 +15,8 @@ class TCA9548AChannel final : public i2c::I2CBus {
|
||||
|
||||
i2c::ErrorCode write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count, uint8_t *read_buffer,
|
||||
size_t read_count) override;
|
||||
i2c::ErrorCode write_prefixed(uint8_t address, const uint8_t *prefix, size_t prefix_len, const uint8_t *data,
|
||||
size_t len) override;
|
||||
|
||||
#ifdef I2C_PORT_FREQUENCY_COUNT
|
||||
void set_frequency(uint32_t frequency) { this->frequency_ = frequency; }
|
||||
@@ -23,13 +25,24 @@ class TCA9548AChannel final : public i2c::I2CBus {
|
||||
#endif
|
||||
|
||||
protected:
|
||||
/// A transfer to forward upstream; a prefix is written before the data, as write_prefixed() does
|
||||
struct 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;
|
||||
};
|
||||
/// Select the channel, run the transfer upstream and deselect
|
||||
i2c::ErrorCode run_(const Transfer &transfer);
|
||||
i2c::ErrorCode forward_(const Transfer &transfer);
|
||||
#ifdef I2C_PORT_FREQUENCY_COUNT
|
||||
uint32_t select_frequency_() const;
|
||||
uint32_t transfer_frequency_() const;
|
||||
i2c::ErrorCode write_readv_at_port_frequency_(uint32_t select_frequency, uint8_t address, const uint8_t *write_buffer,
|
||||
size_t write_count, uint8_t *read_buffer, size_t read_count);
|
||||
i2c::ErrorCode transfer_(uint32_t select_frequency, uint8_t address, const uint8_t *write_buffer, size_t write_count,
|
||||
uint8_t *read_buffer, size_t read_count);
|
||||
i2c::ErrorCode run_at_port_frequency_(uint32_t select_frequency, const Transfer &transfer);
|
||||
i2c::ErrorCode transfer_(uint32_t select_frequency, const Transfer &transfer);
|
||||
/// Switch the upstream bus, flagging the multiplexer on failure
|
||||
i2c::ErrorCode switch_bus_(uint32_t frequency);
|
||||
uint32_t frequency_{0};
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <vector>
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
|
||||
namespace esphome::i2c::testing {
|
||||
|
||||
// A bus that only implements write_readv(), like external buses, so write_prefixed() takes the default path
|
||||
class JoiningBus : public I2CBus {
|
||||
public:
|
||||
std::vector<uint8_t> written;
|
||||
ErrorCode write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count, uint8_t *read_buffer,
|
||||
size_t read_count) override {
|
||||
this->written.assign(write_buffer, write_buffer + write_count);
|
||||
return ERROR_OK;
|
||||
}
|
||||
};
|
||||
|
||||
// A bus that takes the two parts separately, like the built-in buses
|
||||
class GatherBus final : public JoiningBus {
|
||||
public:
|
||||
std::vector<uint8_t> prefix;
|
||||
std::vector<uint8_t> data;
|
||||
const uint8_t *data_ptr{nullptr};
|
||||
ErrorCode write_prefixed(uint8_t address, const uint8_t *prefix, size_t prefix_len, const uint8_t *data,
|
||||
size_t len) override {
|
||||
this->prefix.assign(prefix, prefix + prefix_len);
|
||||
this->data.assign(data, data + len);
|
||||
this->data_ptr = data;
|
||||
return ERROR_OK;
|
||||
}
|
||||
};
|
||||
|
||||
static std::vector<uint8_t> pattern(size_t len) {
|
||||
std::vector<uint8_t> bytes(len);
|
||||
for (size_t i = 0; i < len; i++)
|
||||
bytes[i] = static_cast<uint8_t>(i * 7 + 1);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
TEST(I2CWritePrefixed, DefaultJoinsPrefixAndDataForShortAndLongWrites) {
|
||||
// 18 data bytes fit the stack buffer with a 2-byte prefix; 300 need the heap fallback
|
||||
for (size_t len : {0, 1, 18, 19, 300}) {
|
||||
JoiningBus bus;
|
||||
const uint8_t prefix[2] = {0xAB, 0xCD};
|
||||
const std::vector<uint8_t> data = pattern(len);
|
||||
EXPECT_EQ(bus.write_prefixed(0x10, prefix, sizeof(prefix), data.data(), data.size()), ERROR_OK);
|
||||
std::vector<uint8_t> expected{0xAB, 0xCD};
|
||||
expected.insert(expected.end(), data.begin(), data.end());
|
||||
EXPECT_EQ(bus.written, expected) << "length " << len;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(I2CWritePrefixed, WriteRegisterPassesTheRegisterAsPrefixWithoutCopyingTheData) {
|
||||
GatherBus bus;
|
||||
I2CDevice device;
|
||||
device.set_i2c_bus(&bus);
|
||||
device.set_i2c_address(0x10);
|
||||
const std::vector<uint8_t> data = pattern(64);
|
||||
|
||||
EXPECT_EQ(device.write_register(0x5E, data.data(), data.size()), ERROR_OK);
|
||||
EXPECT_EQ(bus.prefix, std::vector<uint8_t>{0x5E});
|
||||
EXPECT_EQ(bus.data, data);
|
||||
EXPECT_EQ(bus.data_ptr, data.data());
|
||||
|
||||
EXPECT_EQ(device.write_register16(0x1234, data.data(), data.size()), ERROR_OK);
|
||||
EXPECT_EQ(bus.prefix, (std::vector<uint8_t>{0x12, 0x34}));
|
||||
EXPECT_EQ(bus.data_ptr, data.data());
|
||||
}
|
||||
|
||||
} // namespace esphome::i2c::testing
|
||||
@@ -0,0 +1,13 @@
|
||||
import esphome.codegen as cg
|
||||
from esphome.types import ConfigType
|
||||
from tests.testing_helpers import ComponentManifestOverride
|
||||
|
||||
|
||||
def override_manifest(manifest: ComponentManifestOverride) -> None:
|
||||
async def to_code_testing(config: ConfigType) -> None:
|
||||
# Compile the port frequency path so the tests can reach it
|
||||
cg.add_define("I2C_PORT_FREQUENCY_COUNT", 1)
|
||||
|
||||
# A MULTI_CONF component gets no entry in the host build, so its to_code would never run.
|
||||
manifest.multi_conf = False
|
||||
manifest.to_code = to_code_testing
|
||||
@@ -0,0 +1,110 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <vector>
|
||||
#include "esphome/components/tca9548a/tca9548a.h"
|
||||
|
||||
namespace esphome::tca9548a::testing {
|
||||
|
||||
static constexpr uint8_t MUX_ADDRESS = 0x70;
|
||||
static constexpr uint8_t DEVICE_ADDRESS = 0x5A;
|
||||
|
||||
// One upstream call; a prefixed write has a non-empty prefix
|
||||
struct Call {
|
||||
uint8_t address;
|
||||
std::vector<uint8_t> prefix;
|
||||
std::vector<uint8_t> data;
|
||||
size_t read_count;
|
||||
bool operator==(const Call &other) const = default;
|
||||
};
|
||||
|
||||
class RecordingBus final : public i2c::I2CBus {
|
||||
public:
|
||||
std::vector<Call> calls;
|
||||
const uint8_t *data_ptr{nullptr};
|
||||
|
||||
i2c::ErrorCode write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count, uint8_t *read_buffer,
|
||||
size_t read_count) override {
|
||||
this->calls.push_back({address, {}, {write_buffer, write_buffer + write_count}, read_count});
|
||||
return i2c::ERROR_OK;
|
||||
}
|
||||
|
||||
// A frequency switch is recorded as a call to address 0 carrying the frequency
|
||||
i2c::ErrorCode switch_frequency(uint32_t frequency) override {
|
||||
this->calls.push_back({0, {}, {}, frequency});
|
||||
this->frequency = frequency;
|
||||
return i2c::ERROR_OK;
|
||||
}
|
||||
uint32_t get_frequency() const override { return this->frequency; }
|
||||
uint32_t frequency{100000};
|
||||
|
||||
i2c::ErrorCode write_prefixed(uint8_t address, const uint8_t *prefix, size_t prefix_len, const uint8_t *data,
|
||||
size_t len) override {
|
||||
this->calls.push_back({address, {prefix, prefix + prefix_len}, {data, data + len}, 0});
|
||||
this->data_ptr = data;
|
||||
return i2c::ERROR_OK;
|
||||
}
|
||||
};
|
||||
|
||||
static const Call SELECT_CHANNEL_3{MUX_ADDRESS, {}, {1 << 3}, 0};
|
||||
static const Call DESELECT{MUX_ADDRESS, {}, {0}, 0};
|
||||
static Call switch_to(uint32_t frequency) { return {0, {}, {}, frequency}; }
|
||||
|
||||
class TCA9548AWritePrefixedTest : public ::testing::Test {
|
||||
protected:
|
||||
void SetUp() override {
|
||||
this->mux_.set_i2c_bus(&this->bus_);
|
||||
this->mux_.set_i2c_address(MUX_ADDRESS);
|
||||
this->channel_.set_parent(&this->mux_);
|
||||
this->channel_.set_channel(3);
|
||||
this->device_.set_i2c_bus(&this->channel_);
|
||||
this->device_.set_i2c_address(DEVICE_ADDRESS);
|
||||
}
|
||||
|
||||
RecordingBus bus_;
|
||||
TCA9548AComponent mux_;
|
||||
TCA9548AChannel channel_;
|
||||
i2c::I2CDevice device_;
|
||||
};
|
||||
|
||||
TEST_F(TCA9548AWritePrefixedTest, RegisterWriteReachesTheUpstreamBusInTwoParts) {
|
||||
const uint8_t data[3] = {1, 2, 3};
|
||||
EXPECT_EQ(this->device_.write_register16(0x1234, data, sizeof(data)), i2c::ERROR_OK);
|
||||
const std::vector<Call> expected{SELECT_CHANNEL_3, {DEVICE_ADDRESS, {0x12, 0x34}, {1, 2, 3}, 0}, DESELECT};
|
||||
EXPECT_EQ(this->bus_.calls, expected);
|
||||
// The caller's buffer is passed through, not a joined copy
|
||||
EXPECT_EQ(this->bus_.data_ptr, data);
|
||||
}
|
||||
|
||||
TEST_F(TCA9548AWritePrefixedTest, ReadsAndPlainWritesStillUseWriteReadv) {
|
||||
uint8_t out[2];
|
||||
EXPECT_EQ(this->device_.read_register(0x10, out, sizeof(out)), i2c::ERROR_OK);
|
||||
const uint8_t value = 7;
|
||||
EXPECT_EQ(this->device_.write(&value, 1), i2c::ERROR_OK);
|
||||
const std::vector<Call> expected{SELECT_CHANNEL_3, {DEVICE_ADDRESS, {}, {0x10}, 2}, DESELECT,
|
||||
SELECT_CHANNEL_3, {DEVICE_ADDRESS, {}, {7}, 0}, DESELECT};
|
||||
EXPECT_EQ(this->bus_.calls, expected);
|
||||
}
|
||||
|
||||
TEST_F(TCA9548AWritePrefixedTest, RegisterWriteAtAPortFrequencyStaysInTwoParts) {
|
||||
// The mux addresses at 100 kHz (the bus), the device runs at 400 kHz
|
||||
this->channel_.set_frequency(400000);
|
||||
const uint8_t data[3] = {1, 2, 3};
|
||||
EXPECT_EQ(this->device_.write_register(0x10, data, sizeof(data)), i2c::ERROR_OK);
|
||||
const std::vector<Call> expected{
|
||||
switch_to(100000), SELECT_CHANNEL_3, switch_to(400000), {DEVICE_ADDRESS, {0x10}, {1, 2, 3}, 0},
|
||||
switch_to(100000), DESELECT, switch_to(100000)};
|
||||
EXPECT_EQ(this->bus_.calls, expected);
|
||||
EXPECT_EQ(this->bus_.data_ptr, data);
|
||||
}
|
||||
|
||||
TEST_F(TCA9548AWritePrefixedTest, RegisterWriteAtTheMuxFrequencyStaysInTwoParts) {
|
||||
this->mux_.set_frequency(400000);
|
||||
const uint8_t data[2] = {4, 5};
|
||||
EXPECT_EQ(this->device_.write_register(0x20, data, sizeof(data)), i2c::ERROR_OK);
|
||||
const std::vector<Call> expected{
|
||||
switch_to(400000), SELECT_CHANNEL_3, {DEVICE_ADDRESS, {0x20}, {4, 5}, 0}, DESELECT, switch_to(100000)};
|
||||
EXPECT_EQ(this->bus_.calls, expected);
|
||||
EXPECT_EQ(this->bus_.data_ptr, data);
|
||||
}
|
||||
|
||||
} // namespace esphome::tca9548a::testing
|
||||
Reference in new issue
Block a user