Lines Matching +full:scaling +full:- +full:x +full:- +full:resolution

4  * SPDX-License-Identifier: Apache-2.0
14 #include <zephyr/dt-bindings/sensor/ina237.h>
19 /** @brief Calibration scaling value (scaled by 10^-5) */
23 #define INA237_BUS_VOLTAGE_TO_uV(x) ((x) * 3125U) argument
25 /** @brief Power scaling (need factor of 0.2) */
26 #define INA237_POWER_TO_uW(x) ((x) / 5ULL) argument
29 * @brief Scale die temperture from 0.125 degC/bit to micro-degrees C
32 #define INA237_DIETEMP_TO_uDegC(x) (((x) >> 4) * 125000) argument
36 val->val1 = value_microX / 1000000L; in micro_s32_to_sensor_value()
37 val->val2 = value_microX % 1000000L; in micro_s32_to_sensor_value()
42 val->val1 = value_microX / 1000000U; in micro_u64_to_sensor_value()
43 val->val2 = value_microX % 1000000U; in micro_u64_to_sensor_value()
49 const struct ina237_data *data = dev->data; in ina237_channel_get()
50 const struct ina237_config *config = dev->config; in ina237_channel_get()
54 micro_s32_to_sensor_value(val, INA237_BUS_VOLTAGE_TO_uV(data->bus_voltage)); in ina237_channel_get()
59 micro_s32_to_sensor_value(val, data->current * config->current_lsb); in ina237_channel_get()
65 INA237_POWER_TO_uW((uint64_t)data->power * config->current_lsb)); in ina237_channel_get()
70 if (config->config & INA237_CFG_HIGH_PRECISION) { in ina237_channel_get()
71 /* high-resolution mode - 1.25 uV/bit, sensor value is in mV */ in ina237_channel_get()
72 micro_s32_to_sensor_value(val, data->shunt_voltage * 1250); in ina237_channel_get()
74 /* standard-resolution - 5 uV/bit -> nano-volts */ in ina237_channel_get()
75 micro_s32_to_sensor_value(val, data->shunt_voltage * 5000); in ina237_channel_get()
81 micro_s32_to_sensor_value(val, INA237_DIETEMP_TO_uDegC(data->die_temp)); in ina237_channel_get()
85 return -ENOTSUP; in ina237_channel_get()
98 const struct ina237_config *config = dev->config; in ina237_is_triggered_mode_set()
100 uint8_t mode = (config->adc_config & GENMASK(15, 12)) >> 12; in ina237_is_triggered_mode_set()
124 const struct ina237_config *config = dev->config; in ina237_trigg_one_shot_request()
127 ret = ina23x_reg_write(&config->bus, INA237_REG_ADC_CONFIG, config->adc_config); in ina237_trigg_one_shot_request()
140 * @retval -EIO in case of input / output error
144 struct ina237_data *data = dev->data; in ina237_read_data()
145 const struct ina237_config *config = dev->config; in ina237_read_data()
148 if ((data->chan == SENSOR_CHAN_ALL) || (data->chan == SENSOR_CHAN_VOLTAGE)) { in ina237_read_data()
149 ret = ina23x_reg_read_16(&config->bus, INA237_REG_BUS_VOLT, &data->bus_voltage); in ina237_read_data()
156 if ((data->chan == SENSOR_CHAN_ALL) || (data->chan == SENSOR_CHAN_CURRENT)) { in ina237_read_data()
157 ret = ina23x_reg_read_16(&config->bus, INA237_REG_CURRENT, &data->current); in ina237_read_data()
164 if ((data->chan == SENSOR_CHAN_ALL) || (data->chan == SENSOR_CHAN_POWER)) { in ina237_read_data()
165 ret = ina23x_reg_read_24(&config->bus, INA237_REG_POWER, &data->power); in ina237_read_data()
172 if ((data->chan == SENSOR_CHAN_ALL) || (data->chan == SENSOR_CHAN_DIE_TEMP)) { in ina237_read_data()
173 ret = ina23x_reg_read_16(&config->bus, INA237_REG_DIETEMP, &data->die_temp); in ina237_read_data()
181 if ((data->chan == SENSOR_CHAN_ALL) || (data->chan == SENSOR_CHAN_VSHUNT)) { in ina237_read_data()
182 ret = ina23x_reg_read_16(&config->bus, INA237_REG_SHUNT_VOLT, &data->shunt_voltage); in ina237_read_data()
201 struct ina237_data *data = dev->data; in ina237_sample_fetch()
209 return -ENOTSUP; in ina237_sample_fetch()
212 data->chan = chan; in ina237_sample_fetch()
224 const struct ina237_config *config = dev->config; in ina237_attr_set()
225 uint16_t data = val->val1; in ina237_attr_set()
229 return ina23x_reg_write(&config->bus, INA237_REG_CONFIG, data); in ina237_attr_set()
231 return ina23x_reg_write(&config->bus, INA237_REG_CALIB, data); in ina237_attr_set()
234 return -ENOTSUP; in ina237_attr_set()
241 const struct ina237_config *config = dev->config; in ina237_attr_get()
247 ret = ina23x_reg_read_16(&config->bus, INA237_REG_CONFIG, &data); in ina237_attr_get()
253 ret = ina23x_reg_read_16(&config->bus, INA237_REG_CALIB, &data); in ina237_attr_get()
260 return -ENOTSUP; in ina237_attr_get()
263 val->val1 = data; in ina237_attr_get()
264 val->val2 = 0; in ina237_attr_get()
271 const struct ina237_config *config = dev->config; in ina237_calibrate()
275 ret = ina23x_reg_write(&config->bus, INA237_REG_CALIB, config->cal); in ina237_calibrate()
287 const struct ina237_config *config = data->dev->config; in ina237_trigger_work_handler()
292 ret = ina23x_reg_read_16(&config->bus, INA237_REG_ALERT, &reg_alert); in ina237_trigger_work_handler()
298 ret = ina237_read_data(data->dev); in ina237_trigger_work_handler()
303 if (data->trigger.handler_alert) { in ina237_trigger_work_handler()
304 data->trigger.handler_alert(data->dev, data->trigger.trig_alert); in ina237_trigger_work_handler()
310 struct ina237_data *data = dev->data; in ina237_init()
311 const struct ina237_config *config = dev->config; in ina237_init()
315 if (!device_is_ready(config->bus.bus)) { in ina237_init()
316 LOG_ERR("I2C bus %s is not ready", config->bus.bus->name); in ina237_init()
317 return -ENODEV; in ina237_init()
320 data->dev = dev; in ina237_init()
322 ret = ina23x_reg_read_16(&config->bus, INA237_REG_MANUFACTURER_ID, &id); in ina237_init()
330 return -ENODEV; in ina237_init()
333 ret = ina23x_reg_write(&config->bus, INA237_REG_ADC_CONFIG, config->adc_config); in ina237_init()
339 ret = ina23x_reg_write(&config->bus, INA237_REG_CONFIG, config->config); in ina237_init()
352 if ((config->alert_config & GENMASK(15, 14)) != GENMASK(15, 14)) { in ina237_init()
354 return -ENODEV; in ina237_init()
357 k_work_init(&data->trigger.conversion_work, ina237_trigger_work_handler); in ina237_init()
359 ret = ina23x_trigger_mode_init(&data->trigger, &config->alert_gpio); in ina237_init()
365 ret = ina23x_reg_write(&config->bus, INA237_REG_ALERT, config->alert_config); in ina237_init()
379 struct ina237_data *ina237 = dev->data; in ina237_trigger_set()
382 return -ENOTSUP; in ina237_trigger_set()
385 ina237->trigger.handler_alert = handler; in ina237_trigger_set()
386 ina237->trigger.trig_alert = trig; in ina237_trigger_set()
399 /* Shunt calibration must be muliplied by 4 if high-prevision mode is selected */