Skip to content

Commit 7c0258a

Browse files
authored
example(RTC&PSM demo): 添加RTC和PSM的功能示例代码 (#7)
固件版本: N/A 是否需要文案翻译: 否 Co-authored-by: mark.zhu <mark.zhu@QUECTEL.COM>
1 parent c17ea0d commit 7c0258a

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import utime
2+
import pm
3+
from machine import RTC
4+
from misc import Power
5+
import checkNet
6+
7+
def Business_code_example(run_time):
8+
i = 0
9+
for i in range(run_time):
10+
print("Business app running")
11+
#Business code here
12+
utime.sleep(1)
13+
14+
return
15+
16+
def psm_try_set():
17+
if pm.get_psm_time()[0] == 1:#开机时获取psm是否设置,如果已经使能,则无需再次进行设置
18+
print("PSM has been enable, set pass")
19+
return 0
20+
else:
21+
return pm.set_psm_time(0,1,0,1)#T3412=10min T3324=1min
22+
23+
def psm_failed_handle(delay_time):
24+
utime.sleep(delay_time)#等待指定时长后,触发PSM失败的处理逻辑,即代之以关机+RTC关机闹钟
25+
26+
rtc = RTC()
27+
tm_rtc_tuple = rtc.datetime()
28+
tm_rtc_second = utime.mktime((tm_rtc_tuple[0], tm_rtc_tuple[1], tm_rtc_tuple[2], tm_rtc_tuple[4], tm_rtc_tuple[5], tm_rtc_tuple[6], 0, 0))
29+
30+
alarm_second = tm_rtc_second + 600#RTC闹钟设为当前时间 + 10min
31+
alarm_tuple = utime.localtime(alarm_second)
32+
33+
rtc.set_alarm([alarm_tuple[0], alarm_tuple[1], alarm_tuple[2], alarm_tuple[6], alarm_tuple[3], alarm_tuple[4], alarm_tuple[5], 0])
34+
rtc.enable_alarm(1)
35+
36+
Power.powerDown()
37+
38+
39+
if __name__ == '__main__':
40+
psm_failed_delay_time = 60 + 30 #PSM的 T3324超时30S后,启用错误处理逻辑
41+
lpm_fd = pm.create_wakelock("psm_lock", len("psm_lock")) #申请功耗锁
42+
43+
stage, state = checkNet.waitNetworkReady(30)
44+
if stage == 3 and state == 1:
45+
print('Network connection successful.')
46+
psm_try_set() #网络连接成功时尝试设置PSM,如果PSM设置已经生效,则不用再次设置
47+
else:
48+
print('Network connection failed.')
49+
psm_failed_delay_time = 1 #PSM依赖网络,无网络时应立即使用RTC代替之
50+
51+
pm.wakelock_lock(lpm_fd)#锁定功耗锁,防止业务运行过程中因sleep错误进入PSM模式
52+
Business_code_example(10)#业务代码运行
53+
pm.wakelock_unlock(lpm_fd)#业务运行完成后,释放功耗锁。模组空闲状态且T3324超时后,自动进入PSM
54+
55+
psm_failed_handle(psm_failed_delay_time)#运行错误处理,若模组能正常进入PSM,在sleep中就进入PSM了,该处实际的处理逻辑并不会运行
56+
57+

system/power-manager/RTC_example.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import utime
2+
from machine import RTC
3+
from misc import Power
4+
5+
def Business_code_example(run_time):
6+
i = 0
7+
for i in range(run_time):
8+
print("Business app running")
9+
#Business code here
10+
utime.sleep(1)
11+
12+
return
13+
14+
def rtc_alarm_set(alarm_time):
15+
rtc = RTC()
16+
tm_rtc_tuple = rtc.datetime()
17+
tm_rtc_second = utime.mktime((tm_rtc_tuple[0], tm_rtc_tuple[1], tm_rtc_tuple[2], tm_rtc_tuple[4], tm_rtc_tuple[5], tm_rtc_tuple[6], 0, 0))
18+
19+
alarm_second = tm_rtc_second + alarm_time #RTC闹钟设为当前时间 + alarm_time, 即模组会在经过alarm_time s 后重启
20+
alarm_tuple = utime.localtime(alarm_second)
21+
22+
rtc.set_alarm([alarm_tuple[0], alarm_tuple[1], alarm_tuple[2], alarm_tuple[6], alarm_tuple[3], alarm_tuple[4], alarm_tuple[5], 0])
23+
rtc.enable_alarm(1)
24+
25+
utime.sleep(1)#部分模组RTC闹钟的设置是异步的,需要一定延迟,保证底层RTC信息能够被写入
26+
return
27+
28+
29+
if __name__ == '__main__':
30+
alarm_time = 600 #RTC alarm 10min后触发
31+
Business_code_example(10)#业务代码运行
32+
33+
rtc_alarm_set(alarm_time)
34+
Power.powerDown()#设置alarm后关机
35+
36+

0 commit comments

Comments
 (0)