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,48 +12,55 @@ int i2c_scan(void) {
ESP_LOGI(TAG, "Starting I2C bus scan..."); ESP_LOGI(TAG, "Starting I2C bus scan...");
for (addr = 8; addr <= 119; addr++) { // block i2c bus access
if (I2C_MUTEX_LOCK()) {
// scan i2c bus with no more to 100KHz for (addr = 8; addr <= 119; addr++) {
Wire.beginTransmission(addr);
Wire.write(addr);
i2c_ret = Wire.endTransmission();
if (i2c_ret == 0) { // scan i2c bus with no more to 100KHz
devices++; Wire.beginTransmission(addr);
Wire.write(addr);
i2c_ret = Wire.endTransmission();
switch (addr) { if (i2c_ret == 0) {
devices++;
case SSD1306_PRIMARY_ADDRESS: switch (addr) {
case SSD1306_SECONDARY_ADDRESS:
ESP_LOGI(TAG, "0x%X: SSD1306 Display controller", addr);
break;
case BME_PRIMARY_ADDRESS: case SSD1306_PRIMARY_ADDRESS:
case BME_SECONDARY_ADDRESS: case SSD1306_SECONDARY_ADDRESS:
ESP_LOGI(TAG, "0x%X: Bosch BME MEMS", addr); ESP_LOGI(TAG, "0x%X: SSD1306 Display controller", addr);
break; break;
case AXP192_PRIMARY_ADDRESS: case BME_PRIMARY_ADDRESS:
ESP_LOGI(TAG, "0x%X: AXP192 power management", addr); case BME_SECONDARY_ADDRESS:
break; ESP_LOGI(TAG, "0x%X: Bosch BME MEMS", addr);
break;
case QUECTEL_GPS_PRIMARY_ADDRESS: case AXP192_PRIMARY_ADDRESS:
ESP_LOGI(TAG, "0x%X: Quectel GPS", addr); ESP_LOGI(TAG, "0x%X: AXP192 power management", addr);
break; break;
case MCP_24AA02E64_PRIMARY_ADDRESS: case QUECTEL_GPS_PRIMARY_ADDRESS:
ESP_LOGI(TAG, "0x%X: 24AA02E64 serial EEPROM", addr); ESP_LOGI(TAG, "0x%X: Quectel GPS", addr);
break; break;
default: case MCP_24AA02E64_PRIMARY_ADDRESS:
ESP_LOGI(TAG, "0x%X: Unknown device", addr); ESP_LOGI(TAG, "0x%X: 24AA02E64 serial EEPROM", addr);
break; break;
}
} // switch
} // for loop
ESP_LOGI(TAG, "I2C scan done, %u devices found.", devices); default:
ESP_LOGI(TAG, "0x%X: Unknown device", addr);
break;
}
} // switch
} // for loop
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
ESP_LOGI(TAG, "AXP192 PMU initialization failed"); if (I2C_MUTEX_LOCK()) {
else {
axp.setPowerOutPut(AXP192_LDO2, AXP202_ON); if (pmu.begin(Wire, AXP192_PRIMARY_ADDRESS))
axp.setPowerOutPut(AXP192_LDO3, AXP202_ON); ESP_LOGI(TAG, "AXP192 PMU initialization failed");
axp.setPowerOutPut(AXP192_DCDC2, AXP202_ON); else {
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);
#ifdef PMU_INT pmu.setPowerOutPut(AXP192_LDO2, AXP202_ON);
pinMode(PMU_INT, INPUT_PULLUP); pmu.setPowerOutPut(AXP192_LDO3, AXP202_ON);
attachInterrupt(digitalPinToInterrupt(PMU_INT), pmu.setPowerOutPut(AXP192_DCDC2, AXP202_ON);
[] { pmu.setPowerOutPut(AXP192_EXTEN, AXP202_ON);
ESP_LOGI(TAG, "Power source changed"); pmu.setPowerOutPut(AXP192_DCDC1, AXP202_ON);
/* put your code here */ pmu.setDCDC1Voltage(3300);
}, pmu.setChgLEDMode(AXP20X_LED_LOW_LEVEL);
FALLING); pmu.adc1Enable(AXP202_BATT_CUR_ADC1, 1);
axp.enableIRQ(AXP202_VBUS_REMOVED_IRQ | AXP202_VBUS_CONNECT_IRQ |
AXP202_BATT_REMOVED_IRQ | AXP202_BATT_CONNECT_IRQ,
1);
axp.clearIRQ();
#endif // PMU_INT
ESP_LOGI(TAG, "AXP192 PMU initialized."); /*
}
// 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
},
FALLING);
pmu.enableIRQ(AXP202_VBUS_REMOVED_IRQ | AXP202_VBUS_CONNECT_IRQ |
AXP202_BATT_REMOVED_IRQ | AXP202_BATT_CONNECT_IRQ,
1);
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 #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