refactor(radio): unified battery and RTC voltage (#6065)

This commit is contained in:
Raphael Coeffic
2025-04-27 13:46:18 +10:00
committed by GitHub
parent f237e9d40b
commit b52e7717ab
13 changed files with 185 additions and 154 deletions
+2 -54
View File
@@ -29,10 +29,6 @@ const etx_hal_adc_inputs_t* _hal_adc_inputs = nullptr;
static uint16_t adcValues[MAX_ANALOG_INPUTS] __DMA_NO_CACHE;
#if defined(CSD203_SENSOR)
extern uint16_t getCSD203BatteryVoltage(void);
#endif
bool adcInit(const etx_hal_adc_driver_t* driver)
{
// Init buffer, provides non random values before mixer task starts
@@ -197,7 +193,7 @@ void adcCalibSetMinMax()
}
xpot.stepsCount = ++count;
writeXPotCalib(i, xpot.steps, count);
writeXPotCalib(i, xpot.steps, count);
}
}
}
@@ -234,19 +230,6 @@ void adcCalibStore()
storageDirty(EE_GENERAL);
}
uint16_t getRTCBatteryVoltage()
{
// anaIn() outputs value divided by (1 << ANALOG_SCALE)
if (adcGetMaxInputs(ADC_INPUT_RTC_BAT) < 1) return 0;
#if defined(STM32F413xx)
return (anaIn(adcGetInputOffset(ADC_INPUT_RTC_BAT)) * ADC_VREF_PREC2) /
(1024 >> ANALOG_SCALE);
#else
return (anaIn(adcGetInputOffset(ADC_INPUT_RTC_BAT)) * ADC_VREF_PREC2) /
(2048 >> ANALOG_SCALE);
#endif
}
uint16_t getAnalogValue(uint8_t index)
{
if (index >= MAX_ANALOG_INPUTS) return 0;
@@ -307,41 +290,6 @@ JitterMeter<uint16_t> avgJitter[MAX_ANALOG_INPUTS];
tmr10ms_t jitterResetTime = 0;
#endif
uint16_t getBatteryVoltage()
{
#if defined(CSD203_SENSOR) && !defined(SIMU)
return getCSD203BatteryVoltage() / 10;
#else
// using filtered ADC value on purpose
if (adcGetMaxInputs(ADC_INPUT_VBAT) < 1) return 0;
int32_t instant_vbat = anaIn(adcGetInputOffset(ADC_INPUT_VBAT));
// TODO: remove BATT_SCALE / BATTERY_DIVIDER defines
#if defined(VBAT_MOSFET_DROP)
// 1000 is used as multiplier for both numerator and denominator to allow to stay in integer domain
return (uint16_t)((instant_vbat * ADC_VREF_PREC2 * ((((1000 + g_eeGeneral.txVoltageCalibration)) * (VBAT_DIV_R2 + VBAT_DIV_R1)) / VBAT_DIV_R1)) / (2*RESX*1000)) + VBAT_MOSFET_DROP;
#elif defined(BATT_SCALE)
instant_vbat =
(instant_vbat * BATT_SCALE * (128 + g_eeGeneral.txVoltageCalibration)) /
BATTERY_DIVIDER;
// add voltage drop because of the diode TODO check if this is needed, but
// removal will break existing calibrations!
instant_vbat += VOLTAGE_DROP;
return (uint16_t)instant_vbat;
#elif defined(VOLTAGE_DROP)
instant_vbat = ((instant_vbat * (1000 + g_eeGeneral.txVoltageCalibration)) /
BATTERY_DIVIDER);
// add voltage drop because of the diode
// removal will break existing calibrations!
instant_vbat += VOLTAGE_DROP;
return (uint16_t)instant_vbat;
#else
return (uint16_t)((instant_vbat * (1000 + g_eeGeneral.txVoltageCalibration)) /
BATTERY_DIVIDER);
#endif
#endif
}
static uint32_t apply_low_pass_filter(uint32_t v, uint32_t v_prev,
bool is_main_input)
{
@@ -470,7 +418,7 @@ void getADC()
#endif
DEBUG_TIMER_START(debugTimerAdcRead);
if (!adcRead()) TRACE("adcRead failed");
if (!adcRead()) { TRACE("adcRead failed"); }
DEBUG_TIMER_STOP(debugTimerAdcRead);
for (uint8_t x = 0; x < max_analogs; x++) {
+9 -14
View File
@@ -35,6 +35,8 @@
#define ANALOG_SCALE 1
#define JITTER_ALPHA (1<<JITTER_FILTER_STRENGTH)
#define ADC_MAX_FILTERED (ADC_MAX_VALUE >> ANALOG_SCALE)
enum {
ADC_INPUT_MAIN=0, // gimbals / wheel + throttle
ADC_INPUT_FLEX,
@@ -85,11 +87,9 @@ struct etx_hal_adc_driver_t {
bool adcInit(const etx_hal_adc_driver_t* driver);
// void adcDeInit();
bool adcRead();
uint16_t getBatteryVoltage();
uint16_t getRTCBatteryVoltage();
uint16_t getAnalogValue(uint8_t index);
void setAnalogValue(uint8_t index, uint16_t value);
bool adcRead();
uint16_t getAnalogValue(uint8_t index);
void setAnalogValue(uint8_t index, uint16_t value);
uint16_t* getAnalogValues();
// Run calibration steps:
@@ -114,10 +114,9 @@ extern JitterMeter<uint16_t> avgJitter[MAX_ANALOG_INPUTS];
void getADC();
uint16_t anaIn(uint8_t chan);
uint32_t anaIn_diag(uint8_t chan);
uint16_t getBatteryVoltage();
// Warning:
// STM32 uses a 25K+25K voltage divider bridge to measure the battery voltage
// STM32 uses a voltage divider bridge to measure the battery voltage
// Measuring VBAT puts considerable drain (22 µA) on the battery instead of
// normal drain (~10 nA)
void enableVBatBridge();
@@ -143,10 +142,6 @@ const char* adcGetInputShortLabel(uint8_t type, uint8_t idx);
void adcSetInputMask(uint32_t mask);
uint32_t adcGetInputMask();
// To be implemented by the target driver
// int8_t adcGetVRTC();
// int8_t adcGetVBAT();
// const char* adcGetStickName(uint8_t idx);
// const char* adcGetPotName(uint8_t idx);
// uint8_t adcGetMaxSticks();
// uint8_t adcGetMaxPots();
// these 2 must be implemented as part of board drivers
uint16_t getBatteryVoltage();
uint16_t getRTCBatteryVoltage();