i2c functions moved from power.cpp to i2c.cpp

This commit is contained in:
Verkehrsrot 2019-12-21 14:27:33 +01:00
parent 0c584f0f1b
commit 97b02d34b9
4 changed files with 51 additions and 52 deletions

View File

@ -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

View File

@ -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

View File

@ -88,4 +88,52 @@ int i2c_scan(void) {
ESP_LOGE(TAG, "I2c bus busy - scan error");
return devices;
}
}
// 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;
}
}

View File

@ -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) {