drivers: sensor: ina23x: add ina23x common module

That's needed to not duplicate code between INA230/231
and the upcoming INA237 variant.

Signed-off-by: Bartosz Bilas <b.bilas@grinn-global.com>
This commit is contained in:
Bartosz Bilas 2021-11-15 19:47:23 +01:00 committed by Maureen Helm
commit f50c7ab542
4 changed files with 108 additions and 0 deletions

View file

@ -3,4 +3,5 @@
zephyr_library()
zephyr_library_sources(ina23x.c)
zephyr_library_sources(ina23x_common.c)
zephyr_library_sources_ifdef(CONFIG_INA23X_TRIGGER ina23x_trigger.c)

View file

@ -0,0 +1,48 @@
/*
* Copyright 2021 Grinn
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <sys/byteorder.h>
#include "ina23x_common.h"
int ina23x_reg_read_24(const struct i2c_dt_spec *bus, uint8_t reg, uint32_t *val)
{
uint8_t data[3];
int ret;
ret = i2c_burst_read_dt(bus, reg, data, sizeof(data));
if (ret < 0) {
return ret;
}
*val = sys_get_be24(data);
return ret;
}
int ina23x_reg_read_16(const struct i2c_dt_spec *bus, uint8_t reg, uint16_t *val)
{
uint8_t data[2];
int ret;
ret = i2c_burst_read_dt(bus, reg, data, sizeof(data));
if (ret < 0) {
return ret;
}
*val = sys_get_be16(data);
return ret;
}
int ina23x_reg_write(const struct i2c_dt_spec *bus, uint8_t reg, uint16_t val)
{
uint8_t tx_buf[3];
tx_buf[0] = reg;
sys_put_be16(val, &tx_buf[1]);
return i2c_write_dt(bus, tx_buf, sizeof(tx_buf));
}

View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 2021 Grinn
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_DRIVERS_SENSOR_INA23X_COMMON_H_
#define ZEPHYR_DRIVERS_SENSOR_INA23X_COMMON_H_
#include <drivers/i2c.h>
/**
* @brief Macro used to test if the current's sign bit is set
*/
#define INA23X_CURRENT_SIGN_BIT BIT(15)
/**
* @brief Macro used to check if the current's LSB is 1mA
*/
#define INA23X_CURRENT_LSB_1MA 1
int ina23x_reg_read_24(const struct i2c_dt_spec *bus, uint8_t reg, uint32_t *val);
int ina23x_reg_read_16(const struct i2c_dt_spec *bus, uint8_t reg, uint16_t *val);
int ina23x_reg_write(const struct i2c_dt_spec *bus, uint8_t reg, uint16_t val);
#endif /* ZEPHYR_DRIVERS_SENSOR_INA23X_COMMON_H_ */

View file

@ -0,0 +1,33 @@
#
# Copyright 2021 Grinn
#
# SPDX-License-Identifier: Apache-2.0
#
include: i2c-device.yaml
properties:
config:
type: int
required: true
description: |
Value of the configuration register
e.g shunt voltage and bus voltage ADC conversion
times and averaging, operating mode for INA230 or
delay for initial ADC conversion, shunt full scale range
for INA237.
current-lsb:
type: int
required: true
description: |
Value of Current LSB in milliamps. When set to 1mA,
Current is read in A, Bus Voltage in V, Shunt
Voltage in V, and Power in mW. Any other value
results in Current, Voltage, and Power registers
being read in counts.
rshunt:
type: int
required: true
description: Shunt resistor value in milliohms