From 9d09c6950c696887910bacf78a8bbf21ead6ac9d Mon Sep 17 00:00:00 2001 From: XuGuohui Date: Mon, 26 Apr 2021 13:39:22 +0800 Subject: [PATCH 1/2] [system]: power source improvement. --- system/src/system_power_manager.cpp | 34 +++++++++++++++++++++++++---- system/src/system_power_manager.h | 4 ++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/system/src/system_power_manager.cpp b/system/src/system_power_manager.cpp index d35f1b809b..b18f906d93 100644 --- a/system/src/system_power_manager.cpp +++ b/system/src/system_power_manager.cpp @@ -70,6 +70,9 @@ constexpr uint16_t PMIC_FAULT_TERM_CHARGE_CURRENT = 128; // mA constexpr system_tick_t BATTERY_REPEATED_CHARGED_WINDOW = 5000; // ms constexpr uint8_t BATTERY_REPEATED_CHARGED_COUNT = 2; +constexpr system_tick_t DPDM_PERIOD = 1000; +constexpr uint8_t DPDM_RETRY_COUNT = 3; + constexpr hal_power_config defaultPowerConfig = { .flags = 0, .version = 0, @@ -321,10 +324,11 @@ void PowerManager::handleUpdate() { break; } } else { - if (g_batteryState == BATTERY_STATE_DISCHARGING) { - src = POWER_SOURCE_BATTERY; - } else { - src = POWER_SOURCE_UNKNOWN; + // Do not update power source if power is good and in DPDM + if (!pwr_good || !power.isInDPDM()) { + if (g_batteryState == BATTERY_STATE_DISCHARGING) { + src = POWER_SOURCE_BATTERY; + } } } @@ -424,6 +428,7 @@ void PowerManager::loop(void* arg) { } self->checkWatchdog(); self->deduceBatteryStateLoop(); + self->runDpdm(); } exit: @@ -505,6 +510,27 @@ void PowerManager::initDefault(bool dpdm) { clearIntermediateBatteryState(STATE_ALL); } +void PowerManager::runDpdm() { + static system_tick_t lastRun = 0; + + if (millis() - lastRun >= DPDM_PERIOD) { + lastRun = millis(); + if (g_powerSource == POWER_SOURCE_VIN && HAL_USB_Get_State(nullptr) >= HAL_USB_STATE_POWERED) { + if (dpdmRetry_ >= DPDM_RETRY_COUNT) { + return; + } + PMIC power(true); + if (power.isInDPDM()) { + return; + } + power.enableDPDM(); + dpdmRetry_++; + } else { + dpdmRetry_ = 0; + } + } +} + void PowerManager::confirmBatteryState(battery_state_t from, battery_state_t to) { // Whenever we confirm the battery state, we re-start the monitor cycle batMonitorTimeStamp_ = millis(); diff --git a/system/src/system_power_manager.h b/system/src/system_power_manager.h index 8410fdfc1c..3f11120569 100644 --- a/system/src/system_power_manager.h +++ b/system/src/system_power_manager.h @@ -66,6 +66,8 @@ class PowerManager { void batteryStateTransitioningTo(battery_state_t targetState, bool count = true); void clearIntermediateBatteryState(uint8_t state); + void runDpdm(); + static power_source_t powerSourceFromStatus(uint8_t status); private: @@ -108,6 +110,8 @@ class PowerManager { uint8_t repeatedChargedCount_ = 0; bool possibleChargedFault_ = false; + uint8_t dpdmRetry_ = 0; + hal_power_config config_ = {}; }; From 362061843d2469a8d372c3bcef4be341d81f534e Mon Sep 17 00:00:00 2001 From: XuGuohui Date: Mon, 26 Apr 2021 15:42:04 +0800 Subject: [PATCH 2/2] [system]: power source minor. --- system/src/system_power_manager.cpp | 10 +++++++--- system/src/system_power_manager.h | 2 -- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/system/src/system_power_manager.cpp b/system/src/system_power_manager.cpp index b18f906d93..27ec0e665f 100644 --- a/system/src/system_power_manager.cpp +++ b/system/src/system_power_manager.cpp @@ -511,12 +511,15 @@ void PowerManager::initDefault(bool dpdm) { } void PowerManager::runDpdm() { +// FIXME: maybe introduce a new flag? +#if HAL_PLATFORM_POWER_WORKAROUND_USB_HOST_VIN_SOURCE static system_tick_t lastRun = 0; + static uint8_t dpdmRetry = 0; if (millis() - lastRun >= DPDM_PERIOD) { lastRun = millis(); if (g_powerSource == POWER_SOURCE_VIN && HAL_USB_Get_State(nullptr) >= HAL_USB_STATE_POWERED) { - if (dpdmRetry_ >= DPDM_RETRY_COUNT) { + if (dpdmRetry >= DPDM_RETRY_COUNT) { return; } PMIC power(true); @@ -524,11 +527,12 @@ void PowerManager::runDpdm() { return; } power.enableDPDM(); - dpdmRetry_++; + dpdmRetry++; } else { - dpdmRetry_ = 0; + dpdmRetry = 0; } } +#endif } void PowerManager::confirmBatteryState(battery_state_t from, battery_state_t to) { diff --git a/system/src/system_power_manager.h b/system/src/system_power_manager.h index 3f11120569..6bc2ed0d67 100644 --- a/system/src/system_power_manager.h +++ b/system/src/system_power_manager.h @@ -110,8 +110,6 @@ class PowerManager { uint8_t repeatedChargedCount_ = 0; bool possibleChargedFault_ = false; - uint8_t dpdmRetry_ = 0; - hal_power_config config_ = {}; };