Merge remote-tracking branch 'OFW/dev' into dev

This commit is contained in:
MX
2025-02-20 18:40:54 +03:00
21 changed files with 153 additions and 34 deletions

View File

@@ -247,6 +247,7 @@ static bool power_update_info(Power* power) {
.is_charging = furi_hal_power_is_charging(),
.gauge_is_ok = furi_hal_power_gauge_is_ok(),
.is_shutdown_requested = furi_hal_power_is_shutdown_requested(),
.is_otg_enabled = furi_hal_power_is_otg_enabled(),
.charge = furi_hal_power_get_pct(),
.health = furi_hal_power_get_bat_health_pct(),
.capacity_remaining = furi_hal_power_get_battery_remaining_capacity(),
@@ -539,6 +540,30 @@ static void power_message_callback(FuriEventLoopObject* object, void* context) {
power_settings_load(&power->settings);
power_settings_apply(power);
break;
case PowerMessageTypeSwitchOTG:
power->is_otg_requested = *msg.bool_param;
if(power->is_otg_requested) {
// Only try to enable if VBUS voltage is low, otherwise charger will refuse
if(power->info.voltage_vbus < 4.5f) {
size_t retries = 5;
while(retries-- > 0) {
if(furi_hal_power_enable_otg()) {
break;
}
}
if(!retries) {
FURI_LOG_W(TAG, "Failed to enable OTG, will try later");
}
} else {
FURI_LOG_W(
TAG,
"Postponing OTG enable: VBUS(%0.1f) >= 4.5v",
(double)power->info.voltage_vbus);
}
} else {
furi_hal_power_disable_otg();
}
break;
default:
furi_crash();
}
@@ -584,9 +609,18 @@ static void power_tick_callback(void* context) {
if(need_refresh) {
view_port_update(power->battery_view_port);
}
// Check OTG status and disable it in case of fault
if(furi_hal_power_is_otg_enabled()) {
furi_hal_power_check_otg_status();
// Check OTG status, disable in case of a fault
if(furi_hal_power_check_otg_fault()) {
FURI_LOG_E(TAG, "OTG fault detected, disabling OTG");
furi_hal_power_disable_otg();
power->is_otg_requested = false;
}
// Change OTG state if needed (i.e. after disconnecting USB power)
if(power->is_otg_requested &&
(!power->info.is_otg_enabled && power->info.voltage_vbus < 4.5f)) {
FURI_LOG_D(TAG, "OTG requested but not enabled, enabling OTG");
furi_hal_power_enable_otg();
}
}

View File

@@ -39,6 +39,7 @@ typedef struct {
bool gauge_is_ok;
bool is_charging;
bool is_shutdown_requested;
bool is_otg_enabled;
float current_charger;
float current_gauge;
@@ -108,6 +109,19 @@ void power_api_get_settings(Power* instance, PowerSettings* settings);
// set settings from app to service
void power_api_set_settings(Power* instance, const PowerSettings* settings);
/** Enable or disable OTG
*
* @param power Power instance
* @param enable true - enable, false - disable
*/
void power_enable_otg(Power* power, bool enable);
/** Check OTG status
*
* @return true if OTG is requested
*/
bool power_is_otg_enabled(Power* power);
#ifdef __cplusplus
}
#endif

View File

@@ -70,3 +70,22 @@ void power_enable_low_battery_level_notification(Power* power, bool enable) {
furi_check(
furi_message_queue_put(power->message_queue, &msg, FuriWaitForever) == FuriStatusOk);
}
void power_enable_otg(Power* power, bool enable) {
furi_check(power);
PowerMessage msg = {
.type = PowerMessageTypeSwitchOTG,
.bool_param = &enable,
.lock = api_lock_alloc_locked(),
};
furi_check(
furi_message_queue_put(power->message_queue, &msg, FuriWaitForever) == FuriStatusOk);
api_lock_wait_unlock_and_free(msg.lock);
}
bool power_is_otg_enabled(Power* power) {
furi_check(power);
return power->is_otg_requested;
}

View File

@@ -34,6 +34,7 @@ struct Power {
bool battery_low;
bool show_battery_low_warning;
uint8_t displayBatteryPercentage;
bool is_otg_requested;
uint8_t battery_level;
uint8_t power_off_timeout;
PowerSettings settings;
@@ -58,6 +59,7 @@ typedef enum {
PowerMessageTypeGetSettings,
PowerMessageTypeSetSettings,
PowerMessageTypeReloadSettings,
PowerMessageTypeSwitchOTG,
} PowerMessageType;
typedef struct {