2021-11-04 17:26:41 +00:00
|
|
|
#include "ble_glue.h"
|
|
|
|
#include "app_common.h"
|
|
|
|
#include "ble_app.h"
|
2022-08-22 17:06:17 +00:00
|
|
|
#include <ble/ble.h>
|
|
|
|
|
|
|
|
#include <interface/patterns/ble_thread/tl/tl.h>
|
|
|
|
#include <interface/patterns/ble_thread/shci/shci.h>
|
|
|
|
#include <interface/patterns/ble_thread/tl/shci_tl.h>
|
2021-11-04 17:26:41 +00:00
|
|
|
#include "app_debug.h"
|
2022-04-27 15:53:48 +00:00
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
#include <furi_hal.h>
|
2021-11-04 17:26:41 +00:00
|
|
|
|
2021-11-12 13:04:35 +00:00
|
|
|
#define TAG "Core2"
|
|
|
|
|
2021-12-08 11:28:01 +00:00
|
|
|
#define BLE_GLUE_FLAG_SHCI_EVENT (1UL << 0)
|
|
|
|
#define BLE_GLUE_FLAG_KILL_THREAD (1UL << 1)
|
|
|
|
#define BLE_GLUE_FLAG_ALL (BLE_GLUE_FLAG_SHCI_EVENT | BLE_GLUE_FLAG_KILL_THREAD)
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
#define POOL_SIZE \
|
|
|
|
(CFG_TLBLE_EVT_QUEUE_LENGTH * 4U * \
|
|
|
|
DIVC((sizeof(TL_PacketHeader_t) + TL_BLE_EVENT_FRAME_SIZE), 4U))
|
2021-11-04 17:26:41 +00:00
|
|
|
|
|
|
|
PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint8_t ble_glue_event_pool[POOL_SIZE];
|
|
|
|
PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static TL_CmdPacket_t ble_glue_system_cmd_buff;
|
2022-01-05 16:10:18 +00:00
|
|
|
PLACE_IN_SECTION("MB_MEM2")
|
|
|
|
ALIGN(4)
|
|
|
|
static uint8_t ble_glue_system_spare_event_buff[sizeof(TL_PacketHeader_t) + TL_EVT_HDR_SIZE + 255U];
|
|
|
|
PLACE_IN_SECTION("MB_MEM2")
|
|
|
|
ALIGN(4)
|
|
|
|
static uint8_t ble_glue_ble_spare_event_buff[sizeof(TL_PacketHeader_t) + TL_EVT_HDR_SIZE + 255];
|
2021-11-04 17:26:41 +00:00
|
|
|
|
|
|
|
typedef struct {
|
2022-07-20 10:56:33 +00:00
|
|
|
FuriMutex* shci_mtx;
|
|
|
|
FuriSemaphore* shci_sem;
|
2021-12-08 11:28:01 +00:00
|
|
|
FuriThread* thread;
|
2021-11-04 17:26:41 +00:00
|
|
|
BleGlueStatus status;
|
|
|
|
BleGlueKeyStorageChangedCallback callback;
|
2022-04-27 15:53:48 +00:00
|
|
|
BleGlueC2Info c2_info;
|
2021-11-04 17:26:41 +00:00
|
|
|
void* context;
|
|
|
|
} BleGlue;
|
|
|
|
|
|
|
|
static BleGlue* ble_glue = NULL;
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
static int32_t ble_glue_shci_thread(void* argument);
|
2021-11-04 17:26:41 +00:00
|
|
|
static void ble_glue_sys_status_not_callback(SHCI_TL_CmdStatus_t status);
|
|
|
|
static void ble_glue_sys_user_event_callback(void* pPayload);
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
void ble_glue_set_key_storage_changed_callback(
|
|
|
|
BleGlueKeyStorageChangedCallback callback,
|
|
|
|
void* context) {
|
2021-11-04 17:26:41 +00:00
|
|
|
furi_assert(ble_glue);
|
|
|
|
furi_assert(callback);
|
|
|
|
ble_glue->callback = callback;
|
|
|
|
ble_glue->context = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ble_glue_init() {
|
2022-02-18 19:53:46 +00:00
|
|
|
ble_glue = malloc(sizeof(BleGlue));
|
2021-11-04 17:26:41 +00:00
|
|
|
ble_glue->status = BleGlueStatusStartup;
|
|
|
|
|
[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 13:29:51 +00:00
|
|
|
#ifdef BLE_GLUE_DEBUG
|
|
|
|
APPD_Init();
|
|
|
|
#endif
|
2021-11-04 17:26:41 +00:00
|
|
|
|
|
|
|
// Initialize all transport layers
|
|
|
|
TL_MM_Config_t tl_mm_config;
|
|
|
|
SHCI_TL_HciInitConf_t SHci_Tl_Init_Conf;
|
|
|
|
// Reference table initialization
|
|
|
|
TL_Init();
|
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
ble_glue->shci_mtx = furi_mutex_alloc(FuriMutexTypeNormal);
|
|
|
|
ble_glue->shci_sem = furi_semaphore_alloc(1, 0);
|
2021-11-04 17:26:41 +00:00
|
|
|
|
|
|
|
// FreeRTOS system task creation
|
2022-11-23 12:49:17 +00:00
|
|
|
ble_glue->thread = furi_thread_alloc_ex("BleShciDriver", 1024, ble_glue_shci_thread, ble_glue);
|
2021-12-08 11:28:01 +00:00
|
|
|
furi_thread_start(ble_glue->thread);
|
2021-11-04 17:26:41 +00:00
|
|
|
|
|
|
|
// System channel initialization
|
|
|
|
SHci_Tl_Init_Conf.p_cmdbuffer = (uint8_t*)&ble_glue_system_cmd_buff;
|
|
|
|
SHci_Tl_Init_Conf.StatusNotCallBack = ble_glue_sys_status_not_callback;
|
2022-01-05 16:10:18 +00:00
|
|
|
shci_init(ble_glue_sys_user_event_callback, (void*)&SHci_Tl_Init_Conf);
|
2021-11-04 17:26:41 +00:00
|
|
|
|
|
|
|
/**< Memory Manager channel initialization */
|
|
|
|
tl_mm_config.p_BleSpareEvtBuffer = ble_glue_ble_spare_event_buff;
|
|
|
|
tl_mm_config.p_SystemSpareEvtBuffer = ble_glue_system_spare_event_buff;
|
|
|
|
tl_mm_config.p_AsynchEvtPool = ble_glue_event_pool;
|
|
|
|
tl_mm_config.AsynchEvtPoolSize = POOL_SIZE;
|
2022-01-05 16:10:18 +00:00
|
|
|
TL_MM_Init(&tl_mm_config);
|
2021-11-04 17:26:41 +00:00
|
|
|
TL_Enable();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* From now, the application is waiting for the ready event ( VS_HCI_C2_Ready )
|
|
|
|
* received on the system channel before starting the Stack
|
|
|
|
* This system event is received with ble_glue_sys_user_event_callback()
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2022-04-27 15:53:48 +00:00
|
|
|
const BleGlueC2Info* ble_glue_get_c2_info() {
|
|
|
|
return &ble_glue->c2_info;
|
|
|
|
}
|
2022-02-10 13:01:49 +00:00
|
|
|
|
2022-04-27 15:53:48 +00:00
|
|
|
BleGlueStatus ble_glue_get_c2_status() {
|
|
|
|
return ble_glue->status;
|
|
|
|
}
|
2022-02-10 13:01:49 +00:00
|
|
|
|
2022-06-26 12:00:03 +00:00
|
|
|
static const char* ble_glue_get_reltype_str(const uint8_t reltype) {
|
|
|
|
static char relcode[3] = {0};
|
|
|
|
switch(reltype) {
|
|
|
|
case INFO_STACK_TYPE_BLE_FULL:
|
|
|
|
return "F";
|
|
|
|
case INFO_STACK_TYPE_BLE_HCI:
|
|
|
|
return "H";
|
|
|
|
case INFO_STACK_TYPE_BLE_LIGHT:
|
|
|
|
return "L";
|
|
|
|
case INFO_STACK_TYPE_BLE_BEACON:
|
|
|
|
return "Be";
|
|
|
|
case INFO_STACK_TYPE_BLE_BASIC:
|
|
|
|
return "Ba";
|
|
|
|
case INFO_STACK_TYPE_BLE_FULL_EXT_ADV:
|
|
|
|
return "F+";
|
|
|
|
case INFO_STACK_TYPE_BLE_HCI_EXT_ADV:
|
|
|
|
return "H+";
|
|
|
|
default:
|
|
|
|
snprintf(relcode, sizeof(relcode), "%X", reltype);
|
|
|
|
return relcode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-27 15:53:48 +00:00
|
|
|
static void ble_glue_update_c2_fw_info() {
|
|
|
|
WirelessFwInfo_t wireless_info;
|
|
|
|
SHCI_GetWirelessFwInfo(&wireless_info);
|
|
|
|
BleGlueC2Info* local_info = &ble_glue->c2_info;
|
|
|
|
|
|
|
|
local_info->VersionMajor = wireless_info.VersionMajor;
|
|
|
|
local_info->VersionMinor = wireless_info.VersionMinor;
|
|
|
|
local_info->VersionSub = wireless_info.VersionSub;
|
|
|
|
local_info->VersionBranch = wireless_info.VersionBranch;
|
|
|
|
local_info->VersionReleaseType = wireless_info.VersionReleaseType;
|
|
|
|
|
|
|
|
local_info->MemorySizeSram2B = wireless_info.MemorySizeSram2B;
|
|
|
|
local_info->MemorySizeSram2A = wireless_info.MemorySizeSram2A;
|
|
|
|
local_info->MemorySizeSram1 = wireless_info.MemorySizeSram1;
|
|
|
|
local_info->MemorySizeFlash = wireless_info.MemorySizeFlash;
|
|
|
|
|
|
|
|
local_info->StackType = wireless_info.StackType;
|
2022-06-26 12:00:03 +00:00
|
|
|
snprintf(
|
|
|
|
local_info->StackTypeString,
|
|
|
|
BLE_GLUE_MAX_VERSION_STRING_LEN,
|
2022-08-18 16:54:17 +00:00
|
|
|
"%d.%d.%d:%s",
|
2022-06-26 12:00:03 +00:00
|
|
|
local_info->VersionMajor,
|
|
|
|
local_info->VersionMinor,
|
|
|
|
local_info->VersionSub,
|
|
|
|
ble_glue_get_reltype_str(local_info->StackType));
|
2022-04-27 15:53:48 +00:00
|
|
|
|
|
|
|
local_info->FusVersionMajor = wireless_info.FusVersionMajor;
|
|
|
|
local_info->FusVersionMinor = wireless_info.FusVersionMinor;
|
|
|
|
local_info->FusVersionSub = wireless_info.FusVersionSub;
|
|
|
|
local_info->FusMemorySizeSram2B = wireless_info.FusMemorySizeSram2B;
|
|
|
|
local_info->FusMemorySizeSram2A = wireless_info.FusMemorySizeSram2A;
|
|
|
|
local_info->FusMemorySizeFlash = wireless_info.FusMemorySizeFlash;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ble_glue_dump_stack_info() {
|
|
|
|
const BleGlueC2Info* c2_info = &ble_glue->c2_info;
|
|
|
|
FURI_LOG_I(
|
|
|
|
TAG,
|
|
|
|
"Core2: FUS: %d.%d.%d, mem %d/%d, flash %d pages",
|
|
|
|
c2_info->FusVersionMajor,
|
|
|
|
c2_info->FusVersionMinor,
|
|
|
|
c2_info->FusVersionSub,
|
|
|
|
c2_info->FusMemorySizeSram2B,
|
|
|
|
c2_info->FusMemorySizeSram2A,
|
|
|
|
c2_info->FusMemorySizeFlash);
|
|
|
|
FURI_LOG_I(
|
|
|
|
TAG,
|
|
|
|
"Core2: Stack: %d.%d.%d, branch %d, reltype %d, stacktype %d, flash %d pages",
|
|
|
|
c2_info->VersionMajor,
|
|
|
|
c2_info->VersionMinor,
|
|
|
|
c2_info->VersionSub,
|
|
|
|
c2_info->VersionBranch,
|
|
|
|
c2_info->VersionReleaseType,
|
|
|
|
c2_info->StackType,
|
|
|
|
c2_info->MemorySizeFlash);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ble_glue_wait_for_c2_start(int32_t timeout) {
|
|
|
|
bool started = false;
|
|
|
|
|
|
|
|
do {
|
|
|
|
// TODO: use mutex?
|
|
|
|
started = ble_glue->status == BleGlueStatusC2Started;
|
|
|
|
if(!started) {
|
|
|
|
timeout--;
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_delay_tick(1);
|
2022-04-27 15:53:48 +00:00
|
|
|
}
|
|
|
|
} while(!started && (timeout > 0));
|
|
|
|
|
|
|
|
if(started) {
|
|
|
|
FURI_LOG_I(
|
|
|
|
TAG,
|
|
|
|
"C2 boot completed, mode: %s",
|
|
|
|
ble_glue->c2_info.mode == BleGlueC2ModeFUS ? "FUS" : "Stack");
|
|
|
|
ble_glue_update_c2_fw_info();
|
|
|
|
ble_glue_dump_stack_info();
|
2022-01-02 22:36:42 +00:00
|
|
|
} else {
|
2022-04-27 15:53:48 +00:00
|
|
|
FURI_LOG_E(TAG, "C2 startup failed");
|
2022-01-02 22:36:42 +00:00
|
|
|
ble_glue->status = BleGlueStatusBroken;
|
|
|
|
}
|
2022-02-10 13:01:49 +00:00
|
|
|
|
2022-04-27 15:53:48 +00:00
|
|
|
return started;
|
2021-11-13 02:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ble_glue_start() {
|
|
|
|
furi_assert(ble_glue);
|
|
|
|
|
2022-04-27 15:53:48 +00:00
|
|
|
if(ble_glue->status != BleGlueStatusC2Started) {
|
2021-11-13 02:41:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ret = false;
|
|
|
|
if(ble_app_init()) {
|
|
|
|
FURI_LOG_I(TAG, "Radio stack started");
|
2022-04-27 15:53:48 +00:00
|
|
|
ble_glue->status = BleGlueStatusRadioStackRunning;
|
2021-11-13 02:41:54 +00:00
|
|
|
ret = true;
|
|
|
|
if(SHCI_C2_SetFlashActivityControl(FLASH_ACTIVITY_CONTROL_SEM7) == SHCI_Success) {
|
|
|
|
FURI_LOG_I(TAG, "Flash activity control switched to SEM7");
|
|
|
|
} else {
|
|
|
|
FURI_LOG_E(TAG, "Failed to switch flash activity control to SEM7");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
FURI_LOG_E(TAG, "Radio stack startup failed");
|
|
|
|
ble_glue->status = BleGlueStatusRadioStackMissing;
|
2022-01-02 22:36:42 +00:00
|
|
|
ble_app_thread_stop();
|
2021-11-13 02:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ble_glue_is_alive() {
|
|
|
|
if(!ble_glue) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-27 15:53:48 +00:00
|
|
|
return ble_glue->status >= BleGlueStatusC2Started;
|
2021-11-13 02:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ble_glue_is_radio_stack_ready() {
|
|
|
|
if(!ble_glue) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-27 15:53:48 +00:00
|
|
|
return ble_glue->status == BleGlueStatusRadioStackRunning;
|
2021-11-13 02:41:54 +00:00
|
|
|
}
|
|
|
|
|
2022-04-27 15:53:48 +00:00
|
|
|
BleGlueCommandResult ble_glue_force_c2_mode(BleGlueC2Mode desired_mode) {
|
|
|
|
furi_check(desired_mode > BleGlueC2ModeUnknown);
|
|
|
|
|
|
|
|
if(desired_mode == ble_glue->c2_info.mode) {
|
|
|
|
return BleGlueCommandResultOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if((ble_glue->c2_info.mode == BleGlueC2ModeFUS) && (desired_mode == BleGlueC2ModeStack)) {
|
|
|
|
if((ble_glue->c2_info.VersionMajor == 0) && (ble_glue->c2_info.VersionMinor == 0)) {
|
|
|
|
FURI_LOG_W(TAG, "Stack isn't installed!");
|
|
|
|
return BleGlueCommandResultError;
|
|
|
|
}
|
2022-01-14 08:39:41 +00:00
|
|
|
SHCI_CmdStatus_t status = SHCI_C2_FUS_StartWs();
|
|
|
|
if(status) {
|
|
|
|
FURI_LOG_E(TAG, "Failed to start Radio Stack with status: %02X", status);
|
2022-04-27 15:53:48 +00:00
|
|
|
return BleGlueCommandResultError;
|
2022-01-14 08:39:41 +00:00
|
|
|
}
|
2022-04-27 15:53:48 +00:00
|
|
|
return BleGlueCommandResultRestartPending;
|
2022-01-14 08:39:41 +00:00
|
|
|
}
|
2022-04-27 15:53:48 +00:00
|
|
|
if((ble_glue->c2_info.mode == BleGlueC2ModeStack) && (desired_mode == BleGlueC2ModeFUS)) {
|
|
|
|
SHCI_FUS_GetState_ErrorCode_t error_code = 0;
|
|
|
|
uint8_t fus_state = SHCI_C2_FUS_GetState(&error_code);
|
|
|
|
FURI_LOG_D(TAG, "FUS state: %X, error = %x", fus_state, error_code);
|
|
|
|
if(fus_state == SHCI_FUS_CMD_NOT_SUPPORTED) {
|
|
|
|
// Second call to SHCI_C2_FUS_GetState() restarts whole MCU & boots FUS
|
|
|
|
fus_state = SHCI_C2_FUS_GetState(&error_code);
|
|
|
|
FURI_LOG_D(TAG, "FUS state#2: %X, error = %x", fus_state, error_code);
|
|
|
|
return BleGlueCommandResultRestartPending;
|
|
|
|
}
|
|
|
|
return BleGlueCommandResultOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return BleGlueCommandResultError;
|
2022-01-14 08:39:41 +00:00
|
|
|
}
|
|
|
|
|
2021-11-04 17:26:41 +00:00
|
|
|
static void ble_glue_sys_status_not_callback(SHCI_TL_CmdStatus_t status) {
|
2022-01-05 16:10:18 +00:00
|
|
|
switch(status) {
|
2021-11-04 17:26:41 +00:00
|
|
|
case SHCI_TL_CmdBusy:
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_acquire(ble_glue->shci_mtx, FuriWaitForever);
|
2021-11-04 17:26:41 +00:00
|
|
|
break;
|
|
|
|
case SHCI_TL_CmdAvailable:
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_release(ble_glue->shci_mtx);
|
2021-11-04 17:26:41 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2022-01-05 16:10:18 +00:00
|
|
|
}
|
2021-11-04 17:26:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The type of the payload for a system user event is tSHCI_UserEvtRxParam
|
|
|
|
* When the system event is both :
|
|
|
|
* - a ready event (subevtcode = SHCI_SUB_EVT_CODE_READY)
|
|
|
|
* - reported by the FUS (sysevt_ready_rsp == FUS_FW_RUNNING)
|
|
|
|
* The buffer shall not be released
|
|
|
|
* ( eg ((tSHCI_UserEvtRxParam*)pPayload)->status shall be set to SHCI_TL_UserEventFlow_Disable )
|
|
|
|
* When the status is not filled, the buffer is released by default
|
|
|
|
*/
|
2022-01-05 16:10:18 +00:00
|
|
|
static void ble_glue_sys_user_event_callback(void* pPayload) {
|
2021-11-04 17:26:41 +00:00
|
|
|
UNUSED(pPayload);
|
[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 13:29:51 +00:00
|
|
|
|
|
|
|
#ifdef BLE_GLUE_DEBUG
|
|
|
|
APPD_EnableCPU2();
|
|
|
|
#endif
|
2021-11-04 17:26:41 +00:00
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
TL_AsynchEvt_t* p_sys_event =
|
|
|
|
(TL_AsynchEvt_t*)(((tSHCI_UserEvtRxParam*)pPayload)->pckt->evtserial.evt.payload);
|
|
|
|
|
2021-11-04 17:26:41 +00:00
|
|
|
if(p_sys_event->subevtcode == SHCI_SUB_EVT_CODE_READY) {
|
2022-04-27 15:53:48 +00:00
|
|
|
FURI_LOG_I(TAG, "Core2 started");
|
|
|
|
SHCI_C2_Ready_Evt_t* p_c2_ready_evt = (SHCI_C2_Ready_Evt_t*)p_sys_event->payload;
|
|
|
|
if(p_c2_ready_evt->sysevt_ready_rsp == WIRELESS_FW_RUNNING) {
|
|
|
|
ble_glue->c2_info.mode = BleGlueC2ModeStack;
|
|
|
|
} else if(p_c2_ready_evt->sysevt_ready_rsp == FUS_FW_RUNNING) {
|
|
|
|
ble_glue->c2_info.mode = BleGlueC2ModeFUS;
|
|
|
|
}
|
|
|
|
|
|
|
|
ble_glue->status = BleGlueStatusC2Started;
|
2021-11-04 17:26:41 +00:00
|
|
|
} else if(p_sys_event->subevtcode == SHCI_SUB_EVT_ERROR_NOTIF) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Error during initialization");
|
2021-11-04 17:26:41 +00:00
|
|
|
} else if(p_sys_event->subevtcode == SHCI_SUB_EVT_BLE_NVM_RAM_UPDATE) {
|
2022-01-05 16:10:18 +00:00
|
|
|
SHCI_C2_BleNvmRamUpdate_Evt_t* p_sys_ble_nvm_ram_update_event =
|
|
|
|
(SHCI_C2_BleNvmRamUpdate_Evt_t*)p_sys_event->payload;
|
2021-11-04 17:26:41 +00:00
|
|
|
if(ble_glue->callback) {
|
2022-01-05 16:10:18 +00:00
|
|
|
ble_glue->callback(
|
|
|
|
(uint8_t*)p_sys_ble_nvm_ram_update_event->StartAddress,
|
|
|
|
p_sys_ble_nvm_ram_update_event->Size,
|
|
|
|
ble_glue->context);
|
2021-11-04 17:26:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 11:28:01 +00:00
|
|
|
static void ble_glue_clear_shared_memory() {
|
|
|
|
memset(ble_glue_event_pool, 0, sizeof(ble_glue_event_pool));
|
|
|
|
memset(&ble_glue_system_cmd_buff, 0, sizeof(ble_glue_system_cmd_buff));
|
|
|
|
memset(ble_glue_system_spare_event_buff, 0, sizeof(ble_glue_system_spare_event_buff));
|
|
|
|
memset(ble_glue_ble_spare_event_buff, 0, sizeof(ble_glue_ble_spare_event_buff));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ble_glue_thread_stop() {
|
|
|
|
if(ble_glue) {
|
2022-06-20 14:54:48 +00:00
|
|
|
FuriThreadId thread_id = furi_thread_get_id(ble_glue->thread);
|
2022-03-31 14:57:23 +00:00
|
|
|
furi_assert(thread_id);
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_flags_set(thread_id, BLE_GLUE_FLAG_KILL_THREAD);
|
2021-12-08 11:28:01 +00:00
|
|
|
furi_thread_join(ble_glue->thread);
|
|
|
|
furi_thread_free(ble_glue->thread);
|
|
|
|
// Free resources
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_free(ble_glue->shci_mtx);
|
|
|
|
furi_semaphore_free(ble_glue->shci_sem);
|
2021-12-08 11:28:01 +00:00
|
|
|
ble_glue_clear_shared_memory();
|
|
|
|
free(ble_glue);
|
|
|
|
ble_glue = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-04 17:26:41 +00:00
|
|
|
// Wrap functions
|
2021-12-08 11:28:01 +00:00
|
|
|
static int32_t ble_glue_shci_thread(void* context) {
|
2022-05-06 13:37:10 +00:00
|
|
|
UNUSED(context);
|
2021-12-08 11:28:01 +00:00
|
|
|
uint32_t flags = 0;
|
2022-03-31 14:57:23 +00:00
|
|
|
|
2021-12-08 11:28:01 +00:00
|
|
|
while(true) {
|
2022-07-20 10:56:33 +00:00
|
|
|
flags = furi_thread_flags_wait(BLE_GLUE_FLAG_ALL, FuriFlagWaitAny, FuriWaitForever);
|
2021-12-08 11:28:01 +00:00
|
|
|
if(flags & BLE_GLUE_FLAG_SHCI_EVENT) {
|
|
|
|
shci_user_evt_proc();
|
|
|
|
}
|
|
|
|
if(flags & BLE_GLUE_FLAG_KILL_THREAD) {
|
|
|
|
break;
|
|
|
|
}
|
2021-11-04 17:26:41 +00:00
|
|
|
}
|
2021-12-08 11:28:01 +00:00
|
|
|
|
|
|
|
return 0;
|
2021-11-04 17:26:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void shci_notify_asynch_evt(void* pdata) {
|
|
|
|
UNUSED(pdata);
|
2021-12-08 11:28:01 +00:00
|
|
|
if(ble_glue) {
|
2022-06-20 14:54:48 +00:00
|
|
|
FuriThreadId thread_id = furi_thread_get_id(ble_glue->thread);
|
2022-03-31 14:57:23 +00:00
|
|
|
furi_assert(thread_id);
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_flags_set(thread_id, BLE_GLUE_FLAG_SHCI_EVENT);
|
2021-12-08 11:28:01 +00:00
|
|
|
}
|
2021-11-04 17:26:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void shci_cmd_resp_release(uint32_t flag) {
|
|
|
|
UNUSED(flag);
|
2021-12-08 11:28:01 +00:00
|
|
|
if(ble_glue) {
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_semaphore_release(ble_glue->shci_sem);
|
2021-12-08 11:28:01 +00:00
|
|
|
}
|
2021-11-04 17:26:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void shci_cmd_resp_wait(uint32_t timeout) {
|
|
|
|
UNUSED(timeout);
|
2021-12-08 11:28:01 +00:00
|
|
|
if(ble_glue) {
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_semaphore_acquire(ble_glue->shci_sem, FuriWaitForever);
|
2021-12-08 11:28:01 +00:00
|
|
|
}
|
2021-11-04 17:26:41 +00:00
|
|
|
}
|
2022-04-27 15:53:48 +00:00
|
|
|
|
|
|
|
bool ble_glue_reinit_c2() {
|
|
|
|
return SHCI_C2_Reinit() == SHCI_Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
BleGlueCommandResult ble_glue_fus_stack_delete() {
|
|
|
|
FURI_LOG_I(TAG, "Erasing stack");
|
|
|
|
SHCI_CmdStatus_t erase_stat = SHCI_C2_FUS_FwDelete();
|
|
|
|
FURI_LOG_I(TAG, "Cmd res = %x", erase_stat);
|
|
|
|
if(erase_stat == SHCI_Success) {
|
|
|
|
return BleGlueCommandResultOperationOngoing;
|
|
|
|
}
|
|
|
|
ble_glue_fus_get_status();
|
|
|
|
return BleGlueCommandResultError;
|
|
|
|
}
|
|
|
|
|
|
|
|
BleGlueCommandResult ble_glue_fus_stack_install(uint32_t src_addr, uint32_t dst_addr) {
|
|
|
|
FURI_LOG_I(TAG, "Installing stack");
|
|
|
|
SHCI_CmdStatus_t write_stat = SHCI_C2_FUS_FwUpgrade(src_addr, dst_addr);
|
|
|
|
FURI_LOG_I(TAG, "Cmd res = %x", write_stat);
|
|
|
|
if(write_stat == SHCI_Success) {
|
|
|
|
return BleGlueCommandResultOperationOngoing;
|
|
|
|
}
|
|
|
|
ble_glue_fus_get_status();
|
|
|
|
return BleGlueCommandResultError;
|
|
|
|
}
|
|
|
|
|
|
|
|
BleGlueCommandResult ble_glue_fus_get_status() {
|
|
|
|
furi_check(ble_glue->c2_info.mode == BleGlueC2ModeFUS);
|
|
|
|
SHCI_FUS_GetState_ErrorCode_t error_code = 0;
|
|
|
|
uint8_t fus_state = SHCI_C2_FUS_GetState(&error_code);
|
|
|
|
FURI_LOG_I(TAG, "FUS state: %x, error: %x", fus_state, error_code);
|
|
|
|
if((error_code != 0) || (fus_state == FUS_STATE_VALUE_ERROR)) {
|
|
|
|
return BleGlueCommandResultError;
|
|
|
|
} else if(
|
|
|
|
(fus_state >= FUS_STATE_VALUE_FW_UPGRD_ONGOING) &&
|
|
|
|
(fus_state <= FUS_STATE_VALUE_SERVICE_ONGOING_END)) {
|
|
|
|
return BleGlueCommandResultOperationOngoing;
|
|
|
|
}
|
|
|
|
return BleGlueCommandResultOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
BleGlueCommandResult ble_glue_fus_wait_operation() {
|
|
|
|
furi_check(ble_glue->c2_info.mode == BleGlueC2ModeFUS);
|
2022-12-26 12:13:30 +00:00
|
|
|
|
|
|
|
while(true) {
|
2022-04-27 15:53:48 +00:00
|
|
|
BleGlueCommandResult fus_status = ble_glue_fus_get_status();
|
2022-12-26 12:13:30 +00:00
|
|
|
if(fus_status == BleGlueCommandResultOperationOngoing) {
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_delay_ms(20);
|
2022-12-26 12:13:30 +00:00
|
|
|
} else if(fus_status == BleGlueCommandResultError) {
|
|
|
|
return BleGlueCommandResultError;
|
|
|
|
} else {
|
|
|
|
return BleGlueCommandResultOK;
|
2022-04-27 15:53:48 +00:00
|
|
|
}
|
2022-12-26 12:13:30 +00: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 13:29:51 +00:00
|
|
|
}
|