From dc99e50b4f19b6a4ebf8f67aee893604aa420f1c Mon Sep 17 00:00:00 2001 From: Gabriele Pongelli Date: Mon, 21 Dec 2020 17:59:12 +0100 Subject: [PATCH 01/17] Adding fields Explicitly adding all the MASK_ENABLE bits, MANUFACTURER and REVISION registry --- adafruit_ina260.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/adafruit_ina260.py b/adafruit_ina260.py index 4774ba5..a48fba9 100644 --- a/adafruit_ina260.py +++ b/adafruit_ina260.py @@ -161,8 +161,20 @@ def __init__(self, i2c_bus, address=0x40): _raw_current = ROUnaryStruct(_REG_CURRENT, ">h") _raw_voltage = ROUnaryStruct(_REG_BUSVOLTAGE, ">H") _raw_power = ROUnaryStruct(_REG_POWER, ">H") - - _conversion_ready = ROBit(_REG_MASK_ENABLE, 3, 2, False) + + # MASK_ENABLE fields + overcurrent_limit = RWBit(_REG_MASK_ENABLE, 15, 2, False) + under_current_limit = RWBit(_REG_MASK_ENABLE, 14, 2, False) + bus_voltage_over_voltage = RWBit(_REG_MASK_ENABLE, 13, 2, False) + bus_voltage_under_voltage = RWBit(_REG_MASK_ENABLE, 12, 2, False) + power_over_limit = RWBit(_REG_MASK_ENABLE, 11, 2, False) + conversion_ready = RWBit(_REG_MASK_ENABLE, 10, 2, False) + # from 5 to 9 are not used + alert_function_flag = ROBit(_REG_MASK_ENABLE, 4, 2, False) + _conversion_ready_flag = ROBit(_REG_MASK_ENABLE, 3, 2, False) + math_overflow_flag = ROBit(_REG_MASK_ENABLE, 2, 2, False) + alert_polarity_bit = RWBit(_REG_MASK_ENABLE, 1, 2, False) + alert_latch_enable = RWBit(_REG_MASK_ENABLE, 0, 2, False) averaging_count = RWBits(3, _REG_CONFIG, 9, 2, False) """The window size of the rolling average used in continuous mode""" @@ -178,12 +190,16 @@ def __init__(self, i2c_bus, address=0x40): mask_enable = RWBits(16, _REG_MASK_ENABLE, 0, 2, False) alert_limit = RWBits(16, _REG_ALERT_LIMIT, 0, 2, False) + + manufacturer_id = ROUnaryStruct(_REG_MFG_UID, ">H") + device_id = ROBits(12, _REG_DIE_UID, 4, 2, False) + revision_id = ROBits(4, _REG_DIE_UID, 0, 2, False) @property def current(self): """The current (between V+ and V-) in mA""" if self.mode == Mode.TRIGGERED: - while self._conversion_ready == 0: + while self._conversion_ready_flag == 0: pass return self._raw_current * 1.25 @@ -191,7 +207,7 @@ def current(self): def voltage(self): """The bus voltage in V""" if self.mode == Mode.TRIGGERED: - while self._conversion_ready == 0: + while self._conversion_ready_flag == 0: pass return self._raw_voltage * 0.00125 @@ -199,6 +215,6 @@ def voltage(self): def power(self): """The power being delivered to the load in mW""" if self.mode == Mode.TRIGGERED: - while self._conversion_ready == 0: + while self._conversion_ready_flag == 0: pass return self._raw_power * 10 From b5f51ef85f58c7cbeded4a95602ad24361a9386e Mon Sep 17 00:00:00 2001 From: Gabriele Pongelli Date: Tue, 22 Dec 2020 12:47:47 +0100 Subject: [PATCH 02/17] Adding check on manufacturer and device id --- adafruit_ina260.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/adafruit_ina260.py b/adafruit_ina260.py index a48fba9..93e1b90 100644 --- a/adafruit_ina260.py +++ b/adafruit_ina260.py @@ -158,6 +158,16 @@ class INA260: def __init__(self, i2c_bus, address=0x40): self.i2c_device = i2cdevice.I2CDevice(i2c_bus, address) + if self._manufacturer_id != self.TEXAS_INSTRUMENT_ID: + raise RuntimeError( + "Failed to find Texas Instrument ID, read {} while expected {} - check your wiring!".format(self._manufacturer_id, self.TEXAS_INSTRUMENT_ID) + ) + + if self._device_id != self.INA260_ID: + raise RuntimeError( + "Failed to find INA260 ID, read {} while expected {} - check your wiring!".format(self._device_id, self.INA260_ID) + ) + _raw_current = ROUnaryStruct(_REG_CURRENT, ">h") _raw_voltage = ROUnaryStruct(_REG_BUSVOLTAGE, ">H") _raw_power = ROUnaryStruct(_REG_POWER, ">H") @@ -191,8 +201,10 @@ def __init__(self, i2c_bus, address=0x40): mask_enable = RWBits(16, _REG_MASK_ENABLE, 0, 2, False) alert_limit = RWBits(16, _REG_ALERT_LIMIT, 0, 2, False) - manufacturer_id = ROUnaryStruct(_REG_MFG_UID, ">H") - device_id = ROBits(12, _REG_DIE_UID, 4, 2, False) + TEXAS_INSTRUMENT_ID = const(0x5449) + INA260_ID = const(0x227) + _manufacturer_id = ROUnaryStruct(_REG_MFG_UID, ">H") + _device_id = ROBits(12, _REG_DIE_UID, 4, 2, False) revision_id = ROBits(4, _REG_DIE_UID, 0, 2, False) @property From 45ca98c0d3eae7243849029883902a090217ae8d Mon Sep 17 00:00:00 2001 From: Gabriele Pongelli Date: Tue, 22 Dec 2020 12:51:11 +0100 Subject: [PATCH 03/17] adding system reset bit --- adafruit_ina260.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/adafruit_ina260.py b/adafruit_ina260.py index 93e1b90..c6bec55 100644 --- a/adafruit_ina260.py +++ b/adafruit_ina260.py @@ -186,6 +186,8 @@ def __init__(self, i2c_bus, address=0x40): alert_polarity_bit = RWBit(_REG_MASK_ENABLE, 1, 2, False) alert_latch_enable = RWBit(_REG_MASK_ENABLE, 0, 2, False) + reset_bit = RWBit(_REG_CONFIG, 15, 2, False) + """Setting this bit t 1 generates a system reset. Reset all registers to default values.""" averaging_count = RWBits(3, _REG_CONFIG, 9, 2, False) """The window size of the rolling average used in continuous mode""" voltage_conversion_time = RWBits(3, _REG_CONFIG, 6, 2, False) From 7e807634651ffc383ecbd44f907b2e3016e5cbe7 Mon Sep 17 00:00:00 2001 From: Gabriele Pongelli Date: Tue, 22 Dec 2020 13:06:03 +0100 Subject: [PATCH 04/17] documentation --- adafruit_ina260.py | 51 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/adafruit_ina260.py b/adafruit_ina260.py index c6bec55..4a0d966 100644 --- a/adafruit_ina260.py +++ b/adafruit_ina260.py @@ -174,17 +174,55 @@ def __init__(self, i2c_bus, address=0x40): # MASK_ENABLE fields overcurrent_limit = RWBit(_REG_MASK_ENABLE, 15, 2, False) + """Setting this bit high configures the ALERT pin to be asserted if the current measurement + following a conversion exceeds the value programmed in the Alert Limit Register. + """ under_current_limit = RWBit(_REG_MASK_ENABLE, 14, 2, False) + """Setting this bit high configures the ALERT pin to be asserted if the current measurement + following a conversion drops below the value programmed in the Alert Limit Register. + """ bus_voltage_over_voltage = RWBit(_REG_MASK_ENABLE, 13, 2, False) + """Setting this bit high configures the ALERT pin to be asserted if the bus voltage measurement + following a conversion exceeds the value programmed in the Alert Limit Register. + """ bus_voltage_under_voltage = RWBit(_REG_MASK_ENABLE, 12, 2, False) + """Setting this bit high configures the ALERT pin to be asserted if the bus voltage measurement + following a conversion drops below the value programmed in the Alert Limit Register. + """ power_over_limit = RWBit(_REG_MASK_ENABLE, 11, 2, False) + """Setting this bit high configures the ALERT pin to be asserted if the Power calculation + made following a bus voltage measurement exceeds the value programmed in the Alert Limit Register. + """ conversion_ready = RWBit(_REG_MASK_ENABLE, 10, 2, False) + """Setting this bit high configures the ALERT pin to be asserted when the Conversion Ready Flag, Bit 3, + is asserted indicating that the device is ready for the next conversion. + """ # from 5 to 9 are not used alert_function_flag = ROBit(_REG_MASK_ENABLE, 4, 2, False) + """While only one Alert Function can be monitored at the ALERT pin at time, the Conversion Ready can + also be enabled to assert the ALERT pin. + Reading the Alert Function Flag following an alert allows the user to determine if the Alert + Function was the source of the Alert. + + When the Alert Latch Enable bit is set to Latch mode, the Alert Function Flag bit clears only when + the Mask/Enable Register is read. + When the Alert Latch Enable bit is set to Transparent mode, the Alert Function Flag bit is cleared + following the next conversion that does not result in an Alert condition. + """ _conversion_ready_flag = ROBit(_REG_MASK_ENABLE, 3, 2, False) + """Bit to help coordinate one-shot or triggered conversion. This bit is set after all conversion, + averaging, and multiplication are complete. Conversion Ready flag bit clears when writing the + configuration register or reading the Mask/Enable register. + """ math_overflow_flag = ROBit(_REG_MASK_ENABLE, 2, 2, False) + """This bit is set to 1 if an arithmetic operation resulted in an overflow error. + """ alert_polarity_bit = RWBit(_REG_MASK_ENABLE, 1, 2, False) - alert_latch_enable = RWBit(_REG_MASK_ENABLE, 0, 2, False) + """Active-high open collector when True, Active-low open collector when false (default). + """ + alert_latch_enable = RWBit(_REG_MASK_ENABLE, 0, 2, False) + """Configures the latching feature of the ALERT pin and Alert Flag Bits. + """ reset_bit = RWBit(_REG_CONFIG, 15, 2, False) """Setting this bit t 1 generates a system reset. Reset all registers to default values.""" @@ -201,13 +239,24 @@ def __init__(self, i2c_bus, address=0x40): """ mask_enable = RWBits(16, _REG_MASK_ENABLE, 0, 2, False) + """The Mask/Enable Register selects the function that is enabled to control the ALERT pin as well as + how that pin functions. If multiple functions are enabled, the highest significant bit position + Alert Function (D15-D11) takes priority and responds to the Alert Limit Register. + """ alert_limit = RWBits(16, _REG_ALERT_LIMIT, 0, 2, False) + """The Alert Limit Register contains the value used to compare to the register selected in the + Mask/Enable Register to determine if a limit has been exceeded. + The format for this register will match the format of the register that is selected for comparison. + """ TEXAS_INSTRUMENT_ID = const(0x5449) INA260_ID = const(0x227) _manufacturer_id = ROUnaryStruct(_REG_MFG_UID, ">H") + """Manufacturer identification bits""" _device_id = ROBits(12, _REG_DIE_UID, 4, 2, False) + """Device identification bits""" revision_id = ROBits(4, _REG_DIE_UID, 0, 2, False) + """Device revision identification bits""" @property def current(self): From f23ea1cb02540ff5658064e09fea508a406df731 Mon Sep 17 00:00:00 2001 From: Gabriele Pongelli Date: Tue, 22 Dec 2020 13:13:04 +0100 Subject: [PATCH 05/17] removing space only rows --- adafruit_ina260.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_ina260.py b/adafruit_ina260.py index 4a0d966..23ce802 100644 --- a/adafruit_ina260.py +++ b/adafruit_ina260.py @@ -208,7 +208,7 @@ def __init__(self, i2c_bus, address=0x40): the Mask/Enable Register is read. When the Alert Latch Enable bit is set to Transparent mode, the Alert Function Flag bit is cleared following the next conversion that does not result in an Alert condition. - """ + """ _conversion_ready_flag = ROBit(_REG_MASK_ENABLE, 3, 2, False) """Bit to help coordinate one-shot or triggered conversion. This bit is set after all conversion, averaging, and multiplication are complete. Conversion Ready flag bit clears when writing the @@ -248,7 +248,7 @@ def __init__(self, i2c_bus, address=0x40): Mask/Enable Register to determine if a limit has been exceeded. The format for this register will match the format of the register that is selected for comparison. """ - + TEXAS_INSTRUMENT_ID = const(0x5449) INA260_ID = const(0x227) _manufacturer_id = ROUnaryStruct(_REG_MFG_UID, ">H") From e9df7b390bd971c7ebe9126c5e8dd47d2aeae444 Mon Sep 17 00:00:00 2001 From: Gabriele Pongelli Date: Tue, 22 Dec 2020 15:07:51 +0100 Subject: [PATCH 06/17] fixing some lint check I cannot fix errors on CRLF if any cause I'm working on windows. --- adafruit_ina260.py | 72 ++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 41 deletions(-) diff --git a/adafruit_ina260.py b/adafruit_ina260.py index 23ce802..a1abeba 100644 --- a/adafruit_ina260.py +++ b/adafruit_ina260.py @@ -5,24 +5,15 @@ """ `adafruit_ina260` ================================================================================ - CircuitPython driver for the TI INA260 current and power sensor - - * Author(s): Bryan Siepert - Implementation Notes -------------------- - **Hardware:** - * `INA260 Breakout `_ - **Software and Dependencies:** - * Adafruit CircuitPython firmware for the supported boards: https://github.com/adafruit/circuitpython/releases - * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register """ @@ -48,10 +39,10 @@ _REG_MFG_UID = const(0xFE) # MANUFACTURER UNIQUE ID REGISTER (R) _REG_DIE_UID = const(0xFF) # DIE UNIQUE ID REGISTER (R) + # pylint: disable=too-few-public-methods class Mode: """Modes avaible to be set - +--------------------+---------------------------------------------------------------------+ | Mode | Description | +====================+=====================================================================+ @@ -66,7 +57,6 @@ class Mode: | ``Mode.SHUTDOWN`` | Shutdown the sensor, reducing the quiescent current and turning off| | | current into the device inputs. Set another mode to re-enable | +--------------------+---------------------------------------------------------------------+ - """ SHUTDOWN = const(0x0) @@ -76,7 +66,6 @@ class Mode: class ConversionTime: """Options for ``current_conversion_time`` or ``voltage_conversion_time`` - +----------------------------------+------------------+ | ``ConversionTime`` | Time | +==================================+==================+ @@ -96,7 +85,6 @@ class ConversionTime: +----------------------------------+------------------+ | ``ConversionTime.TIME_8_244_ms`` | 8.244 ms | +----------------------------------+------------------+ - """ TIME_140_us = const(0x0) @@ -111,7 +99,6 @@ class ConversionTime: class AveragingCount: """Options for ``averaging_count`` - +-------------------------------+------------------------------------+ | ``AveragingCount`` | Number of measurements to average | +===============================+====================================+ @@ -131,7 +118,6 @@ class AveragingCount: +-------------------------------+------------------------------------+ | ``AveragingCount.COUNT_1024`` | 1024 | +-------------------------------+------------------------------------+ - """ COUNT_1 = const(0x0) @@ -149,10 +135,8 @@ class AveragingCount: class INA260: """Driver for the INA260 power and current sensor. - :param ~busio.I2C i2c_bus: The I2C bus the INA260 is connected to. :param address: The I2C device address for the sensor. Default is ``0x40``. - """ def __init__(self, i2c_bus, address=0x40): @@ -160,18 +144,20 @@ def __init__(self, i2c_bus, address=0x40): if self._manufacturer_id != self.TEXAS_INSTRUMENT_ID: raise RuntimeError( - "Failed to find Texas Instrument ID, read {} while expected {} - check your wiring!".format(self._manufacturer_id, self.TEXAS_INSTRUMENT_ID) + "Failed to find Texas Instrument ID, read {} while expected {}" + " - check your wiring!".format(self._manufacturer_id, self.TEXAS_INSTRUMENT_ID) ) - + if self._device_id != self.INA260_ID: raise RuntimeError( - "Failed to find INA260 ID, read {} while expected {} - check your wiring!".format(self._device_id, self.INA260_ID) + "Failed to find INA260 ID, read {} while expected {}" + " - check your wiring!".format(self._device_id, self.INA260_ID) ) _raw_current = ROUnaryStruct(_REG_CURRENT, ">h") _raw_voltage = ROUnaryStruct(_REG_BUSVOLTAGE, ">H") _raw_power = ROUnaryStruct(_REG_POWER, ">H") - + # MASK_ENABLE fields overcurrent_limit = RWBit(_REG_MASK_ENABLE, 15, 2, False) """Setting this bit high configures the ALERT pin to be asserted if the current measurement @@ -191,28 +177,30 @@ def __init__(self, i2c_bus, address=0x40): """ power_over_limit = RWBit(_REG_MASK_ENABLE, 11, 2, False) """Setting this bit high configures the ALERT pin to be asserted if the Power calculation - made following a bus voltage measurement exceeds the value programmed in the Alert Limit Register. + made following a bus voltage measurement exceeds the value programmed in the + Alert Limit Register. """ conversion_ready = RWBit(_REG_MASK_ENABLE, 10, 2, False) - """Setting this bit high configures the ALERT pin to be asserted when the Conversion Ready Flag, Bit 3, - is asserted indicating that the device is ready for the next conversion. + """Setting this bit high configures the ALERT pin to be asserted when the Conversion Ready Flag, + Bit 3, is asserted indicating that the device is ready for the next conversion. """ # from 5 to 9 are not used alert_function_flag = ROBit(_REG_MASK_ENABLE, 4, 2, False) - """While only one Alert Function can be monitored at the ALERT pin at time, the Conversion Ready can - also be enabled to assert the ALERT pin. - Reading the Alert Function Flag following an alert allows the user to determine if the Alert + """While only one Alert Function can be monitored at the ALERT pin at time, the + Conversion Ready can also be enabled to assert the ALERT pin. + Reading the Alert Function Flag following an alert allows the user to determine if the Alert Function was the source of the Alert. - - When the Alert Latch Enable bit is set to Latch mode, the Alert Function Flag bit clears only when - the Mask/Enable Register is read. - When the Alert Latch Enable bit is set to Transparent mode, the Alert Function Flag bit is cleared - following the next conversion that does not result in an Alert condition. + + When the Alert Latch Enable bit is set to Latch mode, the Alert Function Flag bit + clears only when the Mask/Enable Register is read. + When the Alert Latch Enable bit is set to Transparent mode, the Alert Function Flag bit + is cleared following the next conversion that does not result in an Alert condition. """ _conversion_ready_flag = ROBit(_REG_MASK_ENABLE, 3, 2, False) - """Bit to help coordinate one-shot or triggered conversion. This bit is set after all conversion, - averaging, and multiplication are complete. Conversion Ready flag bit clears when writing the - configuration register or reading the Mask/Enable register. + """Bit to help coordinate one-shot or triggered conversion. This bit is set after all + conversion, averaging, and multiplication are complete. + Conversion Ready flag bit clears when writing the configuration register or + reading the Mask/Enable register. """ math_overflow_flag = ROBit(_REG_MASK_ENABLE, 2, 2, False) """This bit is set to 1 if an arithmetic operation resulted in an overflow error. @@ -239,14 +227,16 @@ def __init__(self, i2c_bus, address=0x40): """ mask_enable = RWBits(16, _REG_MASK_ENABLE, 0, 2, False) - """The Mask/Enable Register selects the function that is enabled to control the ALERT pin as well as - how that pin functions. If multiple functions are enabled, the highest significant bit position - Alert Function (D15-D11) takes priority and responds to the Alert Limit Register. + """The Mask/Enable Register selects the function that is enabled to control the ALERT pin as + well as how that pin functions. + If multiple functions are enabled, the highest significant bit + position Alert Function (D15-D11) takes priority and responds to the Alert Limit Register. """ alert_limit = RWBits(16, _REG_ALERT_LIMIT, 0, 2, False) - """The Alert Limit Register contains the value used to compare to the register selected in the - Mask/Enable Register to determine if a limit has been exceeded. - The format for this register will match the format of the register that is selected for comparison. + """The Alert Limit Register contains the value used to compare to the register selected in the + Mask/Enable Register to determine if a limit has been exceeded. + The format for this register will match the format of the register that is selected for + comparison. """ TEXAS_INSTRUMENT_ID = const(0x5449) From 9e2db6fa0e9be00cd3f629b43568e4bb853803a5 Mon Sep 17 00:00:00 2001 From: Gabriele Pongelli Date: Wed, 23 Dec 2020 10:04:16 +0100 Subject: [PATCH 07/17] black executed --- adafruit_ina260.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/adafruit_ina260.py b/adafruit_ina260.py index a1abeba..97ee716 100644 --- a/adafruit_ina260.py +++ b/adafruit_ina260.py @@ -145,7 +145,9 @@ def __init__(self, i2c_bus, address=0x40): if self._manufacturer_id != self.TEXAS_INSTRUMENT_ID: raise RuntimeError( "Failed to find Texas Instrument ID, read {} while expected {}" - " - check your wiring!".format(self._manufacturer_id, self.TEXAS_INSTRUMENT_ID) + " - check your wiring!".format( + self._manufacturer_id, self.TEXAS_INSTRUMENT_ID + ) ) if self._device_id != self.INA260_ID: From 8ae7e08df8a2dce16ee604a209874679a4f9d288 Mon Sep 17 00:00:00 2001 From: Gabriele Pongelli Date: Wed, 23 Dec 2020 10:14:42 +0100 Subject: [PATCH 08/17] add missing import --- adafruit_ina260.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_ina260.py b/adafruit_ina260.py index 97ee716..24e1892 100644 --- a/adafruit_ina260.py +++ b/adafruit_ina260.py @@ -27,8 +27,8 @@ import adafruit_bus_device.i2c_device as i2cdevice from adafruit_register.i2c_struct import ROUnaryStruct -from adafruit_register.i2c_bits import RWBits -from adafruit_register.i2c_bit import ROBit +from adafruit_register.i2c_bits import ROBits, RWBits +from adafruit_register.i2c_bit import ROBit, RWBit _REG_CONFIG = const(0x00) # CONFIGURATION REGISTER (R/W) _REG_CURRENT = const(0x01) # SHUNT VOLTAGE REGISTER (R) From 18f7da8e779bf8337738e4729f99e45910b7010f Mon Sep 17 00:00:00 2001 From: Gabriele Pongelli Date: Wed, 23 Dec 2020 15:12:46 +0100 Subject: [PATCH 09/17] adding static method to retrieve values giving register's content e.g. ConversionTime.get_seconds(ConversionTime.TIME_332_us) will return 0.000332 . similarly, ConversionTime.get_seconds(obj.voltage_conversion_time) will return the value reading from registry --- adafruit_ina260.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/adafruit_ina260.py b/adafruit_ina260.py index 24e1892..852fa11 100644 --- a/adafruit_ina260.py +++ b/adafruit_ina260.py @@ -96,6 +96,21 @@ class ConversionTime: TIME_4_156_ms = const(0x6) TIME_8_244_ms = const(0x7) + @staticmethod + def get_seconds(time_enum): + """Retrieve the time in seconds giving value read from register""" + conv_dict = { + 0: 140e-6, + 1: 204e-6, + 2: 332e-6, + 3: 558e-6, + 4: 1.1e-3, + 5: 2.116e-3, + 6: 4.156e-3, + 7: 8.244e-3, + } + return conv_dict[time_enum] + class AveragingCount: """Options for ``averaging_count`` @@ -129,6 +144,12 @@ class AveragingCount: COUNT_512 = const(0x6) COUNT_1024 = const(0x7) + @staticmethod + def get_averaging_count(avg_count): + """Retrieve the number of measurements giving value read from register""" + conv_dict = {0: 1, 1: 4, 2: 16, 3: 64, 4: 128, 5: 256, 6: 512, 7: 1024} + return conv_dict[avg_count] + # pylint: enable=too-few-public-methods From de6d8b2c8a876153db68b505be041cdf851a94ad Mon Sep 17 00:00:00 2001 From: Gabriele Pongelli Date: Tue, 29 Dec 2020 10:37:38 +0100 Subject: [PATCH 10/17] fix sphinx-build --- adafruit_ina260.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/adafruit_ina260.py b/adafruit_ina260.py index 852fa11..ce53c1b 100644 --- a/adafruit_ina260.py +++ b/adafruit_ina260.py @@ -12,10 +12,9 @@ **Hardware:** * `INA260 Breakout `_ **Software and Dependencies:** -* Adafruit CircuitPython firmware for the supported boards: - https://github.com/adafruit/circuitpython/releases - * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice - * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register +* Adafruit CircuitPython firmware for the supported boards: https://github.com/adafruit/circuitpython/releases +* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice +* Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register """ # imports From f5148c9fcc4459ad92476313171f66a1fd247d5b Mon Sep 17 00:00:00 2001 From: Gabriele Pongelli Date: Tue, 29 Dec 2020 10:40:16 +0100 Subject: [PATCH 11/17] fix for pylint --- adafruit_ina260.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/adafruit_ina260.py b/adafruit_ina260.py index ce53c1b..6bf2ad4 100644 --- a/adafruit_ina260.py +++ b/adafruit_ina260.py @@ -12,7 +12,8 @@ **Hardware:** * `INA260 Breakout `_ **Software and Dependencies:** -* Adafruit CircuitPython firmware for the supported boards: https://github.com/adafruit/circuitpython/releases +* Adafruit CircuitPython firmware for the supported boards: +* https://github.com/adafruit/circuitpython/releases * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register """ From fb3ec4c4002fb9086acb707ad2c821d8181c73a8 Mon Sep 17 00:00:00 2001 From: Gabriele Pongelli Date: Tue, 29 Dec 2020 10:43:11 +0100 Subject: [PATCH 12/17] remove trailing space --- adafruit_ina260.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_ina260.py b/adafruit_ina260.py index 6bf2ad4..f03a219 100644 --- a/adafruit_ina260.py +++ b/adafruit_ina260.py @@ -12,7 +12,7 @@ **Hardware:** * `INA260 Breakout `_ **Software and Dependencies:** -* Adafruit CircuitPython firmware for the supported boards: +* Adafruit CircuitPython firmware for the supported boards: * https://github.com/adafruit/circuitpython/releases * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register From 8de5c72623a9a06c294318ad05f7ebb122809e2f Mon Sep 17 00:00:00 2001 From: Gabriele Pongelli Date: Tue, 5 Jan 2021 12:14:36 +0100 Subject: [PATCH 13/17] added example file --- examples/ina260_latch.py | 49 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 examples/ina260_latch.py diff --git a/examples/ina260_latch.py b/examples/ina260_latch.py new file mode 100644 index 0000000..cd5c201 --- /dev/null +++ b/examples/ina260_latch.py @@ -0,0 +1,49 @@ +import time +import board +from adafruit_ina260 import INA260, Mode, ConversionTime + + +if __name__ == '__main__': + try: + i2c = board.I2C() + ina260 = INA260(i2c) + except RuntimeError as r_e: + # catch exception on init, no INA260 chip found + print(r_e) + raise r_e + else: + # set overcurrent limit flag and threshold value + # 0x0008 x 1,25 mA = 10 mA as alert limit + ina260.alert_limit = 0x0008 + + # alert pin is asserted, can be check with gpiozero + ina260.overcurrent_limit = True + + # keep the flag high until MASK_ENABLE register will be read + ina260.alert_latch_enable = True + + ina260.mode = Mode.CONTINUOUS + + # set higher conversion time and wait its value before each read + ina260.current_conversion_time = ConversionTime.TIME_8_244_ms + for _ in enumerate(range(5)): + time.sleep(ConversionTime.get_seconds(ina260.current_conversion_time)) + print( + "Current: %.2f mA Voltage: %.2f V Power:%.2f mW" + % (ina260.current, ina260.voltage, ina260.power) + ) + + # supposing meanwhile the alert limit was exceeded, setting an higher limit and clear the ALERT + # 0x0100 x 1,25 mA = 320 mA as alert limit + ina260.alert_limit = 0x0100 + + # alert function flag bit should be true if alert threshold was exceeded + print("Alert function flag: {}".format(ina260.alert_function_flag)) + + # in latch mode, reading the register clears the ALERT & alert function flag + print("MASK register: {}".format(ina260.mask_enable)) + + # reset the whole chip and wait 2 sec + ina260.reset_bit = True + time.sleep(2) + print("MASK_REGISTER check, must be 0x0000 after reset: {}".format(ina260.mask_enable)) From 5ce28203ea8eba528c3c999e42ddf8696eb30bd4 Mon Sep 17 00:00:00 2001 From: Gabriele Pongelli Date: Tue, 5 Jan 2021 12:20:04 +0100 Subject: [PATCH 14/17] reformat after black --- examples/ina260_latch.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/examples/ina260_latch.py b/examples/ina260_latch.py index cd5c201..b801be3 100644 --- a/examples/ina260_latch.py +++ b/examples/ina260_latch.py @@ -3,7 +3,7 @@ from adafruit_ina260 import INA260, Mode, ConversionTime -if __name__ == '__main__': +if __name__ == "__main__": try: i2c = board.I2C() ina260 = INA260(i2c) @@ -33,7 +33,8 @@ % (ina260.current, ina260.voltage, ina260.power) ) - # supposing meanwhile the alert limit was exceeded, setting an higher limit and clear the ALERT + # supposing meanwhile the alert limit was exceeded, setting an higher limit + # and clear the ALERT # 0x0100 x 1,25 mA = 320 mA as alert limit ina260.alert_limit = 0x0100 @@ -46,4 +47,8 @@ # reset the whole chip and wait 2 sec ina260.reset_bit = True time.sleep(2) - print("MASK_REGISTER check, must be 0x0000 after reset: {}".format(ina260.mask_enable)) + print( + "MASK_REGISTER check, must be 0x0000 after reset: {}".format( + ina260.mask_enable + ) + ) From dd2319874ad0f57c48cbb9a8ad471549a6d230b6 Mon Sep 17 00:00:00 2001 From: Gabriele Pongelli Date: Tue, 5 Jan 2021 12:29:43 +0100 Subject: [PATCH 15/17] LF fixes --- examples/ina260_latch.py | 108 +++++++++++++++++++-------------------- 1 file changed, 54 insertions(+), 54 deletions(-) diff --git a/examples/ina260_latch.py b/examples/ina260_latch.py index b801be3..76aa474 100644 --- a/examples/ina260_latch.py +++ b/examples/ina260_latch.py @@ -1,54 +1,54 @@ -import time -import board -from adafruit_ina260 import INA260, Mode, ConversionTime - - -if __name__ == "__main__": - try: - i2c = board.I2C() - ina260 = INA260(i2c) - except RuntimeError as r_e: - # catch exception on init, no INA260 chip found - print(r_e) - raise r_e - else: - # set overcurrent limit flag and threshold value - # 0x0008 x 1,25 mA = 10 mA as alert limit - ina260.alert_limit = 0x0008 - - # alert pin is asserted, can be check with gpiozero - ina260.overcurrent_limit = True - - # keep the flag high until MASK_ENABLE register will be read - ina260.alert_latch_enable = True - - ina260.mode = Mode.CONTINUOUS - - # set higher conversion time and wait its value before each read - ina260.current_conversion_time = ConversionTime.TIME_8_244_ms - for _ in enumerate(range(5)): - time.sleep(ConversionTime.get_seconds(ina260.current_conversion_time)) - print( - "Current: %.2f mA Voltage: %.2f V Power:%.2f mW" - % (ina260.current, ina260.voltage, ina260.power) - ) - - # supposing meanwhile the alert limit was exceeded, setting an higher limit - # and clear the ALERT - # 0x0100 x 1,25 mA = 320 mA as alert limit - ina260.alert_limit = 0x0100 - - # alert function flag bit should be true if alert threshold was exceeded - print("Alert function flag: {}".format(ina260.alert_function_flag)) - - # in latch mode, reading the register clears the ALERT & alert function flag - print("MASK register: {}".format(ina260.mask_enable)) - - # reset the whole chip and wait 2 sec - ina260.reset_bit = True - time.sleep(2) - print( - "MASK_REGISTER check, must be 0x0000 after reset: {}".format( - ina260.mask_enable - ) - ) +import time +import board +from adafruit_ina260 import INA260, Mode, ConversionTime + + +if __name__ == "__main__": + try: + i2c = board.I2C() + ina260 = INA260(i2c) + except RuntimeError as r_e: + # catch exception on init, no INA260 chip found + print(r_e) + raise r_e + else: + # set overcurrent limit flag and threshold value + # 0x0008 x 1,25 mA = 10 mA as alert limit + ina260.alert_limit = 0x0008 + + # alert pin is asserted, can be check with gpiozero + ina260.overcurrent_limit = True + + # keep the flag high until MASK_ENABLE register will be read + ina260.alert_latch_enable = True + + ina260.mode = Mode.CONTINUOUS + + # set higher conversion time and wait its value before each read + ina260.current_conversion_time = ConversionTime.TIME_8_244_ms + for _ in enumerate(range(5)): + time.sleep(ConversionTime.get_seconds(ina260.current_conversion_time)) + print( + "Current: %.2f mA Voltage: %.2f V Power:%.2f mW" + % (ina260.current, ina260.voltage, ina260.power) + ) + + # supposing meanwhile the alert limit was exceeded, setting an higher limit + # and clear the ALERT + # 0x0100 x 1,25 mA = 320 mA as alert limit + ina260.alert_limit = 0x0100 + + # alert function flag bit should be true if alert threshold was exceeded + print("Alert function flag: {}".format(ina260.alert_function_flag)) + + # in latch mode, reading the register clears the ALERT & alert function flag + print("MASK register: {}".format(ina260.mask_enable)) + + # reset the whole chip and wait 2 sec + ina260.reset_bit = True + time.sleep(2) + print( + "MASK_REGISTER check, must be 0x0000 after reset: {}".format( + ina260.mask_enable + ) + ) From c3b5aff336076d02b02d2c04c1110b1c38deed18 Mon Sep 17 00:00:00 2001 From: Gabriele Pongelli Date: Tue, 9 Feb 2021 12:10:24 +0100 Subject: [PATCH 16/17] Update adafruit_ina260.py add empty rows to resolve merge conflicts --- adafruit_ina260.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/adafruit_ina260.py b/adafruit_ina260.py index f03a219..997a311 100644 --- a/adafruit_ina260.py +++ b/adafruit_ina260.py @@ -43,6 +43,7 @@ # pylint: disable=too-few-public-methods class Mode: """Modes avaible to be set + +--------------------+---------------------------------------------------------------------+ | Mode | Description | +====================+=====================================================================+ @@ -57,6 +58,7 @@ class Mode: | ``Mode.SHUTDOWN`` | Shutdown the sensor, reducing the quiescent current and turning off| | | current into the device inputs. Set another mode to re-enable | +--------------------+---------------------------------------------------------------------+ + """ SHUTDOWN = const(0x0) @@ -66,6 +68,7 @@ class Mode: class ConversionTime: """Options for ``current_conversion_time`` or ``voltage_conversion_time`` + +----------------------------------+------------------+ | ``ConversionTime`` | Time | +==================================+==================+ @@ -85,6 +88,7 @@ class ConversionTime: +----------------------------------+------------------+ | ``ConversionTime.TIME_8_244_ms`` | 8.244 ms | +----------------------------------+------------------+ + """ TIME_140_us = const(0x0) @@ -114,6 +118,7 @@ def get_seconds(time_enum): class AveragingCount: """Options for ``averaging_count`` + +-------------------------------+------------------------------------+ | ``AveragingCount`` | Number of measurements to average | +===============================+====================================+ @@ -133,6 +138,7 @@ class AveragingCount: +-------------------------------+------------------------------------+ | ``AveragingCount.COUNT_1024`` | 1024 | +-------------------------------+------------------------------------+ + """ COUNT_1 = const(0x0) @@ -156,8 +162,10 @@ def get_averaging_count(avg_count): class INA260: """Driver for the INA260 power and current sensor. + :param ~busio.I2C i2c_bus: The I2C bus the INA260 is connected to. :param address: The I2C device address for the sensor. Default is ``0x40``. + """ def __init__(self, i2c_bus, address=0x40): From d18ef77b94b68f6f0cd3682f37036844f0b3073f Mon Sep 17 00:00:00 2001 From: gpongelli Date: Tue, 16 Feb 2021 09:26:45 +0100 Subject: [PATCH 17/17] added copyright --- examples/ina260_latch.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/examples/ina260_latch.py b/examples/ina260_latch.py index 76aa474..85da4df 100644 --- a/examples/ina260_latch.py +++ b/examples/ina260_latch.py @@ -1,3 +1,7 @@ +# SPDX-FileCopyrightText: Gabriele Pongelli 2021 for Adafruit Industries +# +# SPDX-License-Identifier: MIT + import time import board from adafruit_ina260 import INA260, Mode, ConversionTime