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. #ifdef BLE_GLUE_DEBUG
  56. APPD_Init();
  57. #endif
  58. // Initialize all transport layers
  59. TL_MM_Config_t tl_mm_config;
  60. SHCI_TL_HciInitConf_t SHci_Tl_Init_Conf;
  61. // Reference table initialization
  62. TL_Init();
  63. ble_glue->shci_mtx = osMutexNew(NULL);
  64. ble_glue->shci_sem = osSemaphoreNew(1, 0, NULL);
  65. // FreeRTOS system task creation
  66. ble_glue->thread = furi_thread_alloc();
  67. furi_thread_set_name(ble_glue->thread, "BleShciDriver");
  68. furi_thread_set_stack_size(ble_glue->thread, 1024);
  69. furi_thread_set_context(ble_glue->thread, ble_glue);
  70. furi_thread_set_callback(ble_glue->thread, ble_glue_shci_thread);
  71. furi_thread_start(ble_glue->thread);
  72. // System channel initialization
  73. SHci_Tl_Init_Conf.p_cmdbuffer = (uint8_t*)&ble_glue_system_cmd_buff;
  74. SHci_Tl_Init_Conf.StatusNotCallBack = ble_glue_sys_status_not_callback;
  75. shci_init(ble_glue_sys_user_event_callback, (void*)&SHci_Tl_Init_Conf);
  76. /**< Memory Manager channel initialization */
  77. tl_mm_config.p_BleSpareEvtBuffer = ble_glue_ble_spare_event_buff;
  78. tl_mm_config.p_SystemSpareEvtBuffer = ble_glue_system_spare_event_buff;
  79. tl_mm_config.p_AsynchEvtPool = ble_glue_event_pool;
  80. tl_mm_config.AsynchEvtPoolSize = POOL_SIZE;
  81. TL_MM_Init(&tl_mm_config);
  82. TL_Enable();
  83. /*
  84. * From now, the application is waiting for the ready event ( VS_HCI_C2_Ready )
  85. * received on the system channel before starting the Stack
  86. * This system event is received with ble_glue_sys_user_event_callback()
  87. */
  88. }
  89. const BleGlueC2Info* ble_glue_get_c2_info() {
  90. return &ble_glue->c2_info;
  91. }
  92. BleGlueStatus ble_glue_get_c2_status() {
  93. return ble_glue->status;
  94. }
  95. static void ble_glue_update_c2_fw_info() {
  96. WirelessFwInfo_t wireless_info;
  97. SHCI_GetWirelessFwInfo(&wireless_info);
  98. BleGlueC2Info* local_info = &ble_glue->c2_info;
  99. local_info->VersionMajor = wireless_info.VersionMajor;
  100. local_info->VersionMinor = wireless_info.VersionMinor;
  101. local_info->VersionMajor = wireless_info.VersionMajor;
  102. local_info->VersionMinor = wireless_info.VersionMinor;
  103. local_info->VersionSub = wireless_info.VersionSub;
  104. local_info->VersionBranch = wireless_info.VersionBranch;
  105. local_info->VersionReleaseType = wireless_info.VersionReleaseType;
  106. local_info->MemorySizeSram2B = wireless_info.MemorySizeSram2B;
  107. local_info->MemorySizeSram2A = wireless_info.MemorySizeSram2A;
  108. local_info->MemorySizeSram1 = wireless_info.MemorySizeSram1;
  109. local_info->MemorySizeFlash = wireless_info.MemorySizeFlash;
  110. local_info->StackType = wireless_info.StackType;
  111. local_info->FusVersionMajor = wireless_info.FusVersionMajor;
  112. local_info->FusVersionMinor = wireless_info.FusVersionMinor;
  113. local_info->FusVersionSub = wireless_info.FusVersionSub;
  114. local_info->FusMemorySizeSram2B = wireless_info.FusMemorySizeSram2B;
  115. local_info->FusMemorySizeSram2A = wireless_info.FusMemorySizeSram2A;
  116. local_info->FusMemorySizeFlash = wireless_info.FusMemorySizeFlash;
  117. }
  118. static void ble_glue_dump_stack_info() {
  119. const BleGlueC2Info* c2_info = &ble_glue->c2_info;
  120. FURI_LOG_I(
  121. TAG,
  122. "Core2: FUS: %d.%d.%d, mem %d/%d, flash %d pages",
  123. c2_info->FusVersionMajor,
  124. c2_info->FusVersionMinor,
  125. c2_info->FusVersionSub,
  126. c2_info->FusMemorySizeSram2B,
  127. c2_info->FusMemorySizeSram2A,
  128. c2_info->FusMemorySizeFlash);
  129. FURI_LOG_I(
  130. TAG,
  131. "Core2: Stack: %d.%d.%d, branch %d, reltype %d, stacktype %d, flash %d pages",
  132. c2_info->VersionMajor,
  133. c2_info->VersionMinor,
  134. c2_info->VersionSub,
  135. c2_info->VersionBranch,
  136. c2_info->VersionReleaseType,
  137. c2_info->StackType,
  138. c2_info->MemorySizeFlash);
  139. }
  140. bool ble_glue_wait_for_c2_start(int32_t timeout) {
  141. bool started = false;
  142. do {
  143. // TODO: use mutex?
  144. started = ble_glue->status == BleGlueStatusC2Started;
  145. if(!started) {
  146. timeout--;
  147. osDelay(1);
  148. }
  149. } while(!started && (timeout > 0));
  150. if(started) {
  151. FURI_LOG_I(
  152. TAG,
  153. "C2 boot completed, mode: %s",
  154. ble_glue->c2_info.mode == BleGlueC2ModeFUS ? "FUS" : "Stack");
  155. ble_glue_update_c2_fw_info();
  156. ble_glue_dump_stack_info();
  157. } else {
  158. FURI_LOG_E(TAG, "C2 startup failed");
  159. ble_glue->status = BleGlueStatusBroken;
  160. }
  161. return started;
  162. }
  163. bool ble_glue_start() {
  164. furi_assert(ble_glue);
  165. if(ble_glue->status != BleGlueStatusC2Started) {
  166. return false;
  167. }
  168. bool ret = false;
  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. return ret;
  184. }
  185. bool ble_glue_is_alive() {
  186. if(!ble_glue) {
  187. return false;
  188. }
  189. return ble_glue->status >= BleGlueStatusC2Started;
  190. }
  191. bool ble_glue_is_radio_stack_ready() {
  192. if(!ble_glue) {
  193. return false;
  194. }
  195. return ble_glue->status == BleGlueStatusRadioStackRunning;
  196. }
  197. BleGlueCommandResult ble_glue_force_c2_mode(BleGlueC2Mode desired_mode) {
  198. furi_check(desired_mode > BleGlueC2ModeUnknown);
  199. if(desired_mode == ble_glue->c2_info.mode) {
  200. return BleGlueCommandResultOK;
  201. }
  202. if((ble_glue->c2_info.mode == BleGlueC2ModeFUS) && (desired_mode == BleGlueC2ModeStack)) {
  203. if((ble_glue->c2_info.VersionMajor == 0) && (ble_glue->c2_info.VersionMinor == 0)) {
  204. FURI_LOG_W(TAG, "Stack isn't installed!");
  205. return BleGlueCommandResultError;
  206. }
  207. SHCI_CmdStatus_t status = SHCI_C2_FUS_StartWs();
  208. if(status) {
  209. FURI_LOG_E(TAG, "Failed to start Radio Stack with status: %02X", status);
  210. return BleGlueCommandResultError;
  211. }
  212. return BleGlueCommandResultRestartPending;
  213. }
  214. if((ble_glue->c2_info.mode == BleGlueC2ModeStack) && (desired_mode == BleGlueC2ModeFUS)) {
  215. SHCI_FUS_GetState_ErrorCode_t error_code = 0;
  216. uint8_t fus_state = SHCI_C2_FUS_GetState(&error_code);
  217. FURI_LOG_D(TAG, "FUS state: %X, error = %x", fus_state, error_code);
  218. if(fus_state == SHCI_FUS_CMD_NOT_SUPPORTED) {
  219. // Second call to SHCI_C2_FUS_GetState() restarts whole MCU & boots FUS
  220. fus_state = SHCI_C2_FUS_GetState(&error_code);
  221. FURI_LOG_D(TAG, "FUS state#2: %X, error = %x", fus_state, error_code);
  222. return BleGlueCommandResultRestartPending;
  223. }
  224. return BleGlueCommandResultOK;
  225. }
  226. return BleGlueCommandResultError;
  227. }
  228. static void ble_glue_sys_status_not_callback(SHCI_TL_CmdStatus_t status) {
  229. switch(status) {
  230. case SHCI_TL_CmdBusy:
  231. osMutexAcquire(ble_glue->shci_mtx, osWaitForever);
  232. break;
  233. case SHCI_TL_CmdAvailable:
  234. osMutexRelease(ble_glue->shci_mtx);
  235. break;
  236. default:
  237. break;
  238. }
  239. }
  240. /*
  241. * The type of the payload for a system user event is tSHCI_UserEvtRxParam
  242. * When the system event is both :
  243. * - a ready event (subevtcode = SHCI_SUB_EVT_CODE_READY)
  244. * - reported by the FUS (sysevt_ready_rsp == FUS_FW_RUNNING)
  245. * The buffer shall not be released
  246. * ( eg ((tSHCI_UserEvtRxParam*)pPayload)->status shall be set to SHCI_TL_UserEventFlow_Disable )
  247. * When the status is not filled, the buffer is released by default
  248. */
  249. static void ble_glue_sys_user_event_callback(void* pPayload) {
  250. UNUSED(pPayload);
  251. #ifdef BLE_GLUE_DEBUG
  252. APPD_EnableCPU2();
  253. #endif
  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. } else if(p_sys_event->subevtcode == SHCI_SUB_EVT_ERROR_NOTIF) {
  266. FURI_LOG_E(TAG, "Error during initialization");
  267. } else if(p_sys_event->subevtcode == SHCI_SUB_EVT_BLE_NVM_RAM_UPDATE) {
  268. SHCI_C2_BleNvmRamUpdate_Evt_t* p_sys_ble_nvm_ram_update_event =
  269. (SHCI_C2_BleNvmRamUpdate_Evt_t*)p_sys_event->payload;
  270. if(ble_glue->callback) {
  271. ble_glue->callback(
  272. (uint8_t*)p_sys_ble_nvm_ram_update_event->StartAddress,
  273. p_sys_ble_nvm_ram_update_event->Size,
  274. ble_glue->context);
  275. }
  276. }
  277. }
  278. static void ble_glue_clear_shared_memory() {
  279. memset(ble_glue_event_pool, 0, sizeof(ble_glue_event_pool));
  280. memset(&ble_glue_system_cmd_buff, 0, sizeof(ble_glue_system_cmd_buff));
  281. memset(ble_glue_system_spare_event_buff, 0, sizeof(ble_glue_system_spare_event_buff));
  282. memset(ble_glue_ble_spare_event_buff, 0, sizeof(ble_glue_ble_spare_event_buff));
  283. }
  284. void ble_glue_thread_stop() {
  285. if(ble_glue) {
  286. osThreadId_t thread_id = furi_thread_get_thread_id(ble_glue->thread);
  287. furi_assert(thread_id);
  288. osThreadFlagsSet(thread_id, BLE_GLUE_FLAG_KILL_THREAD);
  289. furi_thread_join(ble_glue->thread);
  290. furi_thread_free(ble_glue->thread);
  291. // Free resources
  292. osMutexDelete(ble_glue->shci_mtx);
  293. osSemaphoreDelete(ble_glue->shci_sem);
  294. ble_glue_clear_shared_memory();
  295. free(ble_glue);
  296. ble_glue = NULL;
  297. }
  298. }
  299. // Wrap functions
  300. static int32_t ble_glue_shci_thread(void* context) {
  301. UNUSED(context);
  302. uint32_t flags = 0;
  303. while(true) {
  304. flags = osThreadFlagsWait(BLE_GLUE_FLAG_ALL, osFlagsWaitAny, osWaitForever);
  305. if(flags & BLE_GLUE_FLAG_SHCI_EVENT) {
  306. shci_user_evt_proc();
  307. }
  308. if(flags & BLE_GLUE_FLAG_KILL_THREAD) {
  309. break;
  310. }
  311. }
  312. return 0;
  313. }
  314. void shci_notify_asynch_evt(void* pdata) {
  315. UNUSED(pdata);
  316. if(ble_glue) {
  317. osThreadId_t thread_id = furi_thread_get_thread_id(ble_glue->thread);
  318. furi_assert(thread_id);
  319. osThreadFlagsSet(thread_id, BLE_GLUE_FLAG_SHCI_EVENT);
  320. }
  321. }
  322. void shci_cmd_resp_release(uint32_t flag) {
  323. UNUSED(flag);
  324. if(ble_glue) {
  325. osSemaphoreRelease(ble_glue->shci_sem);
  326. }
  327. }
  328. void shci_cmd_resp_wait(uint32_t timeout) {
  329. UNUSED(timeout);
  330. if(ble_glue) {
  331. osSemaphoreAcquire(ble_glue->shci_sem, osWaitForever);
  332. }
  333. }
  334. bool ble_glue_reinit_c2() {
  335. return SHCI_C2_Reinit() == SHCI_Success;
  336. }
  337. BleGlueCommandResult ble_glue_fus_stack_delete() {
  338. FURI_LOG_I(TAG, "Erasing stack");
  339. SHCI_CmdStatus_t erase_stat = SHCI_C2_FUS_FwDelete();
  340. FURI_LOG_I(TAG, "Cmd res = %x", erase_stat);
  341. if(erase_stat == SHCI_Success) {
  342. return BleGlueCommandResultOperationOngoing;
  343. }
  344. ble_glue_fus_get_status();
  345. return BleGlueCommandResultError;
  346. }
  347. BleGlueCommandResult ble_glue_fus_stack_install(uint32_t src_addr, uint32_t dst_addr) {
  348. FURI_LOG_I(TAG, "Installing stack");
  349. SHCI_CmdStatus_t write_stat = SHCI_C2_FUS_FwUpgrade(src_addr, dst_addr);
  350. FURI_LOG_I(TAG, "Cmd res = %x", write_stat);
  351. if(write_stat == SHCI_Success) {
  352. return BleGlueCommandResultOperationOngoing;
  353. }
  354. ble_glue_fus_get_status();
  355. return BleGlueCommandResultError;
  356. }
  357. BleGlueCommandResult ble_glue_fus_get_status() {
  358. furi_check(ble_glue->c2_info.mode == BleGlueC2ModeFUS);
  359. SHCI_FUS_GetState_ErrorCode_t error_code = 0;
  360. uint8_t fus_state = SHCI_C2_FUS_GetState(&error_code);
  361. FURI_LOG_I(TAG, "FUS state: %x, error: %x", fus_state, error_code);
  362. if((error_code != 0) || (fus_state == FUS_STATE_VALUE_ERROR)) {
  363. return BleGlueCommandResultError;
  364. } else if(
  365. (fus_state >= FUS_STATE_VALUE_FW_UPGRD_ONGOING) &&
  366. (fus_state <= FUS_STATE_VALUE_SERVICE_ONGOING_END)) {
  367. return BleGlueCommandResultOperationOngoing;
  368. }
  369. return BleGlueCommandResultOK;
  370. }
  371. BleGlueCommandResult ble_glue_fus_wait_operation() {
  372. furi_check(ble_glue->c2_info.mode == BleGlueC2ModeFUS);
  373. bool wip;
  374. do {
  375. BleGlueCommandResult fus_status = ble_glue_fus_get_status();
  376. if(fus_status == BleGlueCommandResultError) {
  377. return BleGlueCommandResultError;
  378. }
  379. wip = fus_status == BleGlueCommandResultOperationOngoing;
  380. if(wip) {
  381. osDelay(20);
  382. }
  383. } while(wip);
  384. return BleGlueCommandResultOK;
  385. }