YAHAL
Yet Another Hardware Abstraction Library
Loading...
Searching...
No Matches
ina219_drv.cpp
1/*
2 * ina219_drv.cpp
3 *
4 * Created on: Jun 8, 2018
5 * Author: Philipp Dressen
6 */
7
8#include "ina219_drv.h"
9
10ina219_drv::ina219_drv(i2c_interface & i2c, uint8_t i2c_addr) : _i2c(i2c), _i2c_addr(i2c_addr){
11}
12
13/****************************************************************\
14* Sets INA219 config register & resets the chip if desired.
15\****************************************************************/
16void ina219_drv::setConfig(bool reset, INA219::BG busgain, INA219::PG powergain, INA219::ADC busadc, INA219::ADC shuntadc, INA219::MODE mode) {
17 uint16_t conf = 0;
18 conf |= reset << 15;
19 conf |= busgain << 13;
20 conf |= powergain << 11;
21 conf |= busadc << 7;
22 conf |= shuntadc << 3;
23 conf |= mode;
24 writeRegister(INA219::REG_CONF, conf);
25
26 this->busgain = busgain;
27 this->shuntgain = shuntgain;
28 this->busadc = busadc;
29 this->shuntadc = shuntadc;
30 this->mode = mode;
31}
32
33/****************************************************************\
34* Sets INA219 calibration register based on the maximum expected
35* current in mA an the installed shunt resistor value in Ohm.
36\****************************************************************/
37void ina219_drv::setCalibration(unsigned int maxCurrent, float shuntR) {
38 currentLSB = (maxCurrent / 32767.);
39 calibration = (uint16_t)(40.96 / (currentLSB * shuntR)); // see datasheet
40 writeRegister(INA219::REG_CAL, calibration);
41}
42
43/****************************************************************\
44* Returns current shunt voltage in mV.
45\****************************************************************/
46float ina219_drv::getShuntVoltage(){
47 int16_t raw = (int16_t)readRegister(INA219::REG_SHUNT_V);
48 return (raw * 0.01);
49}
50
51/****************************************************************\
52* Returns current bus voltage in V or INA219::OVERFLOW
53* on math overflow.
54\****************************************************************/
55float ina219_drv::getBusVoltage(){
56 uint16_t raw = readRegister(INA219::REG_BUS_V);
57 bool ovf = raw & 0b1; // overflow flag
58 //bool cnvr = raw & 0b10; // conversion ready flag
59
60 if (ovf)
61 return INA219::OVERFLOW; // return magicnumber if overflow occured
62
63 // eliminate flags
64 float result = (int16_t)((raw >> 3) * 4); // LSB = 4 mV
65 result *= 0.001; // mV -> V
66 return result;
67}
68
69/****************************************************************\
70* Returns current in mA.
71* Warning: setCalibration() has to be called first.
72\****************************************************************/
73float ina219_drv::getCurrent() {
74 int16_t raw = (int16_t)readRegister(INA219::REG_CURRENT);
75 return currentLSB * raw;
76}
77
78void ina219_drv::writeRegister(uint8_t reg, uint16_t value){
79 uint8_t txbuf[3];
80 txbuf[0] = reg;
81 txbuf[1] = value >> 8; // MSB
82 txbuf[2] = value & 0xFF; // LSB
83 _i2c.i2cWrite(_i2c_addr, txbuf, 3);
84}
85
86uint16_t ina219_drv::readRegister(uint8_t reg){
87 uint8_t txbuf[] = { reg };
88 uint8_t rxbuf[2];
89 _i2c.i2cWrite(_i2c_addr, txbuf, 1);
90 _i2c.i2cRead (_i2c_addr, rxbuf, 2);
91 return ((rxbuf[0] << 8) | rxbuf[1]);
92}
93