Additionnal functions + fix negative + fix coding format

This commit is contained in:
lilian 2016-12-20 15:03:19 +01:00
parent b9e1472e81
commit 78fff4d5cd
2 changed files with 132 additions and 44 deletions

View file

@ -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);
}