Merge pull request #484 from Zaltora/ina3221_option

Ina3221 crash with NULL fix
This commit is contained in:
Ruslan V. Uss 2017-11-22 16:43:39 +05:00 committed by GitHub
commit 9b4a58c8e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 11 deletions

View file

@ -90,7 +90,7 @@ void ina_measure(void *pvParameters)
if(dev.mask.wf&(1<<(3-WARNING_CHANNEL))) if(dev.mask.wf&(1<<(3-WARNING_CHANNEL)))
warning = true ; warning = true ;
#endif #endif
for (uint8_t i = 0 ; i < BUS_NUMBER ; i++) for (uint8_t i = 0 ; i < INA3221_BUS_NUMBER ; i++)
{ {
if(ina3221_getBusVoltage(&dev, i, &bus_voltage)) // Get voltage in V if(ina3221_getBusVoltage(&dev, i, &bus_voltage)) // Get voltage in V
goto error_loop; goto error_loop;

View file

@ -132,6 +132,7 @@ int ina3221_getBusVoltage(ina3221_t *dev, ina3221_channel_t channel, float *volt
int err = 0; int err = 0;
if ((err = _wireReadRegister(&dev->i2c_dev, INA3221_REG_BUSVOLTAGE_1 + channel * 2, (uint16_t*)&raw_value))) if ((err = _wireReadRegister(&dev->i2c_dev, INA3221_REG_BUSVOLTAGE_1 + channel * 2, (uint16_t*)&raw_value)))
return err; return err;
if (voltage)
*voltage = raw_value * 0.001; //V 8mV step *voltage = raw_value * 0.001; //V 8mV step
return 0; return 0;
} }
@ -140,15 +141,18 @@ int ina3221_getShuntValue(ina3221_t *dev, ina3221_channel_t channel, float *volt
{ {
int16_t raw_value; int16_t raw_value;
int err = 0; int err = 0;
if ((err = _wireReadRegister(&dev->i2c_dev, INA3221_REG_SHUNTVOLTAGE_1 + channel * 2, (uint16_t*)&raw_value))) if ((err = _wireReadRegister(&dev->i2c_dev,INA3221_REG_SHUNTVOLTAGE_1+channel*2, (uint16_t*)&raw_value)))
return err; return err;
*voltage = raw_value * 0.005; //mV 40uV step float compute = raw_value*0.005; //mV 40uV step
if (!dev->shunt[channel]) if (voltage)
*voltage = compute;
if(!dev->shunt[channel])
{ {
debug("No shunt configured for channel %u. Dev:%u:%X\n", channel+1, dev->bus, dev->addr); debug("No shunt configured for channel %u. Dev:%u:%X\n", channel+1, dev->bus, dev->addr);
return -EINVAL; return -EINVAL;
} }
*current = (*voltage * 1000.0) / dev->shunt[channel]; //mA if (current)
*current = (compute*1000.0)/dev->shunt[channel]; //mA
return 0; return 0;
} }

View file

@ -26,7 +26,7 @@ extern "C" {
#define INA3221_ADDR_2 (0x42) ///< A0 to SDA #define INA3221_ADDR_2 (0x42) ///< A0 to SDA
#define INA3221_ADDR_3 (0x43) ///< A0 to SCL #define INA3221_ADDR_3 (0x43) ///< A0 to SCL
#define BUS_NUMBER 3 ///< Number of shunt available #define INA3221_BUS_NUMBER 3 ///< Number of shunt available
#define INA3221_REG_CONFIG (0x00) #define INA3221_REG_CONFIG (0x00)
#define INA3221_REG_SHUNTVOLTAGE_1 (0x01) #define INA3221_REG_SHUNTVOLTAGE_1 (0x01)
@ -134,7 +134,7 @@ typedef union
*/ */
typedef struct { typedef struct {
const i2c_dev_t i2c_dev; ///< ina3221 I2C address const i2c_dev_t i2c_dev; ///< ina3221 I2C address
const uint16_t shunt[BUS_NUMBER]; ///< Memory of shunt value (mOhm) const uint16_t shunt[INA3221_BUS_NUMBER]; ///< Memory of shunt value (mOhm)
ina3221_config_t config; ///< Memory of ina3221 config ina3221_config_t config; ///< Memory of ina3221 config
ina3221_mask_t mask; ///< Memory of mask_config ina3221_mask_t mask; ///< Memory of mask_config
} ina3221_t; } ina3221_t;