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..."); ESP_LOGI(TAG, "Starting I2C bus scan...");
// block i2c bus access
if (I2C_MUTEX_LOCK()) {
for (addr = 8; addr <= 119; addr++) { for (addr = 8; addr <= 119; addr++) {
// scan i2c bus with no more to 100KHz // 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); 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; return devices;
} }

View File

@ -7,40 +7,58 @@ static const char TAG[] = __FILE__;
#ifdef HAS_PMU #ifdef HAS_PMU
AXP20X_Class axp; AXP20X_Class pmu;
void AXP192_init(void) { 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"); ESP_LOGI(TAG, "AXP192 PMU initialization failed");
else { else {
axp.setPowerOutPut(AXP192_LDO2, AXP202_ON); pmu.setPowerOutPut(AXP192_LDO2, AXP202_ON);
axp.setPowerOutPut(AXP192_LDO3, AXP202_ON); pmu.setPowerOutPut(AXP192_LDO3, AXP202_ON);
axp.setPowerOutPut(AXP192_DCDC2, AXP202_ON); pmu.setPowerOutPut(AXP192_DCDC2, AXP202_ON);
axp.setPowerOutPut(AXP192_EXTEN, AXP202_ON); pmu.setPowerOutPut(AXP192_EXTEN, AXP202_ON);
axp.setPowerOutPut(AXP192_DCDC1, AXP202_ON); pmu.setPowerOutPut(AXP192_DCDC1, AXP202_ON);
axp.setDCDC1Voltage(3300); pmu.setDCDC1Voltage(3300);
axp.setChgLEDMode(AXP20X_LED_BLINK_1HZ); pmu.setChgLEDMode(AXP20X_LED_LOW_LEVEL);
// axp.setChgLEDMode(AXP20X_LED_OFF); pmu.adc1Enable(AXP202_BATT_CUR_ADC1, 1);
axp.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); pinMode(PMU_INT, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(PMU_INT), attachInterrupt(digitalPinToInterrupt(PMU_INT),
[] { [] {
ESP_LOGI(TAG, "Power source changed"); ESP_LOGI(TAG, "Power source changed");
/* put your code here */ // put your code here
}, },
FALLING); 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, AXP202_BATT_REMOVED_IRQ | AXP202_BATT_CONNECT_IRQ,
1); 1);
axp.clearIRQ(); pmu.clearIRQ();
#endif // PMU_INT #endif // PMU_INT
*/
ESP_LOGI(TAG, "AXP192 PMU initialized."); 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 #endif // HAS_PMU
@ -116,7 +134,7 @@ uint16_t read_voltage() {
uint16_t voltage = 0; uint16_t voltage = 0;
#ifdef HAS_PMU #ifdef HAS_PMU
voltage = axp.isVBUSPlug() ? 0xffff : axp.getBattVoltage(); voltage = pmu.isVBUSPlug() ? 0xffff : pmu.getBattVoltage();
#else #else
#ifdef BAT_MEASURE_ADC #ifdef BAT_MEASURE_ADC