2022-01-05 19:10:18 +03:00
|
|
|
#include <furi_hal_power.h>
|
|
|
|
#include <furi_hal_clock.h>
|
|
|
|
#include <furi_hal_bt.h>
|
2022-11-30 14:41:23 +03:00
|
|
|
#include <furi_hal_vibro.h>
|
2022-03-30 18:23:40 +03:00
|
|
|
#include <furi_hal_resources.h>
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 16:29:51 +03:00
|
|
|
#include <furi_hal_uart.h>
|
2023-04-13 17:47:38 +03:00
|
|
|
#include <furi_hal_rtc.h>
|
|
|
|
#include <furi_hal_debug.h>
|
2021-09-10 05:19:02 +03:00
|
|
|
|
|
|
|
#include <stm32wbxx_ll_rcc.h>
|
|
|
|
#include <stm32wbxx_ll_pwr.h>
|
|
|
|
#include <stm32wbxx_ll_hsem.h>
|
|
|
|
#include <stm32wbxx_ll_cortex.h>
|
|
|
|
#include <stm32wbxx_ll_gpio.h>
|
|
|
|
|
|
|
|
#include <hw_conf.h>
|
|
|
|
#include <bq27220.h>
|
|
|
|
#include <bq25896.h>
|
|
|
|
|
|
|
|
#include <furi.h>
|
|
|
|
|
2021-11-12 16:04:35 +03:00
|
|
|
#define TAG "FuriHalPower"
|
|
|
|
|
2023-04-13 17:47:38 +03:00
|
|
|
#ifndef FURI_HAL_POWER_DEBUG_WFI_GPIO
|
|
|
|
#define FURI_HAL_POWER_DEBUG_WFI_GPIO (&gpio_ext_pb2)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FURI_HAL_POWER_DEBUG_STOP_GPIO
|
|
|
|
#define FURI_HAL_POWER_DEBUG_STOP_GPIO (&gpio_ext_pc3)
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 16:29:51 +03:00
|
|
|
#endif
|
|
|
|
|
2023-04-20 15:57:51 +03:00
|
|
|
#ifndef FURI_HAL_POWER_STOP_MODE
|
|
|
|
#define FURI_HAL_POWER_STOP_MODE (LL_PWR_MODE_STOP2)
|
|
|
|
#endif
|
|
|
|
|
2021-09-10 05:19:02 +03:00
|
|
|
typedef struct {
|
2021-10-04 10:01:41 +03:00
|
|
|
volatile uint8_t insomnia;
|
|
|
|
volatile uint8_t suppress_charge;
|
2022-04-19 02:09:11 +03:00
|
|
|
|
|
|
|
uint8_t gauge_initialized;
|
|
|
|
uint8_t charger_initialized;
|
2021-09-10 05:19:02 +03:00
|
|
|
} FuriHalPower;
|
|
|
|
|
|
|
|
static volatile FuriHalPower furi_hal_power = {
|
|
|
|
.insomnia = 0,
|
2021-10-04 10:01:41 +03:00
|
|
|
.suppress_charge = 0,
|
2021-09-10 05:19:02 +03:00
|
|
|
};
|
|
|
|
|
2023-05-03 05:08:41 +03:00
|
|
|
#include <furi_hal_power_calibration.h>
|
2021-09-10 05:19:02 +03:00
|
|
|
|
|
|
|
void furi_hal_power_init() {
|
2023-04-13 17:47:38 +03:00
|
|
|
#ifdef FURI_HAL_POWER_DEBUG
|
|
|
|
furi_hal_gpio_init_simple(FURI_HAL_POWER_DEBUG_WFI_GPIO, GpioModeOutputPushPull);
|
|
|
|
furi_hal_gpio_init_simple(FURI_HAL_POWER_DEBUG_STOP_GPIO, GpioModeOutputPushPull);
|
|
|
|
furi_hal_gpio_write(FURI_HAL_POWER_DEBUG_WFI_GPIO, 0);
|
|
|
|
furi_hal_gpio_write(FURI_HAL_POWER_DEBUG_STOP_GPIO, 0);
|
|
|
|
#endif
|
|
|
|
|
2021-09-10 05:19:02 +03:00
|
|
|
LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE1);
|
|
|
|
LL_PWR_SMPS_SetMode(LL_PWR_SMPS_STEP_DOWN);
|
2023-04-27 17:01:13 +03:00
|
|
|
|
2023-04-20 15:57:51 +03:00
|
|
|
LL_PWR_SetPowerMode(FURI_HAL_POWER_STOP_MODE);
|
|
|
|
LL_C2_PWR_SetPowerMode(FURI_HAL_POWER_STOP_MODE);
|
2021-11-28 21:28:19 +03:00
|
|
|
|
|
|
|
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
|
|
|
|
bq27220_init(&furi_hal_i2c_handle_power, &cedv);
|
|
|
|
bq25896_init(&furi_hal_i2c_handle_power);
|
|
|
|
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
|
|
|
|
|
2021-11-12 16:04:35 +03:00
|
|
|
FURI_LOG_I(TAG, "Init OK");
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
2022-04-19 02:09:11 +03:00
|
|
|
bool furi_hal_power_gauge_is_ok() {
|
|
|
|
bool ret = true;
|
|
|
|
|
|
|
|
BatteryStatus battery_status;
|
|
|
|
OperationStatus operation_status;
|
|
|
|
|
|
|
|
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
|
|
|
|
|
|
|
|
if(bq27220_get_battery_status(&furi_hal_i2c_handle_power, &battery_status) == BQ27220_ERROR ||
|
|
|
|
bq27220_get_operation_status(&furi_hal_i2c_handle_power, &operation_status) ==
|
|
|
|
BQ27220_ERROR) {
|
|
|
|
ret = false;
|
|
|
|
} else {
|
|
|
|
ret &= battery_status.BATTPRES;
|
|
|
|
ret &= operation_status.INITCOMP;
|
|
|
|
ret &= (cedv.design_cap == bq27220_get_design_capacity(&furi_hal_i2c_handle_power));
|
|
|
|
}
|
|
|
|
|
|
|
|
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-09-10 05:19:02 +03:00
|
|
|
uint16_t furi_hal_power_insomnia_level() {
|
|
|
|
return furi_hal_power.insomnia;
|
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_power_insomnia_enter() {
|
2022-02-10 21:17:41 +03:00
|
|
|
FURI_CRITICAL_ENTER();
|
2022-02-10 16:01:49 +03:00
|
|
|
furi_assert(furi_hal_power.insomnia < UINT8_MAX);
|
2021-09-10 05:19:02 +03:00
|
|
|
furi_hal_power.insomnia++;
|
2022-02-10 21:17:41 +03:00
|
|
|
FURI_CRITICAL_EXIT();
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_power_insomnia_exit() {
|
2022-02-10 21:17:41 +03:00
|
|
|
FURI_CRITICAL_ENTER();
|
2022-02-10 16:01:49 +03:00
|
|
|
furi_assert(furi_hal_power.insomnia > 0);
|
2021-09-10 05:19:02 +03:00
|
|
|
furi_hal_power.insomnia--;
|
2022-02-10 21:17:41 +03:00
|
|
|
FURI_CRITICAL_EXIT();
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool furi_hal_power_sleep_available() {
|
|
|
|
return furi_hal_power.insomnia == 0;
|
|
|
|
}
|
|
|
|
|
2023-04-13 17:47:38 +03:00
|
|
|
static inline bool furi_hal_power_deep_sleep_available() {
|
|
|
|
return furi_hal_bt_is_alive() && !furi_hal_rtc_is_flag_set(FuriHalRtcFlagLegacySleep) &&
|
2023-04-25 23:11:42 +03:00
|
|
|
!furi_hal_debug_is_gdb_session_active();
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
2023-04-13 17:47:38 +03:00
|
|
|
static inline void furi_hal_power_light_sleep() {
|
2021-09-10 05:19:02 +03:00
|
|
|
__WFI();
|
|
|
|
}
|
|
|
|
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 16:29:51 +03:00
|
|
|
static inline void furi_hal_power_suspend_aux_periphs() {
|
|
|
|
// Disable USART
|
|
|
|
furi_hal_uart_suspend(FuriHalUartIdUSART1);
|
|
|
|
furi_hal_uart_suspend(FuriHalUartIdLPUART1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void furi_hal_power_resume_aux_periphs() {
|
|
|
|
// Re-enable USART
|
|
|
|
furi_hal_uart_resume(FuriHalUartIdUSART1);
|
|
|
|
furi_hal_uart_resume(FuriHalUartIdLPUART1);
|
|
|
|
}
|
|
|
|
|
2023-04-13 17:47:38 +03:00
|
|
|
static inline void furi_hal_power_deep_sleep() {
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 16:29:51 +03:00
|
|
|
furi_hal_power_suspend_aux_periphs();
|
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
while(LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID))
|
|
|
|
;
|
2021-09-10 05:19:02 +03:00
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
if(!LL_HSEM_1StepLock(HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID)) {
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 16:29:51 +03:00
|
|
|
if(LL_PWR_IsActiveFlag_C2DS() || LL_PWR_IsActiveFlag_C2SB()) {
|
2021-09-10 05:19:02 +03:00
|
|
|
// Release ENTRY_STOP_MODE semaphore
|
|
|
|
LL_HSEM_ReleaseLock(HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID, 0);
|
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
// The switch on HSI before entering Stop Mode is required
|
2021-09-10 05:19:02 +03:00
|
|
|
furi_hal_clock_switch_to_hsi();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/**
|
|
|
|
* The switch on HSI before entering Stop Mode is required
|
|
|
|
*/
|
|
|
|
furi_hal_clock_switch_to_hsi();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release RCC semaphore */
|
|
|
|
LL_HSEM_ReleaseLock(HSEM, CFG_HW_RCC_SEMID, 0);
|
|
|
|
|
|
|
|
// Prepare deep sleep
|
|
|
|
LL_LPM_EnableDeepSleep();
|
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
#if defined(__CC_ARM)
|
2021-09-10 05:19:02 +03:00
|
|
|
// Force store operations
|
|
|
|
__force_stores();
|
|
|
|
#endif
|
|
|
|
|
2023-04-25 23:11:42 +03:00
|
|
|
__WFI();
|
2021-09-10 05:19:02 +03:00
|
|
|
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 16:29:51 +03:00
|
|
|
LL_LPM_EnableSleep();
|
|
|
|
|
2021-09-10 05:19:02 +03:00
|
|
|
/* Release ENTRY_STOP_MODE semaphore */
|
|
|
|
LL_HSEM_ReleaseLock(HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID, 0);
|
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
while(LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID))
|
|
|
|
;
|
2021-09-10 05:19:02 +03:00
|
|
|
|
|
|
|
if(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL) {
|
|
|
|
furi_hal_clock_switch_to_pll();
|
|
|
|
}
|
|
|
|
|
|
|
|
LL_HSEM_ReleaseLock(HSEM, CFG_HW_RCC_SEMID, 0);
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 16:29:51 +03:00
|
|
|
|
|
|
|
furi_hal_power_resume_aux_periphs();
|
2023-04-13 17:47:38 +03:00
|
|
|
furi_hal_rtc_sync_shadow();
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_power_sleep() {
|
|
|
|
if(furi_hal_power_deep_sleep_available()) {
|
2023-04-13 17:47:38 +03:00
|
|
|
#ifdef FURI_HAL_POWER_DEBUG
|
|
|
|
furi_hal_gpio_write(FURI_HAL_POWER_DEBUG_STOP_GPIO, 1);
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 16:29:51 +03:00
|
|
|
#endif
|
2021-09-10 05:19:02 +03:00
|
|
|
furi_hal_power_deep_sleep();
|
2023-04-13 17:47:38 +03:00
|
|
|
#ifdef FURI_HAL_POWER_DEBUG
|
|
|
|
furi_hal_gpio_write(FURI_HAL_POWER_DEBUG_STOP_GPIO, 0);
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 16:29:51 +03:00
|
|
|
#endif
|
2021-09-10 05:19:02 +03:00
|
|
|
} else {
|
2023-04-13 17:47:38 +03:00
|
|
|
#ifdef FURI_HAL_POWER_DEBUG
|
|
|
|
furi_hal_gpio_write(FURI_HAL_POWER_DEBUG_WFI_GPIO, 1);
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 16:29:51 +03:00
|
|
|
#endif
|
2021-09-10 05:19:02 +03:00
|
|
|
furi_hal_power_light_sleep();
|
2023-04-13 17:47:38 +03:00
|
|
|
#ifdef FURI_HAL_POWER_DEBUG
|
|
|
|
furi_hal_gpio_write(FURI_HAL_POWER_DEBUG_WFI_GPIO, 0);
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 16:29:51 +03:00
|
|
|
#endif
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t furi_hal_power_get_pct() {
|
2021-11-28 21:28:19 +03:00
|
|
|
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
|
|
|
|
uint8_t ret = bq27220_get_state_of_charge(&furi_hal_i2c_handle_power);
|
|
|
|
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
|
|
|
|
return ret;
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t furi_hal_power_get_bat_health_pct() {
|
2021-11-28 21:28:19 +03:00
|
|
|
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
|
|
|
|
uint8_t ret = bq27220_get_state_of_health(&furi_hal_i2c_handle_power);
|
|
|
|
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
|
|
|
|
return ret;
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool furi_hal_power_is_charging() {
|
2021-11-28 21:28:19 +03:00
|
|
|
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
|
|
|
|
bool ret = bq25896_is_charging(&furi_hal_i2c_handle_power);
|
|
|
|
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
|
|
|
|
return ret;
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
2022-09-26 18:34:59 +03:00
|
|
|
bool furi_hal_power_is_charging_done() {
|
|
|
|
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
|
|
|
|
bool ret = bq25896_is_charging_done(&furi_hal_i2c_handle_power);
|
|
|
|
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 16:29:51 +03:00
|
|
|
void furi_hal_power_shutdown() {
|
|
|
|
furi_hal_power_insomnia_enter();
|
|
|
|
|
|
|
|
furi_hal_bt_reinit();
|
|
|
|
|
|
|
|
while(LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID))
|
|
|
|
;
|
|
|
|
|
|
|
|
if(!LL_HSEM_1StepLock(HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID)) {
|
|
|
|
if(LL_PWR_IsActiveFlag_C2DS() || LL_PWR_IsActiveFlag_C2SB()) {
|
|
|
|
// Release ENTRY_STOP_MODE semaphore
|
|
|
|
LL_HSEM_ReleaseLock(HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare Wakeup pin
|
|
|
|
LL_PWR_SetWakeUpPinPolarityLow(LL_PWR_WAKEUP_PIN2);
|
|
|
|
LL_PWR_EnableWakeUpPin(LL_PWR_WAKEUP_PIN2);
|
|
|
|
LL_C2_PWR_EnableWakeUpPin(LL_PWR_WAKEUP_PIN2);
|
|
|
|
|
|
|
|
/* Release RCC semaphore */
|
|
|
|
LL_HSEM_ReleaseLock(HSEM, CFG_HW_RCC_SEMID, 0);
|
|
|
|
|
|
|
|
LL_PWR_DisableBootC2();
|
|
|
|
LL_PWR_SetPowerMode(LL_PWR_MODE_SHUTDOWN);
|
|
|
|
LL_C2_PWR_SetPowerMode(LL_PWR_MODE_SHUTDOWN);
|
|
|
|
LL_LPM_EnableDeepSleep();
|
|
|
|
|
|
|
|
__WFI();
|
|
|
|
furi_crash("Insomniac core2");
|
|
|
|
}
|
|
|
|
|
2021-09-10 05:19:02 +03:00
|
|
|
void furi_hal_power_off() {
|
2022-07-04 17:26:02 +03:00
|
|
|
// Crutch: shutting down with ext 3V3 off is causing LSE to stop
|
|
|
|
furi_hal_power_enable_external_3_3v();
|
2022-11-30 14:41:23 +03:00
|
|
|
furi_hal_vibro_on(true);
|
|
|
|
furi_delay_us(50000);
|
2022-07-04 17:26:02 +03:00
|
|
|
// Send poweroff to charger
|
2021-11-28 21:28:19 +03:00
|
|
|
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
|
|
|
|
bq25896_poweroff(&furi_hal_i2c_handle_power);
|
|
|
|
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
|
2022-11-30 14:41:23 +03:00
|
|
|
furi_hal_vibro_on(false);
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_power_reset() {
|
|
|
|
NVIC_SystemReset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_power_enable_otg() {
|
2021-11-28 21:28:19 +03:00
|
|
|
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
|
|
|
|
bq25896_enable_otg(&furi_hal_i2c_handle_power);
|
|
|
|
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_power_disable_otg() {
|
2021-11-28 21:28:19 +03:00
|
|
|
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
|
|
|
|
bq25896_disable_otg(&furi_hal_i2c_handle_power);
|
|
|
|
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
2021-09-30 23:03:28 +03:00
|
|
|
bool furi_hal_power_is_otg_enabled() {
|
2021-11-28 21:28:19 +03:00
|
|
|
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
|
|
|
|
bool ret = bq25896_is_otg_enabled(&furi_hal_i2c_handle_power);
|
|
|
|
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
|
|
|
|
return ret;
|
2021-09-30 23:03:28 +03:00
|
|
|
}
|
|
|
|
|
2023-02-26 20:23:39 +03:00
|
|
|
float furi_hal_power_get_battery_charge_voltage_limit() {
|
2022-12-08 09:57:49 +03:00
|
|
|
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
|
|
|
|
float ret = (float)bq25896_get_vreg_voltage(&furi_hal_i2c_handle_power) / 1000.0f;
|
|
|
|
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-02-26 20:23:39 +03:00
|
|
|
void furi_hal_power_set_battery_charge_voltage_limit(float voltage) {
|
2022-12-08 09:57:49 +03:00
|
|
|
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
|
|
|
|
// Adding 0.0005 is necessary because 4.016f is 4.015999794000, which gets truncated
|
|
|
|
bq25896_set_vreg_voltage(&furi_hal_i2c_handle_power, (uint16_t)(voltage * 1000.0f + 0.0005f));
|
|
|
|
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
|
|
|
|
}
|
|
|
|
|
2022-01-21 19:55:44 +03:00
|
|
|
void furi_hal_power_check_otg_status() {
|
|
|
|
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
|
|
|
|
if(bq25896_check_otg_fault(&furi_hal_i2c_handle_power))
|
|
|
|
bq25896_disable_otg(&furi_hal_i2c_handle_power);
|
|
|
|
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
|
|
|
|
}
|
|
|
|
|
2021-09-10 05:19:02 +03:00
|
|
|
uint32_t furi_hal_power_get_battery_remaining_capacity() {
|
2021-11-28 21:28:19 +03:00
|
|
|
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
|
|
|
|
uint32_t ret = bq27220_get_remaining_capacity(&furi_hal_i2c_handle_power);
|
|
|
|
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
|
|
|
|
return ret;
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t furi_hal_power_get_battery_full_capacity() {
|
2021-11-28 21:28:19 +03:00
|
|
|
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
|
|
|
|
uint32_t ret = bq27220_get_full_charge_capacity(&furi_hal_i2c_handle_power);
|
|
|
|
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
|
|
|
|
return ret;
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
2022-04-07 18:00:45 +03:00
|
|
|
uint32_t furi_hal_power_get_battery_design_capacity() {
|
|
|
|
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
|
|
|
|
uint32_t ret = bq27220_get_design_capacity(&furi_hal_i2c_handle_power);
|
|
|
|
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-09-10 05:19:02 +03:00
|
|
|
float furi_hal_power_get_battery_voltage(FuriHalPowerIC ic) {
|
2021-11-28 21:28:19 +03:00
|
|
|
float ret = 0.0f;
|
|
|
|
|
|
|
|
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
|
2022-01-05 19:10:18 +03:00
|
|
|
if(ic == FuriHalPowerICCharger) {
|
2021-11-28 21:28:19 +03:00
|
|
|
ret = (float)bq25896_get_vbat_voltage(&furi_hal_i2c_handle_power) / 1000.0f;
|
2022-01-05 19:10:18 +03:00
|
|
|
} else if(ic == FuriHalPowerICFuelGauge) {
|
2021-11-28 21:28:19 +03:00
|
|
|
ret = (float)bq27220_get_voltage(&furi_hal_i2c_handle_power) / 1000.0f;
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
2021-11-28 21:28:19 +03:00
|
|
|
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
|
|
|
|
|
|
|
|
return ret;
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
float furi_hal_power_get_battery_current(FuriHalPowerIC ic) {
|
2021-11-28 21:28:19 +03:00
|
|
|
float ret = 0.0f;
|
|
|
|
|
|
|
|
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
|
2022-01-05 19:10:18 +03:00
|
|
|
if(ic == FuriHalPowerICCharger) {
|
2021-11-28 21:28:19 +03:00
|
|
|
ret = (float)bq25896_get_vbat_current(&furi_hal_i2c_handle_power) / 1000.0f;
|
2022-01-05 19:10:18 +03:00
|
|
|
} else if(ic == FuriHalPowerICFuelGauge) {
|
2021-11-28 21:28:19 +03:00
|
|
|
ret = (float)bq27220_get_current(&furi_hal_i2c_handle_power) / 1000.0f;
|
2022-01-05 19:10:18 +03:00
|
|
|
}
|
2021-11-28 21:28:19 +03:00
|
|
|
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
|
|
|
|
|
|
|
|
return ret;
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
2021-11-28 21:28:19 +03:00
|
|
|
static float furi_hal_power_get_battery_temperature_internal(FuriHalPowerIC ic) {
|
|
|
|
float ret = 0.0f;
|
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
if(ic == FuriHalPowerICCharger) {
|
2021-09-10 05:19:02 +03:00
|
|
|
// Linear approximation, +/- 5 C
|
2022-01-05 19:10:18 +03:00
|
|
|
ret = (71.0f - (float)bq25896_get_ntc_mpct(&furi_hal_i2c_handle_power) / 1000) / 0.6f;
|
|
|
|
} else if(ic == FuriHalPowerICFuelGauge) {
|
2021-11-28 21:28:19 +03:00
|
|
|
ret = ((float)bq27220_get_temperature(&furi_hal_i2c_handle_power) - 2731.0f) / 10.0f;
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
2021-11-28 21:28:19 +03:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
float furi_hal_power_get_battery_temperature(FuriHalPowerIC ic) {
|
|
|
|
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
|
|
|
|
float ret = furi_hal_power_get_battery_temperature_internal(ic);
|
|
|
|
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
|
|
|
|
|
|
|
|
return ret;
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
float furi_hal_power_get_usb_voltage() {
|
2021-11-28 21:28:19 +03:00
|
|
|
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
|
|
|
|
float ret = (float)bq25896_get_vbus_voltage(&furi_hal_i2c_handle_power) / 1000.0f;
|
|
|
|
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
|
|
|
|
return ret;
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
void furi_hal_power_enable_external_3_3v() {
|
2023-04-06 12:06:19 +03:00
|
|
|
furi_hal_gpio_write(&gpio_periph_power, 1);
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
void furi_hal_power_disable_external_3_3v() {
|
2023-04-06 12:06:19 +03:00
|
|
|
furi_hal_gpio_write(&gpio_periph_power, 0);
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
2021-10-04 10:01:41 +03:00
|
|
|
|
|
|
|
void furi_hal_power_suppress_charge_enter() {
|
|
|
|
vTaskSuspendAll();
|
|
|
|
bool disable_charging = furi_hal_power.suppress_charge == 0;
|
|
|
|
furi_hal_power.suppress_charge++;
|
|
|
|
xTaskResumeAll();
|
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
if(disable_charging) {
|
2021-11-28 21:28:19 +03:00
|
|
|
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
|
|
|
|
bq25896_disable_charging(&furi_hal_i2c_handle_power);
|
|
|
|
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
|
2021-10-04 10:01:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_power_suppress_charge_exit() {
|
|
|
|
vTaskSuspendAll();
|
|
|
|
furi_hal_power.suppress_charge--;
|
|
|
|
bool enable_charging = furi_hal_power.suppress_charge == 0;
|
|
|
|
xTaskResumeAll();
|
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
if(enable_charging) {
|
2021-11-28 21:28:19 +03:00
|
|
|
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
|
|
|
|
bq25896_enable_charging(&furi_hal_i2c_handle_power);
|
|
|
|
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
|
2021-10-04 10:01:41 +03:00
|
|
|
}
|
2022-01-14 21:14:25 +03:00
|
|
|
}
|
2022-04-07 18:00:45 +03:00
|
|
|
|
2022-11-29 12:08:08 +03:00
|
|
|
void furi_hal_power_info_get(PropertyValueCallback out, char sep, void* context) {
|
2022-04-07 18:00:45 +03:00
|
|
|
furi_assert(out);
|
|
|
|
|
2022-11-29 12:08:08 +03:00
|
|
|
FuriString* value = furi_string_alloc();
|
|
|
|
FuriString* key = furi_string_alloc();
|
2022-04-07 18:00:45 +03:00
|
|
|
|
2022-11-29 12:08:08 +03:00
|
|
|
PropertyValueContext property_context = {
|
|
|
|
.key = key, .value = value, .out = out, .sep = sep, .last = false, .context = context};
|
2022-04-07 18:00:45 +03:00
|
|
|
|
2022-11-29 12:08:08 +03:00
|
|
|
if(sep == '.') {
|
|
|
|
property_value_out(&property_context, NULL, 2, "format", "major", "2");
|
2022-12-08 09:57:49 +03:00
|
|
|
property_value_out(&property_context, NULL, 2, "format", "minor", "1");
|
2022-11-29 12:08:08 +03:00
|
|
|
} else {
|
2023-02-26 20:23:39 +03:00
|
|
|
property_value_out(&property_context, NULL, 3, "power", "info", "major", "2");
|
2022-12-08 09:57:49 +03:00
|
|
|
property_value_out(&property_context, NULL, 3, "power", "info", "minor", "1");
|
2022-11-29 12:08:08 +03:00
|
|
|
}
|
2022-04-07 18:00:45 +03:00
|
|
|
|
2022-11-29 12:08:08 +03:00
|
|
|
uint8_t charge = furi_hal_power_get_pct();
|
|
|
|
property_value_out(&property_context, "%u", 2, "charge", "level", charge);
|
2022-04-07 18:00:45 +03:00
|
|
|
|
2022-11-29 12:08:08 +03:00
|
|
|
const char* charge_state;
|
2022-04-07 18:00:45 +03:00
|
|
|
if(furi_hal_power_is_charging()) {
|
2022-12-08 09:57:49 +03:00
|
|
|
if((charge < 100) && (!furi_hal_power_is_charging_done())) {
|
2022-11-29 12:08:08 +03:00
|
|
|
charge_state = "charging";
|
2022-04-07 18:00:45 +03:00
|
|
|
} else {
|
2022-11-29 12:08:08 +03:00
|
|
|
charge_state = "charged";
|
2022-04-07 18:00:45 +03:00
|
|
|
}
|
|
|
|
} else {
|
2022-11-29 12:08:08 +03:00
|
|
|
charge_state = "discharging";
|
2022-04-07 18:00:45 +03:00
|
|
|
}
|
|
|
|
|
2022-11-29 12:08:08 +03:00
|
|
|
property_value_out(&property_context, NULL, 2, "charge", "state", charge_state);
|
2023-02-26 20:23:39 +03:00
|
|
|
uint16_t charge_voltage_limit =
|
|
|
|
(uint16_t)(furi_hal_power_get_battery_charge_voltage_limit() * 1000.f);
|
|
|
|
property_value_out(
|
|
|
|
&property_context, "%u", 3, "charge", "voltage", "limit", charge_voltage_limit);
|
2022-04-07 18:00:45 +03:00
|
|
|
uint16_t voltage =
|
|
|
|
(uint16_t)(furi_hal_power_get_battery_voltage(FuriHalPowerICFuelGauge) * 1000.f);
|
2022-11-29 12:08:08 +03:00
|
|
|
property_value_out(&property_context, "%u", 2, "battery", "voltage", voltage);
|
2022-04-07 18:00:45 +03:00
|
|
|
int16_t current =
|
|
|
|
(int16_t)(furi_hal_power_get_battery_current(FuriHalPowerICFuelGauge) * 1000.f);
|
2022-11-29 12:08:08 +03:00
|
|
|
property_value_out(&property_context, "%d", 2, "battery", "current", current);
|
2022-04-07 18:00:45 +03:00
|
|
|
int16_t temperature = (int16_t)furi_hal_power_get_battery_temperature(FuriHalPowerICFuelGauge);
|
2022-11-29 12:08:08 +03:00
|
|
|
property_value_out(&property_context, "%d", 2, "battery", "temp", temperature);
|
|
|
|
property_value_out(
|
|
|
|
&property_context, "%u", 2, "battery", "health", furi_hal_power_get_bat_health_pct());
|
|
|
|
property_value_out(
|
|
|
|
&property_context,
|
|
|
|
"%lu",
|
|
|
|
2,
|
|
|
|
"capacity",
|
|
|
|
"remain",
|
|
|
|
furi_hal_power_get_battery_remaining_capacity());
|
|
|
|
property_value_out(
|
|
|
|
&property_context,
|
|
|
|
"%lu",
|
|
|
|
2,
|
|
|
|
"capacity",
|
|
|
|
"full",
|
|
|
|
furi_hal_power_get_battery_full_capacity());
|
|
|
|
property_context.last = true;
|
|
|
|
property_value_out(
|
|
|
|
&property_context,
|
|
|
|
"%lu",
|
|
|
|
2,
|
|
|
|
"capacity",
|
|
|
|
"design",
|
|
|
|
furi_hal_power_get_battery_design_capacity());
|
|
|
|
|
|
|
|
furi_string_free(key);
|
|
|
|
furi_string_free(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_power_debug_get(PropertyValueCallback out, void* context) {
|
|
|
|
furi_assert(out);
|
|
|
|
|
|
|
|
FuriString* value = furi_string_alloc();
|
|
|
|
FuriString* key = furi_string_alloc();
|
|
|
|
|
|
|
|
PropertyValueContext property_context = {
|
|
|
|
.key = key, .value = value, .out = out, .sep = '.', .last = false, .context = context};
|
|
|
|
|
|
|
|
BatteryStatus battery_status;
|
|
|
|
OperationStatus operation_status;
|
2022-04-07 18:00:45 +03:00
|
|
|
|
2022-11-29 12:08:08 +03:00
|
|
|
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
|
2022-04-07 18:00:45 +03:00
|
|
|
|
2022-11-29 12:08:08 +03:00
|
|
|
// Power Debug version
|
|
|
|
property_value_out(&property_context, NULL, 2, "format", "major", "1");
|
|
|
|
property_value_out(&property_context, NULL, 2, "format", "minor", "0");
|
|
|
|
|
|
|
|
property_value_out(
|
|
|
|
&property_context,
|
|
|
|
"%d",
|
|
|
|
2,
|
|
|
|
"charger",
|
|
|
|
"vbus",
|
|
|
|
bq25896_get_vbus_voltage(&furi_hal_i2c_handle_power));
|
|
|
|
property_value_out(
|
|
|
|
&property_context,
|
|
|
|
"%d",
|
|
|
|
2,
|
|
|
|
"charger",
|
|
|
|
"vsys",
|
|
|
|
bq25896_get_vsys_voltage(&furi_hal_i2c_handle_power));
|
|
|
|
property_value_out(
|
|
|
|
&property_context,
|
|
|
|
"%d",
|
|
|
|
2,
|
|
|
|
"charger",
|
|
|
|
"vbat",
|
|
|
|
bq25896_get_vbat_voltage(&furi_hal_i2c_handle_power));
|
2022-12-08 09:57:49 +03:00
|
|
|
property_value_out(
|
|
|
|
&property_context,
|
|
|
|
"%d",
|
|
|
|
2,
|
|
|
|
"charger",
|
|
|
|
"vreg",
|
|
|
|
bq25896_get_vreg_voltage(&furi_hal_i2c_handle_power));
|
2022-11-29 12:08:08 +03:00
|
|
|
property_value_out(
|
|
|
|
&property_context,
|
|
|
|
"%d",
|
|
|
|
2,
|
|
|
|
"charger",
|
|
|
|
"current",
|
|
|
|
bq25896_get_vbat_current(&furi_hal_i2c_handle_power));
|
|
|
|
|
|
|
|
const uint32_t ntc_mpct = bq25896_get_ntc_mpct(&furi_hal_i2c_handle_power);
|
|
|
|
|
|
|
|
if(bq27220_get_battery_status(&furi_hal_i2c_handle_power, &battery_status) != BQ27220_ERROR &&
|
|
|
|
bq27220_get_operation_status(&furi_hal_i2c_handle_power, &operation_status) !=
|
|
|
|
BQ27220_ERROR) {
|
|
|
|
property_value_out(&property_context, "%lu", 2, "charger", "ntc", ntc_mpct);
|
|
|
|
property_value_out(&property_context, "%d", 2, "gauge", "calmd", operation_status.CALMD);
|
|
|
|
property_value_out(&property_context, "%d", 2, "gauge", "sec", operation_status.SEC);
|
|
|
|
property_value_out(&property_context, "%d", 2, "gauge", "edv2", operation_status.EDV2);
|
|
|
|
property_value_out(&property_context, "%d", 2, "gauge", "vdq", operation_status.VDQ);
|
|
|
|
property_value_out(
|
|
|
|
&property_context, "%d", 2, "gauge", "initcomp", operation_status.INITCOMP);
|
|
|
|
property_value_out(&property_context, "%d", 2, "gauge", "smth", operation_status.SMTH);
|
|
|
|
property_value_out(&property_context, "%d", 2, "gauge", "btpint", operation_status.BTPINT);
|
|
|
|
property_value_out(
|
|
|
|
&property_context, "%d", 2, "gauge", "cfgupdate", operation_status.CFGUPDATE);
|
2022-04-07 18:00:45 +03:00
|
|
|
|
2022-11-29 12:08:08 +03:00
|
|
|
// Battery status register, part 1
|
|
|
|
property_value_out(&property_context, "%d", 2, "gauge", "chginh", battery_status.CHGINH);
|
|
|
|
property_value_out(&property_context, "%d", 2, "gauge", "fc", battery_status.FC);
|
|
|
|
property_value_out(&property_context, "%d", 2, "gauge", "otd", battery_status.OTD);
|
|
|
|
property_value_out(&property_context, "%d", 2, "gauge", "otc", battery_status.OTC);
|
|
|
|
property_value_out(&property_context, "%d", 2, "gauge", "sleep", battery_status.SLEEP);
|
|
|
|
property_value_out(&property_context, "%d", 2, "gauge", "ocvfail", battery_status.OCVFAIL);
|
|
|
|
property_value_out(&property_context, "%d", 2, "gauge", "ocvcomp", battery_status.OCVCOMP);
|
|
|
|
property_value_out(&property_context, "%d", 2, "gauge", "fd", battery_status.FD);
|
2022-04-07 18:00:45 +03:00
|
|
|
|
2022-11-29 12:08:08 +03:00
|
|
|
// Battery status register, part 2
|
|
|
|
property_value_out(&property_context, "%d", 2, "gauge", "dsg", battery_status.DSG);
|
|
|
|
property_value_out(&property_context, "%d", 2, "gauge", "sysdwn", battery_status.SYSDWN);
|
|
|
|
property_value_out(&property_context, "%d", 2, "gauge", "tda", battery_status.TDA);
|
|
|
|
property_value_out(
|
|
|
|
&property_context, "%d", 2, "gauge", "battpres", battery_status.BATTPRES);
|
|
|
|
property_value_out(&property_context, "%d", 2, "gauge", "authgd", battery_status.AUTH_GD);
|
|
|
|
property_value_out(&property_context, "%d", 2, "gauge", "ocvgd", battery_status.OCVGD);
|
|
|
|
property_value_out(&property_context, "%d", 2, "gauge", "tca", battery_status.TCA);
|
|
|
|
property_value_out(&property_context, "%d", 2, "gauge", "rsvd", battery_status.RSVD);
|
2022-04-07 18:00:45 +03:00
|
|
|
|
2022-11-29 12:08:08 +03:00
|
|
|
// Voltage and current info
|
|
|
|
property_value_out(
|
|
|
|
&property_context,
|
|
|
|
"%d",
|
|
|
|
3,
|
|
|
|
"gauge",
|
|
|
|
"capacity",
|
|
|
|
"full",
|
|
|
|
bq27220_get_full_charge_capacity(&furi_hal_i2c_handle_power));
|
|
|
|
property_value_out(
|
|
|
|
&property_context,
|
|
|
|
"%d",
|
|
|
|
3,
|
|
|
|
"gauge",
|
|
|
|
"capacity",
|
|
|
|
"design",
|
|
|
|
bq27220_get_design_capacity(&furi_hal_i2c_handle_power));
|
|
|
|
property_value_out(
|
|
|
|
&property_context,
|
|
|
|
"%d",
|
|
|
|
3,
|
|
|
|
"gauge",
|
|
|
|
"capacity",
|
|
|
|
"remain",
|
|
|
|
bq27220_get_remaining_capacity(&furi_hal_i2c_handle_power));
|
|
|
|
property_value_out(
|
|
|
|
&property_context,
|
|
|
|
"%d",
|
|
|
|
3,
|
|
|
|
"gauge",
|
|
|
|
"state",
|
|
|
|
"charge",
|
|
|
|
bq27220_get_state_of_charge(&furi_hal_i2c_handle_power));
|
|
|
|
property_value_out(
|
|
|
|
&property_context,
|
|
|
|
"%d",
|
|
|
|
3,
|
|
|
|
"gauge",
|
|
|
|
"state",
|
|
|
|
"health",
|
|
|
|
bq27220_get_state_of_health(&furi_hal_i2c_handle_power));
|
|
|
|
property_value_out(
|
|
|
|
&property_context,
|
|
|
|
"%d",
|
|
|
|
2,
|
|
|
|
"gauge",
|
|
|
|
"voltage",
|
|
|
|
bq27220_get_voltage(&furi_hal_i2c_handle_power));
|
|
|
|
property_value_out(
|
|
|
|
&property_context,
|
|
|
|
"%d",
|
|
|
|
2,
|
|
|
|
"gauge",
|
|
|
|
"current",
|
|
|
|
bq27220_get_current(&furi_hal_i2c_handle_power));
|
|
|
|
|
|
|
|
property_context.last = true;
|
|
|
|
const int battery_temp =
|
|
|
|
(int)furi_hal_power_get_battery_temperature_internal(FuriHalPowerICFuelGauge);
|
|
|
|
property_value_out(&property_context, "%d", 2, "gauge", "temperature", battery_temp);
|
|
|
|
} else {
|
|
|
|
property_context.last = true;
|
|
|
|
property_value_out(&property_context, "%lu", 2, "charger", "ntc", ntc_mpct);
|
|
|
|
}
|
|
|
|
|
|
|
|
furi_string_free(key);
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(value);
|
2022-11-29 12:08:08 +03:00
|
|
|
|
|
|
|
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
|
2022-04-07 18:00:45 +03:00
|
|
|
}
|