app_entry.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include "app_common.h"
  2. #include "main.h"
  3. #include "app_entry.h"
  4. #include "app_ble.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. HW_TS_Init(hw_ts_InitMode_Full, &hrtc); /**< Initialize the TimerServer */
  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 (APP_BLE_Init()) {
  116. ble_glue_status = BleGlueStatusStarted;
  117. } else {
  118. ble_glue_status = BleGlueStatusBroken;
  119. }
  120. furi_hal_power_insomnia_exit();
  121. }
  122. /*************************************************************
  123. *
  124. * FREERTOS WRAPPER FUNCTIONS
  125. *
  126. *************************************************************/
  127. static void ShciUserEvtProcess(void *argument) {
  128. UNUSED(argument);
  129. for(;;) {
  130. osThreadFlagsWait(1, osFlagsWaitAny, osWaitForever);
  131. shci_user_evt_proc();
  132. }
  133. }
  134. /*************************************************************
  135. *
  136. * WRAP FUNCTIONS
  137. *
  138. *************************************************************/
  139. void shci_notify_asynch_evt(void* pdata) {
  140. UNUSED(pdata);
  141. osThreadFlagsSet( ShciUserEvtProcessId, 1 );
  142. }
  143. void shci_cmd_resp_release(uint32_t flag) {
  144. UNUSED(flag);
  145. osSemaphoreRelease( SemShciId );
  146. }
  147. void shci_cmd_resp_wait(uint32_t timeout) {
  148. UNUSED(timeout);
  149. osSemaphoreAcquire( SemShciId, osWaitForever );
  150. }
  151. #if(CFG_DEBUG_TRACE != 0)
  152. void DbgOutputInit( void )
  153. {
  154. }
  155. void DbgOutputTraces( uint8_t *p_data, uint16_t size, void (*cb)(void) )
  156. {
  157. furi_hal_console_tx(p_data, size);
  158. cb();
  159. }
  160. #endif