ble_glue.c 9.0 KB

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