89#define INA3221_NUM_CH (3)
93#define INA3221_NUM_ALERTS (4)
146#define INA3221_MODE_MASK (0x0007)
164#define INA3221_CONV_TIME_SADC_MASK (0x0038)
182#define INA3221_CONV_TIME_BADC_MASK (0x01C0)
200#define INA3221_NUM_SAMPLES_MASK (0x0E00)
213#define INA3221_ENABLE_CH_MASK (INA3221_ENABLE_CH1 \
214 | INA3221_ENABLE_CH2 \
215 | INA3221_ENABLE_CH3)
228#define INA3221_ENABLE_SUM_CH_MASK (INA3221_ENABLE_SUM_CH1 \
229 | INA3221_ENABLE_SUM_CH2 \
230 | INA3221_ENABLE_SUM_CH3)
242#define INA3221_ENABLE_LATCH_MASK (INA3221_ENABLE_WARN_LATCH \
243 | INA3221_ENABLE_CRIT_LATCH)
284#define INA3221_DEFCONFIG (0x7127)
286#define INA3221_RESET (0x8000)
288#define INA3221_FLAG_CRIT_ALERT_CH1 (0x0200)
289#define INA3221_FLAG_CRIT_ALERT_CH2 (0x0100)
290#define INA3221_FLAG_CRIT_ALERT_CH3 (0x0080)
292#define INA3221_FLAG_SHUNT_SUM_ALERT (0x0040)
294#define INA3221_FLAG_WARN_ALERT_CH1 (0x0020)
295#define INA3221_FLAG_WARN_ALERT_CH2 (0x0010)
296#define INA3221_FLAG_WARN_ALERT_CH3 (0x0008)
298#define INA3221_FLAG_POWER_VALID (0x0004)
300#define INA3221_FLAG_TIMING_CONTROL (0x0002)
302#define INA3221_FLAG_CONV_READY (0x0001)
307#define INA3221_FLAGS_MASK (INA3221_FLAG_CRIT_ALERT_CH1 \
308 | INA3221_FLAG_CRIT_ALERT_CH2 \
309 | INA3221_FLAG_CRIT_ALERT_CH3 \
310 | INA3221_FLAG_SHUNT_SUM_ALERT \
311 | INA3221_FLAG_WARN_ALERT_CH1 \
312 | INA3221_FLAG_WARN_ALERT_CH2 \
313 | INA3221_FLAG_WARN_ALERT_CH3 \
314 | INA3221_FLAG_POWER_VALID \
315 | INA3221_FLAG_TIMING_CONTROL \
316 | INA3221_FLAG_CONV_READY)
323#if IS_USED(MODULE_INA3221_ALERTS) || defined(DOXYGEN)
379#if IS_USED(MODULE_INA3221_ALERTS) || defined(DOXYGEN)
555 *cfg &= ~INA3221_ENABLE_CH_MASK;
581 uint16_t u16 = (uint16_t)ns;
582 *cfg &= ~INA3221_NUM_SAMPLES_MASK;
608 uint16_t u16 = (uint16_t)bus;
609 *cfg &= ~INA3221_CONV_TIME_BADC_MASK;
635 uint16_t u16 = (uint16_t)shunt;
636 *cfg &= ~INA3221_CONV_TIME_SADC_MASK;
662 uint16_t u16 = (uint16_t)mode;
663 *cfg &= ~INA3221_MODE_MASK;
1081#define INA3221_TRIGGER_SHUNT(dev) \
1082 ina3221_set_mode(dev, INA3221_MODE_TRIGGER_SHUNT_ONLY)
1090#define INA3221_TRIGGER_BUS(dev) \
1091 ina3221_set_mode(dev, INA3221_MODE_TRIGGER_BUS_ONLY)
1099#define INA3221_TRIGGER_SHUNT_AND_BUS(dev) \
1100 ina3221_set_mode(dev, INA3221_MODE_TRIGGER_SHUNT_BUS)
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Low-level GPIO peripheral driver interface definitions.
ina3221_ch_t ina3221_set_crit_alert_limit(const ina3221_t *dev, ina3221_ch_t ch, int32_t in_uv)
Set critical shunt voltage alert limit to in_uv for each channel in ch.
static void ina3221_get_num_samples(const ina3221_t *dev, ina3221_num_samples_t *ns)
Read currently applied number of samples.
int ina3221_set_config(ina3221_t *dev, uint16_t cfg)
Write configuration register value to configuration register.
int ina3221_set_power_valid_upper_limit(const ina3221_t *dev, int32_t in_mv)
Set bus voltage power valid upper limit to in_mv.
ina3221_ch_t ina3221_get_crit_alert_limit(const ina3221_t *dev, ina3221_ch_t ch, int32_t out_uv[INA3221_NUM_CH])
Read critical shunt voltage alert limit for each channel in ch.
unsigned ina3221_ch_t
Flags ina3221_channel_t.
int ina3221_enable_alert(ina3221_t *dev, ina3221_alert_t alert, ina3221_alert_cb_t cb, void *arg)
Enable alert and set callback and argument for alert alert.
ina3221_mode
Device operation modes.
ina3221_num_samples
Number of samples to calculate average shunt/bus voltage value.
static ina3221_conv_time_shunt_adc_t ina3221_config_get_conv_time_shunt(uint16_t cfg)
Get shunt voltage conversion time from configuration value.
ina3221_conv_time_shunt_adc
ADC shunt voltage conversion times.
#define INA3221_NUM_SAMPLES_MASK
Number of samples to calculate average shunt/bus voltage value bit mask.
int ina3221_set_mode(ina3221_t *dev, ina3221_mode_t mode)
Update device operation mode.
static void ina3221_get_conv_time_bus_adc(const ina3221_t *dev, ina3221_conv_time_bus_adc_t *ctb)
Read currently applied bus voltage ADC conversion time.
ina3221_ch_t ina3221_read_bus_mv(const ina3221_t *dev, int16_t out_mv[INA3221_NUM_CH], uint16_t *flags)
Read bus voltages for each channel in ch.
#define INA3221_CONV_TIME_SADC_MASK
ADC shunt voltage conversion times bit mask.
static int ina3221_enable_warning_alert(ina3221_t *dev, ina3221_alert_cb_t cb, void *arg)
Wrapper around ina3221_enable_alert, to enable warning alert.
int ina3221_get_enable_sum_channel(const ina3221_t *dev, ina3221_ch_t *ch)
Read enabled channels for shunt voltage sum calculation.
ina3221_ch_t ina3221_get_warn_alert_limit(const ina3221_t *dev, ina3221_ch_t ch, int32_t out_uv[INA3221_NUM_CH])
Read warning shunt voltage alert limit for each channel in ch.
enum ina3221_enable_ch ina3221_enable_ch_t
Enable channel flags.
ina3221_dev_addr
I2C device addresses.
int ina3221_set_enable_channel(ina3221_t *dev, ina3221_ch_t ch)
Enable channels.
static ina3221_mode_t ina3221_config_get_mode(uint16_t cfg)
Get operation mode from configuration value.
ina3221_alert
Alert indices.
ina3221_enable_ch
Enable channel flags.
enum ina3221_alert ina3221_alert_t
Alert indices.
int ina3221_get_shunt_voltage_sum_alert_limit(const ina3221_t *dev, int32_t *out_uv)
Read shunt voltage sum alert limit.
ina3221_conv_time_bus_adc
ADC bus voltage conversion times.
enum ina3221_mode ina3221_mode_t
Device operation modes.
int ina3221_set_num_samples(ina3221_t *dev, ina3221_num_samples_t ns)
Update number of samples and write to configuration register.
static void ina3221_config_set_conv_time_shunt(uint16_t *cfg, ina3221_conv_time_shunt_adc_t shunt)
Set shunt voltage conversion time in configuration value.
static ina3221_num_samples_t ina3221_config_get_num_samples(uint16_t cfg)
Get number of samples from configuration value.
static void ina3221_get_enable_channel(const ina3221_t *dev, ina3221_ch_t *ch)
Read which channels are currently enabled.
enum ina3221_conv_time_shunt_adc ina3221_conv_time_shunt_adc_t
ADC shunt voltage conversion times.
static ina3221_conv_time_bus_adc_t ina3221_config_get_conv_time_bus(uint16_t cfg)
Get bus voltage conversion time from configuration value.
#define INA3221_NUM_CH
Number of channels.
static void ina3221_config_set_conv_time_bus(uint16_t *cfg, ina3221_conv_time_bus_adc_t bus)
Set bus voltage conversion time in configuration value.
int ina3221_set_latch(const ina3221_t *dev, bool warn, bool crit)
Enable latches for critical/warning alert pins.
#define INA3221_NUM_ALERTS
Number of alerts.
#define INA3221_CONV_TIME_BADC_MASK
ADC bus voltage conversion times bit mask.
int ina3221_read_shunt_sum_uv(const ina3221_t *dev, int32_t *out_uv, uint16_t *flags)
Read sum of shunt voltages.
static int ina3221_disable_power_valid_alert(ina3221_t *dev)
Wrapper around ina3221_disable_alert, to disable power valid alert.
void ina3221_calculate_current_ua(ina3221_ch_t ch, const uint16_t in_mohm[INA3221_NUM_CH], const int32_t in_uv[INA3221_NUM_CH], int32_t out_ua[INA3221_NUM_CH])
Calculate current for each channel in ch.
int ina3221_disable_alert(ina3221_t *dev, ina3221_alert_t alert)
Disable alert alert.
enum ina3221_num_samples ina3221_num_samples_t
Number of samples to calculate average shunt/bus voltage value.
ina3221_enable_sum_ch
Enable shunt voltage sum calculation channel flags.
static int ina3221_disable_critical_alert(ina3221_t *dev)
Wrapper around ina3221_disable_alert, to disable critical alert.
int ina3221_get_latch(const ina3221_t *dev, bool *warn, bool *crit)
Read enabled latches for critical/warning alert pins.
enum ina3221_conv_time_bus_adc ina3221_conv_time_bus_adc_t
ADC bus voltage conversion times.
int ina3221_init(ina3221_t *dev, const ina3221_params_t *params)
Initialize device.
static int ina3221_enable_power_valid_alert(ina3221_t *dev, ina3221_alert_cb_t cb, void *arg)
Wrapper around ina3221_enable_alert, to enable power valid alert.
enum ina3221_dev_addr ina3221_dev_addr_t
I2C device addresses.
int ina3221_get_power_valid_lower_limit(const ina3221_t *dev, int32_t *out_mv)
Read bus voltage power valid lower limit.
int ina3221_read_flags(const ina3221_t *dev, uint16_t *flags)
Read status flags.
int ina3221_set_enable_sum_channel(const ina3221_t *dev, ina3221_ch_t ch)
Enable channels for shunt voltage sum calculation.
enum ina3221_enable_latch ina3221_enable_latch_t
Enable latch flags.
static int ina3221_disable_warning_alert(ina3221_t *dev)
Wrapper around ina3221_disable_alert, to disable warning alert.
ina3221_ch_t ina3221_set_warn_alert_limit(const ina3221_t *dev, ina3221_ch_t ch, int32_t in_uv)
Set warning shunt voltage alert limit to in_uv for each channel in ch.
ina3221_ch_t ina3221_read_shunt_uv(const ina3221_t *dev, int32_t out_uv[INA3221_NUM_CH], uint16_t *flags)
Read shunt voltages for each channel in ch.
ina3221_channel
Channel flags.
static int ina3221_enable_critical_alert(ina3221_t *dev, ina3221_alert_cb_t cb, void *arg)
Wrapper around ina3221_enable_alert, to enable critical alert.
void(* ina3221_alert_cb_t)(void *arg)
INA3221 alert callback for alert pins CRT, WRN, TC, PV.
ina3221_enable_latch
Enable latch flags.
static void ina3221_get_mode(const ina3221_t *dev, ina3221_mode_t *mode)
Read currently applied device operation mode.
int ina3221_set_shunt_voltage_sum_alert_limit(const ina3221_t *dev, int32_t in_uv)
Set shunt voltage sum alert limit to in_uv.
enum ina3221_enable_sum_ch ina3221_enable_sum_ch_t
Enable shunt voltage sum calculation channel flags.
int ina3221_get_config(const ina3221_t *dev, uint16_t *cfg)
Read currently saved configuration register value.
static int ina3221_disable_timing_control_alert(ina3221_t *dev)
Wrapper around ina3221_disable_alert, to disable timing control alert.
int ina3221_get_power_valid_upper_limit(const ina3221_t *dev, int32_t *out_mv)
Read bus voltage power valid upper limit.
#define INA3221_ENABLE_CH_MASK
Enable channel bit mask.
int ina3221_reset(ina3221_t *dev)
Reset device, i.e.
static void ina3221_config_set_enabled_channels(uint16_t *cfg, ina3221_ch_t ch)
Enable measurement channels in configuration value.
enum ina3221_channel ina3221_channel_t
Channel flags.
static int ina3221_enable_timing_control_alert(ina3221_t *dev, ina3221_alert_cb_t cb, void *arg)
Wrapper around ina3221_enable_alert, to enable timing control alert.
int ina3221_set_conv_time_shunt_adc(ina3221_t *dev, ina3221_conv_time_shunt_adc_t cts)
Update conversion time of shunt voltage ADC and write to configuration register.
int ina3221_set_conv_time_bus_adc(ina3221_t *dev, ina3221_conv_time_bus_adc_t ctb)
Update conversion time of bus voltage ADC and write to configuration register.
void ina3221_calculate_power_uw(ina3221_ch_t ch, const int16_t in_mv[INA3221_NUM_CH], const int32_t in_ua[INA3221_NUM_CH], int32_t out_mw[INA3221_NUM_CH])
Calculate power from bus voltage and current values.
#define INA3221_MODE_MASK
Device operation mode bit mask.
static ina3221_ch_t ina3221_config_get_enabled_channels(uint16_t cfg)
Get enabled measurement channels from configuration value.
int ina3221_set_power_valid_lower_limit(const ina3221_t *dev, int32_t in_mv)
Set bus voltage power valid lower limit to in_mv.
static void ina3221_get_conv_time_shunt_adc(const ina3221_t *dev, ina3221_conv_time_shunt_adc_t *cts)
Read currently applied shunt voltage ADC conversion time.
static void ina3221_config_set_mode(uint16_t *cfg, ina3221_mode_t mode)
Set operation mode in configuration value.
static void ina3221_config_set_num_samples(uint16_t *cfg, ina3221_num_samples_t ns)
Set number of samples in configuration value.
@ INA3221_MODE_CONTINUOUS_SHUNT_ONLY
Continuous shunt voltage measurement only.
@ INA3221_MODE_CONTINUOUS_SHUNT_BUS
Continuous shunt and bus voltage measurement.
@ INA3221_MODE_POWER_DOWN
Sleep mode.
@ INA3221_MODE_TRIGGER_BUS_ONLY
Trigger bus voltage measurement only.
@ INA3221_MODE_TRIGGER_SHUNT_BUS
Trigger shunt and bus voltage measurement.
@ INA3221_MODE_TRIGGER_SHUNT_ONLY
Trigger shunt voltage measurement only.
@ INA3221_MODE_CONTINUOUS_BUS_ONLY
Continuous bus voltage measurement only.
@ INA3221_MODE_POWER_DOWN_
Also power down (see datasheet)
@ INA3221_NUM_SAMPLES_64
64
@ INA3221_NUM_SAMPLES_16
16
@ INA3221_NUM_SAMPLES_128
128
@ INA3221_NUM_SAMPLES_1024
1024
@ INA3221_NUM_SAMPLES_512
512
@ INA3221_NUM_SAMPLES_256
256
@ INA3221_CONV_TIME_SADC_332US
332 us
@ INA3221_CONV_TIME_SADC_204US
204 us
@ INA3221_CONV_TIME_SADC_4156US
4156 us
@ INA3221_CONV_TIME_SADC_140US
140 us
@ INA3221_CONV_TIME_SADC_1100US
1100 us
@ INA3221_CONV_TIME_SADC_588US
588 us
@ INA3221_CONV_TIME_SADC_2116US
2116 us
@ INA3221_CONV_TIME_SADC_8244US
8244 us
@ INA3221_ADDR_00
A0 connected to GND.
@ INA3221_ADDR_11
A0 connected to SCL.
@ INA3221_ADDR_10
A0 connected to SDA.
@ INA3221_ADDR_01
A0 connected to VS
@ INA3221_ALERT_WRN
Warning alert.
@ INA3221_ALERT_TC
Timing control.
@ INA3221_ALERT_CRT
Critical alert.
@ INA3221_ALERT_PV
Power valid alert.
@ INA3221_ENABLE_CH3
Enable channel 3.
@ INA3221_ENABLE_CH2
Enable channel 2.
@ INA3221_ENABLE_CH1
Enable channel 1.
@ INA3221_CONV_TIME_BADC_4156US
4156 us
@ INA3221_CONV_TIME_BADC_140US
140 us
@ INA3221_CONV_TIME_BADC_2116US
2116 us
@ INA3221_CONV_TIME_BADC_204US
204 us
@ INA3221_CONV_TIME_BADC_332US
332 us
@ INA3221_CONV_TIME_BADC_1100US
1100 us
@ INA3221_CONV_TIME_BADC_8244US
8244 us
@ INA3221_CONV_TIME_BADC_588US
588 us
@ INA3221_ENABLE_SUM_CH1
Enable sum channel 1.
@ INA3221_ENABLE_SUM_CH2
Enable sum channel 2.
@ INA3221_ENABLE_SUM_CH3
Enable sum channel 3.
@ INA3221_ENABLE_WARN_LATCH
Enable warning alert latch.
@ INA3221_ENABLE_CRIT_LATCH
Enable critical alert latch.
uint_fast8_t i2c_t
Default i2c_t type definition.
Low-level I2C peripheral driver interface definition.
Common macros and compiler attributes/pragmas configuration.
INA3221 device parameters.
gpio_t pin_warn
Warning alert pin, becomes configured as input.
gpio_t pin_crit
Critical alert pin, becomes configured as input.
uint8_t gpio_config
Configure pull up resistors for gpio pins.
gpio_t pin_pv
Power valid alert pin, becomes configured as input.
uint16_t config
Configuration.
gpio_t pin_tc
Timing control alert pin, becomes configured as input.
INA3221 device handle struct.
void * arg_crit_cb
Argument for Critical alert callback.
ina3221_params_t params
Device parameters.
ina3221_alert_cb_t pv_cb
Power Valid alert callback: executed when PV is pulled low.
ina3221_alert_cb_t warn_cb
Warning alert callback: executed when WRN is pulled low.
void * arg_warn_cb
Argument for Warning alert callback.
ina3221_alert_cb_t crit_cb
Critical alert callback: executed when CRT is pulled low.
void * arg_pv_cb
Argument for Power Valid alert callback.
ina3221_alert_cb_t tc_cb
Timing control alert callback: executed when TC is pulled low.
void * arg_tc_cb
Argument for Timing control alert callback.