From 3219ad33ef9dee4899bbf35d59a6112914da4646 Mon Sep 17 00:00:00 2001 From: code8buster Date: Tue, 16 May 2023 01:40:42 -0400 Subject: [PATCH] Add ADC channels to esp variants, plug code back in to make sure other archs work --- src/Power.cpp | 56 +++++++++++++++++++---------- src/platform/esp32/architecture.h | 5 ++- src/power.h | 2 ++ variants/diy/dr-dev/variant.h | 4 ++- variants/diy/v1/variant.h | 7 ++-- variants/heltec_v1/variant.h | 2 ++ variants/heltec_v2.1/variant.h | 5 +-- variants/heltec_v2/variant.h | 2 ++ variants/heltec_v3/variant.h | 5 +-- variants/heltec_wsl_v3/variant.h | 3 +- variants/nano-g1-explorer/variant.h | 5 +-- variants/station-g1/variant.h | 5 +-- variants/tbeam_v07/variant.h | 1 + variants/tlora_t3s3_v1/variant.h | 1 + variants/tlora_v1_3/variant.h | 1 + variants/tlora_v2/variant.h | 3 +- variants/tlora_v2_1_16/variant.h | 3 +- variants/tlora_v2_1_18/variant.h | 1 + 18 files changed, 75 insertions(+), 36 deletions(-) diff --git a/src/Power.cpp b/src/Power.cpp index d4ea10b2..14ef2e95 100644 --- a/src/Power.cpp +++ b/src/Power.cpp @@ -18,7 +18,7 @@ static const char *TAG = "ADCmod"; #define DELAY_FOREVER portMAX_DELAY #endif -#ifdef BATTERY_PIN +#if defined(BATTERY_PIN) && defined(ARCH_ESP32) #ifndef BAT_MEASURE_ADC_UNIT // ADC1 is default static const adc1_channel_t adc_channel = ADC_CHANNEL; @@ -28,12 +28,12 @@ static const adc2_channel_t adc_channel = ADC_CHANNEL; static const adc_unit_t unit = ADC_UNIT_2; RTC_NOINIT_ATTR uint64_t RTC_reg_b; -#endif +#endif // BAT_MEASURE_ADC_UNIT esp_adc_cal_characteristics_t *adc_characs = (esp_adc_cal_characteristics_t *)calloc(1, sizeof(esp_adc_cal_characteristics_t)); static const adc_atten_t atten = ADC_ATTEN_DB_11; -#endif // BATTERY_PIN +#endif // BATTERY_PIN && ARCH_ESP32 #ifdef HAS_PMU #include "XPowersAXP192.tpp" @@ -146,12 +146,13 @@ class AnalogBatteryLevel : public HasBatteryLevel // Set the number of samples, it has an effect of increasing sensitivity, especially in complex electromagnetic // environment. uint32_t raw = 0; +#ifdef ARCH_ESP32 #ifndef BAT_MEASURE_ADC_UNIT // ADC1 for (int i = 0; i < BATTERY_SENSE_SAMPLES; i++) { raw += adc1_get_raw(adc_channel); } #else // ADC2 - uint32_t adc_buf = 0; + int32_t adc_buf = 0; for (int i = 0; i < BATTERY_SENSE_SAMPLES; i++) { // ADC2 wifi bug workaround, see // https://github.com/espressif/arduino-esp32/issues/102 @@ -161,11 +162,24 @@ class AnalogBatteryLevel : public HasBatteryLevel raw += adc_buf; } #endif // BAT_MEASURE_ADC_UNIT +#else // !ARCH_ESP32 + for (uint32_t i = 0; i < BATTERY_SENSE_SAMPLES; i++) { + raw += analogRead(BATTERY_PIN); + } +#endif raw = raw / BATTERY_SENSE_SAMPLES; float scaled; +#ifdef ARCH_ESP32 scaled = esp_adc_cal_raw_to_voltage(raw, adc_characs); scaled *= operativeAdcMultiplier; - // LOG_DEBUG("battery gpio %d raw val=%u scaled=%u\n", BATTERY_PIN, raw, (uint32_t)(scaled)); +#else +#ifndef VBAT_RAW_TO_SCALED + scaled = 1000.0 * operativeAdcMultiplier * (AREF_VOLTAGE / 1024.0) * raw; +#else + scaled = VBAT_RAW_TO_SCALED(raw); // defined in variant.h +#endif // VBAT RAW TO SCALED +#endif // ARCH_ESP32 + // LOG_DEBUG("battery gpio %d raw val=%u scaled=%u\n", BATTERY_PIN, raw, (uint32_t)(scaled)); last_read_value = scaled; return scaled; @@ -255,16 +269,19 @@ bool Power::analogInit() LOG_DEBUG("Using analog input %d for battery level\n", BATTERY_PIN); // disable any internal pullups - pinMode(35, INPUT); -#endif // BATTERY PIN + pinMode(BATTERY_PIN, INPUT); #ifndef BATTERY_SENSE_RESOLUTION_BITS -#define BATTERY_SENSE_RESOLUTION_BITS 12 +#define BATTERY_SENSE_RESOLUTION_BITS 10 #endif -#ifdef ARCH_ESP32 -// ESP32 needs special analog stuff -// configure ADC +#ifdef ARCH_ESP32 // ESP32 needs special analog stuff + +#ifndef ADC_WIDTH // max resolution by default + static const adc_bits_width_t width = ADC_WIDTH_BIT_12; +#else + static const adc_bits_width_t width = ADC_WIDTH; +#endif #ifndef BAT_MEASURE_ADC_UNIT // ADC1 adc1_config_width(width); adc1_config_channel_atten(adc_channel, atten); @@ -291,7 +308,6 @@ bool Power::analogInit() #else analogReference(AR_INTERNAL); // 3.6V #endif - adcStart(BATTERY_PIN); analogReadResolution(BATTERY_SENSE_RESOLUTION_BITS); // Default of 12 is not very linear. Recommended to use 10 or 11 // depending on needed resolution. @@ -301,6 +317,7 @@ bool Power::analogInit() return true; #else return false; +#endif // BATTERY_PIN #endif // !HAS_PMU } @@ -415,8 +432,8 @@ void Power::readPowerStatus() #endif - // If we have a battery at all and it is less than 10% full, force deep sleep if we have more than 10 low readings in a - // row + // If we have a battery at all and it is less than 10% full, force deep sleep if we have more than 10 low readings in + // a row if (powerStatus2.getHasBattery() && !powerStatus2.getHasUSB()) { if (batteryLevel->getBattVoltage() < MIN_BAT_MILLIVOLTS) { low_voltage_counter++; @@ -494,10 +511,10 @@ int32_t Power::runOnce() * Init the power manager chip * * axp192 power - DCDC1 0.7-3.5V @ 1200mA max -> OLED // If you turn this off you'll lose comms to the axp192 because the OLED and the axp192 - share the same i2c bus, instead use ssd1306 sleep mode DCDC2 -> unused DCDC3 0.7-3.5V @ 700mA max -> ESP32 (keep this on!) LDO1 - 30mA -> charges GPS backup battery // charges the tiny J13 battery by the GPS to power the GPS ram (for a couple of days), can - not be turned off LDO2 200mA -> LORA LDO3 200mA -> GPS + DCDC1 0.7-3.5V @ 1200mA max -> OLED // If you turn this off you'll lose comms to the axp192 because the OLED and the + axp192 share the same i2c bus, instead use ssd1306 sleep mode DCDC2 -> unused DCDC3 0.7-3.5V @ 700mA max -> ESP32 (keep this + on!) LDO1 30mA -> charges GPS backup battery // charges the tiny J13 battery by the GPS to power the GPS ram (for a couple of + days), can not be turned off LDO2 200mA -> LORA LDO3 200mA -> GPS * */ bool Power::axpChipInit() @@ -624,7 +641,8 @@ bool Power::axpChipInit() // t-beam s3 core /** * gnss module power channel - * The default ALDO4 is off, you need to turn on the GNSS power first, otherwise it will be invalid during initialization + * The default ALDO4 is off, you need to turn on the GNSS power first, otherwise it will be invalid during + * initialization */ PMU->setPowerChannelVoltage(XPOWERS_ALDO4, 3300); PMU->enablePowerOutput(XPOWERS_ALDO4); diff --git a/src/platform/esp32/architecture.h b/src/platform/esp32/architecture.h index da2d9d90..7a1e9ba4 100644 --- a/src/platform/esp32/architecture.h +++ b/src/platform/esp32/architecture.h @@ -39,6 +39,9 @@ #ifndef HAS_CPU_SHUTDOWN #define HAS_CPU_SHUTDOWN 1 #endif +#ifndef DEFAULT_VREF +#define DEFAULT_VREF 1100 +#endif #if defined(HAS_AXP192) || defined(HAS_AXP2101) #define HAS_PMU @@ -132,4 +135,4 @@ #define RF95_NSS 18 #endif -#define SERIAL0_RX_GPIO 3 // Always GPIO3 on ESP32 +#define SERIAL0_RX_GPIO 3 // Always GPIO3 on ESP32 \ No newline at end of file diff --git a/src/power.h b/src/power.h index 8c3cd92a..c47e7846 100644 --- a/src/power.h +++ b/src/power.h @@ -1,8 +1,10 @@ #pragma once #include "PowerStatus.h" #include "concurrency/OSThread.h" +#ifdef ARCH_ESP32 #include #include +#endif /** * Per @spattinson * MIN_BAT_MILLIVOLTS seems high. Typical 18650 are different chemistry to LiPo, even for LiPos that chart seems a bit off, other diff --git a/variants/diy/dr-dev/variant.h b/variants/diy/dr-dev/variant.h index 122a617b..113fe5e3 100644 --- a/variants/diy/dr-dev/variant.h +++ b/variants/diy/dr-dev/variant.h @@ -2,6 +2,8 @@ #define HAS_SCREEN 0 #define I2C_SDA 4 #define I2C_SCL 5 +#define BATTERY_PIN 34 +#define ADC1_GPIO34_CHANNEL // GPS #undef GPS_RX_PIN @@ -67,4 +69,4 @@ #define USE_SX1262 //#define USE_SX1268 //#define USE_LLCC68 -#define SX126X_E22 +#define SX126X_E22 \ No newline at end of file diff --git a/variants/diy/v1/variant.h b/variants/diy/v1/variant.h index c4743764..a1083f9b 100644 --- a/variants/diy/v1/variant.h +++ b/variants/diy/v1/variant.h @@ -9,8 +9,9 @@ #define GPS_TX_PIN 15 #define GPS_UBLOX -#define BUTTON_PIN 39 // The middle button GPIO on the T-Beam -#define BATTERY_PIN 35 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage +#define BUTTON_PIN 39 // The middle button GPIO on the T-Beam +#define BATTERY_PIN 35 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage +#define ADC_CHANNEL ADC1_GPIO35_CHANNEL #define ADC_MULTIPLIER 1.85 // (R1 = 470k, R2 = 680k) #define EXT_PWR_DETECT 4 // Pin to detect connected external power source for LILYGO® TTGO T-Energy T18 and other DIY boards #define EXT_NOTIFY_OUT 12 // Overridden default pin to use for Ext Notify Module (#975). @@ -52,4 +53,4 @@ // Internally the TTGO module hooks the SX126x-DIO2 in to control the TX/RX switch // (which is the default for the sx1262interface code) #define SX126X_E22 -#endif +#endif \ No newline at end of file diff --git a/variants/heltec_v1/variant.h b/variants/heltec_v1/variant.h index 948f6ff1..d1338a28 100644 --- a/variants/heltec_v1/variant.h +++ b/variants/heltec_v1/variant.h @@ -27,3 +27,5 @@ #define ADC_MULTIPLIER 3.2 #define BATTERY_PIN 13 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage +#define ADC_CHANNEL ADC2_GPIO13_CHANNEL +#define BAT_MEASURE_ADC_UNIT 2 \ No newline at end of file diff --git a/variants/heltec_v2.1/variant.h b/variants/heltec_v2.1/variant.h index 39a0e677..e7cfd5b3 100644 --- a/variants/heltec_v2.1/variant.h +++ b/variants/heltec_v2.1/variant.h @@ -29,5 +29,6 @@ #define ADC_MULTIPLIER 3.8 -#define BATTERY_PIN 37 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage -#define EXT_NOTIFY_OUT 13 // Default pin to use for Ext Notify Module. +#define BATTERY_PIN 37 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage +#define ADC_CHANNEL ADC1_GPIO37_CHANNEL +#define EXT_NOTIFY_OUT 13 // Default pin to use for Ext Notify Module. \ No newline at end of file diff --git a/variants/heltec_v2/variant.h b/variants/heltec_v2/variant.h index 59e0c76e..5c183818 100644 --- a/variants/heltec_v2/variant.h +++ b/variants/heltec_v2/variant.h @@ -27,3 +27,5 @@ // ratio of voltage divider = 3.20 (R12=100k, R10=220k) #define ADC_MULTIPLIER 3.2 #define BATTERY_PIN 13 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage +#define ADC_CHANNEL ADC2_GPIO13_CHANNEL +#define BAT_MEASURE_ADC_UNIT 2 \ No newline at end of file diff --git a/variants/heltec_v3/variant.h b/variants/heltec_v3/variant.h index a2e8bde8..16ee6d71 100644 --- a/variants/heltec_v3/variant.h +++ b/variants/heltec_v3/variant.h @@ -8,7 +8,8 @@ #define BUTTON_PIN 0 #define BATTERY_PIN 1 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage -#define ADC_MULTIPLIER 5.22 +#define ADC_CHANNEL ADC1_GPIO1_CHANNEL +#define ADC_MULTIPLIER 4.9 #define USE_SX1262 @@ -27,4 +28,4 @@ #define SX126X_DIO1 LORA_DIO1 #define SX126X_BUSY LORA_DIO2 #define SX126X_RESET LORA_RESET -#define SX126X_E22 +#define SX126X_E22 \ No newline at end of file diff --git a/variants/heltec_wsl_v3/variant.h b/variants/heltec_wsl_v3/variant.h index 66415741..38b38e75 100644 --- a/variants/heltec_wsl_v3/variant.h +++ b/variants/heltec_wsl_v3/variant.h @@ -9,7 +9,8 @@ #define BUTTON_PIN 0 #define BATTERY_PIN 1 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage -#define ADC_MULTIPLIER 5.22 +#define ADC_CHANNEL ADC1_GPIO1_CHANNEL +#define ADC_MULTIPLIER 4.9 #define USE_SX1262 diff --git a/variants/nano-g1-explorer/variant.h b/variants/nano-g1-explorer/variant.h index ad8957a5..fb3fc72d 100644 --- a/variants/nano-g1-explorer/variant.h +++ b/variants/nano-g1-explorer/variant.h @@ -29,8 +29,9 @@ // code) #endif -#define BATTERY_PIN 35 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage +#define BATTERY_PIN 35 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage +#define ADC_CHANNEL ADC1_GPIO35_CHANNEL #define BATTERY_SENSE_SAMPLES 15 // Set the number of samples, It has an effect of increasing sensitivity. #define ADC_MULTIPLIER 2 -#define USE_SH1107_128_64 +#define USE_SH1107_128_64 \ No newline at end of file diff --git a/variants/station-g1/variant.h b/variants/station-g1/variant.h index 4572d47a..3439cb12 100644 --- a/variants/station-g1/variant.h +++ b/variants/station-g1/variant.h @@ -32,7 +32,8 @@ // Info:https://uniteng.com/wiki/doku.php?id=meshtastic:station#rf_design_-_lora_station_edition_g1 #endif -#define BATTERY_PIN 35 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage +#define BATTERY_PIN 35 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage +#define ADC_CHANNEL ADC1_GPIO35_CHANNEL #define BATTERY_SENSE_SAMPLES 30 // Set the number of samples, It has an effect of increasing sensitivity. #define ADC_MULTIPLIER 6.45 #define BAT_FULLVOLT 12600 @@ -41,4 +42,4 @@ #define BAT_NOBATVOLT 6690 // different screen -#define USE_SH1106 +#define USE_SH1106 \ No newline at end of file diff --git a/variants/tbeam_v07/variant.h b/variants/tbeam_v07/variant.h index 2c800868..898705ce 100644 --- a/variants/tbeam_v07/variant.h +++ b/variants/tbeam_v07/variant.h @@ -6,6 +6,7 @@ #define BUTTON_PIN 39 #define BATTERY_PIN 35 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage #define EXT_NOTIFY_OUT 13 // Default pin to use for Ext Notify Module. +#define ADC_CHANNEL ADC1_GPIO35_CHANNEL #define USE_RF95 #define LORA_DIO0 26 // a No connect on the SX1262 module diff --git a/variants/tlora_t3s3_v1/variant.h b/variants/tlora_t3s3_v1/variant.h index 768a7c2d..313b08e6 100644 --- a/variants/tlora_t3s3_v1/variant.h +++ b/variants/tlora_t3s3_v1/variant.h @@ -9,6 +9,7 @@ #define BATTERY_PIN 1 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage // ratio of voltage divider = 2.0 (R42=100k, R43=100k) #define ADC_MULTIPLIER 2.11 // 2.0 + 10% for correction of display undervoltage. +#define ADC_CHANNEL ADC1_GPIO1_CHANNEL #define I2C_SDA 18 // I2C pins for this board #define I2C_SCL 17 diff --git a/variants/tlora_v1_3/variant.h b/variants/tlora_v1_3/variant.h index 7e8aec0c..50041f29 100644 --- a/variants/tlora_v1_3/variant.h +++ b/variants/tlora_v1_3/variant.h @@ -4,6 +4,7 @@ #define GPS_TX_PIN 13 // per @eugene #define BATTERY_PIN 35 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage +#define ADC_CHANNEL ADC1_GPIO35_CHANNEL #define I2C_SDA 21 // I2C pins for this board #define I2C_SCL 22 diff --git a/variants/tlora_v2/variant.h b/variants/tlora_v2/variant.h index f1850313..b9b52177 100644 --- a/variants/tlora_v2/variant.h +++ b/variants/tlora_v2/variant.h @@ -4,6 +4,7 @@ #define GPS_TX_PIN 13 // per @eugene #define BATTERY_PIN 35 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage +#define ADC_CHANNEL ADC1_GPIO35_CHANNEL #define I2C_SDA 21 // I2C pins for this board #define I2C_SCL 22 @@ -19,4 +20,4 @@ #define LORA_DIO0 26 // a No connect on the SX1262 module #define LORA_RESET 14 #define LORA_DIO1 33 // Must be manually wired: https://www.thethingsnetwork.org/forum/t/big-esp32-sx127x-topic-part-3/18436 -#define LORA_DIO2 32 // Not really used +#define LORA_DIO2 32 // Not really used \ No newline at end of file diff --git a/variants/tlora_v2_1_16/variant.h b/variants/tlora_v2_1_16/variant.h index 42f3a0d5..adb5af89 100644 --- a/variants/tlora_v2_1_16/variant.h +++ b/variants/tlora_v2_1_16/variant.h @@ -4,9 +4,8 @@ #define GPS_TX_PIN 13 #define BATTERY_PIN 35 -#define ADC_CHANNEL ADC_GPIO35_CHANNEL +#define ADC_CHANNEL ADC1_GPIO35_CHANNEL #define BATTERY_SENSE_SAMPLES 30 -#define DEFAULT_VREF 1100 // ratio of voltage divider = 2.0 (R42=100k, R43=100k) #define ADC_MULTIPLIER 2 diff --git a/variants/tlora_v2_1_18/variant.h b/variants/tlora_v2_1_18/variant.h index 0262f012..e8f0a965 100644 --- a/variants/tlora_v2_1_18/variant.h +++ b/variants/tlora_v2_1_18/variant.h @@ -4,6 +4,7 @@ #define BATTERY_PIN 35 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage // ratio of voltage divider = 2.0 (R42=100k, R43=100k) #define ADC_MULTIPLIER 2.11 // 2.0 + 10% for correction of display undervoltage. +#define ADC_CHANNEL ADC1_GPIO35_CHANNEL #define I2C_SDA 21 // I2C pins for this board #define I2C_SCL 22