Skip to content

Commit f4b8e37

Browse files
authored
Merge pull request #368 from ainyan03/rtc_failure_reporting
RTC: report failures from every setter, add isValid(), make timerSleep fail-closed
2 parents 24dad4f + 504cdd6 commit f4b8e37

13 files changed

Lines changed: 867 additions & 297 deletions

examples/Basic/Rtc/Rtc.ino

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ void setup(void)
9595

9696
time_t t = time(nullptr)+1; // Advance one second.
9797
while (t > time(nullptr)); /// Synchronization in seconds
98-
M5.Rtc.setDateTime( gmtime( &t ) );
98+
if (!M5.Rtc.setDateTime( gmtime( &t ) ))
99+
{
100+
M5.Log.println("RTC write failed.");
101+
}
99102
}
100103
else
101104
{
@@ -114,6 +117,11 @@ void loop(void)
114117

115118
auto dt = M5.Rtc.getDateTime();
116119
M5.Display.setCursor(0,0);
120+
if (!dt.isValid())
121+
{
122+
M5.Log.println("RTC read failed.");
123+
return;
124+
}
117125
M5.Log.printf("RTC UTC :%04d/%02d/%02d (%s) %02d:%02d:%02d\r\n"
118126
, dt.date.year
119127
, dt.date.month

src/utility/Power_Class.hpp

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,20 +246,43 @@ namespace m5
246246

247247
/// sleep and timer boot. The boot condition can be specified by the argument.
248248
/// @param seconds Number of seconds to boot.
249-
void timerSleep(int seconds);
249+
/// @return If this function returns, sleep was not entered and the result is false. Reasons include
250+
/// invalid arguments, existing RTC IRQ cleanup failure, RTC setup failure, ESP timer wakeup
251+
/// registration failure without an RTC, wake source registration failure, and PC builds.
252+
/// Does not return once sleep is entered. Without an RTC the ESP32 timer wakeup is used.
253+
/// @note This function owns the ESP timer wake source: it is set for this sleep and disabled
254+
/// again when the sleep is cancelled, a previous setting is not restored.
255+
/// @note PowerHub has no periodic RTC timer; use a date/time overload on those boards.
256+
/// @note When the ESP timer wakeup cannot be registered the RTC timer is used alone; that
257+
/// wakes only boards where the RTC IRQ is wired to a wake route (not an external RTC unit).
258+
bool timerSleep(int seconds);
250259

251260
/// sleep and timer boot. The boot condition can be specified by the argument.
252261
/// @param time Time to boot. (only minutes and hours can be specified. Ignore seconds)
262+
/// @return If this function returns, sleep was not entered and the result is false. Reasons include
263+
/// invalid arguments, existing RTC IRQ cleanup failure, RTC setup failure,
264+
/// wake source registration failure, and PC builds.
265+
/// Does not return once sleep is entered.
266+
/// @note The RTC alarm wakes only boards where its IRQ is wired to a wake route;
267+
/// an alarm from an external RTC unit does not wake the board.
268+
/// @note An ESP timer wake-up left by an earlier sleep is disabled and not restored.
253269
/// @attention CoreInk and M5Paper can't alarm boot because it can't be turned off while connected to USB.
254270
/// @attention CoreInk と M5Paper は USB接続中はRTCタイマー起動が出来ない。;
255-
void timerSleep(const rtc_time_t& time);
271+
bool timerSleep(const rtc_time_t& time);
256272

257273
/// sleep and timer boot. The boot condition can be specified by the argument.
258274
/// @param date Date to boot. (only date and weekDay can be specified. Ignore year and month)
259275
/// @param time Time to boot. (only minutes and hours can be specified. Ignore seconds)
276+
/// @return If this function returns, sleep was not entered and the result is false. Reasons include
277+
/// invalid arguments, existing RTC IRQ cleanup failure, RTC setup failure,
278+
/// wake source registration failure, and PC builds.
279+
/// Does not return once sleep is entered.
280+
/// @note The RTC alarm wakes only boards where its IRQ is wired to a wake route;
281+
/// an alarm from an external RTC unit does not wake the board.
282+
/// @note An ESP timer wake-up left by an earlier sleep is disabled and not restored.
260283
/// @attention CoreInk and M5Paper can't alarm boot because it can't be turned off while connected to USB.
261284
/// @attention CoreInk と M5Paper は USB接続中はRTCタイマー起動が出来ない。;
262-
void timerSleep(const rtc_date_t& date, const rtc_time_t& time);
285+
bool timerSleep(const rtc_date_t& date, const rtc_time_t& time);
263286

264287
/// Value for micro_seconds of deepSleep / lightSleep, meaning "sleep without a timer wakeup".
265288
/// The device sleeps until a wakeup pin or another wakeup source is triggered.
@@ -458,8 +481,11 @@ namespace m5
458481
/// that the answer is one of the advertised capabilities.
459482
charge_state_t _getChargeState(void);
460483
std::int32_t _getBatteryAdcRaw(void);
461-
void _powerOff(bool withTimer);
462-
void _timerSleep(void);
484+
bool _disableEspTimerWakeup(void);
485+
/// withTimer: wake by an RTC IRQ is expected (an RTC timer/alarm was armed).
486+
bool _powerOff(bool withTimer);
487+
/// rtc_armed: false = no RTC wake-up was armed (ESP timer only), so the RTC IRQ pin is not registered.
488+
bool _timerSleep(bool rtc_armed = true);
463489

464490
#if defined (CONFIG_IDF_TARGET_ESP32C5) || defined (CONFIG_IDF_TARGET_ESP32C61)
465491
/// Check whether a battery is actually attached (non-blocking).

0 commit comments

Comments
 (0)