Forked.
Fork of mbed-dev by
Diff: targets/TARGET_STM/sleep.c
- Revision:
- 160:d5399cc887bb
- Parent:
- 156:95d6b41a828b
- Child:
- 172:7d866c31b3c5
--- a/targets/TARGET_STM/sleep.c Tue Feb 28 17:13:35 2017 +0000 +++ b/targets/TARGET_STM/sleep.c Tue Mar 14 16:40:56 2017 +0000 @@ -27,52 +27,68 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ -#include "sleep_api.h" -#include "rtc_api_hal.h" - #if DEVICE_SLEEP #include "cmsis.h" - +#include "us_ticker_api.h" +#include "sleep_api.h" +#include "rtc_api_hal.h" +#include "hal_tick.h" -void sleep(void) +extern void HAL_SuspendTick(void); +extern void HAL_ResumeTick(void); + +void hal_sleep(void) { - // Stop HAL systick + // Stop HAL tick to avoid to exit sleep in 1ms HAL_SuspendTick(); // Request to enter SLEEP mode HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI); - // Restart HAL systick + + // Restart HAL tick HAL_ResumeTick(); } -void deepsleep(void) +void hal_deepsleep(void) { - // Stop HAL systick + // Stop HAL tick HAL_SuspendTick(); + uint32_t EnterTimeUS = us_ticker_read(); // Request to enter STOP mode with regulator in low power mode #if TARGET_STM32L4 - if (__HAL_RCC_PWR_IS_CLK_ENABLED()) { - HAL_PWREx_EnableLowPowerRunMode(); - HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFI); + int pwrClockEnabled = __HAL_RCC_PWR_IS_CLK_ENABLED(); + int lowPowerModeEnabled = PWR->CR1 & PWR_CR1_LPR; + + if (!pwrClockEnabled) { + __HAL_RCC_PWR_CLK_ENABLE(); + } + if (lowPowerModeEnabled) { HAL_PWREx_DisableLowPowerRunMode(); - } else { - __HAL_RCC_PWR_CLK_ENABLE(); + } + + HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFI); + + if (lowPowerModeEnabled) { HAL_PWREx_EnableLowPowerRunMode(); - HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFI); - HAL_PWREx_DisableLowPowerRunMode(); + } + if (!pwrClockEnabled) { __HAL_RCC_PWR_CLK_DISABLE(); } #else /* TARGET_STM32L4 */ HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); #endif /* TARGET_STM32L4 */ - // Restart HAL systick + // Restart HAL tick HAL_ResumeTick(); // After wake-up from STOP reconfigure the PLL SetSysClock(); + TIM_HandleTypeDef TimMasterHandle; + TimMasterHandle.Instance = TIM_MST; + __HAL_TIM_SET_COUNTER(&TimMasterHandle, EnterTimeUS); + #if DEVICE_LOWPOWERTIMER rtc_synchronize(); #endif