app_entry.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "stm32_lpm.h"
  10. #include "app_debug.h"
  11. #include <api-hal.h>
  12. extern RTC_HandleTypeDef hrtc;
  13. #define POOL_SIZE (CFG_TLBLE_EVT_QUEUE_LENGTH*4U*DIVC(( sizeof(TL_PacketHeader_t) + TL_BLE_EVENT_FRAME_SIZE ), 4U))
  14. PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint8_t EvtPool[POOL_SIZE];
  15. PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static TL_CmdPacket_t SystemCmdBuffer;
  16. PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint8_t SystemSpareEvtBuffer[sizeof(TL_PacketHeader_t) + TL_EVT_HDR_SIZE + 255U];
  17. PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint8_t BleSpareEvtBuffer[sizeof(TL_PacketHeader_t) + TL_EVT_HDR_SIZE + 255];
  18. osMutexId_t MtxShciId;
  19. osSemaphoreId_t SemShciId;
  20. osThreadId_t ShciUserEvtProcessId;
  21. volatile static BleGlueStatus ble_glue_status = BleGlueStatusUninitialized;
  22. const osThreadAttr_t ShciUserEvtProcess_attr = {
  23. .name = CFG_SHCI_USER_EVT_PROCESS_NAME,
  24. .attr_bits = CFG_SHCI_USER_EVT_PROCESS_ATTR_BITS,
  25. .cb_mem = CFG_SHCI_USER_EVT_PROCESS_CB_MEM,
  26. .cb_size = CFG_SHCI_USER_EVT_PROCESS_CB_SIZE,
  27. .stack_mem = CFG_SHCI_USER_EVT_PROCESS_STACK_MEM,
  28. .priority = CFG_SHCI_USER_EVT_PROCESS_PRIORITY,
  29. .stack_size = CFG_SHCI_USER_EVT_PROCESS_STACK_SIZE
  30. };
  31. static void ShciUserEvtProcess(void *argument);
  32. static void SystemPower_Config( void );
  33. static void appe_Tl_Init( void );
  34. static void APPE_SysStatusNot( SHCI_TL_CmdStatus_t status );
  35. static void APPE_SysUserEvtRx( void * pPayload );
  36. BleGlueStatus APPE_Status() {
  37. return ble_glue_status;
  38. }
  39. void APPE_Init() {
  40. ble_glue_status = BleGlueStatusStartup;
  41. SystemPower_Config(); /**< Configure the system Power Mode */
  42. HW_TS_Init(hw_ts_InitMode_Full, &hrtc); /**< Initialize the TimerServer */
  43. // APPD_Init();
  44. api_hal_power_insomnia_enter();
  45. appe_Tl_Init(); /* Initialize all transport layers */
  46. /**
  47. * From now, the application is waiting for the ready event ( VS_HCI_C2_Ready )
  48. * received on the system channel before starting the Stack
  49. * This system event is received with APPE_SysUserEvtRx()
  50. */
  51. }
  52. /*************************************************************
  53. *
  54. * LOCAL FUNCTIONS
  55. *
  56. *************************************************************/
  57. /**
  58. * @brief Configure the system for power optimization
  59. *
  60. * @note This API configures the system to be ready for low power mode
  61. *
  62. * @param None
  63. * @retval None
  64. */
  65. static void SystemPower_Config(void) {
  66. // Select HSI as system clock source after Wake Up from Stop mode
  67. LL_RCC_SetClkAfterWakeFromStop(LL_RCC_STOP_WAKEUPCLOCK_HSI);
  68. /* Initialize the CPU2 reset value before starting CPU2 with C2BOOT */
  69. LL_C2_PWR_SetPowerMode(LL_PWR_MODE_SHUTDOWN);
  70. }
  71. static void appe_Tl_Init( void ) {
  72. TL_MM_Config_t tl_mm_config;
  73. SHCI_TL_HciInitConf_t SHci_Tl_Init_Conf;
  74. /**< Reference table initialization */
  75. TL_Init();
  76. MtxShciId = osMutexNew( NULL );
  77. SemShciId = osSemaphoreNew( 1, 0, NULL ); /*< Create the semaphore and make it busy at initialization */
  78. /** FreeRTOS system task creation */
  79. ShciUserEvtProcessId = osThreadNew(ShciUserEvtProcess, NULL, &ShciUserEvtProcess_attr);
  80. /**< System channel initialization */
  81. SHci_Tl_Init_Conf.p_cmdbuffer = (uint8_t*)&SystemCmdBuffer;
  82. SHci_Tl_Init_Conf.StatusNotCallBack = APPE_SysStatusNot;
  83. shci_init(APPE_SysUserEvtRx, (void*) &SHci_Tl_Init_Conf);
  84. /**< Memory Manager channel initialization */
  85. tl_mm_config.p_BleSpareEvtBuffer = BleSpareEvtBuffer;
  86. tl_mm_config.p_SystemSpareEvtBuffer = SystemSpareEvtBuffer;
  87. tl_mm_config.p_AsynchEvtPool = EvtPool;
  88. tl_mm_config.AsynchEvtPoolSize = POOL_SIZE;
  89. TL_MM_Init( &tl_mm_config );
  90. TL_Enable();
  91. }
  92. static void APPE_SysStatusNot( SHCI_TL_CmdStatus_t status ) {
  93. switch (status) {
  94. case SHCI_TL_CmdBusy:
  95. osMutexAcquire( MtxShciId, osWaitForever );
  96. break;
  97. case SHCI_TL_CmdAvailable:
  98. osMutexRelease( MtxShciId );
  99. break;
  100. default:
  101. break;
  102. }
  103. }
  104. /**
  105. * The type of the payload for a system user event is tSHCI_UserEvtRxParam
  106. * When the system event is both :
  107. * - a ready event (subevtcode = SHCI_SUB_EVT_CODE_READY)
  108. * - reported by the FUS (sysevt_ready_rsp == FUS_FW_RUNNING)
  109. * The buffer shall not be released
  110. * ( eg ((tSHCI_UserEvtRxParam*)pPayload)->status shall be set to SHCI_TL_UserEventFlow_Disable )
  111. * When the status is not filled, the buffer is released by default
  112. */
  113. static void APPE_SysUserEvtRx( void * pPayload ) {
  114. UNUSED(pPayload);
  115. /* Traces channel initialization */
  116. // APPD_EnableCPU2( );
  117. if (APP_BLE_Init()) {
  118. ble_glue_status = BleGlueStatusStarted;
  119. } else {
  120. ble_glue_status = BleGlueStatusBroken;
  121. }
  122. api_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. }