43#define CHIP_NAME "Texas Instruments INA219"
44#define MANUFACTURER_NAME "Texas Instruments"
45#define SUPPLY_VOLTAGE_MIN 3.0f
46#define SUPPLY_VOLTAGE_MAX 5.5f
47#define MAX_CURRENT 1.0f
48#define TEMPERATURE_MIN -25.0f
49#define TEMPERATURE_MAX 85.0f
50#define DRIVER_VERSION 1000
55#define INA219_REG_CONF 0x00
56#define INA219_REG_SHUNT_VOLTAGE 0x01
57#define INA219_REG_BUS_VOLTAGE 0x02
58#define INA219_REG_POWER 0x03
59#define INA219_REG_CURRENT 0x04
60#define INA219_REG_CALIBRATION 0x05
72static uint8_t a_ina219_iic_read(
ina219_handle_t *handle, uint8_t reg, uint16_t *data)
76 memset(buf, 0,
sizeof(uint8_t) * 2);
83 *data = (uint16_t)buf[0] << 8 | buf[1];
99static uint8_t a_ina219_iic_write(
ina219_handle_t *handle, uint8_t reg, uint16_t data)
103 buf[0] = (uint8_t)((data >> 8) & 0xFF);
104 buf[1] = (uint8_t)((data >> 0) & 0xFF);
131 handle->
r = resistance;
152 *resistance = handle->
r;
173 handle->
iic_addr = (uint8_t)addr_pin;
226 handle->
debug_print(
"ina219: read conf register failed.\n");
264 handle->
debug_print(
"ina219: read conf register failed.\n");
302 handle->
debug_print(
"ina219: read conf register failed.\n");
339 handle->
debug_print(
"ina219: read conf register failed.\n");
377 handle->
debug_print(
"ina219: read conf register failed.\n");
414 handle->
debug_print(
"ina219: read conf register failed.\n");
452 handle->
debug_print(
"ina219: read conf register failed.\n");
489 handle->
debug_print(
"ina219: read conf register failed.\n");
527 handle->
debug_print(
"ina219: read conf register failed.\n");
564 handle->
debug_print(
"ina219: read conf register failed.\n");
573 handle->
debug_print(
"ina219: write conf register failed.\n");
580 uint8_t m, mode1, mode2, t;
582 mode1 = (prev >> 3) & 0xF;
583 mode2 = (prev >> 7) & 0xF;
584 m = (mode1 > mode2) ? mode1 : mode2;
591 t = (uint8_t)(0.532 * pow(2, m - 8)) + 1;
631 handle->
debug_print(
"ina219: read conf register failed.\n");
673 handle->
debug_print(
"ina219: read shunt voltage register failed.\n");
678 *mV = (float)(*raw) / 100.0f;
713 handle->
debug_print(
"ina219: read bus voltage register failed.\n");
717 if (((*raw) & (1 << 0)) != 0)
723 if (((*raw) & (1 << 1)) == 0)
725 handle->
debug_print(
"ina219: conversion not ready.\n");
730 *mV = (float)(*raw) * 4.0f;
768 handle->
debug_print(
"ina219: read current register failed.\n");
773 *mA = (float)((
double)(*raw) * handle->
current_lsb * 1000);
806 handle->
debug_print(
"ina219: read power register failed.\n");
810 *mW = (float)((
double)(*raw) * handle->
current_lsb * 20.0 * 1000.0);
842 handle->
debug_print(
"ina219: read calibration register failed.\n");
878 if ((handle->
r >= -0.000001f) && (handle->
r <= 0.000001f))
888 handle->
debug_print(
"ina219: read conf register failed.\n");
892 pga = (prev >> 11) & 0x3;
940 *calibration = (uint16_t)(0.04096 / (v / pow(2.0, 15.0)));
973 handle->
debug_print(
"ina219: write calibration register failed.\n");
1007 handle->
debug_print(
"ina219: iic_init is null.\n");
1013 handle->
debug_print(
"ina219: iic_deinit is null.\n");
1019 handle->
debug_print(
"ina219: iic_read is null.\n");
1025 handle->
debug_print(
"ina219: iic_write is null.\n");
1031 handle->
debug_print(
"ina219: delay_ms is null.\n");
1038 handle->
debug_print(
"ina219: iic init failed.\n");
1045 handle->
debug_print(
"ina219: read conf register failed.\n");
1054 handle->
debug_print(
"ina219: write conf register failed.\n");
1063 handle->
debug_print(
"ina219: read conf register failed.\n");
1068 if ((prev & (1 << 15)) != 0)
1070 handle->
debug_print(
"ina219: soft reset failed.\n");
1109 handle->
debug_print(
"ina219: read conf register failed.\n");
1117 handle->
debug_print(
"ina219: write conf register failed.\n");
1124 handle->
debug_print(
"ina219: iic deinit failed.\n");
1155 return a_ina219_iic_write(handle, reg, data);
1181 return a_ina219_iic_read(handle, reg, data);
#define INA219_REG_BUS_VOLTAGE
#define INA219_REG_CONF
chip register definition
#define SUPPLY_VOLTAGE_MAX
#define INA219_REG_CALIBRATION
#define MANUFACTURER_NAME
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define INA219_REG_CURRENT
#define INA219_REG_SHUNT_VOLTAGE
driver ina219 header file
uint8_t ina219_get_pga(ina219_handle_t *handle, ina219_pga_t *pga)
get the pga
uint8_t ina219_get_resistance(ina219_handle_t *handle, double *resistance)
get the resistance
uint8_t ina219_get_bus_voltage_range(ina219_handle_t *handle, ina219_bus_voltage_range_t *range)
get the bus voltage range
uint8_t ina219_soft_reset(ina219_handle_t *handle)
soft reset the chip
struct ina219_info_s ina219_info_t
ina219 information structure definition
uint8_t ina219_set_addr_pin(ina219_handle_t *handle, ina219_address_t addr_pin)
set the iic address pin
uint8_t ina219_info(ina219_info_t *info)
get chip's information
uint8_t ina219_set_shunt_voltage_adc_mode(ina219_handle_t *handle, ina219_adc_mode_t mode)
set the shunt voltage adc mode
uint8_t ina219_get_mode(ina219_handle_t *handle, ina219_mode_t *mode)
get the mode
uint8_t ina219_get_bus_voltage_adc_mode(ina219_handle_t *handle, ina219_adc_mode_t *mode)
get the bus voltage adc mode
ina219_adc_mode_t
ina219 adc mode enumeration definition
ina219_pga_t
ina219 pga enumeration definition
uint8_t ina219_get_shunt_voltage_adc_mode(ina219_handle_t *handle, ina219_adc_mode_t *mode)
get the shunt voltage adc mode
uint8_t ina219_get_addr_pin(ina219_handle_t *handle, ina219_address_t *addr_pin)
get the iic address pin
uint8_t ina219_set_calibration(ina219_handle_t *handle, uint16_t data)
set the calibration
ina219_bus_voltage_range_t
ina219 bus voltage enumeration definition
uint8_t ina219_deinit(ina219_handle_t *handle)
close the chip
uint8_t ina219_get_calibration(ina219_handle_t *handle, uint16_t *data)
get the calibration
uint8_t ina219_set_mode(ina219_handle_t *handle, ina219_mode_t mode)
set the mode
uint8_t ina219_set_pga(ina219_handle_t *handle, ina219_pga_t pga)
set the pga
uint8_t ina219_set_bus_voltage_range(ina219_handle_t *handle, ina219_bus_voltage_range_t range)
set the bus voltage range
uint8_t ina219_read_bus_voltage(ina219_handle_t *handle, uint16_t *raw, float *mV)
read the bus voltage
uint8_t ina219_read_current(ina219_handle_t *handle, int16_t *raw, float *mA)
read the current
uint8_t ina219_calculate_calibration(ina219_handle_t *handle, uint16_t *calibration)
calculate the calibration
uint8_t ina219_init(ina219_handle_t *handle)
initialize the chip
uint8_t ina219_set_resistance(ina219_handle_t *handle, double resistance)
set the resistance
ina219_mode_t
ina219 mode enumeration definition
ina219_address_t
ina219 address enumeration definition
uint8_t ina219_set_bus_voltage_adc_mode(ina219_handle_t *handle, ina219_adc_mode_t mode)
set the bus voltage adc mode
uint8_t ina219_read_shunt_voltage(ina219_handle_t *handle, int16_t *raw, float *mV)
read the shunt voltage
struct ina219_handle_s ina219_handle_t
ina219 handle structure definition
uint8_t ina219_read_power(ina219_handle_t *handle, uint16_t *raw, float *mW)
read the power
@ INA219_MODE_SHUNT_BUS_VOLTAGE_TRIGGERED
@ INA219_MODE_SHUNT_VOLTAGE_TRIGGERED
uint8_t ina219_get_reg(ina219_handle_t *handle, uint8_t reg, uint16_t *data)
get the chip register
uint8_t ina219_set_reg(ina219_handle_t *handle, uint8_t reg, uint16_t data)
set the chip register
void(* delay_ms)(uint32_t ms)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t(* iic_write)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_read)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_deinit)(void)
float supply_voltage_max_v
char manufacturer_name[32]
float supply_voltage_min_v