Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System: make sure that the power source being USB host is well detected #2310

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions system/src/system_power_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be this be !power.isInDPDM() && !pwr_good?

if (g_batteryState == BATTERY_STATE_DISCHARGING) {
src = POWER_SOURCE_BATTERY;
}
}
}

Expand Down Expand Up @@ -424,6 +428,7 @@ void PowerManager::loop(void* arg) {
}
self->checkWatchdog();
self->deduceBatteryStateLoop();
self->runDpdm();
}

exit:
Expand Down Expand Up @@ -505,6 +510,31 @@ void PowerManager::initDefault(bool dpdm) {
clearIntermediateBatteryState(STATE_ALL);
}

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move these into the class member variables.


if (millis() - lastRun >= DPDM_PERIOD) {
lastRun = millis();
if (g_powerSource == POWER_SOURCE_VIN && HAL_USB_Get_State(nullptr) >= HAL_USB_STATE_POWERED) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what other states are caught by HAL_USB_Get_State(nullptr) >= HAL_USB_STATE_POWERED? Can you make this more explicit such as a switch/case statement?

if (dpdmRetry >= DPDM_RETRY_COUNT) {
return;
}
PMIC power(true);
if (power.isInDPDM()) {
return;
}
power.enableDPDM();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to cause dips to 100mA current temporarily and may cause for example the modem to brownout if there is no battery attached.

See #2215 for some additional info.

We should probably opt to just reporting USB_HOST and configuring the PMIC manually as needed (i.e. lowering the input current limit to 500mA) and avoiding re-running DPDM as much as possible.

dpdmRetry++;
} else {
dpdmRetry = 0;
}
}
#endif
}

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();
Expand Down
2 changes: 2 additions & 0 deletions system/src/system_power_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down