mirror of
https://github.com/esphome/esphome.git
synced 2026-11-16 10:06:00 +00:00
Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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<17> buffer_alloc(len + 1); // Most I2C writes are <= 16 bytes
|
||||
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<18> buffer_alloc(len + 2); // Most I2C writes are <= 16 bytes + 2 for register
|
||||
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,13 +95,15 @@ 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) {
|
||||
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_();
|
||||
|
||||
@@ -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
|
||||
Reference in new issue
Block a user