power_init.lua 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. local i2c_id = 1
  2. local ch224q_i2c_addr = 0x22 -- 受电分析芯片I2C地址
  3. local ch224q_i2c_reg1 = 0x09 -- 快充协议激活情况(只读)
  4. local ch224q_i2c_reg2 = 0x0A -- 电压请求(只写,0:5V 1:9V 2:12V 3:15V 4:20V 5:28V)
  5. local ch224q_i2c_reg3 = 0x50 -- 最大电流参考值(只写,该寄存器仅PD协议有效)
  6. local bq25895_addr = 0x6A -- 充电芯片I2C地址
  7. local bq25895_reg1 = 0x02 -- 控制ADC检测开关
  8. local bq25895_reg2 = 0x09 -- bit5位可以控制VSYS到VBAT的通路开关(默认0导通,1关闭)
  9. local reg_addr_ADCCTL = 0x02
  10. local reg_addr_BATV = 0x0E
  11. local reg_addr_SYSV = 0x0F
  12. local reg_addr_TSPCT = 0x10
  13. local reg_addr_VBUSV = 0x11
  14. local reg_addr_ICHGR = 0x12
  15. local reg_addr_ICO = 0x13
  16. local function get_value()
  17. local ret_val_BATV = i2c.readReg(i2c_id, bq25895_addr, reg_addr_BATV, 1)
  18. local ret_val_SYSV = i2c.readReg(i2c_id, bq25895_addr, reg_addr_SYSV, 1)
  19. local ret_val_TSPCT = i2c.readReg(i2c_id, bq25895_addr, reg_addr_TSPCT, 1)
  20. local ret_val_VBUSV = i2c.readReg(i2c_id, bq25895_addr, reg_addr_VBUSV, 1)
  21. local ret_val_ICHGR = i2c.readReg(i2c_id, bq25895_addr, reg_addr_ICHGR, 1)
  22. local ret_val_ICO = i2c.readReg(i2c_id, bq25895_addr, reg_addr_ICO, 1)
  23. local byte_BATV = (ret_val_BATV == "") and 0x00 or string.byte(ret_val_BATV)
  24. local byte_SYSV = (ret_val_SYSV == "") and 0x00 or string.byte(ret_val_SYSV)
  25. local byte_TSPCT = (ret_val_TSPCT == "") and 0x00 or string.byte(ret_val_TSPCT)
  26. local byte_VBUSV = (ret_val_VBUSV == "") and 0x00 or string.byte(ret_val_VBUSV)
  27. local byte_ICHGR = (ret_val_ICHGR == "") and 0x00 or string.byte(ret_val_ICHGR)
  28. local byte_ICO = (ret_val_ICO == "") and 0x00 or string.byte(ret_val_ICO)
  29. local s_BATV = (ret_val_BATV == "") and "nil" or string.format("%02X", byte_BATV)
  30. local s_SYSV = (ret_val_SYSV == "") and "nil" or string.format("%02X", byte_SYSV)
  31. local s_TSPCT = (ret_val_TSPCT == "") and "nil" or string.format("%02X", byte_TSPCT)
  32. local s_VBUSV = (ret_val_VBUSV == "") and "nil" or string.format("%02X", byte_VBUSV)
  33. local s_ICHGR = (ret_val_ICHGR == "") and "nil" or string.format("%02X", byte_ICHGR)
  34. local s_ICO = (ret_val_ICO == "") and "nil" or string.format("%02X", byte_ICO)
  35. local value_BATV = (byte_BATV == 0x00) and 0x00 or ((byte_BATV & 0x7F) * 20 + 2304)
  36. local value_SYSV = (byte_SYSV == 0x00) and 0x00 or ((byte_SYSV & 0x7F) * 20 + 2304)
  37. local value_TSPCT = (byte_TSPCT == 0x00) and 0x00 or ((byte_TSPCT & 0x7F) * 0.465 + 21)
  38. local value_VBUSV = (byte_VBUSV == 0x00) and 0x00 or ((byte_VBUSV & 0x7F) * 100 + 2600)
  39. local value_ICHGR = (byte_ICHGR == 0x00) and 0x00 or ((byte_ICHGR & 0x7F) * 50)
  40. local value_ICO = (byte_ICO == 0x00) and 0x00 or ((byte_ICO & 0x3F) * 50 + 100)
  41. local value_Pin = 0
  42. local s_Pin = "nil"
  43. if (ret_val_VBUSV ~= "") and (ret_val_ICO ~= "") then
  44. value_Pin = value_VBUSV * value_ICO / 1000
  45. s_Pin = string.format("%.1fmW", value_Pin)
  46. else
  47. value_Pin = 0
  48. s_Pin = "nil"
  49. end
  50. local value_Pchgr = 0
  51. local s_Pchgr = "nil"
  52. if (ret_val_BATV ~= "") and (ret_val_ICHGR ~= "") then
  53. value_Pchgr = value_BATV * value_ICHGR / 1000
  54. s_Pchgr = string.format("%.1fmW", value_Pchgr)
  55. else
  56. value_Pchgr = 0
  57. s_Pchgr = "nil"
  58. end
  59. local dbg_info_s1 = ""
  60. dbg_info_s1 = string.format("%02X=%s, %02X=%s, %02X=%s, %02X=%s, %02X=%s, %02X=%s=%dmA\n",
  61. reg_addr_BATV, s_BATV,
  62. reg_addr_SYSV, s_SYSV,
  63. reg_addr_TSPCT, s_TSPCT,
  64. reg_addr_VBUSV, s_VBUSV,
  65. reg_addr_ICHGR, s_ICHGR,
  66. reg_addr_ICO, s_ICO,value_ICO
  67. )
  68. dbg_info_s1 = dbg_info_s1 .."\n"
  69. dbg_info_s1 = dbg_info_s1..string.format("iBat=%dmV;", value_BATV)
  70. dbg_info_s1 = dbg_info_s1..string.format("Sys=%dmV;", value_SYSV)
  71. dbg_info_s1 = dbg_info_s1..string.format("TS=%.2f%%;", value_TSPCT)
  72. dbg_info_s1 = dbg_info_s1..string.format("Bus=%dmV;", value_VBUSV)
  73. dbg_info_s1 = dbg_info_s1..string.format("Chg=%dmA;", value_ICHGR)
  74. dbg_info_s1 = dbg_info_s1 .."\n"
  75. -- dbg_info_s1 = dbg_info_s1.."iccid:"..(mobile.iccid() or "N/A")..";"
  76. -- dbg_info_s1 = dbg_info_s1.."imei:"..(mobile.imei() or "N/A")..";\n"
  77. -- dbg_info_s1 = dbg_info_s1.."csq:"..(mobile.csq() or "N/A")
  78. -- dbg_info_s1 = dbg_info_s1..";rssi:"..(mobile.rssi() or "N/A")
  79. -- dbg_info_s1 = dbg_info_s1..";rsrq:"..(mobile.rsrq() or "N/A")
  80. -- dbg_info_s1 = dbg_info_s1..";rsrp:"..(mobile.rsrp() or "N/A")
  81. -- dbg_info_s1 = dbg_info_s1..";snr:"..(mobile.snr() or "N/A")
  82. uart.write(1, dbg_info_s1)
  83. log.info("充电信息:", dbg_info_s1)
  84. end
  85. local function init_set()
  86. -- 3V3使能
  87. gpio.setup(140,0)
  88. gpio.set(140, gpio.HIGH)
  89. -- 10V使能
  90. gpio.setup(30,0)
  91. gpio.set(30, gpio.HIGH)
  92. -- BQ_CE电池充电使能
  93. gpio.setup(141,0)
  94. -- 显示模块使能
  95. gpio.setup(32,0)
  96. -- 运行灯闪烁控制
  97. gpio.setup(156,0)
  98. sys.timerLoopStart(function()
  99. gpio.toggle(156)
  100. end, 500)
  101. end
  102. sys.taskInit(function()
  103. init_set()
  104. sys.wait(200)
  105. local result = i2c.setup(i2c_id,i2c.SLOW)
  106. log.info("i2c 1", "setup", result)
  107. local result_soft_rst = i2c.writeReg(i2c_id, bq25895_addr, 0x14, string.char(0x80))
  108. -- local data1 = i2c.readReg(i2c_id, ch224q_i2c_addr, ch224q_i2c_reg1, 1)
  109. -- local data2 = i2c.readReg(i2c_id, ch224q_i2c_addr, ch224q_i2c_reg3, 1)
  110. -- local result1 = i2c.writeReg(i2c_id, ch224q_i2c_addr, ch224q_i2c_reg2, string.char(0x01))
  111. -- if result1 then
  112. -- uart.write(1, "成功写入数据")
  113. -- else
  114. -- uart.write(1, "写入数据失败")
  115. -- end
  116. -- log.info("i2c 请求电压",result1)
  117. -- uart.write(1, "充电协议:"..data1:toHex())
  118. -- uart.write(1, "可用最大电流:"..data2:toHex())
  119. -- local reg2_data = i2c.readReg(i2c_id, bq25895_addr, bq25895_reg1, 1)
  120. -- local reg2_temp = (reg2_data == "") and 0x00 or string.byte(reg2_data)
  121. -- local reg2_data_result = reg2_temp | 0xC0
  122. -- local reg2_result = i2c.writeReg(i2c_id, bq25895_addr, bq25895_reg1, string.char(reg2_data_result))
  123. -- -- local reg02_data = i2c.readReg(i2c_id, bq25895_addr, bq25895_reg1, 1)
  124. -- -- uart.write(1, "\nreg02:"..reg02_data:toHex().."\n")
  125. -- local reg09_result = i2c.writeReg(i2c_id, bq25895_addr, bq25895_reg2, string.char(0x44))
  126. -- while true do
  127. -- sys.wait(3000)
  128. -- for i = 0, 20 do
  129. -- -- if i == 14 then -- 读取VBAT电压值
  130. -- -- -- local reg09_data = i2c.readReg(i2c_id, bq25895_addr, bq25895_reg2, 1)
  131. -- -- -- local reg09_data_temp = string.byte(reg2_data)
  132. -- -- -- local reg09_data_result = 0x64
  133. -- -- local reg09_result = i2c.writeReg(i2c_id, bq25895_addr, bq25895_reg2, string.char(0x64))
  134. -- -- sys.wait(500)
  135. -- -- local data = i2c.readReg(i2c_id, bq25895_addr, i, 1)
  136. -- -- local voltage = string.toValue(data)
  137. -- -- uart.write(1, "寄存器14状态,"..i..":"..data:toHex().." "..voltage.."\n")
  138. -- -- local reg09_result = i2c.writeReg(i2c_id, bq25895_addr, bq25895_reg2, string.char(0x44))
  139. -- -- sys.wait(500)
  140. -- -- end
  141. -- -- local data = i2c.readReg(i2c_id, bq25895_addr, i, 1)
  142. -- -- uart.write(1, "寄存器状态,"..i..":"..data:toHex().."\n")
  143. -- end
  144. -- uart.write(1, "电池管理:\n")
  145. -- log.info("电池管理:")
  146. -- get_value()
  147. -- -- sys.wait(1000)
  148. -- -- local reg09_result = i2c.writeReg(i2c_id, bq25895_addr, bq25895_reg2, string.char(0x64))
  149. -- -- sys.wait(5000)
  150. -- -- uart.write(1, "断开状态下:\n")
  151. -- -- get_value()
  152. -- -- sys.wait(1000)
  153. -- -- local reg09_result = i2c.writeReg(i2c_id, bq25895_addr, bq25895_reg2, string.char(0x44))
  154. -- end
  155. end
  156. )