ble_glue.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 = furi_alloc(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, "BleShciWorker");
  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. furi_hal_power_insomnia_exit();
  116. return ret;
  117. }
  118. bool ble_glue_start() {
  119. furi_assert(ble_glue);
  120. if(ble_glue->status != BleGlueStatusFusStarted) {
  121. return false;
  122. }
  123. bool ret = false;
  124. furi_hal_power_insomnia_enter();
  125. if(ble_app_init()) {
  126. FURI_LOG_I(TAG, "Radio stack started");
  127. ble_glue->status = BleGlueStatusRadioStackStarted;
  128. ret = true;
  129. if(SHCI_C2_SetFlashActivityControl(FLASH_ACTIVITY_CONTROL_SEM7) == SHCI_Success) {
  130. FURI_LOG_I(TAG, "Flash activity control switched to SEM7");
  131. } else {
  132. FURI_LOG_E(TAG, "Failed to switch flash activity control to SEM7");
  133. }
  134. } else {
  135. FURI_LOG_E(TAG, "Radio stack startup failed");
  136. ble_glue->status = BleGlueStatusRadioStackMissing;
  137. ble_app_thread_stop();
  138. }
  139. furi_hal_power_insomnia_exit();
  140. return ret;
  141. }
  142. bool ble_glue_is_alive() {
  143. if(!ble_glue) {
  144. return false;
  145. }
  146. return ble_glue->status >= BleGlueStatusFusStarted;
  147. }
  148. bool ble_glue_is_radio_stack_ready() {
  149. if(!ble_glue) {
  150. return false;
  151. }
  152. return ble_glue->status == BleGlueStatusRadioStackStarted;
  153. }
  154. static void ble_glue_sys_status_not_callback(SHCI_TL_CmdStatus_t status) {
  155. switch(status) {
  156. case SHCI_TL_CmdBusy:
  157. osMutexAcquire(ble_glue->shci_mtx, osWaitForever);
  158. break;
  159. case SHCI_TL_CmdAvailable:
  160. osMutexRelease(ble_glue->shci_mtx);
  161. break;
  162. default:
  163. break;
  164. }
  165. }
  166. /*
  167. * The type of the payload for a system user event is tSHCI_UserEvtRxParam
  168. * When the system event is both :
  169. * - a ready event (subevtcode = SHCI_SUB_EVT_CODE_READY)
  170. * - reported by the FUS (sysevt_ready_rsp == FUS_FW_RUNNING)
  171. * The buffer shall not be released
  172. * ( eg ((tSHCI_UserEvtRxParam*)pPayload)->status shall be set to SHCI_TL_UserEventFlow_Disable )
  173. * When the status is not filled, the buffer is released by default
  174. */
  175. static void ble_glue_sys_user_event_callback(void* pPayload) {
  176. UNUSED(pPayload);
  177. /* Traces channel initialization */
  178. // APPD_EnableCPU2( );
  179. TL_AsynchEvt_t* p_sys_event =
  180. (TL_AsynchEvt_t*)(((tSHCI_UserEvtRxParam*)pPayload)->pckt->evtserial.evt.payload);
  181. if(p_sys_event->subevtcode == SHCI_SUB_EVT_CODE_READY) {
  182. FURI_LOG_I(TAG, "Fus started");
  183. ble_glue->status = BleGlueStatusFusStarted;
  184. furi_hal_power_insomnia_exit();
  185. } else if(p_sys_event->subevtcode == SHCI_SUB_EVT_ERROR_NOTIF) {
  186. FURI_LOG_E(TAG, "Error during initialization");
  187. furi_hal_power_insomnia_exit();
  188. } else if(p_sys_event->subevtcode == SHCI_SUB_EVT_BLE_NVM_RAM_UPDATE) {
  189. SHCI_C2_BleNvmRamUpdate_Evt_t* p_sys_ble_nvm_ram_update_event =
  190. (SHCI_C2_BleNvmRamUpdate_Evt_t*)p_sys_event->payload;
  191. if(ble_glue->callback) {
  192. ble_glue->callback(
  193. (uint8_t*)p_sys_ble_nvm_ram_update_event->StartAddress,
  194. p_sys_ble_nvm_ram_update_event->Size,
  195. ble_glue->context);
  196. }
  197. }
  198. }
  199. static void ble_glue_clear_shared_memory() {
  200. memset(ble_glue_event_pool, 0, sizeof(ble_glue_event_pool));
  201. memset(&ble_glue_system_cmd_buff, 0, sizeof(ble_glue_system_cmd_buff));
  202. memset(ble_glue_system_spare_event_buff, 0, sizeof(ble_glue_system_spare_event_buff));
  203. memset(ble_glue_ble_spare_event_buff, 0, sizeof(ble_glue_ble_spare_event_buff));
  204. }
  205. void ble_glue_thread_stop() {
  206. if(ble_glue) {
  207. osEventFlagsSet(ble_glue->event_flags, BLE_GLUE_FLAG_KILL_THREAD);
  208. furi_thread_join(ble_glue->thread);
  209. furi_thread_free(ble_glue->thread);
  210. // Wait to make sure that EventFlags delivers pending events before memory free
  211. osDelay(50);
  212. // Free resources
  213. osMutexDelete(ble_glue->shci_mtx);
  214. osSemaphoreDelete(ble_glue->shci_sem);
  215. osEventFlagsDelete(ble_glue->event_flags);
  216. ble_glue_clear_shared_memory();
  217. free(ble_glue);
  218. ble_glue = NULL;
  219. }
  220. }
  221. // Wrap functions
  222. static int32_t ble_glue_shci_thread(void* context) {
  223. uint32_t flags = 0;
  224. while(true) {
  225. flags = osEventFlagsWait(
  226. ble_glue->event_flags, BLE_GLUE_FLAG_ALL, osFlagsWaitAny, osWaitForever);
  227. if(flags & BLE_GLUE_FLAG_SHCI_EVENT) {
  228. shci_user_evt_proc();
  229. }
  230. if(flags & BLE_GLUE_FLAG_KILL_THREAD) {
  231. break;
  232. }
  233. }
  234. return 0;
  235. }
  236. void shci_notify_asynch_evt(void* pdata) {
  237. UNUSED(pdata);
  238. if(ble_glue) {
  239. osEventFlagsSet(ble_glue->event_flags, BLE_GLUE_FLAG_SHCI_EVENT);
  240. }
  241. }
  242. void shci_cmd_resp_release(uint32_t flag) {
  243. UNUSED(flag);
  244. if(ble_glue) {
  245. osSemaphoreRelease(ble_glue->shci_sem);
  246. }
  247. }
  248. void shci_cmd_resp_wait(uint32_t timeout) {
  249. UNUSED(timeout);
  250. if(ble_glue) {
  251. osSemaphoreAcquire(ble_glue->shci_sem, osWaitForever);
  252. }
  253. }