app_entry.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "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. HW_TS_Init(hw_ts_InitMode_Full, &hrtc); /**< Initialize the TimerServer */
  42. // APPD_Init();
  43. furi_hal_power_insomnia_enter();
  44. appe_Tl_Init(); /* Initialize all transport layers */
  45. /**
  46. * From now, the application is waiting for the ready event ( VS_HCI_C2_Ready )
  47. * received on the system channel before starting the Stack
  48. * This system event is received with APPE_SysUserEvtRx()
  49. */
  50. }
  51. /*************************************************************
  52. *
  53. * LOCAL FUNCTIONS
  54. *
  55. *************************************************************/
  56. /**
  57. * @brief Configure the system for power optimization
  58. *
  59. * @note This API configures the system to be ready for low power mode
  60. *
  61. * @param None
  62. * @retval None
  63. */
  64. static void SystemPower_Config(void) {
  65. // Select HSI as system clock source after Wake Up from Stop mode
  66. LL_RCC_SetClkAfterWakeFromStop(LL_RCC_STOP_WAKEUPCLOCK_HSI);
  67. /* Initialize the CPU2 reset value before starting CPU2 with C2BOOT */
  68. LL_C2_PWR_SetPowerMode(LL_PWR_MODE_SHUTDOWN);
  69. }
  70. static void appe_Tl_Init( void ) {
  71. TL_MM_Config_t tl_mm_config;
  72. SHCI_TL_HciInitConf_t SHci_Tl_Init_Conf;
  73. /**< Reference table initialization */
  74. TL_Init();
  75. MtxShciId = osMutexNew( NULL );
  76. SemShciId = osSemaphoreNew( 1, 0, NULL ); /*< Create the semaphore and make it busy at initialization */
  77. /** FreeRTOS system task creation */
  78. ShciUserEvtProcessId = osThreadNew(ShciUserEvtProcess, NULL, &ShciUserEvtProcess_attr);
  79. /**< System channel initialization */
  80. SHci_Tl_Init_Conf.p_cmdbuffer = (uint8_t*)&SystemCmdBuffer;
  81. SHci_Tl_Init_Conf.StatusNotCallBack = APPE_SysStatusNot;
  82. shci_init(APPE_SysUserEvtRx, (void*) &SHci_Tl_Init_Conf);
  83. /**< Memory Manager channel initialization */
  84. tl_mm_config.p_BleSpareEvtBuffer = BleSpareEvtBuffer;
  85. tl_mm_config.p_SystemSpareEvtBuffer = SystemSpareEvtBuffer;
  86. tl_mm_config.p_AsynchEvtPool = EvtPool;
  87. tl_mm_config.AsynchEvtPoolSize = POOL_SIZE;
  88. TL_MM_Init( &tl_mm_config );
  89. TL_Enable();
  90. }
  91. static void APPE_SysStatusNot( SHCI_TL_CmdStatus_t status ) {
  92. switch (status) {
  93. case SHCI_TL_CmdBusy:
  94. osMutexAcquire( MtxShciId, osWaitForever );
  95. break;
  96. case SHCI_TL_CmdAvailable:
  97. osMutexRelease( MtxShciId );
  98. break;
  99. default:
  100. break;
  101. }
  102. }
  103. /**
  104. * The type of the payload for a system user event is tSHCI_UserEvtRxParam
  105. * When the system event is both :
  106. * - a ready event (subevtcode = SHCI_SUB_EVT_CODE_READY)
  107. * - reported by the FUS (sysevt_ready_rsp == FUS_FW_RUNNING)
  108. * The buffer shall not be released
  109. * ( eg ((tSHCI_UserEvtRxParam*)pPayload)->status shall be set to SHCI_TL_UserEventFlow_Disable )
  110. * When the status is not filled, the buffer is released by default
  111. */
  112. static void APPE_SysUserEvtRx( void * pPayload ) {
  113. UNUSED(pPayload);
  114. /* Traces channel initialization */
  115. // APPD_EnableCPU2( );
  116. if (APP_BLE_Init()) {
  117. ble_glue_status = BleGlueStatusStarted;
  118. } else {
  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. }