app_ble.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "main.h"
  2. #include "app_entry.h"
  3. #include "app_common.h"
  4. #include "dbg_trace.h"
  5. #include "ble.h"
  6. #include "tl.h"
  7. #include "app_ble.h"
  8. #include "shci.h"
  9. #include "cmsis_os.h"
  10. #include <furi-hal.h>
  11. PLACE_IN_SECTION("MB_MEM1") ALIGN(4) static TL_CmdPacket_t BleCmdBuffer;
  12. // PLACE_IN_SECTION("TAG_OTA_END") const uint32_t MagicKeywordValue = 0x94448A29 ;
  13. // PLACE_IN_SECTION("TAG_OTA_START") const uint32_t MagicKeywordAddress = (uint32_t)&MagicKeywordValue;
  14. osMutexId_t MtxHciId;
  15. osSemaphoreId_t SemHciId;
  16. osThreadId_t HciUserEvtProcessId;
  17. const osThreadAttr_t HciUserEvtProcess_attr = {
  18. .name = CFG_HCI_USER_EVT_PROCESS_NAME,
  19. .attr_bits = CFG_HCI_USER_EVT_PROCESS_ATTR_BITS,
  20. .cb_mem = CFG_HCI_USER_EVT_PROCESS_CB_MEM,
  21. .cb_size = CFG_HCI_USER_EVT_PROCESS_CB_SIZE,
  22. .stack_mem = CFG_HCI_USER_EVT_PROCESS_STACK_MEM,
  23. .priority = CFG_HCI_USER_EVT_PROCESS_PRIORITY,
  24. .stack_size = CFG_HCI_USER_EVT_PROCESS_STACK_SIZE
  25. };
  26. /* Private function prototypes -----------------------------------------------*/
  27. static void HciUserEvtProcess(void *argument);
  28. static void BLE_UserEvtRx( void * pPayload );
  29. static void BLE_StatusNot( HCI_TL_CmdStatus_t status );
  30. static void Ble_Tl_Init( void );
  31. bool APP_BLE_Init() {
  32. SHCI_C2_Ble_Init_Cmd_Packet_t ble_init_cmd_packet = {
  33. {{0,0,0}}, /**< Header unused */
  34. {0, /** pBleBufferAddress not used */
  35. 0, /** BleBufferSize not used */
  36. CFG_BLE_NUM_GATT_ATTRIBUTES,
  37. CFG_BLE_NUM_GATT_SERVICES,
  38. CFG_BLE_ATT_VALUE_ARRAY_SIZE,
  39. CFG_BLE_NUM_LINK,
  40. CFG_BLE_DATA_LENGTH_EXTENSION,
  41. CFG_BLE_PREPARE_WRITE_LIST_SIZE,
  42. CFG_BLE_MBLOCK_COUNT,
  43. CFG_BLE_MAX_ATT_MTU,
  44. CFG_BLE_SLAVE_SCA,
  45. CFG_BLE_MASTER_SCA,
  46. CFG_BLE_LSE_SOURCE,
  47. CFG_BLE_MAX_CONN_EVENT_LENGTH,
  48. CFG_BLE_HSE_STARTUP_TIME,
  49. CFG_BLE_VITERBI_MODE,
  50. CFG_BLE_LL_ONLY,
  51. 0}
  52. };
  53. // Initialize Ble Transport Layer
  54. Ble_Tl_Init( );
  55. // Register the hci transport layer to handle BLE User Asynchronous Events
  56. HciUserEvtProcessId = osThreadNew(HciUserEvtProcess, NULL, &HciUserEvtProcess_attr);
  57. // Starts the BLE Stack on CPU2
  58. return (SHCI_C2_BLE_Init( &ble_init_cmd_packet ) == SHCI_Success);
  59. }
  60. static void Ble_Tl_Init( void ) {
  61. HCI_TL_HciInitConf_t Hci_Tl_Init_Conf;
  62. MtxHciId = osMutexNew( NULL );
  63. SemHciId = osSemaphoreNew( 1, 0, NULL ); /*< Create the semaphore and make it busy at initialization */
  64. Hci_Tl_Init_Conf.p_cmdbuffer = (uint8_t*)&BleCmdBuffer;
  65. Hci_Tl_Init_Conf.StatusNotCallBack = BLE_StatusNot;
  66. hci_init(BLE_UserEvtRx, (void*) &Hci_Tl_Init_Conf);
  67. }
  68. static void HciUserEvtProcess(void *argument) {
  69. UNUSED(argument);
  70. for(;;)
  71. {
  72. osThreadFlagsWait( 1, osFlagsWaitAny, osWaitForever);
  73. hci_user_evt_proc( );
  74. }
  75. }
  76. /*************************************************************
  77. *
  78. * WRAP FUNCTIONS
  79. *
  80. *************************************************************/
  81. void hci_notify_asynch_evt(void* pdata) {
  82. UNUSED(pdata);
  83. osThreadFlagsSet( HciUserEvtProcessId, 1 );
  84. }
  85. void hci_cmd_resp_release(uint32_t flag) {
  86. UNUSED(flag);
  87. osSemaphoreRelease( SemHciId );
  88. }
  89. void hci_cmd_resp_wait(uint32_t timeout) {
  90. UNUSED(timeout);
  91. osSemaphoreAcquire( SemHciId, osWaitForever );
  92. }
  93. static void BLE_UserEvtRx( void * pPayload ) {
  94. SVCCTL_UserEvtFlowStatus_t svctl_return_status;
  95. tHCI_UserEvtRxParam *pParam;
  96. pParam = (tHCI_UserEvtRxParam *)pPayload;
  97. svctl_return_status = SVCCTL_UserEvtRx((void *)&(pParam->pckt->evtserial));
  98. if (svctl_return_status != SVCCTL_UserEvtFlowDisable) {
  99. pParam->status = HCI_TL_UserEventFlow_Enable;
  100. } else {
  101. pParam->status = HCI_TL_UserEventFlow_Disable;
  102. }
  103. }
  104. static void BLE_StatusNot( HCI_TL_CmdStatus_t status ) {
  105. switch (status) {
  106. case HCI_TL_CmdBusy:
  107. osMutexAcquire( MtxHciId, osWaitForever );
  108. break;
  109. case HCI_TL_CmdAvailable:
  110. osMutexRelease( MtxHciId );
  111. break;
  112. default:
  113. break;
  114. }
  115. }
  116. void SVCCTL_ResumeUserEventFlow( void ) {
  117. hci_resume_flow();
  118. }