|
| 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 | + |
0 commit comments