forked from RobTillaart/INA238
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathINA238.cpp
More file actions
604 lines (508 loc) · 13.7 KB
/
INA238.cpp
File metadata and controls
604 lines (508 loc) · 13.7 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
// FILE: INA238.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.2
// DATE: 2025-06-11
// PURPOSE: Arduino library for the INA238, I2C, 16 bit, voltage, current and power sensor.
// URL: https://github.com/RobTillaart/INA238
// https://www.adafruit.com/product/6349
//
// Read the datasheet for the details
#include "INA238.h"
// REGISTERS ADDRESS BITS RW
#define INA238_CONFIG 0x00 // 16 RW
#define INA238_ADC_CONFIG 0x01 // 16 RW
#define INA238_SHUNT_CAL 0x02 // 16 RW
#define INA238_SHUNT_VOLTAGE 0x04 // 16 R-
#define INA238_BUS_VOLTAGE 0x05 // 16 R-
#define INA238_TEMPERATURE 0x06 // 16 R-
#define INA238_CURRENT 0x07 // 16 R-
#define INA238_POWER 0x08 // 24 R-
#define INA238_DIAG_ALERT 0x0B // 16 RW
#define INA238_SOVL 0x0C // 16 RW
#define INA238_SUVL 0x0D // 16 RW
#define INA238_BOVL 0x0E // 16 RW
#define INA238_BUVL 0x0F // 16 RW
#define INA238_TEMP_LIMIT 0x10 // 16 RW
#define INA238_POWER_LIMIT 0x11 // 16 RW
#define INA238_MANUFACTURER 0x3E // 16 R-
#define INA238_DEVICE_ID 0x3F // 16 R-
// CONFIG MASKS (register 0)
#define INA238_CFG_RST 0x8000
#define INA238_CFG_CONVDLY 0x3FC0
#define INA238_CFG_ADCRANGE 0x0010
#define INA238_CFG_RESERVED 0x402F // all unused bits
// ADC MASKS (register 1)
#define INA238_ADC_MODE 0xF000
#define INA238_ADC_VBUSCT 0x0E00
#define INA238_ADC_VSHCT 0x01C0
#define INA238_ADC_VTCT 0x0038
#define INA238_ADC_AVG 0x0007
////////////////////////////////////////////////////////
//
// CONSTRUCTOR
//
INA238::INA238(const uint8_t address, TwoWire *wire)
{
_address = address;
_wire = wire;
// no calibrated values by default.
_shunt = 0.015;
_maxCurrent = 10.0;
// 3.0517578125e-5 = pow(2, -15) 8.1.2 formula (2)
_current_LSB = _maxCurrent * 3.0517578125e-5;
_error = 0;
}
bool INA238::begin()
{
if (! isConnected()) return false;
getADCRange();
return true;
}
bool INA238::isConnected()
{
_wire->beginTransmission(_address);
return ( _wire->endTransmission() == 0);
}
uint8_t INA238::getAddress()
{
return _address;
}
bool INA238::setAddress(uint8_t newAddress)
{
_address = newAddress;
return isConnected();
}
////////////////////////////////////////////////////////
//
// CORE FUNCTIONS
//
// PAGE 23 CHECK
float INA238::getBusVoltage()
{
// always positive, remove reserved bits.
int16_t value = _readRegister(INA238_BUS_VOLTAGE, 2);
float bus_LSB = 3.125e-3; // 3.125 mV
float voltage = value * bus_LSB;
return voltage;
}
// PAGE 23 CHECK
float INA238::getShuntVoltage()
{
// shunt_LSB depends on ADCRANGE in INA238_CONFIG register.
float shunt_LSB = 5.0e-6; // 5.0 uV
if (_ADCRange == true)
{
shunt_LSB = 1.25e-6; // 1.25 uV
}
// remove reserved bits.
int16_t value = _readRegister(INA238_SHUNT_VOLTAGE, 2);
// int16_t handles negative values (16 bit)
float voltage = value * shunt_LSB;
return voltage;
}
// PAGE 24 + 8.1.2 CHECK
float INA238::getCurrent()
{
// remove reserved bits.
int16_t value = _readRegister(INA238_CURRENT, 2);
// int16_t handles negative values (16 bit)
float current = value * _current_LSB; // 8.1.2 formula (3)
return current;
}
// PAGE 24 + 8.1.2 CHECK
float INA238::getPower()
{
// 24 bit !!
uint32_t value = _readRegister(INA238_POWER, 3);
// PAGE 28-29 (8.1.2)
return value * 0.2 * _current_LSB; // formula (4)
}
// PAGE 24 CHECK
float INA238::getTemperature()
{
// INA238 uses two complements, so place 2 bytes in int16_t
int16_t value = (int16_t) _readRegister(INA238_TEMPERATURE, 2);
// shift 4 right as INA238 uses only bits 15-4
value >>= 4;
float LSB = 125e-3; // 125 m°C/LSB
return (float)value * LSB;
}
////////////////////////////////////////////////////////
//
// CONFIG REGISTER 0
//
void INA238::reset()
{
uint16_t value = _readRegister(INA238_CONFIG, 2);
value |= INA238_CFG_RST;
_writeRegister(INA238_CONFIG, value);
}
void INA238::setConversionDelay(uint8_t steps)
{
uint16_t value = _readRegister(INA238_CONFIG, 2);
value &= ~INA238_CFG_CONVDLY;
value |= (steps << 6);
_writeRegister(INA238_CONFIG, value);
}
uint8_t INA238::getConversionDelay()
{
uint16_t value = _readRegister(INA238_CONFIG, 2);
return (value >> 6) & 0xFF; // 8 bits
}
void INA238::setADCRange(bool flag)
{
// if (flag == _ADCRange) return;
_ADCRange = flag;
uint16_t value = _readRegister(INA238_CONFIG, 2);
if (flag) value |= INA238_CFG_ADCRANGE;
else value &= ~INA238_CFG_ADCRANGE;
// if value has not changed we do not need to write it back.
_writeRegister(INA238_CONFIG, value);
}
bool INA238::getADCRange()
{
uint16_t value = _readRegister(INA238_CONFIG, 2);
_ADCRange = (value & INA238_CFG_ADCRANGE) > 0;
return _ADCRange;
}
////////////////////////////////////////////////////////
//
// CONFIG ADC REGISTER 1
//
bool INA238::setMode(uint8_t mode)
{
if (mode > 0x0F) return false;
uint16_t value = _readRegister(INA238_ADC_CONFIG, 2);
value &= ~INA238_ADC_MODE;
value |= (mode << 12);
_writeRegister(INA238_ADC_CONFIG, value);
return true;
}
uint8_t INA238::getMode()
{
uint16_t value = _readRegister(INA238_ADC_CONFIG, 2);
return (value & INA238_ADC_MODE) >> 12;
}
bool INA238::setBusVoltageConversionTime(uint8_t bvct)
{
if (bvct > 7) return false;
uint16_t value = _readRegister(INA238_ADC_CONFIG, 2);
value &= ~INA238_ADC_VBUSCT;
value |= (bvct << 9);
_writeRegister(INA238_ADC_CONFIG, value);
return true;
}
uint8_t INA238::getBusVoltageConversionTime()
{
uint16_t value = _readRegister(INA238_ADC_CONFIG, 2);
return (value & INA238_ADC_VBUSCT) >> 9;
}
bool INA238::setShuntVoltageConversionTime(uint8_t svct)
{
if (svct > 7) return false;
uint16_t value = _readRegister(INA238_ADC_CONFIG, 2);
value &= ~INA238_ADC_VSHCT;
value |= (svct << 6);
_writeRegister(INA238_ADC_CONFIG, value);
return true;
}
uint8_t INA238::getShuntVoltageConversionTime()
{
uint16_t value = _readRegister(INA238_ADC_CONFIG, 2);
return (value & INA238_ADC_VSHCT) >> 6;
}
bool INA238::setTemperatureConversionTime(uint8_t tct)
{
if (tct > 7) return false;
uint16_t value = _readRegister(INA238_ADC_CONFIG, 2);
value &= ~INA238_ADC_VTCT;
value |= (tct << 3);
_writeRegister(INA238_ADC_CONFIG, value);
return true;
}
uint8_t INA238::getTemperatureConversionTime()
{
uint16_t value = _readRegister(INA238_ADC_CONFIG, 2);
return (value & INA238_ADC_VTCT) >> 3;
}
bool INA238::setAverage(uint8_t avg)
{
if (avg > 7) return false;
uint16_t value = _readRegister(INA238_ADC_CONFIG, 2);
value &= ~INA238_ADC_AVG;
value |= avg;
_writeRegister(INA238_ADC_CONFIG, value);
return true;
}
uint8_t INA238::getAverage()
{
uint16_t value = _readRegister(INA238_ADC_CONFIG, 2);
return (value & INA238_ADC_AVG);
}
////////////////////////////////////////////////////////
//
// SHUNT CALIBRATION REGISTER 2
//
int INA238::setMaxCurrentShunt(float maxCurrent, float shunt)
{
// Shunt can be really small
if (shunt < 0.0001) return -2;
if (maxCurrent < 0.0) return -3;
_maxCurrent = maxCurrent;
_shunt = shunt;
_current_LSB = _maxCurrent / (float)(1UL << 15); // pow(2, -15);
// PAGE 28-29 (8.1.2)
float shunt_cal = 819.2e6 * _current_LSB * _shunt; // 8.1.2 formula (1,2)
// depends on ADCRANGE in INA238_CONFIG register.
if (_ADCRange == true)
{
shunt_cal *= 4;
}
// shunt_cal must be written to its REGISTER.
_writeRegister(INA238_SHUNT_CAL, shunt_cal);
return 0;
}
float INA238::getMaxCurrent()
{
return _maxCurrent;
}
float INA238::getShunt()
{
return _shunt;
}
float INA238::getCurrentLSB()
{
return _current_LSB;
}
////////////////////////////////////////////////////////
//
// DIAGNOSE ALERT REGISTER 11
//
void INA238::setDiagnoseAlert(uint16_t flags)
{
_writeRegister(INA238_DIAG_ALERT, flags);
}
uint16_t INA238::getDiagnoseAlert()
{
return _readRegister(INA238_DIAG_ALERT, 2);
}
// INA238.h has an enum for the bit fields.
void INA238::setDiagnoseAlertBit(uint8_t bit)
{
uint16_t value = _readRegister(INA238_DIAG_ALERT, 2);
uint16_t mask = (1 << bit);
// only write new value if bit not set
if ((value & mask) == 0)
{
value |= mask;
_writeRegister(INA238_DIAG_ALERT, value);
}
}
void INA238::clearDiagnoseAlertBit(uint8_t bit)
{
uint16_t value = _readRegister(INA238_DIAG_ALERT, 2);
uint16_t mask = (1 << bit);
// only write new value if bit not set.
if ((value & mask ) != 0)
{
value &= ~mask;
_writeRegister(INA238_DIAG_ALERT, value);
}
}
uint16_t INA238::getDiagnoseAlertBit(uint8_t bit)
{
uint16_t value = _readRegister(INA238_DIAG_ALERT, 2);
return (value >> bit) & 0x01;
}
////////////////////////////////////////////////////////
//
// THRESHOLD AND LIMIT REGISTERS 12-17
// section 7.3.6, 7.6.1.10
//
// (sync INA228 whenever fixed)
// - API
// - return bool for setters?
// - float voltage interface instead of uint16_t? breaking!
void INA238::setOverCurrentLimit(uint32_t milliamp)
{
// Convert mA → A
float current_A = milliamp / 1000.0f;
// Compute shunt voltage
float v_shunt = current_A * _shunt;
// Determine LSB based on ADCRANGE
float lsb = _ADCRange ? 0.00000125f : 0.000005f;
// Convert volts → register value
uint16_t raw = (uint16_t)(v_shunt / lsb + 0.5f);
// Write to SOVL register
_writeRegister(INA238_SOVL, raw);
}
float INA238::getOverCurrentLimit_mA()
{
// Read raw threshold register
uint16_t raw = _readRegister(INA238_SOVL, 2);
// Determine LSB based on ADCRANGE
float lsb = _ADCRange ? 0.00000125f : 0.000005f;
// Convert raw register → shunt voltage
float v_shunt = raw * lsb;
// Convert shunt voltage → current (A)
float current_A = v_shunt / _shunt;
// Convert A → mA
return current_A * 1000.0f;
}
void INA238::setShuntOvervoltageTH(uint16_t threshold)
{
// 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(INA238_SOVL, threshold);
}
uint16_t INA238::getShuntOvervoltageTH()
{
// ADCRANGE DEPENDENT
// float LSB = 5.0e-6;
// if (_ADCRange == 1) LSB = 1.25e-6;
return _readRegister(INA238_SOVL, 2);
}
void INA238::setShuntUndervoltageTH(uint16_t threshold)
{
// ADCRANGE DEPENDENT
// float LSB = 5.0e-6;
// if (_ADCRange == 1) LSB = 1.25e-6;
_writeRegister(INA238_SUVL, threshold);
}
uint16_t INA238::getShuntUndervoltageTH()
{
// ADCRANGE DEPENDENT
// float LSB = 5.0e-6;
// if (_ADCRange == 1) LSB = 1.25e-6;
return _readRegister(INA238_SUVL, 2);
}
void INA238::setBusOvervoltageTH(uint16_t threshold)
{
if (threshold > 0x7FFF) return; // false;
//float LSB = 3.125e-3; // 3.125 mV/LSB.
_writeRegister(INA238_BOVL, threshold);
}
uint16_t INA238::getBusOvervoltageTH()
{
//float LSB = 3.125e-3; // 3.125 mV/LSB.
return _readRegister(INA238_BOVL, 2);
}
void INA238::setBusUndervoltageTH(uint16_t threshold)
{
if (threshold > 0x7FFF) return;
//float LSB = 3.125e-3; // 3.125 mV/LSB.
_writeRegister(INA238_BUVL, threshold);
}
uint16_t INA238::getBusUndervoltageTH()
{
//float LSB = 3.125e-3; // 3.125 mV/LSB.
return _readRegister(INA238_BUVL, 2);
}
void INA238::setTemperatureOverLimitTH(uint16_t threshold)
{
//float LSB = 125e-3; // milli degrees Celsius
_writeRegister(INA238_TEMP_LIMIT, threshold);
}
uint16_t INA238::getTemperatureOverLimitTH()
{
//float LSB = 125e-3; // milli degrees Celsius
return _readRegister(INA238_TEMP_LIMIT, 2);
}
void INA238::setPowerOverLimitTH(uint16_t threshold)
{
// P27
// Conversion factor: 256 × Power LSB.
_writeRegister(INA238_POWER_LIMIT, threshold);
}
uint16_t INA238::getPowerOverLimitTH()
{
// P27
// Conversion factor: 256 × Power LSB.
return _readRegister(INA238_POWER_LIMIT, 2);
}
////////////////////////////////////////////////////////
//
// MANUFACTURER and ID REGISTER 3E/3F
//
uint16_t INA238::getManufacturer()
{
uint16_t value = _readRegister(INA238_MANUFACTURER, 2);
return value;
}
uint16_t INA238::getDieID()
{
uint16_t value = _readRegister(INA238_DEVICE_ID, 2);
return (value >> 4) & 0x0FFF;
}
uint16_t INA238::getRevision()
{
uint16_t value = _readRegister(INA238_DEVICE_ID, 2);
return value & 0x000F;
}
////////////////////////////////////////////////////////
//
// ERROR HANDLING
//
int INA238::getLastError()
{
int e = _error;
_error = 0;
return e;
}
////////////////////////////////////////////////////////
//
// PRIVATE
//
uint32_t INA238::_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;
}
uint16_t INA238::_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;
}
//////////////////////////////////////////////////////////////
//
// DERIVED INA237
//
INA237::INA237(const uint8_t address, TwoWire *wire)
:INA238(address, wire)
{
// wrapper for now.
}
// -- END OF FILE --