| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- local i2c_id = 1
- local ch224q_i2c_addr = 0x22 -- 受电分析芯片I2C地址
- local ch224q_i2c_reg1 = 0x09 -- 快充协议激活情况(只读)
- local ch224q_i2c_reg2 = 0x0A -- 电压请求(只写,0:5V 1:9V 2:12V 3:15V 4:20V 5:28V)
- local ch224q_i2c_reg3 = 0x50 -- 最大电流参考值(只写,该寄存器仅PD协议有效)
- local bq25895_addr = 0x6A -- 充电芯片I2C地址
- local bq25895_reg1 = 0x02 -- 控制ADC检测开关
- local bq25895_reg2 = 0x09 -- bit5位可以控制VSYS到VBAT的通路开关(默认0导通,1关闭)
- local reg_addr_ADCCTL = 0x02
- local reg_addr_BATV = 0x0E
- local reg_addr_SYSV = 0x0F
- local reg_addr_TSPCT = 0x10
- local reg_addr_VBUSV = 0x11
- local reg_addr_ICHGR = 0x12
- local reg_addr_ICO = 0x13
- local function get_value()
- local ret_val_BATV = i2c.readReg(i2c_id, bq25895_addr, reg_addr_BATV, 1)
- local ret_val_SYSV = i2c.readReg(i2c_id, bq25895_addr, reg_addr_SYSV, 1)
- local ret_val_TSPCT = i2c.readReg(i2c_id, bq25895_addr, reg_addr_TSPCT, 1)
- local ret_val_VBUSV = i2c.readReg(i2c_id, bq25895_addr, reg_addr_VBUSV, 1)
- local ret_val_ICHGR = i2c.readReg(i2c_id, bq25895_addr, reg_addr_ICHGR, 1)
- local ret_val_ICO = i2c.readReg(i2c_id, bq25895_addr, reg_addr_ICO, 1)
- local byte_BATV = (ret_val_BATV == "") and 0x00 or string.byte(ret_val_BATV)
- local byte_SYSV = (ret_val_SYSV == "") and 0x00 or string.byte(ret_val_SYSV)
- local byte_TSPCT = (ret_val_TSPCT == "") and 0x00 or string.byte(ret_val_TSPCT)
- local byte_VBUSV = (ret_val_VBUSV == "") and 0x00 or string.byte(ret_val_VBUSV)
- local byte_ICHGR = (ret_val_ICHGR == "") and 0x00 or string.byte(ret_val_ICHGR)
- local byte_ICO = (ret_val_ICO == "") and 0x00 or string.byte(ret_val_ICO)
- local s_BATV = (ret_val_BATV == "") and "nil" or string.format("%02X", byte_BATV)
- local s_SYSV = (ret_val_SYSV == "") and "nil" or string.format("%02X", byte_SYSV)
- local s_TSPCT = (ret_val_TSPCT == "") and "nil" or string.format("%02X", byte_TSPCT)
- local s_VBUSV = (ret_val_VBUSV == "") and "nil" or string.format("%02X", byte_VBUSV)
- local s_ICHGR = (ret_val_ICHGR == "") and "nil" or string.format("%02X", byte_ICHGR)
- local s_ICO = (ret_val_ICO == "") and "nil" or string.format("%02X", byte_ICO)
- local value_BATV = (byte_BATV == 0x00) and 0x00 or ((byte_BATV & 0x7F) * 20 + 2304)
- local value_SYSV = (byte_SYSV == 0x00) and 0x00 or ((byte_SYSV & 0x7F) * 20 + 2304)
- local value_TSPCT = (byte_TSPCT == 0x00) and 0x00 or ((byte_TSPCT & 0x7F) * 0.465 + 21)
- local value_VBUSV = (byte_VBUSV == 0x00) and 0x00 or ((byte_VBUSV & 0x7F) * 100 + 2600)
- local value_ICHGR = (byte_ICHGR == 0x00) and 0x00 or ((byte_ICHGR & 0x7F) * 50)
- local value_ICO = (byte_ICO == 0x00) and 0x00 or ((byte_ICO & 0x3F) * 50 + 100)
- local value_Pin = 0
- local s_Pin = "nil"
- if (ret_val_VBUSV ~= "") and (ret_val_ICO ~= "") then
- value_Pin = value_VBUSV * value_ICO / 1000
- s_Pin = string.format("%.1fmW", value_Pin)
- else
- value_Pin = 0
- s_Pin = "nil"
- end
- local value_Pchgr = 0
- local s_Pchgr = "nil"
- if (ret_val_BATV ~= "") and (ret_val_ICHGR ~= "") then
- value_Pchgr = value_BATV * value_ICHGR / 1000
-
- s_Pchgr = string.format("%.1fmW", value_Pchgr)
- else
- value_Pchgr = 0
- s_Pchgr = "nil"
- end
- local dbg_info_s1 = ""
- dbg_info_s1 = string.format("%02X=%s, %02X=%s, %02X=%s, %02X=%s, %02X=%s, %02X=%s=%dmA\n",
- reg_addr_BATV, s_BATV,
- reg_addr_SYSV, s_SYSV,
- reg_addr_TSPCT, s_TSPCT,
- reg_addr_VBUSV, s_VBUSV,
- reg_addr_ICHGR, s_ICHGR,
- reg_addr_ICO, s_ICO,value_ICO
- )
- dbg_info_s1 = dbg_info_s1 .."\n"
- dbg_info_s1 = dbg_info_s1..string.format("iBat=%dmV;", value_BATV)
- dbg_info_s1 = dbg_info_s1..string.format("Sys=%dmV;", value_SYSV)
- dbg_info_s1 = dbg_info_s1..string.format("TS=%.2f%%;", value_TSPCT)
- dbg_info_s1 = dbg_info_s1..string.format("Bus=%dmV;", value_VBUSV)
- dbg_info_s1 = dbg_info_s1..string.format("Chg=%dmA;", value_ICHGR)
- dbg_info_s1 = dbg_info_s1 .."\n"
- -- dbg_info_s1 = dbg_info_s1.."iccid:"..(mobile.iccid() or "N/A")..";"
- -- dbg_info_s1 = dbg_info_s1.."imei:"..(mobile.imei() or "N/A")..";\n"
- -- dbg_info_s1 = dbg_info_s1.."csq:"..(mobile.csq() or "N/A")
- -- dbg_info_s1 = dbg_info_s1..";rssi:"..(mobile.rssi() or "N/A")
- -- dbg_info_s1 = dbg_info_s1..";rsrq:"..(mobile.rsrq() or "N/A")
- -- dbg_info_s1 = dbg_info_s1..";rsrp:"..(mobile.rsrp() or "N/A")
- -- dbg_info_s1 = dbg_info_s1..";snr:"..(mobile.snr() or "N/A")
- uart.write(1, dbg_info_s1)
- log.info("充电信息:", dbg_info_s1)
- end
- local function init_set()
- -- 3V3使能
- gpio.setup(140,0)
- gpio.set(140, gpio.HIGH)
- -- 10V使能
- gpio.setup(30,0)
- gpio.set(30, gpio.HIGH)
- -- BQ_CE电池充电使能
- gpio.setup(141,0)
- -- 显示模块使能
- gpio.setup(32,0)
- -- 运行灯闪烁控制
- gpio.setup(156,0)
- sys.timerLoopStart(function()
- gpio.toggle(156)
- end, 500)
- end
-
- sys.taskInit(function()
- init_set()
- sys.wait(200)
- local result = i2c.setup(i2c_id,i2c.SLOW)
- log.info("i2c 1", "setup", result)
- local result_soft_rst = i2c.writeReg(i2c_id, bq25895_addr, 0x14, string.char(0x80))
- -- local data1 = i2c.readReg(i2c_id, ch224q_i2c_addr, ch224q_i2c_reg1, 1)
- -- local data2 = i2c.readReg(i2c_id, ch224q_i2c_addr, ch224q_i2c_reg3, 1)
- -- local result1 = i2c.writeReg(i2c_id, ch224q_i2c_addr, ch224q_i2c_reg2, string.char(0x01))
- -- if result1 then
- -- uart.write(1, "成功写入数据")
- -- else
- -- uart.write(1, "写入数据失败")
- -- end
- -- log.info("i2c 请求电压",result1)
- -- uart.write(1, "充电协议:"..data1:toHex())
- -- uart.write(1, "可用最大电流:"..data2:toHex())
- -- local reg2_data = i2c.readReg(i2c_id, bq25895_addr, bq25895_reg1, 1)
- -- local reg2_temp = (reg2_data == "") and 0x00 or string.byte(reg2_data)
- -- local reg2_data_result = reg2_temp | 0xC0
- -- local reg2_result = i2c.writeReg(i2c_id, bq25895_addr, bq25895_reg1, string.char(reg2_data_result))
- -- -- local reg02_data = i2c.readReg(i2c_id, bq25895_addr, bq25895_reg1, 1)
- -- -- uart.write(1, "\nreg02:"..reg02_data:toHex().."\n")
- -- local reg09_result = i2c.writeReg(i2c_id, bq25895_addr, bq25895_reg2, string.char(0x44))
- -- while true do
- -- sys.wait(3000)
- -- for i = 0, 20 do
- -- -- if i == 14 then -- 读取VBAT电压值
- -- -- -- local reg09_data = i2c.readReg(i2c_id, bq25895_addr, bq25895_reg2, 1)
- -- -- -- local reg09_data_temp = string.byte(reg2_data)
- -- -- -- local reg09_data_result = 0x64
- -- -- local reg09_result = i2c.writeReg(i2c_id, bq25895_addr, bq25895_reg2, string.char(0x64))
- -- -- sys.wait(500)
- -- -- local data = i2c.readReg(i2c_id, bq25895_addr, i, 1)
- -- -- local voltage = string.toValue(data)
- -- -- uart.write(1, "寄存器14状态,"..i..":"..data:toHex().." "..voltage.."\n")
- -- -- local reg09_result = i2c.writeReg(i2c_id, bq25895_addr, bq25895_reg2, string.char(0x44))
- -- -- sys.wait(500)
- -- -- end
- -- -- local data = i2c.readReg(i2c_id, bq25895_addr, i, 1)
- -- -- uart.write(1, "寄存器状态,"..i..":"..data:toHex().."\n")
- -- end
- -- uart.write(1, "电池管理:\n")
- -- log.info("电池管理:")
- -- get_value()
- -- -- sys.wait(1000)
- -- -- local reg09_result = i2c.writeReg(i2c_id, bq25895_addr, bq25895_reg2, string.char(0x64))
- -- -- sys.wait(5000)
- -- -- uart.write(1, "断开状态下:\n")
- -- -- get_value()
- -- -- sys.wait(1000)
- -- -- local reg09_result = i2c.writeReg(i2c_id, bq25895_addr, bq25895_reg2, string.char(0x44))
-
- -- end
- end
- )
|