diff --git a/src/drivers/i2c.cpp b/src/drivers/i2c.cpp index 75beacda..36906a50 100644 --- a/src/drivers/i2c.cpp +++ b/src/drivers/i2c.cpp @@ -65,8 +65,12 @@ I2CTransaction::~I2CTransaction() { free(buffer_); } -esp_err_t I2CTransaction::Execute(i2c_port_t port) { - return i2c_master_cmd_begin(port, handle_, kI2CTimeout); +esp_err_t I2CTransaction::Execute(int num_retries) { + esp_err_t res; + do { + res = i2c_master_cmd_begin(I2C_NUM_0, handle_, kI2CTimeout); + } while (res == ESP_ERR_TIMEOUT && num_retries-- > 0); + return res; } I2CTransaction& I2CTransaction::start() { diff --git a/src/drivers/include/i2c.hpp b/src/drivers/include/i2c.hpp index 213fb7b3..0dc1e7c0 100644 --- a/src/drivers/include/i2c.hpp +++ b/src/drivers/include/i2c.hpp @@ -41,7 +41,7 @@ class I2CTransaction { * ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode. * ESP_ERR_TIMEOUT Operation timeout because the bus is busy. */ - esp_err_t Execute(i2c_port_t port = I2C_NUM_0); + esp_err_t Execute(int num_retries = 0); /* * Enqueues a start condition. May also be used for repeated start diff --git a/src/drivers/samd.cpp b/src/drivers/samd.cpp index fe621993..459b7bce 100644 --- a/src/drivers/samd.cpp +++ b/src/drivers/samd.cpp @@ -56,7 +56,7 @@ Samd::Samd() { .write_addr(kAddress, I2C_MASTER_READ) .read(&raw_res, I2C_MASTER_NACK) .stop(); - ESP_ERROR_CHECK(transaction.Execute()); + ESP_ERROR_CHECK(transaction.Execute(1)); ESP_LOGI(kTag, "samd firmware rev: %u", raw_res); UpdateChargeStatus(); @@ -78,7 +78,7 @@ auto Samd::UpdateChargeStatus() -> void { .write_addr(kAddress, I2C_MASTER_READ) .read(&raw_res, I2C_MASTER_NACK) .stop(); - esp_err_t res = transaction.Execute(); + esp_err_t res = transaction.Execute(1); if (res != ESP_OK) { return; } @@ -123,7 +123,7 @@ auto Samd::UpdateUsbStatus() -> void { .write_addr(kAddress, I2C_MASTER_READ) .read(&raw_res, I2C_MASTER_NACK) .stop(); - esp_err_t res = transaction.Execute(); + esp_err_t res = transaction.Execute(1); if (res != ESP_OK) { return; } @@ -141,7 +141,7 @@ auto Samd::ResetToFlashSamd() -> void { .write_addr(kAddress, I2C_MASTER_WRITE) .write_ack(Registers::kUsbControl, 0b100) .stop(); - ESP_ERROR_CHECK(transaction.Execute()); + ESP_ERROR_CHECK(transaction.Execute(3)); } auto Samd::PowerDown() -> void { @@ -150,7 +150,7 @@ auto Samd::PowerDown() -> void { .write_addr(kAddress, I2C_MASTER_WRITE) .write_ack(Registers::kPowerControl, 0b1) .stop(); - ESP_ERROR_CHECK(transaction.Execute()); + ESP_ERROR_CHECK(transaction.Execute(3)); } auto Samd::CreateReadPending() -> SemaphoreHandle_t { diff --git a/src/drivers/touchwheel.cpp b/src/drivers/touchwheel.cpp index 77b78c23..6d624b0f 100644 --- a/src/drivers/touchwheel.cpp +++ b/src/drivers/touchwheel.cpp @@ -76,7 +76,7 @@ void TouchWheel::WriteRegister(uint8_t reg, uint8_t val) { .write_addr(kTouchWheelAddress, I2C_MASTER_WRITE) .write_ack(reg, val) .stop(); - esp_err_t res = transaction.Execute(); + esp_err_t res = transaction.Execute(1); if (res != ESP_OK) { ESP_LOGW(kTag, "write failed: %s", esp_err_to_name(res)); } @@ -92,7 +92,7 @@ uint8_t TouchWheel::ReadRegister(uint8_t reg) { .write_addr(kTouchWheelAddress, I2C_MASTER_READ) .read(&res, I2C_MASTER_NACK) .stop(); - if (transaction.Execute() == ESP_OK) { + if (transaction.Execute(1) == ESP_OK) { return res; } else { return 0;