i2cscan & PMU i2c mutexing

This commit is contained in:
Verkehrsrot 2019-09-09 12:06:23 +02:00
parent 5ca4d2f721
commit 6dbbf14ef8
2 changed files with 86 additions and 61 deletions

View File

@ -12,6 +12,9 @@ int i2c_scan(void) {
ESP_LOGI(TAG, "Starting I2C bus scan...");
// block i2c bus access
if (I2C_MUTEX_LOCK()) {
for (addr = 8; addr <= 119; addr++) {
// scan i2c bus with no more to 100KHz
@ -55,5 +58,9 @@ int i2c_scan(void) {
ESP_LOGI(TAG, "I2C scan done, %u devices found.", devices);
I2C_MUTEX_UNLOCK(); // release i2c bus access
} else
ESP_LOGE(TAG, "I2c bus busy - scan error");
return devices;
}

View File

@ -7,40 +7,58 @@ static const char TAG[] = __FILE__;
#ifdef HAS_PMU
AXP20X_Class axp;
AXP20X_Class pmu;
void AXP192_init(void) {
if (axp.begin(Wire, AXP192_PRIMARY_ADDRESS))
// block i2c bus access
if (I2C_MUTEX_LOCK()) {
if (pmu.begin(Wire, AXP192_PRIMARY_ADDRESS))
ESP_LOGI(TAG, "AXP192 PMU initialization failed");
else {
axp.setPowerOutPut(AXP192_LDO2, AXP202_ON);
axp.setPowerOutPut(AXP192_LDO3, AXP202_ON);
axp.setPowerOutPut(AXP192_DCDC2, AXP202_ON);
axp.setPowerOutPut(AXP192_EXTEN, AXP202_ON);
axp.setPowerOutPut(AXP192_DCDC1, AXP202_ON);
axp.setDCDC1Voltage(3300);
axp.setChgLEDMode(AXP20X_LED_BLINK_1HZ);
// axp.setChgLEDMode(AXP20X_LED_OFF);
axp.adc1Enable(AXP202_BATT_CUR_ADC1, 1);
pmu.setPowerOutPut(AXP192_LDO2, AXP202_ON);
pmu.setPowerOutPut(AXP192_LDO3, AXP202_ON);
pmu.setPowerOutPut(AXP192_DCDC2, AXP202_ON);
pmu.setPowerOutPut(AXP192_EXTEN, AXP202_ON);
pmu.setPowerOutPut(AXP192_DCDC1, AXP202_ON);
pmu.setDCDC1Voltage(3300);
pmu.setChgLEDMode(AXP20X_LED_LOW_LEVEL);
pmu.adc1Enable(AXP202_BATT_CUR_ADC1, 1);
#ifdef PMU_INT
/*
// I2C access of AXP202X library currently is not mutexable
// so we need to disable AXP interrupts
#ifdef PMU_INT
pinMode(PMU_INT, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(PMU_INT),
[] {
ESP_LOGI(TAG, "Power source changed");
/* put your code here */
// put your code here
},
FALLING);
axp.enableIRQ(AXP202_VBUS_REMOVED_IRQ | AXP202_VBUS_CONNECT_IRQ |
pmu.enableIRQ(AXP202_VBUS_REMOVED_IRQ | AXP202_VBUS_CONNECT_IRQ |
AXP202_BATT_REMOVED_IRQ | AXP202_BATT_CONNECT_IRQ,
1);
axp.clearIRQ();
#endif // PMU_INT
pmu.clearIRQ();
#endif // PMU_INT
*/
ESP_LOGI(TAG, "AXP192 PMU initialized.");
if (pmu.isBatteryConnect())
if (pmu.isChargeing())
ESP_LOGI(TAG, "Running on battery, charging.");
else
ESP_LOGI(TAG, "Running on battery, not charging.");
else if (pmu.isVBUSPlug())
ESP_LOGI(TAG, "Running on USB power.");
}
I2C_MUTEX_UNLOCK(); // release i2c bus access
} else
ESP_LOGE(TAG, "I2c bus busy - PMU initialization error");
}
#endif // HAS_PMU
@ -116,7 +134,7 @@ uint16_t read_voltage() {
uint16_t voltage = 0;
#ifdef HAS_PMU
voltage = axp.isVBUSPlug() ? 0xffff : axp.getBattVoltage();
voltage = pmu.isVBUSPlug() ? 0xffff : pmu.getBattVoltage();
#else
#ifdef BAT_MEASURE_ADC