test: sensor: ina237: add emulator unit test

Add emulator unit test of the INA237.

Signed-off-by: Eric Holmberg <eric.holmberg@northriversystems.co.nz>
This commit is contained in:
Eric Holmberg 2023-07-21 12:46:59 +12:00 committed by Maureen Helm
commit 19da119c17
9 changed files with 411 additions and 5 deletions

View file

@ -20,22 +20,29 @@ LOG_MODULE_REGISTER(INA237, CONFIG_SENSOR_LOG_LEVEL);
#define INA237_CAL_SCALING 8192ULL
/** @brief The LSB value for the bus voltage register, in microvolts/LSB. */
#define INA237_BUS_VOLTAGE_UV_LSB 3125
#define INA237_BUS_VOLTAGE_TO_uV(x) ((x) * 3125U)
/** @brief Power scaling (scaled by 10) */
#define INA237_POWER_SCALING 2
/**
* @brief Scale die temperture from 0.125 degC/bit to micro-degrees C
* Note that the bottom 4 bits are reserved and are always zero.
*/
#define INA237_DIETEMP_TO_uDegC(x) (((x) >> 4) * 125000)
static int ina237_channel_get(const struct device *dev, enum sensor_channel chan,
struct sensor_value *val)
{
struct ina237_data *data = dev->data;
const struct ina237_config *config = dev->config;
uint32_t bus_uv, current_ua, power_uw;
int32_t sign;
int32_t temp_uDegC;
int32_t sign = 1;
switch (chan) {
case SENSOR_CHAN_VOLTAGE:
bus_uv = data->bus_voltage * INA237_BUS_VOLTAGE_UV_LSB;
bus_uv = INA237_BUS_VOLTAGE_TO_uV(data->bus_voltage);
val->val1 = bus_uv / 1000000U;
val->val2 = bus_uv % 1000000U;
@ -67,6 +74,17 @@ static int ina237_channel_get(const struct device *dev, enum sensor_channel chan
val->val2 = (int32_t)(power_uw % 1000000U);
break;
case SENSOR_CHAN_DIE_TEMP:
temp_uDegC = INA237_DIETEMP_TO_uDegC(data->die_temp);
val->val1 = temp_uDegC / 1000000L;
val->val2 = abs(temp_uDegC) % 1000000L;
if (temp_uDegC < 0) {
val->val2 = -val->val2;
}
break;
default:
return -ENOTSUP;
}
@ -155,6 +173,14 @@ static int ina237_read_data(const struct device *dev)
}
}
if ((data->chan == SENSOR_CHAN_ALL) || (data->chan == SENSOR_CHAN_DIE_TEMP)) {
ret = ina23x_reg_read_16(&config->bus, INA237_REG_DIETEMP, &data->die_temp);
if (ret < 0) {
LOG_ERR("Failed to read temperature");
return ret;
}
}
return 0;
}
@ -169,7 +195,7 @@ static int ina237_sample_fetch(const struct device *dev, enum sensor_channel cha
struct ina237_data *data = dev->data;
if (chan != SENSOR_CHAN_ALL && chan != SENSOR_CHAN_VOLTAGE && chan != SENSOR_CHAN_CURRENT &&
chan != SENSOR_CHAN_POWER) {
chan != SENSOR_CHAN_POWER && chan != SENSOR_CHAN_DIE_TEMP) {
return -ENOTSUP;
}

View file

@ -21,7 +21,7 @@
#define INA237_REG_CALIB 0x02
#define INA237_REG_SHUNT_VOLT 0x04
#define INA237_REG_BUS_VOLT 0x05
#define INA237_REG_MASK 0x06
#define INA237_REG_DIETEMP 0x06
#define INA237_REG_CURRENT 0x07
#define INA237_REG_POWER 0x08
#define INA237_REG_ALERT 0x0B
@ -40,6 +40,7 @@ struct ina237_data {
uint16_t current;
uint16_t bus_voltage;
uint32_t power;
int16_t die_temp;
enum sensor_channel chan;
struct ina23x_trigger trigger;
};