|
| 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 | + # Business 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 | + # 开机时获取 psm 是否设置,如果已经使能,则无需再次进行设置 |
| 38 | + # Get whether psm is set when booting. If it is already enabled, there is no need to set it again. |
| 39 | + if pm.get_psm_time()[0] == 1: |
| 40 | + print("PSM has been enable, set pass") |
| 41 | + return 0 |
| 42 | + else: |
| 43 | + return pm.set_psm_time(0, 1, 0, 15) # T3412=10min T3324=30s |
| 44 | + |
| 45 | + |
| 46 | +def psm_failed_handle(delay_time, autosleep_timer): |
| 47 | + # 启动 osTimer 重启设备, 当 psm 生效后,该 osTimer 自动失效 |
| 48 | + # Start osTimer and restart the device. When psm takes effect, the osTimer will automatically expire. |
| 49 | + autosleep_timer.start(600 * 1000, 0, device_power_restart) |
| 50 | + print("autosleep_timer start") |
| 51 | + # 等待指定时长后,触发 psm 失败的处理逻辑,即使用 autosleep 并等待 osTimer 重启设备 |
| 52 | + # After waiting for the specified time, trigger the processing logic of psm failure, that is, use autosleep and wait for osTimer to restart the device. |
| 53 | + utime.sleep(delay_time) |
| 54 | + |
| 55 | + print("Device not into psm mode. Try to set autosleep.") |
| 56 | + pm.autosleep(1) |
| 57 | + print("Wait device into autosleep and wait device reset.") |
| 58 | + |
| 59 | + |
| 60 | +if __name__ == '__main__': |
| 61 | + """ |
| 62 | + 测试该脚本功能可将文件重命名为 main.py 烧录进模块中进行测试,模块 PSM 休眠唤醒后即可自动执行脚本中的功能。 |
| 63 | + To test the script function, you can rename the file to main.py and burn it into the module for testing. |
| 64 | + After the module PSM wakes up from sleep, it can automatically execute the functions in the script. |
| 65 | + """ |
| 66 | + pm.autosleep(0) |
| 67 | + if pm.get_psm_time()[0] == 1: |
| 68 | + if pm.set_psm_time(0) is True: |
| 69 | + print("Disable psm successful.") |
| 70 | + else: |
| 71 | + print("Disable psm failed.") |
| 72 | + |
| 73 | + if net.getModemFun() != 1: |
| 74 | + net.setModemFun(1) |
| 75 | + print("Set CFUN1") |
| 76 | + |
| 77 | + # PSM 的 T3324 超时 30s 后,启用错误处理逻辑 |
| 78 | + # After PSM's T3324 times out 30s, error handling logic is enabled |
| 79 | + psm_failed_delay_time = 60 + 30 |
| 80 | + stage, state = checkNet.waitNetworkReady(30) |
| 81 | + if stage == 3 and state == 1: |
| 82 | + print('Network connection successful.') |
| 83 | + else: |
| 84 | + print('Network connection failed.') |
| 85 | + # PSM 依赖网络,无网络时应立即使用 autosleep 代替之 |
| 86 | + # PSM relies on the network. If there is no network, you should use autosleep immediately instead. |
| 87 | + psm_failed_delay_time = 1 |
| 88 | + |
| 89 | + # 业务代码运行 |
| 90 | + # Business code running |
| 91 | + Business_code_example(10) |
| 92 | + |
| 93 | + if stage == 3 and state == 1: |
| 94 | + # 网络连接成功时尝试设置PSM,如果PSM设置已经生效,则不用再次设置 |
| 95 | + # Try to set up PSM when the network connection is successful. |
| 96 | + # If the PSM setting has already taken effect, there is no need to set it up again. |
| 97 | + res = psm_try_set() |
| 98 | + print("psm_try_set res", res) |
| 99 | + autosleep_timer = osTimer() |
| 100 | + print("autosleep_timer init.") |
| 101 | + # 运行错误处理,若模组能正常进入PSM,在sleep中就进入PSM了,该处实际的处理逻辑并不会运行 |
| 102 | + # Run error processing. If the module can enter PSM normally, it will enter PSM in sleep, |
| 103 | + # and the actual processing logic there will not run. |
| 104 | + psm_failed_handle(psm_failed_delay_time, autosleep_timer) |
0 commit comments