-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathBeastdevices_INA3221.cpp
517 lines (411 loc) · 13.8 KB
/
Beastdevices_INA3221.cpp
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*
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 "Beastdevices_INA3221.h"
void Beastdevices_INA3221::_read(ina3221_reg_t reg, uint16_t *val) {
_i2c->beginTransmission(_i2c_addr);
_i2c->write(reg); // Register
_i2c->endTransmission();
_i2c->requestFrom(_i2c_addr, (uint8_t)2);
if (_i2c->available())
{
*val = ((_i2c->read() << 8) | _i2c->read());
}
}
void Beastdevices_INA3221::_write(ina3221_reg_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();
}
void Beastdevices_INA3221::begin(TwoWire *theWire) {
_i2c = theWire;
_shuntRes[0] = 10;
_shuntRes[1] = 10;
_shuntRes[2] = 10;
_filterRes[0] = 0;
_filterRes[1] = 0;
_filterRes[2] = 0;
_i2c->begin();
}
void Beastdevices_INA3221::setShuntRes(uint32_t res_ch1, uint32_t res_ch2, uint32_t res_ch3) {
_shuntRes[0] = res_ch1;
_shuntRes[1] = res_ch2;
_shuntRes[2] = res_ch3;
}
void Beastdevices_INA3221::setFilterRes(uint32_t res_ch1, uint32_t res_ch2, uint32_t res_ch3) {
_filterRes[0] = res_ch1;
_filterRes[1] = res_ch2;
_filterRes[2] = res_ch3;
}
uint16_t Beastdevices_INA3221::getReg(ina3221_reg_t reg){
uint16_t val = 0;
_read(reg, &val);
return val;
}
void Beastdevices_INA3221::reset(){
conf_reg_t conf_reg;
_read(INA3221_REG_CONF, (uint16_t*)&conf_reg);
conf_reg.reset = 1;
_write(INA3221_REG_CONF, (uint16_t*)&conf_reg);
}
void Beastdevices_INA3221::setModePowerDown(){
conf_reg_t conf_reg;
_read(INA3221_REG_CONF, (uint16_t*)&conf_reg);
conf_reg.mode_bus_en = 0;
conf_reg.mode_continious_en =0 ;
_write(INA3221_REG_CONF, (uint16_t*)&conf_reg);
}
void Beastdevices_INA3221::setModeContinious(){
conf_reg_t conf_reg;
_read(INA3221_REG_CONF, (uint16_t*)&conf_reg);
conf_reg.mode_continious_en =1;
_write(INA3221_REG_CONF, (uint16_t*)&conf_reg);
}
void Beastdevices_INA3221::setModeTriggered(){
conf_reg_t conf_reg;
_read(INA3221_REG_CONF, (uint16_t*)&conf_reg);
conf_reg.mode_continious_en = 0;
_write(INA3221_REG_CONF, (uint16_t*)&conf_reg);
}
void Beastdevices_INA3221::setShuntMeasEnable(){
conf_reg_t conf_reg;
_read(INA3221_REG_CONF, (uint16_t*)&conf_reg);
conf_reg.mode_shunt_en = 1;
_write(INA3221_REG_CONF, (uint16_t*)&conf_reg);
}
void Beastdevices_INA3221::setShuntMeasDisable(){
conf_reg_t conf_reg;
_read(INA3221_REG_CONF, (uint16_t*)&conf_reg);
conf_reg.mode_shunt_en = 0;
_write(INA3221_REG_CONF, (uint16_t*)&conf_reg);
}
void Beastdevices_INA3221::setBusMeasEnable(){
conf_reg_t conf_reg;
_read(INA3221_REG_CONF, (uint16_t*)&conf_reg);
conf_reg.mode_bus_en = 1;
_write(INA3221_REG_CONF, (uint16_t*)&conf_reg);
}
void Beastdevices_INA3221::setBusMeasDisable(){
conf_reg_t conf_reg;
_read(INA3221_REG_CONF, (uint16_t*)&conf_reg);
conf_reg.mode_bus_en = 0;
_write(INA3221_REG_CONF, (uint16_t*)&conf_reg);
}
void Beastdevices_INA3221::setAveragingMode(ina3221_avg_mode_t mode) {
conf_reg_t conf_reg;
_read(INA3221_REG_CONF, (uint16_t*)&conf_reg);
conf_reg.avg_mode = mode;
_write(INA3221_REG_CONF, (uint16_t*)&conf_reg);
}
void Beastdevices_INA3221::setBusConversionTime(ina3221_conv_time_t convTime) {
conf_reg_t conf_reg;
_read(INA3221_REG_CONF, (uint16_t*)&conf_reg);
conf_reg.bus_conv_time = convTime;
_write(INA3221_REG_CONF, (uint16_t*)&conf_reg);
}
void Beastdevices_INA3221::setShuntConversionTime(ina3221_conv_time_t convTime) {
conf_reg_t conf_reg;
_read(INA3221_REG_CONF, (uint16_t*)&conf_reg);
conf_reg.shunt_conv_time = convTime;
_write(INA3221_REG_CONF, (uint16_t*)&conf_reg);
}
void Beastdevices_INA3221::setPwrValidUpLimit(int16_t voltagemV) {
_write(INA3221_REG_PWR_VALID_HI_LIM, (uint16_t*)&voltagemV);
}
void Beastdevices_INA3221::setPwrValidLowLimit(int16_t voltagemV) {
_write(INA3221_REG_PWR_VALID_LO_LIM, (uint16_t*)&voltagemV);
}
void Beastdevices_INA3221::setShuntSumAlertLimit(int32_t voltageuV) {
int16_t val = 0;
val = voltageuV / 20;
_write(INA3221_REG_SHUNTV_SUM_LIM, (uint16_t*)&val);
}
void Beastdevices_INA3221::setCurrentSumAlertLimit(int32_t currentmA) {
int16_t shuntuV = 0;
shuntuV = currentmA*(int32_t)_shuntRes[INA3221_CH1];
setShuntSumAlertLimit(shuntuV);
}
void Beastdevices_INA3221::setWarnAlertLatchEnable() {
masken_reg_t masken_reg;
_read(INA3221_REG_MASK_ENABLE, (uint16_t*)&masken_reg);
masken_reg.warn_alert_latch_en = 1;
_write(INA3221_REG_MASK_ENABLE, (uint16_t*)&masken_reg);
_masken_reg = masken_reg;
}
void Beastdevices_INA3221::setWarnAlertLatchDisable() {
masken_reg_t masken_reg;
_read(INA3221_REG_MASK_ENABLE, (uint16_t*)&masken_reg);
masken_reg.warn_alert_latch_en = 1;
_write(INA3221_REG_MASK_ENABLE, (uint16_t*)&masken_reg);
_masken_reg = masken_reg;
}
void Beastdevices_INA3221::setCritAlertLatchEnable() {
masken_reg_t masken_reg;
_read(INA3221_REG_MASK_ENABLE, (uint16_t*)&masken_reg);
masken_reg.crit_alert_latch_en = 1;
_write(INA3221_REG_MASK_ENABLE, (uint16_t*)&masken_reg);
_masken_reg = masken_reg;
}
void Beastdevices_INA3221::setCritAlertLatchDisable() {
masken_reg_t masken_reg;
_read(INA3221_REG_MASK_ENABLE, (uint16_t*)&masken_reg);
masken_reg.crit_alert_latch_en = 1;
_write(INA3221_REG_MASK_ENABLE, (uint16_t*)&masken_reg);
_masken_reg = masken_reg;
}
void Beastdevices_INA3221::readFlags() {
_read(INA3221_REG_MASK_ENABLE, (uint16_t*)&_masken_reg);
}
bool Beastdevices_INA3221::getTimingCtrlAlertFlag(){
return _masken_reg.timing_ctrl_alert;
}
bool Beastdevices_INA3221::getPwrValidAlertFlag(){
return _masken_reg.pwr_valid_alert;
}
bool Beastdevices_INA3221::getCurrentSumAlertFlag(){
return _masken_reg.shunt_sum_alert;
}
bool Beastdevices_INA3221::getConversionReadyFlag(){
return _masken_reg.conv_ready;
}
uint16_t Beastdevices_INA3221::getManufID() {
uint16_t id = 0;
_read(INA3221_REG_MANUF_ID, &id);
return id;
}
uint16_t Beastdevices_INA3221::getDieID() {
uint16_t id = 0;
_read(INA3221_REG_DIE_ID, &id);
return id;
}
void Beastdevices_INA3221::setChannelEnable(ina3221_ch_t channel) {
conf_reg_t conf_reg;
_read(INA3221_REG_CONF, (uint16_t*)&conf_reg);
switch(channel){
case INA3221_CH1:
conf_reg.ch1_en = 1;
break;
case INA3221_CH2:
conf_reg.ch2_en = 1;
break;
case INA3221_CH3:
conf_reg.ch3_en = 1;
break;
}
_write(INA3221_REG_CONF, (uint16_t*)&conf_reg);
}
void Beastdevices_INA3221::setChannelDisable(ina3221_ch_t channel) {
conf_reg_t conf_reg;
_read(INA3221_REG_CONF, (uint16_t*)&conf_reg);
switch(channel){
case INA3221_CH1:
conf_reg.ch1_en = 0;
break;
case INA3221_CH2:
conf_reg.ch2_en = 0;
break;
case INA3221_CH3:
conf_reg.ch3_en = 0;
break;
}
_write(INA3221_REG_CONF, (uint16_t*)&conf_reg);
}
void Beastdevices_INA3221::setWarnAlertShuntLimit(ina3221_ch_t channel, int32_t voltageuV) {
ina3221_reg_t reg;
int16_t val = 0;
switch(channel){
case INA3221_CH1:
reg = INA3221_REG_CH1_WARNING_ALERT_LIM;
break;
case INA3221_CH2:
reg = INA3221_REG_CH2_WARNING_ALERT_LIM;
break;
case INA3221_CH3:
reg = INA3221_REG_CH3_WARNING_ALERT_LIM;
break;
}
val = voltageuV / 5;
_write(reg, (uint16_t*)&val);
}
void Beastdevices_INA3221::setCritAlertShuntLimit(ina3221_ch_t channel, int32_t voltageuV) {
ina3221_reg_t reg;
int16_t val = 0;
switch(channel){
case INA3221_CH1:
reg = INA3221_REG_CH1_CRIT_ALERT_LIM;
break;
case INA3221_CH2:
reg = INA3221_REG_CH2_CRIT_ALERT_LIM;
break;
case INA3221_CH3:
reg = INA3221_REG_CH3_CRIT_ALERT_LIM;
break;
}
val = voltageuV / 5;
_write(reg, (uint16_t*)&val);
}
void Beastdevices_INA3221::setWarnAlertCurrentLimit(ina3221_ch_t channel, int32_t currentmA)
{
int32_t shuntuV = 0;
shuntuV = currentmA*(int32_t)_shuntRes[channel];
setWarnAlertShuntLimit(channel, shuntuV);
}
void Beastdevices_INA3221::setCritAlertCurrentLimit(ina3221_ch_t channel, int32_t currentmA)
{
int32_t shuntuV = 0;
shuntuV = currentmA*(int32_t)_shuntRes[channel];
setCritAlertShuntLimit(channel, shuntuV);
}
void Beastdevices_INA3221::setCurrentSumEnable(ina3221_ch_t channel) {
masken_reg_t masken_reg;
_read(INA3221_REG_MASK_ENABLE, (uint16_t*)&masken_reg);
switch(channel){
case INA3221_CH1:
masken_reg.shunt_sum_en_ch1 = 1;
break;
case INA3221_CH2:
masken_reg.shunt_sum_en_ch2 = 1;
break;
case INA3221_CH3:
masken_reg.shunt_sum_en_ch3 = 1;
break;
}
_write(INA3221_REG_MASK_ENABLE, (uint16_t*)&masken_reg);
_masken_reg = masken_reg;
}
void Beastdevices_INA3221::setCurrentSumDisable(ina3221_ch_t channel) {
masken_reg_t masken_reg;
_read(INA3221_REG_MASK_ENABLE, (uint16_t*)&masken_reg);
switch(channel){
case INA3221_CH1:
masken_reg.shunt_sum_en_ch1 = 0;
break;
case INA3221_CH2:
masken_reg.shunt_sum_en_ch2 = 0;
break;
case INA3221_CH3:
masken_reg.shunt_sum_en_ch3 = 0;
break;
}
_write(INA3221_REG_MASK_ENABLE, (uint16_t*)&masken_reg);
_masken_reg = masken_reg;
}
int32_t Beastdevices_INA3221::getShuntVoltage(ina3221_ch_t channel) {
int32_t res;
ina3221_reg_t reg;
uint16_t val_raw = 0;
switch(channel){
case INA3221_CH1:
reg = INA3221_REG_CH1_SHUNTV;
break;
case INA3221_CH2:
reg = INA3221_REG_CH2_SHUNTV;
break;
case INA3221_CH3:
reg = INA3221_REG_CH3_SHUNTV;
break;
}
_read(reg, &val_raw);
res = (int16_t)val_raw;
res *= 5; // 1 LSB = 5uV
return res;
}
bool Beastdevices_INA3221::getWarnAlertFlag(ina3221_ch_t channel) {
switch(channel){
case INA3221_CH1:
return _masken_reg.warn_alert_ch1;
case INA3221_CH2:
return _masken_reg.warn_alert_ch2;
case INA3221_CH3:
return _masken_reg.warn_alert_ch3;
default:
return -1;
}
}
bool Beastdevices_INA3221::getCritAlertFlag(ina3221_ch_t channel) {
switch(channel){
case INA3221_CH1:
return _masken_reg.crit_alert_ch1;
case INA3221_CH2:
return _masken_reg.crit_alert_ch2;
case INA3221_CH3:
return _masken_reg.crit_alert_ch3;
default:
return -1;
}
}
int32_t Beastdevices_INA3221::estimateOffsetVoltage(ina3221_ch_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 = _shuntRes[channel]/1000.0; // convert to Ohm
float filter_res = _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 Beastdevices_INA3221::getCurrent(ina3221_ch_t channel) {
int32_t shunt_uV = 0;
float current_A = 0;
shunt_uV = getShuntVoltage(channel);
current_A = shunt_uV / (int32_t)_shuntRes[channel] / 1000.0;
return current_A;
}
float Beastdevices_INA3221::getCurrentCompensated(ina3221_ch_t channel) {
int32_t shunt_uV = 0;
int32_t bus_V = 0;
float current_A = 0.0;
int32_t offset_uV = 0;
shunt_uV = getShuntVoltage(channel);
bus_V = getVoltage(channel);
offset_uV = estimateOffsetVoltage(channel, bus_V);
current_A = (shunt_uV - offset_uV) / (int32_t)_shuntRes[channel] / 1000.0;
return current_A;
}
float Beastdevices_INA3221::getVoltage(ina3221_ch_t channel) {
float voltage_V = 0.0;
ina3221_reg_t reg;
uint16_t val_raw = 0;
switch(channel){
case INA3221_CH1:
reg = INA3221_REG_CH1_BUSV;
break;
case INA3221_CH2:
reg = INA3221_REG_CH2_BUSV;
break;
case INA3221_CH3:
reg = INA3221_REG_CH3_BUSV;
break;
}
_read(reg, &val_raw);
voltage_V = val_raw / 1000.0;
return voltage_V;
}