power.cpp added, i2cscan adapted

This commit is contained in:
Verkehrsrot 2019-09-07 19:52:25 +02:00
parent 738543b529
commit a1b7c102d0
6 changed files with 80 additions and 66 deletions

View File

@ -2,11 +2,15 @@
#define _I2CSCAN_H #define _I2CSCAN_H
#include <Arduino.h> #include <Arduino.h>
#ifdef HAS_PMU
#include "axp20x.h" #define SSD1306_PRIMARY_ADDRESS (0x3D)
#endif #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_scan(void);
void AXP192_init(void);
#endif #endif

View File

@ -1,12 +1,13 @@
#ifndef _MAIN_H #ifndef _MAIN_H
#define _MAIN_H #define _MAIN_H
#include <esp_spi_flash.h> // needed for reading ESP32 chip attributes #include <esp_spi_flash.h> // needed for reading ESP32 chip attributes
#include <esp_event_loop.h> // needed for Wifi event handler #include <esp_event_loop.h> // needed for Wifi event handler
#include <esp32-hal-timer.h> // needed for timers #include <esp32-hal-timer.h> // needed for timers
#include <esp_coexist.h> // needed for showing coex sw version #include <esp_coexist.h> // needed for showing coex sw version
#include "globals.h" #include "globals.h"
#include "power.h"
#include "i2cscan.h" #include "i2cscan.h"
#include "blescan.h" #include "blescan.h"
#include "wifiscan.h" #include "wifiscan.h"
@ -17,7 +18,7 @@
#include "irqhandler.h" #include "irqhandler.h"
#include "led.h" #include "led.h"
#include "spislave.h" #include "spislave.h"
#if(HAS_LORA) #if (HAS_LORA)
#include "lorawan.h" #include "lorawan.h"
#endif #endif
#include "timekeeper.h" #include "timekeeper.h"

13
include/power.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef _POWER_H
#define _POWER_H
#include <Arduino.h>
#include "i2cscan.h"
#ifdef HAS_PMU
#include <axp20x.h>
#endif
void AXP192_init(void);
#endif

View File

@ -5,14 +5,6 @@
// Local logging tag // Local logging tag
static const char TAG[] = __FILE__; 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_scan(void) {
int i2c_ret, addr; int i2c_ret, addr;
@ -49,9 +41,6 @@ int i2c_scan(void) {
case AXP192_PRIMARY_ADDRESS: case AXP192_PRIMARY_ADDRESS:
ESP_LOGI(TAG, "0x%X: AXP192 power management", addr); ESP_LOGI(TAG, "0x%X: AXP192 power management", addr);
#ifdef HAS_PMU
AXP192_init();
#endif
break; break;
case QUECTEL_GPS_PRIMARY_ADDRESS: case QUECTEL_GPS_PRIMARY_ADDRESS:
@ -72,43 +61,4 @@ int i2c_scan(void) {
ESP_LOGI(TAG, "I2C scan done, %u devices found.", devices); ESP_LOGI(TAG, "I2C scan done, %u devices found.", devices);
return 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

View File

@ -169,6 +169,17 @@ void setup() {
ESP_LOGI(TAG, "TinyGPS+ version %s", TinyGPSPlus::libraryVersion()); ESP_LOGI(TAG, "TinyGPS+ version %s", TinyGPSPlus::libraryVersion());
#endif #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(); i2c_scan();
#endif // verbose #endif // verbose
@ -189,13 +200,6 @@ void setup() {
strcat_P(features, " PSRAM"); strcat_P(features, " PSRAM");
#endif #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 #ifdef BAT_MEASURE_EN
pinMode(BAT_MEASURE_EN, OUTPUT); pinMode(BAT_MEASURE_EN, OUTPUT);
#endif #endif

42
src/power.cpp Normal file
View File

@ -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