rcbench
Servo and ESC test bench: panel, IOmcu and shared C
Loading...
Searching...
No Matches
ina228.h
Go to the documentation of this file.
1
59
60#pragma once
61
62#include <stdbool.h>
63#include <stdint.h>
64
65#include "sense_bus.h"
66
67#ifdef __cplusplus
68extern "C" {
69#endif
70
72enum {
78 INA228_VBUS = 0x05,
81 INA228_POWER = 0x08,
87};
88
89#define INA228_MAKER_ID 0x5449u
90#define INA228_DIE_ID 0x228u
91#define INA228_ADDR_MIN 0x40u
92#define INA228_ADDR_MAX 0x4Fu
99#define INA228_FS_MA_LIMIT 2000000u
105#define INA228_SHUNT_CAL_ADC 4096u
106
108#define INA228_CONFIG_RST 0x8000u
109#define INA228_CONFIG_RSTACC 0x4000u
110#define INA228_CONFIG_TEMPCOMP 0x0020u
111#define INA228_CONFIG_ADCRANGE 0x0010u
112
114#define INA228_DIAG_ALATCH 0x8000u
115#define INA228_DIAG_CNVR 0x4000u
116#define INA228_DIAG_SLOWALERT 0x2000u
117#define INA228_DIAG_APOL 0x1000u
118#define INA228_DIAG_ENERGYOF 0x0800u
120#define INA228_DIAG_CHARGEOF 0x0400u
122#define INA228_DIAG_MATHOF 0x0200u
124#define INA228_DIAG_TMPOL 0x0080u
125#define INA228_DIAG_SHNTOL 0x0040u
126#define INA228_DIAG_SHNTUL 0x0020u
127#define INA228_DIAG_BUSOL 0x0010u
128#define INA228_DIAG_BUSUL 0x0008u
129#define INA228_DIAG_POL 0x0004u
130#define INA228_DIAG_CNVRF 0x0002u
131#define INA228_DIAG_MEMSTAT 0x0001u
132
139enum {
140 INA228_MODE_SHUTDOWN = 0x0,
141 INA228_MODE_CONT_BUS = 0x9,
142 INA228_MODE_CONT_SHUNT = 0xA,
143 INA228_MODE_CONT_VI = 0xB,
145};
146
155#define INA228_ADC_BENCH 0xF900u
156
158#define INA228_ADC_RESET 0xFB68u
159
170uint16_t ina228_adc_config(uint8_t mode, uint8_t vbusct, uint8_t vshct,
171 uint8_t vtct, uint8_t avg);
172
179uint32_t ina228_averages(uint8_t code);
180
190uint32_t ina228_cycle_us(uint16_t adc_config);
191
199bool ina228_identity_ok(uint16_t maker, uint16_t device);
200
201/* ------------------------------------------------------- calibration */
202
212
216typedef struct {
217 uint32_t shunt_uohm;
218 uint32_t max_ma;
219 uint16_t shunt_cal;
220 uint8_t adcrange;
222
232ina228_setup_err_t ina228_calibrate(uint32_t shunt_uohm, uint32_t max_ma,
233 ina228_cal_t *out);
234
235/* ------------------------------------------------------------ decoding */
236
244int32_t ina228_code20(uint32_t raw24);
245
253sense_value_t ina228_current_ua(const ina228_cal_t *cal, int32_t code);
254
263sense_value_t ina228_vshunt_nv(const ina228_cal_t *cal, int32_t code);
264
271int32_t ina228_vbus_uv(int32_t code);
272
279int32_t ina228_dietemp_mdegc(uint16_t raw);
280
290uint64_t ina228_power_uw(const ina228_cal_t *cal, uint32_t raw24);
291
301uint64_t ina228_energy_mj(const ina228_cal_t *cal, uint64_t raw40);
302
312int64_t ina228_charge_uc(const ina228_cal_t *cal, uint64_t raw40);
313
314/* -------------------------------------------------------------- driver */
315
316typedef struct {
317 sense_part_t part;
318 ina228_cal_t cal;
319 uint16_t config;
320 uint16_t adc_config;
321} ina228_t;
322
339 uint32_t shunt_uohm, uint32_t max_ma,
340 uint16_t adc_config);
341
353bool ina228_step(ina228_t *d, uint32_t now_ms);
354
362
389sense_err_t ina228_read_vbus(ina228_t *d, int32_t *uv);
397sense_err_t ina228_read_power(ina228_t *d, uint64_t *uw);
405sense_err_t ina228_read_dietemp(ina228_t *d, int32_t *mdegc);
413sense_err_t ina228_read_diag(ina228_t *d, uint16_t *diag);
421sense_err_t ina228_read_energy(ina228_t *d, uint64_t *mj);
430
440
460sense_err_t ina228_verify(ina228_t *d, bool *lost);
461
462#ifdef __cplusplus
463}
464#endif
uint64_t ina228_energy_mj(const ina228_cal_t *cal, uint64_t raw40)
ENERGY (40 bit, unsigned) in mJ: 16 × 3.2 × CURRENT_LSB a step (Eq.
Definition ina228.c:188
sense_err_t ina228_read_power(ina228_t *d, uint64_t *uw)
Read the power.
Definition ina228.c:394
sense_err_t ina228_read_current(ina228_t *d, sense_value_t *ua)
Readings.
Definition ina228.c:364
uint32_t ina228_cycle_us(uint16_t adc_config)
Time from one result to the next for an ADC_CONFIG, µs: the enabled inputs' conversion times,...
Definition ina228.c:53
uint16_t ina228_adc_config(uint8_t mode, uint8_t vbusct, uint8_t vshct, uint8_t vtct, uint8_t avg)
ADC_CONFIG from its fields; each is masked to its width.
Definition ina228.c:25
sense_err_t ina228_read_diag(ina228_t *d, uint16_t *diag)
DIAG_ALRT as read; the read clears the latched flags (Table 7-16).
Definition ina228.c:414
ina228_setup_err_t ina228_calibrate(uint32_t shunt_uohm, uint32_t max_ma, ina228_cal_t *out)
SHUNT_CAL and ADCRANGE for a shunt and a maximum current. out is filled only on INA228_SETUP_OK.
Definition ina228.c:79
sense_value_t ina228_current_ua(const ina228_cal_t *cal, int32_t code)
CURRENT in µA; a code at either end is a clip.
Definition ina228.c:148
sense_err_t ina228_read_vshunt(ina228_t *d, sense_value_t *nv)
Read the shunt voltage.
Definition ina228.c:374
uint32_t ina228_averages(uint8_t code)
Samples averaged for an AVG code (Table 7-6).
Definition ina228.c:47
sense_err_t ina228_read_vbus(ina228_t *d, int32_t *uv)
Read the bus voltage.
Definition ina228.c:384
sense_err_t ina228_read_dietemp(ina228_t *d, int32_t *mdegc)
Read the die temperature.
Definition ina228.c:404
bool ina228_identity_ok(uint16_t maker, uint16_t device)
Whether MANUFACTURER_ID and DEVICE_ID name an INA228.
Definition ina228.c:72
bool ina228_step(ina228_t *d, uint32_t now_ms)
Every tick: probes the part when it is due – at the first call, and SENSE_RETRY_MS after it was found...
Definition ina228.c:297
sense_err_t ina228_read_charge(ina228_t *d, int64_t *uc)
Read the charge.
Definition ina228.c:429
sense_err_t ina228_read_energy(ina228_t *d, uint64_t *mj)
Read the energy.
Definition ina228.c:419
int32_t ina228_vbus_uv(int32_t code)
VBUS in µV, 195.3125µV a step (Table 7-10).
Definition ina228.c:170
sense_err_t ina228_verify(ina228_t *d, bool *lost)
ADC_CONFIG read back against the value written.
Definition ina228.c:449
ina228_setup_err_t ina228_init(ina228_t *d, sense_bus_t *bus, uint8_t addr, uint32_t shunt_uohm, uint32_t max_ma, uint16_t adc_config)
A part at addr with a shunt, a maximum current and an ADC_CONFIG (INA228_ADC_BENCH for this bench).
Definition ina228.c:206
int32_t ina228_code20(uint32_t raw24)
The 20-bit value in bits 23-4 of a 24-bit register (VSHUNT, VBUS, CURRENT), as raw: -2^19 to 2^19 - 1...
Definition ina228.c:125
sense_value_t ina228_vshunt_nv(const ina228_cal_t *cal, int32_t code)
VSHUNT in nV: 312.5 nV a step at ADCRANGE 0, 78.125 nV at 1 (Table 7-9); a code at either end is a cl...
Definition ina228.c:160
int64_t ina228_charge_uc(const ina228_cal_t *cal, uint64_t raw40)
CHARGE (40 bit, two's complement) in µC: CURRENT_LSB a step (Eq.
Definition ina228.c:196
uint64_t ina228_power_uw(const ina228_cal_t *cal, uint32_t raw24)
POWER (24 bit, unsigned) in µW: 3.2 × CURRENT_LSB a step (Eq.
Definition ina228.c:180
sense_state_t ina228_state(const ina228_t *d)
The part's state as of the last probe.
Definition ina228.c:305
sense_err_t ina228_clear_totals(ina228_t *d)
ENERGY and CHARGE to 0: CONFIG with RSTACC, then without.
Definition ina228.c:439
int32_t ina228_dietemp_mdegc(uint16_t raw)
DIETEMP in m°C, 7.8125m°C a step (Table 7-11).
Definition ina228.c:175
@ INA228_MODE_CONT_ALL
Definition ina228.h:144
@ INA228_SHUNT_CAL
Definition ina228.h:75
@ INA228_POWER
Definition ina228.h:81
@ INA228_CURRENT
Definition ina228.h:80
@ INA228_DIAG_ALRT
Definition ina228.h:84
@ INA228_VBUS
Definition ina228.h:78
@ INA228_DIETEMP
Definition ina228.h:79
@ INA228_ENERGY
Definition ina228.h:82
@ INA228_SHUNT_TEMPCO
Definition ina228.h:76
@ INA228_DEVICE_ID
Definition ina228.h:86
@ INA228_ADC_CONFIG
Definition ina228.h:74
@ INA228_CONFIG
Definition ina228.h:73
@ INA228_MANUFACTURER
Definition ina228.h:85
@ INA228_VSHUNT
Definition ina228.h:77
@ INA228_CHARGE
Definition ina228.h:83
ina228_setup_err_t
Definition ina228.h:203
@ INA228_SETUP_BAD_ADDR
Definition ina228.h:205
@ INA228_SETUP_NO_MAX
Definition ina228.h:207
@ INA228_SETUP_NO_SHUNT
Definition ina228.h:206
@ INA228_SETUP_OVER_RANGE
Definition ina228.h:208
The sensor bus: one I2C (Inter-Integrated Circuit) bus with current monitors on it,...
sense_err_t
What a transaction came to.
Definition sense_bus.h:62
sense_state_t
A part on the bus, as far as it is known.
Definition sense_bus.h:175
The calibration of an INA228: shunt, maximum current, SHUNT_CAL and ADCRANGE.
Definition ina228.h:216
uint8_t adcrange
Definition ina228.h:220
uint32_t shunt_uohm
Definition ina228.h:217
uint16_t shunt_cal
Definition ina228.h:219
uint32_t max_ma
Definition ina228.h:218
Definition ina228.h:316
uint16_t config
Definition ina228.h:319
uint16_t adc_config
Definition ina228.h:320
Definition sense_bus.h:91
A part on the bus: its address, state and last probe.
Definition sense_bus.h:190
A measurement, or a clip.
Definition sense_bus.h:341