ble_glue.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 POOL_SIZE (CFG_TLBLE_EVT_QUEUE_LENGTH*4U*DIVC(( sizeof(TL_PacketHeader_t) + TL_BLE_EVENT_FRAME_SIZE ), 4U))
  13. PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint8_t ble_glue_event_pool[POOL_SIZE];
  14. PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static TL_CmdPacket_t ble_glue_system_cmd_buff;
  15. 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];
  16. 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];
  17. typedef struct {
  18. osMutexId_t shci_mtx;
  19. osSemaphoreId_t shci_sem;
  20. osThreadId_t shci_user_event_thread_id;
  21. osThreadAttr_t shci_user_event_thread_attr;
  22. BleGlueStatus status;
  23. BleGlueKeyStorageChangedCallback callback;
  24. void* context;
  25. } BleGlue;
  26. static BleGlue* ble_glue = NULL;
  27. static void ble_glue_user_event_thread(void *argument);
  28. static void ble_glue_sys_status_not_callback(SHCI_TL_CmdStatus_t status);
  29. static void ble_glue_sys_user_event_callback(void* pPayload);
  30. BleGlueStatus ble_glue_get_status() {
  31. if(!ble_glue) {
  32. return BleGlueStatusUninitialized;
  33. }
  34. return ble_glue->status;
  35. }
  36. void ble_glue_set_key_storage_changed_callback(BleGlueKeyStorageChangedCallback callback, void* context) {
  37. furi_assert(ble_glue);
  38. furi_assert(callback);
  39. ble_glue->callback = callback;
  40. ble_glue->context = context;
  41. }
  42. void ble_glue_init() {
  43. ble_glue = furi_alloc(sizeof(BleGlue));
  44. ble_glue->status = BleGlueStatusStartup;
  45. ble_glue->shci_user_event_thread_attr.name = "ble_shci_evt";
  46. ble_glue->shci_user_event_thread_attr.stack_size = 1024;
  47. // Configure the system Power Mode
  48. // Select HSI as system clock source after Wake Up from Stop mode
  49. LL_RCC_SetClkAfterWakeFromStop(LL_RCC_STOP_WAKEUPCLOCK_HSI);
  50. /* Initialize the CPU2 reset value before starting CPU2 with C2BOOT */
  51. LL_C2_PWR_SetPowerMode(LL_PWR_MODE_SHUTDOWN);
  52. furi_hal_power_insomnia_enter();
  53. // APPD_Init();
  54. // Initialize all transport layers
  55. TL_MM_Config_t tl_mm_config;
  56. SHCI_TL_HciInitConf_t SHci_Tl_Init_Conf;
  57. // Reference table initialization
  58. TL_Init();
  59. ble_glue->shci_mtx = osMutexNew(NULL);
  60. ble_glue->shci_sem = osSemaphoreNew(1, 0, NULL);
  61. // FreeRTOS system task creation
  62. ble_glue->shci_user_event_thread_id = osThreadNew(ble_glue_user_event_thread, NULL, &ble_glue->shci_user_event_thread_attr);
  63. // System channel initialization
  64. SHci_Tl_Init_Conf.p_cmdbuffer = (uint8_t*)&ble_glue_system_cmd_buff;
  65. SHci_Tl_Init_Conf.StatusNotCallBack = ble_glue_sys_status_not_callback;
  66. shci_init(ble_glue_sys_user_event_callback, (void*) &SHci_Tl_Init_Conf);
  67. /**< Memory Manager channel initialization */
  68. tl_mm_config.p_BleSpareEvtBuffer = ble_glue_ble_spare_event_buff;
  69. tl_mm_config.p_SystemSpareEvtBuffer = ble_glue_system_spare_event_buff;
  70. tl_mm_config.p_AsynchEvtPool = ble_glue_event_pool;
  71. tl_mm_config.AsynchEvtPoolSize = POOL_SIZE;
  72. TL_MM_Init( &tl_mm_config );
  73. TL_Enable();
  74. /*
  75. * From now, the application is waiting for the ready event ( VS_HCI_C2_Ready )
  76. * received on the system channel before starting the Stack
  77. * This system event is received with ble_glue_sys_user_event_callback()
  78. */
  79. }
  80. static void ble_glue_sys_status_not_callback(SHCI_TL_CmdStatus_t status) {
  81. switch (status) {
  82. case SHCI_TL_CmdBusy:
  83. osMutexAcquire( ble_glue->shci_mtx, osWaitForever );
  84. break;
  85. case SHCI_TL_CmdAvailable:
  86. osMutexRelease( ble_glue->shci_mtx );
  87. break;
  88. default:
  89. break;
  90. }
  91. }
  92. /*
  93. * The type of the payload for a system user event is tSHCI_UserEvtRxParam
  94. * When the system event is both :
  95. * - a ready event (subevtcode = SHCI_SUB_EVT_CODE_READY)
  96. * - reported by the FUS (sysevt_ready_rsp == FUS_FW_RUNNING)
  97. * The buffer shall not be released
  98. * ( eg ((tSHCI_UserEvtRxParam*)pPayload)->status shall be set to SHCI_TL_UserEventFlow_Disable )
  99. * When the status is not filled, the buffer is released by default
  100. */
  101. static void ble_glue_sys_user_event_callback( void * pPayload ) {
  102. UNUSED(pPayload);
  103. /* Traces channel initialization */
  104. // APPD_EnableCPU2( );
  105. TL_AsynchEvt_t *p_sys_event = (TL_AsynchEvt_t*)(((tSHCI_UserEvtRxParam*)pPayload)->pckt->evtserial.evt.payload);
  106. if(p_sys_event->subevtcode == SHCI_SUB_EVT_CODE_READY) {
  107. if(ble_app_init()) {
  108. FURI_LOG_I("Core2", "BLE stack started");
  109. ble_glue->status = BleGlueStatusStarted;
  110. if(SHCI_C2_SetFlashActivityControl(FLASH_ACTIVITY_CONTROL_SEM7) == SHCI_Success) {
  111. FURI_LOG_I("Core2", "Flash activity control switched to SEM7");
  112. } else {
  113. FURI_LOG_E("Core2", "Failed to switch flash activity control to SEM7");
  114. }
  115. } else {
  116. FURI_LOG_E("Core2", "BLE stack startup failed");
  117. ble_glue->status = BleGlueStatusBleStackMissing;
  118. }
  119. furi_hal_power_insomnia_exit();
  120. } else if(p_sys_event->subevtcode == SHCI_SUB_EVT_ERROR_NOTIF) {
  121. FURI_LOG_E("Core2", "Error during initialization");
  122. furi_hal_power_insomnia_exit();
  123. } else if(p_sys_event->subevtcode == SHCI_SUB_EVT_BLE_NVM_RAM_UPDATE) {
  124. SHCI_C2_BleNvmRamUpdate_Evt_t* p_sys_ble_nvm_ram_update_event = (SHCI_C2_BleNvmRamUpdate_Evt_t*)p_sys_event->payload;
  125. if(ble_glue->callback) {
  126. ble_glue->callback((uint8_t*)p_sys_ble_nvm_ram_update_event->StartAddress, p_sys_ble_nvm_ram_update_event->Size, ble_glue->context);
  127. }
  128. }
  129. }
  130. // Wrap functions
  131. static void ble_glue_user_event_thread(void *argument) {
  132. UNUSED(argument);
  133. for(;;) {
  134. osThreadFlagsWait(1, osFlagsWaitAny, osWaitForever);
  135. shci_user_evt_proc();
  136. }
  137. }
  138. void shci_notify_asynch_evt(void* pdata) {
  139. UNUSED(pdata);
  140. osThreadFlagsSet(ble_glue->shci_user_event_thread_id, 1);
  141. }
  142. void shci_cmd_resp_release(uint32_t flag) {
  143. UNUSED(flag);
  144. osSemaphoreRelease(ble_glue->shci_sem);
  145. }
  146. void shci_cmd_resp_wait(uint32_t timeout) {
  147. UNUSED(timeout);
  148. osSemaphoreAcquire(ble_glue->shci_sem, osWaitForever);
  149. }