ble_glue.c 9.0 KB

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