12#if DETWS_ENABLE_INA219
16 return (int32_t)((raw >> 3) * 4);
21 return (int32_t)raw * 10;
26 uint32_t denom = current_lsb_ua * shunt_mohm;
30 uint32_t cal = 40960000u / denom;
31 return (uint16_t)(cal > 0xFFFF ? 0xFFFF : cal);
36 return (int32_t)((int64_t)raw * current_lsb_ua);
41 return (int32_t)((int64_t)raw * 20 * current_lsb_ua);
65bool wr16(uint8_t reg, uint16_t v)
67 Wire.beginTransmission(s_ina.addr);
69 Wire.write((uint8_t)(v >> 8));
70 Wire.write((uint8_t)(v & 0xFF));
71 return Wire.endTransmission() == 0;
74bool rd16(uint8_t reg, uint16_t *v)
76 Wire.beginTransmission(s_ina.addr);
78 if (Wire.endTransmission(
false) != 0)
80 if (Wire.requestFrom((
int)s_ina.addr, 2) != 2)
82 uint8_t hi = (uint8_t)Wire.read();
83 uint8_t lo = (uint8_t)Wire.read();
84 *v = (uint16_t)(((uint16_t)hi << 8) | lo);
89bool ina219_begin(uint8_t addr, uint32_t current_lsb_ua, uint32_t shunt_mohm)
User-facing configuration for DeterministicESPAsyncWebServer.
#define DETWS_INA219_CURRENT_LSB_UA
Default INA219 current LSB in microamps per bit (calibration input). The fallback when ina219_begin()...
#define DETWS_INA219_I2C_ADDR
I2C address of the INA219 (0x40 default; the A0/A1 pins select 0x40..0x4F).
#define DETWS_INA219_SHUNT_MOHM
Default INA219 shunt resistance in milliohms (calibration input). The fallback when ina219_begin() is...
The one owner of the shared I2C bus bring-up for the peripheral drivers.
void detws_i2c_begin()
Bring up the shared I2C bus on DETWS_I2C_SDA_PIN / DETWS_I2C_SCL_PIN (-1 = default).
TI INA219 high-side current / power monitor codec (DETWS_ENABLE_INA219).
uint16_t ina219_calibration(uint32_t current_lsb_ua, uint32_t shunt_mohm)
Compute the calibration register from the current LSB (microamps per bit) and the shunt resistance (m...
bool ina219_read_bus_mv(int32_t *millivolts)
Read the bus voltage into millivolts.
#define INA219_REG_SHUNT
shunt voltage
int32_t ina219_power_uw(int16_t raw, uint32_t current_lsb_ua)
Scale the raw power register to microwatts (power LSB is 20 * current LSB).
bool ina219_read_shunt_uv(int32_t *microvolts)
Read the shunt voltage into microvolts.
int32_t ina219_bus_mv(uint16_t raw)
Decode the bus-voltage register to millivolts (value is bits [15:3], LSB 4 mV).
int32_t ina219_shunt_uv(int16_t raw)
Decode the shunt-voltage register to microvolts (signed, LSB 10 uV).
bool ina219_begin(uint8_t addr, uint32_t current_lsb_ua, uint32_t shunt_mohm)
Program the INA219 at addr: write the calibration for current_lsb_ua (uA/bit) and shunt_mohm (millioh...
#define INA219_REG_CALIBRATION
calibration
#define INA219_REG_CONFIG
configuration
bool ina219_read_current_ua(int32_t *microamps)
Read the current into microamps (needs the calibration set by ina219_begin).
#define INA219_REG_BUS
bus voltage
bool ina219_read_power_uw(int32_t *microwatts)
Read the power into microwatts (needs the calibration set by ina219_begin).
#define INA219_REG_POWER
power
int32_t ina219_current_ua(int16_t raw, uint32_t current_lsb_ua)
Scale the raw current register to microamps (raw * current_lsb_ua).
#define INA219_REG_CURRENT
current