From 97b02d34b93acedfdeebeaedb00c6b2e71b4c9fd Mon Sep 17 00:00:00 2001 From: Verkehrsrot Date: Sat, 21 Dec 2019 14:27:33 +0100 Subject: [PATCH] i2c functions moved from power.cpp to i2c.cpp --- include/i2c.h | 2 ++ include/power.h | 2 -- src/i2c.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++- src/power.cpp | 49 ------------------------------------------------ 4 files changed, 51 insertions(+), 52 deletions(-) diff --git a/include/i2c.h b/include/i2c.h index ed042b31..0975893c 100644 --- a/include/i2c.h +++ b/include/i2c.h @@ -14,5 +14,7 @@ void i2c_init(void); void i2c_deinit(void); int i2c_scan(void); +uint8_t i2c_readBytes(uint8_t addr, uint8_t reg, uint8_t *data, uint8_t len); +uint8_t i2c_writeBytes(uint8_t addr, uint8_t reg, uint8_t *data, uint8_t len); #endif \ No newline at end of file diff --git a/include/power.h b/include/power.h index 40e1a010..2db12da5 100644 --- a/include/power.h +++ b/include/power.h @@ -22,8 +22,6 @@ void AXP192_powerevent_IRQ(void); void AXP192_power(pmu_power_t powerlevel); void AXP192_init(void); void AXP192_showstatus(void); -uint8_t i2c_writeBytes(uint8_t addr, uint8_t reg, uint8_t *data, uint8_t len); -uint8_t i2c_readBytes(uint8_t addr, uint8_t reg, uint8_t *data, uint8_t len); #endif // HAS_PMU diff --git a/src/i2c.cpp b/src/i2c.cpp index 4fe58adc..4947ed05 100644 --- a/src/i2c.cpp +++ b/src/i2c.cpp @@ -88,4 +88,52 @@ int i2c_scan(void) { ESP_LOGE(TAG, "I2c bus busy - scan error"); return devices; -} \ No newline at end of file +} + +// mutexed functions for i2c r/w access +uint8_t i2c_readBytes(uint8_t addr, uint8_t reg, uint8_t *data, uint8_t len) { + if (I2C_MUTEX_LOCK()) { + + uint8_t ret = 0; + Wire.beginTransmission(addr); + Wire.write(reg); + Wire.endTransmission(false); + uint8_t cnt = Wire.requestFrom(addr, (uint8_t)len, (uint8_t)1); + if (!cnt) + ret = 0xFF; + uint16_t index = 0; + while (Wire.available()) { + if (index > len) { + ret = 0xFF; + goto finish; + } + data[index++] = Wire.read(); + } + + finish: + I2C_MUTEX_UNLOCK(); // release i2c bus access + return ret; + } else { + ESP_LOGW(TAG, "[%0.3f] i2c mutex lock failed", millis() / 1000.0); + return 0xFF; + } +} + +uint8_t i2c_writeBytes(uint8_t addr, uint8_t reg, uint8_t *data, uint8_t len) { + if (I2C_MUTEX_LOCK()) { + + uint8_t ret = 0; + Wire.beginTransmission(addr); + Wire.write(reg); + for (uint16_t i = 0; i < len; i++) { + Wire.write(data[i]); + } + ret = Wire.endTransmission(); + + I2C_MUTEX_UNLOCK(); // release i2c bus access + return ret ? ret : 0xFF; + } else { + ESP_LOGW(TAG, "[%0.3f] i2c mutex lock failed", millis() / 1000.0); + return 0xFF; + } +} diff --git a/src/power.cpp b/src/power.cpp index 2afe9d25..80d961cd 100644 --- a/src/power.cpp +++ b/src/power.cpp @@ -147,55 +147,6 @@ void AXP192_init(void) { } } -// helper functions for mutexing pmu i2c access -uint8_t i2c_readBytes(uint8_t addr, uint8_t reg, uint8_t *data, uint8_t len) { - if (I2C_MUTEX_LOCK()) { - - uint8_t ret = 0; - Wire.beginTransmission(addr); - Wire.write(reg); - Wire.endTransmission(false); - uint8_t cnt = Wire.requestFrom(addr, (uint8_t)len, (uint8_t)1); - if (!cnt) - ret = 0xFF; - uint16_t index = 0; - while (Wire.available()) { - if (index > len) { - ret = 0xFF; - goto finish; - } - data[index++] = Wire.read(); - } - - finish: - I2C_MUTEX_UNLOCK(); // release i2c bus access - return ret; - } else { - ESP_LOGW(TAG, "[%0.3f] i2c mutex lock failed", millis() / 1000.0); - return 0xFF; - } -} - -uint8_t i2c_writeBytes(uint8_t addr, uint8_t reg, uint8_t *data, uint8_t len) { - if (I2C_MUTEX_LOCK()) { - - uint8_t ret = 0; - Wire.beginTransmission(addr); - Wire.write(reg); - for (uint16_t i = 0; i < len; i++) { - Wire.write(data[i]); - } - ret = Wire.endTransmission(); - - I2C_MUTEX_UNLOCK(); // release i2c bus access - // return ret ? 0xFF : ret; - return ret ? ret : 0xFF; - } else { - ESP_LOGW(TAG, "[%0.3f] i2c mutex lock failed", millis() / 1000.0); - return 0xFF; - } -} - #endif // HAS_PMU void calibrate_voltage(void) {