misc.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. --- 模块功能:配置管理-序列号、IMEI、底层软件版本号、时钟、是否校准、飞行模式、查询电池电量等功能
  2. -- @module misc
  3. -- @author openLuat
  4. -- @license MIT
  5. -- @copyright openLuat
  6. -- @release 2017.10.20
  7. require "ril"
  8. local req = ril.request
  9. module(..., package.seeall)
  10. --sn:序列号
  11. --imei:IMEI
  12. --modeltype:模块型号,例如724,720,722等
  13. --calib: 校准标志
  14. --ant: 耦合测试标志位
  15. --temp:模块温度
  16. local sn, imei, calib, ver, muid, ant,modeltype,temp
  17. local setSnCbFnc,setImeiCbFnc,setClkCbFnc,getTemperatureCbFnc
  18. local function timeReport()
  19. sys.publish("TIME_CLK_IND")
  20. sys.timerStart(setTimeReport,2000)
  21. end
  22. function setTimeReport()
  23. sys.timerStart(timeReport,(os.time()%60==0) and 50 or (60-os.time()%60)*1000)
  24. end
  25. --[[
  26. 函数名:rsp
  27. 功能 :本功能模块内“通过虚拟串口发送到底层core软件的AT命令”的应答处理
  28. 参数 :
  29. cmd:此应答对应的AT命令
  30. success:AT命令执行结果,true或者false
  31. response:AT命令的应答中的执行结果字符串
  32. intermediate:AT命令的应答中的中间信息
  33. 返回值:无
  34. ]]
  35. local function rsp(cmd, success, response, intermediate)
  36. local prefix = string.match(cmd, "AT(%+%u+)")
  37. --查询序列号
  38. if cmd == "AT+WISN?" then
  39. result = (intermediate~="*CME ERROR: Missing SN")
  40. if result then
  41. sn = intermediate
  42. sys.publish('SN_READY_IND')
  43. end
  44. if setSnCbFnc then setSnCbFnc(result) end
  45. --查询IMEI
  46. elseif cmd == "AT+CGSN" then
  47. imei = intermediate
  48. if setImeiCbFnc then setImeiCbFnc(true) end
  49. sys.publish('IMEI_READY_IND')
  50. --查询模块温度
  51. elseif cmd =="AT+RFTEMPERATURE?" then
  52. temp = string.match(intermediate, ':(.+)')
  53. if getTemperatureCbFnc and type(getTemperatureCbFnc)=="function" then
  54. if success then
  55. getTemperatureCbFnc(temp)
  56. else
  57. getTemperatureCbFnc("")
  58. end
  59. end
  60. --查询模块型号
  61. elseif cmd == 'AT+CGMM' then
  62. modeltype = string.match(intermediate, '"(.+)"')
  63. if success then
  64. sys.publish('MODEL_NUMBER_READY_IND')
  65. end
  66. elseif cmd == 'AT+VER' then
  67. ver = intermediate
  68. elseif prefix == '+CCLK' then
  69. if success then
  70. sys.publish('TIME_UPDATE_IND')
  71. setTimeReport()
  72. end
  73. if setClkCbFnc then setClkCbFnc(getClock(),success) end
  74. elseif cmd:match("AT%+WISN=") then
  75. if success then
  76. req("AT+WISN?")
  77. else
  78. if setSnCbFnc then setSnCbFnc(false) end
  79. end
  80. elseif cmd:match("AT%+CALIBINFO%?") then
  81. if intermediate then
  82. local LTE_afc = intermediate:match("LTE_afc:(%d)")
  83. local LTE_TDD_agc = intermediate:match("LTE_TDD_agc:(%d)")
  84. local LTE_TDD_apc = intermediate:match("LTE_TDD_apc:(%d)")
  85. local LTE_FDD_agc = intermediate:match("LTE_FDD_agc:(%d)")
  86. local LTE_FDD_apc = intermediate:match("LTE_FDD_apc:(%d)")
  87. local ANT_LTE = intermediate:match("ANT_LTE:(%d)")
  88. calib = (LTE_afc == "1" and LTE_TDD_agc == "1" and LTE_TDD_apc == "1" and LTE_FDD_agc == "1" and LTE_FDD_apc == "1")
  89. ant = (ANT_LTE == "1")
  90. end
  91. elseif cmd:match("AT%*CALINFO%=R") then
  92. if intermediate then
  93. local LteTest=intermediate:match("LteTest%,PASS")
  94. local LteCal=intermediate:match("LteCal%,PASS")
  95. if LteCal then
  96. calib=(LteCal=="LteCal,PASS")
  97. end
  98. if LteTest then
  99. ant=(LteTest=="LteTest,PASS")
  100. end
  101. end
  102. elseif cmd:match("AT%+WIMEI=") then
  103. if success then
  104. req("AT+CGSN")
  105. else
  106. if setImeiCbFnc then setImeiCbFnc(false) end
  107. end
  108. elseif cmd:match("AT%+MUID?") then
  109. if intermediate then muid = intermediate:match("+MUID:%s*(.+)") end
  110. end
  111. end
  112. --- 获取core固件名
  113. -- @return string version,core固件名
  114. -- @usage
  115. -- local version = misc.getVersion()
  116. -- 如果core为Luat_V0026_RDA8910_TTS_FLOAT,则version为string类型的"Luat_V0026_RDA8910_TTS_FLOAT"
  117. function getVersion()
  118. return rtos.get_version()
  119. end
  120. --- 设置系统时间
  121. -- @table t 系统时间,格式参考:{year=2017,month=2,day=14,hour=14,min=2,sec=58}
  122. -- @function[opt=nil] cbFnc 设置结果回调函数,回调函数的调用形式为:
  123. -- cbFnc(time,result)
  124. -- result为true表示成功,false或者nil为失败
  125. -- time表示设置之后的系统时间,table类型,例如{year=2017,month=2,day=14,hour=14,min=19,sec=23}
  126. -- @return nil
  127. -- @usage misc.setClock({year=2017,month=2,day=14,hour=14,min=2,sec=58})
  128. function setClock(t,cbFnc)
  129. if type(t) ~= "table" or (t.year-2000>38) then
  130. if cbFnc then cbFnc(getClock(),false) end
  131. return
  132. end
  133. setClkCbFnc = cbFnc
  134. req(string.format("AT+CCLK=\"%02d/%02d/%02d,%02d:%02d:%02d+32\"", string.sub(t.year, 3, 4), t.month, t.day, t.hour, t.min, t.sec), nil, rsp)
  135. end
  136. --- 获取系统时间
  137. -- @return table time,{year=2017,month=2,day=14,hour=14,min=19,sec=23}
  138. -- @usage time = getClock()
  139. function getClock()
  140. return os.date("*t")
  141. end
  142. --- 获取星期
  143. -- @return number week,1-7分别对应周一到周日
  144. -- @usage week = misc.getWeek()
  145. function getWeek()
  146. local clk = os.date("*t")
  147. return ((clk.wday == 1) and 7 or (clk.wday - 1))
  148. end
  149. --- 获取校准标志
  150. -- @return bool calib, true表示已校准,false或者nil表示未校准
  151. -- @usage calib = misc.getCalib()
  152. function getCalib()
  153. return calib
  154. end
  155. --- 获取耦合测试标志
  156. -- @return bool ant, true表示已耦合测试,false或者nil表示未耦合测试
  157. -- @usage ant = misc.getAnt()
  158. function getAnt()
  159. return ant
  160. end
  161. --- 设置SN
  162. -- @string s 新sn的字符串
  163. -- @function[opt=nil] cbFnc 设置结果回调函数,回调函数的调用形式为:
  164. -- cnFnc(result),result为true表示成功,false或者nil为失败
  165. -- @return nil
  166. -- @usage
  167. -- misc.setSn("1234567890")
  168. -- misc.setSn("1234567890",cbFnc)
  169. function setSn(s, cbFnc)
  170. if s ~= sn then
  171. setSnCbFnc = cbFnc
  172. req("AT+WISN=\"" .. s .. "\"")
  173. else
  174. if cbFnc then cbFnc(true) end
  175. end
  176. end
  177. --- 获取模块序列号
  178. -- @return string sn,序列号,如果未获取到返回""
  179. -- 注意:开机lua脚本运行之后,会发送at命令去查询sn,所以需要一定时间才能获取到sn。开机后立即调用此接口,基本上返回""
  180. -- @usage sn = misc.getSn()
  181. function getSn()
  182. return sn or ""
  183. end
  184. --- 设置IMEI
  185. -- @string s 新IMEI字符串
  186. -- @function[opt=nil] cbFnc 设置结果回调函数,回调函数的调用形式为:
  187. -- cnFnc(result),result为true表示成功,false或者nil为失败
  188. -- @return nil
  189. -- @usage misc.setImei(”359759002514931”)
  190. function setImei(s, cbFnc)
  191. if s ~= imei then
  192. setImeiCbFnc = cbFnc
  193. req("AT+WIMEI=\"" .. s .. "\"")
  194. else
  195. if cbFnc then cbFnc(true) end
  196. end
  197. end
  198. --- 获取模块IMEI
  199. -- @return string,IMEI号,如果未获取到返回""
  200. -- 注意:开机lua脚本运行之后,会发送at命令去查询imei,所以需要一定时间才能获取到imei。开机后立即调用此接口,基本上返回""
  201. -- @usage imei = misc.getImei()
  202. function getImei()
  203. return imei or ""
  204. end
  205. --- 获取模块型号
  206. -- @return string,模块型号,如果未获取到返回""
  207. -- 例如:模块型号为724UG,则返回值为Air724UG;模块型号为722UG,则返回值为Air722UG;模块型号为820UG,则返回值为Air820UG
  208. -- 注意:开机lua脚本运行之后,会发送at命令去查询模块型号,所以需要一定时间才能获取到模块型号。开机后立即调用此接口,基本上返回""
  209. -- @usage modeltype = getModelType()
  210. function getModelType()
  211. return modeltype or ""
  212. end
  213. -- 获取模块温度
  214. -- @return string,模块温度,如果要对该值进行运算,可以使用带float的固件将该值转为number
  215. -- 例如:模块温度为29.77摄氏度,则返回值为29.77
  216. function getTemperature(cb)
  217. getTemperatureCbFnc = cb
  218. ril.request("AT+RFTEMPERATURE?")
  219. end
  220. --- 获取VBAT的电池电压
  221. -- @return number,电池电压,单位mv
  222. -- @usage vb = getVbatt()
  223. function getVbatt()
  224. if type(pmd.libScriptInit)=="function" then pmd.libScriptInit() end
  225. local v1, v2, v3, v4, v5 = pmd.param_get()
  226. return v2
  227. end
  228. --- 获取VBUS连接状态
  229. -- @return boolean,true表示VBUS连接,false表示未连接
  230. -- @usage vbus = getVbus()
  231. function getVbus()
  232. local v1, v2, v3, v4, v5 = pmd.param_get()
  233. log.info("misc.getVbus",v1, v2, v3, v4, v5)
  234. return v4
  235. end
  236. --- 获取模块MUID
  237. -- @return string,MUID号,如果未获取到返回""
  238. -- 注意:开机lua脚本运行之后,会发送at命令去查询muid,所以需要一定时间才能获取到muid。开机后立即调用此接口,基本上返回""
  239. -- @usage muid = misc.getMuid()
  240. function getMuid()
  241. return muid or ""
  242. end
  243. --- 打开并且配置PWM(支持2路PWM,仅支持输出)
  244. -- @number id PWM输出通道,仅支持0和1
  245. -- 0使用MODULE_STATUS/GPIO_5引脚
  246. -- 1使用GPIO_13引脚,注意:上电的时候不要把 GPIO_13 拉高到V_GLOBAL_1V8,否则模块会进入校准模式,不正常开机
  247. -- @number para1 当id为0时,para1表示分频系数,最大值为2047;分频系数和频率的换算关系为:频率=25000000/para1(Hz);例如para1为500时,频率为50000Hz
  248. -- 分频系数和周期的换算关系为:周期=para1/25000000(s);例如para1为500时,周期为20us
  249. -- 当id为1时,para1表示时钟周期,取值范围为0-7,仅支持整数
  250. -- 0-7分别对应125、250、500、1000、1500、2000、2500、3000毫秒
  251. -- @number para2 当id为0时,para2表示占空比计算系数,最大值为1023;占空比计算系数和占空比的计算关系为:占空比=para2/para1
  252. -- 当id为1时,para2表示一个时钟周期内的高电平时间,取值范围为1-15,仅支持整数
  253. -- 1-15分别对应15.6、31.2、46.8、62、78、94、110、125、140、156、172、188、200、218、234毫秒
  254. -- @return nil
  255. -- @usage
  256. -- 通道0,频率为50000Hz,占空比为0.2:
  257. -- 频率为50000Hz,表示时钟周期为1/50000=0.00002秒=0.02毫秒=20微秒
  258. -- 占空比表示在一个时钟周期内,高电平的时长/时钟周期的时长,本例子中的0.2就表示,高电平时长为4微秒,低电平时长为16微秒
  259. -- misc.openPwm(0,500,100)
  260. --
  261. -- 通道1,时钟周期为500ms,高电平时间为125毫秒:
  262. -- misc.openPwm(1,2,8)
  263. function openPwm(id, para1, para2)
  264. pwm.open(id)
  265. pwm.set(id,para1,para2)
  266. end
  267. --- 关闭PWM
  268. -- @number id PWM输出通道,仅支持0和1
  269. -- 0使用MODULE_STATUS/GPIO_5引脚
  270. -- 1使用GPIO_13引脚,注意:上电的时候不要把 GPIO_13 拉高到V_GLOBAL_1V8,否则模块会进入校准模式,不正常开机
  271. -- @return nil
  272. function closePwm(id)
  273. assert(id == 0 or id == 1, "closepwm id error: " .. id)
  274. pwm.close(id)
  275. end
  276. --注册以下AT命令的应答处理函数
  277. ril.regRsp("+WISN", rsp)
  278. ril.regRsp("+CGSN", rsp)
  279. ril.regRsp("+RFTEMPERATURE",rsp)
  280. ril.regRsp("+CGMM", rsp)
  281. ril.regRsp("+MUID", rsp)
  282. ril.regRsp("+WIMEI", rsp)
  283. ril.regRsp("+AMFAC", rsp)
  284. --ril.regRsp('+VER', rsp, 4, '^[%w_]+$')
  285. ril.regRsp("+CALIBINFO",rsp)
  286. ril.regRsp("*CALINFO",rsp)
  287. --req('AT+VER')
  288. --查询序列号
  289. req("AT+WISN?")
  290. --查询IMEI
  291. req("AT+CGSN")
  292. req("AT+MUID?")
  293. req("AT*EXINFO?")
  294. --查询模块温度
  295. -- req("AT+RFTEMPERATURE?")
  296. --查询模块型号
  297. if string.match(rtos.get_version(),"ASR1603") then
  298. req("AT*CALINFO=R,LteCal")
  299. req("AT*CALINFO=R,LteTest")
  300. end
  301. req("AT+CGMM")
  302. setTimeReport()