ble_glue.c 9.7 KB

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