/** *************************************************************************************** * File Name : stm32_lpm_if.c * Description : Low layer function to enter/exit low power modes (stop, sleep). *************************************************************************************** * @attention * * <h2><center>© Copyright (c) 2019 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under BSD 3-Clause license, * the "License"; You may not use this file except in compliance with the * License. You may obtain a copy of the License at: * opensource.org/licenses/BSD-3-Clause * ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ #include "stm32_lpm_if.h" #include "app_conf.h" #include "stm32wbxx_ll_hsem.h" #include "stm32wbxx_ll_cortex.h" /* Exported variables --------------------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/ static void Switch_On_HSI( void ); /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ /* Functions Definition ------------------------------------------------------*/ /** * @brief Enters Low Power Off Mode * @param none * @retval none */ void PWR_EnterOffMode( void ) { /************************************************************************************ * ENTER OFF MODE ***********************************************************************************/ /* * There is no risk to clear all the WUF here because in the current implementation, this API is called * in critical section. If an interrupt occurs while in that critical section before that point, * the flag is set and will be cleared here but the system will not enter Off Mode * because an interrupt is pending in the NVIC. The ISR will be executed when moving out * of this critical section */ LL_PWR_ClearFlag_WU( ); LL_PWR_SetPowerMode( LL_PWR_MODE_STANDBY ); LL_LPM_EnableDeepSleep( ); /**< Set 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( ); } /** * @brief Exits Low Power Off Mode * @param none * @retval none */ void PWR_ExitOffMode( void ) { } /** * @brief Enters Low Power Stop Mode * @note ARM exists the function when waking up * @param none * @retval none */ void PWR_EnterStopMode( void ) { /** * This function is called from CRITICAL SECTION */ while( LL_HSEM_1StepLock( HSEM, CFG_HW_RCC_SEMID ) ); if ( ! LL_HSEM_1StepLock( HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID ) ) { if( LL_PWR_IsActiveFlag_C2DS( ) ) { /* Release ENTRY_STOP_MODE semaphore */ LL_HSEM_ReleaseLock( HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID, 0 ); /** * The switch on HSI before entering Stop Mode is required */ Switch_On_HSI( ); } } else { /** * The switch on HSI before entering Stop Mode is required */ Switch_On_HSI( ); } /* Release RCC semaphore */ LL_HSEM_ReleaseLock( HSEM, CFG_HW_RCC_SEMID, 0 ); /************************************************************************************ * ENTER STOP MODE ***********************************************************************************/ LL_PWR_SetPowerMode( LL_PWR_MODE_STOP2 ); LL_LPM_EnableDeepSleep( ); /**< Set 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(); } /** * @brief Exits Low Power Stop Mode * @note Enable the pll at 32MHz * @param none * @retval none */ void PWR_ExitStopMode( void ) { /** * This function is called from CRITICAL SECTION */ /* Release ENTRY_STOP_MODE semaphore */ LL_HSEM_ReleaseLock( HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID, 0 ); while( LL_HSEM_1StepLock( HSEM, CFG_HW_RCC_SEMID ) ); if(LL_RCC_GetSysClkSource( ) == LL_RCC_SYS_CLKSOURCE_STATUS_HSI) { LL_RCC_HSE_Enable( ); while(!LL_RCC_HSE_IsReady( )); LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_HSE); while (LL_RCC_GetSysClkSource( ) != LL_RCC_SYS_CLKSOURCE_STATUS_HSE); } else { /** * As long as the current application is fine with HSE as system clock source, * there is nothing to do here */ } /* Release RCC semaphore */ LL_HSEM_ReleaseLock( HSEM, CFG_HW_RCC_SEMID, 0 ); } /** * @brief Enters Low Power Sleep Mode * @note ARM exits the function when waking up * @param none * @retval none */ void PWR_EnterSleepMode( void ) { HAL_SuspendTick(); /************************************************************************************ * 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( ); } /** * @brief Exits Low Power Sleep Mode * @note ARM exits the function when waking up * @param none * @retval none */ void PWR_ExitSleepMode( void ) { HAL_ResumeTick(); } /************************************************************* * * LOCAL FUNCTIONS * *************************************************************/ /** * @brief Switch the system clock on HSI * @param none * @retval none */ static void Switch_On_HSI( void ) { LL_RCC_HSI_Enable( ); while(!LL_RCC_HSI_IsReady( )); LL_RCC_SetSysClkSource( LL_RCC_SYS_CLKSOURCE_HSI ); LL_RCC_SetSMPSClockSource(LL_RCC_SMPS_CLKSOURCE_HSI); while (LL_RCC_GetSysClkSource( ) != LL_RCC_SYS_CLKSOURCE_STATUS_HSI); } /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/