The INA219 is a combined voltage and current sensor that can measure up to 26 volts and a current that depends on an external shunt resistor. Connection happens via i2c/smbus and the chip features a power supply rail that is independent from the measured voltage, so it can measure low voltages.
This commit adds a driver for this chip. Right now it measures bus voltage and current, and does not use the internal calibrated current reading, nor the available power measurement.
This commit is contained in:
parent
4a3c1ef2ac
commit
d16d4d5568
@ -206,6 +206,18 @@ config LTC4151_I2C_FREQUENCY
|
|||||||
default 400000
|
default 400000
|
||||||
depends on SENSORS_LTC4151
|
depends on SENSORS_LTC4151
|
||||||
|
|
||||||
|
config SENSORS_INA219
|
||||||
|
bool "INA219 current and voltage monitor"
|
||||||
|
default n
|
||||||
|
select I2C
|
||||||
|
---help---
|
||||||
|
Enable driver support for the Texas Instruments INA219 current and voltage monitor.
|
||||||
|
|
||||||
|
config INA219_I2C_FREQUENCY
|
||||||
|
int "INA219 I2C frequency"
|
||||||
|
default 400000
|
||||||
|
depends on SENSORS_INA219
|
||||||
|
|
||||||
config SENSORS_MB7040
|
config SENSORS_MB7040
|
||||||
bool "MaxBotix MB7040 Sonar support"
|
bool "MaxBotix MB7040 Sonar support"
|
||||||
default n
|
default n
|
||||||
|
@ -121,6 +121,10 @@ ifeq ($(CONFIG_SENSORS_LTC4151),y)
|
|||||||
CSRCS += ltc4151.c
|
CSRCS += ltc4151.c
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
ifeq ($(CONFIG_SENSORS_INA219),y)
|
||||||
|
CSRCS += ina219.c
|
||||||
|
endif
|
||||||
|
|
||||||
endif # CONFIG_I2C
|
endif # CONFIG_I2C
|
||||||
|
|
||||||
# These drivers depend on SPI support
|
# These drivers depend on SPI support
|
||||||
|
438
drivers/sensors/ina219.c
Normal file
438
drivers/sensors/ina219.c
Normal file
@ -0,0 +1,438 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* drivers/sensors/ina219.c
|
||||||
|
* Character driver for the INA219 Power Sensor
|
||||||
|
*
|
||||||
|
* Copyright (C) 2017 Sebastien Lorquet. All rights reserved.
|
||||||
|
* Author: Sebastien Lorquet <sebastien@lorquet.fr>
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in
|
||||||
|
* the documentation and/or other materials provided with the
|
||||||
|
* distribution.
|
||||||
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
||||||
|
* used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||||
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||||
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||||
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||||
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||||
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Included Files
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <nuttx/config.h>
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <debug.h>
|
||||||
|
|
||||||
|
#include <nuttx/kmalloc.h>
|
||||||
|
#include <nuttx/fs/fs.h>
|
||||||
|
#include <nuttx/i2c/i2c_master.h>
|
||||||
|
#include <nuttx/sensors/ina219.h>
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Pre-processor Definitions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#if !defined(CONFIG_I2C)
|
||||||
|
# error i2c support required
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define INA219_REG_CONFIG 0 /* See below */
|
||||||
|
#define INA219_REG_SHUNT_VOLTAGE 1 /* Shunt voltage in 10 uV units */
|
||||||
|
#define INA219_REG_BUS_VOLTAGE 2 /* Bus votlage in 4 mV units */
|
||||||
|
#define INA219_REG_POWER 3 /* Requires prior calibration */
|
||||||
|
#define INA219_REG_CURRENT 4 /* Requires prior calibration */
|
||||||
|
#define INA219_REG_CALIBRATION 5 /* Calibration value to compute current */
|
||||||
|
|
||||||
|
/* Operating modes - not controllable by user */
|
||||||
|
|
||||||
|
#define INA219_CONFIG_OPMODE_SHIFT 0
|
||||||
|
#define INA219_CONFIG_OPMODE_MASK (7 << INA219_CONFIG_OPMODE_SHIFT)
|
||||||
|
#define INA219_CONFIG_OPMODE_PWRDOWN (0 << INA219_CONFIG_OPMODE_SHIFT)
|
||||||
|
#define INA219_CONFIG_OPMODE_STRIG (1 << INA219_CONFIG_OPMODE_SHIFT)
|
||||||
|
#define INA219_CONFIG_OPMODE_BTRIG (2 << INA219_CONFIG_OPMODE_SHIFT)
|
||||||
|
#define INA219_CONFIG_OPMODE_SBTRIG (3 << INA219_CONFIG_OPMODE_SHIFT)
|
||||||
|
#define INA219_CONFIG_OPMODE_OFF (4 << INA219_CONFIG_OPMODE_SHIFT)
|
||||||
|
#define INA219_CONFIG_OPMODE_SCONT (5 << INA219_CONFIG_OPMODE_SHIFT)
|
||||||
|
#define INA219_CONFIG_OPMODE_BCONT (6 << INA219_CONFIG_OPMODE_SHIFT)
|
||||||
|
#define INA219_CONFIG_OPMODE_SBCONT (7 << INA219_CONFIG_OPMODE_SHIFT)
|
||||||
|
|
||||||
|
#ifndef CONFIG_INA219_I2C_FREQUENCY
|
||||||
|
# define CONFIG_INA219_I2C_FREQUENCY 400000
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define I2C_NOSTARTSTOP_MSGS 2
|
||||||
|
#define I2C_NOSTARTSTOP_ADDRESS_MSG_INDEX 0
|
||||||
|
#define I2C_NOSTARTSTOP_DATA_MSG_INDEX 1
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Types
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
struct ina219_dev_s
|
||||||
|
{
|
||||||
|
FAR struct i2c_master_s *i2c; /* I2C interface */
|
||||||
|
uint8_t addr; /* I2C address */
|
||||||
|
uint16_t config; /* INA 219 config shadow */
|
||||||
|
int32_t shunt_resistor_value; /* micro-ohms, max 2.15 kohms */
|
||||||
|
};
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Function Prototypes
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* I2C Helpers */
|
||||||
|
|
||||||
|
static int ina219_write16(FAR struct ina219_dev_s *priv, uint8_t regaddr,
|
||||||
|
FAR uint16_t regvalue);
|
||||||
|
static int ina219_read16(FAR struct ina219_dev_s *priv, uint8_t regaddr,
|
||||||
|
FAR uint16_t *regvalue);
|
||||||
|
static int ina219_readpower(FAR struct ina219_dev_s *priv,
|
||||||
|
FAR struct ina219_s *buffer);
|
||||||
|
|
||||||
|
/* Character driver methods */
|
||||||
|
|
||||||
|
static int ina219_open(FAR struct file *filep);
|
||||||
|
static int ina219_close(FAR struct file *filep);
|
||||||
|
static ssize_t ina219_read(FAR struct file *filep, FAR char *buffer,
|
||||||
|
size_t buflen);
|
||||||
|
static ssize_t ina219_write(FAR struct file *filep, FAR const char *buffer,
|
||||||
|
size_t buflen);
|
||||||
|
static int ina219_ioctl(FAR struct file *filep, int cmd,
|
||||||
|
unsigned long arg);
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Data
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
static const struct file_operations g_ina219fops =
|
||||||
|
{
|
||||||
|
ina219_open,
|
||||||
|
ina219_close,
|
||||||
|
ina219_read,
|
||||||
|
ina219_write,
|
||||||
|
NULL,
|
||||||
|
ina219_ioctl
|
||||||
|
#ifndef CONFIG_DISABLE_POLL
|
||||||
|
, NULL
|
||||||
|
#endif
|
||||||
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
||||||
|
, NULL
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
static int ina219_access(FAR struct ina219_dev_s *priv,
|
||||||
|
uint8_t start_register_address, bool reading,
|
||||||
|
FAR uint8_t* register_value, uint8_t data_length)
|
||||||
|
{
|
||||||
|
struct i2c_msg_s msg[I2C_NOSTARTSTOP_MSGS];
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
msg[I2C_NOSTARTSTOP_ADDRESS_MSG_INDEX].frequency = CONFIG_INA219_I2C_FREQUENCY;
|
||||||
|
|
||||||
|
msg[I2C_NOSTARTSTOP_ADDRESS_MSG_INDEX].addr = priv->addr;
|
||||||
|
msg[I2C_NOSTARTSTOP_ADDRESS_MSG_INDEX].flags = 0;
|
||||||
|
msg[I2C_NOSTARTSTOP_ADDRESS_MSG_INDEX].buffer = &start_register_address;
|
||||||
|
msg[I2C_NOSTARTSTOP_ADDRESS_MSG_INDEX].length = 1;
|
||||||
|
|
||||||
|
msg[I2C_NOSTARTSTOP_DATA_MSG_INDEX].addr = msg[I2C_NOSTARTSTOP_ADDRESS_MSG_INDEX].addr;
|
||||||
|
msg[I2C_NOSTARTSTOP_DATA_MSG_INDEX].flags = reading ? I2C_M_READ : 0;
|
||||||
|
msg[I2C_NOSTARTSTOP_DATA_MSG_INDEX].buffer = register_value;
|
||||||
|
msg[I2C_NOSTARTSTOP_DATA_MSG_INDEX].length = data_length;
|
||||||
|
|
||||||
|
ret = I2C_TRANSFER(priv->i2c, msg, I2C_NOSTARTSTOP_MSGS);
|
||||||
|
|
||||||
|
sninfo("start_register_address: "
|
||||||
|
"0x%02X data_length: %d register_value: 0x%02x (0x%04x) ret: %d\n",
|
||||||
|
start_register_address, data_length, *register_value,
|
||||||
|
*((uint16_t*)register_value), ret);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ina219_read16(FAR struct ina219_dev_s *priv, uint8_t regaddr,
|
||||||
|
FAR uint16_t* regvalue)
|
||||||
|
{
|
||||||
|
uint8_t buf[2];
|
||||||
|
|
||||||
|
int ret = ina219_access(priv, regaddr, true, buf, 2);
|
||||||
|
|
||||||
|
if (ret == 0)
|
||||||
|
{
|
||||||
|
*regvalue = ((uint16_t)buf[0] << 8) | (uint16_t)buf[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ina219_write16(FAR struct ina219_dev_s *priv, uint8_t regaddr,
|
||||||
|
FAR uint16_t regvalue)
|
||||||
|
{
|
||||||
|
uint8_t buf[2];
|
||||||
|
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
buf[0] = (regvalue >> 8) & 0xff;
|
||||||
|
buf[1] = regvalue & 0xff;
|
||||||
|
|
||||||
|
ret = ina219_access(priv, regaddr, false, buf, 2);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: ina219_readpower
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Read the current and voltage register with special scaling
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
static int ina219_readpower(FAR struct ina219_dev_s *priv,
|
||||||
|
FAR struct ina219_s *buffer)
|
||||||
|
{
|
||||||
|
uint16_t reg;
|
||||||
|
int64_t tmp;
|
||||||
|
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
/* Read the raw bus voltage */
|
||||||
|
|
||||||
|
ret = ina219_read16(priv, INA219_REG_BUS_VOLTAGE, ®);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
snerr("ERROR: ina219_read16 failed: %d\n", ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Convert register value to bus voltage */
|
||||||
|
|
||||||
|
reg >>= 3; /* 3 LSB of reg contains status bits */
|
||||||
|
buffer->voltage = ((uint32_t)reg) * 4000LU;
|
||||||
|
|
||||||
|
/* Read the raw shunt voltage */
|
||||||
|
|
||||||
|
ret = ina219_read16(priv, INA219_REG_SHUNT_VOLTAGE, ®);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
snerr("ERROR: ina219_read16 failed: %d\n", ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Convert register value to shunt voltage */
|
||||||
|
|
||||||
|
tmp = ((int64_t)(int16_t)reg) * 10LL; /* micro volts across shunt */
|
||||||
|
|
||||||
|
/* Convert shunt voltage to current across the shunt resistor.
|
||||||
|
* I(uA) = U(uV)/R(ohms)
|
||||||
|
* = U(uV)/(R(uohms)/1000000)
|
||||||
|
* = U(uV) * 1000000 / R(uohms)
|
||||||
|
* We use a temporary 64-bit accumulator to avoid overflows.
|
||||||
|
*/
|
||||||
|
|
||||||
|
tmp = tmp * 1000000LL;
|
||||||
|
tmp = tmp / (int64_t)priv->shunt_resistor_value;
|
||||||
|
|
||||||
|
buffer->current = (int32_t)tmp;
|
||||||
|
|
||||||
|
sninfo("Voltage: %u uV, Current: %d uA\n",
|
||||||
|
buffer->voltage, buffer->current);
|
||||||
|
|
||||||
|
return OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: ina219_open
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* This function is called whenever the INA219 device is opened.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
static int ina219_open(FAR struct file *filep)
|
||||||
|
{
|
||||||
|
FAR struct inode *inode = filep->f_inode;
|
||||||
|
FAR struct ina219_dev_s *priv = inode->i_private;
|
||||||
|
|
||||||
|
return ina219_write16(priv, INA219_REG_CONFIG,
|
||||||
|
priv->config | INA219_CONFIG_OPMODE_SBCONT);
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: ina219_close
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* This routine is called when the INA219 device is closed.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
static int ina219_close(FAR struct file *filep)
|
||||||
|
{
|
||||||
|
FAR struct inode *inode = filep->f_inode;
|
||||||
|
FAR struct ina219_dev_s *priv = inode->i_private;
|
||||||
|
|
||||||
|
return ina219_write16(priv, INA219_REG_CONFIG,
|
||||||
|
priv->config | INA219_CONFIG_OPMODE_OFF);
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: ina219_read
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
static ssize_t ina219_read(FAR struct file *filep, FAR char *buffer,
|
||||||
|
size_t buflen)
|
||||||
|
{
|
||||||
|
FAR struct inode *inode = filep->f_inode;
|
||||||
|
FAR struct ina219_dev_s *priv = inode->i_private;
|
||||||
|
FAR struct ina219_s *ptr;
|
||||||
|
ssize_t nsamples;
|
||||||
|
int i;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
/* How many samples were requested to get? */
|
||||||
|
|
||||||
|
nsamples = buflen / sizeof(struct ina219_s);
|
||||||
|
ptr = (FAR struct ina219_s *)buffer;
|
||||||
|
|
||||||
|
sninfo("buflen: %d nsamples: %d\n", buflen, nsamples);
|
||||||
|
|
||||||
|
/* Get the requested number of samples */
|
||||||
|
|
||||||
|
for (i = 0; i < nsamples; i++)
|
||||||
|
{
|
||||||
|
struct ina219_s pwr;
|
||||||
|
|
||||||
|
/* Read the next struct ina219_s power value */
|
||||||
|
|
||||||
|
ret = ina219_readpower(priv, &pwr);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
snerr("ERROR: ina219_readpower failed: %d\n", ret);
|
||||||
|
return (ssize_t)ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Save the temperature value in the user buffer */
|
||||||
|
|
||||||
|
*ptr++ = pwr;
|
||||||
|
}
|
||||||
|
|
||||||
|
return nsamples * sizeof(struct ina219_s);
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: ina219_write
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
static ssize_t ina219_write(FAR struct file *filep, FAR const char *buffer,
|
||||||
|
size_t buflen)
|
||||||
|
{
|
||||||
|
return -ENOSYS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: ina219_ioctl
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
static int ina219_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
||||||
|
{
|
||||||
|
return -ENOTTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: ina219_register
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Register the INA219 character device as 'devpath'
|
||||||
|
*
|
||||||
|
* Input Parameters:
|
||||||
|
* devpath - The full path to the driver to register. E.g., "/dev/pwrmntr0"
|
||||||
|
* i2c - An instance of the I2C interface to use to communicate with INA219
|
||||||
|
* addr - The I2C address of the INA219.
|
||||||
|
* shuntval - the shunt resistor value in micro-ohms.
|
||||||
|
*
|
||||||
|
* Returned Value:
|
||||||
|
* Zero (OK) on success; a negated errno value on failure.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
int ina219_register(FAR const char *devpath, FAR struct i2c_master_s *i2c,
|
||||||
|
uint8_t addr, int32_t shuntval, uint16_t config)
|
||||||
|
{
|
||||||
|
FAR struct ina219_dev_s *priv;
|
||||||
|
int ret = 0;
|
||||||
|
|
||||||
|
/* Sanity check */
|
||||||
|
|
||||||
|
DEBUGASSERT(i2c != NULL);
|
||||||
|
|
||||||
|
/* Initialize the ina219 device structure */
|
||||||
|
|
||||||
|
priv = (FAR struct ina219_dev_s *)kmm_malloc(sizeof(struct ina219_dev_s));
|
||||||
|
if (priv == NULL)
|
||||||
|
{
|
||||||
|
snerr("ERROR: Failed to allocate instance\n");
|
||||||
|
return -ENOMEM;
|
||||||
|
}
|
||||||
|
|
||||||
|
priv->i2c = i2c;
|
||||||
|
priv->addr = addr;
|
||||||
|
priv->shunt_resistor_value = shuntval;
|
||||||
|
|
||||||
|
/* Save the config (except opmode) */
|
||||||
|
|
||||||
|
priv->config = config & ~INA219_CONFIG_OPMODE_MASK;
|
||||||
|
|
||||||
|
/* Apply config, keep chip switched off */
|
||||||
|
|
||||||
|
ret = ina219_write16(priv, INA219_REG_CONFIG,
|
||||||
|
priv->config | INA219_CONFIG_OPMODE_OFF);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
snerr("ERROR: Failed to apply config: %d\n", ret);
|
||||||
|
goto errout;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Register the character driver */
|
||||||
|
|
||||||
|
ret = register_driver(devpath, &g_ina219fops, 0666, priv);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
snerr("ERROR: Failed to register driver: %d\n", ret);
|
||||||
|
goto errout;
|
||||||
|
}
|
||||||
|
|
||||||
|
sninfo("(addr=0x%02x) registered at %s\n", priv->addr, devpath);
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
errout:
|
||||||
|
kmm_free(priv);
|
||||||
|
return ret;
|
||||||
|
}
|
144
include/nuttx/sensors/ina219.h
Normal file
144
include/nuttx/sensors/ina219.h
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* include/nuttx/sensors/ina219.h
|
||||||
|
*
|
||||||
|
* Copyright (C) 2017 Giorgio Groß. All rights reserved.
|
||||||
|
* Author: Giorgio Groß <giorgio.gross@robodev.eu>
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in
|
||||||
|
* the documentation and/or other materials provided with the
|
||||||
|
* distribution.
|
||||||
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
||||||
|
* used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||||
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||||
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||||
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||||
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||||
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#ifndef __INCLUDE_NUTTX_SENSORS_INA219_H
|
||||||
|
#define __INCLUDE_NUTTX_SENSORS_INA219_H
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Included Files
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <nuttx/config.h>
|
||||||
|
#include <nuttx/sensors/ioctl.h>
|
||||||
|
|
||||||
|
#if defined(CONFIG_I2C) && defined(CONFIG_SENSORS_INA219)
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Pre-processor Definitions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* INA219 Register Definitions ***********************************************/
|
||||||
|
/* INA219 Config Register bits */
|
||||||
|
|
||||||
|
#define INA219_CONFIG_SADC_SHIFT 3
|
||||||
|
#define INA219_CONFIG_SADC_MASK (15 << INA219_CONFIG_SADC_SHIFT)
|
||||||
|
#define INA219_CONFIG_SADC_9BIT ( 0 << INA219_CONFIG_SADC_SHIFT)
|
||||||
|
#define INA219_CONFIG_SADC_10BIT ( 1 << INA219_CONFIG_SADC_SHIFT)
|
||||||
|
#define INA219_CONFIG_SADC_11BIT ( 2 << INA219_CONFIG_SADC_SHIFT)
|
||||||
|
#define INA219_CONFIG_SADC_12BIT ( 3 << INA219_CONFIG_SADC_SHIFT)
|
||||||
|
#define INA219_CONFIG_SADC_AVG2 ( 9 << INA219_CONFIG_SADC_SHIFT)
|
||||||
|
#define INA219_CONFIG_SADC_AVG4 (10 << INA219_CONFIG_SADC_SHIFT)
|
||||||
|
#define INA219_CONFIG_SADC_AVG8 (11 << INA219_CONFIG_SADC_SHIFT)
|
||||||
|
#define INA219_CONFIG_SADC_AVG16 (12 << INA219_CONFIG_SADC_SHIFT)
|
||||||
|
#define INA219_CONFIG_SADC_AVG32 (13 << INA219_CONFIG_SADC_SHIFT)
|
||||||
|
#define INA219_CONFIG_SADC_AVG64 (14 << INA219_CONFIG_SADC_SHIFT)
|
||||||
|
#define INA219_CONFIG_SADC_AVG128 (15 << INA219_CONFIG_SADC_SHIFT)
|
||||||
|
|
||||||
|
#define INA219_CONFIG_BADC_SHIFT 7
|
||||||
|
#define INA219_CONFIG_BADC_MASK (15 << INA219_CONFIG_BADC_SHIFT)
|
||||||
|
#define INA219_CONFIG_BADC_9BIT ( 0 << INA219_CONFIG_BADC_SHIFT)
|
||||||
|
#define INA219_CONFIG_BADC_10BIT ( 1 << INA219_CONFIG_BADC_SHIFT)
|
||||||
|
#define INA219_CONFIG_BADC_11BIT ( 2 << INA219_CONFIG_BADC_SHIFT)
|
||||||
|
#define INA219_CONFIG_BADC_12BIT ( 3 << INA219_CONFIG_BADC_SHIFT)
|
||||||
|
#define INA219_CONFIG_BADC_AVG2 ( 9 << INA219_CONFIG_BADC_SHIFT)
|
||||||
|
#define INA219_CONFIG_BADC_AVG4 (10 << INA219_CONFIG_BADC_SHIFT)
|
||||||
|
#define INA219_CONFIG_BADC_AVG8 (11 << INA219_CONFIG_BADC_SHIFT)
|
||||||
|
#define INA219_CONFIG_BADC_AVG16 (12 << INA219_CONFIG_BADC_SHIFT)
|
||||||
|
#define INA219_CONFIG_BADC_AVG32 (13 << INA219_CONFIG_BADC_SHIFT)
|
||||||
|
#define INA219_CONFIG_BADC_AVG64 (14 << INA219_CONFIG_BADC_SHIFT)
|
||||||
|
#define INA219_CONFIG_BADC_AVG128 (15 << INA219_CONFIG_BADC_SHIFT)
|
||||||
|
|
||||||
|
#define INA219_CONFIG_PGA_X1 (0 << 11) /* 40 mV shunt voltage range */
|
||||||
|
#define INA219_CONFIG_PGA_X2 (1 << 11) /* 80 mV shunt voltage range */
|
||||||
|
#define INA219_CONFIG_PGA_X4 (2 << 11) /* 160 mV shunt voltage range */
|
||||||
|
#define INA219_CONFIG_PGA_X8 (3 << 11) /* 320 mV shunt voltage range */
|
||||||
|
|
||||||
|
#define INA219_CONFIG_RANGE_16V 0
|
||||||
|
#define INA219_CONFIG_RANGE_32V (1 << 13)
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Types
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
struct i2c_master_s;
|
||||||
|
|
||||||
|
struct ina219_s
|
||||||
|
{
|
||||||
|
uint32_t voltage; /* [microvolt] max 4.2 kV - device max 26V */
|
||||||
|
int32_t current; /* [microampere] max 2.1 kA - sensor is bidirectional */
|
||||||
|
};
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Function Prototypes
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
#define EXTERN extern "C"
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#else
|
||||||
|
#define EXTERN extern
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: ina219_register
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Register the ina219 character device as 'devpath'
|
||||||
|
*
|
||||||
|
* Input Parameters:
|
||||||
|
* devpath - The full path to the driver to register. E.g., "/dev/pwrmntr0"
|
||||||
|
* i2c - An instance of the I2C interface to use to communicate with INA219
|
||||||
|
* addr - The I2C address of the INA219. The base I2C address of the INA219
|
||||||
|
* is 0x40. Bits 0-3 can be controlled to get 16 unique addresses from 0x40
|
||||||
|
* through 0x4f.
|
||||||
|
* shuntval - resistor value in microohms
|
||||||
|
* config - a combination of the constants defined earlier in this file
|
||||||
|
*
|
||||||
|
* Returned Value:
|
||||||
|
* Zero (OK) on success; a negated errno value on failure.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
int ina219_register(FAR const char *devpath, FAR struct i2c_master_s *i2c,
|
||||||
|
uint8_t addr, int32_t shuntval, uint16_t config);
|
||||||
|
|
||||||
|
#undef EXTERN
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* CONFIG_I2C && CONFIG_SENSORS_INA219 */
|
||||||
|
#endif /* __INCLUDE_NUTTX_SENSORS_INA219_H */
|
Loading…
x
Reference in New Issue
Block a user