Additionnal functions + fix negative + fix coding format
This commit is contained in:
parent
b9e1472e81
commit
78fff4d5cd
2 changed files with 132 additions and 44 deletions
|
|
@ -17,13 +17,13 @@ static int _wireWriteRegister (uint8_t addr, uint8_t reg, uint16_t value)
|
|||
{
|
||||
i2c_start();
|
||||
if (!i2c_write(addr<<1)) // adress + W
|
||||
goto error ;
|
||||
goto error;
|
||||
if (!i2c_write(reg))
|
||||
goto error;
|
||||
if (!i2c_write((value >> 8) & 0xFF))
|
||||
goto error;
|
||||
if (!i2c_write(value & 0xFF))
|
||||
goto error ;
|
||||
goto error;
|
||||
i2c_stop();
|
||||
|
||||
return 0 ;
|
||||
|
|
@ -31,16 +31,16 @@ static int _wireWriteRegister (uint8_t addr, uint8_t reg, uint16_t value)
|
|||
error:
|
||||
debug("Error while xmitting I2C slave INA3221\n");
|
||||
i2c_stop();
|
||||
return -EIO ;
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
static int _wireReadRegister(uint8_t addr, uint8_t reg, uint16_t *value)
|
||||
{
|
||||
uint8_t* tampon = (uint8_t*)&value ;
|
||||
uint8_t* tampon = (uint8_t*)&value;
|
||||
|
||||
i2c_start();
|
||||
if (!i2c_write(addr<<1)) // adress + W
|
||||
goto error ;
|
||||
goto error;
|
||||
if (!i2c_write(reg))
|
||||
goto error;
|
||||
i2c_stop();
|
||||
|
|
@ -50,98 +50,143 @@ static int _wireReadRegister(uint8_t addr, uint8_t reg, uint16_t *value)
|
|||
tampon[0] = i2c_read(0);
|
||||
tampon[1] = i2c_read(1);
|
||||
i2c_stop();
|
||||
|
||||
//*value = (tampon[1] << 8) | tampon[0] ;
|
||||
|
||||
return 0 ;
|
||||
return 0;
|
||||
|
||||
error:
|
||||
debug("Error while xmitting I2C slave INA3221\n");
|
||||
i2c_stop();
|
||||
return -EIO ;
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
int ina3221_trigger(ina3221_t *dev)
|
||||
{
|
||||
return _wireReadRegister(dev->addr, INA3221_REG_MASK, dev->config->mask_register);
|
||||
return _wireReadRegister(dev->addr, INA3221_REG_MASK, &dev->mask.mask_register);
|
||||
}
|
||||
|
||||
int ina3221_sync(ina3221_t *dev)
|
||||
{
|
||||
return _wireReadRegister(dev->addr, INA3221_REG_CONFIG, dev->config->config_register);
|
||||
return _wireReadRegister(dev->addr, INA3221_REG_CONFIG, &dev->config.config_register);
|
||||
}
|
||||
|
||||
int ina3221_setting(ina3221_t *dev ,bool mode, bool bus, bool shunt)
|
||||
{
|
||||
dev->config.mode = mode ;
|
||||
dev->config.ebus = bus ;
|
||||
dev->config.esht = shunt ;
|
||||
dev->config.mode = mode;
|
||||
dev->config.ebus = bus;
|
||||
dev->config.esht = shunt;
|
||||
return _wireWriteRegister(dev->addr, INA3221_REG_CONFIG, dev->config.config_register);
|
||||
}
|
||||
|
||||
int ina3221_enableChannel(ina3221_t *dev ,bool ch1, bool ch2, bool ch3)
|
||||
{
|
||||
dev->config.ch1 = ch1 ;
|
||||
dev->config.ch2 = ch2 ;
|
||||
dev->config.ch3 = ch3 ;
|
||||
dev->config.ch1 = ch1;
|
||||
dev->config.ch2 = ch2;
|
||||
dev->config.ch3 = ch3;
|
||||
return _wireWriteRegister(dev->addr, INA3221_REG_CONFIG, dev->config.config_register);
|
||||
}
|
||||
|
||||
int ina3221_setAverage(ina3221_t *dev, ina3221_avg_t avg)
|
||||
{
|
||||
dev->config.avg = avg ;
|
||||
dev->config.avg = avg;
|
||||
return _wireWriteRegister(dev->addr, INA3221_REG_CONFIG, dev->config.config_register);
|
||||
}
|
||||
|
||||
int ina3221_setBusConversionTime(ina3221_t *dev,ina3221_ct_t ct)
|
||||
{
|
||||
dev->config.vbus = ct ;
|
||||
dev->config.vbus = ct;
|
||||
return _wireWriteRegister(dev->addr, INA3221_REG_CONFIG, dev->config.config_register);
|
||||
}
|
||||
|
||||
int ina3221_setShuntConversionTime(ina3221_t *dev,ina3221_ct_t ct)
|
||||
{
|
||||
dev->config.vsht = ct ;
|
||||
dev->config.vsht = ct;
|
||||
return _wireWriteRegister(dev->addr, INA3221_REG_CONFIG, dev->config.config_register);
|
||||
}
|
||||
|
||||
int ina3221_reset(ina3221_t *dev)
|
||||
{
|
||||
dev->config.config_register = INA3221_DEFAULT_CONFIG ; //dev reset
|
||||
dev->config.mask = INA3221_DEFAULT_CONFIG ; //dev reset
|
||||
dev->mask.mask_register = INA3221_DEFAULT_CONFIG ; //dev reset
|
||||
dev->config.reset = 1 ;
|
||||
return _wireWriteRegister(dev->addr, INA3221_REG_CONFIG, dev->config.config_register); // send reset to device
|
||||
}
|
||||
|
||||
int ina3221_getBusVoltage(ina3221_t *dev, ina3221_channel_t channel, uint32_t *voltage)
|
||||
int ina3221_getBusVoltage(ina3221_t *dev, ina3221_channel_t channel, int32_t *voltage)
|
||||
{
|
||||
uint16_t raw_value ;
|
||||
int err = 0 ;
|
||||
if ((err = _wireReadRegister(dev->addr,INA3221_REG_BUSVOLTAGE_1+channel*2, &raw_value)))
|
||||
int16_t raw_value;
|
||||
int err = 0;
|
||||
if ((err = _wireReadRegister(dev->addr,INA3221_REG_BUSVOLTAGE_1+channel*2, (uint16_t*)&raw_value)))
|
||||
return err;
|
||||
*voltage = (raw_value>>3)*8 ; //mV 8mV step
|
||||
*voltage = (raw_value>>3)*8; //mV 8mV step
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ina3221_getShuntValue(ina3221_t *dev, ina3221_channel_t channel, uint32_t *voltage, uint32_t *current)
|
||||
int ina3221_getShuntValue(ina3221_t *dev, ina3221_channel_t channel, int32_t *voltage, int32_t *current)
|
||||
{
|
||||
uint16_t raw_value ;
|
||||
int err = 0 ;
|
||||
if ((err = _wireReadRegister(dev->addr,INA3221_REG_SHUNTVOLTAGE_1+channel*2, &raw_value)))
|
||||
int16_t raw_value;
|
||||
int err = 0;
|
||||
if ((err = _wireReadRegister(dev->addr,INA3221_REG_SHUNTVOLTAGE_1+channel*2, (uint16_t*)&raw_value)))
|
||||
return err;
|
||||
*voltage = (raw_value>>3)*40 ; //uV 40uV step
|
||||
if(dev->shunt[channel]) *current = *voltage/dev->shunt[channel] ; //mA = uV / mOhm
|
||||
if(!dev->shunt[channel])
|
||||
{
|
||||
debug("No shunt configured for channel %u. Dev:%X INA3221\n",channel+1, dev->addr);
|
||||
return -EINVAL;
|
||||
}
|
||||
*current = *voltage/dev->shunt[channel] ; //mA = uV / mOhm
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ina3221_setCriticalAlert(ina3221_t *dev, ina3221_channel_t channel, uint32_t current)
|
||||
int ina3221_getSumShuntValue(ina3221_t *dev, int32_t *voltage)
|
||||
{
|
||||
uint16_t raw_value = (current*dev->shunt[channel]/40)<<3 ;
|
||||
return _wireWriteRegister(dev->addr,INA3221_REG_CRITICAL_ALERT_1+channel*2, raw_value);
|
||||
int16_t raw_value;
|
||||
int err = 0;
|
||||
if ((err = _wireReadRegister(dev->addr,INA3221_REG_SHUNT_VOLTAGE_SUM, (uint16_t*)&raw_value)))
|
||||
return err;
|
||||
*voltage = (raw_value>>1)*40; //uV 40uV step
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ina3221_setWarningAlert(ina3221_t *dev, ina3221_channel_t channel, uint32_t current)
|
||||
int ina3221_setCriticalAlert(ina3221_t *dev, ina3221_channel_t channel, int32_t current)
|
||||
{
|
||||
uint16_t raw_value = (current*dev->shunt[channel]/40)<<3 ;
|
||||
return _wireWriteRegister(dev->addr,INA3221_REG_WARNING_ALERT_1+channel*2, raw_value);
|
||||
int16_t raw_value = (current*dev->shunt[channel]/40)<<3;
|
||||
uint16_t* ptr_value = (uint16_t*)&raw_value; // FIXME: Need this to convert int to uint without change data ?
|
||||
return _wireWriteRegister(dev->addr,INA3221_REG_CRITICAL_ALERT_1+channel*2, *ptr_value);
|
||||
}
|
||||
|
||||
int ina3221_setWarningAlert(ina3221_t *dev, ina3221_channel_t channel, int32_t current)
|
||||
{
|
||||
int16_t raw_value = (current*dev->shunt[channel]/40)<<3;
|
||||
uint16_t* ptr_value = (uint16_t*)&raw_value; // FIXME: Need this to convert int to uint without change data ?
|
||||
return _wireWriteRegister(dev->addr,INA3221_REG_WARNING_ALERT_1+channel*2, *ptr_value);
|
||||
}
|
||||
|
||||
int ina3221_setSumWarningAlert(ina3221_t *dev, int32_t voltage)
|
||||
{
|
||||
int16_t raw_value = (voltage/40)<<1;
|
||||
uint16_t* ptr_value = (uint16_t*)&raw_value; // FIXME: Need this to convert int to uint without change data ?
|
||||
return _wireWriteRegister(dev->addr,INA3221_REG_SHUNT_VOLTAGE_SUM_LIMIT, *ptr_value);
|
||||
}
|
||||
|
||||
int ina3221_setPowerValidUpperLimit(ina3221_t *dev, int32_t voltage)
|
||||
{
|
||||
if(!dev->config.ebus)
|
||||
{
|
||||
debug("Bus not enable. Dev:%X INA3221\n", dev->addr);
|
||||
return -ENOTSUP;
|
||||
}
|
||||
int16_t raw_value = (voltage/8)<<3;
|
||||
uint16_t* ptr_value = (uint16_t*)&raw_value; // FIXME: Need this to convert int to uint without change data ?
|
||||
return _wireWriteRegister(dev->addr,INA3221_REG_VALID_POWER_UPPER_LIMIT, *ptr_value);
|
||||
}
|
||||
|
||||
int ina3221_setPowerValidLowerLimit(ina3221_t *dev, int32_t voltage)
|
||||
{
|
||||
if(!dev->config.ebus)
|
||||
{
|
||||
debug("Bus not enable. Dev:%X INA3221\n", dev->addr);
|
||||
return -ENOTSUP;
|
||||
}
|
||||
int16_t raw_value = (voltage/8)<<3;
|
||||
uint16_t* ptr_value = (uint16_t*)&raw_value; // FIXME: Need this to convert int to uint without change data ?
|
||||
return _wireWriteRegister(dev->addr,INA3221_REG_VALID_POWER_LOWER_LIMIT, *ptr_value);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,8 +8,12 @@
|
|||
#ifndef INA3221_H_
|
||||
#define INA3221_H_
|
||||
|
||||
#include "espressif/esp_common.h"
|
||||
#include "esp8266.h"
|
||||
#include <errno.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "i2c/i2c.h"
|
||||
|
||||
#define INA3221_ADDR_0 (0x40) // A0 to GND
|
||||
|
|
@ -200,7 +204,7 @@ int ina3221_reset(ina3221_t *dev);
|
|||
* @param voltage Data pointer to get bus voltage (mV)
|
||||
* @return Non-zero if error occured
|
||||
*/
|
||||
int ina3221_getBusVoltage(ina3221_t *dev, ina3221_channel_t channel, uint32_t *voltage);
|
||||
int ina3221_getBusVoltage(ina3221_t *dev, ina3221_channel_t channel, int32_t *voltage);
|
||||
|
||||
/**
|
||||
* Get Shunt voltage (uV) and current (mA)
|
||||
|
|
@ -210,7 +214,16 @@ int ina3221_getBusVoltage(ina3221_t *dev, ina3221_channel_t channel, uint32_t *v
|
|||
* @param current Data pointer to get shunt voltage (mA)
|
||||
* @return Non-zero if error occured
|
||||
*/
|
||||
int ina3221_getShuntValue(ina3221_t *dev, ina3221_channel_t channel, uint32_t *voltage, uint32_t *current);
|
||||
int ina3221_getShuntValue(ina3221_t *dev, ina3221_channel_t channel, int32_t *voltage, int32_t *current);
|
||||
|
||||
/**
|
||||
* Get Shunt-voltage (uV) sum value of selected channels
|
||||
* @param dev Pointer to device descriptor
|
||||
* @param channel Select channel value to get
|
||||
* @param voltage Data pointer to get shunt voltage (uV)
|
||||
* @return Non-zero if error occured
|
||||
*/
|
||||
int ina3221_getSumShuntValue(ina3221_t *dev, int32_t *voltage);
|
||||
|
||||
/**
|
||||
* Set Critical alert (when measurement(s) is greater that value set )
|
||||
|
|
@ -219,7 +232,7 @@ int ina3221_getShuntValue(ina3221_t *dev, ina3221_channel_t channel, uint32_t *v
|
|||
* @param current Value to set (mA)
|
||||
* @return Non-zero if error occured
|
||||
*/
|
||||
int ina3221_setCriticalAlert(ina3221_t *dev, ina3221_channel_t channel, uint32_t current);
|
||||
int ina3221_setCriticalAlert(ina3221_t *dev, ina3221_channel_t channel, int32_t current);
|
||||
|
||||
/**
|
||||
* Set Warning alert (when average measurement(s) is greater that value set )
|
||||
|
|
@ -228,6 +241,36 @@ int ina3221_setCriticalAlert(ina3221_t *dev, ina3221_channel_t channel, uint32_t
|
|||
* @param current Value to set (mA)
|
||||
* @return Non-zero if error occured
|
||||
*/
|
||||
int ina3221_setWarningAlert(ina3221_t *dev, ina3221_channel_t channel, uint32_t current);
|
||||
int ina3221_setWarningAlert(ina3221_t *dev, ina3221_channel_t channel, int32_t current);
|
||||
|
||||
/**
|
||||
* Set Sum Warning alert (Compared to each completed cycle of all selected channels : Sum register )
|
||||
* @param dev Pointer to device descriptor
|
||||
* @param voltage voltage to set (uV)
|
||||
* @return Non-zero if error occured
|
||||
*/
|
||||
int ina3221_setSumWarningAlert(ina3221_t *dev, int32_t voltage);
|
||||
|
||||
/**
|
||||
* Set Power-valid upper-limit ( To determine if power conditions are met.)( bus need enable )
|
||||
* If bus voltage exceed the value set, PV pin is high
|
||||
* @param dev Pointer to device descriptor
|
||||
* @param voltage voltage to set (mV)
|
||||
* @return Non-zero if error occured
|
||||
*/
|
||||
int ina3221_setPowerValidUpperLimit(ina3221_t *dev, int32_t voltage);
|
||||
|
||||
/**
|
||||
* Set Power-valid lower-limit ( To determine if power conditions are met.)( bus need enable )
|
||||
* If bus voltage drops below the value set, PV pin is low
|
||||
* @param dev Pointer to device descriptor
|
||||
* @param voltage voltage to set (mV)
|
||||
* @return Non-zero if error occured
|
||||
*/
|
||||
int ina3221_setPowerValidLowerLimit(ina3221_t *dev, int32_t voltage);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* INA3221_H_ */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue