From f7120413ec8d393e0e024077ba851bc71b1dda43 Mon Sep 17 00:00:00 2001 From: Erwan GOURIOU Date: Wed, 13 Jul 2016 17:15:00 +0200 Subject: [PATCH] [STM32L4xx] Sleep API factorization In order to enable sleep API factorization, HAL_Suspend/ResumeTick functions have been implemented in hal_tick.c for each platform. --- .../TARGET_DISCO_L476VG/hal_tick.c | 15 +++++++++++ .../TARGET_NUCLEO_L476RG/hal_tick.c | 15 +++++++++++ .../hal/TARGET_STM/TARGET_STM32L4/sleep.c | 26 +++++++------------ 3 files changed, 39 insertions(+), 17 deletions(-) diff --git a/hal/targets/cmsis/TARGET_STM/TARGET_STM32L4/TARGET_DISCO_L476VG/hal_tick.c b/hal/targets/cmsis/TARGET_STM/TARGET_STM32L4/TARGET_DISCO_L476VG/hal_tick.c index 48e22d387b..5a5ad5b106 100644 --- a/hal/targets/cmsis/TARGET_STM/TARGET_STM32L4/TARGET_DISCO_L476VG/hal_tick.c +++ b/hal/targets/cmsis/TARGET_STM/TARGET_STM32L4/TARGET_DISCO_L476VG/hal_tick.c @@ -107,6 +107,21 @@ HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) { return HAL_OK; } +void HAL_SuspendTick(void) +{ + TimMasterHandle.Instance = TIM_MST; + + // Disable HAL tick and us_ticker update interrupts (used for 32 bit counter) + __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC2); +} + +void HAL_ResumeTick(void) +{ + TimMasterHandle.Instance = TIM_MST; + + // Enable HAL tick and us_ticker update interrupts (used for 32 bit counter) + __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2); +} /** * @} */ diff --git a/hal/targets/cmsis/TARGET_STM/TARGET_STM32L4/TARGET_NUCLEO_L476RG/hal_tick.c b/hal/targets/cmsis/TARGET_STM/TARGET_STM32L4/TARGET_NUCLEO_L476RG/hal_tick.c index 48e22d387b..5a5ad5b106 100644 --- a/hal/targets/cmsis/TARGET_STM/TARGET_STM32L4/TARGET_NUCLEO_L476RG/hal_tick.c +++ b/hal/targets/cmsis/TARGET_STM/TARGET_STM32L4/TARGET_NUCLEO_L476RG/hal_tick.c @@ -107,6 +107,21 @@ HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) { return HAL_OK; } +void HAL_SuspendTick(void) +{ + TimMasterHandle.Instance = TIM_MST; + + // Disable HAL tick and us_ticker update interrupts (used for 32 bit counter) + __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC2); +} + +void HAL_ResumeTick(void) +{ + TimMasterHandle.Instance = TIM_MST; + + // Enable HAL tick and us_ticker update interrupts (used for 32 bit counter) + __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2); +} /** * @} */ diff --git a/hal/targets/hal/TARGET_STM/TARGET_STM32L4/sleep.c b/hal/targets/hal/TARGET_STM/TARGET_STM32L4/sleep.c index b7ef08b793..6fb162e453 100644 --- a/hal/targets/hal/TARGET_STM/TARGET_STM32L4/sleep.c +++ b/hal/targets/hal/TARGET_STM/TARGET_STM32L4/sleep.c @@ -32,28 +32,20 @@ #if DEVICE_SLEEP #include "cmsis.h" -#include "hal_tick.h" - -static TIM_HandleTypeDef TimMasterHandle; - -void sleep(void) -{ - // Disable HAL tick interrupt - TimMasterHandle.Instance = TIM_MST; - __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC2); +void sleep(void) { + // Stop HAL systick + HAL_SuspendTick(); // Request to enter SLEEP mode HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI); - - // Enable HAL tick interrupt - __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2); + // Restart HAL systick + HAL_ResumeTick(); } void deepsleep(void) { - // Disable HAL tick interrupt - TimMasterHandle.Instance = TIM_MST; - __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC2); + // Stop HAL systick + HAL_SuspendTick(); // Request to enter STOP mode with regulator in low power mode HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); @@ -61,8 +53,8 @@ void deepsleep(void) // After wake-up from STOP reconfigure the PLL SetSysClock(); - // Enable HAL tick interrupt - __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2); + // Restart HAL systick + HAL_ResumeTick(); } #endif