21 return (int32_t)((raw >> 3) * 4);
26 return (int32_t)raw * 10;
31 uint32_t denom = current_lsb_ua * shunt_mohm;
37 uint32_t cal = 40960000u / denom;
38 return (uint16_t)(cal > 0xFFFF ? 0xFFFF : cal);
43 return (int32_t)((int64_t)raw * current_lsb_ua);
48 return (int32_t)((int64_t)raw * 20 * current_lsb_ua);
68bool wr16(uint8_t reg, uint16_t v)
70 Wire.beginTransmission(s_ina.addr);
72 Wire.write((uint8_t)(v >> 8));
73 Wire.write((uint8_t)(v & 0xFF));
74 return Wire.endTransmission() == 0;
77bool rd16(uint8_t reg, uint16_t *v)
79 Wire.beginTransmission(s_ina.addr);
81 if (Wire.endTransmission(
false) != 0)
85 if (Wire.requestFrom((
int)s_ina.addr, 2) != 2)
89 uint8_t hi = (uint8_t)Wire.read();
90 uint8_t lo = (uint8_t)Wire.read();
91 *v = (uint16_t)(((uint16_t)hi << 8) | lo);
96bool pc_ina219_begin(uint8_t addr, uint32_t current_lsb_ua, uint32_t shunt_mohm)
The one owner of the shared I2C bus bring-up for the peripheral drivers.
void pc_i2c_begin()
Bring up the shared I2C bus on PC_I2C_SDA_PIN / PC_I2C_SCL_PIN (-1 = default).
TI INA219 high-side current / power monitor codec (PC_ENABLE_INA219).
int32_t pc_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 pc_ina219_read_shunt_uv(int32_t *microvolts)
Read the shunt voltage into microvolts.
#define INA219_REG_SHUNT
shunt voltage
bool pc_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...
uint16_t pc_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...
#define INA219_REG_CALIBRATION
calibration
int32_t pc_ina219_current_ua(int16_t raw, uint32_t current_lsb_ua)
Scale the raw current register to microamps (raw * current_lsb_ua).
int32_t pc_ina219_shunt_uv(int16_t raw)
Decode the shunt-voltage register to microvolts (signed, LSB 10 uV).
#define INA219_REG_CONFIG
configuration
bool pc_ina219_read_bus_mv(int32_t *millivolts)
Read the bus voltage into millivolts.
bool pc_ina219_read_current_ua(int32_t *microamps)
Read the current into microamps (needs the calibration set by pc_ina219_begin).
#define INA219_REG_BUS
bus voltage
int32_t pc_ina219_bus_mv(uint16_t raw)
Decode the bus-voltage register to millivolts (value is bits [15:3], LSB 4 mV).
bool pc_ina219_read_power_uw(int32_t *microwatts)
Read the power into microwatts (needs the calibration set by pc_ina219_begin).
#define INA219_REG_POWER
power
#define INA219_REG_CURRENT
current
User-facing configuration for ProtoCore.
#define PC_INA219_SHUNT_MOHM
Default INA219 shunt resistance in milliohms (calibration input). The fallback when pc_ina219_begin()...
#define PC_INA219_CURRENT_LSB_UA
Default INA219 current LSB in microamps per bit (calibration input). The fallback when pc_ina219_begi...
#define PC_INA219_I2C_ADDR
I2C address of the INA219 (0x40 default; the A0/A1 pins select 0x40..0x4F).