void ina3221_trigger_measurement(){ //send configuration to trigger one measurement ina3221_set_config(config); //wait until the measurement is ready while ((ina3221_read_value(INA_MASK_ENABLE_R)&(1)) ==0) { } }
static int ina3221_read_curr(struct device *dev, u32 attr, int channel, long *val) { struct ina3221_data *ina = dev_get_drvdata(dev); struct ina3221_input *input = &ina->inputs[channel]; int resistance_uo = input->shunt_resistor; u8 reg = ina3221_curr_reg[attr][channel]; int regval, voltage_nv, ret; switch (attr) { case hwmon_curr_input: if (!ina3221_is_enabled(ina, channel)) return -ENODATA; /* Write CONFIG register to trigger a single-shot measurement */ if (ina->single_shot) regmap_write(ina->regmap, INA3221_CONFIG, ina->reg_config); ret = ina3221_wait_for_data(ina); if (ret) return ret; /* fall through */ case hwmon_curr_crit: case hwmon_curr_max: ret = ina3221_read_value(ina, reg, ®val); if (ret) return ret; /* Scale of shunt voltage: LSB is 40uV (40000nV) */ voltage_nv = regval * 40000; /* Return current in mA */ *val = DIV_ROUND_CLOSEST(voltage_nv, resistance_uo); return 0; case hwmon_curr_crit_alarm: case hwmon_curr_max_alarm: /* No actual register read if channel is disabled */ if (!ina3221_is_enabled(ina, channel)) { /* Return 0 for alert flags */ *val = 0; return 0; } ret = regmap_field_read(ina->fields[reg], ®val); if (ret) return ret; *val = regval; return 0; default: return -EOPNOTSUPP; } }
uint16_t ina3221_calculate_current(uint16_t channel){ uint16_t current = 0; //read shunt voltage current = ina3221_read_value(channel); //take absolute value (ina3221 has signed values) current = abs(current); //shift the value 3 times to the right current = current>>3; //calc the current with I=LSB*shuntvoltage / resistor current = current *40/100; return current; }
static int ina3221_read_in(struct device *dev, u32 attr, int channel, long *val) { const bool is_shunt = channel > INA3221_CHANNEL3; struct ina3221_data *ina = dev_get_drvdata(dev); u8 reg = ina3221_in_reg[channel]; int regval, ret; /* Translate shunt channel index to sensor channel index */ channel %= INA3221_NUM_CHANNELS; switch (attr) { case hwmon_in_input: if (!ina3221_is_enabled(ina, channel)) return -ENODATA; /* Write CONFIG register to trigger a single-shot measurement */ if (ina->single_shot) regmap_write(ina->regmap, INA3221_CONFIG, ina->reg_config); ret = ina3221_wait_for_data(ina); if (ret) return ret; ret = ina3221_read_value(ina, reg, ®val); if (ret) return ret; /* * Scale of shunt voltage (uV): LSB is 40uV * Scale of bus voltage (mV): LSB is 8mV */ *val = regval * (is_shunt ? 40 : 8); return 0; case hwmon_in_enable: *val = ina3221_is_enabled(ina, channel); return 0; default: return -EOPNOTSUPP; } }