Skip to content

Commit fb820a5

Browse files
committed
Fix RTC wakeup
1 parent 3061135 commit fb820a5

File tree

4 files changed

+47
-103
lines changed

4 files changed

+47
-103
lines changed

examples/Basics/rtc/rtc_wakeup/rtc_wakeup.ino

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* 获取更多资料请访问: https://docs.m5stack.com/zh_CN/core/core2
88
*
99
* Describe: RTC--时间管理示例
10-
* Date: 2021/7/21
10+
* Date: 2023/7/14
1111
*******************************************************************************
1212
*/
1313
#include <M5Core2.h>
@@ -18,53 +18,60 @@ RTC_TimeTypeDef RTCtime_Now;
1818
char timeStrbuff[64];
1919

2020
/* After M5Core2 is started or reset
21-
the program in the setUp () function will be run, and this part will only be run once.
22-
在 M5Core2 启动或者复位后,即会开始执行setup()函数中的程序,该部分只会执行一次。 */
21+
the program in the setUp () function will be run, and this part will only be run
22+
once. 在 M5Core2
23+
启动或者复位后,即会开始执行setup()函数中的程序,该部分只会执行一次。 */
2324
void setup() {
24-
M5.begin(); //Init M5Core2. 初始化 M5Stack
25+
M5.begin(); // Init M5Core2. 初始化 M5Stack
2526

2627
RTCtime.Hours = 12; // Set the time. 设置时间
2728
RTCtime.Minutes = 31;
28-
RTCtime.Seconds = 45;
29+
RTCtime.Seconds = 00;
30+
2931
RTCtime_Now.Hours = 12;
3032
RTCtime_Now.Minutes = 30;
31-
RTCtime_Now.Seconds = 45;
33+
RTCtime_Now.Seconds = 00;
3234

33-
M5.Rtc.SetTime(&RTCtime);
3435
M5.Rtc.SetTime(&RTCtime_Now);
3536

36-
//The screen prints the formatted string and wraps it.
37-
// 屏幕打印格式化字符串并换行
37+
// The screen prints the formatted string and wraps it.
38+
// 屏幕打印格式化字符串并换行
3839
M5.Lcd.println("BtnA: shutdown, use power button to turn back on");
3940
M5.Lcd.println("BtnB: shutdown, wake up after 5 seconds");
40-
M5.Lcd.printf("BtnC: shutdown, wake up at RTC Time %d:%d:%d", RTCtime.Hours,
41-
RTCtime.Minutes, RTCtime.Seconds);
41+
M5.Lcd.printf("BtnC: shutdown, wake up at RTC Time %d:%d", RTCtime.Hours,
42+
RTCtime.Minutes);
4243
}
4344

4445
/* After the program in setup() runs, it runs the program in loop()
4546
The loop() function is an infinite loop in which the program runs repeatedly
4647
在setup()函数中的程序执行完后,会接着执行loop()函数中的程序
4748
loop()函数是一个死循环,其中的程序会不断的重复运行 */
4849
void loop() {
49-
M5.update(); //Read the status of keys A, B, and C. 读取按键 A, B, C 的状态
50+
M5.update(); // Read the status of keys A, B, and C. 读取按键 A, B, C
51+
// 的状态
5052

51-
if (M5.BtnA
52-
.wasPressed()) { //Constantly check the status of keys A, B, and C, if A press..... 不断检测按键A、B、C的状态,如果A按下....
53-
M5.shutdown(); //Turn off the power. 关闭电源
53+
if (M5.BtnA.wasPressed()) { // Constantly check the status of keys A, B,
54+
// and C, if A press.....
55+
// 不断检测按键A、B、C的状态,如果A按下....
56+
M5.shutdown(); // Turn off the power. 关闭电源
5457
} else if (M5.BtnB.wasPressed()) {
55-
M5.shutdown(
56-
5); //Turn off the power and wake up again after 5 seconds. 关闭电源,5秒后再次唤醒
58+
M5.shutdown(5); // Turn off the power and wake up again after 5
59+
// seconds. 关闭电源,5秒后再次唤醒
5760
} else if (M5.BtnC.wasPressed()) {
58-
M5.shutdown(
59-
RTCtime); //Turn off the power and wake up at the specified time. 关闭电源,在指定时间唤醒
61+
M5.shutdown(RTCtime);
62+
// Turn off the power and wake up at the specified time.
63+
// 关闭电源,在指定时间唤醒
64+
// Note: Wakeups are only accurate to the minute.
65+
// 注意:唤醒只能精确到分钟
6066
}
6167

6268
M5.Lcd.setCursor(0, 140);
63-
M5.Rtc.GetTime(&RTCtime_Now); //Gets the current time. 获取当前时间
64-
sprintf(
65-
timeStrbuff,
66-
"RTC Time Now is %02d:%02d:%02d", //Stores real-time time data to timeStrbuff. 将实时时间数据存储至timeStrbuff
67-
RTCtime_Now.Hours, RTCtime_Now.Minutes, RTCtime_Now.Seconds);
69+
M5.Rtc.GetTime(&RTCtime_Now); // Gets the current time. 获取当前时间
70+
sprintf(timeStrbuff,
71+
"RTC Time Now is %02d:%02d:%02d", // Stores real-time time data to
72+
// timeStrbuff.
73+
// 将实时时间数据存储至timeStrbuff
74+
RTCtime_Now.Hours, RTCtime_Now.Minutes, RTCtime_Now.Seconds);
6875
M5.Lcd.println(
69-
timeStrbuff); //Screen printing output timeStrbuff. 输出timeStrbuff
76+
timeStrbuff); // Screen printing output timeStrbuff. 输出timeStrbuff
7077
}

src/M5Core2.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,23 @@ void M5Core2::update() {
6262
}
6363

6464
void M5Core2::shutdown() { Axp.PowerOff(); }
65+
6566
int M5Core2::shutdown(int seconds) {
6667
Rtc.clearIRQ();
6768
Rtc.SetAlarmIRQ(seconds);
6869
delay(10);
6970
Axp.PowerOff();
7071
return 0;
7172
}
73+
7274
int M5Core2::shutdown(const RTC_TimeTypeDef &RTC_TimeStruct) {
7375
Rtc.clearIRQ();
7476
Rtc.SetAlarmIRQ(RTC_TimeStruct);
7577
delay(10);
7678
Axp.PowerOff();
7779
return 0;
7880
}
81+
7982
int M5Core2::shutdown(const RTC_DateTypeDef &RTC_DateStruct,
8083
const RTC_TimeTypeDef &RTC_TimeStruct) {
8184
Rtc.clearIRQ();

src/M5Core2.h

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,3 @@
1-
// Copyright (c) M5Core2. All rights reserved.
2-
3-
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4-
/**
5-
* \par Copyright (C), 2016-2017, M5Core2
6-
* \class M5Core2
7-
* \brief M5Core2 library.
8-
* @file M5Core2.h
9-
* @author M5Core2
10-
* @version V0.0.1 Beta
11-
* @date 2020/08/12
12-
* @brief Header for M5Core2.cpp module
13-
*
14-
* \par Description
15-
* This file is a drive for M5Core2.
16-
*
17-
* \par Method List:
18-
*
19-
* System:
20-
M5.begin();
21-
LCD:
22-
M5.lcd.setBrightness(uint8_t brightness);
23-
M5.Lcd.drawPixel(int16_t x, int16_t y, uint16_t color);
24-
M5.Lcd.drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color);
25-
M5.Lcd.fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);
26-
M5.Lcd.fillScreen(uint16_t color);
27-
M5.Lcd.drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color);
28-
M5.Lcd.drawCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername,uint16_t color);
29-
M5.Lcd.fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color);
30-
M5.Lcd.fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername,int16_t delta, uint16_t color);
31-
M5.Lcd.drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color);
32-
M5.Lcd.fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color);
33-
M5.Lcd.drawRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius, uint16_t color);
34-
M5.Lcd.fillRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius, uint16_t color);
35-
M5.Lcd.drawBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color);
36-
M5.Lcd.drawRGBBitmap(int16_t x, int16_t y, const uint16_t bitmap[], int16_t w, int16_t h),
37-
M5.Lcd.drawChar(uint16_t x, uint16_t y, char c, uint16_t color, uint16_t bg, uint8_t size);
38-
M5.Lcd.setCursor(uint16_t x0, uint16_t y0);
39-
M5.Lcd.setTextColor(uint16_t color);
40-
M5.Lcd.setTextColor(uint16_t color, uint16_t backgroundcolor);
41-
M5.Lcd.setTextSize(uint8_t size);
42-
M5.Lcd.setTextWrap(boolean w);
43-
M5.Lcd.printf();
44-
M5.Lcd.print();
45-
M5.Lcd.println();
46-
M5.Lcd.drawCentreString(const char *string, int dX, int poY, int font);
47-
M5.Lcd.drawRightString(const char *string, int dX, int poY, int font);
48-
M5.Lcd.drawJpg(const uint8_t *jpg_data, size_t jpg_len, uint16_t x, uint16_t y);
49-
M5.Lcd.drawJpgFile(fs::FS &fs, const char *path, uint16_t x, uint16_t y);
50-
M5.Lcd.drawBmpFile(fs::FS &fs, const char *path, uint16_t x, uint16_t y);
51-
Touch:
52-
See M5Touch.h for documentation
53-
Buttons:
54-
See utility/M5Button.h for documentation
55-
*
56-
* \par History:
57-
* <pre>
58-
* `<Author>` `<Time>` `<Version>` `<Descr>`
59-
* Hades2001 2020/08/12 0.0.1 Rebuild the new.
60-
* </pre>
61-
*
62-
*/
63-
// #define ESP32
64-
651
#ifndef _M5Core2_H_
662
#define _M5Core2_H_
673

src/RTC.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,8 @@ uint8_t RTC::Bcd2ToByte(uint8_t Value) {
9292
}
9393

9494
uint8_t RTC::ByteToBcd2(uint8_t Value) {
95-
uint8_t bcdhigh = 0;
96-
97-
while (Value >= 10) {
98-
bcdhigh++;
99-
Value -= 10;
100-
}
101-
102-
return ((uint8_t)(bcdhigh << 4) | Value);
95+
uint8_t bcdhigh = Value / 10;
96+
return (bcdhigh << 4) | (Value - (bcdhigh * 10));
10397
}
10498

10599
void RTC::GetTime(RTC_TimeTypeDef *RTC_TimeStruct) {
@@ -220,25 +214,29 @@ int RTC::SetAlarmIRQ(const RTC_TimeTypeDef &RTC_TimeStruct) {
220214

221215
if (RTC_TimeStruct.Minutes >= 0) {
222216
irq_enable = true;
223-
out_buf[0] = ByteToBcd2(RTC_TimeStruct.Minutes) & 0x7f;
217+
out_buf[0] =
218+
ByteToBcd2(RTC_TimeStruct.Minutes) & 0x7f; //将第7位置0,其他表示分钟
224219
}
225220

226221
if (RTC_TimeStruct.Hours >= 0) {
227222
irq_enable = true;
228-
out_buf[1] = ByteToBcd2(RTC_TimeStruct.Hours) & 0x3f;
223+
out_buf[1] =
224+
ByteToBcd2(RTC_TimeStruct.Hours) & 0x3f; //将第7,6位置0,其他表示小时
225+
}
226+
227+
for (int i = 0; i < 4; i++) {
228+
WriteReg(0x09 + i, out_buf[i]);
229+
delay(2);
229230
}
230231

231232
uint8_t reg_value = ReadReg(0x01);
232233

233234
if (irq_enable) {
234-
reg_value |= (1 << 1);
235+
reg_value |= (1 << 1); //第 2 位设置为 1
235236
} else {
236237
reg_value &= ~(1 << 1);
237238
}
238239

239-
for (int i = 0; i < 4; i++) {
240-
WriteReg(0x09 + i, out_buf[i]);
241-
}
242240
WriteReg(0x01, reg_value);
243241

244242
return irq_enable ? 1 : 0;

0 commit comments

Comments
 (0)