AXP192 low level I2C mutexing
This commit is contained in:
parent
58ed680a92
commit
c93b0867a9
@ -19,6 +19,8 @@ void power_event_IRQ(void);
|
||||
void AXP192_power(bool on);
|
||||
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
|
||||
|
||||
#endif
|
@ -11,12 +11,7 @@ AXP20X_Class pmu;
|
||||
|
||||
void power_event_IRQ(void) {
|
||||
|
||||
if (!I2C_MUTEX_LOCK())
|
||||
ESP_LOGW(TAG, "[%0.3f] i2c mutex lock failed", millis() / 1000.0);
|
||||
else {
|
||||
|
||||
pmu.readIRQ();
|
||||
// put your power event handler code here
|
||||
|
||||
if (pmu.isVbusOverVoltageIRQ())
|
||||
ESP_LOGI(TAG, "USB voltage %.2fV too high.", pmu.getVbusVoltage() / 1000);
|
||||
@ -51,8 +46,6 @@ void power_event_IRQ(void) {
|
||||
}
|
||||
|
||||
pmu.clearIRQ();
|
||||
I2C_MUTEX_UNLOCK();
|
||||
} // mutex
|
||||
|
||||
// refresh stored voltage value
|
||||
read_voltage();
|
||||
@ -75,10 +68,6 @@ void AXP192_power(bool on) {
|
||||
|
||||
void AXP192_showstatus(void) {
|
||||
|
||||
if (!I2C_MUTEX_LOCK())
|
||||
ESP_LOGW(TAG, "[%0.3f] i2c mutex lock failed", millis() / 1000.0);
|
||||
else {
|
||||
|
||||
if (pmu.isBatteryConnect())
|
||||
if (pmu.isChargeing())
|
||||
ESP_LOGI(TAG, "Battery charging, %.2fV @ %.0fmAh",
|
||||
@ -93,17 +82,11 @@ void AXP192_showstatus(void) {
|
||||
pmu.getVbusVoltage() / 1000 * pmu.getVbusCurrent());
|
||||
else
|
||||
ESP_LOGI(TAG, "USB not present");
|
||||
|
||||
I2C_MUTEX_UNLOCK();
|
||||
} // mutex
|
||||
}
|
||||
|
||||
void AXP192_init(void) {
|
||||
|
||||
// block i2c bus access
|
||||
if (I2C_MUTEX_LOCK()) {
|
||||
|
||||
if (pmu.begin(Wire, AXP192_PRIMARY_ADDRESS))
|
||||
if (pmu.begin(i2c_readBytes, i2c_writeBytes, AXP192_PRIMARY_ADDRESS) == AXP_FAIL)
|
||||
ESP_LOGI(TAG, "AXP192 PMU initialization failed");
|
||||
else {
|
||||
|
||||
@ -121,8 +104,6 @@ void AXP192_init(void) {
|
||||
// switch power rails on
|
||||
AXP192_power(true);
|
||||
|
||||
// I2C access of AXP202X library currently is not mutexable
|
||||
// so we better should disable AXP interrupts... ?
|
||||
#ifdef PMU_INT
|
||||
pinMode(PMU_INT, INPUT_PULLUP);
|
||||
attachInterrupt(digitalPinToInterrupt(PMU_INT), PMUIRQ, FALLING);
|
||||
@ -135,10 +116,55 @@ void AXP192_init(void) {
|
||||
|
||||
ESP_LOGI(TAG, "AXP192 PMU initialized");
|
||||
}
|
||||
I2C_MUTEX_UNLOCK(); // release i2c bus access
|
||||
} else
|
||||
ESP_LOGE(TAG, "I2c bus busy - PMU initialization error");
|
||||
}
|
||||
|
||||
// helper functions for mutexing 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)
|
||||
return 0xFF;
|
||||
data[index++] = Wire.read();
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
#ifdef BAT_MEASURE_ADC
|
||||
@ -196,12 +222,12 @@ uint16_t read_voltage() {
|
||||
uint16_t voltage = 0;
|
||||
|
||||
#ifdef HAS_PMU
|
||||
if (!I2C_MUTEX_LOCK())
|
||||
ESP_LOGW(TAG, "[%0.3f] i2c mutex lock failed", millis() / 1000.0);
|
||||
else {
|
||||
// if (!I2C_MUTEX_LOCK())
|
||||
// ESP_LOGW(TAG, "[%0.3f] i2c mutex lock failed", millis() / 1000.0);
|
||||
// else {
|
||||
voltage = pmu.isVBUSPlug() ? 0xffff : pmu.getBattVoltage();
|
||||
I2C_MUTEX_UNLOCK();
|
||||
}
|
||||
// I2C_MUTEX_UNLOCK();
|
||||
// }
|
||||
#else
|
||||
|
||||
#ifdef BAT_MEASURE_ADC
|
||||
|
Loading…
Reference in New Issue
Block a user