|
@@ -21,112 +21,54 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
#include "INA219.h"
|
|
|
#include "i2c.h"
|
|
|
|
|
|
-static uint8_t inaAddress;
|
|
|
static uint16_t currentLSB, powerLSB;
|
|
|
-static uint16_t vShuntMax;
|
|
|
-static uint16_t vBusMax;
|
|
|
-static uint16_t Rshunt;
|
|
|
+static uint16_t vShuntMax; // millivolt
|
|
|
+static uint16_t vBusMax; // millivolt
|
|
|
+static uint16_t rShunt; // milliohms
|
|
|
|
|
|
-/**
|
|
|
- * Configure I2C, INA219
|
|
|
- */
|
|
|
-void INA219_Config(INA219_InitTypeDef * INA219_InitStruct) {
|
|
|
- uint16_t config;
|
|
|
+static uint8_t flagCNVR; // Conversion Ready
|
|
|
+static uint8_t flagOVF; // Math Overflow Flag
|
|
|
|
|
|
- /** INA219 configure */
|
|
|
- config = INA219_InitStruct->INA219_RST;
|
|
|
- config |= INA219_InitStruct->INA219_BVR;
|
|
|
- config |= INA219_InitStruct->INA219_PG;
|
|
|
- config |= INA219_InitStruct->INA219_BADC;
|
|
|
- config |= INA219_InitStruct->INA219_SADC;
|
|
|
- config |= INA219_InitStruct->INA219_MODE;
|
|
|
-
|
|
|
- inaAddress = INA219_InitStruct->INA219_Addr;
|
|
|
-
|
|
|
- switch(INA219_InitStruct->INA219_BVR) {
|
|
|
- case INA219_RANGE_32V:
|
|
|
- vBusMax = 32000;
|
|
|
- break;
|
|
|
- case INA219_RANGE_16V:
|
|
|
- vBusMax = 16000;
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
- switch(INA219_InitStruct->INA219_PG) {
|
|
|
- case INA219_GAIN_320MV:
|
|
|
- vShuntMax = 320;
|
|
|
- break;
|
|
|
- case INA219_GAIN_160MV:
|
|
|
- vShuntMax = 160;
|
|
|
- break;
|
|
|
- case INA219_GAIN_80MV:
|
|
|
- vShuntMax = 80;
|
|
|
- break;
|
|
|
- case INA219_GAIN_40MV:
|
|
|
- vShuntMax = 40;
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
- I2C_Write_Transaction(inaAddress, INA219_REG_CONFIG, config);
|
|
|
-}
|
|
|
+static t_i2c_status status = I2C_SUCCESS;
|
|
|
|
|
|
/**
|
|
|
- * @brief Fills each INA219_InitStruct member with its default value.
|
|
|
- * @param INA219_InitStruct: pointer to a INA219_InitTypeDef structure which will
|
|
|
- * be initialized.
|
|
|
- * @retval None
|
|
|
- */
|
|
|
-void INA219_StructInit(INA219_InitTypeDef * INA219_InitStruct) {
|
|
|
- /* Reset INA219 init structure parameters values */
|
|
|
- INA219_InitStruct->INA219_Addr = INA219_ADDRESS;
|
|
|
- INA219_InitStruct->INA219_RST = INA219_RESET_OFF;
|
|
|
- INA219_InitStruct->INA219_BVR = INA219_RANGE_32V;
|
|
|
- INA219_InitStruct->INA219_PG = INA219_GAIN_320MV;
|
|
|
- INA219_InitStruct->INA219_BADC = INA219_BUS_RES_12BIT;
|
|
|
- INA219_InitStruct->INA219_SADC = INA219_SHUNT_RES_12BIT;
|
|
|
- INA219_InitStruct->INA219_MODE = INA219_MODE_SHUNT_BUS_CONT;
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * Calculate calibration values.
|
|
|
- * rShuntValue in miliohms, iMaxExpected in miliampers.
|
|
|
+ * Configure I2C, INA219
|
|
|
*/
|
|
|
-void INA219_Calibrate(void) {
|
|
|
+void INA219_Config(void) {
|
|
|
+ uint16_t config;
|
|
|
uint16_t calibrationValue;
|
|
|
- /* uint64_t tmp; */
|
|
|
|
|
|
- Rshunt = CURRENT_SHUNT_RESISTENCE; // in milli ohms
|
|
|
+ vBusMax = 32000; // !!! Maximum input voltage only 26V !!!
|
|
|
+ vShuntMax = 80; // !! for shunt 10 mOhm and maximum 8A input current
|
|
|
+ rShunt = CURRENT_SHUNT_RESISTANCE; // in mOhm
|
|
|
|
|
|
- /*
|
|
|
- tmp = iMaxExpected * 1000UL;
|
|
|
- currentLSB = (uint16_t)((tmp + 16384) / 32768); // uA
|
|
|
- powerLSB = (uint16_t)(((tmp * 20) + 16384) / 32768); // uW
|
|
|
+ /** INA219 configure */
|
|
|
+ config = INA219_RESET_OFF; // INA219_RST
|
|
|
+ config |= INA219_RANGE_32V; // INA219_BVR
|
|
|
+ config |= INA219_GAIN_80MV; // INA219_PG
|
|
|
+ config |= INA219_BUS_RES_12BIT_128S; // INA219_BADC
|
|
|
+ config |= INA219_SHUNT_RES_12BIT_128S; //INA219_SADC
|
|
|
+ config |= INA219_MODE_SHUNT_BUS_CONT; //INA219_MODE
|
|
|
|
|
|
- tmp = ((tmp * rShuntValue * 1000UL) + 16384) / 32768UL;
|
|
|
- calibrationValue = (uint16_t)((40960000000UL + (tmp >> 1)) / tmp);
|
|
|
- */
|
|
|
+ i2c_wr_reg(INA219_ADDRESS, INA219_REG_CONFIG, config);
|
|
|
|
|
|
/**
|
|
|
- * Íàõåð òàêèå ðàñ÷¸òû...
|
|
|
* ôîðìóëû èç ìàíóàëà:
|
|
|
* Current_LSB = Maximum Expected Current / 2^15
|
|
|
* Power_LSB = 20 * Current_LSB
|
|
|
* Calibration_Value = trunc( 0.04096 / (Current_LSB * Rshunt) )
|
|
|
*/
|
|
|
+ currentLSB = 244; // 8000000 uA / 32768
|
|
|
+ powerLSB = 4880;
|
|
|
|
|
|
- /* íàãðóçêà 50Âò, ìàêñ òîê 4, øóíò ó íàñ 20 ìÎ,
|
|
|
- Çíà÷åíèÿ LSB â ìèêðîàìïåðàõ è ìèêðîâàòàõ äëÿ äàëüíåéøèõ ðàñ÷¸òîâ */
|
|
|
- currentLSB = 122; // 4000000 uA / 32768
|
|
|
- powerLSB = 2440;
|
|
|
-
|
|
|
- calibrationValue = 16777; // 0.04096 / ((4/32768) * 0.02)
|
|
|
+ calibrationValue = 16777; // 0.04096 / ((8/32768) * 0.01)
|
|
|
|
|
|
- I2C_Write_Transaction(inaAddress, INA219_REG_CALIBRATION, calibrationValue);
|
|
|
+ i2c_wr_reg(INA219_ADDRESS, INA219_REG_CALIBRATION, calibrationValue);
|
|
|
}
|
|
|
|
|
|
/** */
|
|
|
uint16_t getMaxPossibleCurrent(void) {
|
|
|
- return (((1000 * vShuntMax) + (Rshunt>>1)) / Rshunt);
|
|
|
+ return (((1000 * vShuntMax) + (rShunt>>1)) / rShunt);
|
|
|
}
|
|
|
|
|
|
/** */
|
|
@@ -143,7 +85,7 @@ uint16_t getMaxCurrent(void) {
|
|
|
|
|
|
/** */
|
|
|
uint16_t getMaxShuntVoltage(void) {
|
|
|
- uint16_t maxVoltage = ((getMaxCurrent() * Rshunt) + 500) / 1000;
|
|
|
+ uint16_t maxVoltage = ((getMaxCurrent() * rShunt) + 500) / 1000;
|
|
|
|
|
|
if (maxVoltage >= vShuntMax) {
|
|
|
return vShuntMax;
|
|
@@ -162,7 +104,11 @@ uint16_t readBusCurrent(void) {
|
|
|
uint16_t current;
|
|
|
int16_t tmp;
|
|
|
|
|
|
- I2C_Read_Transaction(inaAddress, INA219_REG_CURRENT, (uint16_t *)&tmp);
|
|
|
+ status = i2c_rd_reg(INA219_ADDRESS, INA219_REG_CURRENT, (uint16_t *)&tmp);
|
|
|
+ if (status != I2C_SUCCESS) {
|
|
|
+ return 9999;
|
|
|
+ }
|
|
|
+
|
|
|
if (tmp < 0) {
|
|
|
tmp = - tmp;
|
|
|
}
|
|
@@ -174,7 +120,7 @@ uint16_t readBusCurrent(void) {
|
|
|
/** */
|
|
|
uint32_t readBusPower(void) {
|
|
|
uint16_t power;
|
|
|
- I2C_Read_Transaction(inaAddress, INA219_REG_POWER, &power);
|
|
|
+ i2c_rd_reg(INA219_ADDRESS, INA219_REG_POWER, &power);
|
|
|
return (((power * powerLSB) + 500) / 1000);
|
|
|
}
|
|
|
|
|
@@ -183,7 +129,10 @@ uint32_t readBusPower(void) {
|
|
|
*/
|
|
|
int16_t readShuntVoltage(void) {
|
|
|
uint16_t shvolt;
|
|
|
- I2C_Read_Transaction(inaAddress, INA219_REG_SHUNTVOLTAGE, &shvolt);
|
|
|
+ status = i2c_rd_reg(INA219_ADDRESS, INA219_REG_SHUNTVOLTAGE, &shvolt);
|
|
|
+ if (status != I2C_SUCCESS) {
|
|
|
+ return 999;
|
|
|
+ }
|
|
|
return shvolt;
|
|
|
}
|
|
|
|
|
@@ -192,14 +141,29 @@ int16_t readShuntVoltage(void) {
|
|
|
*/
|
|
|
uint16_t readBusVoltage(void) {
|
|
|
uint16_t volt;
|
|
|
- I2C_Read_Transaction(inaAddress, INA219_REG_BUSVOLTAGE, &volt);
|
|
|
+
|
|
|
+ flagCNVR = 0;
|
|
|
+ flagOVF = 0;
|
|
|
+
|
|
|
+ status = i2c_rd_reg(INA219_ADDRESS, INA219_REG_BUSVOLTAGE, &volt);
|
|
|
+ if (status != I2C_SUCCESS) {
|
|
|
+ return 65535;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ((volt & 0x0001) != 0) {
|
|
|
+ flagOVF = 1;
|
|
|
+ }
|
|
|
+ if ((volt & 0x0002) != 0) {
|
|
|
+ flagCNVR = 1;
|
|
|
+ }
|
|
|
+
|
|
|
return ((volt >> 3) * 4);
|
|
|
}
|
|
|
|
|
|
ina219_bvr_t getRange(void) {
|
|
|
uint16_t value;
|
|
|
|
|
|
- I2C_Read_Transaction(inaAddress, INA219_REG_CONFIG, &value);
|
|
|
+ i2c_rd_reg(INA219_ADDRESS, INA219_REG_CONFIG, &value);
|
|
|
value &= 0x2000;
|
|
|
value >>= 13;
|
|
|
|
|
@@ -209,7 +173,7 @@ ina219_bvr_t getRange(void) {
|
|
|
ina219_pg_t getGain(void) {
|
|
|
uint16_t value;
|
|
|
|
|
|
- I2C_Read_Transaction(inaAddress, INA219_REG_CONFIG, &value);
|
|
|
+ i2c_rd_reg(INA219_ADDRESS, INA219_REG_CONFIG, &value);
|
|
|
value &= 0x1800;
|
|
|
value >>= 11;
|
|
|
|
|
@@ -219,7 +183,7 @@ ina219_pg_t getGain(void) {
|
|
|
ina219_badc_t getBusRes(void) {
|
|
|
uint16_t value;
|
|
|
|
|
|
- I2C_Read_Transaction(inaAddress, INA219_REG_CONFIG, &value);
|
|
|
+ i2c_rd_reg(INA219_ADDRESS, INA219_REG_CONFIG, &value);
|
|
|
value &= 0x0780;
|
|
|
value >>= 7;
|
|
|
|
|
@@ -229,7 +193,7 @@ ina219_badc_t getBusRes(void) {
|
|
|
ina219_sadc_t getShuntRes(void) {
|
|
|
uint16_t value;
|
|
|
|
|
|
- I2C_Read_Transaction(inaAddress, INA219_REG_CONFIG, &value);
|
|
|
+ i2c_rd_reg(INA219_ADDRESS, INA219_REG_CONFIG, &value);
|
|
|
value &= 0x0078;
|
|
|
value >>= 3;
|
|
|
|
|
@@ -239,7 +203,7 @@ ina219_sadc_t getShuntRes(void) {
|
|
|
ina219_mode_t getMode(void) {
|
|
|
uint16_t value;
|
|
|
|
|
|
- I2C_Read_Transaction(inaAddress, INA219_REG_CONFIG, &value);
|
|
|
+ i2c_rd_reg(INA219_ADDRESS, INA219_REG_CONFIG, &value);
|
|
|
value &= 0x0007;
|
|
|
|
|
|
return (ina219_mode_t)value;
|