Round,functions and fix.

add round when send value to component. rework sync function. Add
functions with mask features. Fix I2C data receive
This commit is contained in:
lilian 2016-12-21 10:08:42 +01:00
parent 78fff4d5cd
commit 69280f8ec8
2 changed files with 61 additions and 13 deletions

View file

@ -13,6 +13,8 @@
#define debug(fmt, ...)
#endif
#define sign(x) ((x > 0) - (x < 0))
static int _wireWriteRegister (uint8_t addr, uint8_t reg, uint16_t value)
{
i2c_start();
@ -47,8 +49,8 @@ static int _wireReadRegister(uint8_t addr, uint8_t reg, uint16_t *value)
i2c_start(); // restart condition
if (!i2c_write((addr<<1) | 1)) // adress + R
goto error;
tampon[0] = i2c_read(0);
tampon[1] = i2c_read(1);
tampon[1] = i2c_read(0);
tampon[0] = i2c_read(1);
i2c_stop();
return 0;
@ -65,7 +67,15 @@ int ina3221_trigger(ina3221_t *dev)
int ina3221_sync(ina3221_t *dev)
{
return _wireReadRegister(dev->addr, INA3221_REG_CONFIG, &dev->config.config_register);
uint16_t ptr_config ;
int err = 0;
if ((err = _wireReadRegister(dev->addr, INA3221_REG_CONFIG, &ptr_config))) // Read config
return err;
if( ptr_config != dev->config.config_register) {
if ((err = _wireWriteRegister(dev->addr, INA3221_REG_CONFIG, dev->config.config_register))) // Update config
return err;
}
return 0;
}
int ina3221_setting(ina3221_t *dev ,bool mode, bool bus, bool shunt)
@ -84,6 +94,21 @@ int ina3221_enableChannel(ina3221_t *dev ,bool ch1, bool ch2, bool ch3)
return _wireWriteRegister(dev->addr, INA3221_REG_CONFIG, dev->config.config_register);
}
int ina3221_enableChannelSum(ina3221_t *dev ,bool ch1, bool ch2, bool ch3)
{
dev->mask.scc1 = ch1;
dev->mask.scc2 = ch2;
dev->mask.scc3 = ch3;
return _wireWriteRegister(dev->addr, INA3221_REG_MASK, dev->mask.mask_register);
}
int ina3221_enableLatchPin(ina3221_t *dev ,bool warning, bool critical)
{
dev->mask.wen = warning;
dev->mask.cen = critical;
return _wireWriteRegister(dev->addr, INA3221_REG_MASK, dev->mask.mask_register);
}
int ina3221_setAverage(ina3221_t *dev, ina3221_avg_t avg)
{
dev->config.avg = avg;
@ -148,21 +173,21 @@ int ina3221_getSumShuntValue(ina3221_t *dev, int32_t *voltage)
int ina3221_setCriticalAlert(ina3221_t *dev, ina3221_channel_t channel, int32_t current)
{
int16_t raw_value = (current*dev->shunt[channel]/40)<<3;
int16_t raw_value = ((current*dev->shunt[channel]+20*sign(current))/40)<<3; // round and format
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;
int16_t raw_value = ((current*dev->shunt[channel]+ 20*sign(current))/40)<<3; // round and format
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;
int16_t raw_value = ((voltage + 20*sign(voltage))/40)<<1; // round and format
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);
}
@ -174,7 +199,7 @@ int ina3221_setPowerValidUpperLimit(ina3221_t *dev, int32_t voltage)
debug("Bus not enable. Dev:%X INA3221\n", dev->addr);
return -ENOTSUP;
}
int16_t raw_value = (voltage/8)<<3;
int16_t raw_value = ((voltage+4*sign(voltage))/8)<<3; // round and format
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);
}
@ -186,7 +211,7 @@ int ina3221_setPowerValidLowerLimit(ina3221_t *dev, int32_t voltage)
debug("Bus not enable. Dev:%X INA3221\n", dev->addr);
return -ENOTSUP;
}
int16_t raw_value = (voltage/8)<<3;
int16_t raw_value = ((voltage+4*sign(voltage))/8)<<3; // round and format
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);
}

View file

@ -34,10 +34,14 @@ extern "C" {
#define INA3221_REG_VALID_POWER_UPPER_LIMIT (0x10)
#define INA3221_REG_VALID_POWER_LOWER_LIMIT (0x11)
#define INA3221_DEFAULT_CONFIG (0x7127) //Default register after reset
/*
* Default register after reset
*/
#define INA3221_DEFAULT_CONFIG (0x7127)
#define INA3221_DEFAULT_MASK (0x0002)
#define INA3221_DEFAULT_POWER_UPPER_LIMIT (0x2710) //10V
#define INA3221_DEFAULT_POWER_LOWER_LIMIT (0x2328) //9V
/*
* Numbrer of samples
*/
@ -133,7 +137,7 @@ typedef struct {
} ina3221_t;
/**
* get config register from the device
* sync internal config buffer with external device register
* @param dev Pointer to device descriptor
* @return Non-zero if error occured
*/
@ -166,6 +170,25 @@ int ina3221_setting(ina3221_t *dev ,bool mode, bool bus, bool shunt);
*/
int ina3221_enableChannel(ina3221_t *dev ,bool ch1, bool ch2, bool ch3);
/**
* Select channel to be sum (don't impact enable channel status)
* @param dev Pointer to device descriptor
* @param ch1 Enable/Disable channel 1 ( true : enable // false : disable )
* @param ch2 Enable/Disable channel 2 ( true : enable // false : disable )
* @param ch3 Enable/Disable channel 3 ( true : enable // false : disable )
* @return Non-zero if error occured
*/
int ina3221_enableChannelSum(ina3221_t *dev ,bool ch1, bool ch2, bool ch3);
/**
* enable Latch on warning and critical alert pin
* @param dev Pointer to device descriptor
* @param warning Enable/Disable warning latch ( true : Latch // false : Transparent )
* @param critical Enable/Disable critical latch ( true : Latch // false : Transparent )
* @return Non-zero if error occured
*/
int ina3221_enableLatchPin(ina3221_t *dev ,bool warning, bool critical);
/**
* Set average ( number(s) of point measured )
* @param dev Pointer to device descriptor
@ -246,7 +269,7 @@ int ina3221_setWarningAlert(ina3221_t *dev, ina3221_channel_t channel, int32_t c
/**
* 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)
* @param voltage voltage to set (uV) : 40uV step
* @return Non-zero if error occured
*/
int ina3221_setSumWarningAlert(ina3221_t *dev, int32_t voltage);
@ -255,7 +278,7 @@ 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)
* @param voltage voltage to set (mV) : 8mV step
* @return Non-zero if error occured
*/
int ina3221_setPowerValidUpperLimit(ina3221_t *dev, int32_t voltage);
@ -264,7 +287,7 @@ 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)
* @param voltage voltage to set (mV) : 8mV step
* @return Non-zero if error occured
*/
int ina3221_setPowerValidLowerLimit(ina3221_t *dev, int32_t voltage);