-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaxp173.lua
366 lines (334 loc) · 10.1 KB
/
axp173.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
--- 模块功能:AXP173 电源芯片控制模块
-- @author 666Qi
-- @module power.PMU_AXP173
-- @url http://www.x-powers.com/index.php/Info/product_detail/article_id/27
-- @license MIT
-- @copyright 666Qi
-- @release 2023.01.08
--
module(..., package.seeall)
-- function IRQFnc(msg)
-- log.info("getIRQFnc.gpio13IntFnc",msg,getIRQFnc())
-- --上升沿中断时:msg为cpu.INT_GPIO_POSEDGE;下降沿中断时:msg为cpu.INT_GPIO_NEGEDGE
-- if msg==cpu.INT_GPIO_NEGEDGE then
-- log.info("getIRQFnc INT msg")
-- else
-- end
-- end
-- getIRQFnc = pins.setup(pio.P0_12,IRQFnc,pio.PULLUP)
OUTPUT_CHANNEL = {
OP_DCDC1 = 0,
OP_LDO4 = 1,
OP_LDO2 = 2,
OP_LDO3 = 3,
OP_DCDC2 = 4
}
ADC_CHANNEL = {
ADC_TS = 0,
ADC_APS_V = 1,
ADC_VBUS_C = 2,
ADC_VBUS_V = 3,
ADC_ACIN_C = 4,
ADC_ACIN_V = 5,
ADC_BAT_C = 6,
ADC_BAT_V = 7
};
CHARGE_CURRENT = {
CHG_100mA = 0,
CHG_190mA = 1,
CHG_280mA = 2,
CHG_360mA = 3,
CHG_450mA = 4,
CHG_550mA = 5,
CHG_630mA = 6,
CHG_700mA = 7,
CHG_780mA = 8,
CHG_880mA = 9,
CHG_960mA = 10,
CHG_1000mA = 11,
CHG_1080mA = 12,
CHG_1160mA = 13,
CHG_1240mA = 14,
CHG_1320mA = 15
};
COULOMETER_CTRL = {
COULOMETER_RESET = 5,
COULOMETER_PAUSE = 6,
COULOMETER_ENABLE = 7
};
POWEROFF_TIME = {
POWEROFF_4S = 0,
POWEROFF_6S = 1,
POWEROFF_8S = 2,
POWEROFF_10S = 3
};
CHARGE_LED_FRE = {
HIGH_RES = 0,
CHARGE_LED_1HZ = 1,
CHARGE_LED_4HZ = 2,
LOW_LEVEL = 3
};
-- i2cid 1,2,3对应硬件的I2C1,I2C2,I2C3
-- 之前的i2cid为0不再使用
local i2cid = 2
local i2cslaveaddr = 0x34
function init()
if i2c.setup(i2cid, i2c.SLOW, i2cslaveaddr) ~= i2c.SLOW then
print("axp173 i2c init fail")
return
end
end
-- /* Private functions */
function _getMin(a, b)
if a < b then
return a
else
return b
end
end
function _getMax(a, b)
if a > b then
return a
else
return b
end
end
function _getMid(input, min, max) return _getMax(_getMin(input, max), min) end
--[[
@brief Set channels' output enable or disable
@param channel Output channel
@param state true:Enable, false:Disable
]]
function setOutputEnable(channel, state)
local buff = string.byte(i2c.read(i2cid, 0x12, 1))
local txt = "Disable"
if state == true then
-- buff | (1U << channel)
buff = bit.bor(buff, bit.lshift(1, channel))
txt = "Enable"
else
-- buff & ~(1U << channel)
buff = (bit.band(buff, bit.bnot(bit.lshift(1, channel))))
end
i2c.write(i2cid, 0x12, buff)
log.info("axp173", "channel "..channel.."Change Output ".. txt .." finish")
end
--[[
@brief Set channels' output voltage
@param channel Output channel
@param voltage DCDC1 & LDO4: 700~3500(mV), DCDC2: 700~2275(mV), LDO2 & LDO3: 1800~3300{mV}
]]
function setOutputVoltage(channel, voltage)
if channel == OUTPUT_CHANNEL.OP_DCDC1 then
-- voltage = (_getMid(voltage, 700, 3500) - 700) / 25;
-- buff = _I2C_read8Bit(0x26);
-- buff = (buff & 0B10000000) | (voltage & 0B01111111);
-- _I2C_write1Byte(0x26, buff);
voltage = math.floor((_getMid(voltage, 700, 3500) - 700) / 25)
local buff = string.byte(i2c.read(i2cid, 0x26, 1))
buff = bit.bor(bit.band(buff, 0x80), bit.band(voltage, 0x7F))
i2c.write(i2cid, 0x26, buff)
end
if channel == OUTPUT_CHANNEL.OP_DCDC2 then
-- voltage = (_getMid(voltage, 700, 2275) - 700) / 25;
-- buff = _I2C_read8Bit(0x23);
-- buff = (buff & 0B11000000) | (voltage & 0B00111111);
-- _I2C_write1Byte(0x23, buff);
voltage = math.floor((_getMid(voltage, 700, 2275) - 700) / 25)
local buff = string.byte(i2c.read(i2cid, 0x23, 1))
buff = bit.bor(bit.band(buff, 0xC0), bit.band(voltage, 0x3F))
i2c.write(i2cid, 0x23, buff)
end
if channel == OUTPUT_CHANNEL.OP_LDO2 then
-- voltage = (_getMid(voltage, 1800, 3300) - 1800) / 100;
-- buff = _I2C_read8Bit(0x28);
-- buff = (buff & 0B00001111) | (voltage << 4);
-- _I2C_write1Byte(0x28, buff);
voltage = math.floor((_getMid(voltage, 1800, 3300) - 1800) / 100)
local buff = string.byte(i2c.read(i2cid, 0x28, 1))
buff = bit.bor(bit.band(buff, 0x0f), bit.lshift(voltage, 4))
i2c.write(i2cid, 0x28, buff)
end
if channel == OUTPUT_CHANNEL.OP_LDO3 then
-- voltage = (_getMid(voltage, 1800, 3300) - 1800) / 100;
-- buff = _I2C_read8Bit(0x28);
-- buff = (buff & 0B11110000) | (voltage);
-- _I2C_write1Byte(0x28, buff);
voltage = math.floor((_getMid(voltage, 1800, 3300) - 1800) / 100)
local buff = string.byte(i2c.read(i2cid, 0x28, 1))
buff = bit.bor(bit.band(buff, 0xF0), voltage)
i2c.write(i2cid, 0x28, buff)
end
if channel == OUTPUT_CHANNEL.OP_LDO4 then
-- voltage = (_getMid(voltage, 700, 3500) - 700) / 25;
-- buff = _I2C_read8Bit(0x27);
-- buff = (buff & 0B10000000) | (voltage & 0B01111111);
-- _I2C_write1Byte(0x27, buff);
voltage = math.floor((_getMid(voltage, 700, 3500) - 700) / 25)
local buff = string.byte(i2c.read(i2cid, 0x27, 1))
buff = bit.bor(bit.band(buff, 0x80), bit.band(voltage, 0x7F))
i2c.write(i2cid, 0x27, buff)
end
print("change Output Voltage finish")
end
function powerOFF()
-- _I2C_write1Byte(0x32, (_I2C_read8Bit(0x32) | 0B10000000));
local buff = string.byte(i2c.read(i2cid, 0x32, 1))
buff = bit.bor(buff, 0x80)
i2c.write(i2cid, 0x32, buff)
end
function setPowerOffTime(time)
-- _I2C_write1Byte(0x36, ((_I2C_read8Bit(0x36) & 0B11111100) | time));
local buff = string.byte(i2c.read(i2cid, 0x36, 1))
buff = bit.bor(bit.band(buff, 0xFC), time)
i2c.write(i2cid, 0x36, buff)
end
function isACINExist()
-- return ( _I2C_read8Bit(0x00) & 0B10000000 ) ? true : false;
local buff = string.byte(i2c.read(i2cid, 0x00, 1))
if bit.band(buff, 0x80) == 1 then
return true
else
return false
end
end
function isACINAvl()
-- return ( _I2C_read8Bit(0x00) & 0B01000000 ) ? true : false;
local buff = string.byte(i2c.read(i2cid, 0x00, 1))
if bit.band(buff, 0x40) == 1 then
return true
else
return false
end
end
function isVBUSExist()
-- return ( _I2C_read8Bit(0x00) & 0B00100000 ) ? true : false;
local buff = string.byte(i2c.read(i2cid, 0x00, 1))
if bit.band(buff, 0x20) == 1 then
return true
else
return false
end
end
function isVBUSAvl()
-- return ( _I2C_read8Bit(0x00) & 0B00010000 ) ? true : false;
local buff = string.byte(i2c.read(i2cid, 0x00, 1))
if bit.band(buff, 0x10) == 1 then
return true
else
return false
end
end
--[[
* @brief Get bat current direction
*
* @return true Bat charging
* @return false Bat discharging
]]
function getBatCurrentDir()
-- return ( _I2C_read8Bit(0x00) & 0B00000100 ) ? true : false;
local buff = string.byte(i2c.read(i2cid, 0x00, 1))
if bit.band(buff, 0x04) == 1 then
return true
else
return false
end
end
function isAXP173OverTemp()
-- return ( _I2C_read8Bit(0x01) & 0B10000000 ) ? true : false;
local buff = string.byte(i2c.read(i2cid, 0x01, 1))
if bit.band(buff, 0x80) == 1 then
return true
else
return false
end
end
--[[
* @brief Get bat charging state
*
* @return true Charging
* @return false Charge finished or not charging
]]
function isCharging()
-- return ( _I2C_read8Bit(0x01) & 0B01000000 ) ? true : false;
local buff = string.byte(i2c.read(i2cid, 0x01, 1))
if bit.band(buff, 0x40) == 1 then
return true
else
return false
end
end
function isBatExist()
-- return ( _I2C_read8Bit(0x01) & 0B00100000 ) ? true : false;
local buff = string.byte(i2c.read(i2cid, 0x01, 1))
if bit.band(buff, 0x20) == 1 then
return true
else
return false
end
end
function isChargeCsmaller()
-- return ( _I2C_read8Bit(0x01) & 0B00000100 ) ? true : false;
local buff = string.byte(i2c.read(i2cid, 0x01, 1))
if bit.band(buff, 0x04) == 1 then
return true
else
return false
end
end
-- 下面是库仑计内容 --
function setCoulometer(option, state)
local buff = string.byte(i2c.read(i2cid, 0xB8, 1))
if state == true then
-- buff | (1U << option)
buff = bit.bor(buff, bit.lshift(1, option))
else
-- buff & ~(1U << option)
buff = (bit.band(buff, bit.bnot(bit.lshift(1, option))))
end
i2c.write(i2cid, 0xB8, buff)
end
-- inline uint32_t AXP173::getCoulometerChargeData() {
-- return _I2C_read32Bit(0xB0);
-- }
function getCoulometerChargeData()
return string.byte(i2c.read(i2cid, 0xB0, 4))
end
-- inline uint32_t AXP173::getCoulometerDischargeData() {
-- return _I2C_read32Bit(0xB4);
-- }
function getCoulometerDischargeData()
return string.byte(i2c.read(i2cid, 0xB4, 4))
end
-- float AXP173::getCoulometerData() {
-- uint32_t coin = getCoulometerChargeData();
-- uint32_t coout = getCoulometerDischargeData();
-- // data = 65536 * current_LSB * (coin - coout) / 3600 / ADC rate
-- return 65536 * 0.5 * (int32_t)(coin - coout) / 3600.0 / 25.0;
-- }
function getCoulometerData()
local coin = getCoulometerChargeData()
local coout = getCoulometerDischargeData()
-- data = 65536 * current_LSB * (coin - coout) / 3600 / ADC rate
return 65536 * 0.5 * (coin - coout) / 3600.0 / 25.0
end
-- float AXP173::getBatVoltage() {
-- float ADCLSB = 1.1 / 1000.0;
-- return _I2C_read12Bit(0x78) * ADCLSB;
-- }
function getBatVoltage()
local ADCLSB = 1.1 / 1000.0;
return string.byte(i2c.read(i2cid, 0x78, 2)) * ADCLSB
end
-- float AXP173::getAXP173Temp() {
-- float ADCLSB = 0.1;
-- const float OFFSET_DEG_C = -144.7;
-- uint16_t ReData = _I2C_read12Bit(0x5E);
-- return OFFSET_DEG_C + ReData * ADCLSB;
-- }
function getAXP173Temp()
local ADCLSB = 0.1;
local OFFSET_DEG_C = -144.7;
local ReData = string.byte(i2c.read(i2cid, 0x5E, 2))
return OFFSET_DEG_C + ReData * ADCLSB
end