Skip to content

Commit fa8f64f

Browse files
committed
chore: enhance shutdown mode
Signed-off-by: Frederic Pillon <[email protected]>
1 parent 5ae219c commit fa8f64f

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

README.md

+8-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ Arduino library to support STM32 Low Power.
2121
* **`void shutdown(uint32_t ms)`**: enter in shutdown mode
2222
**param** ms (optional): number of milliseconds before to exit the mode. The RTC is used in alarm mode to wakeup the board in ms milliseconds.
2323

24-
**Note: With [STM32RTC](https://github.com/stm32duino/STM32RTC) version lower than 1.1.0, the minimum number of milliseconds is 1000 ms.**
24+
> [!Note]
25+
> With [STM32RTC](https://github.com/stm32duino/STM32RTC) version lower than 1.1.0, the minimum number of milliseconds is 1000 ms.**
2526
2627
* **`void attachInterruptWakeup(uint32_t pin, voidFuncPtrVoid callback, uint32_t mode, LP_Mode LowPowerMode)`**: Enable GPIO pin in interrupt mode. If the pin is a wakeup pin, it is configured as wakeup source (see board documentation).
2728
**param** pin: pin number
@@ -49,11 +50,12 @@ enable an I2C peripheral in low power mode. See board documentation for low powe
4950

5051
`attachInterruptWakeup()` or `enableWakeupFrom()` functions should be called before `idle()`, `sleep()`, `deepSleep()` or `shutdown()` functions.
5152

52-
HardwareSerial used as Wakeup source will configure it to use HSI clock source even if another peripheral clock is configured.
53-
54-
RTC used as Wakeup source requires to have LSE or LSI as clock source. If one of them is used nothing is changed else it will configure it to use LSI clock source. One exception exists when `SHUTDOWN_MODE` is requested and `PWR_CR1_LPMS` is defined, in that case LSE is used.
55-
56-
The board will restart when exit shutdown mode.
53+
> [!Important]
54+
> * HardwareSerial used as Wakeup source will configure it to use HSI clock source even if another peripheral clock is configured.
55+
>
56+
> * RTC used as Wakeup source requires to have LSE or LSI as clock source. If one of them is used nothing is changed else it will configure it to use LSI clock source. One exception exists when `SHUTDOWN_MODE` is requested and `PWR_CR1_LPMS` is defined, in that case LSE is required. So, if the board does not have LSE, it will fail.
57+
>
58+
> * The board will restart when exit shutdown mode.
5759
5860
## Hardware state
5961

src/STM32LowPower.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ void STM32LowPower::shutdown(uint32_t ms)
115115
if ((ms != 0) || _rtc_wakeup) {
116116
programRtcWakeUp(ms, SHUTDOWN_MODE);
117117
}
118-
LowPower_shutdown();
118+
/* Get the rtc object to know if it is configured */
119+
STM32RTC &rtc = STM32RTC::getInstance();
120+
LowPower_shutdown(rtc.isConfigured());
119121
}
120122

121123
/**

src/low_power.c

+7-5
Original file line numberDiff line numberDiff line change
@@ -567,10 +567,10 @@ void LowPower_standby()
567567
/**
568568
* @brief Enable the shutdown mode.The board reset when leaves this mode.
569569
* If shutdown mode not available, use standby mode instead.
570-
* @param None
570+
* @param boolean true if RTC is configured, in that case LSE is required
571571
* @retval None
572572
*/
573-
void LowPower_shutdown()
573+
void LowPower_shutdown(bool isRTC)
574574
{
575575
__disable_irq();
576576

@@ -593,11 +593,13 @@ void LowPower_shutdown()
593593
will make system enter in Stop2 mode. */
594594
LL_C2_PWR_SetPowerMode(LL_PWR_MODE_SHUTDOWN);
595595
#endif
596-
#if defined(PWR_LOWPOWERMODE_SHUTDOWN) || defined(PWR_CR1_LPMS_SHUTDOWN) || defined(LL_PWR_SHUTDOWN_MODE)
597-
/* LSE must be on to use shutdown mode */
598-
if (__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == SET) {
596+
#if defined(PWR_CR1_LPMS)
597+
/* LSE must be on to use shutdown mode within RTC else fallback to standby */
598+
if ((!isRTC) || (__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == SET)) {
599599
HAL_PWREx_EnterSHUTDOWNMode();
600600
} else
601+
#else
602+
UNUSED(isRTC);
601603
#endif
602604
{
603605
LowPower_standby();

src/low_power.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void LowPower_EnableWakeUpUart(serial_t *serial, void (*FuncPtr)(void));
5858
void LowPower_sleep(uint32_t regulator);
5959
void LowPower_stop(serial_t *obj);
6060
void LowPower_standby();
61-
void LowPower_shutdown();
61+
void LowPower_shutdown(bool isRTC);
6262
/* Weaked function */
6363
void SystemClock_ConfigFromStop(void);
6464
#ifdef __cplusplus

0 commit comments

Comments
 (0)