Go to the documentation of this file.
75 #define INA3221_NUM_CH (3)
77 #define INA3221_NUM_ALERTS (4)
83 typedef void (*ina3221_alert_cb_t)(void *arg);
139 #define INA3221_MODE_MASK (0x0007)
144 typedef enum ina3221_conv_time_shunt_adc {
154 #define INA3221_CONV_TIME_SADC_MASK (0x0038)
159 typedef enum ina3221_conv_time_bus_adc {
169 #define INA3221_CONV_TIME_BADC_MASK (0x01C0)
174 typedef enum ina3221_num_samples {
184 #define INA3221_NUM_SAMPLES_MASK (0x0E00)
189 typedef enum ina3221_enable_ch {
194 #define INA3221_ENABLE_CH_MASK (INA3221_ENABLE_CH1 \
195 | INA3221_ENABLE_CH2 \
196 | INA3221_ENABLE_CH3)
201 typedef enum ina3221_channel_state {
214 #define INA3221_ENABLE_SUM_CH_MASK (INA3221_ENABLE_SUM_CH1 \
215 | INA3221_ENABLE_SUM_CH2 \
216 | INA3221_ENABLE_SUM_CH3)
221 typedef enum ina3221_enable_latch {
225 #define INA3221_ENABLE_LATCH_MASK (INA3221_ENABLE_WARN_LATCH \
226 | INA3221_ENABLE_CRIT_LATCH)
261 #define INA3221_DEFCONFIG (0x7127)
263 #define INA3221_RESET (0x8000)
265 #define INA3221_FLAG_CRIT_ALERT_CH1 (0x0200)
266 #define INA3221_FLAG_CRIT_ALERT_CH2 (0x0100)
267 #define INA3221_FLAG_CRIT_ALERT_CH3 (0x0080)
269 #define INA3221_FLAG_SHUNT_SUM_ALERT (0x0040)
271 #define INA3221_FLAG_WARN_ALERT_CH1 (0x0020)
272 #define INA3221_FLAG_WARN_ALERT_CH2 (0x0010)
273 #define INA3221_FLAG_WARN_ALERT_CH3 (0x0008)
275 #define INA3221_FLAG_POWER_VALID (0x0004)
277 #define INA3221_FLAG_TIMING_CONTROL (0x0002)
279 #define INA3221_FLAG_CONV_READY (0x0001)
281 #define INA3221_FLAGS_MASK (INA3221_FLAG_CRIT_ALERT_CH1 \
282 | INA3221_FLAG_CRIT_ALERT_CH2 \
283 | INA3221_FLAG_CRIT_ALERT_CH3 \
284 | INA3221_FLAG_SHUNT_SUM_ALERT \
285 | INA3221_FLAG_WARN_ALERT_CH1 \
286 | INA3221_FLAG_WARN_ALERT_CH2 \
287 | INA3221_FLAG_WARN_ALERT_CH3 \
288 | INA3221_FLAG_POWER_VALID \
289 | INA3221_FLAG_TIMING_CONTROL \
290 | INA3221_FLAG_CONV_READY)
297 #if defined(MODULE_INA3221_ALERTS) || defined(DOXYGEN)
354 #if defined(MODULE_INA3221_ALERTS) || defined(DOXYGEN)
1028 int32_t *out_uv, uint16_t *flags);
1045 int16_t *out_mv, uint16_t *flags);
1058 int32_t *in_uv, int32_t *out_ma);
1086 size_t res_val_size);
1093 #define INA3221_TRIGGER_SHUNT(dev) \
1094 ina3221_set_mode(dev, INA3221_MODE_TRIGGER_SHUNT_ONLY)
1101 #define INA3221_TRIGGER_BUS(dev) \
1102 ina3221_set_mode(dev, INA3221_MODE_TRIGGER_BUS_ONLY)
1109 #define INA3221_TRIGGER_SHUNT_AND_BUS(dev) \
1110 ina3221_set_mode(dev, INA3221_MODE_TRIGGER_SHUNT_BUS)
static int ina3221_disable_timing_control_alert(ina3221_t *dev)
Wrapper around.
static int ina3221_enable_critical_alert(ina3221_t *dev, ina3221_alert_cb_t cb, void *arg)
Wrapper around.
int 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.
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.
@ INA3221_MODE_TRIGGER_SHUNT_ONLY
Trigger shunt voltage measurement only.
gpio_t pin_crit
Critical alert pin, becomes configured as input.
@ INA3221_ENABLE_CH3
Enable channel 3.
@ INA3221_MODE_TRIGGER_BUS_ONLY
Trigger bus voltage measurement only.
@ INA3221_CONV_TIME_SADC_8244US
8244 us
@ INA3221_ENABLE_CRIT_LATCH
Enable critical alert latch.
@ INA3221_NUM_SAMPLES_64
64
@ INA3221_CONV_TIME_BADC_588US
588 us
int ina3221_set_latch(const ina3221_t *dev, ina3221_enable_latch_t latch)
Enable latches for critical/warning alert pins.
enum ina3221_dev_addr ina3221_dav_addr_t
I2C device addresses.
#define INA3221_CONV_TIME_SADC_MASK
ADC shunt voltage conversion times bit mask.
void(* ina3221_alert_cb_t)(void *arg)
INA3221 alert callback for alert pins CRT, WRN, TC, PV.
@ INA3221_ADDR_01
A0 connected to VS
@ INA3221_CONFIG_FAILED
Device configuration failed.
static int ina3221_disable_power_valid_alert(ina3221_t *dev)
Wrapper around.
INA3221 device handle struct.
@ INA3221_CONV_TIME_BADC_1100US
1100 us
@ INA3221_CONV_TIME_BADC_4156US
4156 us
ina3221_alert
Alert indices.
@ INA3221_ENABLE_SUM_CH1
Enable sum channel 1.
@ INA3221_MODE_POWER_DOWN
Sleep mode.
@ INA3221_ALERT_CRT
Critical alert.
@ INA3221_MODE_CONTINUOUS_BUS_ONLY
Continuous bus voltage measurement only.
@ INA3221_ENABLE_SUM_CH2
Enable sum channel 2.
int ina3221_get_power_valid_upper_limit(const ina3221_t *dev, int32_t *out_mv)
Read bus voltage power valid upper limit.
@ INA3221_CONV_TIME_SADC_1100US
1100 us
int ina3221_read_shunt_uv(const ina3221_t *dev, ina3221_channel_t ch, int32_t *out_uv, uint16_t *flags)
Read shunt voltages for each channel in ch.
@ INA3221_ENABLE_WARN_LATCH
Enable warning alert latch.
@ INA3221_CONV_TIME_BADC_140US
140 us
ina3221_alert_cb_t warn_cb
Warning alert callback: executed when WRN is pulled low.
@ INA3221_CONV_TIME_BADC_204US
204 us
unsigned int i2c_t
Default i2c_t type definition.
enum ina3221_ret_val ina3221_ret_val_t
Return codes.
enum ina3221_channel_state ina3221_channel_state_t
Channel state enabled/disabled.
int ina3221_get_latch(const ina3221_t *dev, ina3221_enable_latch_t *latch)
Read enabled latches for critical/warning alert pins.
@ INA3221_OK
Everything is fine.
enum ina3221_channel ina3221_channel_t
Channel flags.
static int ina3221_set_enable_sum_channel(const ina3221_t *dev, ina3221_channel_state_t ch1, ina3221_channel_state_t ch2, ina3221_channel_state_t ch3)
Wrapper around.
static int ina3221_disable_critical_alert(ina3221_t *dev)
Wrapper around.
@ INA3221_I2C_ERROR
I2C bus acquirenment failed.
int _ina3221_get_config(const ina3221_t *dev, uint16_t *cfg)
Read currently saved configuration register value.
@ INA3221_RESET_FAILED
Device reset failed.
ina3221_alert_cb_t tc_cb
Timing control alert callback: executed when TC is pulled low.
int ina3221_set_num_samples(ina3221_t *dev, ina3221_num_samples_t ns)
Update number of samples and write to configuration register.
static int ina3221_get_enable_sum_channel(const ina3221_t *dev, ina3221_channel_state_t *ch1, ina3221_channel_state_t *ch2, ina3221_channel_state_t *ch3)
Wrapper for.
int ina3221_get_mode(const ina3221_t *dev, ina3221_mode_t *mode)
Read currently applied device operation mode.
enum ina3221_conv_time_shunt_adc ina3221_conv_time_shunt_adc_t
ADC shunt voltage conversion times.
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.
int _ina3221_get_enable_channel(const ina3221_t *dev, ina3221_enable_ch_t *ech)
Read which channels are currently enabled.
int _ina3221_disable_alert(ina3221_t *dev, ina3221_alert_t alert)
Disable alert callback and argument for alert alert.
@ INA3221_CONV_TIME_SADC_204US
204 us
int _ina3221_set_config(ina3221_t *dev, uint16_t cfg)
Write configuration register value to configuration register.
@ INA3221_MODE_POWER_DOWN_
Also power down (see datasheet)
int ina3221_set_warn_alert_limit(const ina3221_t *dev, ina3221_channel_t ch, int32_t in_uv)
Set warning shunt voltage alert limit to in_uv for each channel in ch.
static int ina3221_enable_timing_control_alert(ina3221_t *dev, ina3221_alert_cb_t cb, void *arg)
Wrapper around.
int _ina3221_set_enable_channel(ina3221_t *dev, ina3221_enable_ch_t ech)
Enable channels.
@ INA3221_CH_ENABLE
Channel enabled.
int ina3221_set_crit_alert_limit(const ina3221_t *dev, ina3221_channel_t ch, int32_t in_uv)
Set critical shunt voltage alert limit to in_uv for each channel in ch.
ina3221_enable_sum_ch
Enable shunt voltage sum calculation channel flags.
static int ina3221_get_channel_state(const ina3221_t *dev, ina3221_channel_state_t *ch1, ina3221_channel_state_t *ch2, ina3221_channel_state_t *ch3)
Wrapper around _ina3221_get_enable_channel.
uint16_t config
Configuration.
int _ina3221_enable_alert(ina3221_t *dev, ina3221_alert_t alert, ina3221_alert_cb_t cb, void *arg)
Enable alert callback and argument for alert alert.
ina3221_dev_addr
I2C device addresses.
#define INA3221_ENABLE_CH_MASK
Enable channel bit mask.
@ INA3221_ADDR_11
A0 connected to SCL.
void ina3221_ch_align(ina3221_channel_t ch, const void *in_res, void *out_res, size_t res_val_size)
Align in_res to the number of channels For example: ch = (INA3221_CH1 | INA3221_CH3) in_res = {value_...
int _ina3221_set_enable_sum_channel(const ina3221_t *dev, ina3221_enable_sum_ch_t esch)
Enable channels for shunt voltage sum calculation.
int ina3221_reset(ina3221_t *dev)
Reset device, i.e.
void * arg_crit_cb
Argument for Critical alert callback.
int 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.
enum ina3221_enable_sum_ch ina3221_enable_sum_ch_t
Enable shunt voltage sum calculation channel flags.
@ INA3221_CONV_TIME_BADC_2116US
2116 us
int _ina3221_get_enable_sum_channel(const ina3221_t *dev, ina3221_enable_sum_ch_t *esch)
Read enabled channels for shunt voltage sum calculation.
#define INA3221_NUM_SAMPLES_MASK
Number of samples to calculate average shunt/bus voltage value bit mask.
@ INA3221_ENABLE_CH1
Enable channel 1.
@ INA3221_NUM_SAMPLES_128
128
@ INA3221_NUM_SAMPLES_256
256
int ina3221_read_shunt_sum_uv(const ina3221_t *dev, int32_t *out_uv, uint16_t *flags)
Read sum of shunt voltages.
#define INA3221_NUM_ALERTS
Number of alerts.
@ INA3221_CONV_TIME_BADC_332US
332 us
@ INA3221_CONV_TIME_SADC_2116US
2116 us
@ INA3221_NUM_SAMPLES_512
512
#define INA3221_NUM_CH
Number of channels.
enum ina3221_alert ina3221_alert_t
Alert indices.
gpio_t pin_warn
Warning alert pin, becomes configured as input.
@ INA3221_ALERT_TC
Timing control.
@ INA3221_BAD_MANUF_ID
Read device manufacturer ID failed.
enum ina3221_mode ina3221_mode_t
Device operation modes.
void * arg_tc_cb
Argument for Timing control alert callback.
#define INA3221_MODE_MASK
Device operation mode bit mask.
@ INA3221_ALERT_WRN
Warning alert.
static int ina3221_disable_warning_alert(ina3221_t *dev)
Wrapper around.
@ INA3221_ADDR_00
A0 connected to GND.
@ INA3221_CONV_TIME_SADC_140US
140 us
static int ina3221_set_config(ina3221_t *dev, ina3221_enable_ch_t chs, ina3221_num_samples_t ns, ina3221_conv_time_bus_adc_t ctbadc, ina3221_conv_time_shunt_adc_t ctsadc, ina3221_mode_t mode)
Wrapper around.
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.
@ INA3221_BAD_DIE_ID
Read device DIE ID failed.
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.
int ina3221_calculate_power_uw(int16_t *in_mv, int32_t *in_ua, uint8_t num, int32_t *out_mw)
Calculate power from bus voltage and current values.
INA3221 device parameters.
enum ina3221_num_samples ina3221_num_samples_t
Number of samples to calculate average shunt/bus voltage value.
ina3221_mode
Device operation modes.
Low-level GPIO peripheral driver interface definitions.
int ina3221_read_bus_mv(const ina3221_t *dev, ina3221_channel_t ch, int16_t *out_mv, uint16_t *flags)
Read bus voltages for each channel in ch.
uint8_t gpio_config
Configure pull up resistors for gpio pins.
@ INA3221_MODE_CONTINUOUS_SHUNT_ONLY
Continuous shunt voltage measurement only.
static int ina3221_set_channel_state(ina3221_t *dev, ina3221_channel_state_t ch1, ina3221_channel_state_t ch2, ina3221_channel_state_t ch3)
Wrapper around.
#define INA3221_CONV_TIME_BADC_MASK
ADC bus voltage conversion times bit mask.
void * arg_pv_cb
Argument for Power Valid alert callback.
@ INA3221_ALERT_PV
Power valid alert.
@ INA3221_MODE_CONTINUOUS_SHUNT_BUS
Continuous shunt and bus voltage measurement.
int ina3221_get_crit_alert_limit(const ina3221_t *dev, ina3221_channel_t ch, int32_t *out_uv)
Read critical shunt voltage alert limit for each channel in ch.
gpio_t pin_tc
Timing control alert pin, becomes configured as input.
enum ina3221_enable_latch ina3221_enable_latch_t
Enable latch flags.
@ INA3221_CONV_TIME_SADC_588US
588 us
ina3221_params_t params
Device parameters.
@ INA3221_ENABLE_CH2
Enable channel 2.
@ INA3221_CONV_TIME_SADC_4156US
4156 us
@ INA3221_CONV_TIME_BADC_8244US
8244 us
ina3221_ret_val
Return codes.
enum ina3221_conv_time_bus_adc ina3221_conv_time_bus_adc_t
ADC bus voltage conversion times.
ina3221_channel
Channel flags.
static int ina3221_enable_power_valid_alert(ina3221_t *dev, ina3221_alert_cb_t cb, void *arg)
Wrapper around.
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.
int ina3221_get_num_samples(const ina3221_t *dev, ina3221_num_samples_t *ns)
Read currently applied number of samples.
int ina3221_calculate_current_ua(const ina3221_t *dev, ina3221_channel_t ch, int32_t *in_uv, int32_t *out_ma)
Calculate current for each channel in ch.
int ina3221_init(ina3221_t *dev, const ina3221_params_t *params)
Initialize device.
@ INA3221_NUM_SAMPLES_16
16
int ina3221_read_flags(const ina3221_t *dev, uint16_t *flags)
Read status flags.
ina3221_alert_cb_t crit_cb
Critical alert callback: executed when CRT is pulled low.
int ina3221_get_warn_alert_limit(const ina3221_t *dev, ina3221_channel_t ch, int32_t *out_uv)
Read warning shunt voltage alert limit for each channel in ch.
ina3221_alert_cb_t pv_cb
Power Valid alert callback: executed when PV is pulled low.
@ INA3221_CONV_TIME_SADC_332US
332 us
int ina3221_get_power_valid_lower_limit(const ina3221_t *dev, int32_t *out_mv)
Read bus voltage power valid lower limit.
Low-level I2C peripheral driver interface definition.
@ INA3221_CH_DISABLE
Channel disabled.
@ INA3221_MODE_TRIGGER_SHUNT_BUS
Trigger shunt and bus voltage measurement.
int ina3221_set_mode(ina3221_t *dev, ina3221_mode_t mode)
Update device operation mode.
static int ina3221_get_config(const ina3221_t *dev, ina3221_enable_ch_t *chs, ina3221_num_samples_t *ns, ina3221_conv_time_bus_adc_t *ctbadc, ina3221_conv_time_shunt_adc_t *ctsadc, ina3221_mode_t *mode)
Wrapper around.
static int ina3221_enable_warning_alert(ina3221_t *dev, ina3221_alert_cb_t cb, void *arg)
Wrapper around.
enum ina3221_enable_ch ina3221_enable_ch_t
Enable channel flags.
@ INA3221_ADDR_10
A0 connected to SDA.
gpio_t pin_pv
Power valid alert pin, becomes configured as input.
@ INA3221_ENABLE_SUM_CH3
Enable sum channel 3.
void * arg_warn_cb
Argument for Warning alert callback.
int ina3221_get_shunt_voltage_sum_alert_limit(const ina3221_t *dev, int32_t *out_uv)
Read shunt voltage sum alert limit.
@ INA3221_NUM_SAMPLES_1024
1024