app_entry.c 5.6 KB

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