[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>
This commit is contained in:
@@ -396,7 +396,7 @@ typedef enum {
|
||||
* keep debugger enabled while in any low power mode when set to 1
|
||||
* should be set to 0 in production
|
||||
*/
|
||||
#define CFG_DEBUGGER_SUPPORTED 0
|
||||
#define CFG_DEBUGGER_SUPPORTED 1
|
||||
|
||||
/**
|
||||
* When set to 1, the traces are enabled in the BLE services
|
||||
|
@@ -1,25 +1,3 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* File Name : app_debug.c
|
||||
* Description : Debug capabilities source file for STM32WPAN Middleware
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* This software component is licensed by ST under Ultimate Liberty license
|
||||
* SLA0044, the "License"; You may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at:
|
||||
* www.st.com/SLA0044
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "utilities_common.h"
|
||||
|
||||
#include "app_common.h"
|
||||
@@ -28,10 +6,7 @@
|
||||
#include "tl.h"
|
||||
#include "dbg_trace.h"
|
||||
#include <furi_hal.h>
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN PTD */
|
||||
typedef PACKED_STRUCT {
|
||||
GPIO_TypeDef* port;
|
||||
uint16_t pin;
|
||||
@@ -39,10 +14,7 @@ typedef PACKED_STRUCT {
|
||||
uint8_t reserved;
|
||||
}
|
||||
APPD_GpioConfig_t;
|
||||
/* USER CODE END PTD */
|
||||
|
||||
/* Private defines -----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN PD */
|
||||
#define GPIO_NBR_OF_RF_SIGNALS 9
|
||||
#define GPIO_CFG_NBR_OF_FEATURES 34
|
||||
#define NBR_OF_TRACES_CONFIG_PARAMETERS 4
|
||||
@@ -51,12 +23,11 @@ APPD_GpioConfig_t;
|
||||
/**
|
||||
* THIS SHALL BE SET TO A VALUE DIFFERENT FROM 0 ONLY ON REQUEST FROM ST SUPPORT
|
||||
*/
|
||||
#define BLE_DTB_CFG 7
|
||||
#define BLE_DTB_CFG 0
|
||||
// #define BLE_DTB_CFG 7
|
||||
#define SYS_DBG_CFG1 (SHCI_C2_DEBUG_OPTIONS_IPCORE_LP | SHCI_C2_DEBUG_OPTIONS_CPU2_STOP_EN)
|
||||
/* USER CODE END PD */
|
||||
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN PV */
|
||||
PLACE_IN_SECTION("MB_MEM2")
|
||||
ALIGN(4) static SHCI_C2_DEBUG_TracesConfig_t APPD_TracesConfig = {0, 0, 0, 0};
|
||||
PLACE_IN_SECTION("MB_MEM2")
|
||||
@@ -91,7 +62,7 @@ static const APPD_GpioConfig_t aGpioConfigList[GPIO_CFG_NBR_OF_FEATURES] = {
|
||||
{GPIOA, LL_GPIO_PIN_0, 0, 0}, /* IPCC_TRACES_TX - Set on Entry / Reset on Exit */
|
||||
{GPIOA, LL_GPIO_PIN_6, 1, 0}, /* HARD_FAULT - Set on Entry / Reset on Exit */
|
||||
/* From v1.1.1 */
|
||||
{GPIOA, LL_GPIO_PIN_0, 0, 0}, /* IP_CORE_LP_STATUS - Set on Entry / Reset on Exit */
|
||||
{GPIOC, LL_GPIO_PIN_1, 1, 0}, /* IP_CORE_LP_STATUS - Set on Entry / Reset on Exit */
|
||||
/* From v1.2.0 */
|
||||
{GPIOA, LL_GPIO_PIN_0, 0, 0}, /* END_OF_CONNECTION_EVENT - Set on Entry / Reset on Exit */
|
||||
{GPIOA, LL_GPIO_PIN_0, 0, 0}, /* TIMER_SERVER_CALLBACK - Toggle on Entry */
|
||||
@@ -130,65 +101,20 @@ static const APPD_GpioConfig_t aRfConfigList[GPIO_NBR_OF_RF_SIGNALS] = {
|
||||
{GPIOB, LL_GPIO_PIN_10, 0, 0}, /* DTB18 - FSM4 */
|
||||
};
|
||||
#endif
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Global variables ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN GV */
|
||||
/* USER CODE END GV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
/* USER CODE BEGIN PFP */
|
||||
static void APPD_SetCPU2GpioConfig(void);
|
||||
static void APPD_BleDtbCfg(void);
|
||||
/* USER CODE END PFP */
|
||||
|
||||
/* Functions Definition ------------------------------------------------------*/
|
||||
void APPD_Init(void) {
|
||||
/* USER CODE BEGIN APPD_Init */
|
||||
#if(CFG_DEBUGGER_SUPPORTED == 1)
|
||||
/**
|
||||
* Keep debugger enabled while in any low power mode
|
||||
*/
|
||||
LL_DBGMCU_EnableDBGSleepMode();
|
||||
LL_DBGMCU_EnableDBGStopMode();
|
||||
|
||||
/***************** ENABLE DEBUGGER *************************************/
|
||||
LL_EXTI_EnableIT_32_63(LL_EXTI_LINE_48);
|
||||
|
||||
#else
|
||||
LL_GPIO_InitTypeDef gpio_config = {0};
|
||||
LL_PWR_EnableVddUSB();
|
||||
|
||||
gpio_config.Mode = LL_GPIO_MODE_ANALOG;
|
||||
gpio_config.Speed = LL_GPIO_SPEED_FREQ_LOW;
|
||||
// gpio_config.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
||||
// gpio_config.Pull = LL_GPIO_PULL_NO;
|
||||
// gpio_config.Alternate = LL_GPIO_AF_10;
|
||||
gpio_config.Pin = LL_GPIO_PIN_15 | LL_GPIO_PIN_14 | LL_GPIO_PIN_13;
|
||||
LL_GPIO_Init(GPIOA, &gpio_config);
|
||||
|
||||
gpio_config.Pin = LL_GPIO_PIN_4 | LL_GPIO_PIN_3;
|
||||
LL_GPIO_Init(GPIOB, &gpio_config);
|
||||
|
||||
LL_DBGMCU_DisableDBGSleepMode();
|
||||
LL_DBGMCU_DisableDBGStopMode();
|
||||
LL_DBGMCU_DisableDBGStandbyMode();
|
||||
|
||||
#endif /* (CFG_DEBUGGER_SUPPORTED == 1) */
|
||||
|
||||
void APPD_Init() {
|
||||
#if(CFG_DEBUG_TRACE != 0)
|
||||
DbgTraceInit();
|
||||
#endif
|
||||
|
||||
APPD_SetCPU2GpioConfig();
|
||||
APPD_BleDtbCfg();
|
||||
|
||||
/* USER CODE END APPD_Init */
|
||||
return;
|
||||
}
|
||||
|
||||
void APPD_EnableCPU2(void) {
|
||||
/* USER CODE BEGIN APPD_EnableCPU2 */
|
||||
SHCI_C2_DEBUG_Init_Cmd_Packet_t DebugCmdPacket = {
|
||||
{{0, 0, 0}}, /**< Does not need to be initialized */
|
||||
{(uint8_t*)aGpioConfigList,
|
||||
@@ -204,6 +130,7 @@ void APPD_EnableCPU2(void) {
|
||||
/** GPIO DEBUG Initialization */
|
||||
SHCI_C2_DEBUG_Init(&DebugCmdPacket);
|
||||
|
||||
// We don't need External Power Amplifier
|
||||
// LL_GPIO_InitTypeDef gpio_config;
|
||||
// gpio_config.Pull = GPIO_NOPULL;
|
||||
// gpio_config.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
@@ -212,17 +139,10 @@ void APPD_EnableCPU2(void) {
|
||||
// HAL_GPIO_Init(GPIOC, &gpio_config);
|
||||
// SHCI_C2_ExtpaConfig((uint32_t)GPIOC, LL_GPIO_PIN_3, EXT_PA_ENABLED_LOW, EXT_PA_ENABLED);
|
||||
|
||||
/* USER CODE END APPD_EnableCPU2 */
|
||||
return;
|
||||
}
|
||||
|
||||
/*************************************************************
|
||||
*
|
||||
* LOCAL FUNCTIONS
|
||||
*
|
||||
*************************************************************/
|
||||
static void APPD_SetCPU2GpioConfig(void) {
|
||||
/* USER CODE BEGIN APPD_SetCPU2GpioConfig */
|
||||
LL_GPIO_InitTypeDef gpio_config = {0};
|
||||
uint8_t local_loop;
|
||||
uint16_t gpioa_pin_list;
|
||||
@@ -259,8 +179,9 @@ static void APPD_SetCPU2GpioConfig(void) {
|
||||
gpio_config.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
||||
gpio_config.Pull = LL_GPIO_PULL_NO;
|
||||
|
||||
gpio_config.Pin = LL_GPIO_PIN_15 | LL_GPIO_PIN_14 | LL_GPIO_PIN_13;
|
||||
LL_GPIO_Init(GPIOA, &gpio_config);
|
||||
// Never disable SWD, why would you?
|
||||
// gpio_config.Pin = LL_GPIO_PIN_15 | LL_GPIO_PIN_14 | LL_GPIO_PIN_13;
|
||||
// LL_GPIO_Init(GPIOA, &gpio_config);
|
||||
|
||||
if(gpioa_pin_list != 0) {
|
||||
gpio_config.Pin = gpioa_pin_list;
|
||||
@@ -282,13 +203,9 @@ static void APPD_SetCPU2GpioConfig(void) {
|
||||
LL_GPIO_Init(GPIOC, &gpio_config);
|
||||
LL_GPIO_ResetOutputPin(GPIOC, gpioa_pin_list);
|
||||
}
|
||||
|
||||
/* USER CODE END APPD_SetCPU2GpioConfig */
|
||||
return;
|
||||
}
|
||||
|
||||
static void APPD_BleDtbCfg(void) {
|
||||
/* USER CODE BEGIN APPD_BleDtbCfg */
|
||||
#if(BLE_DTB_CFG != 0)
|
||||
LL_GPIO_InitTypeDef gpio_config = {0};
|
||||
uint8_t local_loop;
|
||||
@@ -304,11 +221,9 @@ static void APPD_BleDtbCfg(void) {
|
||||
case(uint32_t)GPIOA:
|
||||
gpioa_pin_list |= aRfConfigList[local_loop].pin;
|
||||
break;
|
||||
|
||||
case(uint32_t)GPIOB:
|
||||
gpiob_pin_list |= aRfConfigList[local_loop].pin;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -334,16 +249,8 @@ static void APPD_BleDtbCfg(void) {
|
||||
LL_GPIO_Init(GPIOB, &gpio_config);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* USER CODE END APPD_BleDtbCfg */
|
||||
return;
|
||||
}
|
||||
|
||||
/*************************************************************
|
||||
*
|
||||
* WRAP FUNCTIONS
|
||||
*
|
||||
*************************************************************/
|
||||
#if(CFG_DEBUG_TRACE != 0)
|
||||
void DbgOutputInit(void) {
|
||||
}
|
||||
@@ -353,5 +260,3 @@ void DbgOutputTraces(uint8_t* p_data, uint16_t size, void (*cb)(void)) {
|
||||
cb();
|
||||
}
|
||||
#endif
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
||||
|
@@ -63,9 +63,10 @@ void ble_glue_init() {
|
||||
LL_RCC_SetClkAfterWakeFromStop(LL_RCC_STOP_WAKEUPCLOCK_HSI);
|
||||
/* Initialize the CPU2 reset value before starting CPU2 with C2BOOT */
|
||||
LL_C2_PWR_SetPowerMode(LL_PWR_MODE_SHUTDOWN);
|
||||
furi_hal_power_insomnia_enter();
|
||||
|
||||
// APPD_Init();
|
||||
#ifdef BLE_GLUE_DEBUG
|
||||
APPD_Init();
|
||||
#endif
|
||||
|
||||
// Initialize all transport layers
|
||||
TL_MM_Config_t tl_mm_config;
|
||||
@@ -198,7 +199,6 @@ bool ble_glue_start() {
|
||||
}
|
||||
|
||||
bool ret = false;
|
||||
furi_hal_power_insomnia_enter();
|
||||
if(ble_app_init()) {
|
||||
FURI_LOG_I(TAG, "Radio stack started");
|
||||
ble_glue->status = BleGlueStatusRadioStackRunning;
|
||||
@@ -213,7 +213,6 @@ bool ble_glue_start() {
|
||||
ble_glue->status = BleGlueStatusRadioStackMissing;
|
||||
ble_app_thread_stop();
|
||||
}
|
||||
furi_hal_power_insomnia_exit();
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -293,8 +292,10 @@ static void ble_glue_sys_status_not_callback(SHCI_TL_CmdStatus_t status) {
|
||||
*/
|
||||
static void ble_glue_sys_user_event_callback(void* pPayload) {
|
||||
UNUSED(pPayload);
|
||||
/* Traces channel initialization */
|
||||
// APPD_EnableCPU2( );
|
||||
|
||||
#ifdef BLE_GLUE_DEBUG
|
||||
APPD_EnableCPU2();
|
||||
#endif
|
||||
|
||||
TL_AsynchEvt_t* p_sys_event =
|
||||
(TL_AsynchEvt_t*)(((tSHCI_UserEvtRxParam*)pPayload)->pckt->evtserial.evt.payload);
|
||||
@@ -309,10 +310,8 @@ static void ble_glue_sys_user_event_callback(void* pPayload) {
|
||||
}
|
||||
|
||||
ble_glue->status = BleGlueStatusC2Started;
|
||||
furi_hal_power_insomnia_exit();
|
||||
} else if(p_sys_event->subevtcode == SHCI_SUB_EVT_ERROR_NOTIF) {
|
||||
FURI_LOG_E(TAG, "Error during initialization");
|
||||
furi_hal_power_insomnia_exit();
|
||||
} else if(p_sys_event->subevtcode == SHCI_SUB_EVT_BLE_NVM_RAM_UPDATE) {
|
||||
SHCI_C2_BleNvmRamUpdate_Evt_t* p_sys_ble_nvm_ram_update_event =
|
||||
(SHCI_C2_BleNvmRamUpdate_Evt_t*)p_sys_event->payload;
|
||||
@@ -444,4 +443,4 @@ BleGlueCommandResult ble_glue_fus_wait_operation() {
|
||||
} while(wip);
|
||||
|
||||
return BleGlueCommandResultOK;
|
||||
}
|
||||
}
|
||||
|
@@ -115,7 +115,6 @@ SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification(void* pckt) {
|
||||
if(gap->enable_adv) {
|
||||
// Restart advertising
|
||||
gap_advertise_start(GapStateAdvFast);
|
||||
furi_hal_power_insomnia_exit();
|
||||
}
|
||||
GapEvent event = {.type = GapEventTypeDisconnected};
|
||||
gap->on_event_cb(event, gap->context);
|
||||
@@ -151,8 +150,7 @@ SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification(void* pckt) {
|
||||
}
|
||||
break;
|
||||
|
||||
case EVT_LE_CONN_COMPLETE:
|
||||
furi_hal_power_insomnia_enter();
|
||||
case EVT_LE_CONN_COMPLETE: {
|
||||
hci_le_connection_complete_event_rp0* event =
|
||||
(hci_le_connection_complete_event_rp0*)meta_evt->data;
|
||||
gap->connection_params.conn_interval = event->Conn_Interval;
|
||||
@@ -169,7 +167,7 @@ SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification(void* pckt) {
|
||||
gap_verify_connection_parameters(gap);
|
||||
// Start pairing by sending security request
|
||||
aci_gap_slave_security_req(event->Connection_Handle);
|
||||
break;
|
||||
} break;
|
||||
|
||||
case EVT_LE_ADVERTISING_REPORT: {
|
||||
if(gap_scan) {
|
||||
@@ -414,7 +412,9 @@ static void gap_advertise_start(GapState new_state) {
|
||||
// Stop advertising
|
||||
status = aci_gap_set_non_discoverable();
|
||||
if(status) {
|
||||
FURI_LOG_E(TAG, "Stop Advertising Failed, result: %d", status);
|
||||
FURI_LOG_E(TAG, "set_non_discoverable failed %d", status);
|
||||
} else {
|
||||
FURI_LOG_D(TAG, "set_non_discoverable success");
|
||||
}
|
||||
}
|
||||
// Configure advertising
|
||||
@@ -431,7 +431,7 @@ static void gap_advertise_start(GapState new_state) {
|
||||
0,
|
||||
0);
|
||||
if(status) {
|
||||
FURI_LOG_E(TAG, "Set discoverable err: %d", status);
|
||||
FURI_LOG_E(TAG, "set_discoverable failed %d", status);
|
||||
}
|
||||
gap->state = new_state;
|
||||
GapEvent event = {.type = GapEventTypeStartAdvertising};
|
||||
@@ -440,14 +440,25 @@ static void gap_advertise_start(GapState new_state) {
|
||||
}
|
||||
|
||||
static void gap_advertise_stop() {
|
||||
tBleStatus ret;
|
||||
if(gap->state > GapStateIdle) {
|
||||
if(gap->state == GapStateConnected) {
|
||||
// Terminate connection
|
||||
aci_gap_terminate(gap->service.connection_handle, 0x13);
|
||||
ret = aci_gap_terminate(gap->service.connection_handle, 0x13);
|
||||
if(ret != BLE_STATUS_SUCCESS) {
|
||||
FURI_LOG_E(TAG, "terminate failed %d", ret);
|
||||
} else {
|
||||
FURI_LOG_D(TAG, "terminate success");
|
||||
}
|
||||
}
|
||||
// Stop advertising
|
||||
osTimerStop(gap->advertise_timer);
|
||||
aci_gap_set_non_discoverable();
|
||||
ret = aci_gap_set_non_discoverable();
|
||||
if(ret != BLE_STATUS_SUCCESS) {
|
||||
FURI_LOG_E(TAG, "set_non_discoverable failed %d", ret);
|
||||
} else {
|
||||
FURI_LOG_D(TAG, "set_non_discoverable success");
|
||||
}
|
||||
gap->state = GapStateIdle;
|
||||
}
|
||||
GapEvent event = {.type = GapEventTypeStopAdvertising};
|
||||
|
Reference in New Issue
Block a user