sensors/ina226: Add a uORB interface.

The INA226 driver was character mode only, and stayed that way after
everything around it moved, because the sensor framework had no type it
could publish: there was nothing for volts or amps until now.

Add the framework version beside it, in the shape the tree uses for a
part with both.  The old driver is untouched and still builds by
default; the new one replaces it when SENSORS_INA226_UORB is set.

The part publishes three topics, voltage, current and power, from a
single reading that they all share the timestamp of.  Reading once per
topic would put three transfers on the bus for one sample and, worse,
would leave the three values describing three different instants, which
is the wrong property for a power measurement: the product of a voltage
and a current measured at different moments is not the power at either.
The power is computed here rather than read from the part, because the
part's own power register needs its calibration register given a
current scale first, and multiplying two values already in hand does
not.

One worker feeds all three, so it starts when the first topic is
subscribed and stops when the last goes away, and the part is left
powered down until then rather than converting into a void.

Each topic keeps the interval it asked for and the worker runs at the
shortest of them, since one reading serves all three.  Neither is taken
at face value: asking faster than the part converts returns the same
reading twice, and a period shorter than a clock tick rounds down to no
delay at all, which would leave the worker re-queueing itself with the
bus never idle.  Both floors are applied and the caller is told what it
will actually get, which is what the interface is for.

The shunt is rejected if it is zero or negative, which would otherwise
divide by zero on the first reading.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Justin Hammond <justin@dynam.ac>
This commit is contained in:
Justin Hammond 2026-08-04 22:46:50 +08:00 • committed by Xiang Xiao
parent d741e1a818
commit cc2edca964
5 changed files with 613 additions and 1 deletions

View file

@ -142,8 +142,33 @@ extern "C"
*
****************************************************************************/
#ifndef CONFIG_SENSORS_INA226_UORB
int ina226_register(FAR const char *devpath, FAR struct i2c_master_s *i2c,
uint8_t addr, int32_t shuntval, uint16_t config);
#else
/****************************************************************************
* Name: ina226_register_uorb
*
* Description:
* Register the part as uORB voltage, current and power sensors, all
* sharing one topic number and one reading.
*
* Input Parameters:
* devno - The topic number
* i2c - An instance of the I2C interface to use
* addr - The I2C address of the INA226
* shuntval - The shunt resistance in micro-ohms
* config - Averaging and conversion times; the mode bits are managed
* by the driver
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/
int ina226_register_uorb(int devno, FAR struct i2c_master_s *i2c,
uint8_t addr, int32_t shuntval, uint16_t config);
#endif /* CONFIG_SENSORS_INA226_UORB */
#undef EXTERN
#ifdef __cplusplus