ESP32-PaxCounter/src/i2cscan.cpp

114 lines
2.8 KiB
C++
Raw Normal View History

2019-09-02 16:53:39 +02:00
// Basic config
#include "globals.h"
#include "i2cscan.h"
// Local logging tag
static const char TAG[] = __FILE__;
#define SSD1306_PRIMARY_ADDRESS (0x3D)
#define SSD1306_SECONDARY_ADDRESS (0x3C)
#define BME_PRIMARY_ADDRESS (0x77)
#define BME_SECONDARY_ADDRESS (0x76)
#define AXP192_PRIMARY_ADDRESS (0x34)
#define MCP_24AA02E64_PRIMARY_ADDRESS (0x50)
2019-09-02 20:45:40 +02:00
#define QUECTEL_GPS_PRIMARY_ADDRESS (0x10)
2019-09-02 16:53:39 +02:00
int i2c_scan(void) {
int i2c_ret, addr;
int devices = 0;
ESP_LOGI(TAG, "Starting I2C bus scan...");
for (addr = 8; addr <= 119; addr++) {
2019-09-07 19:12:23 +02:00
// scan i2c bus with no more to 100KHz
#ifdef HAS_DISPLAY
Wire.begin(MY_OLED_SDA, MY_OLED_SCL, 100000);
#else
2019-09-02 16:53:39 +02:00
Wire.begin(SDA, SCL, 100000);
2019-09-07 19:12:23 +02:00
#endif
2019-09-02 16:53:39 +02:00
Wire.beginTransmission(addr);
Wire.write(addr);
i2c_ret = Wire.endTransmission();
if (i2c_ret == 0) {
devices++;
switch (addr) {
case SSD1306_PRIMARY_ADDRESS:
case SSD1306_SECONDARY_ADDRESS:
ESP_LOGI(TAG, "0x%X: SSD1306 Display controller", addr);
break;
case BME_PRIMARY_ADDRESS:
case BME_SECONDARY_ADDRESS:
ESP_LOGI(TAG, "0x%X: Bosch BME MEMS", addr);
break;
case AXP192_PRIMARY_ADDRESS:
ESP_LOGI(TAG, "0x%X: AXP192 power management", addr);
#ifdef HAS_PMU
AXP192_init();
#endif
break;
2019-09-02 20:45:40 +02:00
case QUECTEL_GPS_PRIMARY_ADDRESS:
ESP_LOGI(TAG, "0x%X: Quectel GPS", addr);
break;
2019-09-02 16:53:39 +02:00
case MCP_24AA02E64_PRIMARY_ADDRESS:
ESP_LOGI(TAG, "0x%X: 24AA02E64 serial EEPROM", addr);
break;
default:
ESP_LOGI(TAG, "0x%X: Unknown device", addr);
break;
}
} // switch
} // for loop
ESP_LOGI(TAG, "I2C scan done, %u devices found.", devices);
return devices;
}
#ifdef HAS_PMU
void AXP192_init(void) {
AXP20X_Class axp;
if (axp.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);
2019-09-02 20:45:40 +02:00
axp.setChgLEDMode(AXP20X_LED_BLINK_1HZ);
2019-09-07 19:12:23 +02:00
// axp.setChgLEDMode(AXP20X_LED_OFF);
2019-09-02 16:53:39 +02:00
axp.adc1Enable(AXP202_BATT_CUR_ADC1, 1);
2019-09-02 20:45:40 +02:00
#ifdef PMU_INT
pinMode(PMU_INT, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(PMU_INT),
[] {
ESP_LOGI(TAG, "Power source changed");
/* put your code here */
},
FALLING);
2019-09-02 16:53:39 +02:00
axp.enableIRQ(AXP202_VBUS_REMOVED_IRQ | AXP202_VBUS_CONNECT_IRQ |
AXP202_BATT_REMOVED_IRQ | AXP202_BATT_CONNECT_IRQ,
1);
axp.clearIRQ();
2019-09-02 20:45:40 +02:00
#endif // PMU_INT
2019-09-02 16:53:39 +02:00
ESP_LOGI(TAG, "AXP192 PMU initialized.");
}
2019-09-02 20:45:40 +02:00
}
#endif // HAS_PMU