ble_glue.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 "cmsis_os.h"
  9. #include "shci_tl.h"
  10. #include "app_debug.h"
  11. #include <furi-hal.h>
  12. #define TAG "Core2"
  13. #define POOL_SIZE (CFG_TLBLE_EVT_QUEUE_LENGTH*4U*DIVC(( sizeof(TL_PacketHeader_t) + TL_BLE_EVENT_FRAME_SIZE ), 4U))
  14. PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint8_t ble_glue_event_pool[POOL_SIZE];
  15. PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static TL_CmdPacket_t ble_glue_system_cmd_buff;
  16. 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];
  17. 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];
  18. typedef enum {
  19. // Stage 1: core2 startup and FUS
  20. BleGlueStatusStartup,
  21. BleGlueStatusBroken,
  22. BleGlueStatusFusStarted,
  23. // Stage 2: radio stack
  24. BleGlueStatusRadioStackStarted,
  25. BleGlueStatusRadioStackMissing
  26. } BleGlueStatus;
  27. typedef struct {
  28. osMutexId_t shci_mtx;
  29. osSemaphoreId_t shci_sem;
  30. osThreadId_t shci_user_event_thread_id;
  31. osThreadAttr_t shci_user_event_thread_attr;
  32. BleGlueStatus status;
  33. BleGlueKeyStorageChangedCallback callback;
  34. void* context;
  35. } BleGlue;
  36. static BleGlue* ble_glue = NULL;
  37. static void ble_glue_user_event_thread(void *argument);
  38. static void ble_glue_sys_status_not_callback(SHCI_TL_CmdStatus_t status);
  39. static void ble_glue_sys_user_event_callback(void* pPayload);
  40. void ble_glue_set_key_storage_changed_callback(BleGlueKeyStorageChangedCallback callback, void* context) {
  41. furi_assert(ble_glue);
  42. furi_assert(callback);
  43. ble_glue->callback = callback;
  44. ble_glue->context = context;
  45. }
  46. void ble_glue_init() {
  47. ble_glue = furi_alloc(sizeof(BleGlue));
  48. ble_glue->status = BleGlueStatusStartup;
  49. ble_glue->shci_user_event_thread_attr.name = "BleShciWorker";
  50. ble_glue->shci_user_event_thread_attr.stack_size = 1024;
  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. // FreeRTOS system task creation
  66. ble_glue->shci_user_event_thread_id = osThreadNew(ble_glue_user_event_thread, NULL, &ble_glue->shci_user_event_thread_attr);
  67. // System channel initialization
  68. SHci_Tl_Init_Conf.p_cmdbuffer = (uint8_t*)&ble_glue_system_cmd_buff;
  69. SHci_Tl_Init_Conf.StatusNotCallBack = ble_glue_sys_status_not_callback;
  70. shci_init(ble_glue_sys_user_event_callback, (void*) &SHci_Tl_Init_Conf);
  71. /**< Memory Manager channel initialization */
  72. tl_mm_config.p_BleSpareEvtBuffer = ble_glue_ble_spare_event_buff;
  73. tl_mm_config.p_SystemSpareEvtBuffer = ble_glue_system_spare_event_buff;
  74. tl_mm_config.p_AsynchEvtPool = ble_glue_event_pool;
  75. tl_mm_config.AsynchEvtPoolSize = POOL_SIZE;
  76. TL_MM_Init( &tl_mm_config );
  77. TL_Enable();
  78. /*
  79. * From now, the application is waiting for the ready event ( VS_HCI_C2_Ready )
  80. * received on the system channel before starting the Stack
  81. * This system event is received with ble_glue_sys_user_event_callback()
  82. */
  83. }
  84. static bool ble_glue_wait_status(BleGlueStatus status) {
  85. bool ret = false;
  86. size_t countdown = 1000;
  87. while (countdown > 0) {
  88. if (ble_glue->status == status) {
  89. ret = true;
  90. break;
  91. }
  92. countdown--;
  93. osDelay(1);
  94. }
  95. return ret;
  96. }
  97. bool ble_glue_start() {
  98. furi_assert(ble_glue);
  99. if (!ble_glue_wait_status(BleGlueStatusFusStarted)) {
  100. // shutdown core2 power
  101. FURI_LOG_E(TAG, "Core2 catastrophic failure, cutting its power");
  102. LL_C2_PWR_SetPowerMode(LL_PWR_MODE_SHUTDOWN);
  103. ble_glue->status = BleGlueStatusBroken;
  104. furi_hal_power_insomnia_exit();
  105. return false;
  106. }
  107. bool ret = false;
  108. furi_hal_power_insomnia_enter();
  109. if(ble_app_init()) {
  110. FURI_LOG_I(TAG, "Radio stack started");
  111. ble_glue->status = BleGlueStatusRadioStackStarted;
  112. ret = true;
  113. if(SHCI_C2_SetFlashActivityControl(FLASH_ACTIVITY_CONTROL_SEM7) == SHCI_Success) {
  114. FURI_LOG_I(TAG, "Flash activity control switched to SEM7");
  115. } else {
  116. FURI_LOG_E(TAG, "Failed to switch flash activity control to SEM7");
  117. }
  118. } else {
  119. FURI_LOG_E(TAG, "Radio stack startup failed");
  120. ble_glue->status = BleGlueStatusRadioStackMissing;
  121. }
  122. furi_hal_power_insomnia_exit();
  123. return ret;
  124. }
  125. bool ble_glue_is_alive() {
  126. if(!ble_glue) {
  127. return false;
  128. }
  129. return ble_glue->status >= BleGlueStatusFusStarted;
  130. }
  131. bool ble_glue_is_radio_stack_ready() {
  132. if(!ble_glue) {
  133. return false;
  134. }
  135. return ble_glue->status == BleGlueStatusRadioStackStarted;
  136. }
  137. static void ble_glue_sys_status_not_callback(SHCI_TL_CmdStatus_t status) {
  138. switch (status) {
  139. case SHCI_TL_CmdBusy:
  140. osMutexAcquire( ble_glue->shci_mtx, osWaitForever );
  141. break;
  142. case SHCI_TL_CmdAvailable:
  143. osMutexRelease( ble_glue->shci_mtx );
  144. break;
  145. default:
  146. break;
  147. }
  148. }
  149. /*
  150. * The type of the payload for a system user event is tSHCI_UserEvtRxParam
  151. * When the system event is both :
  152. * - a ready event (subevtcode = SHCI_SUB_EVT_CODE_READY)
  153. * - reported by the FUS (sysevt_ready_rsp == FUS_FW_RUNNING)
  154. * The buffer shall not be released
  155. * ( eg ((tSHCI_UserEvtRxParam*)pPayload)->status shall be set to SHCI_TL_UserEventFlow_Disable )
  156. * When the status is not filled, the buffer is released by default
  157. */
  158. static void ble_glue_sys_user_event_callback( void * pPayload ) {
  159. UNUSED(pPayload);
  160. /* Traces channel initialization */
  161. // APPD_EnableCPU2( );
  162. TL_AsynchEvt_t *p_sys_event = (TL_AsynchEvt_t*)(((tSHCI_UserEvtRxParam*)pPayload)->pckt->evtserial.evt.payload);
  163. if(p_sys_event->subevtcode == SHCI_SUB_EVT_CODE_READY) {
  164. FURI_LOG_I(TAG, "Fus started");
  165. ble_glue->status = BleGlueStatusFusStarted;
  166. furi_hal_power_insomnia_exit();
  167. } else if(p_sys_event->subevtcode == SHCI_SUB_EVT_ERROR_NOTIF) {
  168. FURI_LOG_E(TAG, "Error during initialization");
  169. furi_hal_power_insomnia_exit();
  170. } else if(p_sys_event->subevtcode == SHCI_SUB_EVT_BLE_NVM_RAM_UPDATE) {
  171. SHCI_C2_BleNvmRamUpdate_Evt_t* p_sys_ble_nvm_ram_update_event = (SHCI_C2_BleNvmRamUpdate_Evt_t*)p_sys_event->payload;
  172. if(ble_glue->callback) {
  173. ble_glue->callback((uint8_t*)p_sys_ble_nvm_ram_update_event->StartAddress, p_sys_ble_nvm_ram_update_event->Size, ble_glue->context);
  174. }
  175. }
  176. }
  177. // Wrap functions
  178. static void ble_glue_user_event_thread(void *argument) {
  179. UNUSED(argument);
  180. for(;;) {
  181. osThreadFlagsWait(1, osFlagsWaitAny, osWaitForever);
  182. shci_user_evt_proc();
  183. }
  184. }
  185. void shci_notify_asynch_evt(void* pdata) {
  186. UNUSED(pdata);
  187. osThreadFlagsSet(ble_glue->shci_user_event_thread_id, 1);
  188. }
  189. void shci_cmd_resp_release(uint32_t flag) {
  190. UNUSED(flag);
  191. osSemaphoreRelease(ble_glue->shci_sem);
  192. }
  193. void shci_cmd_resp_wait(uint32_t timeout) {
  194. UNUSED(timeout);
  195. osSemaphoreAcquire(ble_glue->shci_sem, osWaitForever);
  196. }