diff --git a/include/i2cscan.h b/include/i2cscan.h index b834b34f..38487e76 100644 --- a/include/i2cscan.h +++ b/include/i2cscan.h @@ -2,11 +2,15 @@ #define _I2CSCAN_H #include -#ifdef HAS_PMU -#include "axp20x.h" -#endif + +#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) +#define QUECTEL_GPS_PRIMARY_ADDRESS (0x10) int i2c_scan(void); -void AXP192_init(void); #endif \ No newline at end of file diff --git a/include/main.h b/include/main.h index ddc49d5d..4b5617cb 100644 --- a/include/main.h +++ b/include/main.h @@ -1,12 +1,13 @@ #ifndef _MAIN_H #define _MAIN_H -#include // needed for reading ESP32 chip attributes -#include // needed for Wifi event handler +#include // needed for reading ESP32 chip attributes +#include // needed for Wifi event handler #include // needed for timers -#include // needed for showing coex sw version +#include // needed for showing coex sw version #include "globals.h" +#include "power.h" #include "i2cscan.h" #include "blescan.h" #include "wifiscan.h" @@ -17,7 +18,7 @@ #include "irqhandler.h" #include "led.h" #include "spislave.h" -#if(HAS_LORA) +#if (HAS_LORA) #include "lorawan.h" #endif #include "timekeeper.h" diff --git a/include/power.h b/include/power.h new file mode 100644 index 00000000..f557edb4 --- /dev/null +++ b/include/power.h @@ -0,0 +1,13 @@ +#ifndef _POWER_H +#define _POWER_H + +#include +#include "i2cscan.h" + +#ifdef HAS_PMU +#include +#endif + +void AXP192_init(void); + +#endif \ No newline at end of file diff --git a/src/i2cscan.cpp b/src/i2cscan.cpp index ea7e83ce..e3deb9f0 100644 --- a/src/i2cscan.cpp +++ b/src/i2cscan.cpp @@ -5,14 +5,6 @@ // 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) -#define QUECTEL_GPS_PRIMARY_ADDRESS (0x10) - int i2c_scan(void) { int i2c_ret, addr; @@ -49,9 +41,6 @@ int i2c_scan(void) { case AXP192_PRIMARY_ADDRESS: ESP_LOGI(TAG, "0x%X: AXP192 power management", addr); -#ifdef HAS_PMU - AXP192_init(); -#endif break; case QUECTEL_GPS_PRIMARY_ADDRESS: @@ -72,43 +61,4 @@ int i2c_scan(void) { 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); - axp.setChgLEDMode(AXP20X_LED_BLINK_1HZ); - // axp.setChgLEDMode(AXP20X_LED_OFF); - axp.adc1Enable(AXP202_BATT_CUR_ADC1, 1); - -#ifdef PMU_INT - pinMode(PMU_INT, INPUT_PULLUP); - attachInterrupt(digitalPinToInterrupt(PMU_INT), - [] { - ESP_LOGI(TAG, "Power source changed"); - /* put your code here */ - }, - FALLING); - 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."); - } -} -#endif // HAS_PMU \ No newline at end of file +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 0dc90395..3a466e68 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -169,6 +169,17 @@ void setup() { ESP_LOGI(TAG, "TinyGPS+ version %s", TinyGPSPlus::libraryVersion()); #endif +// setup power on boards with power management logic +#ifdef EXT_POWER_SW + pinMode(EXT_POWER_SW, OUTPUT); + digitalWrite(EXT_POWER_SW, EXT_POWER_ON); + strcat_P(features, " VEXT"); +#endif +#ifdef HAS_PMU + AXP192_init(); + strcat_P(features, " PMU"); +#endif + i2c_scan(); #endif // verbose @@ -189,13 +200,6 @@ void setup() { strcat_P(features, " PSRAM"); #endif -// set external power mode -#ifdef EXT_POWER_SW - pinMode(EXT_POWER_SW, OUTPUT); - digitalWrite(EXT_POWER_SW, EXT_POWER_ON); - strcat_P(features, " VEXT"); -#endif - #ifdef BAT_MEASURE_EN pinMode(BAT_MEASURE_EN, OUTPUT); #endif diff --git a/src/power.cpp b/src/power.cpp new file mode 100644 index 00000000..3a492256 --- /dev/null +++ b/src/power.cpp @@ -0,0 +1,42 @@ +// Basic config +#include "globals.h" +#include "power.h" + +#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); + axp.setChgLEDMode(AXP20X_LED_BLINK_1HZ); + // axp.setChgLEDMode(AXP20X_LED_OFF); + axp.adc1Enable(AXP202_BATT_CUR_ADC1, 1); + +#ifdef PMU_INT + pinMode(PMU_INT, INPUT_PULLUP); + attachInterrupt(digitalPinToInterrupt(PMU_INT), + [] { + ESP_LOGI(TAG, "Power source changed"); + /* put your code here */ + }, + FALLING); + 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."); + } +} +#endif // HAS_PMU \ No newline at end of file