ble_glue.c 6.3 KB

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