|
1 |
| -# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries |
2 | 1 | # SPDX-FileCopyrightText: Copyright (c) 2025 Liz Clark for Adafruit Industries
|
3 | 2 | #
|
4 | 3 | # SPDX-License-Identifier: MIT
|
5 | 4 | """
|
6 | 5 | `adafruit_ina23x`
|
7 | 6 | ================================================================================
|
8 | 7 |
|
9 |
| -CircuitPython driver for the INA237 and INA238 DC Current Voltage Power Monitor |
| 8 | +CircuitPython driver for the INA237 and INA238 DC Current Voltage Power Monitors |
10 | 9 |
|
11 | 10 |
|
12 | 11 | * Author(s): Liz Clark
|
|
16 | 15 |
|
17 | 16 | **Hardware:**
|
18 | 17 |
|
19 |
| -.. todo:: Add links to any specific hardware product page(s), or category page(s). |
20 |
| - Use unordered list & hyperlink rST inline format: "* `Link Text <url>`_" |
| 18 | +* `Adafruit INA237 Breakout <https://www.adafruit.com/product/6340>`_ |
| 19 | +* `Adafruit INA238 Breakout <https://www.adafruit.com/product/6349>`_ |
21 | 20 |
|
22 | 21 | **Software and Dependencies:**
|
23 | 22 |
|
24 | 23 | * Adafruit CircuitPython firmware for the supported boards:
|
25 | 24 | https://circuitpython.org/downloads
|
26 | 25 |
|
27 |
| -.. todo:: Uncomment or remove the Bus Device and/or the Register library dependencies |
28 |
| - based on the library's use of either. |
29 |
| -
|
30 |
| -# * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice |
31 |
| -# * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register |
| 26 | +* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice |
| 27 | +* Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register |
| 28 | +* Adafruit CircuitPython INA228 library: https://github.com/adafruit/Adafruit_CircuitPython_INA228 |
32 | 29 | """
|
33 | 30 |
|
34 |
| -# imports |
| 31 | +import time |
| 32 | + |
| 33 | +from adafruit_ina228 import INA2XX, AlertType |
| 34 | +from adafruit_register.i2c_bit import ROBit |
| 35 | +from adafruit_register.i2c_bits import ROBits, RWBits |
| 36 | +from adafruit_register.i2c_struct import ROUnaryStruct |
| 37 | +from micropython import const |
| 38 | + |
| 39 | +try: |
| 40 | + import typing # pylint: disable=unused-import |
| 41 | + |
| 42 | + from busio import I2C |
| 43 | +except ImportError: |
| 44 | + pass |
35 | 45 |
|
36 | 46 | __version__ = "0.0.0+auto.0"
|
37 | 47 | __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_INA23x.git"
|
| 48 | + |
| 49 | +_SOVL = const(0x0C) # Shunt Overvoltage Threshold |
| 50 | +_SUVL = const(0x0D) # Shunt Undervoltage Threshold |
| 51 | +_BOVL = const(0x0E) # Bus Overvoltage Threshold |
| 52 | +_BUVL = const(0x0F) # Bus Undervoltage Threshold |
| 53 | +_TEMPLIMIT = const(0x10) # Temperature Over-Limit Threshold |
| 54 | +_PWRLIMIT = const(0x11) # Power Over-Limit Threshold |
| 55 | + |
| 56 | +# Constants |
| 57 | +_INA237_DEVICE_ID = const(0x237) |
| 58 | +_INA238_DEVICE_ID = const(0x238) |
| 59 | + |
| 60 | + |
| 61 | +class INA23X(INA2XX): # noqa: PLR0904 |
| 62 | + """Driver for the INA237/INA238 current and power sensor. |
| 63 | +
|
| 64 | + :param ~busio.I2C i2c_bus: The I2C bus the INA23X is connected to. |
| 65 | + :param int address: The I2C device address. Defaults to :const:`0x40` |
| 66 | + :param bool skip_reset: Skip resetting the device on init. Defaults to False. |
| 67 | + """ |
| 68 | + |
| 69 | + # INA23X-specific register bits |
| 70 | + _alert_type = RWBits(7, 0x0B, 5, register_width=2, lsb_first=False) |
| 71 | + _conversion_ready = ROBit(0x0B, 1, register_width=2, lsb_first=False) |
| 72 | + _alert_flags = ROBits(12, 0x0B, 0, register_width=2, lsb_first=False) |
| 73 | + |
| 74 | + _raw_vshunt = ROUnaryStruct(0x04, ">h") |
| 75 | + _raw_current = ROUnaryStruct(0x07, ">h") |
| 76 | + _raw_power = ROUnaryStruct(0x08, ">H") |
| 77 | + |
| 78 | + def __init__(self, i2c_bus: I2C, address: int = 0x40, skip_reset: bool = False) -> None: |
| 79 | + super().__init__(i2c_bus, address, skip_reset) |
| 80 | + |
| 81 | + # Verify device ID (both INA237 and INA238 use compatible IDs) |
| 82 | + if self.device_id not in {_INA237_DEVICE_ID, _INA238_DEVICE_ID}: |
| 83 | + raise ValueError("Failed to find INA237/INA238 - incorrect device ID") |
| 84 | + |
| 85 | + # Set INA23X defaults |
| 86 | + self.set_calibration(0.015, 10.0) |
| 87 | + |
| 88 | + def set_calibration(self, shunt_res: float = 0.015, max_current: float = 10.0) -> None: |
| 89 | + """Set the calibration based on shunt resistance and maximum expected current. |
| 90 | +
|
| 91 | + :param float shunt_res: Shunt resistance in ohms |
| 92 | + :param float max_current: Maximum expected current in amperes |
| 93 | + """ |
| 94 | + self._shunt_res = shunt_res |
| 95 | + # INA237/238 uses 2^15 as divisor |
| 96 | + self._current_lsb = max_current / (1 << 15) |
| 97 | + self._update_shunt_cal() |
| 98 | + |
| 99 | + def _update_shunt_cal(self) -> None: |
| 100 | + """Update the shunt calibration register.""" |
| 101 | + # Scale factor based on ADC range |
| 102 | + scale = 4 if self._adc_range else 1 |
| 103 | + |
| 104 | + # INA237/238 formula: SHUNT_CAL = 819.2 × 10^6 × CURRENT_LSB × RSHUNT × scale |
| 105 | + shunt_cal = int(819.2e6 * self._current_lsb * self._shunt_res * scale) |
| 106 | + self._shunt_cal = min(shunt_cal, 0xFFFF) |
| 107 | + |
| 108 | + @property |
| 109 | + def die_temperature(self) -> float: |
| 110 | + """Die temperature in degrees Celsius.""" |
| 111 | + # INA237/238 uses 12 bits (15:4) with 125 m°C/LSB |
| 112 | + return (self._raw_dietemp >> 4) * 0.125 |
| 113 | + |
| 114 | + @property |
| 115 | + def bus_voltage(self) -> float: |
| 116 | + """Bus voltage in volts.""" |
| 117 | + # INA237/238 uses 3.125 mV/LSB |
| 118 | + return self._raw_vbus * 0.003125 |
| 119 | + |
| 120 | + @property |
| 121 | + def shunt_voltage(self) -> float: |
| 122 | + """Shunt voltage in volts.""" |
| 123 | + # Scale depends on ADC range |
| 124 | + scale = 1.25e-6 if self._adc_range else 5.0e-6 # µV/LSB |
| 125 | + return self._raw_vshunt * scale |
| 126 | + |
| 127 | + @property |
| 128 | + def current(self) -> float: |
| 129 | + """Current in amperes.""" |
| 130 | + return self._raw_current * self._current_lsb |
| 131 | + |
| 132 | + @property |
| 133 | + def power(self) -> float: |
| 134 | + """Power in watts.""" |
| 135 | + # INA237/238 power LSB = 20 × current_lsb |
| 136 | + return self._raw_power * 20.0 * self._current_lsb |
| 137 | + |
| 138 | + @property |
| 139 | + def conversion_ready(self) -> bool: |
| 140 | + """Check if conversion is complete.""" |
| 141 | + return bool(self._conversion_ready) |
| 142 | + |
| 143 | + @property |
| 144 | + def alert_type(self) -> int: |
| 145 | + """Alert type configuration.""" |
| 146 | + return self._alert_type |
| 147 | + |
| 148 | + @alert_type.setter |
| 149 | + def alert_type(self, value: int) -> None: |
| 150 | + # Alert type can be a combination of flags, so we check if all bits are valid |
| 151 | + valid_mask = ( |
| 152 | + AlertType.CONVERSION_READY |
| 153 | + | AlertType.OVERTEMPERATURE |
| 154 | + | AlertType.OVERPOWER |
| 155 | + | AlertType.UNDERVOLTAGE |
| 156 | + | AlertType.OVERVOLTAGE |
| 157 | + | AlertType.UNDERSHUNT |
| 158 | + | AlertType.OVERSHUNT |
| 159 | + ) |
| 160 | + if value & ~valid_mask: |
| 161 | + raise ValueError( |
| 162 | + f"Invalid alert type 0x{value:02X}. Must be a combination of AlertType.* constants" |
| 163 | + ) |
| 164 | + self._alert_type = value |
| 165 | + |
| 166 | + @property |
| 167 | + def alert_flags(self) -> int: |
| 168 | + """Current alert flags.""" |
| 169 | + return self._alert_flags |
0 commit comments