-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathINA228.cpp
More file actions
686 lines (590 loc) · 15.6 KB
/
Copy pathINA228.cpp
File metadata and controls
686 lines (590 loc) · 15.6 KB
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
// FILE: INA228.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.4.1
// DATE: 2024-05-09
// PURPOSE: Arduino library for the INA228, I2C, 20 bit, voltage, current and power sensor.
// URL: https://github.com/RobTillaart/INA228
// https://www.adafruit.com/product/5832 ( 10 A version)
// https://www.mateksys.com/?portfolio=i2c-ina-bm (200 A version))
//
// Read the datasheet for the details
#include "INA228.h"
// REGISTERS ADDRESS BITS RW
#define INA228_CONFIG 0x00 // 16 RW
#define INA228_ADC_CONFIG 0x01 // 16 RW
#define INA228_SHUNT_CAL 0x02 // 16 RW
#define INA228_SHUNT_TEMP_CO 0x03 // 16 RW
#define INA228_SHUNT_VOLTAGE 0x04 // 24 R-
#define INA228_BUS_VOLTAGE 0x05 // 24 R-
#define INA228_TEMPERATURE 0x06 // 16 R-
#define INA228_CURRENT 0x07 // 24 R-
#define INA228_POWER 0x08 // 24 R-
#define INA228_ENERGY 0x09 // 40 R-
#define INA228_CHARGE 0x0A // 40 R-
#define INA228_DIAG_ALERT 0x0B // 16 RW
#define INA228_SOVL 0x0C // 16 RW
#define INA228_SUVL 0x0D // 16 RW
#define INA228_BOVL 0x0E // 16 RW
#define INA228_BUVL 0x0F // 16 RW
#define INA228_TEMP_LIMIT 0x10 // 16 RW
#define INA228_POWER_LIMIT 0x11 // 16 RW
#define INA228_MANUFACTURER 0x3E // 16 R-
#define INA228_DEVICE_ID 0x3F // 16 R-
// CONFIG MASKS (register 0)
#define INA228_CFG_RST 0x8000
#define INA228_CFG_RSTACC 0x4000
#define INA228_CFG_CONVDLY 0x3FC0
#define INA228_CFG_TEMPCOMP 0x0020
#define INA228_CFG_ADCRANGE 0x0010
#define INA228_CFG_RESERVED 0x000F // all unused bits
// ADC MASKS (register 1)
#define INA228_ADC_MODE 0xF000
#define INA228_ADC_VBUSCT 0x0E00
#define INA228_ADC_VSHCT 0x01C0
#define INA228_ADC_VTCT 0x0038
#define INA228_ADC_AVG 0x0007
////////////////////////////////////////////////////////
//
// CONSTRUCTOR
//
INA228::INA228(const uint8_t address, TwoWire *wire)
{
_address = address;
_wire = wire;
// no calibrated values by default.
_shunt = 0.015;
_maxCurrent = 10.0;
_current_LSB = _maxCurrent * pow(2, -19);
_error = 0;
}
bool INA228::begin()
{
if (! isConnected()) return false;
getADCRange();
return true;
}
bool INA228::isConnected()
{
_wire->beginTransmission(_address);
return ( _wire->endTransmission() == 0);
}
uint8_t INA228::getAddress()
{
return _address;
}
////////////////////////////////////////////////////////
//
// CORE FUNCTIONS
//
// PAGE 25
float INA228::getBusVoltage()
{
// always positive, remove reserved bits.
int32_t value = _readRegister(INA228_BUS_VOLTAGE, 3) >> 4;
float bus_LSB = 195.3125e-6; // 195.3125 uV
float voltage = value * bus_LSB;
return voltage;
}
// PAGE 25
float INA228::getShuntVoltage()
{
// shunt_LSB depends on ADCRANGE in INA228_CONFIG register.
float shunt_LSB = 312.5e-9; // 312.5 nV
if (_ADCRange == true)
{
shunt_LSB = 78.125e-9; // 78.125 nV
}
// remove reserved bits.
int32_t value = _readRegister(INA228_SHUNT_VOLTAGE, 3) >> 4;
// handle negative values (20 bit)
if (value & 0x00080000)
{
value |= 0xFFF00000;
}
float voltage = value * shunt_LSB;
return voltage;
}
int32_t INA228::getShuntVoltageRAW()
{
// remove reserved bits.
uint32_t value = _readRegister(INA228_SHUNT_VOLTAGE, 3) >> 4;
// handle negative values (20 bit)
if (value & 0x00080000)
{
value |= 0xFFF00000;
}
return (int32_t)value;
}
// PAGE 25 + 8.1.2
float INA228::getCurrent()
{
// remove reserved bits.
int32_t value = _readRegister(INA228_CURRENT, 3) >> 4;
// handle negative values (20 bit)
if (value & 0x00080000)
{
value |= 0xFFF00000;
}
float current = value * _current_LSB;
return current;
}
// PAGE 26 + 8.1.2
float INA228::getPower()
{
uint32_t value = _readRegister(INA228_POWER, 3);
// PAGE 31 (8.1.2)
return value * 3.2 * _current_LSB;
}
// PAGE 25
float INA228::getTemperature()
{
uint32_t value = _readRegister(INA228_TEMPERATURE, 2);
float LSB = 7.8125e-3; // milli degree Celsius
return value * LSB;
}
// PAGE 26 + 8.1.2
double INA228::getEnergy()
{
// read 40 bit UNSIGNED as a double to prevent 64 bit integers
// double might be 8 or 4 byte, depends on platform
// 40 bit ==> O(10^12)
double value = _readRegisterF(INA228_ENERGY, 'U');
// PAGE 31 (8.1.2)
return value * (16 * 3.2) * _current_LSB;
}
// PAGE 26 + 8.1.2
double INA228::getCharge()
{
// read 40 bit SIGNED as a float to prevent 64 bit integers
// double might be 8 or 4 byte, depends on platform
// 40 bit ==> O(10^12)
double value = _readRegisterF(INA228_CHARGE, 'S');
// PAGE 32 (8.1.2)
return value * _current_LSB;
}
////////////////////////////////////////////////////////
//
// CONFIG REGISTER 0
//
void INA228::reset()
{
uint16_t value = _readRegister(INA228_CONFIG, 2);
value |= INA228_CFG_RST;
_writeRegister(INA228_CONFIG, value);
}
bool INA228::setAccumulation(uint8_t value)
{
if (value > 1) return false;
uint16_t reg = _readRegister(INA228_CONFIG, 2);
if (value == 1) reg |= INA228_CFG_RSTACC;
else reg &= ~INA228_CFG_RSTACC;
_writeRegister(INA228_CONFIG, reg);
return true;
}
bool INA228::getAccumulation()
{
uint16_t value = _readRegister(INA228_CONFIG, 2);
return (value & INA228_CFG_RSTACC) > 0;
}
void INA228::setConversionDelay(uint8_t steps)
{
uint16_t value = _readRegister(INA228_CONFIG, 2);
value &= ~INA228_CFG_CONVDLY;
value |= (steps << 6);
_writeRegister(INA228_CONFIG, value);
}
uint8_t INA228::getConversionDelay()
{
uint16_t value = _readRegister(INA228_CONFIG, 2);
return (value >> 6) & 0xFF;
}
void INA228::setTemperatureCompensation(bool on)
{
uint16_t value = _readRegister(INA228_CONFIG, 2);
if (on) value |= INA228_CFG_TEMPCOMP;
else value &= ~INA228_CFG_TEMPCOMP;
_writeRegister(INA228_CONFIG, value);
}
bool INA228::getTemperatureCompensation()
{
uint16_t value = _readRegister(INA228_CONFIG, 2);
return (value & INA228_CFG_TEMPCOMP) > 0;
}
bool INA228::setADCRange(bool flag)
{
uint16_t value = _readRegister(INA228_CONFIG, 2);
_ADCRange = (value & INA228_CFG_ADCRANGE) > 0;
// nothing changed ==> we're done.
if (flag == _ADCRange) return true;
_ADCRange = flag;
if (flag) value |= INA228_CFG_ADCRANGE;
else value &= ~INA228_CFG_ADCRANGE;
_writeRegister(INA228_CONFIG, value);
// Fix #26, issue where shunt_cal was not modified
bool rv = setMaxCurrentShunt(getMaxCurrent(), getShunt()) == 0;
return rv;
}
bool INA228::getADCRange()
{
uint16_t value = _readRegister(INA228_CONFIG, 2);
_ADCRange = (value & INA228_CFG_ADCRANGE) > 0;
return _ADCRange;
}
////////////////////////////////////////////////////////
//
// CONFIG ADC REGISTER 1
//
bool INA228::setMode(uint8_t mode)
{
if (mode > 0x0F) return false;
uint16_t value = _readRegister(INA228_ADC_CONFIG, 2);
value &= ~INA228_ADC_MODE;
value |= (mode << 12);
_writeRegister(INA228_ADC_CONFIG, value);
return true;
}
uint8_t INA228::getMode()
{
uint16_t value = _readRegister(INA228_ADC_CONFIG, 2);
return (value & INA228_ADC_MODE) >> 12;
}
bool INA228::setBusVoltageConversionTime(uint8_t bvct)
{
if (bvct > 7) return false;
uint16_t value = _readRegister(INA228_ADC_CONFIG, 2);
value &= ~INA228_ADC_VBUSCT;
value |= (bvct << 9);
_writeRegister(INA228_ADC_CONFIG, value);
return true;
}
uint8_t INA228::getBusVoltageConversionTime()
{
uint16_t value = _readRegister(INA228_ADC_CONFIG, 2);
return (value & INA228_ADC_VBUSCT) >> 9;
}
bool INA228::setShuntVoltageConversionTime(uint8_t svct)
{
if (svct > 7) return false;
uint16_t value = _readRegister(INA228_ADC_CONFIG, 2);
value &= ~INA228_ADC_VSHCT;
value |= (svct << 6);
_writeRegister(INA228_ADC_CONFIG, value);
return true;
}
uint8_t INA228::getShuntVoltageConversionTime()
{
uint16_t value = _readRegister(INA228_ADC_CONFIG, 2);
return (value & INA228_ADC_VSHCT) >> 6;
}
bool INA228::setTemperatureConversionTime(uint8_t tct)
{
if (tct > 7) return false;
uint16_t value = _readRegister(INA228_ADC_CONFIG, 2);
value &= ~INA228_ADC_VTCT;
value |= (tct << 3);
_writeRegister(INA228_ADC_CONFIG, value);
return true;
}
uint8_t INA228::getTemperatureConversionTime()
{
uint16_t value = _readRegister(INA228_ADC_CONFIG, 2);
return (value & INA228_ADC_VTCT) >> 3;
}
bool INA228::setAverage(uint8_t avg)
{
if (avg > 7) return false;
uint16_t value = _readRegister(INA228_ADC_CONFIG, 2);
value &= ~INA228_ADC_AVG;
value |= avg;
_writeRegister(INA228_ADC_CONFIG, value);
return true;
}
uint8_t INA228::getAverage()
{
uint16_t value = _readRegister(INA228_ADC_CONFIG, 2);
return (value & INA228_ADC_AVG);
}
////////////////////////////////////////////////////////
//
// SHUNT CALIBRATION REGISTER 2
//
int INA228::setMaxCurrentShunt(float maxCurrent, float shunt)
{
// Shunt can be really small
if (shunt < 0.0001) return -2; // TODO error code
if (maxCurrent < 0.0) return -3; // TODO error code
_maxCurrent = maxCurrent;
_shunt = shunt;
_current_LSB = _maxCurrent * 1.9073486328125e-6; // pow(2, -19);
// PAGE 31 (8.1.2)
float shunt_cal = 13107.2e6 * _current_LSB * _shunt;
// depends on ADCRANGE in INA228_CONFIG register.
if (_ADCRange == true)
{
shunt_cal *= 4;
}
// shunt_cal must be written to its REGISTER.
_writeRegister(INA228_SHUNT_CAL, shunt_cal);
return 0;
}
float INA228::getMaxCurrent()
{
return _maxCurrent;
}
float INA228::getShunt()
{
return _shunt;
}
float INA228::getCurrentLSB()
{
return _current_LSB;
}
////////////////////////////////////////////////////////
//
// SHUNT TEMPERATURE COEFFICIENT REGISTER 3
//
bool INA228::setShuntTemperatureCoefficent(uint16_t ppm)
{
if (ppm > 16383) return false;
_writeRegister(INA228_SHUNT_TEMP_CO, ppm);
return true;
}
uint16_t INA228::getShuntTemperatureCoefficent()
{
uint16_t value = _readRegister(INA228_SHUNT_TEMP_CO, 2);
return value;
}
////////////////////////////////////////////////////////
//
// DIAGNOSE ALERT REGISTER 11
//
void INA228::setDiagnoseAlert(uint16_t flags)
{
_writeRegister(INA228_DIAG_ALERT, flags);
}
uint16_t INA228::getDiagnoseAlert()
{
return _readRegister(INA228_DIAG_ALERT, 2);
}
// INA228.h has an enum for the bit fields.
void INA228::setDiagnoseAlertBit(uint8_t bit)
{
uint16_t value = _readRegister(INA228_DIAG_ALERT, 2);
uint16_t mask = (1 << bit);
// only write new value if bit not set
if ((value & mask) == 0)
{
value |= mask;
_writeRegister(INA228_DIAG_ALERT, value);
}
}
void INA228::clearDiagnoseAlertBit(uint8_t bit)
{
uint16_t value = _readRegister(INA228_DIAG_ALERT, 2);
uint16_t mask = (1 << bit);
// only write new value if bit not set.
if ((value & mask ) != 0)
{
value &= ~mask;
_writeRegister(INA228_DIAG_ALERT, value);
}
}
uint16_t INA228::getDiagnoseAlertBit(uint8_t bit)
{
uint16_t value = _readRegister(INA228_DIAG_ALERT, 2);
return (value >> bit) & 0x01;
}
////////////////////////////////////////////////////////
//
// THRESHOLD AND LIMIT REGISTERS 12-17
//
// TODO (sync INA228)
// - API ?
// - return bool for setters
// - float voltage interface instead of uint16_t? breaking!
void INA228::setShuntOvervoltageTH(uint16_t threshold)
{
// TODO ADCRANGE DEPENDENT
// Conversion Factor: 5 μV/LSB when ADCRANGE = 0
// 1.25 μV/LSB when ADCRANGE = 1.
// float LSB = 5.0e-6;
// if (_ADCRange == 1) LSB = 1.25e-6;
_writeRegister(INA228_SOVL, threshold);
}
uint16_t INA228::getShuntOvervoltageTH()
{
// TODO ADCRANGE DEPENDENT
// float LSB = 5.0e-6;
// if (_ADCRange == 1) LSB = 1.25e-6;
return _readRegister(INA228_SOVL, 2);
}
void INA228::setShuntUndervoltageTH(uint16_t threshold)
{
// TODO ADCRANGE DEPENDENT
// float LSB = 5.0e-6;
// if (_ADCRange == 1) LSB = 1.25e-6;
_writeRegister(INA228_SUVL, threshold);
}
uint16_t INA228::getShuntUndervoltageTH()
{
// TODO ADCRANGE DEPENDENT
// float LSB = 5.0e-6;
// if (_ADCRange == 1) LSB = 1.25e-6;
return _readRegister(INA228_SUVL, 2);
}
void INA228::setBusOvervoltageTH(uint16_t threshold)
{
if (threshold > 0x7FFF) return; // false;
//float LSB = 3.125e-3; // 3.125 mV/LSB.
_writeRegister(INA228_BOVL, threshold);
}
uint16_t INA228::getBusOvervoltageTH()
{
//float LSB = 3.125e-3; // 3.125 mV/LSB.
return _readRegister(INA228_BOVL, 2);
}
void INA228::setBusUndervoltageTH(uint16_t threshold)
{
if (threshold > 0x7FFF) return;
//float LSB = 3.125e-3; // 3.125 mV/LSB.
_writeRegister(INA228_BUVL, threshold);
}
uint16_t INA228::getBusUndervoltageTH()
{
//float LSB = 3.125e-3; // 3.125 mV/LSB.
return _readRegister(INA228_BUVL, 2);
}
void INA228::setTemperatureOverLimitTH(uint16_t threshold)
{
//float LSB = 7.8125e-3; // milli degrees Celsius
_writeRegister(INA228_TEMP_LIMIT, threshold);
}
uint16_t INA228::getTemperatureOverLimitTH()
{
//float LSB = 7.8125e-3; // milli degrees Celsius
return _readRegister(INA228_TEMP_LIMIT, 2);
}
void INA228::setPowerOverLimitTH(uint16_t threshold)
{
// P29
// Conversion factor: 256 × Power LSB.
_writeRegister(INA228_POWER_LIMIT, threshold);
}
uint16_t INA228::getPowerOverLimitTH()
{
// P29
// Conversion factor: 256 × Power LSB.
return _readRegister(INA228_POWER_LIMIT, 2);
}
////////////////////////////////////////////////////////
//
// MANUFACTURER and ID REGISTER 3E/3F
//
uint16_t INA228::getManufacturer()
{
uint16_t value = _readRegister(INA228_MANUFACTURER, 2);
return value;
}
uint16_t INA228::getDieID()
{
uint16_t value = _readRegister(INA228_DEVICE_ID, 2);
return (value >> 4) & 0x0FFF;
}
uint16_t INA228::getRevision()
{
uint16_t value = _readRegister(INA228_DEVICE_ID, 2);
return value & 0x000F;
}
////////////////////////////////////////////////////////
//
// ERROR HANDLING
//
int INA228::getLastError()
{
int e = _error;
_error = 0;
return e;
}
////////////////////////////////////////////////////////
//
// PRIVATE
//
uint32_t INA228::_readRegister(uint8_t reg, uint8_t bytes)
{
_error = 0;
_wire->beginTransmission(_address);
_wire->write(reg);
int n = _wire->endTransmission();
if (n != 0)
{
_error = -1;
return 0;
}
uint32_t value = 0;
if (bytes == _wire->requestFrom(_address, (uint8_t)bytes))
{
for (int i = 0; i < bytes; i++)
{
value <<= 8;
value |= _wire->read();
}
}
else
{
_error = -2;
return 0;
}
return value;
}
// always 5 bytes
double INA228::_readRegisterF(uint8_t reg, char mode)
{
_error = 0;
_wire->beginTransmission(_address);
_wire->write(reg);
int n = _wire->endTransmission();
if (n != 0)
{
_error = -1;
return 0;
}
double value = 0;
if (5 == _wire->requestFrom(_address, (uint8_t)5))
{
uint32_t val = 0;
// fetch 4 MSB bytes first.
for (int i = 0; i < 4; i++)
{
val <<= 8;
val |= _wire->read();
}
// handle signed / unsigned by casting.
if (mode == 'U') value = val;
else value = (int32_t) val;
// process last byte
value *= 256;
// note: mar05c
uint8_t n = _wire->read();
value += n;
}
else
{
_error = -2;
return 0;
}
return value;
}
uint16_t INA228::_writeRegister(uint8_t reg, uint16_t value)
{
_wire->beginTransmission(_address);
_wire->write(reg);
_wire->write(value >> 8);
_wire->write(value & 0xFF);
int n = _wire->endTransmission();
if (n != 0)
{
_error = -1;
}
return n;
}
// -- END OF FILE --