-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathINA3221.c
491 lines (426 loc) · 16.8 KB
/
INA3221.c
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
Arduino library for INA3221 current and voltage sensor.
MIT License
Copyright (c) 2020 Beast Devices, Andrejs Bondarevs
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
#include "INA3221.h"
#include <stddef.h>
#define swapwordp(a) ({ uint16_t __t=(*a)>>8; (*a)<<=8; (*a)|=__t; })
void ina3221_read(ina3221_t *d, uint8_t reg, uint16_t *val) {
//pDev->beginTransmission(_i2c_addr);
//pDev->write(reg); // Register
//pDev->endTransmission(false);
//pDev->requestFrom((uint8_t)_i2c_addr, (uint8_t)2);
//if (pDev->available()) { *val = ((pDev->read() << 8) | pDev->read()); }
if(d->pDev == NULL) return;
swi2c_Read_8addr(d->pDev, d->_i2c_addr, reg, (uint8_t*)val, 2);
swapwordp(val);
}
uint16_t ina3221_read16(ina3221_t *d, uint8_t reg) {
uint16_t val;
if(d->pDev == NULL) return 65535;
swi2c_Read_8addr(d->pDev, d->_i2c_addr, reg, (uint8_t*)&val, 2);
swapwordp(&val);
return val;
}
void ina3221_write(ina3221_t *d, uint8_t reg, uint16_t *val) {
//_i2c->beginTransmission(_i2c_addr);
//_i2c->write(reg); // Register
//_i2c->write((*val >> 8) & 0xFF); // Upper 8-bits
//_i2c->write(*val & 0xFF); // Lower 8-bits
//_i2c->endTransmission();
if(d->pDev == NULL) return;
swapwordp(val);
swi2c_Write_8addr(d->pDev, d->_i2c_addr, reg, (uint8_t*)val, 2);
}
void ina3221_write16(ina3221_t *d, uint8_t reg, uint16_t val) {
if(d->pDev == NULL) return;
swapwordp(&val);
swi2c_Write_8addr(d->pDev, d->_i2c_addr, reg, (uint8_t*)&val, 2);
}
uint8_t ina3221_detect(ina3221_t *d, struct swi2c_t *pvDev) {
uint8_t i, res;
uint8_t bAdr[4] = { INA3221_ADDR43_SCL, INA3221_ADDR42_SDA, INA3221_ADDR41_VCC, INA3221_ADDR40_GND };
uint8_t temp[4];
uint16_t *pID = (uint16_t*)&temp;
res = 0;
d->pDev = pvDev;
for(i=0; i<4; i++) {
swi2c_Read_8addr(d->pDev, bAdr[i], INA3221_REG_DIE_ID, temp, 2);
if(pID[0] == 0x2032) {
res = bAdr[i];
d->_i2c_addr = bAdr[i];
break;
}
}
if(res == 0) return 1;
return 0;
}
//void HAL_Delay(uint32_t Delay); //stm32 api
uint8_t ina3221_init(ina3221_t *d, struct swi2c_t *pvDev) {
if(d==NULL || pvDev==NULL) return 255;
d->pDev = pvDev;
if(d->_i2c_addr == 0) d->_i2c_addr = default_i2caddr;
swi2c_dummy_clock(d->pDev);
d->_shuntRes[0] = 100; //100 mean 100mOhm.
d->_shuntRes[1] = 100;
d->_shuntRes[2] = 100;
#ifdef use_float
d->_filterRes[0] = 0;
d->_filterRes[1] = 0;
d->_filterRes[2] = 0;
#endif
if(ina3221_detect(d, pvDev) != 0) { d->pDev = NULL; return 2; }
//if(ina3221_getDieID(d) != 0x3220) { d->pDev = NULL; return 2; }
ina3221_write16(d, INA3221_REG_CONF, 0x8000); //reset
//HAL_Delay(10);
//d->pDev->hal_delay_ms(10);
swi2c_delay_ms(10);
//STM32_DELAY_MS(10);
ina3221_write16(d, INA3221_REG_CONF, 0x7127); //to default
//HAL_Delay(2);
//if(ina3221_getDieID(d) != 0x3220) { d->pDev = NULL; return 3; }
//else return 0;
return 0;
}
/*void ina3221_setShuntRes(ina3221_t *d, uint32_t res_ch1, uint32_t res_ch2,
uint32_t res_ch3) {
d->_shuntRes[0] = res_ch1;
d->_shuntRes[1] = res_ch2;
d->_shuntRes[2] = res_ch3;
}
void ina3221_setFilterRes(ina3221_t *d, uint32_t res_ch1, uint32_t res_ch2,
uint32_t res_ch3) {
d->_filterRes[0] = res_ch1;
d->_filterRes[1] = res_ch2;
d->_filterRes[2] = res_ch3;
}*/
/*uint16_t ina3221_getReg(ina3221_t *d, uint16_t reg) {
uint16_t val = 0;
ina3221_read(d, reg, &val);
return val;
}*/
/*void ina3221_reset(ina3221_t *d) {
conf_reg_t CR = { 0 };
//ina3221_read(d, INA3221_REG_CONF, (uint16_t *)&CR);
CR.reset = 1;
ina3221_write(d, INA3221_REG_CONF, (uint16_t *)&CR);
}*/
/*void ina3221_setModePowerDown(ina3221_t *d) {
conf_reg_t CR;
ina3221_read(d, INA3221_REG_CONF, (uint16_t *)&CR);
CR.mode_bus_en = 0;
CR.mode_continious_en = 0;
ina3221_write(d, INA3221_REG_CONF, (uint16_t *)&CR);
}
void ina3221_setModeContinious(ina3221_t *d, uint8_t triggerd) {
conf_reg_t CR;
ina3221_read(d, INA3221_REG_CONF, (uint16_t *)&CR);
CR.mode_continious_en = (triggerd)?0:1;
ina3221_write(d, INA3221_REG_CONF, (uint16_t *)&CR);
}
void ina3221_setShuntMeasAble(ina3221_t *d, uint8_t enable) {
conf_reg_t CR;
ina3221_read(d, INA3221_REG_CONF, (uint16_t *)&CR);
CR.mode_shunt_en = (enable)?1:0;
ina3221_write(d, INA3221_REG_CONF, (uint16_t *)&CR);
}
void ina3221_setBusMeasAble(ina3221_t *d, uint8_t enable) {
conf_reg_t CR;
ina3221_read(d, INA3221_REG_CONF, (uint16_t *)&CR);
CR.mode_bus_en = (enable)?1:0;;
ina3221_write(d, INA3221_REG_CONF, (uint16_t *)&CR);
}*/
void ina3221_setAveragingMode(ina3221_t *d, ina3221_avg_mode_t mode) {
conf_reg_t CR;
ina3221_read(d, INA3221_REG_CONF, (uint16_t *)&CR);
CR.avg_mode = mode;
ina3221_write(d, INA3221_REG_CONF, (uint16_t *)&CR);
}
void ina3221_setBusConversionTime(ina3221_t *d, ina3221_conv_time_t convTime) {
conf_reg_t CR;
ina3221_read(d, INA3221_REG_CONF, (uint16_t *)&CR);
CR.bus_conv_time = convTime;
ina3221_write(d, INA3221_REG_CONF, (uint16_t *)&CR);
}
void ina3221_setShuntConversionTime(ina3221_t *d, ina3221_conv_time_t convTime) {
conf_reg_t CR;
ina3221_read(d, INA3221_REG_CONF, (uint16_t *)&CR);
CR.shunt_conv_time = convTime;
ina3221_write(d, INA3221_REG_CONF, (uint16_t *)&CR);
}
/*void ina3221_setPwrValidUpLimit(ina3221_t *d, int16_t voltagemV) {
ina3221_write(d, INA3221_REG_PWR_VALID_HI_LIM, (uint16_t *)&voltagemV);
}
void ina3221_setPwrValidLowLimit(ina3221_t *d, int16_t voltagemV) {
ina3221_write(d, INA3221_REG_PWR_VALID_LO_LIM, (uint16_t *)&voltagemV);
}
void ina3221_setShuntSumAlertLimit(ina3221_t *d, int32_t voltageuV) {
int16_t val = voltageuV / 20;
ina3221_write(d, INA3221_REG_SHUNTV_SUM_LIM, (uint16_t *)&val);
}
void ina3221_setCurrentSumAlertLimit(ina3221_t *d, int32_t currentmA) {
int16_t shuntuV = currentmA * (int32_t)d->_shuntRes[INA3221_CH1];
ina3221_setShuntSumAlertLimit(d, shuntuV);
}*/
/*void ina3221_setWarnAlertLatchAble(ina3221_t *d, uint8_t enable) {
masken_reg_t MR;
ina3221_read(d, INA3221_REG_MASK_ENABLE, (uint16_t*)&MR);
MR.warn_alert_latch_en = 1;
ina3221_write(d, INA3221_REG_MASK_ENABLE, (uint16_t*)&MR);
d->_masken_reg = MR;
}*/
/*void ina3221_setCritAlertLatchAble(ina3221_t *d, uint8_t enable) {
masken_reg_t MR;
ina3221_read(d, INA3221_REG_MASK_ENABLE, (uint16_t*)&MR);
MR.crit_alert_latch_en = (enable)?1:0;
ina3221_write(d, INA3221_REG_MASK_ENABLE, (uint16_t*)&MR);
d->_masken_reg = MR;
}*/
/*uint16_t ina3221_readFlags(ina3221_t *d) {
masken_reg_t MR;
ina3221_read(d, INA3221_REG_MASK_ENABLE, (uint16_t*)&MR);
d->_masken_reg = MR;
return *(uint16_t*)&MR;
}*/
/*uint8_t ina3221_getTimingCtrlAlertFlag(ina3221_t *d) {
return (uint8_t)d->_masken_reg.timing_ctrl_alert;
}
uint8_t ina3221_getPwrValidAlertFlag(ina3221_t *d) {
return (uint8_t)d->_masken_reg.pwr_valid_alert;
}
uint8_t ina3221_getCurrentSumAlertFlag(ina3221_t *d) {
return (uint8_t)d->_masken_reg.shunt_sum_alert;
}
uint8_t ina3221_getConversionReadyFlag(ina3221_t *d) {
return (uint8_t)d->_masken_reg.conv_ready;
}*/
uint16_t ina3221_getManufID(ina3221_t *d) {
return ina3221_read16(d, INA3221_REG_MANUF_ID);
}
uint16_t ina3221_getDieID(ina3221_t *d) {
return ina3221_read16(d, INA3221_REG_DIE_ID);
}
void ina3221_setChannelAble(ina3221_t *d, uint8_t channel, uint8_t enable) {
conf_reg_t CR;
ina3221_read(d, INA3221_REG_CONF, (uint16_t*)&CR);
enable = (enable)?1:0;
switch (channel) {
case INA3221_CH1: CR.ch1_en = enable; break;
case INA3221_CH2: CR.ch2_en = enable; break;
case INA3221_CH3: CR.ch3_en = enable; break;
}
ina3221_write(d, INA3221_REG_CONF, (uint16_t*)&CR);
}
/*void ina3221_setWarnAlertShuntLimit(ina3221_t *d, uint8_t channel, int32_t voltageuV) {
int16_t val = voltageuV / 5; uint16_t *pT = (uint16_t*)&val;
switch (channel) {
case INA3221_CH1: ina3221_write16(d, INA3221_REG_CH1_WARNING_ALERT_LIM, *pT); break;
case INA3221_CH2: ina3221_write16(d, INA3221_REG_CH2_WARNING_ALERT_LIM, *pT); break;
case INA3221_CH3: ina3221_write16(d, INA3221_REG_CH3_WARNING_ALERT_LIM, *pT); break;
}
}
void ina3221_setCritAlertShuntLimit(ina3221_t *d, uint8_t channel, int32_t voltageuV) {
int16_t val = voltageuV / 5; uint16_t *pT = (uint16_t*)&val;
switch (channel) {
case INA3221_CH1: ina3221_write16(d, INA3221_REG_CH1_CRIT_ALERT_LIM, *pT); break;
case INA3221_CH2: ina3221_write16(d, INA3221_REG_CH2_CRIT_ALERT_LIM, *pT); break;
case INA3221_CH3: ina3221_write16(d, INA3221_REG_CH3_CRIT_ALERT_LIM, *pT); break;
}
}
void ina3221_setWarnAlertCurrentLimit(ina3221_t *d, uint8_t channel,
int32_t currentmA) {
int32_t shuntuV = currentmA * (int32_t)d->_shuntRes[channel];
ina3221_setWarnAlertShuntLimit(d, channel, shuntuV);
}
void ina3221_setCritAlertCurrentLimit(ina3221_t *d, uint8_t channel,
int32_t currentmA) {
int32_t shuntuV = currentmA * (int32_t)d->_shuntRes[channel];
ina3221_setCritAlertShuntLimit(d, channel, shuntuV);
}*/
void ina3221_setCurrentSumAble(ina3221_t *d, uint8_t channel, uint8_t enable) {
masken_reg_t MR;
ina3221_read(d, INA3221_REG_MASK_ENABLE, (uint16_t*)&MR);
enable = (enable)?1:0;
switch (channel) {
case INA3221_CH1: MR.shunt_sum_en_ch1 = enable; break;
case INA3221_CH2: MR.shunt_sum_en_ch2 = enable; break;
case INA3221_CH3: MR.shunt_sum_en_ch3 = enable; break;
}
ina3221_write(d, INA3221_REG_MASK_ENABLE, (uint16_t*)&MR);
d->_masken_reg = MR;
}
int16_t ina3221_getShuntVolRaw(ina3221_t *d, uint8_t channel) { //40uV per bit, -32768 ~ +32760
int16_t v = -1;
switch (channel) { //40uV per bit
case INA3221_CH1: ina3221_read(d, INA3221_REG_CH1_SHUNTV, (uint16_t*)&v); break;
case INA3221_CH2: ina3221_read(d, INA3221_REG_CH2_SHUNTV, (uint16_t*)&v); break;
case INA3221_CH3: ina3221_read(d, INA3221_REG_CH3_SHUNTV, (uint16_t*)&v); break;
}
return v;
}
/*int32_t ina3221_getShuntVoltage(ina3221_t *d, uint8_t channel) { //by mV, -163840 ~ +163800
int32_t res;
//int16_t val_raw = 0;
//switch (channel) {
// case INA3221_CH1: ina3221_read(d, INA3221_REG_CH1_SHUNTV, (uint16_t*)&val_raw); break;
// case INA3221_CH2: ina3221_read(d, INA3221_REG_CH2_SHUNTV, (uint16_t*)&val_raw); break;
// case INA3221_CH3: ina3221_read(d, INA3221_REG_CH3_SHUNTV, (uint16_t*)&val_raw); break;
//}
//res = (int32_t)(val_raw >> 3) * 40;
res = (int32_t)ina3221_getShuntVolRaw(d, channel);
res += res << 2;
return res;
}*/
/*uint8_t ina3221_getWarnAlertFlag(ina3221_t *d, uint8_t channel) {
switch (channel) {
case INA3221_CH1: return (uint8_t)d->_masken_reg.warn_alert_ch1; break;
case INA3221_CH2: return (uint8_t)d->_masken_reg.warn_alert_ch2; break;
case INA3221_CH3: return (uint8_t)d->_masken_reg.warn_alert_ch3; break;
default: return 0;
}
}
uint8_t ina3221_getCritAlertFlag(ina3221_t *d, uint8_t channel) {
switch (channel) {
case INA3221_CH1: return (uint8_t)d->_masken_reg.crit_alert_ch1; break;
case INA3221_CH2: return (uint8_t)d->_masken_reg.crit_alert_ch2; break;
case INA3221_CH3: return (uint8_t)d->_masken_reg.crit_alert_ch3; break;
default: return 0;
}
}*/
//mV, low-3bit always 0
int16_t ina3221_getVol_Raw(ina3221_t *d, uint8_t channel) { //mV, low-3bit always 0
int16_t v = 32767;
if(d->pDev == NULL) return -1;
switch (channel) { //just get, no shift.
case INA3221_CH1: ina3221_read(d, INA3221_REG_CH1_BUSV, (uint16_t*)&v); break;
case INA3221_CH2: ina3221_read(d, INA3221_REG_CH2_BUSV, (uint16_t*)&v); break;
case INA3221_CH3: ina3221_read(d, INA3221_REG_CH3_BUSV, (uint16_t*)&v); break;
}
return v;
}
#ifdef use_float
int32_t ina3221_estimateOffsetVoltage(ina3221_t *d, uint8_t channel, uint32_t busV) {
float bias_in = 10.0; // Input bias current at IN– in uA
float r_in = 0.670; // Input resistance at IN– in MOhm
uint32_t adc_step = 40; // smallest shunt ADC step in uV
float shunt_res = d->_shuntRes[channel] / 1000.0; // convert to Ohm
float filter_res = d->_filterRes[channel];
int32_t offset = 0.0;
float reminder;
offset = (shunt_res + filter_res) * (busV / r_in + bias_in) -
bias_in * filter_res;
// Round the offset to the closest shunt ADC value
reminder = offset % adc_step;
if (reminder < adc_step / 2) {
offset -= reminder;
} else {
offset += adc_step - reminder;
}
return offset;
}
float ina3221_getCurrent(ina3221_t *d, uint8_t channel) { //by A
//int32_t shunt_uV = 0;
float current_A;
//shunt_uV = ina3221_getShuntVoltage(d, channel);
//current_A = shunt_uV / 1000.0 / (int32_t)d->_shuntRes[channel];
current_A = (float)ina3221_getShuntVoltage(d, channel);
current_A /= (1000.0f * (float)d->_shuntRes[channel]);
return current_A;
}
float ina3221_getVoltage(ina3221_t *d, uint8_t channel) { //by V
float voltage_V = 0.0;
//int16_t val_raw = 0;
//switch (channel) {
// case INA3221_CH1: ina3221_read(d, INA3221_REG_CH1_BUSV, (uint16_t*)&val_raw); break;
// case INA3221_CH2: ina3221_read(d, INA3221_REG_CH2_BUSV, (uint16_t*)&val_raw); break;
// case INA3221_CH3: ina3221_read(d, INA3221_REG_CH3_BUSV, (uint16_t*)&val_raw); break;
//}
//voltage_V = (float)(val_raw);
voltage_V = (float)ina3221_getVol_Raw(d, channel);
voltage_V /= 1000.0f;
return voltage_V;
}
float ina3221_getCurrentCompensated(ina3221_t *d, uint8_t channel) {
int32_t shunt_uV = 0;
int32_t bus_V = 0;
float current_A = 0.0;
int32_t offset_uV = 0;
shunt_uV = ina3221_getShuntVoltage(d, channel);
bus_V = ina3221_getVoltage(d, channel);
offset_uV = ina3221_estimateOffsetVoltage(d, channel, bus_V);
current_A = (shunt_uV - offset_uV) / (int32_t)d->_shuntRes[channel] / 1000.0;
return current_A;
}
#endif //use_float
int16_t ina3221_getCur_mA(ina3221_t *d, uint8_t channel) {
int32_t val;
if(d->pDev == NULL) return -1;
//switch (channel) {
// case INA3221_CH1: ina3221_read(d, INA3221_REG_CH1_SHUNTV, (uint16_t*)&val); break;
// case INA3221_CH2: ina3221_read(d, INA3221_REG_CH2_SHUNTV, (uint16_t*)&val); break;
// case INA3221_CH3: ina3221_read(d, INA3221_REG_CH3_SHUNTV, (uint16_t*)&val); break;
//}
val = ina3221_getShuntVolRaw(d, channel);
val += val << 2;
val /= d->_shuntRes[channel];
return (int16_t)val;
}
#ifdef INA3221_AVGVAL
int32_t ina3221_getAvgVol(ina3221_t *d, uint8_t channel) {
AvgVal_t *t;
int16_t wVol;
if(!d || !d->pDev || channel > 2 || channel < 0) return 0;
wVol = ina3221_getVol_Raw(d, channel) >> 2;
t = &(d->avg[channel]);
t->VolAvg -= (int32_t)t->VolTab[t->vidx];
t->VolTab[t->vidx] = wVol;
t->VolAvg += (int32_t)wVol;
(t->vidx)++;
if(t->vidx >= VolTabNum) t->vidx = 0;
return (t->VolAvg >> ScaleShift);
}
int32_t ina3221_getAvgCur(ina3221_t *d, uint8_t channel) { //channel0-2
AvgVal_t *t;
int32_t wCur;
if(!d || !d->pDev || channel > 2 || channel < 0) return 0;
//when shunt-res=0.04ohm, wCur=ina3221_getShuntVolRaw*5/40=ina3221_getShuntVolRaw/8
#ifdef use_shuntres
wCur = ina3221_getShuntVolRaw(d, channel);
wCur += wCur << 2;
wCur /= d->_shuntRes[channel];
#else
//wCur = ina3221_getShuntVolRaw(d, channel) >> 3; //2 for 20mOhm, 3 for 40mOhm
wCur = ina3221_getShuntVolRaw(d, channel) >> 3;
#endif
t = &(d->avg[channel]);
t->CurAvg -= (int32_t)t->CurTab[t->cidx];
t->CurTab[t->cidx] = wCur;
t->CurAvg += (int32_t)wCur;
(t->cidx)++;
if(t->cidx >= CurTabNum) t->cidx = 0;
return (t->CurAvg >> ScaleShift);
}
#endif
int32_t ina3221_getCurPower(ina3221_t *d, uint8_t channel) { //mW
if(d->pDev == NULL) return -1;
d->voltage[channel] = ina3221_getVol_Raw(d, channel);
d->curcuit[channel] = ina3221_getCur_mA(d, channel);
d->power[channel] = (d->voltage[channel] * d->curcuit[channel]) / 1000;
return d->power[channel];
}