Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'Projects/STM32WB5MM-DK/Applications/BLE/BLE_p2pServer/Core/Src/app_entry.c')
-rw-r--r--Projects/STM32WB5MM-DK/Applications/BLE/BLE_p2pServer/Core/Src/app_entry.c532
1 files changed, 532 insertions, 0 deletions
diff --git a/Projects/STM32WB5MM-DK/Applications/BLE/BLE_p2pServer/Core/Src/app_entry.c b/Projects/STM32WB5MM-DK/Applications/BLE/BLE_p2pServer/Core/Src/app_entry.c
new file mode 100644
index 000000000..0d34dee6a
--- /dev/null
+++ b/Projects/STM32WB5MM-DK/Applications/BLE/BLE_p2pServer/Core/Src/app_entry.c
@@ -0,0 +1,532 @@
+/* USER CODE BEGIN Header */
+/**
+ ******************************************************************************
+ * File Name : app_entry.c
+ * Description : Entry application source file for STM32WPAN Middleware
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
+ * All rights reserved.</center></h2>
+ *
+ * This software component is licensed by ST under Ultimate Liberty license
+ * SLA0044, the "License"; You may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at:
+ * www.st.com/SLA0044
+ *
+ ******************************************************************************
+ */
+/* USER CODE END Header */
+
+/* Includes ------------------------------------------------------------------*/
+#include "app_common.h"
+#include "main.h"
+#include "app_entry.h"
+#include "app_ble.h"
+#include "ble.h"
+#include "tl.h"
+#include "stm32_seq.h"
+#include "shci_tl.h"
+#include "stm32_lpm.h"
+#include "app_debug.h"
+#include "otp.h"
+
+/* Private includes -----------------------------------------------------------*/
+/* USER CODE BEGIN Includes */
+
+/* USER CODE END Includes */
+
+/* Private typedef -----------------------------------------------------------*/
+extern RTC_HandleTypeDef hrtc;
+/* USER CODE BEGIN PTD */
+
+/* USER CODE END PTD */
+
+/* Private defines -----------------------------------------------------------*/
+#define POOL_SIZE (CFG_TLBLE_EVT_QUEUE_LENGTH*4U*DIVC(( sizeof(TL_PacketHeader_t) + TL_BLE_EVENT_FRAME_SIZE ), 4U))
+
+/* USER CODE BEGIN PD */
+
+/* USER CODE END PD */
+
+/* Private macros ------------------------------------------------------------*/
+/* USER CODE BEGIN PM */
+
+/* USER CODE END PM */
+
+/* Private variables ---------------------------------------------------------*/
+PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint8_t EvtPool[POOL_SIZE];
+PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static TL_CmdPacket_t SystemCmdBuffer;
+PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint8_t SystemSpareEvtBuffer[sizeof(TL_PacketHeader_t) + TL_EVT_HDR_SIZE + 255U];
+PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint8_t BleSpareEvtBuffer[sizeof(TL_PacketHeader_t) + TL_EVT_HDR_SIZE + 255];
+
+/* USER CODE BEGIN PV */
+
+/* USER CODE END PV */
+
+/* Private functions prototypes-----------------------------------------------*/
+static void Config_HSE(void);
+static void Reset_Device( void );
+static void Reset_IPCC( void );
+static void Reset_BackupDomain( void );
+static void System_Init( void );
+static void SystemPower_Config( void );
+static void appe_Tl_Init( void );
+static void APPE_SysStatusNot( SHCI_TL_CmdStatus_t status );
+static void APPE_SysUserEvtRx( void * pPayload );
+static void Init_Exti( void );
+static void Init_Rtc( void );
+static void Init_Smps( void );
+
+/* USER CODE BEGIN PFP */
+static void Button_Init( void );
+/* USER CODE END PFP */
+
+/* Functions Definition ------------------------------------------------------*/
+void MX_APPE_Config( void )
+{
+ /**
+ * The OPTVERR flag is wrongly set at power on
+ * It shall be cleared before using any HAL_FLASH_xxx() api
+ */
+ __HAL_FLASH_CLEAR_FLAG( FLASH_FLAG_OPTVERR );
+
+ /**
+ * Reset some configurations so that the system behave in the same way
+ * when either out of nReset or Power On
+ */
+ Reset_Device( );
+
+ /* Configure HSE Tuning */
+ Config_HSE();
+
+ return;
+}
+
+void MX_APPE_Init( void )
+{
+ System_Init( ); /**< System initialization */
+
+ SystemPower_Config(); /**< Configure the system Power Mode */
+
+ HW_TS_Init(hw_ts_InitMode_Full, &hrtc); /**< Initialize the TimerServer */
+
+/* USER CODE BEGIN APPE_Init_1 */
+ APPD_Init();
+
+ /**
+ * The Standby mode should not be entered before the initialization is over
+ * The default state of the Low Power Manager is to allow the Standby Mode so an request is needed here
+ */
+ UTIL_LPM_SetOffMode(1 << CFG_LPM_APP, UTIL_LPM_DISABLE);
+
+ BSP_LCD_Init(0, LCD_ORIENTATION_LANDSCAPE);
+ /* Set LCD Foreground Layer */
+ UTIL_LCD_SetFuncDriver(&LCD_Driver); /* SetFunc before setting device */
+ UTIL_LCD_SetDevice(0); /* SetDevice after funcDriver is set */
+ BSP_LCD_Clear(0,SSD1315_COLOR_BLACK);
+ BSP_LCD_DisplayOn(0);
+ BSP_LCD_Refresh(0);
+ UTIL_LCD_SetFont(&Font12);
+ /* Set the LCD Text Color */
+ UTIL_LCD_SetTextColor(SSD1315_COLOR_WHITE);
+ UTIL_LCD_SetBackColor(SSD1315_COLOR_BLACK);
+ BSP_LCD_Clear(0,SSD1315_COLOR_BLACK);
+ BSP_LCD_Refresh(0);
+ UTIL_LCD_DisplayStringAt(0, 0, (uint8_t *)"ST BLE P2P Server", CENTER_MODE);
+ BSP_LCD_Refresh(0);
+
+ //Switch RGB LED off
+ LED_Off();
+ //Initialize user buttons
+ Button_Init();
+
+
+/* USER CODE END APPE_Init_1 */
+ appe_Tl_Init(); /* Initialize all transport layers */
+
+ /**
+ * From now, the application is waiting for the ready event ( VS_HCI_C2_Ready )
+ * received on the system channel before starting the Stack
+ * This system event is received with APPE_SysUserEvtRx()
+ */
+/* USER CODE BEGIN APPE_Init_2 */
+
+/* USER CODE END APPE_Init_2 */
+ return;
+}
+/* USER CODE BEGIN FD */
+
+void LED_Deinit(void)
+{
+ GPIO_InitTypeDef GPIO_InitStructure = {0};
+
+ /* RGB Led de-init */
+ BSP_PWM_LED_DeInit();
+
+ /* configure SPIx MOSI for LCD */
+ GPIO_InitStructure.Pin = BUS_SPI1_MOSI_PIN;
+ GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
+ GPIO_InitStructure.Pull = GPIO_PULLDOWN;
+ GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW;
+ GPIO_InitStructure.Alternate = BUS_SPI1_AF;
+ HAL_GPIO_Init(BUS_SPI1_GPIO_PORTA, &GPIO_InitStructure);
+}
+
+
+void LED_On(aPwmLedGsData_TypeDef aPwmLedGsData)
+{
+ BSP_PWM_LED_Init();
+ BSP_PWM_LED_On(aPwmLedGsData);
+ LED_Deinit();
+}
+
+void LED_Off(void)
+{
+ BSP_PWM_LED_Init();
+ BSP_PWM_LED_Off();
+ LED_Deinit();
+}
+/* USER CODE END FD */
+
+/*************************************************************
+ *
+ * LOCAL FUNCTIONS
+ *
+ *************************************************************/
+static void Reset_Device( void )
+{
+#if ( CFG_HW_RESET_BY_FW == 1 )
+ Reset_BackupDomain();
+
+ Reset_IPCC();
+#endif
+
+ return;
+}
+
+static void Reset_BackupDomain( void )
+{
+ if ((LL_RCC_IsActiveFlag_PINRST() != FALSE) && (LL_RCC_IsActiveFlag_SFTRST() == FALSE))
+ {
+ HAL_PWR_EnableBkUpAccess(); /**< Enable access to the RTC registers */
+
+ /**
+ * Write twice the value to flush the APB-AHB bridge
+ * This bit shall be written in the register before writing the next one
+ */
+ HAL_PWR_EnableBkUpAccess();
+
+ __HAL_RCC_BACKUPRESET_FORCE();
+ __HAL_RCC_BACKUPRESET_RELEASE();
+ }
+
+ return;
+}
+
+static void Reset_IPCC( void )
+{
+ LL_AHB3_GRP1_EnableClock(LL_AHB3_GRP1_PERIPH_IPCC);
+
+ LL_C1_IPCC_ClearFlag_CHx(
+ IPCC,
+ LL_IPCC_CHANNEL_1 | LL_IPCC_CHANNEL_2 | LL_IPCC_CHANNEL_3 | LL_IPCC_CHANNEL_4
+ | LL_IPCC_CHANNEL_5 | LL_IPCC_CHANNEL_6);
+
+ LL_C2_IPCC_ClearFlag_CHx(
+ IPCC,
+ LL_IPCC_CHANNEL_1 | LL_IPCC_CHANNEL_2 | LL_IPCC_CHANNEL_3 | LL_IPCC_CHANNEL_4
+ | LL_IPCC_CHANNEL_5 | LL_IPCC_CHANNEL_6);
+
+ LL_C1_IPCC_DisableTransmitChannel(
+ IPCC,
+ LL_IPCC_CHANNEL_1 | LL_IPCC_CHANNEL_2 | LL_IPCC_CHANNEL_3 | LL_IPCC_CHANNEL_4
+ | LL_IPCC_CHANNEL_5 | LL_IPCC_CHANNEL_6);
+
+ LL_C2_IPCC_DisableTransmitChannel(
+ IPCC,
+ LL_IPCC_CHANNEL_1 | LL_IPCC_CHANNEL_2 | LL_IPCC_CHANNEL_3 | LL_IPCC_CHANNEL_4
+ | LL_IPCC_CHANNEL_5 | LL_IPCC_CHANNEL_6);
+
+ LL_C1_IPCC_DisableReceiveChannel(
+ IPCC,
+ LL_IPCC_CHANNEL_1 | LL_IPCC_CHANNEL_2 | LL_IPCC_CHANNEL_3 | LL_IPCC_CHANNEL_4
+ | LL_IPCC_CHANNEL_5 | LL_IPCC_CHANNEL_6);
+
+ LL_C2_IPCC_DisableReceiveChannel(
+ IPCC,
+ LL_IPCC_CHANNEL_1 | LL_IPCC_CHANNEL_2 | LL_IPCC_CHANNEL_3 | LL_IPCC_CHANNEL_4
+ | LL_IPCC_CHANNEL_5 | LL_IPCC_CHANNEL_6);
+
+ return;
+}
+
+static void Config_HSE(void)
+{
+ OTP_ID0_t * p_otp;
+
+ /**
+ * Read HSE_Tuning from OTP
+ */
+ p_otp = (OTP_ID0_t *) OTP_Read(0);
+ if (p_otp)
+ {
+ LL_RCC_HSE_SetCapacitorTuning(p_otp->hse_tuning);
+ }
+
+ return;
+}
+
+static void System_Init( void )
+{
+ Init_Smps( );
+
+ Init_Exti( );
+
+ Init_Rtc( );
+
+ return;
+}
+
+static void Init_Smps( void )
+{
+#if (CFG_USE_SMPS != 0)
+ /**
+ * Configure and enable SMPS
+ *
+ * The SMPS configuration is not yet supported by CubeMx
+ * when SMPS output voltage is set to 1.4V, the RF output power is limited to 3.7dBm
+ * the SMPS output voltage shall be increased for higher RF output power
+ */
+ LL_PWR_SMPS_SetStartupCurrent(LL_PWR_SMPS_STARTUP_CURRENT_80MA);
+ LL_PWR_SMPS_SetOutputVoltageLevel(LL_PWR_SMPS_OUTPUT_VOLTAGE_1V40);
+ LL_PWR_SMPS_Enable();
+#endif
+
+ return;
+}
+
+static void Init_Exti( void )
+{
+ /**< Disable all wakeup interrupt on CPU1 except IPCC(36), HSEM(38) */
+ LL_EXTI_DisableIT_0_31(~0);
+ LL_EXTI_DisableIT_32_63( (~0) & (~(LL_EXTI_LINE_36 | LL_EXTI_LINE_38)) );
+
+ return;
+}
+
+static void Init_Rtc( void )
+{
+ /* Disable RTC registers write protection */
+ LL_RTC_DisableWriteProtection(RTC);
+
+ LL_RTC_WAKEUP_SetClock(RTC, CFG_RTC_WUCKSEL_DIVIDER);
+
+ /* Enable RTC registers write protection */
+ LL_RTC_EnableWriteProtection(RTC);
+
+ return;
+}
+/**
+ * @brief Configure the system for power optimization
+ *
+ * @note This API configures the system to be ready for low power mode
+ *
+ * @param None
+ * @retval None
+ */
+static void SystemPower_Config(void)
+{
+ /**
+ * Select HSI as system clock source after Wake Up from Stop mode
+ */
+ LL_RCC_SetClkAfterWakeFromStop(LL_RCC_STOP_WAKEUPCLOCK_HSI);
+
+ /* Initialize low power manager */
+ UTIL_LPM_Init();
+ /* Initialize the CPU2 reset value before starting CPU2 with C2BOOT */
+ LL_C2_PWR_SetPowerMode(LL_PWR_MODE_SHUTDOWN);
+
+#if (CFG_USB_INTERFACE_ENABLE != 0)
+ /**
+ * Enable USB power
+ */
+ HAL_PWREx_EnableVddUSB();
+#endif
+
+ return;
+}
+
+static void appe_Tl_Init( void )
+{
+ TL_MM_Config_t tl_mm_config;
+ SHCI_TL_HciInitConf_t SHci_Tl_Init_Conf;
+ /**< Reference table initialization */
+ TL_Init();
+
+ /**< System channel initialization */
+ UTIL_SEQ_RegTask( 1<< CFG_TASK_SYSTEM_HCI_ASYNCH_EVT_ID, UTIL_SEQ_RFU, shci_user_evt_proc );
+ SHci_Tl_Init_Conf.p_cmdbuffer = (uint8_t*)&SystemCmdBuffer;
+ SHci_Tl_Init_Conf.StatusNotCallBack = APPE_SysStatusNot;
+ shci_init(APPE_SysUserEvtRx, (void*) &SHci_Tl_Init_Conf);
+
+ /**< Memory Manager channel initialization */
+ tl_mm_config.p_BleSpareEvtBuffer = BleSpareEvtBuffer;
+ tl_mm_config.p_SystemSpareEvtBuffer = SystemSpareEvtBuffer;
+ tl_mm_config.p_AsynchEvtPool = EvtPool;
+ tl_mm_config.AsynchEvtPoolSize = POOL_SIZE;
+ TL_MM_Init( &tl_mm_config );
+
+ TL_Enable();
+
+ return;
+}
+
+static void APPE_SysStatusNot( SHCI_TL_CmdStatus_t status )
+{
+ UNUSED(status);
+ return;
+}
+
+/**
+ * The type of the payload for a system user event is tSHCI_UserEvtRxParam
+ * When the system event is both :
+ * - a ready event (subevtcode = SHCI_SUB_EVT_CODE_READY)
+ * - reported by the FUS (sysevt_ready_rsp == FUS_FW_RUNNING)
+ * The buffer shall not be released
+ * ( eg ((tSHCI_UserEvtRxParam*)pPayload)->status shall be set to SHCI_TL_UserEventFlow_Disable )
+ * When the status is not filled, the buffer is released by default
+ */
+static void APPE_SysUserEvtRx( void * pPayload )
+{
+ UNUSED(pPayload);
+ /* Traces channel initialization */
+ APPD_EnableCPU2( );
+
+ APP_BLE_Init( );
+ UTIL_LPM_SetOffMode(1U << CFG_LPM_APP, UTIL_LPM_ENABLE);
+ return;
+}
+
+/* USER CODE BEGIN FD_LOCAL_FUNCTIONS */
+static void Button_Init( void )
+{
+#if (CFG_BUTTON_SUPPORTED == 1)
+ /**
+ * Button Initialization
+ */
+ BSP_PB_Init(BUTTON_USER1, BUTTON_MODE_EXTI);
+ BSP_PB_Init(BUTTON_USER2, BUTTON_MODE_EXTI);
+
+#endif
+
+ return;
+}
+
+/* USER CODE END FD_LOCAL_FUNCTIONS */
+
+/*************************************************************
+ *
+ * WRAP FUNCTIONS
+ *
+ *************************************************************/
+void HAL_Delay(uint32_t Delay)
+{
+ uint32_t tickstart = HAL_GetTick();
+ uint32_t wait = Delay;
+
+ /* Add a freq to guarantee minimum wait */
+ if (wait < HAL_MAX_DELAY)
+ {
+ wait += HAL_GetTickFreq();
+ }
+
+ while ((HAL_GetTick() - tickstart) < wait)
+ {
+ /************************************************************************************
+ * ENTER SLEEP MODE
+ ***********************************************************************************/
+ LL_LPM_EnableSleep( ); /**< Clear SLEEPDEEP bit of Cortex System Control Register */
+
+ /**
+ * This option is used to ensure that store operations are completed
+ */
+ #if defined ( __CC_ARM)
+ __force_stores();
+ #endif
+
+ __WFI( );
+ }
+}
+
+void MX_APPE_Process(void)
+{
+ /* USER CODE BEGIN MX_APPE_Process_1 */
+
+ /* USER CODE END MX_APPE_Process_1 */
+ UTIL_SEQ_Run(UTIL_SEQ_DEFAULT);
+ /* USER CODE BEGIN MX_APPE_Process_2 */
+
+ /* USER CODE END MX_APPE_Process_2 */
+}
+
+void UTIL_SEQ_Idle( void )
+{
+#if ( CFG_LPM_SUPPORTED == 1)
+ UTIL_LPM_EnterLowPower( );
+#endif
+ return;
+}
+
+/**
+ * @brief This function is called by the scheduler each time an event
+ * is pending.
+ *
+ * @param evt_waited_bm : Event pending.
+ * @retval None
+ */
+void UTIL_SEQ_EvtIdle( UTIL_SEQ_bm_t task_id_bm, UTIL_SEQ_bm_t evt_waited_bm )
+{
+ UTIL_SEQ_Run( UTIL_SEQ_DEFAULT );
+}
+
+void shci_notify_asynch_evt(void* pdata)
+{
+ UTIL_SEQ_SetTask( 1<<CFG_TASK_SYSTEM_HCI_ASYNCH_EVT_ID, CFG_SCH_PRIO_0);
+ return;
+}
+
+void shci_cmd_resp_release(uint32_t flag)
+{
+ UTIL_SEQ_SetEvt( 1<< CFG_IDLEEVT_SYSTEM_HCI_CMD_EVT_RSP_ID );
+ return;
+}
+
+void shci_cmd_resp_wait(uint32_t timeout)
+{
+ UTIL_SEQ_WaitEvt( 1<< CFG_IDLEEVT_SYSTEM_HCI_CMD_EVT_RSP_ID );
+ return;
+}
+
+/* USER CODE BEGIN FD_WRAP_FUNCTIONS */
+void HAL_GPIO_EXTI_Callback( uint16_t GPIO_Pin )
+{
+ switch (GPIO_Pin)
+ {
+ case GPIO_PIN_12:
+ /* SW button 1 */
+ APP_BLE_Key_Button1_Action();
+ break;
+
+ case GPIO_PIN_13:
+ /* SW button 2 */
+ APP_BLE_Key_Button2_Action();
+ break;
+ default:
+ break;
+ }
+ return;
+}
+
+/* USER CODE END FD_WRAP_FUNCTIONS */
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/