ble_glue.c 9.7 KB

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