|
| 1 | +# Copyright (c) Quectel Wireless Solution, Co., Ltd.All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import pm |
| 16 | +import net |
| 17 | +import utime |
| 18 | +import osTimer |
| 19 | +import checkNet |
| 20 | +from misc import Power |
| 21 | + |
| 22 | + |
| 23 | +def Business_code_example(run_time): |
| 24 | + i = 0 |
| 25 | + for i in range(run_time): |
| 26 | + print("Business app running") |
| 27 | + # usiness code here |
| 28 | + utime.sleep(1) |
| 29 | + |
| 30 | + |
| 31 | +def device_power_restart(args): |
| 32 | + print("device_power_restart") |
| 33 | + Power.powerRestart() |
| 34 | + |
| 35 | + |
| 36 | +def psm_try_set(): |
| 37 | + if pm.get_psm_time()[0] == 1: # 开机时获取psm是否设置,如果已经使能,则无需再次进行设置 |
| 38 | + print("PSM has been enable, set pass") |
| 39 | + return 0 |
| 40 | + else: |
| 41 | + return pm.set_psm_time(0, 1, 0, 15) # T3412=10min T3324=30s |
| 42 | + |
| 43 | + |
| 44 | +def psm_failed_handle(delay_time, autosleep_timer): |
| 45 | + autosleep_timer.start(600 * 1000, 0, device_power_restart) # 启动osTimer重启设备, 当psm生效后,该osTimer自动失效 |
| 46 | + print("autosleep_timer start") |
| 47 | + utime.sleep(delay_time) # 等待指定时长后,触发PSM失败的处理逻辑,即使用autosleep并等待osTimer重启设备 |
| 48 | + |
| 49 | + print("Device not into psm mode. Try to set autosleep.") |
| 50 | + pm.autosleep(1) |
| 51 | + print("Wait device into autosleep and wait device reset.") |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == '__main__': |
| 55 | + pm.autosleep(0) |
| 56 | + if pm.get_psm_time()[0] == 1: |
| 57 | + if pm.set_psm_time(0) is True: |
| 58 | + print("Disable psm successful.") |
| 59 | + else: |
| 60 | + print("Disable psm failed.") |
| 61 | + |
| 62 | + if net.getModemFun() != 1: |
| 63 | + net.setModemFun(1) |
| 64 | + print("Set CFUN1") |
| 65 | + |
| 66 | + psm_failed_delay_time = 60 + 30 # PSM的 T3324超时30S后,启用错误处理逻辑 |
| 67 | + stage, state = checkNet.waitNetworkReady(30) |
| 68 | + if stage == 3 and state == 1: |
| 69 | + print('Network connection successful.') |
| 70 | + else: |
| 71 | + print('Network connection failed.') |
| 72 | + psm_failed_delay_time = 1 # PSM依赖网络,无网络时应立即使用RTC代替之 |
| 73 | + |
| 74 | + Business_code_example(10) # 业务代码运行 |
| 75 | + |
| 76 | + if stage == 3 and state == 1: |
| 77 | + res = psm_try_set() # 网络连接成功时尝试设置PSM,如果PSM设置已经生效,则不用再次设置 |
| 78 | + print("psm_try_set res", res) |
| 79 | + autosleep_timer = osTimer() |
| 80 | + print("autosleep_timer init.") |
| 81 | + psm_failed_handle(psm_failed_delay_time, autosleep_timer) # 运行错误处理,若模组能正常进入PSM,在sleep中就进入PSM了,该处实际的处理逻辑并不会运行 |
0 commit comments