ble_glue.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. #include "ble_glue.h"
  2. #include "app_common.h"
  3. #include "main.h"
  4. #include "ble_app.h"
  5. #include "ble.h"
  6. #include "tl.h"
  7. #include "shci.h"
  8. #include "shci_tl.h"
  9. #include "app_debug.h"
  10. #include <furi_hal.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 enum {
  27. // Stage 1: core2 startup and FUS
  28. BleGlueStatusStartup,
  29. BleGlueStatusBroken,
  30. BleGlueStatusFusStarted,
  31. // Stage 2: radio stack
  32. BleGlueStatusRadioStackStarted,
  33. BleGlueStatusRadioStackMissing
  34. } BleGlueStatus;
  35. typedef struct {
  36. osMutexId_t shci_mtx;
  37. osSemaphoreId_t shci_sem;
  38. osEventFlagsId_t event_flags;
  39. FuriThread* thread;
  40. BleGlueStatus status;
  41. BleGlueKeyStorageChangedCallback callback;
  42. void* context;
  43. } BleGlue;
  44. static BleGlue* ble_glue = NULL;
  45. static int32_t ble_glue_shci_thread(void* argument);
  46. static void ble_glue_sys_status_not_callback(SHCI_TL_CmdStatus_t status);
  47. static void ble_glue_sys_user_event_callback(void* pPayload);
  48. void ble_glue_set_key_storage_changed_callback(
  49. BleGlueKeyStorageChangedCallback callback,
  50. void* context) {
  51. furi_assert(ble_glue);
  52. furi_assert(callback);
  53. ble_glue->callback = callback;
  54. ble_glue->context = context;
  55. }
  56. void ble_glue_init() {
  57. ble_glue = malloc(sizeof(BleGlue));
  58. ble_glue->status = BleGlueStatusStartup;
  59. // Configure the system Power Mode
  60. // Select HSI as system clock source after Wake Up from Stop mode
  61. LL_RCC_SetClkAfterWakeFromStop(LL_RCC_STOP_WAKEUPCLOCK_HSI);
  62. /* Initialize the CPU2 reset value before starting CPU2 with C2BOOT */
  63. LL_C2_PWR_SetPowerMode(LL_PWR_MODE_SHUTDOWN);
  64. furi_hal_power_insomnia_enter();
  65. // APPD_Init();
  66. // Initialize all transport layers
  67. TL_MM_Config_t tl_mm_config;
  68. SHCI_TL_HciInitConf_t SHci_Tl_Init_Conf;
  69. // Reference table initialization
  70. TL_Init();
  71. ble_glue->shci_mtx = osMutexNew(NULL);
  72. ble_glue->shci_sem = osSemaphoreNew(1, 0, NULL);
  73. ble_glue->event_flags = osEventFlagsNew(NULL);
  74. // FreeRTOS system task creation
  75. ble_glue->thread = furi_thread_alloc();
  76. furi_thread_set_name(ble_glue->thread, "BleShciDriver");
  77. furi_thread_set_stack_size(ble_glue->thread, 1024);
  78. furi_thread_set_context(ble_glue->thread, ble_glue);
  79. furi_thread_set_callback(ble_glue->thread, ble_glue_shci_thread);
  80. furi_thread_start(ble_glue->thread);
  81. // System channel initialization
  82. SHci_Tl_Init_Conf.p_cmdbuffer = (uint8_t*)&ble_glue_system_cmd_buff;
  83. SHci_Tl_Init_Conf.StatusNotCallBack = ble_glue_sys_status_not_callback;
  84. shci_init(ble_glue_sys_user_event_callback, (void*)&SHci_Tl_Init_Conf);
  85. /**< Memory Manager channel initialization */
  86. tl_mm_config.p_BleSpareEvtBuffer = ble_glue_ble_spare_event_buff;
  87. tl_mm_config.p_SystemSpareEvtBuffer = ble_glue_system_spare_event_buff;
  88. tl_mm_config.p_AsynchEvtPool = ble_glue_event_pool;
  89. tl_mm_config.AsynchEvtPoolSize = POOL_SIZE;
  90. TL_MM_Init(&tl_mm_config);
  91. TL_Enable();
  92. /*
  93. * From now, the application is waiting for the ready event ( VS_HCI_C2_Ready )
  94. * received on the system channel before starting the Stack
  95. * This system event is received with ble_glue_sys_user_event_callback()
  96. */
  97. }
  98. bool ble_glue_wait_for_fus_start(WirelessFwInfo_t* info) {
  99. bool ret = false;
  100. size_t countdown = 1000;
  101. while(countdown > 0) {
  102. if(ble_glue->status == BleGlueStatusFusStarted) {
  103. ret = true;
  104. break;
  105. }
  106. countdown--;
  107. osDelay(1);
  108. }
  109. if(ble_glue->status == BleGlueStatusFusStarted) {
  110. SHCI_GetWirelessFwInfo(info);
  111. } else {
  112. FURI_LOG_E(TAG, "Failed to start FUS");
  113. ble_glue->status = BleGlueStatusBroken;
  114. }
  115. return ret;
  116. }
  117. bool ble_glue_start() {
  118. furi_assert(ble_glue);
  119. if(ble_glue->status != BleGlueStatusFusStarted) {
  120. return false;
  121. }
  122. bool ret = false;
  123. furi_hal_power_insomnia_enter();
  124. if(ble_app_init()) {
  125. FURI_LOG_I(TAG, "Radio stack started");
  126. ble_glue->status = BleGlueStatusRadioStackStarted;
  127. ret = true;
  128. if(SHCI_C2_SetFlashActivityControl(FLASH_ACTIVITY_CONTROL_SEM7) == SHCI_Success) {
  129. FURI_LOG_I(TAG, "Flash activity control switched to SEM7");
  130. } else {
  131. FURI_LOG_E(TAG, "Failed to switch flash activity control to SEM7");
  132. }
  133. } else {
  134. FURI_LOG_E(TAG, "Radio stack startup failed");
  135. ble_glue->status = BleGlueStatusRadioStackMissing;
  136. ble_app_thread_stop();
  137. }
  138. furi_hal_power_insomnia_exit();
  139. return ret;
  140. }
  141. bool ble_glue_is_alive() {
  142. if(!ble_glue) {
  143. return false;
  144. }
  145. return ble_glue->status >= BleGlueStatusFusStarted;
  146. }
  147. bool ble_glue_is_radio_stack_ready() {
  148. if(!ble_glue) {
  149. return false;
  150. }
  151. return ble_glue->status == BleGlueStatusRadioStackStarted;
  152. }
  153. bool ble_glue_radio_stack_fw_launch_started() {
  154. bool ret = false;
  155. // Get FUS status
  156. SHCI_FUS_GetState_ErrorCode_t err_code = 0;
  157. uint8_t state = SHCI_C2_FUS_GetState(&err_code);
  158. if(state == FUS_STATE_VALUE_IDLE) {
  159. // When FUS is running we can't read radio stack version correctly
  160. // Trying to start radio stack fw, which leads to reset
  161. FURI_LOG_W(TAG, "FUS is running. Restart to launch Radio Stack");
  162. SHCI_CmdStatus_t status = SHCI_C2_FUS_StartWs();
  163. if(status) {
  164. FURI_LOG_E(TAG, "Failed to start Radio Stack with status: %02X", status);
  165. } else {
  166. ret = true;
  167. }
  168. }
  169. return ret;
  170. }
  171. static void ble_glue_sys_status_not_callback(SHCI_TL_CmdStatus_t status) {
  172. switch(status) {
  173. case SHCI_TL_CmdBusy:
  174. osMutexAcquire(ble_glue->shci_mtx, osWaitForever);
  175. break;
  176. case SHCI_TL_CmdAvailable:
  177. osMutexRelease(ble_glue->shci_mtx);
  178. break;
  179. default:
  180. break;
  181. }
  182. }
  183. /*
  184. * The type of the payload for a system user event is tSHCI_UserEvtRxParam
  185. * When the system event is both :
  186. * - a ready event (subevtcode = SHCI_SUB_EVT_CODE_READY)
  187. * - reported by the FUS (sysevt_ready_rsp == FUS_FW_RUNNING)
  188. * The buffer shall not be released
  189. * ( eg ((tSHCI_UserEvtRxParam*)pPayload)->status shall be set to SHCI_TL_UserEventFlow_Disable )
  190. * When the status is not filled, the buffer is released by default
  191. */
  192. static void ble_glue_sys_user_event_callback(void* pPayload) {
  193. UNUSED(pPayload);
  194. /* Traces channel initialization */
  195. // APPD_EnableCPU2( );
  196. TL_AsynchEvt_t* p_sys_event =
  197. (TL_AsynchEvt_t*)(((tSHCI_UserEvtRxParam*)pPayload)->pckt->evtserial.evt.payload);
  198. if(p_sys_event->subevtcode == SHCI_SUB_EVT_CODE_READY) {
  199. FURI_LOG_I(TAG, "Fus started");
  200. ble_glue->status = BleGlueStatusFusStarted;
  201. furi_hal_power_insomnia_exit();
  202. } else if(p_sys_event->subevtcode == SHCI_SUB_EVT_ERROR_NOTIF) {
  203. FURI_LOG_E(TAG, "Error during initialization");
  204. furi_hal_power_insomnia_exit();
  205. } else if(p_sys_event->subevtcode == SHCI_SUB_EVT_BLE_NVM_RAM_UPDATE) {
  206. SHCI_C2_BleNvmRamUpdate_Evt_t* p_sys_ble_nvm_ram_update_event =
  207. (SHCI_C2_BleNvmRamUpdate_Evt_t*)p_sys_event->payload;
  208. if(ble_glue->callback) {
  209. ble_glue->callback(
  210. (uint8_t*)p_sys_ble_nvm_ram_update_event->StartAddress,
  211. p_sys_ble_nvm_ram_update_event->Size,
  212. ble_glue->context);
  213. }
  214. }
  215. }
  216. static void ble_glue_clear_shared_memory() {
  217. memset(ble_glue_event_pool, 0, sizeof(ble_glue_event_pool));
  218. memset(&ble_glue_system_cmd_buff, 0, sizeof(ble_glue_system_cmd_buff));
  219. memset(ble_glue_system_spare_event_buff, 0, sizeof(ble_glue_system_spare_event_buff));
  220. memset(ble_glue_ble_spare_event_buff, 0, sizeof(ble_glue_ble_spare_event_buff));
  221. }
  222. void ble_glue_thread_stop() {
  223. if(ble_glue) {
  224. osEventFlagsSet(ble_glue->event_flags, BLE_GLUE_FLAG_KILL_THREAD);
  225. furi_thread_join(ble_glue->thread);
  226. furi_thread_free(ble_glue->thread);
  227. // Wait to make sure that EventFlags delivers pending events before memory free
  228. osDelay(50);
  229. // Free resources
  230. osMutexDelete(ble_glue->shci_mtx);
  231. osSemaphoreDelete(ble_glue->shci_sem);
  232. osEventFlagsDelete(ble_glue->event_flags);
  233. ble_glue_clear_shared_memory();
  234. free(ble_glue);
  235. ble_glue = NULL;
  236. }
  237. }
  238. // Wrap functions
  239. static int32_t ble_glue_shci_thread(void* context) {
  240. uint32_t flags = 0;
  241. while(true) {
  242. flags = osEventFlagsWait(
  243. ble_glue->event_flags, BLE_GLUE_FLAG_ALL, osFlagsWaitAny, osWaitForever);
  244. if(flags & BLE_GLUE_FLAG_SHCI_EVENT) {
  245. shci_user_evt_proc();
  246. }
  247. if(flags & BLE_GLUE_FLAG_KILL_THREAD) {
  248. break;
  249. }
  250. }
  251. return 0;
  252. }
  253. void shci_notify_asynch_evt(void* pdata) {
  254. UNUSED(pdata);
  255. if(ble_glue) {
  256. osEventFlagsSet(ble_glue->event_flags, BLE_GLUE_FLAG_SHCI_EVENT);
  257. }
  258. }
  259. void shci_cmd_resp_release(uint32_t flag) {
  260. UNUSED(flag);
  261. if(ble_glue) {
  262. osSemaphoreRelease(ble_glue->shci_sem);
  263. }
  264. }
  265. void shci_cmd_resp_wait(uint32_t timeout) {
  266. UNUSED(timeout);
  267. if(ble_glue) {
  268. osSemaphoreAcquire(ble_glue->shci_sem, osWaitForever);
  269. }
  270. }