periph / devices

Go·Hardware·Lean - Device drivers
https://periph.io
Apache License 2.0
85 stars 42 forks source link

ina219: Overflow bit indicates a math overflow error #73

Open mid-kid opened 4 months ago

mid-kid commented 4 months ago

Describe the bug In ina219.go there's a check for the overflow flag respresented in the voltage register. This error reads bus voltage register overflow. This is a misnomer.

This register is documented in the datasheet as follows:

Math Overflow Flag

The Math Overflow Flag (OVF) is set when the Power or Current calculations are out of range. It indicates that
current and power data may be meaningless.

This flag indicates not an overflow in the voltage register, but an overflow in the power and current registers, which are read out later in the code.

This is unfortunate, because the data in the voltage register is completely valid in such a state. The problem would be a misconfiguration in the calibration register, causing the internal calculations from shunt voltage to current and power to fail. The sad thing about this, is that a lot of precision is lost in this conversion, as the registers the chip is working with are very small (16 bits). I'd like to retrieve the Voltage value, and do the calculations myself in the code instead of on the chip.

Expected behavior I would like the library to clarify the error message, and ideally provide a way to retrieve the measured full-scale voltage and shunt voltage regardless of this calculation overflow.

Personally I also think this library would be more useful calculating the current/power itself instead of relying on the chip, but that's out of the scope of this specific request.

Additional context Device datasheet: https://www.ti.com/lit/gpn/ina219