2020-11-11 06:31:35 +00:00
|
|
|
#include <api-hal-power.h>
|
2021-01-20 07:43:12 +00:00
|
|
|
#include <api-hal-clock.h>
|
[FL-781] FURI, CLI, stdlib: stdout hooks, integration between subsystems, uniform printf usage (#311)
* FURI stdglue: stdout hooks, local and global, ISR safe printf. Uniform newlines for terminal/debug output. Power: prevent sleep while core 2 has not started.
* Furi record, stdglue: check mutex allocation
* remove unused test
* Furi stdglue: buferized output, dynamically allocated state. Furi record: dynamically allocated state. Input dump: proper line ending. Hal VCP: dynamically allocated state.
* Interrupt manager: explicitly init list.
* Makefile: cleanup rules, fix broken dfu upload. F4: add compiler stack protection options.
* BLE: call debug uart callback on transmission complete
* FreeRTOS: add configUSE_NEWLIB_REENTRANT
* API HAL Timebase: fix issue with idle thread stack corruption caused by systick interrupt. BT: cleanup debug info output. FreeRTOS: disable reentry for newlib.
* F4: update stack protection CFLAGS to match used compiller
* F4: disable compiller stack protection because of incompatibility with current compiller
* Makefile: return openocd logs to gdb
* BLE: fixed pin, moar power, ble trace info.
* Prevent sleep when connection is active
* Makefile: return serial port to upload rule, add workaround for mac os
* Furi: prevent usage of stack for cmsis functions.
* F4: add missing includes, add debugger breakpoints
* Applications: per app stack size.
* Furi: honor kernel state in stdglue
* FreeRTOS: remove unused hooks
* Cleanup and format sources
Co-authored-by: DrZlo13 <who.just.the.doctor@gmail.com>
2021-01-29 00:09:33 +00:00
|
|
|
#include <api-hal-bt.h>
|
2021-01-20 07:43:12 +00:00
|
|
|
|
|
|
|
#include <stm32wbxx_ll_rcc.h>
|
|
|
|
#include <stm32wbxx_ll_pwr.h>
|
|
|
|
#include <stm32wbxx_ll_hsem.h>
|
|
|
|
#include <stm32wbxx_ll_cortex.h>
|
|
|
|
|
2020-12-11 12:34:17 +00:00
|
|
|
#include <main.h>
|
2021-01-20 07:43:12 +00:00
|
|
|
#include <hw_conf.h>
|
2020-11-11 06:31:35 +00:00
|
|
|
#include <bq27220.h>
|
|
|
|
#include <bq25896.h>
|
|
|
|
|
2021-02-18 12:49:32 +00:00
|
|
|
volatile uint32_t api_hal_power_insomnia = 1;
|
2021-02-12 17:24:34 +00:00
|
|
|
|
2020-12-11 12:34:17 +00:00
|
|
|
void HAL_RCC_CSSCallback(void) {
|
2021-03-08 15:55:51 +00:00
|
|
|
// TODO: notify user about issue with HSE
|
2020-12-11 12:34:17 +00:00
|
|
|
NVIC_SystemReset();
|
|
|
|
}
|
|
|
|
|
2020-11-11 06:31:35 +00:00
|
|
|
void api_hal_power_init() {
|
2021-02-04 12:20:27 +00:00
|
|
|
LL_PWR_SMPS_SetMode(LL_PWR_SMPS_STEP_DOWN);
|
2020-11-11 06:31:35 +00:00
|
|
|
bq27220_init();
|
|
|
|
bq25896_init();
|
|
|
|
}
|
|
|
|
|
2021-02-12 17:24:34 +00:00
|
|
|
uint16_t api_hal_power_insomnia_level() {
|
|
|
|
return api_hal_power_insomnia;
|
|
|
|
}
|
|
|
|
|
|
|
|
void api_hal_power_insomnia_enter() {
|
|
|
|
api_hal_power_insomnia++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void api_hal_power_insomnia_exit() {
|
|
|
|
api_hal_power_insomnia--;
|
|
|
|
}
|
|
|
|
|
[FL-781] FURI, CLI, stdlib: stdout hooks, integration between subsystems, uniform printf usage (#311)
* FURI stdglue: stdout hooks, local and global, ISR safe printf. Uniform newlines for terminal/debug output. Power: prevent sleep while core 2 has not started.
* Furi record, stdglue: check mutex allocation
* remove unused test
* Furi stdglue: buferized output, dynamically allocated state. Furi record: dynamically allocated state. Input dump: proper line ending. Hal VCP: dynamically allocated state.
* Interrupt manager: explicitly init list.
* Makefile: cleanup rules, fix broken dfu upload. F4: add compiler stack protection options.
* BLE: call debug uart callback on transmission complete
* FreeRTOS: add configUSE_NEWLIB_REENTRANT
* API HAL Timebase: fix issue with idle thread stack corruption caused by systick interrupt. BT: cleanup debug info output. FreeRTOS: disable reentry for newlib.
* F4: update stack protection CFLAGS to match used compiller
* F4: disable compiller stack protection because of incompatibility with current compiller
* Makefile: return openocd logs to gdb
* BLE: fixed pin, moar power, ble trace info.
* Prevent sleep when connection is active
* Makefile: return serial port to upload rule, add workaround for mac os
* Furi: prevent usage of stack for cmsis functions.
* F4: add missing includes, add debugger breakpoints
* Applications: per app stack size.
* Furi: honor kernel state in stdglue
* FreeRTOS: remove unused hooks
* Cleanup and format sources
Co-authored-by: DrZlo13 <who.just.the.doctor@gmail.com>
2021-01-29 00:09:33 +00:00
|
|
|
bool api_hal_power_deep_available() {
|
2021-02-12 17:24:34 +00:00
|
|
|
return api_hal_bt_is_alive() && api_hal_power_insomnia == 0;
|
[FL-781] FURI, CLI, stdlib: stdout hooks, integration between subsystems, uniform printf usage (#311)
* FURI stdglue: stdout hooks, local and global, ISR safe printf. Uniform newlines for terminal/debug output. Power: prevent sleep while core 2 has not started.
* Furi record, stdglue: check mutex allocation
* remove unused test
* Furi stdglue: buferized output, dynamically allocated state. Furi record: dynamically allocated state. Input dump: proper line ending. Hal VCP: dynamically allocated state.
* Interrupt manager: explicitly init list.
* Makefile: cleanup rules, fix broken dfu upload. F4: add compiler stack protection options.
* BLE: call debug uart callback on transmission complete
* FreeRTOS: add configUSE_NEWLIB_REENTRANT
* API HAL Timebase: fix issue with idle thread stack corruption caused by systick interrupt. BT: cleanup debug info output. FreeRTOS: disable reentry for newlib.
* F4: update stack protection CFLAGS to match used compiller
* F4: disable compiller stack protection because of incompatibility with current compiller
* Makefile: return openocd logs to gdb
* BLE: fixed pin, moar power, ble trace info.
* Prevent sleep when connection is active
* Makefile: return serial port to upload rule, add workaround for mac os
* Furi: prevent usage of stack for cmsis functions.
* F4: add missing includes, add debugger breakpoints
* Applications: per app stack size.
* Furi: honor kernel state in stdglue
* FreeRTOS: remove unused hooks
* Cleanup and format sources
Co-authored-by: DrZlo13 <who.just.the.doctor@gmail.com>
2021-01-29 00:09:33 +00:00
|
|
|
}
|
|
|
|
|
2021-03-19 14:23:53 +00:00
|
|
|
void api_hal_power_light_sleep() {
|
|
|
|
__WFI();
|
|
|
|
}
|
|
|
|
|
2021-01-20 07:43:12 +00:00
|
|
|
void api_hal_power_deep_sleep() {
|
|
|
|
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()) {
|
|
|
|
// Release ENTRY_STOP_MODE semaphore
|
|
|
|
LL_HSEM_ReleaseLock(HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID, 0);
|
|
|
|
|
|
|
|
// The switch on HSI before entering Stop Mode is required
|
|
|
|
api_hal_clock_switch_to_hsi();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/**
|
|
|
|
* The switch on HSI before entering Stop Mode is required
|
|
|
|
*/
|
|
|
|
api_hal_clock_switch_to_hsi();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release RCC semaphore */
|
|
|
|
LL_HSEM_ReleaseLock(HSEM, CFG_HW_RCC_SEMID, 0);
|
|
|
|
|
|
|
|
// Prepare deep sleep
|
2021-02-25 10:22:46 +00:00
|
|
|
LL_PWR_SetPowerMode(LL_PWR_MODE_STOP1);
|
2021-01-20 07:43:12 +00:00
|
|
|
LL_LPM_EnableDeepSleep();
|
|
|
|
|
|
|
|
#if defined ( __CC_ARM)
|
|
|
|
// Force store operations
|
|
|
|
__force_stores();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
__WFI();
|
|
|
|
|
|
|
|
/* Release ENTRY_STOP_MODE semaphore */
|
|
|
|
LL_HSEM_ReleaseLock(HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID, 0);
|
|
|
|
|
|
|
|
while(LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID));
|
|
|
|
|
|
|
|
if(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL) {
|
|
|
|
api_hal_clock_switch_to_pll();
|
|
|
|
}
|
|
|
|
|
|
|
|
LL_HSEM_ReleaseLock(HSEM, CFG_HW_RCC_SEMID, 0);
|
|
|
|
}
|
|
|
|
|
2021-03-19 14:23:53 +00:00
|
|
|
void api_hal_power_sleep() {
|
|
|
|
if(api_hal_power_deep_available()) {
|
|
|
|
api_hal_power_deep_sleep();
|
|
|
|
} else {
|
|
|
|
api_hal_power_light_sleep();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-11 06:31:35 +00:00
|
|
|
uint8_t api_hal_power_get_pct() {
|
|
|
|
return bq27220_get_state_of_charge();
|
|
|
|
}
|
|
|
|
|
2021-03-02 16:07:26 +00:00
|
|
|
uint8_t api_hal_power_get_bat_health_pct() {
|
|
|
|
return bq27220_get_state_of_health();
|
|
|
|
}
|
|
|
|
|
2020-11-11 06:31:35 +00:00
|
|
|
bool api_hal_power_is_charging() {
|
|
|
|
return bq25896_is_charging();
|
|
|
|
}
|
|
|
|
|
|
|
|
void api_hal_power_off() {
|
|
|
|
bq25896_poweroff();
|
|
|
|
}
|
|
|
|
|
|
|
|
void api_hal_power_enable_otg() {
|
|
|
|
bq25896_enable_otg();
|
|
|
|
}
|
|
|
|
|
|
|
|
void api_hal_power_disable_otg() {
|
|
|
|
bq25896_disable_otg();
|
|
|
|
}
|
2020-12-02 10:47:13 +00:00
|
|
|
|
2020-12-22 18:16:32 +00:00
|
|
|
uint32_t api_hal_power_get_battery_remaining_capacity() {
|
|
|
|
return bq27220_get_remaining_capacity();
|
2020-12-02 10:47:13 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 18:16:32 +00:00
|
|
|
uint32_t api_hal_power_get_battery_full_capacity() {
|
|
|
|
return bq27220_get_full_charge_capacity();
|
2020-12-02 10:47:13 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 18:16:32 +00:00
|
|
|
float api_hal_power_get_battery_voltage(ApiHalPowerIC ic) {
|
|
|
|
if (ic == ApiHalPowerICCharger) {
|
|
|
|
return (float)bq25896_get_vbat_voltage() / 1000.0f;
|
|
|
|
} else if (ic == ApiHalPowerICFuelGauge) {
|
|
|
|
return (float)bq27220_get_voltage() / 1000.0f;
|
|
|
|
} else {
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float api_hal_power_get_battery_current(ApiHalPowerIC ic) {
|
|
|
|
if (ic == ApiHalPowerICCharger) {
|
|
|
|
return (float)bq25896_get_vbat_current() / 1000.0f;
|
|
|
|
} else if (ic == ApiHalPowerICFuelGauge) {
|
|
|
|
return (float)bq27220_get_current() / 1000.0f;
|
|
|
|
} else {
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float api_hal_power_get_battery_temperature(ApiHalPowerIC ic) {
|
|
|
|
if (ic == ApiHalPowerICCharger) {
|
|
|
|
// Linear approximation, +/- 5 C
|
|
|
|
return (71.0f - (float)bq25896_get_ntc_mpct()/1000) / 0.6f;
|
|
|
|
} else if (ic == ApiHalPowerICFuelGauge) {
|
|
|
|
return ((float)bq27220_get_temperature() - 2731.0f) / 10.0f;
|
|
|
|
} else {
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
|
2020-12-18 18:03:28 +00:00
|
|
|
}
|
|
|
|
|
2021-03-19 19:37:52 +00:00
|
|
|
float api_hal_power_get_usb_voltage(){
|
|
|
|
return (float)bq25896_get_vbus_voltage() / 1000.0f;
|
|
|
|
}
|
|
|
|
|
2020-12-02 10:47:13 +00:00
|
|
|
void api_hal_power_dump_state(string_t buffer) {
|
|
|
|
BatteryStatus battery_status;
|
|
|
|
OperationStatus operation_status;
|
|
|
|
if (bq27220_get_battery_status(&battery_status) == BQ27220_ERROR
|
|
|
|
|| bq27220_get_operation_status(&operation_status) == BQ27220_ERROR) {
|
|
|
|
string_cat_printf(buffer, "Failed to get bq27220 status. Communication error.\r\n");
|
|
|
|
} else {
|
|
|
|
string_cat_printf(buffer,
|
|
|
|
"bq27220: CALMD: %d, SEC0: %d, SEC1: %d, EDV2: %d, VDQ: %d, INITCOMP: %d, SMTH: %d, BTPINT: %d, CFGUPDATE: %d\r\n",
|
|
|
|
operation_status.CALMD, operation_status.SEC0, operation_status.SEC1,
|
|
|
|
operation_status.EDV2, operation_status.VDQ, operation_status.INITCOMP,
|
|
|
|
operation_status.SMTH, operation_status.BTPINT, operation_status.CFGUPDATE
|
|
|
|
);
|
|
|
|
// Battery status register, part 1
|
|
|
|
string_cat_printf(buffer,
|
|
|
|
"bq27220: CHGINH: %d, FC: %d, OTD: %d, OTC: %d, SLEEP: %d, OCVFAIL: %d, OCVCOMP: %d, FD: %d\r\n",
|
|
|
|
battery_status.CHGINH, battery_status.FC, battery_status.OTD,
|
|
|
|
battery_status.OTC, battery_status.SLEEP, battery_status.OCVFAIL,
|
|
|
|
battery_status.OCVCOMP, battery_status.FD
|
|
|
|
);
|
|
|
|
// Battery status register, part 2
|
|
|
|
string_cat_printf(buffer,
|
|
|
|
"bq27220: DSG: %d, SYSDWN: %d, TDA: %d, BATTPRES: %d, AUTH_GD: %d, OCVGD: %d, TCA: %d, RSVD: %d\r\n",
|
|
|
|
battery_status.DSG, battery_status.SYSDWN, battery_status.TDA,
|
|
|
|
battery_status.BATTPRES, battery_status.AUTH_GD, battery_status.OCVGD,
|
|
|
|
battery_status.TCA, battery_status.RSVD
|
|
|
|
);
|
|
|
|
// Voltage and current info
|
|
|
|
string_cat_printf(buffer,
|
2021-03-02 16:07:26 +00:00
|
|
|
"bq27220: Full capacity: %dmAh, Remaining capacity: %dmAh, State of Charge: %d%%, State of health: %d%%\r\n",
|
2020-12-02 10:47:13 +00:00
|
|
|
bq27220_get_full_charge_capacity(), bq27220_get_remaining_capacity(),
|
2021-03-02 16:07:26 +00:00
|
|
|
bq27220_get_state_of_charge(), bq27220_get_state_of_health()
|
2020-12-02 10:47:13 +00:00
|
|
|
);
|
|
|
|
string_cat_printf(buffer,
|
|
|
|
"bq27220: Voltage: %dmV, Current: %dmA, Temperature: %dC\r\n",
|
2020-12-22 18:16:32 +00:00
|
|
|
bq27220_get_voltage(), bq27220_get_current(), (int)api_hal_power_get_battery_temperature(ApiHalPowerICFuelGauge)
|
2020-12-02 10:47:13 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
string_cat_printf(buffer,
|
|
|
|
"bq25896: VBUS: %d, VSYS: %d, VBAT: %d, Current: %d, NTC: %dm%%\r\n",
|
|
|
|
bq25896_get_vbus_voltage(), bq25896_get_vsys_voltage(),
|
|
|
|
bq25896_get_vbat_voltage(), bq25896_get_vbat_current(),
|
|
|
|
bq25896_get_ntc_mpct()
|
|
|
|
);
|
|
|
|
}
|
2021-03-09 11:53:33 +00:00
|
|
|
|
|
|
|
void api_hal_power_enable_external_3_3v(){
|
|
|
|
}
|
|
|
|
|
|
|
|
void api_hal_power_disable_external_3_3v(){
|
2021-03-19 14:23:53 +00:00
|
|
|
}
|