i2c functions moved from power.cpp to i2c.cpp
This commit is contained in:
parent
0c584f0f1b
commit
97b02d34b9
@ -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
|
@ -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
|
||||
|
||||
|
50
src/i2c.cpp
50
src/i2c.cpp
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user