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()
151152float 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
159160double 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
171172double 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//
557570uint32_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
0 commit comments