ble_glue.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. #include "ble_glue.h"
  2. #include "app_common.h"
  3. #include "ble_app.h"
  4. #include "ble.h"
  5. #include "tl.h"
  6. #include "shci.h"
  7. #include "shci_tl.h"
  8. #include "app_debug.h"
  9. #include <furi_hal.h>
  10. #include <shci/shci.h>
  11. #define TAG "Core2"
  12. #define BLE_GLUE_FLAG_SHCI_EVENT (1UL << 0)
  13. #define BLE_GLUE_FLAG_KILL_THREAD (1UL << 1)
  14. #define BLE_GLUE_FLAG_ALL (BLE_GLUE_FLAG_SHCI_EVENT | BLE_GLUE_FLAG_KILL_THREAD)
  15. #define POOL_SIZE \
  16. (CFG_TLBLE_EVT_QUEUE_LENGTH * 4U * \
  17. DIVC((sizeof(TL_PacketHeader_t) + TL_BLE_EVENT_FRAME_SIZE), 4U))
  18. PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint8_t ble_glue_event_pool[POOL_SIZE];
  19. PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static TL_CmdPacket_t ble_glue_system_cmd_buff;
  20. PLACE_IN_SECTION("MB_MEM2")
  21. ALIGN(4)
  22. static uint8_t ble_glue_system_spare_event_buff[sizeof(TL_PacketHeader_t) + TL_EVT_HDR_SIZE + 255U];
  23. PLACE_IN_SECTION("MB_MEM2")
  24. ALIGN(4)
  25. static uint8_t ble_glue_ble_spare_event_buff[sizeof(TL_PacketHeader_t) + TL_EVT_HDR_SIZE + 255];
  26. typedef struct {
  27. osMutexId_t shci_mtx;
  28. osSemaphoreId_t shci_sem;
  29. FuriThread* thread;
  30. BleGlueStatus status;
  31. BleGlueKeyStorageChangedCallback callback;
  32. BleGlueC2Info c2_info;
  33. void* context;
  34. } BleGlue;
  35. static BleGlue* ble_glue = NULL;
  36. static int32_t ble_glue_shci_thread(void* argument);
  37. static void ble_glue_sys_status_not_callback(SHCI_TL_CmdStatus_t status);
  38. static void ble_glue_sys_user_event_callback(void* pPayload);
  39. void ble_glue_set_key_storage_changed_callback(
  40. BleGlueKeyStorageChangedCallback callback,
  41. void* context) {
  42. furi_assert(ble_glue);
  43. furi_assert(callback);
  44. ble_glue->callback = callback;
  45. ble_glue->context = context;
  46. }
  47. void ble_glue_init() {
  48. ble_glue = malloc(sizeof(BleGlue));
  49. ble_glue->status = BleGlueStatusStartup;
  50. // Configure the system Power Mode
  51. // Select HSI as system clock source after Wake Up from Stop mode
  52. LL_RCC_SetClkAfterWakeFromStop(LL_RCC_STOP_WAKEUPCLOCK_HSI);
  53. /* Initialize the CPU2 reset value before starting CPU2 with C2BOOT */
  54. LL_C2_PWR_SetPowerMode(LL_PWR_MODE_SHUTDOWN);
  55. furi_hal_power_insomnia_enter();
  56. // APPD_Init();
  57. // Initialize all transport layers
  58. TL_MM_Config_t tl_mm_config;
  59. SHCI_TL_HciInitConf_t SHci_Tl_Init_Conf;
  60. // Reference table initialization
  61. TL_Init();
  62. ble_glue->shci_mtx = osMutexNew(NULL);
  63. ble_glue->shci_sem = osSemaphoreNew(1, 0, NULL);
  64. // FreeRTOS system task creation
  65. ble_glue->thread = furi_thread_alloc();
  66. furi_thread_set_name(ble_glue->thread, "BleShciDriver");
  67. furi_thread_set_stack_size(ble_glue->thread, 1024);
  68. furi_thread_set_context(ble_glue->thread, ble_glue);
  69. furi_thread_set_callback(ble_glue->thread, ble_glue_shci_thread);
  70. furi_thread_start(ble_glue->thread);
  71. // System channel initialization
  72. SHci_Tl_Init_Conf.p_cmdbuffer = (uint8_t*)&ble_glue_system_cmd_buff;
  73. SHci_Tl_Init_Conf.StatusNotCallBack = ble_glue_sys_status_not_callback;
  74. shci_init(ble_glue_sys_user_event_callback, (void*)&SHci_Tl_Init_Conf);
  75. /**< Memory Manager channel initialization */
  76. tl_mm_config.p_BleSpareEvtBuffer = ble_glue_ble_spare_event_buff;
  77. tl_mm_config.p_SystemSpareEvtBuffer = ble_glue_system_spare_event_buff;
  78. tl_mm_config.p_AsynchEvtPool = ble_glue_event_pool;
  79. tl_mm_config.AsynchEvtPoolSize = POOL_SIZE;
  80. TL_MM_Init(&tl_mm_config);
  81. TL_Enable();
  82. /*
  83. * From now, the application is waiting for the ready event ( VS_HCI_C2_Ready )
  84. * received on the system channel before starting the Stack
  85. * This system event is received with ble_glue_sys_user_event_callback()
  86. */
  87. }
  88. const BleGlueC2Info* ble_glue_get_c2_info() {
  89. return &ble_glue->c2_info;
  90. }
  91. BleGlueStatus ble_glue_get_c2_status() {
  92. return ble_glue->status;
  93. }
  94. static void ble_glue_update_c2_fw_info() {
  95. WirelessFwInfo_t wireless_info;
  96. SHCI_GetWirelessFwInfo(&wireless_info);
  97. BleGlueC2Info* local_info = &ble_glue->c2_info;
  98. local_info->VersionMajor = wireless_info.VersionMajor;
  99. local_info->VersionMinor = wireless_info.VersionMinor;
  100. local_info->VersionMajor = wireless_info.VersionMajor;
  101. local_info->VersionMinor = wireless_info.VersionMinor;
  102. local_info->VersionSub = wireless_info.VersionSub;
  103. local_info->VersionBranch = wireless_info.VersionBranch;
  104. local_info->VersionReleaseType = wireless_info.VersionReleaseType;
  105. local_info->MemorySizeSram2B = wireless_info.MemorySizeSram2B;
  106. local_info->MemorySizeSram2A = wireless_info.MemorySizeSram2A;
  107. local_info->MemorySizeSram1 = wireless_info.MemorySizeSram1;
  108. local_info->MemorySizeFlash = wireless_info.MemorySizeFlash;
  109. local_info->StackType = wireless_info.StackType;
  110. local_info->FusVersionMajor = wireless_info.FusVersionMajor;
  111. local_info->FusVersionMinor = wireless_info.FusVersionMinor;
  112. local_info->FusVersionSub = wireless_info.FusVersionSub;
  113. local_info->FusMemorySizeSram2B = wireless_info.FusMemorySizeSram2B;
  114. local_info->FusMemorySizeSram2A = wireless_info.FusMemorySizeSram2A;
  115. local_info->FusMemorySizeFlash = wireless_info.FusMemorySizeFlash;
  116. }
  117. static void ble_glue_dump_stack_info() {
  118. const BleGlueC2Info* c2_info = &ble_glue->c2_info;
  119. FURI_LOG_I(
  120. TAG,
  121. "Core2: FUS: %d.%d.%d, mem %d/%d, flash %d pages",
  122. c2_info->FusVersionMajor,
  123. c2_info->FusVersionMinor,
  124. c2_info->FusVersionSub,
  125. c2_info->FusMemorySizeSram2B,
  126. c2_info->FusMemorySizeSram2A,
  127. c2_info->FusMemorySizeFlash);
  128. FURI_LOG_I(
  129. TAG,
  130. "Core2: Stack: %d.%d.%d, branch %d, reltype %d, stacktype %d, flash %d pages",
  131. c2_info->VersionMajor,
  132. c2_info->VersionMinor,
  133. c2_info->VersionSub,
  134. c2_info->VersionBranch,
  135. c2_info->VersionReleaseType,
  136. c2_info->StackType,
  137. c2_info->MemorySizeFlash);
  138. }
  139. bool ble_glue_wait_for_c2_start(int32_t timeout) {
  140. bool started = false;
  141. do {
  142. // TODO: use mutex?
  143. started = ble_glue->status == BleGlueStatusC2Started;
  144. if(!started) {
  145. timeout--;
  146. osDelay(1);
  147. }
  148. } while(!started && (timeout > 0));
  149. if(started) {
  150. FURI_LOG_I(
  151. TAG,
  152. "C2 boot completed, mode: %s",
  153. ble_glue->c2_info.mode == BleGlueC2ModeFUS ? "FUS" : "Stack");
  154. ble_glue_update_c2_fw_info();
  155. ble_glue_dump_stack_info();
  156. } else {
  157. FURI_LOG_E(TAG, "C2 startup failed");
  158. ble_glue->status = BleGlueStatusBroken;
  159. }
  160. return started;
  161. }
  162. bool ble_glue_start() {
  163. furi_assert(ble_glue);
  164. if(ble_glue->status != BleGlueStatusC2Started) {
  165. return false;
  166. }
  167. bool ret = false;
  168. furi_hal_power_insomnia_enter();
  169. if(ble_app_init()) {
  170. FURI_LOG_I(TAG, "Radio stack started");
  171. ble_glue->status = BleGlueStatusRadioStackRunning;
  172. ret = true;
  173. if(SHCI_C2_SetFlashActivityControl(FLASH_ACTIVITY_CONTROL_SEM7) == SHCI_Success) {
  174. FURI_LOG_I(TAG, "Flash activity control switched to SEM7");
  175. } else {
  176. FURI_LOG_E(TAG, "Failed to switch flash activity control to SEM7");
  177. }
  178. } else {
  179. FURI_LOG_E(TAG, "Radio stack startup failed");
  180. ble_glue->status = BleGlueStatusRadioStackMissing;
  181. ble_app_thread_stop();
  182. }
  183. furi_hal_power_insomnia_exit();
  184. return ret;
  185. }
  186. bool ble_glue_is_alive() {
  187. if(!ble_glue) {
  188. return false;
  189. }
  190. return ble_glue->status >= BleGlueStatusC2Started;
  191. }
  192. bool ble_glue_is_radio_stack_ready() {
  193. if(!ble_glue) {
  194. return false;
  195. }
  196. return ble_glue->status == BleGlueStatusRadioStackRunning;
  197. }
  198. BleGlueCommandResult ble_glue_force_c2_mode(BleGlueC2Mode desired_mode) {
  199. furi_check(desired_mode > BleGlueC2ModeUnknown);
  200. if(desired_mode == ble_glue->c2_info.mode) {
  201. return BleGlueCommandResultOK;
  202. }
  203. if((ble_glue->c2_info.mode == BleGlueC2ModeFUS) && (desired_mode == BleGlueC2ModeStack)) {
  204. if((ble_glue->c2_info.VersionMajor == 0) && (ble_glue->c2_info.VersionMinor == 0)) {
  205. FURI_LOG_W(TAG, "Stack isn't installed!");
  206. return BleGlueCommandResultError;
  207. }
  208. SHCI_CmdStatus_t status = SHCI_C2_FUS_StartWs();
  209. if(status) {
  210. FURI_LOG_E(TAG, "Failed to start Radio Stack with status: %02X", status);
  211. return BleGlueCommandResultError;
  212. }
  213. return BleGlueCommandResultRestartPending;
  214. }
  215. if((ble_glue->c2_info.mode == BleGlueC2ModeStack) && (desired_mode == BleGlueC2ModeFUS)) {
  216. SHCI_FUS_GetState_ErrorCode_t error_code = 0;
  217. uint8_t fus_state = SHCI_C2_FUS_GetState(&error_code);
  218. FURI_LOG_D(TAG, "FUS state: %X, error = %x", fus_state, error_code);
  219. if(fus_state == SHCI_FUS_CMD_NOT_SUPPORTED) {
  220. // Second call to SHCI_C2_FUS_GetState() restarts whole MCU & boots FUS
  221. fus_state = SHCI_C2_FUS_GetState(&error_code);
  222. FURI_LOG_D(TAG, "FUS state#2: %X, error = %x", fus_state, error_code);
  223. return BleGlueCommandResultRestartPending;
  224. }
  225. return BleGlueCommandResultOK;
  226. }
  227. return BleGlueCommandResultError;
  228. }
  229. static void ble_glue_sys_status_not_callback(SHCI_TL_CmdStatus_t status) {
  230. switch(status) {
  231. case SHCI_TL_CmdBusy:
  232. osMutexAcquire(ble_glue->shci_mtx, osWaitForever);
  233. break;
  234. case SHCI_TL_CmdAvailable:
  235. osMutexRelease(ble_glue->shci_mtx);
  236. break;
  237. default:
  238. break;
  239. }
  240. }
  241. /*
  242. * The type of the payload for a system user event is tSHCI_UserEvtRxParam
  243. * When the system event is both :
  244. * - a ready event (subevtcode = SHCI_SUB_EVT_CODE_READY)
  245. * - reported by the FUS (sysevt_ready_rsp == FUS_FW_RUNNING)
  246. * The buffer shall not be released
  247. * ( eg ((tSHCI_UserEvtRxParam*)pPayload)->status shall be set to SHCI_TL_UserEventFlow_Disable )
  248. * When the status is not filled, the buffer is released by default
  249. */
  250. static void ble_glue_sys_user_event_callback(void* pPayload) {
  251. UNUSED(pPayload);
  252. /* Traces channel initialization */
  253. // APPD_EnableCPU2( );
  254. TL_AsynchEvt_t* p_sys_event =
  255. (TL_AsynchEvt_t*)(((tSHCI_UserEvtRxParam*)pPayload)->pckt->evtserial.evt.payload);
  256. if(p_sys_event->subevtcode == SHCI_SUB_EVT_CODE_READY) {
  257. FURI_LOG_I(TAG, "Core2 started");
  258. SHCI_C2_Ready_Evt_t* p_c2_ready_evt = (SHCI_C2_Ready_Evt_t*)p_sys_event->payload;
  259. if(p_c2_ready_evt->sysevt_ready_rsp == WIRELESS_FW_RUNNING) {
  260. ble_glue->c2_info.mode = BleGlueC2ModeStack;
  261. } else if(p_c2_ready_evt->sysevt_ready_rsp == FUS_FW_RUNNING) {
  262. ble_glue->c2_info.mode = BleGlueC2ModeFUS;
  263. }
  264. ble_glue->status = BleGlueStatusC2Started;
  265. furi_hal_power_insomnia_exit();
  266. } else if(p_sys_event->subevtcode == SHCI_SUB_EVT_ERROR_NOTIF) {
  267. FURI_LOG_E(TAG, "Error during initialization");
  268. furi_hal_power_insomnia_exit();
  269. } else if(p_sys_event->subevtcode == SHCI_SUB_EVT_BLE_NVM_RAM_UPDATE) {
  270. SHCI_C2_BleNvmRamUpdate_Evt_t* p_sys_ble_nvm_ram_update_event =
  271. (SHCI_C2_BleNvmRamUpdate_Evt_t*)p_sys_event->payload;
  272. if(ble_glue->callback) {
  273. ble_glue->callback(
  274. (uint8_t*)p_sys_ble_nvm_ram_update_event->StartAddress,
  275. p_sys_ble_nvm_ram_update_event->Size,
  276. ble_glue->context);
  277. }
  278. }
  279. }
  280. static void ble_glue_clear_shared_memory() {
  281. memset(ble_glue_event_pool, 0, sizeof(ble_glue_event_pool));
  282. memset(&ble_glue_system_cmd_buff, 0, sizeof(ble_glue_system_cmd_buff));
  283. memset(ble_glue_system_spare_event_buff, 0, sizeof(ble_glue_system_spare_event_buff));
  284. memset(ble_glue_ble_spare_event_buff, 0, sizeof(ble_glue_ble_spare_event_buff));
  285. }
  286. void ble_glue_thread_stop() {
  287. if(ble_glue) {
  288. osThreadId_t thread_id = furi_thread_get_thread_id(ble_glue->thread);
  289. furi_assert(thread_id);
  290. osThreadFlagsSet(thread_id, BLE_GLUE_FLAG_KILL_THREAD);
  291. furi_thread_join(ble_glue->thread);
  292. furi_thread_free(ble_glue->thread);
  293. // Free resources
  294. osMutexDelete(ble_glue->shci_mtx);
  295. osSemaphoreDelete(ble_glue->shci_sem);
  296. ble_glue_clear_shared_memory();
  297. free(ble_glue);
  298. ble_glue = NULL;
  299. }
  300. }
  301. // Wrap functions
  302. static int32_t ble_glue_shci_thread(void* context) {
  303. uint32_t flags = 0;
  304. while(true) {
  305. flags = osThreadFlagsWait(BLE_GLUE_FLAG_ALL, osFlagsWaitAny, osWaitForever);
  306. if(flags & BLE_GLUE_FLAG_SHCI_EVENT) {
  307. shci_user_evt_proc();
  308. }
  309. if(flags & BLE_GLUE_FLAG_KILL_THREAD) {
  310. break;
  311. }
  312. }
  313. return 0;
  314. }
  315. void shci_notify_asynch_evt(void* pdata) {
  316. UNUSED(pdata);
  317. if(ble_glue) {
  318. osThreadId_t thread_id = furi_thread_get_thread_id(ble_glue->thread);
  319. furi_assert(thread_id);
  320. osThreadFlagsSet(thread_id, BLE_GLUE_FLAG_SHCI_EVENT);
  321. }
  322. }
  323. void shci_cmd_resp_release(uint32_t flag) {
  324. UNUSED(flag);
  325. if(ble_glue) {
  326. osSemaphoreRelease(ble_glue->shci_sem);
  327. }
  328. }
  329. void shci_cmd_resp_wait(uint32_t timeout) {
  330. UNUSED(timeout);
  331. if(ble_glue) {
  332. osSemaphoreAcquire(ble_glue->shci_sem, osWaitForever);
  333. }
  334. }
  335. bool ble_glue_reinit_c2() {
  336. return SHCI_C2_Reinit() == SHCI_Success;
  337. }
  338. BleGlueCommandResult ble_glue_fus_stack_delete() {
  339. FURI_LOG_I(TAG, "Erasing stack");
  340. SHCI_CmdStatus_t erase_stat = SHCI_C2_FUS_FwDelete();
  341. FURI_LOG_I(TAG, "Cmd res = %x", erase_stat);
  342. if(erase_stat == SHCI_Success) {
  343. return BleGlueCommandResultOperationOngoing;
  344. }
  345. ble_glue_fus_get_status();
  346. return BleGlueCommandResultError;
  347. }
  348. BleGlueCommandResult ble_glue_fus_stack_install(uint32_t src_addr, uint32_t dst_addr) {
  349. FURI_LOG_I(TAG, "Installing stack");
  350. SHCI_CmdStatus_t write_stat = SHCI_C2_FUS_FwUpgrade(src_addr, dst_addr);
  351. FURI_LOG_I(TAG, "Cmd res = %x", write_stat);
  352. if(write_stat == SHCI_Success) {
  353. return BleGlueCommandResultOperationOngoing;
  354. }
  355. ble_glue_fus_get_status();
  356. return BleGlueCommandResultError;
  357. }
  358. BleGlueCommandResult ble_glue_fus_get_status() {
  359. furi_check(ble_glue->c2_info.mode == BleGlueC2ModeFUS);
  360. SHCI_FUS_GetState_ErrorCode_t error_code = 0;
  361. uint8_t fus_state = SHCI_C2_FUS_GetState(&error_code);
  362. FURI_LOG_I(TAG, "FUS state: %x, error: %x", fus_state, error_code);
  363. if((error_code != 0) || (fus_state == FUS_STATE_VALUE_ERROR)) {
  364. return BleGlueCommandResultError;
  365. } else if(
  366. (fus_state >= FUS_STATE_VALUE_FW_UPGRD_ONGOING) &&
  367. (fus_state <= FUS_STATE_VALUE_SERVICE_ONGOING_END)) {
  368. return BleGlueCommandResultOperationOngoing;
  369. }
  370. return BleGlueCommandResultOK;
  371. }
  372. BleGlueCommandResult ble_glue_fus_wait_operation() {
  373. furi_check(ble_glue->c2_info.mode == BleGlueC2ModeFUS);
  374. bool wip;
  375. do {
  376. BleGlueCommandResult fus_status = ble_glue_fus_get_status();
  377. if(fus_status == BleGlueCommandResultError) {
  378. return BleGlueCommandResultError;
  379. }
  380. wip = fus_status == BleGlueCommandResultOperationOngoing;
  381. if(wip) {
  382. osDelay(20);
  383. }
  384. } while(wip);
  385. return BleGlueCommandResultOK;
  386. }