app_entry.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include "app_common.h"
  2. #include "main.h"
  3. #include "app_entry.h"
  4. #include "ble_app.h"
  5. #include "ble.h"
  6. #include "tl.h"
  7. #include "cmsis_os.h"
  8. #include "shci_tl.h"
  9. #include "app_debug.h"
  10. #include <furi-hal.h>
  11. extern RTC_HandleTypeDef hrtc;
  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 EvtPool[POOL_SIZE];
  14. PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static TL_CmdPacket_t SystemCmdBuffer;
  15. PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint8_t SystemSpareEvtBuffer[sizeof(TL_PacketHeader_t) + TL_EVT_HDR_SIZE + 255U];
  16. PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint8_t BleSpareEvtBuffer[sizeof(TL_PacketHeader_t) + TL_EVT_HDR_SIZE + 255];
  17. osMutexId_t MtxShciId;
  18. osSemaphoreId_t SemShciId;
  19. osThreadId_t ShciUserEvtProcessId;
  20. volatile static BleGlueStatus ble_glue_status = BleGlueStatusUninitialized;
  21. const osThreadAttr_t ShciUserEvtProcess_attr = {
  22. .name = CFG_SHCI_USER_EVT_PROCESS_NAME,
  23. .attr_bits = CFG_SHCI_USER_EVT_PROCESS_ATTR_BITS,
  24. .cb_mem = CFG_SHCI_USER_EVT_PROCESS_CB_MEM,
  25. .cb_size = CFG_SHCI_USER_EVT_PROCESS_CB_SIZE,
  26. .stack_mem = CFG_SHCI_USER_EVT_PROCESS_STACK_MEM,
  27. .priority = CFG_SHCI_USER_EVT_PROCESS_PRIORITY,
  28. .stack_size = CFG_SHCI_USER_EVT_PROCESS_STACK_SIZE
  29. };
  30. static void ShciUserEvtProcess(void *argument);
  31. static void SystemPower_Config( void );
  32. static void appe_Tl_Init( void );
  33. static void APPE_SysStatusNot( SHCI_TL_CmdStatus_t status );
  34. static void APPE_SysUserEvtRx( void * pPayload );
  35. BleGlueStatus APPE_Status() {
  36. return ble_glue_status;
  37. }
  38. void APPE_Init() {
  39. ble_glue_status = BleGlueStatusStartup;
  40. SystemPower_Config(); /**< Configure the system Power Mode */
  41. // APPD_Init();
  42. furi_hal_power_insomnia_enter();
  43. appe_Tl_Init(); /* Initialize all transport layers */
  44. /**
  45. * From now, the application is waiting for the ready event ( VS_HCI_C2_Ready )
  46. * received on the system channel before starting the Stack
  47. * This system event is received with APPE_SysUserEvtRx()
  48. */
  49. }
  50. /*************************************************************
  51. *
  52. * LOCAL FUNCTIONS
  53. *
  54. *************************************************************/
  55. /**
  56. * @brief Configure the system for power optimization
  57. *
  58. * @note This API configures the system to be ready for low power mode
  59. *
  60. * @param None
  61. * @retval None
  62. */
  63. static void SystemPower_Config(void) {
  64. // Select HSI as system clock source after Wake Up from Stop mode
  65. LL_RCC_SetClkAfterWakeFromStop(LL_RCC_STOP_WAKEUPCLOCK_HSI);
  66. /* Initialize the CPU2 reset value before starting CPU2 with C2BOOT */
  67. LL_C2_PWR_SetPowerMode(LL_PWR_MODE_SHUTDOWN);
  68. }
  69. static void appe_Tl_Init( void ) {
  70. TL_MM_Config_t tl_mm_config;
  71. SHCI_TL_HciInitConf_t SHci_Tl_Init_Conf;
  72. /**< Reference table initialization */
  73. TL_Init();
  74. MtxShciId = osMutexNew( NULL );
  75. SemShciId = osSemaphoreNew( 1, 0, NULL ); /*< Create the semaphore and make it busy at initialization */
  76. /** FreeRTOS system task creation */
  77. ShciUserEvtProcessId = osThreadNew(ShciUserEvtProcess, NULL, &ShciUserEvtProcess_attr);
  78. /**< System channel initialization */
  79. SHci_Tl_Init_Conf.p_cmdbuffer = (uint8_t*)&SystemCmdBuffer;
  80. SHci_Tl_Init_Conf.StatusNotCallBack = APPE_SysStatusNot;
  81. shci_init(APPE_SysUserEvtRx, (void*) &SHci_Tl_Init_Conf);
  82. /**< Memory Manager channel initialization */
  83. tl_mm_config.p_BleSpareEvtBuffer = BleSpareEvtBuffer;
  84. tl_mm_config.p_SystemSpareEvtBuffer = SystemSpareEvtBuffer;
  85. tl_mm_config.p_AsynchEvtPool = EvtPool;
  86. tl_mm_config.AsynchEvtPoolSize = POOL_SIZE;
  87. TL_MM_Init( &tl_mm_config );
  88. TL_Enable();
  89. }
  90. static void APPE_SysStatusNot( SHCI_TL_CmdStatus_t status ) {
  91. switch (status) {
  92. case SHCI_TL_CmdBusy:
  93. osMutexAcquire( MtxShciId, osWaitForever );
  94. break;
  95. case SHCI_TL_CmdAvailable:
  96. osMutexRelease( MtxShciId );
  97. break;
  98. default:
  99. break;
  100. }
  101. }
  102. /**
  103. * The type of the payload for a system user event is tSHCI_UserEvtRxParam
  104. * When the system event is both :
  105. * - a ready event (subevtcode = SHCI_SUB_EVT_CODE_READY)
  106. * - reported by the FUS (sysevt_ready_rsp == FUS_FW_RUNNING)
  107. * The buffer shall not be released
  108. * ( eg ((tSHCI_UserEvtRxParam*)pPayload)->status shall be set to SHCI_TL_UserEventFlow_Disable )
  109. * When the status is not filled, the buffer is released by default
  110. */
  111. static void APPE_SysUserEvtRx( void * pPayload ) {
  112. UNUSED(pPayload);
  113. /* Traces channel initialization */
  114. // APPD_EnableCPU2( );
  115. if(ble_app_init()) {
  116. FURI_LOG_I("Core2", "BLE stack started");
  117. ble_glue_status = BleGlueStatusStarted;
  118. } else {
  119. FURI_LOG_E("Core2", "BLE stack startup failed");
  120. ble_glue_status = BleGlueStatusBroken;
  121. }
  122. furi_hal_power_insomnia_exit();
  123. }
  124. /*************************************************************
  125. *
  126. * FREERTOS WRAPPER FUNCTIONS
  127. *
  128. *************************************************************/
  129. static void ShciUserEvtProcess(void *argument) {
  130. UNUSED(argument);
  131. for(;;) {
  132. osThreadFlagsWait(1, osFlagsWaitAny, osWaitForever);
  133. shci_user_evt_proc();
  134. }
  135. }
  136. /*************************************************************
  137. *
  138. * WRAP FUNCTIONS
  139. *
  140. *************************************************************/
  141. void shci_notify_asynch_evt(void* pdata) {
  142. UNUSED(pdata);
  143. osThreadFlagsSet( ShciUserEvtProcessId, 1 );
  144. }
  145. void shci_cmd_resp_release(uint32_t flag) {
  146. UNUSED(flag);
  147. osSemaphoreRelease( SemShciId );
  148. }
  149. void shci_cmd_resp_wait(uint32_t timeout) {
  150. UNUSED(timeout);
  151. osSemaphoreAcquire( SemShciId, osWaitForever );
  152. }