Skip to content

Commit b69e7f6

Browse files
committed
0.2.0 INA228
1 parent 20ce3a9 commit b69e7f6

File tree

7 files changed

+117
-39
lines changed

7 files changed

+117
-39
lines changed

libraries/INA228/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.2.0] - 2025-03-05
10+
- fix #20, INA.getCharge => make it signed.
11+
- add **getLastError()** low level (I2C) error handling
12+
13+
----
14+
915
## [0.1.5] - 2025-01-30
1016
- fix #17, cache ADCRange to improve getShuntVoltage()
1117
- add **INA_comparison_table.md**

libraries/INA228/INA228.cpp

Lines changed: 79 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// FILE: INA228.cpp
22
// AUTHOR: Rob Tillaart
3-
// VERSION: 0.1.5
3+
// VERSION: 0.2.0
44
// DATE: 2024-05-09
55
// PURPOSE: Arduino library for the INA228, I2C, 20 bit, voltage, current and power sensor.
66
// URL: https://github.com/RobTillaart/INA228
@@ -65,6 +65,7 @@ INA228::INA228(const uint8_t address, TwoWire *wire)
6565
_shunt = 0.015;
6666
_maxCurrent = 10.0;
6767
_current_LSB = _maxCurrent * pow(2, -19);
68+
_error = 0;
6869
}
6970

7071

@@ -151,17 +152,17 @@ float INA228::getPower()
151152
float INA228::getTemperature()
152153
{
153154
uint32_t value = _readRegister(INA228_TEMPERATURE, 2);
154-
float LSB = 7.8125e-3; // milli degree Celsius
155+
float LSB = 7.8125e-3; // milli degree Celsius
155156
return value * LSB;
156157
}
157158

158159
// PAGE 26 + 8.1.2
159160
double INA228::getEnergy()
160161
{
161-
// read 40 bit unsigned as a double to prevent 64 bit ints
162+
// read 40 bit UNSIGNED as a double to prevent 64 bit integers
162163
// double might be 8 or 4 byte, depends on platform
163164
// 40 bit ==> O(10^12)
164-
double value = _readRegisterF(INA228_ENERGY, 5);
165+
double value = _readRegisterF(INA228_ENERGY);
165166
// PAGE 31 (8.1.2)
166167
return value * (16 * 3.2) * _current_LSB;
167168
}
@@ -170,10 +171,10 @@ double INA228::getEnergy()
170171
// PAGE 26 + 8.1.2
171172
double INA228::getCharge()
172173
{
173-
// read 40 bit unsigned as a float to prevent 64 bit ints
174+
// read 40 bit SIGNED as a float to prevent 64 bit integers
174175
// double might be 8 or 4 byte, depends on platform
175176
// 40 bit ==> O(10^12)
176-
double value = _readRegisterF(INA228_CHARGE, 5);
177+
double value = _readRegisterF(INA228_CHARGE);
177178
// PAGE 32 (8.1.2)
178179
return value * _current_LSB;
179180
}
@@ -552,37 +553,91 @@ uint16_t INA228::getRevision()
552553

553554
////////////////////////////////////////////////////////
554555
//
555-
// SHOULD BE PRIVATE
556+
// ERROR HANDLING
557+
//
558+
int INA228::getLastError()
559+
{
560+
int e = _error;
561+
_error = 0;
562+
return e;
563+
}
564+
565+
566+
////////////////////////////////////////////////////////
567+
//
568+
// PRIVATE
556569
//
557570
uint32_t INA228::_readRegister(uint8_t reg, uint8_t bytes)
558571
{
572+
_error = 0;
559573
_wire->beginTransmission(_address);
560574
_wire->write(reg);
561-
_wire->endTransmission();
575+
int n = _wire->endTransmission();
576+
if (n != 0)
577+
{
578+
_error = -1;
579+
return 0;
580+
}
562581

563-
_wire->requestFrom(_address, (uint8_t)bytes);
564582
uint32_t value = 0;
565-
for (int i = 0; i < bytes; i++)
583+
if (bytes == _wire->requestFrom(_address, (uint8_t)bytes))
566584
{
567-
value <<= 8;
568-
value |= _wire->read();
585+
for (int i = 0; i < bytes; i++)
586+
{
587+
value <<= 8;
588+
value |= _wire->read();
589+
}
590+
}
591+
else
592+
{
593+
_error = -2;
594+
return 0;
569595
}
570596
return value;
571597
}
572598

573599

574-
double INA228::_readRegisterF(uint8_t reg, uint8_t bytes)
600+
// always 5 bytes
601+
double INA228::_readRegisterF(uint8_t reg)
575602
{
603+
_error = 0;
576604
_wire->beginTransmission(_address);
577605
_wire->write(reg);
578-
_wire->endTransmission();
606+
int n = _wire->endTransmission();
607+
if (n != 0)
608+
{
609+
_error = -1;
610+
return 0;
611+
}
579612

580-
_wire->requestFrom(_address, (uint8_t)bytes);
581613
double value = 0;
582-
for (int i = 0; i < bytes; i++)
614+
615+
int32_t ival = 0;
616+
if (5 == _wire->requestFrom(_address, (uint8_t)5))
583617
{
584-
value *= 256.0;
585-
value += _wire->read();
618+
// fetch 4 MSB bytes first.
619+
for (int i = 0; i < 4; i++)
620+
{
621+
ival <<= 8;
622+
ival |= _wire->read();
623+
}
624+
value = ival;
625+
value *= 256;
626+
// note: mar05c
627+
uint8_t n = _wire->read();
628+
value += n;
629+
630+
// ORG
631+
// for (int i = 0; i < bytes; i++)
632+
// {
633+
// value *= 256.0;
634+
// value += _wire->read();
635+
// }
636+
}
637+
else
638+
{
639+
_error = -2;
640+
return 0;
586641
}
587642
return value;
588643
}
@@ -594,7 +649,12 @@ uint16_t INA228::_writeRegister(uint8_t reg, uint16_t value)
594649
_wire->write(reg);
595650
_wire->write(value >> 8);
596651
_wire->write(value & 0xFF);
597-
return _wire->endTransmission();
652+
int n = _wire->endTransmission();
653+
if (n != 0)
654+
{
655+
_error = -1;
656+
}
657+
return n;
598658
}
599659

600660

libraries/INA228/INA228.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22
// FILE: INA228.h
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.5
4+
// VERSION: 0.2.0
55
// DATE: 2024-05-09
66
// PURPOSE: Arduino library for the INA228, I2C, 20 bit, voltage, current and power sensor.
77
// URL: https://github.com/RobTillaart/INA228
@@ -16,7 +16,7 @@
1616
#include "Wire.h"
1717

1818

19-
#define INA228_LIB_VERSION (F("0.1.5"))
19+
#define INA228_LIB_VERSION (F("0.2.0"))
2020

2121

2222
// for setMode() and getMode()
@@ -242,12 +242,16 @@ class INA228
242242
uint16_t getDieID(); // 0x0228
243243
uint16_t getRevision(); // 0x0001
244244

245+
//
246+
// ERROR HANDLING
247+
//
248+
int getLastError();
245249

246250
private:
247251
// max 4 bytes
248252
uint32_t _readRegister(uint8_t reg, uint8_t bytes);
249-
// 5 bytes or more
250-
double _readRegisterF(uint8_t reg, uint8_t bytes);
253+
// always 5 bytes
254+
double _readRegisterF(uint8_t reg);
251255
uint16_t _writeRegister(uint8_t reg, uint16_t value);
252256

253257
float _current_LSB;
@@ -257,6 +261,8 @@ class INA228
257261

258262
uint8_t _address;
259263
TwoWire * _wire;
264+
265+
int _error;
260266
};
261267

262268

libraries/INA228/INA_comparison_table.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44
Comparison of my INA2xx libraries
55

66

7-
| | 219 | 226 | 228 | 229 | 236 | 239 | 3221 |
8-
|:------------------|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|
9-
| bits | 12 | 16 | 20 | 20 | 16 | 16 | 13 |
10-
| Max Voltage | 26 | 36 | 85 | 85 | 48 | 85 | 26 |
11-
| Communication | I2C | I2C | I2C | SPI | I2C | SPI | I2C |
12-
| Channels | 1 | 1 | 1 | 1 | 1 | 1 | 3 |
13-
| | | | | | | | |
14-
| getBusVoltage | Y | Y | Y | Y | Y | Y | Y |
15-
| getShuntVoltage | Y | Y | Y | Y | Y | Y | Y |
16-
| getCurrent | Y | Y | Y | Y | Y | Y | Y |
17-
| getPower | Y | Y | Y | Y | Y | Y | Y |
18-
| getTemperature | - | - | Y | Y | - | Y | - |
19-
| getEnergy | - | - | Y | Y | - | - | - |
20-
| getCharge | - | - | Y | Y | - | - | - |
7+
| | 219 | 226 | 228 | 229 | 236 | 239 | 260 | 3221 |
8+
|:------------------|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|
9+
| bits | 12 | 16 | 20 | 20 | 16 | 16 | 16 | 13 |
10+
| Max Voltage | 26 | 36 | 85 | 85 | 48 | 85 | 36 | 26 |
11+
| Communication | I2C | I2C | I2C | SPI | I2C | SPI | I2C | I2C |
12+
| Channels | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 3 |
13+
| | | | | | | | | |
14+
| getBusVoltage | Y | Y | Y | Y | Y | Y | Y | Y |
15+
| getShuntVoltage | Y | Y | Y | Y | Y | Y | Y | Y |
16+
| getCurrent | Y | Y | Y | Y | Y | Y | Y | Y |
17+
| getPower | Y | Y | Y | Y | Y | Y | Y | Y |
18+
| getTemperature | - | - | Y | Y | - | Y | - | - |
19+
| getEnergy | - | - | Y | Y | - | - | - | - |
20+
| getCharge | - | - | Y | Y | - | - | - | - |
2121

2222

2323
https://www.ti.com/amplifier-circuit/current-sense/digital-power-monitors/products.html

libraries/INA228/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ The library is limited tested and verified with hardware.
4646

4747
Feedback as always is welcome.
4848

49+
### Breaking change 0.2.0
50+
51+
The function **getCharge()** is updated as the value can be negative too.
52+
The previous versions assumed it could only be positive. See #22.
53+
These are obsolete now.
54+
4955

5056
### Details
5157

libraries/INA228/library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"type": "git",
1616
"url": "https://github.com/RobTillaart/INA228.git"
1717
},
18-
"version": "0.1.5",
18+
"version": "0.2.0",
1919
"license": "MIT",
2020
"frameworks": "*",
2121
"platforms": "*",

libraries/INA228/library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=INA228
2-
version=0.1.5
2+
version=0.2.0
33
author=Rob Tillaart <rob.tillaart@gmail.com>
44
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
55
sentence=Arduino library for the INA228, I2C, 20 bit, voltage, current and power sensor.

0 commit comments

Comments
 (0)