YAHAL
Yet Another Hardware Abstraction Library
Loading...
Searching...
No Matches
ina219_drv.h
1/*
2 * ina219_drv.h
3 *
4 * Created on: Jun 8, 2018
5 * Author: Philipp Dressen
6 *
7 * Based on: http://www.ti.com/lit/ds/symlink/ina219.pdf
8 */
9
10#ifndef SRC_DRIVER_INA219_DRV_H_
11#define SRC_DRIVER_INA219_DRV_H_
12
13#include "i2c_interface.h"
14
15namespace INA219 {
16 // adafruit: 0x40, 0x41, 0x44, 0x45
17 static const uint8_t ADDRESS = 0x40;
18 static const uint16_t OVERFLOW = 0xFFFF;
19
20 // 16-Bit registers, MSB-First
21 static const uint8_t REG_CONF = 0x00;
22 static const uint8_t REG_SHUNT_V = 0x01;
23 static const uint8_t REG_BUS_V = 0x02;
24 static const uint8_t REG_POWER = 0x03;
25 static const uint8_t REG_CURRENT = 0x04;
26 static const uint8_t REG_CAL = 0x05;
27
28 // Reset (bit 15)
29 // unused (bit 14)
30
31 // Bus Voltage Range (bit 13)
32 enum BG : uint8_t {
33 FSR16V = 0,
34 FSR32V = 1
35 };
36
37 // Shunt Voltage Range (bit 12-11)
38 enum PG : uint8_t {
39 X1 = 0b00, // 40mV
40 X2 = 0b01, // 80mV
41 X4 = 0b10, // 160mV
42 X8 = 0b11 // 320mV
43 };
44
45 // Bus Voltage ADC (bit 10-7)
46 // Shunt Voltage ADC (bit 6-3)
47 // Resolution/Samples
48 enum ADC : uint8_t {
49 ADC9 = 0b0000,
50 ADC10 = 0b0001,
51 ADC11 = 0b0010,
52 ADC12 = 0b0011,
53 SAMPLES2 = 0b1001,
54 SAMPLES4 = 0b1010,
55 SAMPLES8 = 0b1011,
56 SAMPLES16 = 0b1100,
57 SAMPLES32 = 0b1101,
58 SAMPLES64 = 0b1110,
59 SAMPLES128 = 0b1111
60 };
61
62 // Mode (bit 2-0)
63 enum MODE : uint8_t {
64 SLEEP = 0b000,
65 TRIG_SHUNT = 0b001,
66 TRIG_BUS = 0b001,
67 TRIG = 0b011,
68 ADC_OFF = 0b100,
69 CONT_SHUNT = 0b101,
70 CONT_BUS = 0b110,
71 CONT = 0b111
72 };
73}
74
76
77public:
78 ina219_drv(i2c_interface & i2c, uint8_t _i2c_addr = INA219::ADDRESS);
79
80 void setConfig(bool reset, INA219::BG busgain, INA219::PG powergain, INA219::ADC busadc, INA219::ADC shuntadc, INA219::MODE mode);
81 void setCalibration(unsigned int maxCurrent = 3200, float shuntR = 0.1);
82 float getShuntVoltage(); // mV
83 float getBusVoltage(); // V
84 float getCurrent(); // ma
85
86private:
87 i2c_interface & _i2c;
88 uint8_t _i2c_addr;
89
90 INA219::BG busgain = INA219::BG::FSR32V;
91 INA219::PG shuntgain = INA219::PG::X8;
92 INA219::ADC busadc = INA219::ADC::ADC12;
93 INA219::ADC shuntadc = INA219::ADC::ADC12;
94 INA219::MODE mode = INA219::MODE::CONT;
95
96 uint16_t calibration = 0;
97 float currentLSB = 0;
98
99 void writeRegister(uint8_t reg, uint16_t val);
100 uint16_t readRegister(uint8_t reg);
101};
102
103#endif