Merged in antmerlino/nuttx/ina3221 (pull request #636)

drivers/sensors: Adds support for INA3221 power sensor. This has a 3 channel bus/shunt voltage pairs.

Approved-by: Gregory Nutt <gnutt@nuttx.org>
This commit is contained in:
Anthony Merlino 2018-05-02 22:29:59 +00:00 committed by Gregory Nutt
parent cd12ea7788
commit b1275abd4b
4 changed files with 633 additions and 0 deletions

View File

@ -244,6 +244,18 @@ config INA219_I2C_FREQUENCY
default 400000
depends on SENSORS_INA219
config SENSORS_INA3221
bool "INA3221 current and voltage monitor"
default n
select I2C
---help---
Enable driver support for the Texas Instruments INA3221 current and voltage monitor.
config INA3221_I2C_FREQUENCY
int "INA3221 I2C frequency"
default 400000
depends on SENSORS_INA3221
config SENSORS_MB7040
bool "MaxBotix MB7040 Sonar support"
default n

View File

@ -133,6 +133,10 @@ ifeq ($(CONFIG_SENSORS_INA219),y)
CSRCS += ina219.c
endif
ifeq ($(CONFIG_SENSORS_INA3221),y)
CSRCS += ina3221.c
endif
ifeq ($(CONFIG_SENSORS_SHT21),y)
CSRCS += sht21.c
endif

457
drivers/sensors/ina3221.c Normal file
View File

@ -0,0 +1,457 @@
/****************************************************************************
* drivers/sensors/ina3221.c
* Character driver for the INA3221 Power Sensor
*
* Copyright (C) 2018 Verge Inc. All rights reserved.
* Author: Anthony Merlino <anthony@vergeaero.com>
*
* Adapted from:
* drivers/sensors/ina219.c
*
* 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 <string.h>
#include <nuttx/kmalloc.h>
#include <nuttx/fs/fs.h>
#include <nuttx/i2c/i2c_master.h>
#include <nuttx/sensors/ina3221.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#if !defined(CONFIG_I2C)
# error i2c support required
#endif
#define INA3221_REG_CONFIG 0x00 /* See below */
#define INA3221_REG_CH1_SHUNT_VOLTAGE 0x01 /* Shunt voltage in 40 uV units */
#define INA3221_REG_CH1_BUS_VOLTAGE 0x02 /* Bus votlage in 8 mV units */
#define INA3221_REG_CH2_SHUNT_VOLTAGE 0x03 /* Shunt voltage in 40 uV units */
#define INA3221_REG_CH2_BUS_VOLTAGE 0x04 /* Bus votlage in 8 mV units */
#define INA3221_REG_CH3_SHUNT_VOLTAGE 0x05 /* Shunt voltage in 40 uV units */
#define INA3221_REG_CH3_BUS_VOLTAGE 0x06 /* Bus votlage in 8 mV units */
#define INA3221_REG_CH1_CRIT_LIMIT 0x07 /* Critical Alert limit */
#define INA3221_REG_CH1_WARN_LIMIT 0x08 /* Warning Alert limit */
#define INA3221_REG_CH2_CRIT_LIMIT 0x09 /* Critical Alert limit */
#define INA3221_REG_CH2_WARN_LIMIT 0x0A /* Warning Alert limit */
#define INA3221_REG_CH3_CRIT_LIMIT 0x0B /* Critical Alert limit */
#define INA3221_REG_CH3_WARN_LIMIT 0x0C /* Warning Alert limit */
#define INA3221_REG_VSHUNT_SUM 0x0D /* Shunt Voltage Sum */
#define INA3221_REG_VSHUNT_SUM_LIMIT 0x0E /* Shunt Voltage Sum Limit */
#define INA3221_REG_MASKENABLE 0x0F /* Mask/Enable register */
#define INA3221_REG_POWERVALID_UPPER 0x10 /* Power-Valid Upper Limit */
#define INA3221_REG_POWERVALID_LOWER 0x11 /* Power-Valid Lower Limit */
#define INA3221_REG_MANUFACTURER_ID 0xFE /* Always 0x5449 */
#define INA3221_REG_DIE_ID 0xFF /* Always 0x3220 */
#define INA3221_CONFIG_RST (1 << 15)
#ifndef CONFIG_INA3221_I2C_FREQUENCY
# define CONFIG_INA3221_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 ina3221_dev_s
{
FAR struct i2c_master_s *i2c; /* I2C interface */
uint8_t addr; /* I2C address */
uint16_t config; /* INA 3221 config shadow */
int32_t shunt_resistor[3]; /* micro-ohms */
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/* I2C Helpers */
static int ina3221_write16(FAR struct ina3221_dev_s *priv, uint8_t regaddr,
FAR uint16_t regvalue);
static int ina3221_read16(FAR struct ina3221_dev_s *priv, uint8_t regaddr,
FAR uint16_t *regvalue);
static int ina3221_readpower(FAR struct ina3221_dev_s *priv,
FAR struct ina3221_s *buffer);
/* Character driver methods */
static int ina3221_open(FAR struct file *filep);
static int ina3221_close(FAR struct file *filep);
static ssize_t ina3221_read(FAR struct file *filep, FAR char *buffer,
size_t buflen);
static ssize_t ina3221_write(FAR struct file *filep, FAR const char *buffer,
size_t buflen);
static int ina3221_ioctl(FAR struct file *filep, int cmd,
unsigned long arg);
/****************************************************************************
* Private Data
****************************************************************************/
static const struct file_operations g_ina3221fops =
{
ina3221_open,
ina3221_close,
ina3221_read,
ina3221_write,
NULL,
ina3221_ioctl
#ifndef CONFIG_DISABLE_POLL
, NULL
#endif
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
, NULL
#endif
};
/****************************************************************************
* Private Functions
****************************************************************************/
static int ina3221_access(FAR struct ina3221_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_INA3221_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 ina3221_read16(FAR struct ina3221_dev_s *priv, uint8_t regaddr,
FAR uint16_t* regvalue)
{
uint8_t buf[2];
int ret = ina3221_access(priv, regaddr, true, buf, 2);
if (ret == 0)
{
*regvalue = ((uint16_t)buf[0] << 8) | (uint16_t)buf[1];
}
return ret;
}
static int ina3221_write16(FAR struct ina3221_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 = ina3221_access(priv, regaddr, false, buf, 2);
return ret;
}
/****************************************************************************
* Name: ina3221_readpower
*
* Description:
* Read the current and voltage register with special scaling
*
****************************************************************************/
static int ina3221_readpower(FAR struct ina3221_dev_s *priv,
FAR struct ina3221_s *buffer)
{
uint16_t reg;
int64_t tmp;
int i;
int ret;
for (i = 0; i < 3; i++)
{
if (priv->config & (INA3221_CONFIG_CH1_EN << i))
{
/* Read the raw bus voltage */
ret = ina3221_read16(priv, (INA3221_REG_CH1_BUS_VOLTAGE << i), &reg);
if (ret < 0)
{
snerr("ERROR: ina3221_read16 failed: %d\n", ret);
return ret;
}
/* Convert register value to bus voltage */
reg >>= 3; /* 3 LSB of reg contains mode */
buffer->ch[i].voltage = ((uint32_t)reg) * 8000LU;
/* Read the raw shunt voltage */
ret = ina3221_read16(priv, (INA3221_REG_CH1_SHUNT_VOLTAGE << i), &reg);
if (ret < 0)
{
snerr("ERROR: ina3221_read16 failed: %d\n", ret);
return ret;
}
/* Convert register value to shunt voltage */
reg >>= 3; /* 3 LSB of reg contains mode */
tmp = ((int64_t)(int16_t)reg) * 40LL; /* 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[i];
buffer->ch[i].current = (int32_t)tmp;
sninfo("Voltage: %u uV, Current: %d uA\n",
buffer->ch[i].voltage, buffer->ch[i].current);
}
}
return OK;
}
/****************************************************************************
* Name: ina3221_open
*
* Description:
* This function is called whenever the INA3221 device is opened.
*
****************************************************************************/
static int ina3221_open(FAR struct file *filep)
{
FAR struct inode *inode = filep->f_inode;
FAR struct ina3221_dev_s *priv = inode->i_private;
return OK;
}
/****************************************************************************
* Name: ina3221_close
*
* Description:
* This routine is called when the INA3221 device is closed.
*
****************************************************************************/
static int ina3221_close(FAR struct file *filep)
{
FAR struct inode *inode = filep->f_inode;
FAR struct ina3221_dev_s *priv = inode->i_private;
return OK;
}
/****************************************************************************
* Name: ina3221_read
****************************************************************************/
static ssize_t ina3221_read(FAR struct file *filep, FAR char *buffer,
size_t buflen)
{
FAR struct inode *inode = filep->f_inode;
FAR struct ina3221_dev_s *priv = inode->i_private;
FAR struct ina3221_s *ptr;
ssize_t nsamples;
int i;
int ret;
/* How many samples were requested to get? */
nsamples = buflen / sizeof(struct ina3221_s);
ptr = (FAR struct ina3221_s *)buffer;
sninfo("buflen: %d nsamples: %d\n", buflen, nsamples);
/* Get the requested number of samples */
for (i = 0; i < nsamples; i++)
{
struct ina3221_s pwr;
/* Read the next struct ina3221_s power value */
ret = ina3221_readpower(priv, &pwr);
if (ret < 0)
{
snerr("ERROR: ina3221_readpower failed: %d\n", ret);
return (ssize_t)ret;
}
/* Save the temperature value in the user buffer */
*ptr++ = pwr;
}
return nsamples * sizeof(struct ina3221_s);
}
/****************************************************************************
* Name: ina3221_write
****************************************************************************/
static ssize_t ina3221_write(FAR struct file *filep, FAR const char *buffer,
size_t buflen)
{
return -ENOSYS;
}
/****************************************************************************
* Name: ina3221_ioctl
****************************************************************************/
static int ina3221_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
{
return -ENOTTY;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: ina3221_reCommits
gister
*
* Description:
* Register the INA3221 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 INA3221
* config - Configuration including I2C address, shunt values, and INA3221
* configuration mask.
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/
int ina3221_register(FAR const char *devpath, FAR struct i2c_master_s *i2c,
FAR const struct ina3221_config_s *config)
{
FAR struct ina3221_dev_s *priv;
int ret = 0;
/* Sanity check */
DEBUGASSERT(i2c != NULL);
/* Initialize the ina3221 device structure */
priv = (FAR struct ina3221_dev_s *)kmm_malloc(sizeof(struct ina3221_dev_s));
if (priv == NULL)
{
snerr("ERROR: Failed to allocate instance\n");
return -ENOMEM;
}
priv->i2c = i2c;
priv->addr = config->addr;
/* Save the config (except opmode) */
priv->config = config->cfgreg & ~(INA3221_CONFIG_RST);
/* Copy the shunt resistor values */
memcpy(priv->shunt_resistor, config->shunt_resistor, 3 * sizeof(int32_t));
/* Apply config, keep chip switched off */
ret = ina3221_write16(priv, INA3221_REG_CONFIG, priv->config);
if (ret < 0)
{
snerr("ERROR: Failed to apply config: %d\n", ret);
goto errout;
}
/* Register the character driver */
ret = register_driver(devpath, &g_ina3221fops, 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;
}

View File

@ -0,0 +1,160 @@
/****************************************************************************
* include/nuttx/sensors/ina3221.h
*
* Copyright (C) 2018 Verge Inc. All rights reserved.
* Author: Anthony Merlino <anthony@vergeaero.com>
*
* 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_INA3221_H
#define __INCLUDE_NUTTX_SENSORS_INA3221_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/sensors/ioctl.h>
#if defined(CONFIG_I2C) && defined(CONFIG_SENSORS_INA3221)
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* INA3221 Register Definitions ***********************************************/
/* INA3221 Config Register bits */
#define INA3221_CONFIG_MODE_POWERDOWN 0x000
#define INA3221_CONFIG_MODE_SHUNT_TRIG 0x001
#define INA3221_CONFIG_MODE_BUS_TRIG 0x010
#define INA3221_CONFIG_MODE_BOTH_TRIG 0x011
#define INA3221_CONFIG_MODE_POWERDOWN_1 0x100
#define INA3221_CONFIG_MODE_SHUNT_CONT 0x101
#define INA3221_CONFIG_MODE_BUS_CONT 0x110
#define INA3221_CONFIG_MODE_BOTH_CONT 0x111
#define INA3221_CONFIG_VSHUNTCT_140_US (0x000 << 3)
#define INA3221_CONFIG_VSHUNTCT_204_US (0x001 << 3)
#define INA3221_CONFIG_VSHUNTCT_332_US (0x010 << 3)
#define INA3221_CONFIG_VSHUNTCT_588_US (0x011 << 3)
#define INA3221_CONFIG_VSHUNTCT_1100_US (0x100 << 3)
#define INA3221_CONFIG_VSHUNTCT_2116_US (0x101 << 3)
#define INA3221_CONFIG_VSHUNTCT_4156_US (0x110 << 3)
#define INA3221_CONFIG_VSHUNTCT_8244_US (0x111 << 3)
#define INA3221_CONFIG_VBUSCT_140_US (0x000 << 6)
#define INA3221_CONFIG_VBUSCT_204_US (0x001 << 6)
#define INA3221_CONFIG_VBUSCT_332_US (0x010 << 6)
#define INA3221_CONFIG_VBUSCT_588_US (0x011 << 6)
#define INA3221_CONFIG_VBUSCT_1100_US (0x100 << 6)
#define INA3221_CONFIG_VBUSCT_2116_US (0x101 << 6)
#define INA3221_CONFIG_VBUSCT_4156_US (0x110 << 6)
#define INA3221_CONFIG_VBUSCT_8244_US (0x111 << 6)
#define INA3221_CONFIG_AVG_NSAMPLES_1 (0x000 << 9)
#define INA3221_CONFIG_AVG_NSAMPLES_4 (0x001 << 9)
#define INA3221_CONFIG_AVG_NSAMPLES_16 (0x010 << 9)
#define INA3221_CONFIG_AVG_NSAMPLES_64 (0x011 << 9)
#define INA3221_CONFIG_AVG_NSAMPLES_128 (0x100 << 9)
#define INA3221_CONFIG_AVG_NSAMPLES_256 (0x101 << 9)
#define INA3221_CONFIG_AVG_NSAMPLES_512 (0x110 << 9)
#define INA3221_CONFIG_AVG_NSAMPLES_1024 (0x111 << 9)
#define INA3221_CONFIG_CH1_EN (1 << 12)
#define INA3221_CONFIG_CH2_EN (1 << 13)
#define INA3221_CONFIG_CH3_EN (1 << 14)
/****************************************************************************
* Public Types
****************************************************************************/
struct i2c_master_s;
struct ina3221_config_s
{
uint8_t addr;
int32_t shunt_resistor[3];
uint16_t cfgreg;
};
struct ina3221_channel_s
{
uint32_t voltage; /* [microvolt] max 4.2 kV - device max 26V */
int32_t current; /* [microampere] max 2.1 kA - sensor is bidirectional */
};
struct ina3221_s
{
struct ina3221_channel_s ch[3];
};
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Name: ina3221_register
*
* Description:
* Register the ina3221 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 INA3221
* addr - The I2C address of the INA3221. The base I2C address of the INA3221
* is 0x80. Bits 0-1 can be controlled to get 4 unique addresses from 0x80
* through 0x83.
* 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 ina3221_register(FAR const char *devpath, FAR struct i2c_master_s *i2c,
FAR const struct ina3221_config_s *config);
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* CONFIG_I2C && CONFIG_SENSORS_INA3221 */
#endif /* __INCLUDE_NUTTX_SENSORS_INA3221_H */