Add parameter to control i2c stop signal at endTransmission (#3370)

This commit is contained in:
Keilin Bickar 2022-04-10 16:38:29 -04:00 committed by GitHub
parent 5e79a1f500
commit a9e653724c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 29 additions and 19 deletions

View file

@ -46,21 +46,21 @@ class I2CDevice {
I2CRegister reg(uint8_t a_register) { return {this, a_register}; } I2CRegister reg(uint8_t a_register) { return {this, a_register}; }
ErrorCode read(uint8_t *data, size_t len) { return bus_->read(address_, data, len); } ErrorCode read(uint8_t *data, size_t len) { return bus_->read(address_, data, len); }
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len) { ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len, bool stop = true) {
ErrorCode err = this->write(&a_register, 1); ErrorCode err = this->write(&a_register, 1, stop);
if (err != ERROR_OK) if (err != ERROR_OK)
return err; return err;
return this->read(data, len); return this->read(data, len);
} }
ErrorCode write(const uint8_t *data, uint8_t len) { return bus_->write(address_, data, len); } ErrorCode write(const uint8_t *data, uint8_t len, bool stop = true) { return bus_->write(address_, data, len, stop); }
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) { ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len, bool stop = true) {
WriteBuffer buffers[2]; WriteBuffer buffers[2];
buffers[0].data = &a_register; buffers[0].data = &a_register;
buffers[0].len = 1; buffers[0].len = 1;
buffers[1].data = data; buffers[1].data = data;
buffers[1].len = len; buffers[1].len = len;
return bus_->writev(address_, buffers, 2); return bus_->writev(address_, buffers, 2, stop);
} }
// Compat APIs // Compat APIs
@ -93,7 +93,9 @@ class I2CDevice {
return true; return true;
} }
bool read_byte(uint8_t a_register, uint8_t *data) { return read_register(a_register, data, 1) == ERROR_OK; } bool read_byte(uint8_t a_register, uint8_t *data, bool stop = true) {
return read_register(a_register, data, 1, stop) == ERROR_OK;
}
optional<uint8_t> read_byte(uint8_t a_register) { optional<uint8_t> read_byte(uint8_t a_register) {
uint8_t data; uint8_t data;
@ -104,8 +106,8 @@ class I2CDevice {
bool read_byte_16(uint8_t a_register, uint16_t *data) { return read_bytes_16(a_register, data, 1); } bool read_byte_16(uint8_t a_register, uint16_t *data) { return read_bytes_16(a_register, data, 1); }
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) { bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop = true) {
return write_register(a_register, data, len) == ERROR_OK; return write_register(a_register, data, len, stop) == ERROR_OK;
} }
bool write_bytes(uint8_t a_register, const std::vector<uint8_t> &data) { bool write_bytes(uint8_t a_register, const std::vector<uint8_t> &data) {
@ -118,7 +120,9 @@ class I2CDevice {
bool write_bytes_16(uint8_t a_register, const uint16_t *data, uint8_t len); bool write_bytes_16(uint8_t a_register, const uint16_t *data, uint8_t len);
bool write_byte(uint8_t a_register, uint8_t data) { return write_bytes(a_register, &data, 1); } bool write_byte(uint8_t a_register, uint8_t data, bool stop = true) {
return write_bytes(a_register, &data, 1, stop);
}
bool write_byte_16(uint8_t a_register, uint16_t data) { return write_bytes_16(a_register, &data, 1); } bool write_byte_16(uint8_t a_register, uint16_t data) { return write_bytes_16(a_register, &data, 1); }

View file

@ -36,12 +36,18 @@ class I2CBus {
} }
virtual ErrorCode readv(uint8_t address, ReadBuffer *buffers, size_t cnt) = 0; virtual ErrorCode readv(uint8_t address, ReadBuffer *buffers, size_t cnt) = 0;
virtual ErrorCode write(uint8_t address, const uint8_t *buffer, size_t len) { virtual ErrorCode write(uint8_t address, const uint8_t *buffer, size_t len) {
return write(address, buffer, len, true);
}
virtual ErrorCode write(uint8_t address, const uint8_t *buffer, size_t len, bool stop) {
WriteBuffer buf; WriteBuffer buf;
buf.data = buffer; buf.data = buffer;
buf.len = len; buf.len = len;
return writev(address, &buf, 1); return writev(address, &buf, 1, stop);
} }
virtual ErrorCode writev(uint8_t address, WriteBuffer *buffers, size_t cnt) = 0; virtual ErrorCode writev(uint8_t address, WriteBuffer *buffers, size_t cnt) {
return writev(address, buffers, cnt, true);
}
virtual ErrorCode writev(uint8_t address, WriteBuffer *buffers, size_t cnt, bool stop) = 0;
protected: protected:
void i2c_scan_() { void i2c_scan_() {

View file

@ -104,7 +104,7 @@ ErrorCode ArduinoI2CBus::readv(uint8_t address, ReadBuffer *buffers, size_t cnt)
return ERROR_OK; return ERROR_OK;
} }
ErrorCode ArduinoI2CBus::writev(uint8_t address, WriteBuffer *buffers, size_t cnt) { ErrorCode ArduinoI2CBus::writev(uint8_t address, WriteBuffer *buffers, size_t cnt, bool stop) {
// logging is only enabled with vv level, if warnings are shown the caller // logging is only enabled with vv level, if warnings are shown the caller
// should log them // should log them
if (!initialized_) { if (!initialized_) {
@ -139,7 +139,7 @@ ErrorCode ArduinoI2CBus::writev(uint8_t address, WriteBuffer *buffers, size_t cn
return ERROR_UNKNOWN; return ERROR_UNKNOWN;
} }
} }
uint8_t status = wire_->endTransmission(true); uint8_t status = wire_->endTransmission(stop);
if (status == 0) { if (status == 0) {
return ERROR_OK; return ERROR_OK;
} else if (status == 1) { } else if (status == 1) {

View file

@ -20,7 +20,7 @@ class ArduinoI2CBus : public I2CBus, public Component {
void setup() override; void setup() override;
void dump_config() override; void dump_config() override;
ErrorCode readv(uint8_t address, ReadBuffer *buffers, size_t cnt) override; ErrorCode readv(uint8_t address, ReadBuffer *buffers, size_t cnt) override;
ErrorCode writev(uint8_t address, WriteBuffer *buffers, size_t cnt) override; ErrorCode writev(uint8_t address, WriteBuffer *buffers, size_t cnt, bool stop) override;
float get_setup_priority() const override { return setup_priority::BUS; } float get_setup_priority() const override { return setup_priority::BUS; }
void set_scan(bool scan) { scan_ = scan; } void set_scan(bool scan) { scan_ = scan; }

View file

@ -142,7 +142,7 @@ ErrorCode IDFI2CBus::readv(uint8_t address, ReadBuffer *buffers, size_t cnt) {
return ERROR_OK; return ERROR_OK;
} }
ErrorCode IDFI2CBus::writev(uint8_t address, WriteBuffer *buffers, size_t cnt) { ErrorCode IDFI2CBus::writev(uint8_t address, WriteBuffer *buffers, size_t cnt, bool stop) {
// logging is only enabled with vv level, if warnings are shown the caller // logging is only enabled with vv level, if warnings are shown the caller
// should log them // should log them
if (!initialized_) { if (!initialized_) {

View file

@ -20,7 +20,7 @@ class IDFI2CBus : public I2CBus, public Component {
void setup() override; void setup() override;
void dump_config() override; void dump_config() override;
ErrorCode readv(uint8_t address, ReadBuffer *buffers, size_t cnt) override; ErrorCode readv(uint8_t address, ReadBuffer *buffers, size_t cnt) override;
ErrorCode writev(uint8_t address, WriteBuffer *buffers, size_t cnt) override; ErrorCode writev(uint8_t address, WriteBuffer *buffers, size_t cnt, bool stop) override;
float get_setup_priority() const override { return setup_priority::BUS; } float get_setup_priority() const override { return setup_priority::BUS; }
void set_scan(bool scan) { scan_ = scan; } void set_scan(bool scan) { scan_ = scan; }

View file

@ -12,11 +12,11 @@ i2c::ErrorCode TCA9548AChannel::readv(uint8_t address, i2c::ReadBuffer *buffers,
return err; return err;
return parent_->bus_->readv(address, buffers, cnt); return parent_->bus_->readv(address, buffers, cnt);
} }
i2c::ErrorCode TCA9548AChannel::writev(uint8_t address, i2c::WriteBuffer *buffers, size_t cnt) { i2c::ErrorCode TCA9548AChannel::writev(uint8_t address, i2c::WriteBuffer *buffers, size_t cnt, bool stop) {
auto err = parent_->switch_to_channel(channel_); auto err = parent_->switch_to_channel(channel_);
if (err != i2c::ERROR_OK) if (err != i2c::ERROR_OK)
return err; return err;
return parent_->bus_->writev(address, buffers, cnt); return parent_->bus_->writev(address, buffers, cnt, stop);
} }
void TCA9548AComponent::setup() { void TCA9548AComponent::setup() {

View file

@ -13,7 +13,7 @@ class TCA9548AChannel : public i2c::I2CBus {
void set_parent(TCA9548AComponent *parent) { parent_ = parent; } void set_parent(TCA9548AComponent *parent) { parent_ = parent; }
i2c::ErrorCode readv(uint8_t address, i2c::ReadBuffer *buffers, size_t cnt) override; i2c::ErrorCode readv(uint8_t address, i2c::ReadBuffer *buffers, size_t cnt) override;
i2c::ErrorCode writev(uint8_t address, i2c::WriteBuffer *buffers, size_t cnt) override; i2c::ErrorCode writev(uint8_t address, i2c::WriteBuffer *buffers, size_t cnt, bool stop) override;
protected: protected:
uint8_t channel_; uint8_t channel_;