Skip to content

Commit 17456dc

Browse files
committed
STM32WB align deepsleep functions with CubeFW
1 parent ea0e4df commit 17456dc

File tree

3 files changed

+351
-105
lines changed

3 files changed

+351
-105
lines changed
Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
/* USER CODE BEGIN Header */
2+
/**
3+
***************************************************************************************
4+
* File Name : stm32_lpm_if.c
5+
* Description : Low layer function to enter/exit low power modes (stop, sleep).
6+
***************************************************************************************
7+
* @attention
8+
*
9+
* <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
10+
* All rights reserved.</center></h2>
11+
*
12+
* This software component is licensed by ST under BSD 3-Clause license,
13+
* the "License"; You may not use this file except in compliance with the
14+
* License. You may obtain a copy of the License at:
15+
* opensource.org/licenses/BSD-3-Clause
16+
*
17+
******************************************************************************
18+
*/
19+
/* USER CODE END Header */
20+
21+
/* Includes ------------------------------------------------------------------*/
22+
#include "stm32wbxx_ll_hsem.h"
23+
#include "stm32wbxx_ll_cortex.h"
24+
#include "app_conf.h"
25+
/* USER CODE BEGIN include */
26+
27+
/* USER CODE END include */
28+
29+
/* Exported variables --------------------------------------------------------*/
30+
31+
/* Private function prototypes -----------------------------------------------*/
32+
static void Switch_On_HSI( void );
33+
/* USER CODE BEGIN Private_Function_Prototypes */
34+
35+
/* USER CODE END Private_Function_Prototypes */
36+
/* Private typedef -----------------------------------------------------------*/
37+
/* USER CODE BEGIN Private_Typedef */
38+
39+
/* USER CODE END Private_Typedef */
40+
/* Private define ------------------------------------------------------------*/
41+
/* USER CODE BEGIN Private_Define */
42+
43+
/* USER CODE END Private_Define */
44+
/* Private macro -------------------------------------------------------------*/
45+
/* USER CODE BEGIN Private_Macro */
46+
47+
/* USER CODE END Private_Macro */
48+
/* Private variables ---------------------------------------------------------*/
49+
/* USER CODE BEGIN Private_Variables */
50+
51+
/* USER CODE END Private_Variables */
52+
53+
/* Functions Definition ------------------------------------------------------*/
54+
/**
55+
* @brief Enters Low Power Off Mode
56+
* @param none
57+
* @retval none
58+
*/
59+
void PWR_EnterOffMode( void )
60+
{
61+
/* USER CODE BEGIN PWR_EnterOffMode */
62+
/************************************************************************************
63+
* ENTER OFF MODE
64+
***********************************************************************************/
65+
/*
66+
* There is no risk to clear all the WUF here because in the current implementation, this API is called
67+
* in critical section. If an interrupt occurs while in that critical section before that point,
68+
* the flag is set and will be cleared here but the system will not enter Off Mode
69+
* because an interrupt is pending in the NVIC. The ISR will be executed when moving out
70+
* of this critical section
71+
*/
72+
LL_PWR_ClearFlag_WU( );
73+
74+
LL_PWR_SetPowerMode( LL_PWR_MODE_STANDBY );
75+
76+
LL_LPM_EnableDeepSleep( ); /**< Set SLEEPDEEP bit of Cortex System Control Register */
77+
78+
/**
79+
* This option is used to ensure that store operations are completed
80+
*/
81+
#if defined ( __CC_ARM)
82+
__force_stores( );
83+
#endif
84+
85+
__WFI( );
86+
/* USER CODE END PWR_EnterOffMode */
87+
}
88+
89+
/**
90+
* @brief Exits Low Power Off Mode
91+
* @param none
92+
* @retval none
93+
*/
94+
void PWR_ExitOffMode( void )
95+
{
96+
/* USER CODE BEGIN PWR_ExitOffMode */
97+
98+
/* USER CODE END PWR_ExitOffMode */
99+
}
100+
101+
/**
102+
* @brief Enters Low Power Stop Mode
103+
* @note ARM exists the function when waking up
104+
* @param none
105+
* @retval none
106+
*/
107+
void PWR_EnterStopMode( void )
108+
{
109+
/* USER CODE BEGIN PWR_EnterStopMode */
110+
/**
111+
* This function is called from CRITICAL SECTION
112+
*/
113+
while( LL_HSEM_1StepLock( HSEM, CFG_HW_RCC_SEMID ) );
114+
115+
if ( ! LL_HSEM_1StepLock( HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID ) )
116+
{
117+
if( LL_PWR_IsActiveFlag_C2DS( ) )
118+
{
119+
/* Release ENTRY_STOP_MODE semaphore */
120+
LL_HSEM_ReleaseLock( HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID, 0 );
121+
122+
/**
123+
* The switch on HSI before entering Stop Mode is required on Cut2.0
124+
* It is useless from Cut2.1
125+
*/
126+
Switch_On_HSI( );
127+
}
128+
}
129+
else
130+
{
131+
/**
132+
* The switch on HSI before entering Stop Mode is required on Cut2.0
133+
* It is useless from Cut2.1
134+
*/
135+
Switch_On_HSI( );
136+
}
137+
138+
/* Release RCC semaphore */
139+
LL_HSEM_ReleaseLock( HSEM, CFG_HW_RCC_SEMID, 0 );
140+
141+
/************************************************************************************
142+
* ENTER STOP MODE
143+
***********************************************************************************/
144+
LL_PWR_SetPowerMode( LL_PWR_MODE_STOP2 );
145+
146+
LL_LPM_EnableDeepSleep( ); /**< Set SLEEPDEEP bit of Cortex System Control Register */
147+
148+
/**
149+
* This option is used to ensure that store operations are completed
150+
*/
151+
#if defined ( __CC_ARM)
152+
__force_stores( );
153+
#endif
154+
155+
__WFI();
156+
/* USER CODE END PWR_EnterStopMode */
157+
}
158+
159+
/**
160+
* @brief Exits Low Power Stop Mode
161+
* @note Enable the pll at 32MHz
162+
* @param none
163+
* @retval none
164+
*/
165+
void PWR_ExitStopMode( void )
166+
{
167+
/* USER CODE BEGIN PWR_ExitStopMode */
168+
/**
169+
* This function is called from CRITICAL SECTION
170+
*/
171+
172+
/* Release ENTRY_STOP_MODE semaphore */
173+
LL_HSEM_ReleaseLock( HSEM, CFG_HW_ENTRY_STOP_MODE_SEMID, 0 );
174+
175+
while( LL_HSEM_1StepLock( HSEM, CFG_HW_RCC_SEMID ) );
176+
177+
if(LL_RCC_GetSysClkSource( ) == LL_RCC_SYS_CLKSOURCE_STATUS_HSI)
178+
{
179+
LL_RCC_HSE_Enable( );
180+
while(!LL_RCC_HSE_IsReady( ));
181+
LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_HSE);
182+
while (LL_RCC_GetSysClkSource( ) != LL_RCC_SYS_CLKSOURCE_STATUS_HSE);
183+
}
184+
else
185+
{
186+
/**
187+
* As long as the current application is fine with HSE as system clock source,
188+
* there is nothing to do here
189+
*/
190+
}
191+
192+
/* Release RCC semaphore */
193+
LL_HSEM_ReleaseLock( HSEM, CFG_HW_RCC_SEMID, 0 );
194+
/* USER CODE END PWR_ExitStopMode */
195+
}
196+
197+
/**
198+
* @brief Enters Low Power Sleep Mode
199+
* @note ARM exits the function when waking up
200+
* @param none
201+
* @retval none
202+
*/
203+
void PWR_EnterSleepMode( void )
204+
{
205+
/* USER CODE BEGIN PWR_EnterSleepMode */
206+
207+
HAL_SuspendTick();
208+
209+
/************************************************************************************
210+
* ENTER SLEEP MODE
211+
***********************************************************************************/
212+
LL_LPM_EnableSleep( ); /**< Clear SLEEPDEEP bit of Cortex System Control Register */
213+
214+
/**
215+
* This option is used to ensure that store operations are completed
216+
*/
217+
#if defined ( __CC_ARM)
218+
__force_stores();
219+
#endif
220+
221+
__WFI( );
222+
/* USER CODE END PWR_EnterSleepMode */
223+
}
224+
225+
/**
226+
* @brief Exits Low Power Sleep Mode
227+
* @note ARM exits the function when waking up
228+
* @param none
229+
* @retval none
230+
*/
231+
void PWR_ExitSleepMode( void )
232+
{
233+
/* USER CODE BEGIN PWR_ExitSleepMode */
234+
235+
HAL_ResumeTick();
236+
237+
/* USER CODE END PWR_ExitSleepMode */
238+
}
239+
240+
/*************************************************************
241+
*
242+
* LOCAL FUNCTIONS
243+
*
244+
*************************************************************/
245+
/**
246+
* @brief Switch the system clock on HSI
247+
* @param none
248+
* @retval none
249+
*/
250+
static void Switch_On_HSI( void )
251+
{
252+
LL_RCC_HSI_Enable( );
253+
while(!LL_RCC_HSI_IsReady( ));
254+
LL_RCC_SetSysClkSource( LL_RCC_SYS_CLKSOURCE_HSI );
255+
LL_RCC_SetSMPSClockSource(LL_RCC_SMPS_CLKSOURCE_HSI);
256+
while (LL_RCC_GetSysClkSource( ) != LL_RCC_SYS_CLKSOURCE_STATUS_HSI);
257+
}
258+
259+
/* USER CODE BEGIN Private_Functions */
260+
261+
/* USER CODE END Private_Functions */
262+
263+
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
264+
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/* USER CODE BEGIN Header */
2+
/**
3+
******************************************************************************
4+
* @file stm32_lpm_if.h
5+
* @brief Header for stm32_lpm_if.c module (device specific LP management)
6+
******************************************************************************
7+
* @attention
8+
*
9+
* <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
10+
* All rights reserved.</center></h2>
11+
*
12+
* This software component is licensed by ST under BSD 3-Clause license,
13+
* the "License"; You may not use this file except in compliance with the
14+
* License. You may obtain a copy of the License at:
15+
* opensource.org/licenses/BSD-3-Clause
16+
*
17+
******************************************************************************
18+
*/
19+
/* USER CODE END Header */
20+
21+
/* Define to prevent recursive inclusion -------------------------------------*/
22+
#ifndef __STM32_LPM_IF_H
23+
#define __STM32_LPM_IF_H
24+
25+
#ifdef __cplusplus
26+
extern "C" {
27+
#endif
28+
29+
/* Includes ------------------------------------------------------------------*/
30+
31+
/**
32+
* @brief Enters Low Power Off Mode
33+
* @param none
34+
* @retval none
35+
*/
36+
void PWR_EnterOffMode( void );
37+
/**
38+
* @brief Exits Low Power Off Mode
39+
* @param none
40+
* @retval none
41+
*/
42+
void PWR_ExitOffMode( void );
43+
44+
/**
45+
* @brief Enters Low Power Stop Mode
46+
* @note ARM exists the function when waking up
47+
* @param none
48+
* @retval none
49+
*/
50+
void PWR_EnterStopMode( void );
51+
/**
52+
* @brief Exits Low Power Stop Mode
53+
* @note Enable the pll at 32MHz
54+
* @param none
55+
* @retval none
56+
*/
57+
void PWR_ExitStopMode( void );
58+
59+
/**
60+
* @brief Enters Low Power Sleep Mode
61+
* @note ARM exits the function when waking up
62+
* @param none
63+
* @retval none
64+
*/
65+
void PWR_EnterSleepMode( void );
66+
67+
/**
68+
* @brief Exits Low Power Sleep Mode
69+
* @note ARM exits the function when waking up
70+
* @param none
71+
* @retval none
72+
*/
73+
void PWR_ExitSleepMode( void );
74+
75+
#ifdef __cplusplus
76+
}
77+
#endif
78+
79+
#endif /*__STM32_LPM_IF_H */
80+
81+
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

0 commit comments

Comments
 (0)