diff --git a/drivers/sensors/Kconfig b/drivers/sensors/Kconfig index 43c82293f1..30613c820c 100644 --- a/drivers/sensors/Kconfig +++ b/drivers/sensors/Kconfig @@ -167,6 +167,13 @@ config DEBUG_LPS25H endif # LPS25H +config LTC4151 + bool "LTC4151 current and voltage monitor" + default n + select I2C + ---help--- + Enable driver support for the LinearTechnology LTC4151 current and voltage monitor. + config MB7040 bool "MaxBotix MB7040 Sonar support" default n diff --git a/drivers/sensors/Make.defs b/drivers/sensors/Make.defs index 74464abb7d..1f6affc225 100644 --- a/drivers/sensors/Make.defs +++ b/drivers/sensors/Make.defs @@ -113,6 +113,10 @@ ifeq ($(CONFIG_MS58XX),y) CSRCS += ms58xx.c endif +ifeq ($(CONFIG_LTC4151),y) + CSRCS += ltc4151.c +endif + endif # CONFIG_I2C # These drivers depend on SPI support diff --git a/drivers/sensors/ltc4151.c b/drivers/sensors/ltc4151.c new file mode 100644 index 0000000000..a3a8e31621 --- /dev/null +++ b/drivers/sensors/ltc4151.c @@ -0,0 +1,332 @@ +/**************************************************************************** + * drivers/sensors/ltc4151.c + * Character driver for the LTC 4151 Power Sensor + * + * Copyright (C) 2017 Giorgio Groß. All rights reserved. + * Author: Giorgio Groß + * + * 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 + +#include +#include +#include +#include + +#include +#include +#include +#include + +#if defined(CONFIG_I2C) && defined(CONFIG_LTC4151) + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifndef CONFIG_LTC4151_I2C_FREQUENCY +# define CONFIG_LTC4151_I2C_FREQUENCY 400000 +#endif + +/**************************************************************************** + * Private + ****************************************************************************/ + +struct ltc4151_dev_s +{ + FAR struct i2c_master_s *i2c; /* I2C interface */ + uint8_t addr; /* I2C address */ + float shunt_resistor_value; /* [ohm] */ +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ +/* I2C Helpers */ + +static int ltc4151_read16(FAR struct ltc4151_dev_s *priv, uint8_t regaddr, + FAR uint16_t *regvalue); +static int ltc4151_readpower(FAR struct ltc4151_dev_s *priv, FAR ltc4151_t *buffer); + +/* Character driver methods */ + +static int ltc4151_open(FAR struct file *filep); +static int ltc4151_close(FAR struct file *filep); +static ssize_t ltc4151_read(FAR struct file *filep, FAR char *buffer, size_t buflen); +static ssize_t ltc4151_write(FAR struct file *filep, FAR const char *buffer, + size_t buflen); +static int ltc4151_ioctl(FAR struct file *filep,int cmd,unsigned long arg); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct file_operations g_ltc4151fops = +{ + ltc4151_open, + ltc4151_close, + ltc4151_read, + ltc4151_write, + NULL, + ltc4151_ioctl +#ifndef CONFIG_DISABLE_POLL + , NULL +#endif +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static int ltc4151_read_reg(FAR struct ltc4151_dev_s *priv, uint8_t start_register_address, uint8_t* register_value, uint8_t data_length) { +#define I2C_NOSTARTSTOP_MSGS 2 +#define I2C_NOSTARTSTOP_ADDRESS_MSG_INDEX 0 +#define I2C_NOSTARTSTOP_DATA_MSG_INDEX 1 + struct i2c_msg_s msg[I2C_NOSTARTSTOP_MSGS]; + + msg[I2C_NOSTARTSTOP_ADDRESS_MSG_INDEX].frequency = CONFIG_LTC4151_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 = I2C_M_READ; + msg[I2C_NOSTARTSTOP_DATA_MSG_INDEX].buffer = register_value; + msg[I2C_NOSTARTSTOP_DATA_MSG_INDEX].length = data_length; + + int 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 ltc4151_read16(FAR struct ltc4151_dev_s *priv, uint8_t regaddr, FAR uint16_t* regvalue) +{ + int ret = ltc4151_read_reg(priv, regaddr, (uint8_t*)regvalue, sizeof(*regvalue)); + *regvalue = ((*regvalue & LTC4151_VALUE_MSB_MASK) << 4) | ((*regvalue & LTC4151_VALUE_LSB_MASK) >> 8); // bytes are 8bit_msb.4bit_0.4bit_lsb = 16bit + return ret; +} + +/**************************************************************************** + * Name: ltc4151_readpower + * + * Description: + * Read the current and voltage register with special scaling + * + ****************************************************************************/ + +static int ltc4151_readpower(FAR struct ltc4151_dev_s *priv, FAR ltc4151_t *buffer) +{ + uint16_t current_reg, volt_reg; + int ret; + + /* Read the raw temperature data (b16_t) */ + + ret = ltc4151_read16(priv, LTC4151_CURR_REG, ¤t_reg); + if (ret < 0) + { + snerr("ERROR: ltc4151_read16 failed: %d\n", ret); + return ret; + } + ret = ltc4151_read16(priv, LTC4151_VOLT_REG, &volt_reg); + if (ret < 0) + { + snerr("ERROR: ltc4151_read16 failed: %d\n", ret); + return ret; + } + + // current is passed as delta voltage, to get the current divide it by the used resistors resistance + float float_current = (((float)current_reg) * 20.0 / (1000.0 * priv->shunt_resistor_value)); // register in 20 micro volt, shunt in ohm -> result in milliampere + buffer->current = ftob16(float_current); + sninfo("current_reg=0x%04x float_current=%d\n", current_reg, (int)float_current); + + float float_voltage = ((float)volt_reg) * 25.0 / 1000.0; // register in 25 micro volt -> result in volt + buffer->voltage = ftob16(float_voltage); + sninfo("volt_reg=0x%04x float_voltage=%d\n", volt_reg, (int)float_voltage); + + return OK; +} + + +/**************************************************************************** + * Name: ltc4151_open + * + * Description: + * This function is called whenever the LTC4151 device is opened. + * + ****************************************************************************/ + +static int ltc4151_open(FAR struct file *filep) +{ + return OK; +} + +/**************************************************************************** + * Name: ltc4151_close + * + * Description: + * This routine is called when the LTC4151 device is closed. + * + ****************************************************************************/ + +static int ltc4151_close(FAR struct file *filep) +{ + return OK; +} + +/**************************************************************************** + * Name: ltc4151_read + ****************************************************************************/ + +static ssize_t ltc4151_read(FAR struct file *filep, FAR char *buffer, size_t buflen) +{ + FAR struct inode *inode = filep->f_inode; + FAR struct ltc4151_dev_s *priv = inode->i_private; + FAR struct ltc4151_t *ptr; + ssize_t nsamples; + int i; + int ret; + + /* How many samples were requested to get? */ + + nsamples = buflen / sizeof(struct ltc4151_t); + ptr = (FAR struct ltc4151_t *)buffer; + + sninfo("buflen: %d nsamples: %d\n", buflen, nsamples); + + /* Get the requested number of samples */ + + for (i = 0; i < nsamples; i++) + { + struct ltc4151_t pwr; + + /* Read the next ltc4151_t power value */ + + ret = ltc4151_readpower(priv, &pwr); + if (ret < 0) + { + snerr("ERROR: ltc4151_readtemp failed: %d\n", ret); + return (ssize_t)ret; + } + + /* Save the temperature value in the user buffer */ + + *ptr++ = pwr; + } + + return nsamples * sizeof(struct ltc4151_t); +} + +/**************************************************************************** + * Name: ltc4151_write + ****************************************************************************/ + +static ssize_t ltc4151_write(FAR struct file *filep, FAR const char *buffer, + size_t buflen) +{ + return -ENOSYS; +} + +/**************************************************************************** + * Name: ltc4151_ioctl + ****************************************************************************/ + +static int ltc4151_ioctl(FAR struct file *filep, int cmd, unsigned long arg) +{ + return -ENOTTY; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: ltc4151_register + * + * Description: + * Register the LTC4151 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 LTC4151 + * addr - The I2C address of the LTC4151. The base I2C address of the LTC4151 + * is 0x6f. Bits 0-3 can be controlled to get 10 unique addresses from 0x66 + * through 0x6f. + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ****************************************************************************/ + +int ltc4151_register(FAR const char *devpath, FAR struct i2c_master_s *i2c, uint8_t addr, float shunt_resistor_value) +{ + FAR struct ltc4151_dev_s *priv; + int ret; + + /* Sanity check */ + + DEBUGASSERT(i2c != NULL); + DEBUGASSERT(addr == CONFIG_LTC4151_ADDR0 || addr == CONFIG_LTC4151_ADDR1 || + addr == CONFIG_LTC4151_ADDR2 || addr == CONFIG_LTC4151_ADDR3 || + addr == CONFIG_LTC4151_ADDR4 || addr == CONFIG_LTC4151_ADDR5 || + addr == CONFIG_LTC4151_ADDR6 || addr == CONFIG_LTC4151_ADDR7 || + addr == CONFIG_LTC4151_ADDR8 || addr == CONFIG_LTC4151_ADDR9); + + /* Initialize the ltc4151 device structure */ + + priv = (FAR struct ltc4151_dev_s *)kmm_malloc(sizeof(struct ltc4151_dev_s)); + if (priv == NULL) + { + snerr("ERROR: Failed to allocate instance\n"); + return -ENOMEM; + } + + priv->i2c = i2c; + priv->addr = addr; + priv->shunt_resistor_value = shunt_resistor_value; + + /* Register the character driver */ + + ret = register_driver(devpath, &g_ltc4151fops, 0666, priv); + if (ret < 0) + { + snerr("ERROR: Failed to register driver: %d\n", ret); + kmm_free(priv); + } + + sninfo("ltc4151 (addr=0x%02x) registered at %s\n", priv->addr, devpath); + + return ret; +} +#endif /* CONFIG_I2C && CONFIG_LTC4151 */ diff --git a/include/nuttx/sensors/ltc4151.h b/include/nuttx/sensors/ltc4151.h new file mode 100644 index 0000000000..045346e54e --- /dev/null +++ b/include/nuttx/sensors/ltc4151.h @@ -0,0 +1,129 @@ +/**************************************************************************** + * include/nuttx/sensors/ltc4151.h + * + * Copyright (C) 2017 Giorgio Groß. All rights reserved. + * Author: Giorgio Groß + * + * 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_LTC4151_H +#define __INCLUDE_NUTTX_SENSORS_LTC4151_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include + +#if defined(CONFIG_I2C) && defined(CONFIG_LTC4151) + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define CONFIG_LTC4151_BASEADDR 0x66 +#define CONFIG_LTC4151_ADDR0 (CONFIG_LTC4151_BASEADDR + 0) +#define CONFIG_LTC4151_ADDR1 (CONFIG_LTC4151_BASEADDR + 1) +#define CONFIG_LTC4151_ADDR2 (CONFIG_LTC4151_BASEADDR + 2) +#define CONFIG_LTC4151_ADDR3 (CONFIG_LTC4151_BASEADDR + 3) +#define CONFIG_LTC4151_ADDR4 (CONFIG_LTC4151_BASEADDR + 4) +#define CONFIG_LTC4151_ADDR5 (CONFIG_LTC4151_BASEADDR + 5) +#define CONFIG_LTC4151_ADDR6 (CONFIG_LTC4151_BASEADDR + 6) +#define CONFIG_LTC4151_ADDR7 (CONFIG_LTC4151_BASEADDR + 7) +#define CONFIG_LTC4151_ADDR8 (CONFIG_LTC4151_BASEADDR + 8) +#define CONFIG_LTC4151_ADDR9 (CONFIG_LTC4151_BASEADDR + 9) + +/* LTC4151 Register Definitions ***********************************************/ +/* LTC4151 Registers addresses */ + +#define LTC4151_CURR_REG 0x00 /* Current Register start address */ +#define LTC4151_VOLT_REG 0x02 /* Voltage Register start address*/ + +#define LTC4151_VALUE_MSB_MASK 0x00ff +#define LTC4151_VALUE_LSB_MASK 0x0f00 + +/* NOTE: When meassurement values are read, they are return as b16_t, fixed + * precision integer values (see include/fixedmath.h). + */ + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +struct i2c_master_s; + +typedef struct ltc4151_t { + b16_t current; // [milliampere] + b16_t voltage; // [volt] +} ltc4151_t; + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Name: ltc4151_register + * + * Description: + * Register the ltc4151 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 LTC4151 + * addr - The I2C address of the LTC4151. The base I2C address of the LTC4151 + * is 0x18. Bits 0-3 can be controlled to get 8 unique addresses from 0x18 + * through 0x1f. + * shunt_resistor_value - resistor value in ohm + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ****************************************************************************/ + +int ltc4151_register(FAR const char *devpath, FAR struct i2c_master_s *i2c, + uint8_t addr, float shunt_resistor_value); + +#undef EXTERN +#ifdef __cplusplus +} +#endif + +#endif /* CONFIG_I2C && CONFIG_LTC4151 */ +#endif /* __INCLUDE_NUTTX_SENSORS_LTC4151_H */