From a22a5e1bfc3aab3cd99e03a24eccf29e152b5905 Mon Sep 17 00:00:00 2001 From: Linar Yusupov Date: Mon, 15 Mar 2021 09:44:52 +0300 Subject: [PATCH] clean-up of Battery API --- .../source/SoftRF/src/driver/Battery.cpp | 61 +++--------- .../source/SoftRF/src/driver/Battery.h | 12 ++- .../source/SoftRF/src/driver/Bluetooth.cpp | 4 +- .../source/SoftRF/src/platform/CC13XX.cpp | 41 ++++++-- .../source/SoftRF/src/platform/ESP32.cpp | 78 +++++++++++---- .../source/SoftRF/src/platform/ESP8266.cpp | 31 +++++- .../source/SoftRF/src/platform/PSoC4.cpp | 95 +++++++++++++------ .../source/SoftRF/src/platform/RPi.cpp | 30 +++++- .../source/SoftRF/src/platform/STM32.cpp | 54 +++++++++-- .../source/SoftRF/src/platform/nRF52.cpp | 72 ++++++++++---- .../firmware/source/SoftRF/src/system/SoC.h | 2 +- .../source/SoftRF/src/ui/Time_EPD.cpp | 2 +- 12 files changed, 348 insertions(+), 134 deletions(-) diff --git a/software/firmware/source/SoftRF/src/driver/Battery.cpp b/software/firmware/source/SoftRF/src/driver/Battery.cpp index 10aafa62e..4d8356825 100644 --- a/software/firmware/source/SoftRF/src/driver/Battery.cpp +++ b/software/firmware/source/SoftRF/src/driver/Battery.cpp @@ -32,7 +32,7 @@ void Battery_setup() { SoC->Battery_setup(); - Battery_voltage_cache = SoC->Battery_voltage(); + Battery_voltage_cache = SoC->Battery_param(BATTERY_PARAM_VOLTAGE); Battery_TimeMarker = millis(); } @@ -44,67 +44,36 @@ float Battery_voltage() /* low battery voltage threshold */ float Battery_threshold() { - return hw_info.model == SOFTRF_MODEL_PRIME_MK2 || - (hw_info.model == SOFTRF_MODEL_STANDALONE && hw_info.revision == 16) || /* TTGO T3 V2.1.6 */ - hw_info.model == SOFTRF_MODEL_DONGLE || - hw_info.model == SOFTRF_MODEL_BADGE || - hw_info.model == SOFTRF_MODEL_MINI ? BATTERY_THRESHOLD_LIPO : - hw_info.model == SOFTRF_MODEL_UNI ? BATTERY_THRESHOLD_NIZNX2 : - BATTERY_THRESHOLD_NIMHX2; + return SoC->Battery_param(BATTERY_PARAM_THRESHOLD); } /* Battery is empty */ float Battery_cutoff() { - return hw_info.model == SOFTRF_MODEL_PRIME_MK2 || - (hw_info.model == SOFTRF_MODEL_STANDALONE && hw_info.revision == 16) || /* TTGO T3 V2.1.6 */ - hw_info.model == SOFTRF_MODEL_DONGLE || - hw_info.model == SOFTRF_MODEL_BADGE || - hw_info.model == SOFTRF_MODEL_MINI ? BATTERY_CUTOFF_LIPO : - hw_info.model == SOFTRF_MODEL_UNI ? BATTERY_CUTOFF_NIZNX2 : - BATTERY_CUTOFF_NIMHX2; + return SoC->Battery_param(BATTERY_PARAM_CUTOFF); +} + +/* Battery charge level (in %) */ +uint8_t Battery_charge() { + return (uint8_t) SoC->Battery_param(BATTERY_PARAM_CHARGE); } void Battery_loop() { if (isTimeToBattery()) { - float voltage = SoC->Battery_voltage(); - - if (hw_info.model == SOFTRF_MODEL_PRIME_MK2 || - (hw_info.model == SOFTRF_MODEL_STANDALONE && hw_info.revision == 16) || /* TTGO T3 V2.1.6 */ - hw_info.model == SOFTRF_MODEL_DONGLE || - hw_info.model == SOFTRF_MODEL_MINI || - hw_info.model == SOFTRF_MODEL_BADGE || - hw_info.model == SOFTRF_MODEL_UNI ) { + float voltage = SoC->Battery_param(BATTERY_PARAM_VOLTAGE); - if (voltage > BATTERY_THRESHOLD_INVALID && voltage < Battery_cutoff()) { - if (Battery_cutoff_count > 2) { - shutdown(SOFTRF_SHUTDOWN_LOWBAT); - } else { - Battery_cutoff_count++; - } + if (voltage > BATTERY_THRESHOLD_INVALID && voltage < Battery_cutoff()) { + if (Battery_cutoff_count > 2) { + shutdown(SOFTRF_SHUTDOWN_LOWBAT); } else { - Battery_cutoff_count = 0; + Battery_cutoff_count++; } + } else { + Battery_cutoff_count = 0; } Battery_voltage_cache = voltage; Battery_TimeMarker = millis(); } } - -uint8_t Battery_VoltsToPercent(float volts) { - if (volts < Battery_cutoff()) - return 0; - - if (volts > 4.2) - return 100; - - if (volts < 3.6) { - volts -= 3.3; - return (volts * 100) / 3; - } - - volts -= 3.6; - return 10 + (volts * 150 ); -} diff --git a/software/firmware/source/SoftRF/src/driver/Battery.h b/software/firmware/source/SoftRF/src/driver/Battery.h index c3e7bf6ff..9fd52dedf 100644 --- a/software/firmware/source/SoftRF/src/driver/Battery.h +++ b/software/firmware/source/SoftRF/src/driver/Battery.h @@ -27,10 +27,12 @@ #define BATTERY_THRESHOLD_LIPO 3.5 #define BATTERY_THRESHOLD_LIFE 3.0 #define BATTERY_THRESHOLD_NIZNX2 3.0 +#define BATTERY_THRESHOLD_USB 5.0 #define BATTERY_CUTOFF_NIMHX2 2.1 #define BATTERY_CUTOFF_LIPO 3.2 #define BATTERY_CUTOFF_NIZNX2 2.9 +#define BATTERY_CUTOFF_USB 4.9 enum { @@ -39,12 +41,20 @@ enum POWER_SAVE_GNSS = 2 }; +enum +{ + BATTERY_PARAM_VOLTAGE, + BATTERY_PARAM_THRESHOLD, + BATTERY_PARAM_CUTOFF, + BATTERY_PARAM_CHARGE +}; + void Battery_setup(void); void Battery_loop(void); float Battery_voltage(void); float Battery_threshold(void); float Battery_cutoff(void); -uint8_t Battery_VoltsToPercent(float); +uint8_t Battery_charge(void); extern unsigned long Battery_TimeMarker; diff --git a/software/firmware/source/SoftRF/src/driver/Bluetooth.cpp b/software/firmware/source/SoftRF/src/driver/Bluetooth.cpp index e7a8d57f9..a328e0b61 100644 --- a/software/firmware/source/SoftRF/src/driver/Bluetooth.cpp +++ b/software/firmware/source/SoftRF/src/driver/Bluetooth.cpp @@ -291,7 +291,7 @@ static void ESP32_Bluetooth_loop() oldDeviceConnected = deviceConnected; } if (deviceConnected && isTimeToBattery()) { - uint8_t battery_level = Battery_VoltsToPercent(Battery_voltage()); + uint8_t battery_level = Battery_charge(); pBATCharacteristic->setValue(&battery_level, 1); pBATCharacteristic->notify(); @@ -1285,7 +1285,7 @@ static void nRF52_Bluetooth_loop() } if (isTimeToBattery()) { - blebas.write(Battery_VoltsToPercent(Battery_voltage())); + blebas.write(Battery_charge()); } } diff --git a/software/firmware/source/SoftRF/src/platform/CC13XX.cpp b/software/firmware/source/SoftRF/src/platform/CC13XX.cpp index 6b093711e..2d963c801 100644 --- a/software/firmware/source/SoftRF/src/platform/CC13XX.cpp +++ b/software/firmware/source/SoftRF/src/platform/CC13XX.cpp @@ -28,6 +28,7 @@ #include "../driver/LED.h" #include "../driver/Sound.h" #include "../driver/OLED.h" +#include "../driver/Battery.h" #include "../protocol/data/NMEA.h" #include "../protocol/data/GDL90.h" #include "../protocol/data/D1090.h" @@ -559,14 +560,42 @@ static void CC13XX_Battery_setup() AONBatMonEnable(); } -static float CC13XX_Battery_voltage() +static float CC13XX_Battery_param(uint8_t param) { - if (AONBatMonNewBatteryMeasureReady()) { - // Get the VDDS voltage - cc13xx_vdd = AONBatMonBatteryVoltageGet(); + float rval; + + switch (param) + { + case BATTERY_PARAM_THRESHOLD: + rval = hw_info.model == SOFTRF_MODEL_UNI ? BATTERY_THRESHOLD_NIZNX2 : + BATTERY_THRESHOLD_NIMHX2; + break; + + case BATTERY_PARAM_CUTOFF: + rval = hw_info.model == SOFTRF_MODEL_UNI ? BATTERY_CUTOFF_NIZNX2 : + BATTERY_CUTOFF_NIMHX2; + break; + + case BATTERY_PARAM_CHARGE: + + /* TBD */ + + rval = 100; + break; + + case BATTERY_PARAM_VOLTAGE: + default: + + if (AONBatMonNewBatteryMeasureReady()) { + // Get the VDDS voltage + cc13xx_vdd = AONBatMonBatteryVoltageGet(); + } + + rval = ((cc13xx_vdd >> 8) & 0x7) + (float) (cc13xx_vdd & 0xFF) / 256.0; + break; } - return ((cc13xx_vdd >> 8) & 0x7) + (float) (cc13xx_vdd & 0xFF) / 256.0; + return rval; } void CC13XX_GNSS_PPS_Interrupt_handler() { @@ -767,7 +796,7 @@ const SoC_ops_t CC13XX_ops = { CC13XX_Display_loop, CC13XX_Display_fini, CC13XX_Battery_setup, - CC13XX_Battery_voltage, + CC13XX_Battery_param, CC13XX_GNSS_PPS_Interrupt_handler, CC13XX_get_PPS_TimeMarker, CC13XX_Baro_setup, diff --git a/software/firmware/source/SoftRF/src/platform/ESP32.cpp b/software/firmware/source/SoftRF/src/platform/ESP32.cpp index 636b9d633..db1d0dba1 100644 --- a/software/firmware/source/SoftRF/src/platform/ESP32.cpp +++ b/software/firmware/source/SoftRF/src/platform/ESP32.cpp @@ -251,7 +251,7 @@ static void ESP32_setup() axp.setLDO2Voltage (3300); // LoRa, AXP192 power-on value: 3300 axp.setLDO3Voltage (3000); // GPS, AXP192 power-on value: 2800 - pinMode(SOC_GPIO_PIN_TBEAM_V08_PMU_IRQ, INPUT_PULLUP); + pinMode(SOC_GPIO_PIN_TBEAM_V08_PMU_IRQ, INPUT /* INPUT_PULLUP */); attachInterrupt(digitalPinToInterrupt(SOC_GPIO_PIN_TBEAM_V08_PMU_IRQ), ESP32_PMU_Interrupt_handler, FALLING); @@ -1090,29 +1090,73 @@ static void ESP32_Battery_setup() } } -static float ESP32_Battery_voltage() +static float ESP32_Battery_param(uint8_t param) { - float voltage = 0.0; + float rval, voltage; - if ((hw_info.model == SOFTRF_MODEL_PRIME_MK2 && - hw_info.revision == 8) || - hw_info.model == SOFTRF_MODEL_SKYWATCH) { + switch (param) + { + case BATTERY_PARAM_THRESHOLD: + rval = (hw_info.model == SOFTRF_MODEL_PRIME_MK2 && hw_info.revision == 8) ? + BATTERY_THRESHOLD_LIPO + 0.1 : + hw_info.model == SOFTRF_MODEL_PRIME_MK2 || + /* TTGO T3 V2.1.6 */ + (hw_info.model == SOFTRF_MODEL_STANDALONE && hw_info.revision == 16) ? + BATTERY_THRESHOLD_LIPO : BATTERY_THRESHOLD_NIMHX2; + break; - /* T-Beam v08 and T-Watch have PMU */ - if (axp.isBatteryConnect()) { - voltage = axp.getBattVoltage(); + case BATTERY_PARAM_CUTOFF: + rval = (hw_info.model == SOFTRF_MODEL_PRIME_MK2 && hw_info.revision == 8) ? + BATTERY_CUTOFF_LIPO + 0.2 : + hw_info.model == SOFTRF_MODEL_PRIME_MK2 || + /* TTGO T3 V2.1.6 */ + (hw_info.model == SOFTRF_MODEL_STANDALONE && hw_info.revision == 16) ? + BATTERY_CUTOFF_LIPO : BATTERY_CUTOFF_NIMHX2; + break; + + case BATTERY_PARAM_CHARGE: + voltage = Battery_voltage(); + if (voltage < Battery_cutoff()) + return 0; + + if (voltage > 4.2) + return 100; + + if (voltage < 3.6) { + voltage -= 3.3; + return (voltage * 100) / 3; } - } else { - voltage = (float) read_voltage(); - /* T-Beam v02-v07 and T3 V2.1.6 have voltage divider 100k/100k on board */ - if (hw_info.model == SOFTRF_MODEL_PRIME_MK2 || - (esp32_board == ESP32_TTGO_V2_OLED && hw_info.revision == 16)) { - voltage += voltage; + voltage -= 3.6; + rval = 10 + (voltage * 150 ); + break; + + case BATTERY_PARAM_VOLTAGE: + default: + voltage = 0.0; + + if ((hw_info.model == SOFTRF_MODEL_PRIME_MK2 && + hw_info.revision == 8) || + hw_info.model == SOFTRF_MODEL_SKYWATCH) { + + /* T-Beam v08 and T-Watch have PMU */ + if (axp.isBatteryConnect()) { + voltage = axp.getBattVoltage(); + } + } else { + voltage = (float) read_voltage(); + + /* T-Beam v02-v07 and T3 V2.1.6 have voltage divider 100k/100k on board */ + if (hw_info.model == SOFTRF_MODEL_PRIME_MK2 || + (esp32_board == ESP32_TTGO_V2_OLED && hw_info.revision == 16)) { + voltage += voltage; + } } + rval = voltage * 0.001; + break; } - return (voltage * 0.001); + return rval; } static void IRAM_ATTR ESP32_GNSS_PPS_Interrupt_handler() @@ -1317,7 +1361,7 @@ const SoC_ops_t ESP32_ops = { ESP32_Display_loop, ESP32_Display_fini, ESP32_Battery_setup, - ESP32_Battery_voltage, + ESP32_Battery_param, ESP32_GNSS_PPS_Interrupt_handler, ESP32_get_PPS_TimeMarker, ESP32_Baro_setup, diff --git a/software/firmware/source/SoftRF/src/platform/ESP8266.cpp b/software/firmware/source/SoftRF/src/platform/ESP8266.cpp index 899079353..f9b0a1b60 100644 --- a/software/firmware/source/SoftRF/src/platform/ESP8266.cpp +++ b/software/firmware/source/SoftRF/src/platform/ESP8266.cpp @@ -28,6 +28,7 @@ #include "../driver/WiFi.h" #include "../driver/LED.h" #include "../driver/GNSS.h" +#include "../driver/Battery.h" #include #include @@ -333,9 +334,33 @@ static void ESP8266_Battery_setup() } -static float ESP8266_Battery_voltage() +static float ESP8266_Battery_param(uint8_t param) { - return analogRead (SOC_GPIO_PIN_BATTERY) / SOC_A0_VOLTAGE_DIVIDER ; + float rval; + + switch (param) + { + case BATTERY_PARAM_THRESHOLD: + rval = BATTERY_THRESHOLD_NIMHX2; + break; + + case BATTERY_PARAM_CUTOFF: + rval = BATTERY_CUTOFF_NIMHX2; + break; + + case BATTERY_PARAM_CHARGE: + /* TBD */ + + rval = 100; + break; + + case BATTERY_PARAM_VOLTAGE: + default: + rval = analogRead (SOC_GPIO_PIN_BATTERY) / SOC_A0_VOLTAGE_DIVIDER ; + break; + } + + return rval; } void ESP8266_GNSS_PPS_Interrupt_handler() @@ -434,7 +459,7 @@ const SoC_ops_t ESP8266_ops = { ESP8266_Display_loop, ESP8266_Display_fini, ESP8266_Battery_setup, - ESP8266_Battery_voltage, + ESP8266_Battery_param, ESP8266_GNSS_PPS_Interrupt_handler, ESP8266_get_PPS_TimeMarker, ESP8266_Baro_setup, diff --git a/software/firmware/source/SoftRF/src/platform/PSoC4.cpp b/software/firmware/source/SoftRF/src/platform/PSoC4.cpp index b83c8dba5..9d9620b92 100644 --- a/software/firmware/source/SoftRF/src/platform/PSoC4.cpp +++ b/software/firmware/source/SoftRF/src/platform/PSoC4.cpp @@ -28,6 +28,7 @@ #include "../driver/OLED.h" #include "../driver/Baro.h" #include "../driver/Sound.h" +#include "../driver/Battery.h" #include "../protocol/data/NMEA.h" #include "../protocol/data/GDL90.h" #include "../protocol/data/D1090.h" @@ -404,41 +405,79 @@ static void PSoC4_Battery_setup() #endif } -static float PSoC4_Battery_voltage() +static float PSoC4_Battery_param(uint8_t param) { -#if defined(BAT_MON_DISABLE) - if (PSoC4_bat_mon_disable) { - return 0; - } else -#endif - { - uint16_t mV = 0; + float rval, voltage; - if (hw_info.model == SOFTRF_MODEL_MINI && - PSoC4_state == PSOC4_ACTIVE) { - /* GPIO7 is shared between USER_KEY and VBAT_ADC_CTL functions */ - int user_key_state = digitalRead(SOC_GPIO_PIN_BUTTON); + switch (param) + { + case BATTERY_PARAM_THRESHOLD: + rval = hw_info.model == SOFTRF_MODEL_MINI ? BATTERY_THRESHOLD_LIPO : + BATTERY_THRESHOLD_NIMHX2; + break; + + case BATTERY_PARAM_CUTOFF: + rval = hw_info.model == SOFTRF_MODEL_MINI ? BATTERY_CUTOFF_LIPO : + BATTERY_CUTOFF_NIMHX2; + break; + + case BATTERY_PARAM_CHARGE: + voltage = Battery_voltage(); + if (voltage < Battery_cutoff()) + return 0; + + if (voltage > 4.2) + return 100; + + if (voltage < 3.6) { + voltage -= 3.3; + return (voltage * 100) / 3; + } - /* if the key is not pressed down - activate VBAT_ADC_CTL */ - if (user_key_state == HIGH) { - pinMode(VBAT_ADC_CTL,OUTPUT); - digitalWrite(VBAT_ADC_CTL,LOW); - } + voltage -= 3.6; + rval = 10 + (voltage * 150 ); + break; - mV = analogRead(SOC_GPIO_PIN_BATTERY); + case BATTERY_PARAM_VOLTAGE: + default: - /* restore previous state of VBAT_ADC_CTL pin */ - if (user_key_state == HIGH) { - /* - * CubeCell-GPS has external 10K VDD pullup resistor - * connected to GPIO7 (USER_KEY / VBAT_ADC_CTL) pin - */ - pinMode(VBAT_ADC_CTL, INPUT); + #if defined(BAT_MON_DISABLE) + if (PSoC4_bat_mon_disable) { + rval = 0; + } else + #endif + { + uint16_t mV = 0; + + if (hw_info.model == SOFTRF_MODEL_MINI && + PSoC4_state == PSOC4_ACTIVE) { + /* GPIO7 is shared between USER_KEY and VBAT_ADC_CTL functions */ + int user_key_state = digitalRead(SOC_GPIO_PIN_BUTTON); + + /* if the key is not pressed down - activate VBAT_ADC_CTL */ + if (user_key_state == HIGH) { + pinMode(VBAT_ADC_CTL,OUTPUT); + digitalWrite(VBAT_ADC_CTL,LOW); + } + + mV = analogRead(SOC_GPIO_PIN_BATTERY); + + /* restore previous state of VBAT_ADC_CTL pin */ + if (user_key_state == HIGH) { + /* + * CubeCell-GPS has external 10K VDD pullup resistor + * connected to GPIO7 (USER_KEY / VBAT_ADC_CTL) pin + */ + pinMode(VBAT_ADC_CTL, INPUT); + } } - } - return mV * SOC_ADC_VOLTAGE_DIV / 1000.0; + rval = mV * SOC_ADC_VOLTAGE_DIV / 1000.0; + } + break; } + + return rval; } void PSoC4_GNSS_PPS_Interrupt_handler() { @@ -646,7 +685,7 @@ const SoC_ops_t PSoC4_ops = { PSoC4_Display_loop, PSoC4_Display_fini, PSoC4_Battery_setup, - PSoC4_Battery_voltage, + PSoC4_Battery_param, PSoC4_GNSS_PPS_Interrupt_handler, PSoC4_get_PPS_TimeMarker, PSoC4_Baro_setup, diff --git a/software/firmware/source/SoftRF/src/platform/RPi.cpp b/software/firmware/source/SoftRF/src/platform/RPi.cpp index b3e3fd2e2..c6d5e1d3b 100644 --- a/software/firmware/source/SoftRF/src/platform/RPi.cpp +++ b/software/firmware/source/SoftRF/src/platform/RPi.cpp @@ -420,9 +420,33 @@ static void RPi_Battery_setup() /* TBD */ } -static float RPi_Battery_voltage() +static float RPi_Battery_param(uint8_t param) { - return 0.0; /* TBD */ + float rval; + + switch (param) + { + case BATTERY_PARAM_THRESHOLD: + rval = BATTERY_THRESHOLD_USB; + break; + + case BATTERY_PARAM_CUTOFF: + rval = BATTERY_CUTOFF_USB; + break; + + case BATTERY_PARAM_CHARGE: + /* TBD */ + + rval = 100; + break; + + case BATTERY_PARAM_VOLTAGE: + default: + rval = BATTERY_THRESHOLD_USB + 0.05; + break; + } + + return rval; } void RPi_GNSS_PPS_Interrupt_handler() { @@ -520,7 +544,7 @@ const SoC_ops_t RPi_ops = { RPi_Display_loop, RPi_Display_fini, RPi_Battery_setup, - RPi_Battery_voltage, + RPi_Battery_param, NULL, RPi_get_PPS_TimeMarker, NULL, diff --git a/software/firmware/source/SoftRF/src/platform/STM32.cpp b/software/firmware/source/SoftRF/src/platform/STM32.cpp index 83dcdc449..2661a1dc7 100644 --- a/software/firmware/source/SoftRF/src/platform/STM32.cpp +++ b/software/firmware/source/SoftRF/src/platform/STM32.cpp @@ -549,19 +549,57 @@ static void STM32_Battery_setup() } -static float STM32_Battery_voltage() +static float STM32_Battery_param(uint8_t param) { + float rval, voltage; + + switch (param) + { + case BATTERY_PARAM_THRESHOLD: + rval = hw_info.model == SOFTRF_MODEL_DONGLE ? BATTERY_THRESHOLD_LIPO : + BATTERY_THRESHOLD_NIMHX2; + break; + + case BATTERY_PARAM_CUTOFF: + rval = hw_info.model == SOFTRF_MODEL_DONGLE ? BATTERY_CUTOFF_LIPO : + BATTERY_CUTOFF_NIMHX2; + break; + + case BATTERY_PARAM_CHARGE: + voltage = Battery_voltage(); + if (voltage < Battery_cutoff()) + return 0; + + if (voltage > 4.2) + return 100; + + if (voltage < 3.6) { + voltage -= 3.3; + return (voltage * 100) / 3; + } + + voltage -= 3.6; + rval = 10 + (voltage * 150 ); + break; + + case BATTERY_PARAM_VOLTAGE: + default: + #ifdef __LL_ADC_CALC_VREFANALOG_VOLTAGE - int32_t Vref = (__LL_ADC_CALC_VREFANALOG_VOLTAGE(analogRead(AVREF), LL_ADC_RESOLUTION)); + int32_t Vref = (__LL_ADC_CALC_VREFANALOG_VOLTAGE(analogRead(AVREF), LL_ADC_RESOLUTION)); #else - int32_t Vref = (VREFINT * ADC_RANGE / analogRead(AVREF)); // ADC sample to mV + int32_t Vref = (VREFINT * ADC_RANGE / analogRead(AVREF)); // ADC sample to mV #endif - int32_t mV = (__LL_ADC_CALC_DATA_TO_VOLTAGE(Vref, - analogRead(SOC_GPIO_PIN_BATTERY), - LL_ADC_RESOLUTION)); + int32_t mV = (__LL_ADC_CALC_DATA_TO_VOLTAGE(Vref, + analogRead(SOC_GPIO_PIN_BATTERY), + LL_ADC_RESOLUTION)); + + rval = mV * SOC_ADC_VOLTAGE_DIV / 1000.0; + break; + } - return mV * SOC_ADC_VOLTAGE_DIV / 1000.0; + return rval; } void STM32_GNSS_PPS_Interrupt_handler() { @@ -776,7 +814,7 @@ const SoC_ops_t STM32_ops = { STM32_Display_loop, STM32_Display_fini, STM32_Battery_setup, - STM32_Battery_voltage, + STM32_Battery_param, STM32_GNSS_PPS_Interrupt_handler, STM32_get_PPS_TimeMarker, STM32_Baro_setup, diff --git a/software/firmware/source/SoftRF/src/platform/nRF52.cpp b/software/firmware/source/SoftRF/src/platform/nRF52.cpp index 01bdc00f1..f39347f1d 100644 --- a/software/firmware/source/SoftRF/src/platform/nRF52.cpp +++ b/software/firmware/source/SoftRF/src/platform/nRF52.cpp @@ -1007,30 +1007,66 @@ static void nRF52_Battery_setup() } -static float nRF52_Battery_voltage() +static float nRF52_Battery_param(uint8_t param) { - float raw; + float rval, voltage; - // Set the analog reference to 3.0V (default = 3.6V) - analogReference(AR_INTERNAL_3_0); + switch (param) + { + case BATTERY_PARAM_THRESHOLD: + rval = hw_info.model == SOFTRF_MODEL_BADGE ? BATTERY_THRESHOLD_LIPO : + BATTERY_THRESHOLD_NIMHX2; + break; - // Set the resolution to 12-bit (0..4095) - analogReadResolution(12); // Can be 8, 10, 12 or 14 + case BATTERY_PARAM_CUTOFF: + rval = hw_info.model == SOFTRF_MODEL_BADGE ? BATTERY_CUTOFF_LIPO : + BATTERY_CUTOFF_NIMHX2; + break; - // Let the ADC settle - delay(1); + case BATTERY_PARAM_CHARGE: + voltage = Battery_voltage(); + if (voltage < Battery_cutoff()) + return 0; - // Get the raw 12-bit, 0..3000mV ADC value - raw = analogRead(SOC_GPIO_PIN_BATTERY); + if (voltage > 4.2) + return 100; + + if (voltage < 3.6) { + voltage -= 3.3; + return (voltage * 100) / 3; + } + + voltage -= 3.6; + rval = 10 + (voltage * 150 ); + break; + + case BATTERY_PARAM_VOLTAGE: + default: - // Set the ADC back to the default settings - analogReference(AR_DEFAULT); - analogReadResolution(10); + // Set the analog reference to 3.0V (default = 3.6V) + analogReference(AR_INTERNAL_3_0); - // Convert the raw value to compensated mv, taking the resistor- - // divider into account (providing the actual LIPO voltage) - // ADC range is 0..3000mV and resolution is 12-bit (0..4095) - return raw * REAL_VBAT_MV_PER_LSB * 0.001; + // Set the resolution to 12-bit (0..4095) + analogReadResolution(12); // Can be 8, 10, 12 or 14 + + // Let the ADC settle + delay(1); + + // Get the raw 12-bit, 0..3000mV ADC value + voltage = analogRead(SOC_GPIO_PIN_BATTERY); + + // Set the ADC back to the default settings + analogReference(AR_DEFAULT); + analogReadResolution(10); + + // Convert the raw value to compensated mv, taking the resistor- + // divider into account (providing the actual LIPO voltage) + // ADC range is 0..3000mV and resolution is 12-bit (0..4095) + rval = voltage * REAL_VBAT_MV_PER_LSB * 0.001; + break; + } + + return rval; } void nRF52_GNSS_PPS_Interrupt_handler() { @@ -1321,7 +1357,7 @@ const SoC_ops_t nRF52_ops = { nRF52_Display_loop, nRF52_Display_fini, nRF52_Battery_setup, - nRF52_Battery_voltage, + nRF52_Battery_param, nRF52_GNSS_PPS_Interrupt_handler, nRF52_get_PPS_TimeMarker, nRF52_Baro_setup, diff --git a/software/firmware/source/SoftRF/src/system/SoC.h b/software/firmware/source/SoftRF/src/system/SoC.h index 733e1187d..d40cb92a3 100644 --- a/software/firmware/source/SoftRF/src/system/SoC.h +++ b/software/firmware/source/SoftRF/src/system/SoC.h @@ -64,7 +64,7 @@ typedef struct SoC_ops_struct { void (*Display_loop)(); void (*Display_fini)(int); void (*Battery_setup)(); - float (*Battery_voltage)(); + float (*Battery_param)(uint8_t); void (*GNSS_PPS_handler)(); unsigned long (*get_PPS_TimeMarker)(); bool (*Baro_setup)(); diff --git a/software/firmware/source/SoftRF/src/ui/Time_EPD.cpp b/software/firmware/source/SoftRF/src/ui/Time_EPD.cpp index 4e940115f..5deb80024 100644 --- a/software/firmware/source/SoftRF/src/ui/Time_EPD.cpp +++ b/software/firmware/source/SoftRF/src/ui/Time_EPD.cpp @@ -108,7 +108,7 @@ void EPD_time_loop() } u8g2Fonts.setCursor(display->width() - 5, 15); - u8g2Fonts.print(Battery_VoltsToPercent(Battery_voltage()) / 20); + u8g2Fonts.print(Battery_charge() / 20); display->setFont(&FreeMonoBold24pt7b); display->getTextBounds(buf_hm, 0, 0, &tbx, &tby, &tbw, &tbh);