1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-08 06:12:20 +01:00

[mcp4461] Fix read transaction (#10465)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Clyde Stubbs
2025-09-02 06:53:03 +10:00
committed by Jesse Hills
parent c55bc93f70
commit a1a336783e
2 changed files with 19 additions and 4 deletions

View File

@@ -122,7 +122,7 @@ uint8_t Mcp4461Component::get_status_register_() {
uint8_t addr = static_cast<uint8_t>(Mcp4461Addresses::MCP4461_STATUS);
uint8_t reg = addr | static_cast<uint8_t>(Mcp4461Commands::READ);
uint16_t buf;
if (!this->read_byte_16(reg, &buf)) {
if (!this->read_16_(reg, &buf)) {
this->error_code_ = MCP4461_STATUS_REGISTER_ERROR;
this->mark_failed();
return 0;
@@ -148,6 +148,20 @@ void Mcp4461Component::read_status_register_to_log() {
((status_register_value >> 3) & 0x01), ((status_register_value >> 2) & 0x01),
((status_register_value >> 1) & 0x01), ((status_register_value >> 0) & 0x01));
}
bool Mcp4461Component::read_16_(uint8_t address, uint16_t *buf) {
// read 16 bits and convert from big endian to host,
// Do this as two separate operations to ensure a stop condition between the write and read
i2c::ErrorCode err = this->write(&address, 1);
if (err != i2c::ERROR_OK) {
return false;
}
err = this->read(reinterpret_cast<uint8_t *>(buf), 2);
if (err != i2c::ERROR_OK) {
return false;
}
*buf = convert_big_endian(*buf);
return true;
}
uint8_t Mcp4461Component::get_wiper_address_(uint8_t wiper) {
uint8_t addr;
@@ -205,7 +219,7 @@ uint16_t Mcp4461Component::read_wiper_level_(uint8_t wiper_idx) {
}
}
uint16_t buf = 0;
if (!(this->read_byte_16(reg, &buf))) {
if (!(this->read_16_(reg, &buf))) {
this->error_code_ = MCP4461_STATUS_I2C_ERROR;
this->status_set_warning();
ESP_LOGW(TAG, "Error fetching %swiper %u value", (wiper_idx > 3) ? "nonvolatile " : "", wiper_idx);
@@ -392,7 +406,7 @@ uint8_t Mcp4461Component::get_terminal_register_(Mcp4461TerminalIdx terminal_con
: static_cast<uint8_t>(Mcp4461Addresses::MCP4461_TCON1);
reg |= static_cast<uint8_t>(Mcp4461Commands::READ);
uint16_t buf;
if (this->read_byte_16(reg, &buf)) {
if (this->read_16_(reg, &buf)) {
return static_cast<uint8_t>(buf & 0x00ff);
} else {
this->error_code_ = MCP4461_STATUS_I2C_ERROR;
@@ -517,7 +531,7 @@ uint16_t Mcp4461Component::get_eeprom_value(Mcp4461EepromLocation location) {
if (!this->is_eeprom_ready_for_writing_(true)) {
return 0;
}
if (!this->read_byte_16(reg, &buf)) {
if (!this->read_16_(reg, &buf)) {
this->error_code_ = MCP4461_STATUS_I2C_ERROR;
this->status_set_warning();
ESP_LOGW(TAG, "Error fetching EEPROM location value");

View File

@@ -96,6 +96,7 @@ class Mcp4461Component : public Component, public i2c::I2CDevice {
protected:
friend class Mcp4461Wiper;
bool read_16_(uint8_t address, uint16_t *buf);
void update_write_protection_status_();
uint8_t get_wiper_address_(uint8_t wiper);
uint16_t read_wiper_level_(uint8_t wiper);