DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
ina219.h
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file ina219.h
6 * @brief TI INA219 high-side current / power monitor codec (DETWS_ENABLE_INA219).
7 *
8 * The INA219 measures the voltage across a shunt resistor (LSB 10 uV) and the bus voltage (LSB
9 * 4 mV, in the upper 13 bits of its register), and - once a calibration value derived from the
10 * shunt resistance and a chosen current LSB is programmed - reports current and power directly.
11 * From those you get how much current and power a circuit draws.
12 *
13 * This codec is pure and host-tested: ::ina219_bus_mv / ::ina219_shunt_uv decode the voltage
14 * registers, ::ina219_calibration computes the calibration register, and ::ina219_current_ua /
15 * ::ina219_power_uw scale the raw current / power registers by the current LSB. On an ESP32 the
16 * binding programs the calibration + config and reads the registers over I2C (Wire); only that
17 * touches hardware.
18 *
19 * A cheap solder-and-bench-test breakout: put it in series with a load and watch the current.
20 *
21 * @author Douglas Quigg (dstroy0)
22 * @date 2026
23 */
24
25#ifndef DETERMINISTICESPASYNCWEBSERVER_INA219_H
26#define DETERMINISTICESPASYNCWEBSERVER_INA219_H
27
28#include <stdint.h>
29
30#define INA219_REG_CONFIG 0x00 ///< configuration
31#define INA219_REG_SHUNT 0x01 ///< shunt voltage
32#define INA219_REG_BUS 0x02 ///< bus voltage
33#define INA219_REG_POWER 0x03 ///< power
34#define INA219_REG_CURRENT 0x04 ///< current
35#define INA219_REG_CALIBRATION 0x05 ///< calibration
36
37/** @brief Decode the bus-voltage register to millivolts (value is bits [15:3], LSB 4 mV). */
38int32_t ina219_bus_mv(uint16_t raw);
39
40/** @brief Decode the shunt-voltage register to microvolts (signed, LSB 10 uV). */
41int32_t ina219_shunt_uv(int16_t raw);
42
43/**
44 * @brief Compute the calibration register from the current LSB (microamps per bit) and the shunt
45 * resistance (milliohms): `trunc(0.04096 / (current_LSB[A] * R[ohm]))` = `40960000 / (lsb_ua *
46 * shunt_mohm)`, clamped to 16 bits. (100 uA + 100 mohm -> 4096.) 0 on a zero denominator.
47 */
48uint16_t ina219_calibration(uint32_t current_lsb_ua, uint32_t shunt_mohm);
49
50/** @brief Scale the raw current register to microamps (raw * current_lsb_ua). */
51int32_t ina219_current_ua(int16_t raw, uint32_t current_lsb_ua);
52
53/** @brief Scale the raw power register to microwatts (power LSB is 20 * current LSB). */
54int32_t ina219_power_uw(int16_t raw, uint32_t current_lsb_ua);
55
56// --- ESP32 binding (I2C via Wire; no-ops on a host build) ------------------------------------
57
58/**
59 * @brief Program the INA219 at @p addr: write the calibration for @p current_lsb_ua (uA/bit) and
60 * @p shunt_mohm (milliohms), then the default 32 V / 320 mV continuous config. @return true on ack.
61 */
62bool ina219_begin(uint8_t addr, uint32_t current_lsb_ua, uint32_t shunt_mohm);
63
64/** @brief Read the bus voltage into @p millivolts. @return false on I2C error. */
65bool ina219_read_bus_mv(int32_t *millivolts);
66
67/** @brief Read the shunt voltage into @p microvolts. @return false on I2C error. */
68bool ina219_read_shunt_uv(int32_t *microvolts);
69
70/** @brief Read the current into @p microamps (needs the calibration set by ina219_begin). */
71bool ina219_read_current_ua(int32_t *microamps);
72
73/** @brief Read the power into @p microwatts (needs the calibration set by ina219_begin). */
74bool ina219_read_power_uw(int32_t *microwatts);
75
76#endif // DETERMINISTICESPASYNCWEBSERVER_INA219_H
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.
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...
bool ina219_read_current_ua(int32_t *microamps)
Read the current into microamps (needs the calibration set by ina219_begin).
bool ina219_read_power_uw(int32_t *microwatts)
Read the power into microwatts (needs the calibration set by ina219_begin).
int32_t ina219_current_ua(int16_t raw, uint32_t current_lsb_ua)
Scale the raw current register to microamps (raw * current_lsb_ua).