rcbench
Servo and ESC test bench: panel, IOmcu and shared C
Loading...
Searching...
No Matches
ina3221.h
Go to the documentation of this file.
1
31
32#pragma once
33
34#include <stdbool.h>
35#include <stdint.h>
36
37#include "sense_bus.h"
38
39#ifdef __cplusplus
40extern "C" {
41#endif
42
48enum {
49 INA3221_CONFIG = 0x00,
51 INA3221_BUS1 = 0x02,
52 INA3221_MASK_ENABLE = 0x0F,
53 INA3221_MANUFACTURER = 0xFE,
54 INA3221_DIE_ID = 0xFF,
55};
56
60#define INA3221_MAKER_ID 0x5449u
61#define INA3221_DIE 0x3220u
62#define INA3221_ADDR_MIN 0x40u
63#define INA3221_ADDR_MAX 0x43u
64#define INA3221_CHANNELS 3u
65
70#define INA3221_SHUNT_MIN_UOHM 100u
71
73#define INA3221_CONFIG_RST 0x8000u
74#define INA3221_CONFIG_CH1EN 0x4000u
75#define INA3221_CONFIG_CH2EN 0x2000u
76#define INA3221_CONFIG_CH3EN 0x1000u
77#define INA3221_CONFIG_RESET 0x7127u
78
84enum {
85 INA3221_MODE_POWER_DOWN = 0,
86 INA3221_MODE_SHUNT_CONT = 5,
87 INA3221_MODE_BUS_CONT = 6,
89};
90
96#define INA3221_ME_SCC1 0x4000u
97#define INA3221_ME_SCC2 0x2000u
98#define INA3221_ME_SCC3 0x1000u
99#define INA3221_ME_WEN 0x0800u
100#define INA3221_ME_CEN 0x0400u
101#define INA3221_ME_CF1 0x0200u
102#define INA3221_ME_CF2 0x0100u
103#define INA3221_ME_CF3 0x0080u
104#define INA3221_ME_SF 0x0040u
105#define INA3221_ME_WF1 0x0020u
106#define INA3221_ME_WF2 0x0010u
107#define INA3221_ME_WF3 0x0008u
108#define INA3221_ME_PVF 0x0004u
109#define INA3221_ME_TCF 0x0002u
110#define INA3221_ME_CVRF 0x0001u
111
118#define INA3221_CONFIG_BENCH_CH1 0x4007u
119#define INA3221_CONFIG_BENCH_ALL 0x7007u
120
133uint16_t ina3221_config(uint8_t channels, uint8_t avg, uint8_t vbusct,
134 uint8_t vshct, uint8_t mode);
135
142uint8_t ina3221_channels(uint16_t config);
143
151uint32_t ina3221_ct_us(uint8_t code);
152
164uint32_t ina3221_cycle_us(uint16_t config);
165
173bool ina3221_identity_ok(uint16_t maker, uint16_t die);
174
182int32_t ina3221_code13(uint16_t raw);
183
193sense_value_t ina3221_current_ua(uint32_t shunt_uohm, int32_t code);
194
206int32_t ina3221_end_ua(uint32_t shunt_uohm, sense_clip_t end);
207
214int32_t ina3221_bus_mv(int32_t code);
215
216/* -------------------------------------------------------------- driver */
217
224
228typedef struct {
229 sense_part_t part;
232 uint16_t config;
233} ina3221_t;
234
251 uint32_t shunt_uohm, uint16_t config);
252
268 uint8_t addr,
269 const uint32_t *shunt_uohm,
270 uint16_t config);
271
283bool ina3221_step(ina3221_t *d, uint32_t now_ms);
284
292
306
315sense_err_t ina3221_read_bus(ina3221_t *d, uint8_t ch, int32_t *mv);
316
324sense_err_t ina3221_read_flags(ina3221_t *d, uint16_t *mask_enable);
325
343sense_err_t ina3221_verify(ina3221_t *d, bool *lost);
344
345#ifdef __cplusplus
346}
347#endif
int32_t ina3221_bus_mv(int32_t code)
A bus code in mV, 8mV a step.
Definition ina3221.c:108
ina3221_setup_err_t ina3221_init(ina3221_t *d, sense_bus_t *bus, uint8_t addr, uint32_t shunt_uohm, uint16_t config)
A part at addr with one shunt value for its channels and a Configuration value (INA3221_CONFIG_BENCH_...
Definition ina3221.c:115
sense_state_t ina3221_state(const ina3221_t *d)
The part's state as of the last probe.
Definition ina3221.c:210
sense_err_t ina3221_read_flags(ina3221_t *d, uint16_t *mask_enable)
Mask/Enable as read; the read clears its flags.
Definition ina3221.c:285
uint16_t ina3221_config(uint8_t channels, uint8_t avg, uint8_t vbusct, uint8_t vshct, uint8_t mode)
Configuration from its fields. channels: bit 0 CH1, bit 1 CH2, bit 2 CH3, which the register holds in...
Definition ina3221.c:18
bool ina3221_identity_ok(uint16_t maker, uint16_t die)
Whether the manufacturer and die IDs name an INA3221.
Definition ina3221.c:75
int32_t ina3221_code13(uint16_t raw)
The 13-bit value in bits 15-3 of a shunt or bus register: -4096 to 4095.
Definition ina3221.c:80
uint32_t ina3221_cycle_us(uint16_t config)
Time from one result to the next on every enabled channel, µs, with averaging at 1: per channel the s...
Definition ina3221.c:57
uint8_t ina3221_channels(uint16_t config)
The channels a configuration enables: bit 0 CH1 to bit 2 CH3.
Definition ina3221.c:36
sense_err_t ina3221_verify(ina3221_t *d, bool *lost)
The Configuration register read back against the value written.
Definition ina3221.c:290
sense_err_t ina3221_read_bus(ina3221_t *d, uint8_t ch, int32_t *mv)
Channel ch (1 to 3) bus voltage, mV, at IN-; the same refusals.
Definition ina3221.c:271
sense_value_t ina3221_current_ua(uint32_t shunt_uohm, int32_t code)
A shunt code as current, µA: 40µV a step over shunt_uohm.
Definition ina3221.c:85
bool ina3221_step(ina3221_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 ina3221.c:202
int32_t ina3221_end_ua(uint32_t shunt_uohm, sense_clip_t end)
The current the end code of the range stands for, µA: code 4095 for SENSE_CLIP_HIGH (1....
Definition ina3221.c:99
sense_err_t ina3221_read_current(ina3221_t *d, uint8_t ch, sense_value_t *ua)
Channel ch (1 to 3) current, µA.
Definition ina3221.c:256
uint32_t ina3221_ct_us(uint8_t code)
Conversion time of a VBUSCT or VSHCT code, µs, typical (Table 8-4; the maxima in §6....
Definition ina3221.c:51
ina3221_setup_err_t ina3221_init_shunts(ina3221_t *d, sense_bus_t *bus, uint8_t addr, const uint32_t *shunt_uohm, uint16_t config)
The same with a shunt for each channel, shunt_uohm[0] CH1's.
Definition ina3221.c:130
@ INA3221_SHUNT1
Definition ina3221.h:50
@ INA3221_BUS1
Definition ina3221.h:51
ina3221_setup_err_t
Definition ina3221.h:218
@ INA3221_SETUP_NO_CHANNEL
Definition ina3221.h:222
@ INA3221_SETUP_BAD_ADDR
Definition ina3221.h:220
@ INA3221_SETUP_NO_SHUNT
Definition ina3221.h:221
@ INA3221_MODE_CONT
Definition ina3221.h:88
#define INA3221_CHANNELS
Definition ina3221.h:64
The sensor bus: one I2C (Inter-Integrated Circuit) bus with current monitors on it,...
sense_clip_t
A reading that may be at the end of its register's range.
Definition sense_bus.h:329
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
One INA3221 on a sensor bus: its part record, shunts and Configuration.
Definition ina3221.h:228
uint32_t shunt_uohm[INA3221_CHANNELS]
Definition ina3221.h:230
uint16_t config
Definition ina3221.h:232
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