From fe24d96f41d7ad0b573644726d686b1800258671 Mon Sep 17 00:00:00 2001 From: victor Date: Fri, 7 Oct 2022 10:43:34 +0200 Subject: [PATCH 1/4] Enabled external vbat measurements to be interpreted as system voltage when. This can be handy for BQD. --- src/hal/src/pm_stm32f4.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/hal/src/pm_stm32f4.c b/src/hal/src/pm_stm32f4.c index 2beed40135..39da3debc2 100644 --- a/src/hal/src/pm_stm32f4.c +++ b/src/hal/src/pm_stm32f4.c @@ -256,8 +256,17 @@ static void pmGracefulShutdown() void pmSyslinkUpdate(SyslinkPacket *slp) { if (slp->type == SYSLINK_PM_BATTERY_STATE) { + // First byte of the packet contains some PM flags such as USB power, charging etc. memcpy(&pmSyslinkInfo, &slp->data[0], sizeof(pmSyslinkInfo)); + + // If using voltage measurements from external battery, we'll set the + // voltage to this instead of the one sent from syslink. + if (isExtBatVoltDeckPinSet) { + pmSetBatteryVoltage(extBatteryVoltage); + } else { pmSetBatteryVoltage(pmSyslinkInfo.vBat); + } + #ifdef PM_SYSTLINK_INLCUDE_TEMP temp = pmSyslinkInfo.temp; #endif From 96ed299a37c486d52171df9d30a5311f7f26b172 Mon Sep 17 00:00:00 2001 From: victor Date: Fri, 7 Oct 2022 10:46:48 +0200 Subject: [PATCH 2/4] Added voltage multiplier and amp per volt values in Kconfig instead of in source code. Added them as mili Volts/Amps, because kbuild does not support floats. --- src/deck/drivers/src/Kconfig | 15 ++++++++++----- src/deck/drivers/src/bigquad.c | 9 +++++---- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/deck/drivers/src/Kconfig b/src/deck/drivers/src/Kconfig index 345eab1acd..b236ea7354 100644 --- a/src/deck/drivers/src/Kconfig +++ b/src/deck/drivers/src/Kconfig @@ -65,18 +65,23 @@ config DECK_BIGQUAD accessories such as external receiver (CPPM input) and GPS. It can also monitor battery voltage and current. -config DECK_BIGQUAD_ENABLE - bool "Enable Bigquad deck capabilities" +config DECK_BIGQUAD_BAT_VOLT_MULT_MV + int "Battery voltage divider multiplier (in mV)." depends on DECK_BIGQUAD - default n + default 7800 + +config DECK_BIGQUAD_BAT_AMP_PER_VOLT_MA + int "Battery ampere per volt (in mA)." + depends on DECK_BIGQUAD + default 1000 config DECK_BIGQUAD_ENABLE_OSD - depends on DECK_BIGQUAD_ENABLE + depends on DECK_BIGQUAD bool "Enable Bigquad deck OSD" default n config DECK_BIGQUAD_ENABLE_PM - depends on DECK_BIGQUAD_ENABLE + depends on DECK_BIGQUAD bool "Enable Bigquad deck PM" default n diff --git a/src/deck/drivers/src/bigquad.c b/src/deck/drivers/src/bigquad.c index 87ca3be8d8..03139082f0 100644 --- a/src/deck/drivers/src/bigquad.c +++ b/src/deck/drivers/src/bigquad.c @@ -44,11 +44,11 @@ #include "task.h" #define BIGQUAD_BAT_VOLT_PIN DECK_GPIO_MISO -#define BIGQUAD_BAT_VOLT_MULT 7.8f +#define BIGQUAD_BAT_VOLT_MULT (CONFIG_DECK_BIGQUAD_BAT_VOLT_MULT_MV / 1000.0) #define BIGQUAD_BAT_CURR_PIN DECK_GPIO_SCK -#define BIGQUAD_BAT_AMP_PER_VOLT 1.0f +#define BIGQUAD_BAT_AMP_PER_VOLT (CONFIG_DECK_BIGQUAD_BAT_AMP_PER_VOLT_MA / 1000.0) -#ifdef CONFIG_DECK_BIGQUAD_ENABLE +#ifdef CONFIG_DECK_BIGQUAD //Hardware configuration static bool isInit; @@ -132,4 +132,5 @@ PARAM_GROUP_START(deck) PARAM_ADD_CORE(PARAM_UINT8 | PARAM_RONLY, bcBigQuad, &isInit) PARAM_GROUP_STOP(deck) -#endif // CONFIG_DECK_BIGQUAD_ENABLE + +#endif // CONFIG_DECK_BIGQUAD From 3ca801048b127d599d7cd48a96e2e5b9515daed5 Mon Sep 17 00:00:00 2001 From: victor Date: Fri, 7 Oct 2022 10:50:17 +0200 Subject: [PATCH 3/4] Changed bit flags that is sent via syslink of PM status to easier decide what power state we should be in, regardless of platform. We still need to know if we're using the BQD though. --- src/hal/src/pm_stm32f4.c | 47 +++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/src/hal/src/pm_stm32f4.c b/src/hal/src/pm_stm32f4.c index 39da3debc2..d007612613 100644 --- a/src/hal/src/pm_stm32f4.c +++ b/src/hal/src/pm_stm32f4.c @@ -57,9 +57,10 @@ typedef struct _PmSyslinkInfo uint8_t flags; struct { - uint8_t chg : 1; - uint8_t pgood : 1; - uint8_t unused : 6; + uint8_t isCharging : 1; + uint8_t usbPluggedIn : 1; + uint8_t canCharge : 1; + uint8_t unused : 5; }; }; float vBat; @@ -102,6 +103,8 @@ static PmSyslinkInfo pmSyslinkInfo; static uint8_t batteryLevel; +static bool usingBigQuadDeck; + static void pmSetBatteryVoltage(float voltage); const static float LiPoTypicalChargeCurve[10] = @@ -264,7 +267,7 @@ void pmSyslinkUpdate(SyslinkPacket *slp) if (isExtBatVoltDeckPinSet) { pmSetBatteryVoltage(extBatteryVoltage); } else { - pmSetBatteryVoltage(pmSyslinkInfo.vBat); + pmSetBatteryVoltage(pmSyslinkInfo.vBat); } #ifdef PM_SYSTLINK_INLCUDE_TEMP @@ -282,31 +285,38 @@ void pmSetChargeState(PMChargeStates chgState) PMStates pmUpdateState() { - PMStates state; - bool isCharging = pmSyslinkInfo.chg; - bool isPgood = pmSyslinkInfo.pgood; - uint32_t batteryLowTime; + bool usbPluggedIn = pmSyslinkInfo.usbPluggedIn; + bool isCharging = pmSyslinkInfo.isCharging; + PMStates nextState; - batteryLowTime = xTaskGetTickCount() - batteryLowTimeStamp; + uint32_t batteryLowTime = xTaskGetTickCount() - batteryLowTimeStamp; - if (isPgood && !isCharging) + if (usingBigQuadDeck) { - state = charged; + // Need special care if big quad deck is connected, because the battery voltage + // makes the usbPluggedIn high. + nextState = battery; } - else if (isPgood && isCharging) + else if (usbPluggedIn && !isCharging) { - state = charging; + nextState = charged; } - else if (!isPgood && !isCharging && (batteryLowTime > PM_BAT_LOW_TIMEOUT)) + else if (usbPluggedIn && isCharging) { - state = lowPower; + nextState = charging; } else { - state = battery; + nextState = battery; } - return state; + if (nextState == battery && batteryLowTime > PM_BAT_LOW_TIMEOUT) + { + // This is to avoid setting state to lowPower when we're plugged in to USB. + nextState = lowPower; + } + + return nextState; } void pmEnableExtBatteryCurrMeasuring(const deckPin_t pin, float ampPerVolt) @@ -386,6 +396,9 @@ void pmTask(void *param) pmSetChargeState(charge500mA); systemWaitStart(); + // Figure out if we're using the big quad deck, which is needed to estimate the correct PM state. + usingBigQuadDeck = deckFindDriverByName("bcBigQuad") != NULL; + while(1) { vTaskDelay(100); From 86adde25b2276619017ac451bb244b6f5480ba7d Mon Sep 17 00:00:00 2001 From: victor Date: Thu, 13 Oct 2022 13:26:20 +0200 Subject: [PATCH 4/4] Removed dependency from pm to BQD. Made BQD driver call into pm instead to ignore charging/charged state, which is needed for correct low-battery warnings when using BQD. --- src/deck/drivers/src/bigquad.c | 4 ++++ src/hal/interface/pm.h | 6 ++++++ src/hal/src/pm_stm32f4.c | 14 +++++++------- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/deck/drivers/src/bigquad.c b/src/deck/drivers/src/bigquad.c index 03139082f0..42ca446703 100644 --- a/src/deck/drivers/src/bigquad.c +++ b/src/deck/drivers/src/bigquad.c @@ -83,6 +83,10 @@ static void bigquadInit(DeckInfo *info) DEBUG_PRINT("Switching to brushless.\n"); motorsInit(motorMapBigQuadDeck); extRxInit(); + + // Ignore charging/charged state to allow low-battery warning. + pmIgnoreChargedState(true); + #ifdef CONFIG_DECK_BIGQUAD_ENABLE_PM pmEnableExtBatteryVoltMeasuring(BIGQUAD_BAT_VOLT_PIN, BIGQUAD_BAT_VOLT_MULT); pmEnableExtBatteryCurrMeasuring(BIGQUAD_BAT_CURR_PIN, BIGQUAD_BAT_AMP_PER_VOLT); diff --git a/src/hal/interface/pm.h b/src/hal/interface/pm.h index 8d0c96a89f..6e16de6f60 100644 --- a/src/hal/interface/pm.h +++ b/src/hal/interface/pm.h @@ -132,6 +132,12 @@ void pmEnableExtBatteryCurrMeasuring(const deckPin_t pin, float ampPerVolt); */ float pmMeasureExtBatteryCurrent(void); +/* + * Ignore charging/charge state in the PM state machine. + * This can be useful if a platform doesn't have a charger. + */ +void pmIgnoreChargedState(bool ignore); + /** * Register a callback to be run when the NRF51 signals shutdown */ diff --git a/src/hal/src/pm_stm32f4.c b/src/hal/src/pm_stm32f4.c index d007612613..b52cb84169 100644 --- a/src/hal/src/pm_stm32f4.c +++ b/src/hal/src/pm_stm32f4.c @@ -103,7 +103,7 @@ static PmSyslinkInfo pmSyslinkInfo; static uint8_t batteryLevel; -static bool usingBigQuadDeck; +static bool ignoreChargedState = false; static void pmSetBatteryVoltage(float voltage); @@ -291,10 +291,9 @@ PMStates pmUpdateState() uint32_t batteryLowTime = xTaskGetTickCount() - batteryLowTimeStamp; - if (usingBigQuadDeck) + if (ignoreChargedState) { - // Need special care if big quad deck is connected, because the battery voltage - // makes the usbPluggedIn high. + // For some scenarios we might not care about the charging/charged state. nextState = battery; } else if (usbPluggedIn && !isCharging) @@ -365,6 +364,10 @@ float pmMeasureExtBatteryVoltage(void) return voltage; } +void pmIgnoreChargedState(bool ignore) { + ignoreChargedState = ignore; +} + bool pmIsBatteryLow(void) { return (pmState == lowPower); } @@ -396,9 +399,6 @@ void pmTask(void *param) pmSetChargeState(charge500mA); systemWaitStart(); - // Figure out if we're using the big quad deck, which is needed to estimate the correct PM state. - usingBigQuadDeck = deckFindDriverByName("bcBigQuad") != NULL; - while(1) { vTaskDelay(100);