5
5
"""
6
6
`adafruit_ina260`
7
7
================================================================================
8
-
9
8
CircuitPython driver for the TI INA260 current and power sensor
10
-
11
-
12
9
* Author(s): Bryan Siepert
13
-
14
10
Implementation Notes
15
11
--------------------
16
-
17
12
**Hardware:**
18
-
19
13
* `INA260 Breakout <https://www.adafruit.com/products/4226>`_
20
-
21
14
**Software and Dependencies:**
22
-
23
15
* Adafruit CircuitPython firmware for the supported boards:
24
- https://github.com/adafruit/circuitpython/releases
25
-
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
16
+ * https://github.com/adafruit/circuitpython/releases
17
+ * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
18
+ * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
28
19
"""
29
20
30
21
# imports
36
27
import adafruit_bus_device .i2c_device as i2cdevice
37
28
38
29
from adafruit_register .i2c_struct import ROUnaryStruct
39
- from adafruit_register .i2c_bits import RWBits
40
- from adafruit_register .i2c_bit import ROBit
30
+ from adafruit_register .i2c_bits import ROBits , RWBits
31
+ from adafruit_register .i2c_bit import ROBit , RWBit
41
32
42
33
_REG_CONFIG = const (0x00 ) # CONFIGURATION REGISTER (R/W)
43
34
_REG_CURRENT = const (0x01 ) # SHUNT VOLTAGE REGISTER (R)
48
39
_REG_MFG_UID = const (0xFE ) # MANUFACTURER UNIQUE ID REGISTER (R)
49
40
_REG_DIE_UID = const (0xFF ) # DIE UNIQUE ID REGISTER (R)
50
41
42
+
51
43
# pylint: disable=too-few-public-methods
52
44
class Mode :
53
45
"""Modes avaible to be set
@@ -108,6 +100,21 @@ class ConversionTime:
108
100
TIME_4_156_ms = const (0x6 )
109
101
TIME_8_244_ms = const (0x7 )
110
102
103
+ @staticmethod
104
+ def get_seconds (time_enum ):
105
+ """Retrieve the time in seconds giving value read from register"""
106
+ conv_dict = {
107
+ 0 : 140e-6 ,
108
+ 1 : 204e-6 ,
109
+ 2 : 332e-6 ,
110
+ 3 : 558e-6 ,
111
+ 4 : 1.1e-3 ,
112
+ 5 : 2.116e-3 ,
113
+ 6 : 4.156e-3 ,
114
+ 7 : 8.244e-3 ,
115
+ }
116
+ return conv_dict [time_enum ]
117
+
111
118
112
119
class AveragingCount :
113
120
"""Options for ``averaging_count``
@@ -143,6 +150,12 @@ class AveragingCount:
143
150
COUNT_512 = const (0x6 )
144
151
COUNT_1024 = const (0x7 )
145
152
153
+ @staticmethod
154
+ def get_averaging_count (avg_count ):
155
+ """Retrieve the number of measurements giving value read from register"""
156
+ conv_dict = {0 : 1 , 1 : 4 , 2 : 16 , 3 : 64 , 4 : 128 , 5 : 256 , 6 : 512 , 7 : 1024 }
157
+ return conv_dict [avg_count ]
158
+
146
159
147
160
# pylint: enable=too-few-public-methods
148
161
@@ -158,12 +171,80 @@ class INA260:
158
171
def __init__ (self , i2c_bus , address = 0x40 ):
159
172
self .i2c_device = i2cdevice .I2CDevice (i2c_bus , address )
160
173
174
+ if self ._manufacturer_id != self .TEXAS_INSTRUMENT_ID :
175
+ raise RuntimeError (
176
+ "Failed to find Texas Instrument ID, read {} while expected {}"
177
+ " - check your wiring!" .format (
178
+ self ._manufacturer_id , self .TEXAS_INSTRUMENT_ID
179
+ )
180
+ )
181
+
182
+ if self ._device_id != self .INA260_ID :
183
+ raise RuntimeError (
184
+ "Failed to find INA260 ID, read {} while expected {}"
185
+ " - check your wiring!" .format (self ._device_id , self .INA260_ID )
186
+ )
187
+
161
188
_raw_current = ROUnaryStruct (_REG_CURRENT , ">h" )
162
189
_raw_voltage = ROUnaryStruct (_REG_BUSVOLTAGE , ">H" )
163
190
_raw_power = ROUnaryStruct (_REG_POWER , ">H" )
164
191
165
- _conversion_ready = ROBit (_REG_MASK_ENABLE , 3 , 2 , False )
192
+ # MASK_ENABLE fields
193
+ overcurrent_limit = RWBit (_REG_MASK_ENABLE , 15 , 2 , False )
194
+ """Setting this bit high configures the ALERT pin to be asserted if the current measurement
195
+ following a conversion exceeds the value programmed in the Alert Limit Register.
196
+ """
197
+ under_current_limit = RWBit (_REG_MASK_ENABLE , 14 , 2 , False )
198
+ """Setting this bit high configures the ALERT pin to be asserted if the current measurement
199
+ following a conversion drops below the value programmed in the Alert Limit Register.
200
+ """
201
+ bus_voltage_over_voltage = RWBit (_REG_MASK_ENABLE , 13 , 2 , False )
202
+ """Setting this bit high configures the ALERT pin to be asserted if the bus voltage measurement
203
+ following a conversion exceeds the value programmed in the Alert Limit Register.
204
+ """
205
+ bus_voltage_under_voltage = RWBit (_REG_MASK_ENABLE , 12 , 2 , False )
206
+ """Setting this bit high configures the ALERT pin to be asserted if the bus voltage measurement
207
+ following a conversion drops below the value programmed in the Alert Limit Register.
208
+ """
209
+ power_over_limit = RWBit (_REG_MASK_ENABLE , 11 , 2 , False )
210
+ """Setting this bit high configures the ALERT pin to be asserted if the Power calculation
211
+ made following a bus voltage measurement exceeds the value programmed in the
212
+ Alert Limit Register.
213
+ """
214
+ conversion_ready = RWBit (_REG_MASK_ENABLE , 10 , 2 , False )
215
+ """Setting this bit high configures the ALERT pin to be asserted when the Conversion Ready Flag,
216
+ Bit 3, is asserted indicating that the device is ready for the next conversion.
217
+ """
218
+ # from 5 to 9 are not used
219
+ alert_function_flag = ROBit (_REG_MASK_ENABLE , 4 , 2 , False )
220
+ """While only one Alert Function can be monitored at the ALERT pin at time, the
221
+ Conversion Ready can also be enabled to assert the ALERT pin.
222
+ Reading the Alert Function Flag following an alert allows the user to determine if the Alert
223
+ Function was the source of the Alert.
224
+
225
+ When the Alert Latch Enable bit is set to Latch mode, the Alert Function Flag bit
226
+ clears only when the Mask/Enable Register is read.
227
+ When the Alert Latch Enable bit is set to Transparent mode, the Alert Function Flag bit
228
+ is cleared following the next conversion that does not result in an Alert condition.
229
+ """
230
+ _conversion_ready_flag = ROBit (_REG_MASK_ENABLE , 3 , 2 , False )
231
+ """Bit to help coordinate one-shot or triggered conversion. This bit is set after all
232
+ conversion, averaging, and multiplication are complete.
233
+ Conversion Ready flag bit clears when writing the configuration register or
234
+ reading the Mask/Enable register.
235
+ """
236
+ math_overflow_flag = ROBit (_REG_MASK_ENABLE , 2 , 2 , False )
237
+ """This bit is set to 1 if an arithmetic operation resulted in an overflow error.
238
+ """
239
+ alert_polarity_bit = RWBit (_REG_MASK_ENABLE , 1 , 2 , False )
240
+ """Active-high open collector when True, Active-low open collector when false (default).
241
+ """
242
+ alert_latch_enable = RWBit (_REG_MASK_ENABLE , 0 , 2 , False )
243
+ """Configures the latching feature of the ALERT pin and Alert Flag Bits.
244
+ """
166
245
246
+ reset_bit = RWBit (_REG_CONFIG , 15 , 2 , False )
247
+ """Setting this bit t 1 generates a system reset. Reset all registers to default values."""
167
248
averaging_count = RWBits (3 , _REG_CONFIG , 9 , 2 , False )
168
249
"""The window size of the rolling average used in continuous mode"""
169
250
voltage_conversion_time = RWBits (3 , _REG_CONFIG , 6 , 2 , False )
@@ -177,28 +258,47 @@ def __init__(self, i2c_bus, address=0x40):
177
258
"""
178
259
179
260
mask_enable = RWBits (16 , _REG_MASK_ENABLE , 0 , 2 , False )
261
+ """The Mask/Enable Register selects the function that is enabled to control the ALERT pin as
262
+ well as how that pin functions.
263
+ If multiple functions are enabled, the highest significant bit
264
+ position Alert Function (D15-D11) takes priority and responds to the Alert Limit Register.
265
+ """
180
266
alert_limit = RWBits (16 , _REG_ALERT_LIMIT , 0 , 2 , False )
267
+ """The Alert Limit Register contains the value used to compare to the register selected in the
268
+ Mask/Enable Register to determine if a limit has been exceeded.
269
+ The format for this register will match the format of the register that is selected for
270
+ comparison.
271
+ """
272
+
273
+ TEXAS_INSTRUMENT_ID = const (0x5449 )
274
+ INA260_ID = const (0x227 )
275
+ _manufacturer_id = ROUnaryStruct (_REG_MFG_UID , ">H" )
276
+ """Manufacturer identification bits"""
277
+ _device_id = ROBits (12 , _REG_DIE_UID , 4 , 2 , False )
278
+ """Device identification bits"""
279
+ revision_id = ROBits (4 , _REG_DIE_UID , 0 , 2 , False )
280
+ """Device revision identification bits"""
181
281
182
282
@property
183
283
def current (self ):
184
284
"""The current (between V+ and V-) in mA"""
185
285
if self .mode == Mode .TRIGGERED :
186
- while self ._conversion_ready == 0 :
286
+ while self ._conversion_ready_flag == 0 :
187
287
pass
188
288
return self ._raw_current * 1.25
189
289
190
290
@property
191
291
def voltage (self ):
192
292
"""The bus voltage in V"""
193
293
if self .mode == Mode .TRIGGERED :
194
- while self ._conversion_ready == 0 :
294
+ while self ._conversion_ready_flag == 0 :
195
295
pass
196
296
return self ._raw_voltage * 0.00125
197
297
198
298
@property
199
299
def power (self ):
200
300
"""The power being delivered to the load in mW"""
201
301
if self .mode == Mode .TRIGGERED :
202
- while self ._conversion_ready == 0 :
302
+ while self ._conversion_ready_flag == 0 :
203
303
pass
204
304
return self ._raw_power * 10
0 commit comments