Skip to content

Commit

Permalink
clean-up of Battery API
Browse files Browse the repository at this point in the history
  • Loading branch information
lyusupov committed Mar 15, 2021
1 parent 2e3ac3a commit a22a5e1
Show file tree
Hide file tree
Showing 12 changed files with 348 additions and 134 deletions.
61 changes: 15 additions & 46 deletions software/firmware/source/SoftRF/src/driver/Battery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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 );
}
12 changes: 11 additions & 1 deletion software/firmware/source/SoftRF/src/driver/Battery.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions software/firmware/source/SoftRF/src/driver/Bluetooth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -1285,7 +1285,7 @@ static void nRF52_Bluetooth_loop()
}

if (isTimeToBattery()) {
blebas.write(Battery_VoltsToPercent(Battery_voltage()));
blebas.write(Battery_charge());
}
}

Expand Down
41 changes: 35 additions & 6 deletions software/firmware/source/SoftRF/src/platform/CC13XX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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,
Expand Down
78 changes: 61 additions & 17 deletions software/firmware/source/SoftRF/src/platform/ESP32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand Down
31 changes: 28 additions & 3 deletions software/firmware/source/SoftRF/src/platform/ESP8266.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "../driver/WiFi.h"
#include "../driver/LED.h"
#include "../driver/GNSS.h"
#include "../driver/Battery.h"

#include <ets_sys.h>
#include <osapi.h>
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand Down
Loading

0 comments on commit a22a5e1

Please sign in to comment.