From 81ace53cc17346d7f4a055222c7407f888c3804a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=82=E3=81=8F?= Date: Wed, 31 Mar 2021 20:55:00 +0300 Subject: [PATCH] Api Hal OS/Power: split insomnia and return to where it belongs. (#393) --- .../targets/api-hal-include/api-hal-power.h | 5 +++- firmware/targets/f4/api-hal/api-hal-power.c | 27 ++++++++++++----- firmware/targets/f5/api-hal/api-hal-os.c | 5 ++++ firmware/targets/f5/api-hal/api-hal-power.c | 29 ++++++++++++++----- 4 files changed, 50 insertions(+), 16 deletions(-) diff --git a/firmware/targets/api-hal-include/api-hal-power.h b/firmware/targets/api-hal-include/api-hal-power.h index 505c2d2e..1280fd6c 100644 --- a/firmware/targets/api-hal-include/api-hal-power.h +++ b/firmware/targets/api-hal-include/api-hal-power.h @@ -39,8 +39,11 @@ void api_hal_power_insomnia_enter(); */ void api_hal_power_insomnia_exit(); +/** Check if sleep availble */ +bool api_hal_power_sleep_available(); + /** Check if deep sleep availble */ -bool api_hal_power_deep_available(); +bool api_hal_power_deep_sleep_available(); /** Go to sleep */ void api_hal_power_sleep(); diff --git a/firmware/targets/f4/api-hal/api-hal-power.c b/firmware/targets/f4/api-hal/api-hal-power.c index 7eb930e6..c0ba2638 100644 --- a/firmware/targets/f4/api-hal/api-hal-power.c +++ b/firmware/targets/f4/api-hal/api-hal-power.c @@ -12,7 +12,16 @@ #include #include -volatile uint32_t api_hal_power_insomnia = 1; +typedef struct { + volatile uint32_t insomnia; + volatile uint32_t deep_insomnia; +} ApiHalPower; + +static volatile ApiHalPower api_hal_power = { + .insomnia = 0, + .deep_insomnia = 1, +}; + const ParamCEDV cedv = { .full_charge_cap = 2100, .design_cap = 2100, @@ -48,19 +57,23 @@ void api_hal_power_init() { } uint16_t api_hal_power_insomnia_level() { - return api_hal_power_insomnia; + return api_hal_power.insomnia; } void api_hal_power_insomnia_enter() { - api_hal_power_insomnia++; + api_hal_power.insomnia++; } void api_hal_power_insomnia_exit() { - api_hal_power_insomnia--; + api_hal_power.insomnia--; } -bool api_hal_power_deep_available() { - return api_hal_bt_is_alive() && api_hal_power_insomnia == 0; +bool api_hal_power_sleep_available() { + return api_hal_power.insomnia == 0; +} + +bool api_hal_power_deep_sleep_available() { + return api_hal_bt_is_alive() && api_hal_power.deep_insomnia == 0; } void api_hal_power_light_sleep() { @@ -112,7 +125,7 @@ void api_hal_power_deep_sleep() { } void api_hal_power_sleep() { - if(api_hal_power_deep_available()) { + if(api_hal_power_deep_sleep_available()) { api_hal_power_deep_sleep(); } else { api_hal_power_light_sleep(); diff --git a/firmware/targets/f5/api-hal/api-hal-os.c b/firmware/targets/f5/api-hal/api-hal-os.c index 7d9dc259..3016cc51 100644 --- a/firmware/targets/f5/api-hal/api-hal-os.c +++ b/firmware/targets/f5/api-hal/api-hal-os.c @@ -99,6 +99,11 @@ static inline uint32_t api_hal_os_sleep(TickType_t expected_idle_ticks) { } void vPortSuppressTicksAndSleep(TickType_t expected_idle_ticks) { + if(!api_hal_power_sleep_available()) { + __WFI(); + return; + } + // Limit mount of ticks to maximum that timer can count if (expected_idle_ticks > API_HAL_OS_MAX_SLEEP) { expected_idle_ticks = API_HAL_OS_MAX_SLEEP; diff --git a/firmware/targets/f5/api-hal/api-hal-power.c b/firmware/targets/f5/api-hal/api-hal-power.c index fda81dce..659ab581 100644 --- a/firmware/targets/f5/api-hal/api-hal-power.c +++ b/firmware/targets/f5/api-hal/api-hal-power.c @@ -13,7 +13,16 @@ #include #include -volatile uint32_t api_hal_power_insomnia = 1; +typedef struct { + volatile uint32_t insomnia; + volatile uint32_t deep_insomnia; +} ApiHalPower; + +static volatile ApiHalPower api_hal_power = { + .insomnia = 0, + .deep_insomnia = 1, +}; + const ParamCEDV cedv = { .full_charge_cap = 2100, .design_cap = 2100, @@ -49,19 +58,23 @@ void api_hal_power_init() { } uint16_t api_hal_power_insomnia_level() { - return api_hal_power_insomnia; + return api_hal_power.insomnia; } void api_hal_power_insomnia_enter() { - api_hal_power_insomnia++; + api_hal_power.insomnia++; } void api_hal_power_insomnia_exit() { - api_hal_power_insomnia--; + api_hal_power.insomnia--; } -bool api_hal_power_deep_available() { - return api_hal_bt_is_alive() && api_hal_power_insomnia == 0; +bool api_hal_power_sleep_available() { + return api_hal_power.insomnia == 0; +} + +bool api_hal_power_deep_sleep_available() { + return api_hal_bt_is_alive() && api_hal_power.deep_insomnia == 0; } void api_hal_power_light_sleep() { @@ -113,7 +126,7 @@ void api_hal_power_deep_sleep() { } void api_hal_power_sleep() { - if(api_hal_power_deep_available()) { + if(api_hal_power_deep_sleep_available()) { api_hal_power_deep_sleep(); } else { api_hal_power_light_sleep(); @@ -186,7 +199,7 @@ float api_hal_power_get_battery_temperature(ApiHalPowerIC ic) { } float api_hal_power_get_usb_voltage(){ - return (float)bq25896_get_vbus_voltage() / 1000.0f;; + return (float)bq25896_get_vbus_voltage() / 1000.0f; } void api_hal_power_dump_state(string_t buffer) {