ina260 - Driver for INA260 precision digital current and power monitor

group ina260

ESP-IDF driver for INA260 precision digital current and power monitor.

Copyright (C) 2020 Ruslan V. Uss unclerus@gmail.com

BSD Licensed as described in the file LICENSE

Todo:

Add support for SPI interface

Defines

INA260_ADDR_PIN_GND
INA260_ADDR_PIN_VS
INA260_ADDR_PIN_SDA
INA260_ADDR_PIN_SCL
INA260_ADDR(A0, A1)

Macro to define I2C address.

Examples: INA260_ADDR(INA260_ADDR_PIN_GND, INA260_ADDR_PIN_GND) = 0x40 (A0 = A1 = GND) INA260_ADDR(INA260_ADDR_PIN_VS, INA260_ADDR_PIN_SDA) = 0x49 (A0 = VS, A1 = SDA)

Enums

enum ina260_averaging_mode_t

Averaging mode.

Determines the number of samples that are collected and averaged.

Values:

enumerator INA260_AVG_1

1 sample, default

enumerator INA260_AVG_4

4 samples

enumerator INA260_AVG_16

16 samples

enumerator INA260_AVG_64

64 samples

enumerator INA260_AVG_128

128 samples

enumerator INA260_AVG_256

256 samples

enumerator INA260_AVG_512

512 samples

enumerator INA260_AVG_1024

1024 samples

enum ina260_conversion_time_t

Conversion time.

Values:

enumerator INA260_CT_140

140 us

enumerator INA260_CT_204

204 us

enumerator INA260_CT_332

332 us

enumerator INA260_CT_588

588 us

enumerator INA260_CT_1100

1.1 ms, default

enumerator INA260_CT_2116

2.116 ms

enumerator INA260_CT_4156

4.156 ms

enumerator INA260_CT_8244

8.244 ms

enum ina260_mode_t

Operating mode.

Values:

enumerator INA260_MODE_POWER_DOWN

Power-done.

enumerator INA260_MODE_TRIG_SHUNT

Shunt current, triggered.

enumerator INA260_MODE_TRIG_BUS

Bus voltage, triggered.

enumerator INA260_MODE_TRIG_SHUNT_BUS

Shunt current and bus voltage, triggered.

enumerator INA260_MODE_POWER_DOWN2

Power-done.

enumerator INA260_MODE_CONT_SHUNT

Shunt current, continuous.

enumerator INA260_MODE_CONT_BUS

Bus voltage, continuous.

enumerator INA260_MODE_CONT_SHUNT_BUS

Shunt current and bus voltage, continuous (default)

enum ina260_alert_mode_t

Alert function mode.

Values:

enumerator INA260_ALERT_DISABLED

No alert function.

enumerator INA260_ALERT_OCL

Over current limit.

enumerator INA260_ALERT_UCL

Under current limit.

enumerator INA260_ALERT_BOL

Bus voltage over-voltage.

enumerator INA260_ALERT_BUL

Bus voltage under-voltage.

enumerator INA260_ALERT_POL

Power over-limit.

Functions

esp_err_t ina260_init_desc(ina260_t *dev, uint8_t addr, i2c_port_t port, gpio_num_t sda_gpio, gpio_num_t scl_gpio)

Initialize device descriptor.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • addr: Device I2C address

  • port: I2C port

  • sda_gpio: SDA GPIO

  • scl_gpio: SCL GPIO

esp_err_t ina260_free_desc(ina260_t *dev)

Free device descriptor.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

esp_err_t ina260_init(ina260_t *dev)

Initialize device.

Reads sensor configuration.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

esp_err_t ina260_reset(ina260_t *dev)

Reset sensor.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

esp_err_t ina260_set_config(ina260_t *dev, ina260_mode_t mode, ina260_averaging_mode_t avg_mode, ina260_conversion_time_t vbus_ct, ina260_conversion_time_t ish_ct)

Configure sensor.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • mode: Operating mode

  • avg_mode: Averaging mide

  • vbus_ct: Bus voltage conversion time

  • ish_ct: Shunt current conversion time

esp_err_t ina260_get_config(ina260_t *dev, ina260_mode_t *mode, ina260_averaging_mode_t *avg_mode, ina260_conversion_time_t *vbus_ct, ina260_conversion_time_t *ish_ct)

Read sensor configuration.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • [out] mode: Operating mode

  • [out] avg_mode: Averaging mide

  • [out] vbus_ct: Bus voltage conversion time

  • [out] ish_ct: Shunt current conversion time

esp_err_t ina260_set_alert(ina260_t *dev, ina260_alert_mode_t mode, float limit, bool cvrf, bool active_high, bool latch)

Setup ALERT pin.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • mode: Alert function mode

  • limit: Alert limit value

  • cvrf: If true also assert ALERT pin when device is ready to next conversion

  • active_high: Set active ALERT pin level is high

  • latch: Enable latch mode on ALERT pin (see ina260_get_status())

esp_err_t ina260_trigger(ina260_t *dev)

Trigger single conversion.

Function will return an error if current operating mode is not INA260_MODE_TRIG_SHUNT/INA260_MODE_TRIG_BUS/INA260_MODE_TRIG_SHUNT_BUS

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

esp_err_t ina260_get_status(ina260_t *dev, bool *ready, bool *alert, bool *overflow)

Get device status.

This function also clears ALERT state if latch mode is enabled for ALERT pin.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • [out] ready: If true, device is ready for the next conversion

  • [out] alert: If true, there was alert

  • [out] overflow: If true, power data have exceeded max 419.43 W

esp_err_t ina260_get_current(ina260_t *dev, float *current)

Read current.

This function works properly only after calibration.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • [out] current: Current, A

esp_err_t ina260_get_bus_voltage(ina260_t *dev, float *voltage)

Read bus voltage.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • [out] voltage: Bus voltage, V

esp_err_t ina260_get_power(ina260_t *dev, float *power)

Read power.

This function works properly only after calibration.

Return

ESP_OK on success

Parameters
  • dev: Device descriptor

  • [out] power: Power, W

struct ina260_t
#include <ina260.h>

Device descriptor.