Select one of the symbols to view example projects that use it.
 
Outline
Includes
#include "main.h"
Private variables
RTCHandle
Private function prototypes
SleepMode_Measure()
StopMode_Measure()
StandbyMode_Measure()
StandbyRTCMode_Measure()
StandbyRTCBKPSRAMMode_Measure()
SYSCLKConfig_STOP()
Files
loading...
CodeScopeSTM32 Libraries and SamplesPWR_CurrentConsumptionSrc/stm32f4xx_lp_modes.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** ****************************************************************************** * @file PWR/PWR_CurrentConsumption/stm32f4xx_lp_modes.c * @author MCD Application Team * @brief This file provides firmware functions to manage the following * functionalities of the STM32F4xx Low Power Modes: * - Sleep Mode * - STOP mode with RTC * - STANDBY mode without RTC and BKPSRAM * - STANDBY mode with RTC * - STANDBY mode with RTC and BKPSRAM ****************************************************************************** * @attention * * Copyright (c) 2017 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** *//* ... */ /* Includes ------------------------------------------------------------------*/ #include "main.h" /** @addtogroup STM32F4xx_HAL_Examples * @{ *//* ... */ /** @addtogroup PWR_CurrentConsumption * @{ *//* ... */ Includes /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ /* RTC handler declaration */ RTC_HandleTypeDef RTCHandle; Private variables /* Private function prototypes -----------------------------------------------*/ static void SYSCLKConfig_STOP(void); Private function prototypes /* Private functions ---------------------------------------------------------*/ /** * @brief This function configures the system to enter Sleep mode for * current consumption measurement purpose. * Sleep Mode * ========== * - System Running at PLL (84MHz) * - Flash 2 wait state * - Instruction and Data caches ON * - Prefetch ON * - Code running from Internal FLASH * - All peripherals disabled. * - Wakeup using EXTI Line (Key Button) * @param None * @retval None *//* ... */ void SleepMode_Measure(void) { GPIO_InitTypeDef GPIO_InitStruct; /* Configure all GPIO as analog to reduce current consumption on non used IOs */ /* Enable GPIOs clock */ __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOD_CLK_ENABLE(); __HAL_RCC_GPIOE_CLK_ENABLE(); __HAL_RCC_GPIOH_CLK_ENABLE(); GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Pin = GPIO_PIN_All; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); HAL_GPIO_Init(GPIOH, &GPIO_InitStruct); HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* Disable GPIOs clock */ __HAL_RCC_GPIOA_CLK_DISABLE(); __HAL_RCC_GPIOB_CLK_DISABLE(); __HAL_RCC_GPIOC_CLK_DISABLE(); __HAL_RCC_GPIOD_CLK_DISABLE(); __HAL_RCC_GPIOE_CLK_DISABLE(); __HAL_RCC_GPIOH_CLK_DISABLE(); /* Configure Key Button */ BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_EXTI); /* Suspend Tick increment to prevent wakeup by Systick interrupt. Otherwise the Systick interrupt will wake up the device within 1ms (HAL time base) *//* ... */ HAL_SuspendTick(); /* Request to enter SLEEP mode */ HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI); /* Resume Tick interrupt if disabled prior to sleep mode entry */ HAL_ResumeTick(); }{ ... } /** * @brief This function configures the system to enter Stop mode with RTC * clocked by LSE or LSI for current consumption measurement purpose. * STOP Mode with RTC clocked by LSE/LSI * ===================================== * - RTC Clocked by LSE or LSI * - Regulator in LP mode * - HSI, HSE OFF and LSI OFF if not used as RTC Clock source * - No IWDG * - FLASH in deep power down mode * - Automatic Wakeup using RTC clocked by LSE/LSI (~20s) * @param None * @retval None *//* ... */ void StopMode_Measure(void) { GPIO_InitTypeDef GPIO_InitStruct; /* Configure all GPIO as analog to reduce current consumption on non used IOs */ /* Enable GPIOs clock */ __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOD_CLK_ENABLE(); __HAL_RCC_GPIOE_CLK_ENABLE(); __HAL_RCC_GPIOH_CLK_ENABLE(); GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Pin = GPIO_PIN_All; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); HAL_GPIO_Init(GPIOH, &GPIO_InitStruct); HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* Disable GPIOs clock */ __HAL_RCC_GPIOA_CLK_DISABLE(); __HAL_RCC_GPIOB_CLK_DISABLE(); __HAL_RCC_GPIOC_CLK_DISABLE(); __HAL_RCC_GPIOD_CLK_DISABLE(); __HAL_RCC_GPIOE_CLK_DISABLE(); __HAL_RCC_GPIOH_CLK_DISABLE(); /* Configure RTC prescaler and RTC data registers as follow: - Hour Format = Format 24 - Asynch Prediv = Value according to source clock - Synch Prediv = Value according to source clock - OutPut = Output Disable - OutPutPolarity = High Polarity - OutPutType = Open Drain *//* ... */ RTCHandle.Instance = RTC; RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24; RTCHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; RTCHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV; RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE; RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; if(HAL_RTC_Init(&RTCHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); }if (HAL_RTC_Init(&RTCHandle) != HAL_OK) { ... } /*## Configure the Wake up timer ###########################################*/ /* RTC Wakeup Interrupt Generation: Wakeup Time Base = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSI)) Wakeup Time = Wakeup Time Base * WakeUpCounter = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSI)) * WakeUpCounter ==> WakeUpCounter = Wakeup Time / Wakeup Time Base To configure the wake up timer to 20s the WakeUpCounter is set to 0xA017: RTC_WAKEUPCLOCK_RTCCLK_DIV = RTCCLK_Div16 = 16 Wakeup Time Base = 16 /(~32.768KHz) = ~0,488 ms Wakeup Time = ~20s = 0,488ms * WakeUpCounter ==> WakeUpCounter = ~20s/0,488ms = 40983 = 0xA017 *//* ... */ /* Disable Wake-up timer */ HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle); /* Enable Wake-up timer */ HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, (0xA000-1), RTC_WAKEUPCLOCK_RTCCLK_DIV16); /* FLASH Deep Power Down Mode enabled */ HAL_PWREx_EnableFlashPowerDown(); /*## Enter Stop Mode #######################################################*/ HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); /* Configures system clock after wake-up from STOP: enable HSI, PLL and select PLL as system clock source (HSI and PLL are disabled in STOP mode) *//* ... */ SYSCLKConfig_STOP(); /* Disable Wake-up timer */ if(HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); }if (HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle) != HAL_OK) { ... } }{ ... } /** * @brief This function configures the system to enter Standby mode for * current consumption measurement purpose. * STANDBY Mode * ============ * - Backup SRAM and RTC OFF * - IWDG and LSI OFF * - Wakeup using WakeUp Pin (PA.00) * @param None * @retval None *//* ... */ void StandbyMode_Measure(void) { /* Enable Power Clock*/ __HAL_RCC_PWR_CLK_ENABLE(); /* Allow access to Backup */ HAL_PWR_EnableBkUpAccess(); /* Reset RTC Domain */ __HAL_RCC_BACKUPRESET_FORCE(); __HAL_RCC_BACKUPRESET_RELEASE(); /* Disable all used wakeup sources: Pin1(PA.0) */ HAL_PWR_DisableWakeUpPin(PWR_WAKEUP_PIN1); /* Clear all related wakeup flags */ __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU); /* Re-enable all used wakeup sources: Pin1(PA.0) */ HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1); /*## Enter Standby Mode ####################################################*/ /* Request to enter STANDBY mode */ HAL_PWR_EnterSTANDBYMode(); }{ ... } /** * @brief This function configures the system to enter Standby mode with RTC * clocked by LSE or LSI for current consumption measurement purpose. * STANDBY Mode with RTC clocked by LSE/LSI * ======================================== * - RTC Clocked by LSE/LSI * - IWDG OFF * - Backup SRAM OFF * - Automatic Wakeup using RTC clocked by LSE/LSI (after ~20s) * @param None * @retval None *//* ... */ void StandbyRTCMode_Measure(void) { /*## Configure the RTC peripheral###########################################*/ /* Configure RTC prescaler and RTC data registers as follow: - Hour Format = Format 24 - Asynch Prediv = Value according to source clock - Synch Prediv = Value according to source clock - OutPut = Output Disable - OutPutPolarity = High Polarity - OutPutType = Open Drain *//* ... */ RTCHandle.Instance = RTC; RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24; RTCHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; RTCHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV; RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE; RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; if(HAL_RTC_Init(&RTCHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); }if (HAL_RTC_Init(&RTCHandle) != HAL_OK) { ... } /*## Configure the Wake up timer ###########################################*/ /* RTC Wakeup Interrupt Generation: Wakeup Time Base = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSI)) Wakeup Time = Wakeup Time Base * WakeUpCounter = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSI)) * WakeUpCounter ==> WakeUpCounter = Wakeup Time / Wakeup Time Base To configure the wake up timer to 20s the WakeUpCounter is set to 0xA017: RTC_WAKEUPCLOCK_RTCCLK_DIV = RTCCLK_Div16 = 16 Wakeup Time Base = 16 /(~32.768KHz) = ~0,488 ms Wakeup Time = ~20s = 0,488ms * WakeUpCounter ==> WakeUpCounter = ~20s/0,488ms = 40983 = 0xA017 *//* ... */ /* Disable Wake-up timer */ HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle); /*## Clear all related wakeup flags ########################################*/ /* Clear PWR wake up Flag */ __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU); /* Clear RTC Wake Up timer Flag */ __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&RTCHandle, RTC_FLAG_WUTF); /*## Setting the Wake up time ##############################################*/ HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, 0xA000-1, RTC_WAKEUPCLOCK_RTCCLK_DIV16); /*## Enter Standby Mode ####################################################*/ /* Request to enter STANDBY mode */ HAL_PWR_EnterSTANDBYMode(); }{ ... } /** * @brief This function configures the system to enter Standby mode with RTC * clocked by LSE or LSI and with Backup SRAM ON for current consumption * measurement purpose. * STANDBY Mode with RTC clocked by LSE/LSI and BKPSRAM * ==================================================== * - RTC Clocked by LSE or LSI * - Backup SRAM ON * - IWDG OFF * - Automatic Wakeup using RTC clocked by LSE/LSI (after ~20s) * @param None * @retval None *//* ... */ void StandbyRTCBKPSRAMMode_Measure(void) { /*## Configure the RTC peripheral###########################################*/ /* Configure RTC prescaler and RTC data registers as follow: - Hour Format = Format 24 - Asynch Prediv = Value according to source clock - Synch Prediv = Value according to source clock - OutPut = Output Disable - OutPutPolarity = High Polarity - OutPutType = Open Drain *//* ... */ RTCHandle.Instance = RTC; RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24; RTCHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; RTCHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV; RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE; RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; if(HAL_RTC_Init(&RTCHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); }if (HAL_RTC_Init(&RTCHandle) != HAL_OK) { ... } /*## Configure the Wake up timer ###########################################*/ /* RTC Wakeup Interrupt Generation: Wakeup Time Base = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSI)) Wakeup Time = Wakeup Time Base * WakeUpCounter = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSI)) * WakeUpCounter ==> WakeUpCounter = Wakeup Time / Wakeup Time Base To configure the wake up timer to 20s the WakeUpCounter is set to 0xA017: RTC_WAKEUPCLOCK_RTCCLK_DIV = RTCCLK_Div16 = 16 Wakeup Time Base = 16 /(~32.768KHz) = ~0,488 ms Wakeup Time = ~20s = 0,488ms * WakeUpCounter ==> WakeUpCounter = ~20s/0,488ms = 40983 = 0xA017 *//* ... */ /* Disable Wake-up timer */ HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle); /*## Clear all related wakeup flags ########################################*/ /* Clear PWR wake up Flag */ __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU); /* Clear RTC Wake Up timer Flag */ __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&RTCHandle, RTC_FLAG_WUTF); /*## Setting the Wake up time ##############################################*/ HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, (0xA000-1), RTC_WAKEUPCLOCK_RTCCLK_DIV16); /* Enable the Backup SRAM low power Regulator */ HAL_PWREx_EnableBkUpReg(); /*## Enter Standby Mode ####################################################*/ /* Request to enter STANDBY mode */ HAL_PWR_EnterSTANDBYMode(); }{ ... } /** * @brief Configures system clock after wake-up from STOP: enable HSI, PLL * and select PLL as system clock source. * @param None * @retval None *//* ... */ static void SYSCLKConfig_STOP(void) { RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_OscInitTypeDef RCC_OscInitStruct; uint32_t pFLatency = 0; /* Get the Oscillators configuration according to the internal RCC registers */ HAL_RCC_GetOscConfig(&RCC_OscInitStruct); /* After wake-up from STOP reconfigure the system clock: Enable HSI and PLL */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.HSICalibrationValue = 0x10; if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); }if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { ... } /* Get the Clocks configuration according to the internal RCC registers */ HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &pFLatency); /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers *//* ... */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, pFLatency) != HAL_OK) { Error_Handler(); }if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, pFLatency) != HAL_OK) { ... } }{ ... } /** * @} *//* ... */ /** * @} *//* ... */