43 #define CHIP_NAME "Texas Instruments INA226"
44 #define MANUFACTURER_NAME "Texas Instruments"
45 #define SUPPLY_VOLTAGE_MIN 2.7f
46 #define SUPPLY_VOLTAGE_MAX 5.5f
47 #define MAX_CURRENT 0.33f
48 #define TEMPERATURE_MIN -40.0f
49 #define TEMPERATURE_MAX 125.0f
50 #define DRIVER_VERSION 1000
55 #define INA226_REG_CONF 0x00
56 #define INA226_REG_SHUNT_VOLTAGE 0x01
57 #define INA226_REG_BUS_VOLTAGE 0x02
58 #define INA226_REG_POWER 0x03
59 #define INA226_REG_CURRENT 0x04
60 #define INA226_REG_CALIBRATION 0x05
61 #define INA226_REG_MASK 0x06
62 #define INA226_REG_ALERT_LIMIT 0x07
63 #define INA226_REG_MANUFACTURER 0xFE
64 #define INA226_REG_DIE 0xFF
76 static uint8_t a_ina226_iic_read(
ina226_handle_t *handle, uint8_t reg, uint16_t *data)
80 memset(buf, 0,
sizeof(uint8_t) * 2);
87 *data = (uint16_t)buf[0] << 8 | buf[1];
103 static uint8_t a_ina226_iic_write(
ina226_handle_t *handle, uint8_t reg, uint16_t data)
107 buf[0] = (uint8_t)((data >> 8) & 0xFF);
108 buf[1] = (uint8_t)((data >> 0) & 0xFF);
135 handle->
r = resistance;
156 *resistance = handle->
r;
177 handle->
iic_addr = (uint8_t)addr_pin;
230 handle->
debug_print(
"ina226: read conf register failed.\n");
268 handle->
debug_print(
"ina226: read conf register failed.\n");
306 handle->
debug_print(
"ina226: read conf register failed.\n");
343 handle->
debug_print(
"ina226: read conf register failed.\n");
381 handle->
debug_print(
"ina226: read conf register failed.\n");
418 handle->
debug_print(
"ina226: read conf register failed.\n");
456 handle->
debug_print(
"ina226: read conf register failed.\n");
493 handle->
debug_print(
"ina226: read conf register failed.\n");
502 handle->
debug_print(
"ina226: write conf register failed.\n");
548 handle->
debug_print(
"ina226: read conf register failed.\n");
583 res = a_ina226_iic_read(handle,
INA226_REG_DIE, (uint16_t *)&prev);
586 handle->
debug_print(
"ina226: read die register failed.\n");
590 *device_id = (prev >> 4) & 0xFFF;
591 *die_revision_id = prev & 0xF;
632 handle->
debug_print(
"ina226: read mask register failed.\n");
636 if ((prev & (1 << 2)) != 0)
647 if ((prev & (1 << 3)) == 0)
650 for (i = 0; i< timeout; i++)
655 handle->
debug_print(
"ina226: read mask register failed.\n");
659 if ((prev & (1 << 3)) != 0)
679 handle->
debug_print(
"ina226: read shunt voltage register failed.\n");
684 *mV = (float)(*raw) / 400.0f;
720 handle->
debug_print(
"ina226: read mask register failed.\n");
724 if ((prev & (1 << 2)) != 0)
735 if ((prev & (1 << 3)) == 0)
738 for (i = 0; i< timeout; i++)
743 handle->
debug_print(
"ina226: read mask register failed.\n");
747 if ((prev & (1 << 3)) != 0)
767 handle->
debug_print(
"ina226: read bus voltage register failed.\n");
771 *mV = (float)(*raw) * 1.25f;
812 handle->
debug_print(
"ina226: read mask register failed.\n");
816 if ((prev & (1 << 2)) != 0)
827 if ((prev & (1 << 3)) == 0)
830 for (i = 0; i< timeout; i++)
835 handle->
debug_print(
"ina226: read mask register failed.\n");
839 if ((prev & (1 << 3)) != 0)
859 handle->
debug_print(
"ina226: read current register failed.\n");
864 *mA = (float)((
double)(*raw) * handle->
current_lsb * 1000);
900 handle->
debug_print(
"ina226: read mask register failed.\n");
904 if ((prev & (1 << 2)) != 0)
915 if ((prev & (1 << 3)) == 0)
918 for (i = 0; i< timeout; i++)
923 handle->
debug_print(
"ina226: read mask register failed.\n");
927 if ((prev & (1 << 3)) != 0)
947 handle->
debug_print(
"ina226: read power register failed.\n");
951 *mW = (float)((
double)(*raw) * handle->
current_lsb * 25.0 * 1000.0);
983 handle->
debug_print(
"ina226: read calibration register failed.\n");
1015 if ((handle->
r >= -0.000001f) && (handle->
r <= 0.000001f))
1017 handle->
debug_print(
"ina226: r can't be zero.\n");
1024 *calibration = (uint16_t)(0.00512 / (v / pow(2.0, 15.0)));
1056 handle->
debug_print(
"ina226: write calibration register failed.\n");
1093 handle->
debug_print(
"ina226: read mask register failed.\n");
1097 prev &= ~(1 << mask);
1098 prev |= (enable << mask);
1102 handle->
debug_print(
"ina226: write mask register failed.\n");
1139 handle->
debug_print(
"ina226: read mask register failed.\n");
1176 handle->
debug_print(
"ina226: read mask register failed.\n");
1181 prev |= (enable << 10);
1185 handle->
debug_print(
"ina226: write mask register failed.\n");
1221 handle->
debug_print(
"ina226: read mask register failed.\n");
1258 handle->
debug_print(
"ina226: read mask register failed.\n");
1267 handle->
debug_print(
"ina226: write mask register failed.\n");
1303 handle->
debug_print(
"ina226: read mask register failed.\n");
1340 handle->
debug_print(
"ina226: read mask register failed.\n");
1345 prev |= (enable << 0);
1349 handle->
debug_print(
"ina226: write mask register failed.\n");
1385 handle->
debug_print(
"ina226: read mask register failed.\n");
1421 handle->
debug_print(
"ina226: write alert limit register failed.\n");
1456 handle->
debug_print(
"ina226: read alert limit register failed.\n");
1486 *reg = (uint16_t)(mV * 400.0f);
1513 *mV = (float)(reg) / 400.0f;
1540 *reg = (uint16_t)(mV / 1.25f);
1567 *mV = (float)(reg) * 1.25f;
1594 *reg = (uint16_t)((
double)(mW) / (handle->
current_lsb * 25.0 * 1000.0));
1621 *mW = (float)((
double)(reg) * handle->
current_lsb * 25.0 * 1000.0);
1653 handle->
debug_print(
"ina226: read mask register failed.\n");
1657 if ((prev & (1 << 4)) != 0)
1659 if ((prev & (1 << 15)) != 0)
1666 else if ((prev & (1 << 14)) != 0)
1673 else if ((prev & (1 << 13)) != 0)
1680 else if ((prev & (1 << 12)) != 0)
1687 else if ((prev & (1 << 11)) != 0)
1730 handle->
debug_print(
"ina226: iic_init is null.\n");
1736 handle->
debug_print(
"ina226: iic_deinit is null.\n");
1742 handle->
debug_print(
"ina226: iic_read is null.\n");
1748 handle->
debug_print(
"ina226: iic_write is null.\n");
1754 handle->
debug_print(
"ina226: delay_ms is null.\n");
1760 handle->
debug_print(
"ina226: receive_callback is null.\n");
1767 handle->
debug_print(
"ina226: iic init failed.\n");
1774 handle->
debug_print(
"ina226: read manufacturer failed.\n");
1789 handle->
debug_print(
"ina226: read conf register failed.\n");
1798 handle->
debug_print(
"ina226: write conf register failed.\n");
1807 handle->
debug_print(
"ina226: read conf register failed.\n");
1812 if ((prev & (1 << 15)) != 0)
1814 handle->
debug_print(
"ina226: soft reset failed.\n");
1853 handle->
debug_print(
"ina226: read conf register failed.\n");
1861 handle->
debug_print(
"ina226: write conf register failed.\n");
1868 handle->
debug_print(
"ina226: iic deinit failed.\n");
1899 return a_ina226_iic_write(handle, reg, data);
1925 return a_ina226_iic_read(handle, reg, data);
#define INA226_REG_MANUFACTURER
#define SUPPLY_VOLTAGE_MAX
#define INA226_REG_SHUNT_VOLTAGE
#define MANUFACTURER_NAME
#define INA226_REG_ALERT_LIMIT
#define SUPPLY_VOLTAGE_MIN
#define INA226_REG_CALIBRATION
#define INA226_REG_BUS_VOLTAGE
#define INA226_REG_CONF
chip register definition
#define CHIP_NAME
chip information definition
#define INA226_REG_CURRENT
driver ina226 header file
uint8_t ina226_calculate_calibration(ina226_handle_t *handle, uint16_t *calibration)
calculate the calibration
uint8_t ina226_get_addr_pin(ina226_handle_t *handle, ina226_address_t *addr_pin)
get the iic address pin
uint8_t ina226_set_bus_voltage_conversion_time(ina226_handle_t *handle, ina226_conversion_time_t t)
set bus voltage conversion time
uint8_t ina226_info(ina226_info_t *info)
get chip's information
uint8_t ina226_bus_voltage_convert_to_data(ina226_handle_t *handle, uint16_t reg, float *mV)
convert the register raw data to the bus voltage
uint8_t ina226_power_convert_to_data(ina226_handle_t *handle, uint16_t reg, float *mW)
convert the register raw data to the power
ina226_mask_t
ina226 mask enumeration definition
uint8_t ina226_bus_voltage_convert_to_register(ina226_handle_t *handle, float mV, uint16_t *reg)
convert the bus voltage to the register raw data
uint8_t ina226_read_current(ina226_handle_t *handle, int16_t *raw, float *mA)
read the current
uint8_t ina226_deinit(ina226_handle_t *handle)
close the chip
uint8_t ina226_set_conversion_ready_alert_pin(ina226_handle_t *handle, ina226_bool_t enable)
enable or disable conversion ready alert pin
uint8_t ina226_get_alert_latch(ina226_handle_t *handle, ina226_bool_t *enable)
get alert latch status
uint8_t ina226_get_bus_voltage_conversion_time(ina226_handle_t *handle, ina226_conversion_time_t *t)
get bus voltage conversion time
uint8_t ina226_set_mode(ina226_handle_t *handle, ina226_mode_t mode)
set the mode
uint8_t ina226_set_alert_polarity_pin(ina226_handle_t *handle, ina226_alert_polarity_t pin)
set alert polarity pin
uint8_t ina226_shunt_voltage_convert_to_register(ina226_handle_t *handle, float mV, uint16_t *reg)
convert the shunt voltage to the register raw data
#define INA226_READ_TIMEOUT
ina226 read timeout definition
uint8_t ina226_set_mask(ina226_handle_t *handle, ina226_mask_t mask, ina226_bool_t enable)
enable or disable mask
uint8_t ina226_get_conversion_ready_alert_pin(ina226_handle_t *handle, ina226_bool_t *enable)
get conversion ready alert pin status
uint8_t ina226_get_alert_limit(ina226_handle_t *handle, uint16_t *reg)
get alert limit
uint8_t ina226_set_addr_pin(ina226_handle_t *handle, ina226_address_t addr_pin)
set the iic address pin
uint8_t ina226_set_alert_latch(ina226_handle_t *handle, ina226_bool_t enable)
enable or disable alert latch
uint8_t ina226_read_power(ina226_handle_t *handle, uint16_t *raw, float *mW)
read the power
uint8_t ina226_set_average_mode(ina226_handle_t *handle, ina226_avg_t mode)
set average mode
uint8_t ina226_get_shunt_voltage_conversion_time(ina226_handle_t *handle, ina226_conversion_time_t *t)
get shunt voltage conversion time
uint8_t ina226_get_calibration(ina226_handle_t *handle, uint16_t *data)
get the calibration
uint8_t ina226_get_mode(ina226_handle_t *handle, ina226_mode_t *mode)
get the mode
ina226_address_t
ina226 address enumeration definition
uint8_t ina226_soft_reset(ina226_handle_t *handle)
soft reset the chip
uint8_t ina226_get_mask(ina226_handle_t *handle, ina226_mask_t mask, ina226_bool_t *enable)
get mask
uint8_t ina226_irq_handler(ina226_handle_t *handle)
irq handler
uint8_t ina226_get_alert_polarity_pin(ina226_handle_t *handle, ina226_alert_polarity_t *pin)
get alert polarity pin
ina226_mode_t
ina226 mode enumeration definition
uint8_t ina226_read_shunt_voltage(ina226_handle_t *handle, int16_t *raw, float *mV)
read the shunt voltage
uint8_t ina226_get_average_mode(ina226_handle_t *handle, ina226_avg_t *mode)
get average mode
uint8_t ina226_init(ina226_handle_t *handle)
initialize the chip
uint8_t ina226_get_resistance(ina226_handle_t *handle, double *resistance)
get the resistance
ina226_bool_t
ina226 bool enumeration definition
uint8_t ina226_set_shunt_voltage_conversion_time(ina226_handle_t *handle, ina226_conversion_time_t t)
set shunt voltage conversion time
uint8_t ina226_set_alert_limit(ina226_handle_t *handle, uint16_t reg)
set alert limit
uint8_t ina226_set_calibration(ina226_handle_t *handle, uint16_t data)
set the calibration
uint8_t ina226_shunt_voltage_convert_to_data(ina226_handle_t *handle, uint16_t reg, float *mV)
convert the register raw data to the shunt voltage
uint8_t ina226_set_resistance(ina226_handle_t *handle, double resistance)
set the resistance
uint8_t ina226_power_convert_to_register(ina226_handle_t *handle, float mW, uint16_t *reg)
convert the power to the register raw data
ina226_avg_t
ina226 average enumeration definition
ina226_alert_polarity_t
ina226 alert polarity enumeration definition
uint8_t ina226_get_die_id(ina226_handle_t *handle, uint16_t *device_id, uint8_t *die_revision_id)
get the die id
uint8_t ina226_read_bus_voltage(ina226_handle_t *handle, uint16_t *raw, float *mV)
read the bus voltage
@ INA226_STATUS_BUS_VOLTAGE_OVER_VOLTAGE
@ INA226_STATUS_POWER_OVER_LIMIT
@ INA226_STATUS_SHUNT_VOLTAGE_UNDER_VOLTAGE
@ INA226_STATUS_BUS_VOLTAGE_UNDER_VOLTAGE
@ INA226_STATUS_SHUNT_VOLTAGE_OVER_VOLTAGE
@ INA226_MODE_BUS_VOLTAGE_TRIGGERED
@ INA226_MODE_SHUNT_BUS_VOLTAGE_TRIGGERED
@ INA226_MODE_SHUNT_VOLTAGE_TRIGGERED
uint8_t ina226_set_reg(ina226_handle_t *handle, uint8_t reg, uint16_t data)
set the chip register
uint8_t ina226_get_reg(ina226_handle_t *handle, uint8_t reg, uint16_t *data)
get the chip register
ina226 handle structure definition
void(* delay_ms)(uint32_t ms)
void(* receive_callback)(uint8_t type)
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)
ina226 information structure definition
float supply_voltage_max_v
char manufacturer_name[32]
float supply_voltage_min_v