2015-08-06 19:07:33 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/sensors/lm92.c
|
|
|
|
* Character driver for the TI LM92 Temperature Sensor
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011, 2013 Gregory Nutt. All rights reserved.
|
2015-08-10 20:03:18 +02:00
|
|
|
* Copyright (C) 2015 Omni Hoverboards Inc. All rights reserved.
|
|
|
|
* Authors: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
* Paul Alexander Patience <paul-a.patience@polymtl.ca>
|
2015-08-06 19:07:33 +02:00
|
|
|
*
|
|
|
|
* 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 <fixedmath.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2015-08-06 19:07:33 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
2016-01-30 15:00:16 +01:00
|
|
|
#include <nuttx/i2c/i2c_master.h>
|
2015-08-06 19:07:33 +02:00
|
|
|
#include <nuttx/sensors/lm92.h>
|
2017-03-30 15:38:37 +02:00
|
|
|
#include <nuttx/random.h>
|
2015-08-06 19:07:33 +02:00
|
|
|
|
2017-08-24 18:10:00 +02:00
|
|
|
#if defined(CONFIG_I2C) && defined(CONFIG_SENSORS_LM92)
|
2015-08-06 19:07:33 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-02-01 15:57:22 +01:00
|
|
|
#ifndef CONFIG_LM92_I2C_FREQUENCY
|
|
|
|
# define CONFIG_LM92_I2C_FREQUENCY 400000
|
|
|
|
#endif
|
|
|
|
|
2015-08-06 19:07:33 +02:00
|
|
|
/* Centigrade to Fahrenheit conversion: F = 9*C/5 + 32 */
|
|
|
|
|
|
|
|
#define B16_9DIV5 (9 * 65536 / 5)
|
|
|
|
#define B16_32 (32 * 65536)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct lm92_dev_s
|
|
|
|
{
|
2016-01-30 15:36:47 +01:00
|
|
|
FAR struct i2c_master_s *i2c; /* I2C interface */
|
|
|
|
uint8_t addr; /* I2C address */
|
|
|
|
bool fahrenheit; /* true: temperature will be reported in Fahrenheit */
|
2015-08-06 19:07:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
2019-12-07 02:07:34 +01:00
|
|
|
|
2015-08-06 19:07:33 +02:00
|
|
|
/* I2C Helpers */
|
|
|
|
|
2016-02-01 15:57:22 +01:00
|
|
|
static int lm92_i2c_write(FAR struct lm92_dev_s *priv,
|
|
|
|
FAR const uint8_t *buffer, int buflen);
|
|
|
|
static int lm92_i2c_read(FAR struct lm92_dev_s *priv,
|
|
|
|
FAR uint8_t *buffer, int buflen);
|
|
|
|
static int lm92_readb16(FAR struct lm92_dev_s *priv, uint8_t regaddr,
|
|
|
|
FAR b16_t *regvalue);
|
|
|
|
static int lm92_writeb16(FAR struct lm92_dev_s *priv, uint8_t regaddr,
|
|
|
|
b16_t regval);
|
|
|
|
static int lm92_readtemp(FAR struct lm92_dev_s *priv, FAR b16_t *temp);
|
|
|
|
static int lm92_readconf(FAR struct lm92_dev_s *priv, FAR uint8_t *conf);
|
|
|
|
static int lm92_writeconf(FAR struct lm92_dev_s *priv, uint8_t conf);
|
2015-08-06 19:07:33 +02:00
|
|
|
|
|
|
|
/* Character driver methods */
|
|
|
|
|
|
|
|
static int lm92_open(FAR struct file *filep);
|
|
|
|
static int lm92_close(FAR struct file *filep);
|
|
|
|
static ssize_t lm92_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static ssize_t lm92_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen);
|
2021-04-04 11:58:11 +02:00
|
|
|
static int lm92_ioctl(FAR struct file *filep,
|
|
|
|
int cmd, unsigned long arg);
|
2015-08-06 19:07:33 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations g_lm92fops =
|
|
|
|
{
|
|
|
|
lm92_open,
|
|
|
|
lm92_close,
|
|
|
|
lm92_read,
|
|
|
|
lm92_write,
|
2015-08-10 20:59:11 +02:00
|
|
|
NULL,
|
2019-05-22 02:57:54 +02:00
|
|
|
lm92_ioctl,
|
|
|
|
NULL
|
2015-08-06 19:07:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
2016-02-01 15:57:22 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lm92_i2c_write
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write to the I2C device.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int lm92_i2c_write(FAR struct lm92_dev_s *priv,
|
|
|
|
FAR const uint8_t *buffer, int buflen)
|
|
|
|
{
|
2016-02-02 14:13:03 +01:00
|
|
|
struct i2c_msg_s msg;
|
2017-08-04 15:31:36 +02:00
|
|
|
int ret;
|
2016-02-01 15:57:22 +01:00
|
|
|
|
2016-02-02 14:13:03 +01:00
|
|
|
/* Setup for the transfer */
|
2016-02-01 15:57:22 +01:00
|
|
|
|
2016-02-02 14:13:03 +01:00
|
|
|
msg.frequency = CONFIG_LM92_I2C_FREQUENCY,
|
|
|
|
msg.addr = priv->addr;
|
|
|
|
msg.flags = 0;
|
|
|
|
msg.buffer = (FAR uint8_t *)buffer; /* Override const */
|
|
|
|
msg.length = buflen;
|
2016-02-01 15:57:22 +01:00
|
|
|
|
2016-02-02 14:13:03 +01:00
|
|
|
/* Then perform the transfer. */
|
|
|
|
|
2017-08-04 15:31:36 +02:00
|
|
|
ret = I2C_TRANSFER(priv->i2c, &msg, 1);
|
|
|
|
return (ret >= 0) ? OK : ret;
|
2016-02-01 15:57:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lm92_i2c_read
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read from the I2C device.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int lm92_i2c_read(FAR struct lm92_dev_s *priv,
|
|
|
|
FAR uint8_t *buffer, int buflen)
|
|
|
|
{
|
2016-02-02 14:13:03 +01:00
|
|
|
struct i2c_msg_s msg;
|
2017-08-04 15:31:36 +02:00
|
|
|
int ret;
|
2016-02-02 14:13:03 +01:00
|
|
|
|
|
|
|
/* Setup for the transfer */
|
2016-02-01 15:57:22 +01:00
|
|
|
|
2016-02-02 14:13:03 +01:00
|
|
|
msg.frequency = CONFIG_LM92_I2C_FREQUENCY,
|
|
|
|
msg.addr = priv->addr,
|
|
|
|
msg.flags = I2C_M_READ;
|
|
|
|
msg.buffer = buffer;
|
|
|
|
msg.length = buflen;
|
2016-02-01 15:57:22 +01:00
|
|
|
|
2016-02-02 14:13:03 +01:00
|
|
|
/* Then perform the transfer. */
|
2016-02-01 15:57:22 +01:00
|
|
|
|
2017-08-04 15:31:36 +02:00
|
|
|
ret = I2C_TRANSFER(priv->i2c, &msg, 1);
|
|
|
|
return (ret >= 0) ? OK : ret;
|
2016-02-01 15:57:22 +01:00
|
|
|
}
|
|
|
|
|
2015-08-06 19:07:33 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: lm92_readb16
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read a 16-bit register (LM92_TEMP_REG, LM92_THYS_REG, LM92_TCRIT_REG,
|
|
|
|
* LM92_TLOW_REG, LM92_THIGH_REG, or LM92_ID_REG)
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int lm92_readb16(FAR struct lm92_dev_s *priv, uint8_t regaddr,
|
|
|
|
FAR b16_t *regvalue)
|
|
|
|
{
|
|
|
|
uint8_t buffer[2];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Write the register address */
|
|
|
|
|
2016-02-01 15:57:22 +01:00
|
|
|
ret = lm92_i2c_write(priv, ®addr, 1);
|
2015-08-06 19:07:33 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: i2c_write failed: %d\n", ret);
|
2015-08-06 19:07:33 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Restart and read 16 bits from the register (discarding 3) */
|
|
|
|
|
2016-02-01 15:57:22 +01:00
|
|
|
ret = lm92_i2c_read(priv, buffer, 2);
|
2015-08-06 19:07:33 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: i2c_read failed: %d\n", ret);
|
2015-08-06 19:07:33 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-04-04 11:58:11 +02:00
|
|
|
/* Data format is:
|
|
|
|
* TTTTTTTT TTTTTxxx where TTTTTTTTTTTTT is a thirteen-bit,
|
2015-08-06 19:07:33 +02:00
|
|
|
* signed temperature value with LSB = 0.0625 degrees Centigrade.
|
|
|
|
*/
|
|
|
|
|
|
|
|
*regvalue = (b16_t)((uint32_t)(buffer[0] & (1 << 7)) << 24 |
|
|
|
|
(uint32_t)(buffer[0] & ~(1 << 7)) << 17 |
|
|
|
|
(uint32_t)(buffer[1] & ~7) << 9);
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("addr: %02x value: %08x ret: %d\n", regaddr, *regvalue, ret);
|
2015-08-06 19:07:33 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lm92_writeb16
|
|
|
|
*
|
|
|
|
* Description:
|
2021-04-04 11:58:11 +02:00
|
|
|
* Write to a 16-bit register
|
|
|
|
* (LM92_TEMP_REG, LM92_THYS_REG, LM92_TCRIT_REG,
|
2015-08-06 19:07:33 +02:00
|
|
|
* LM92_TLOW_REG, LM92_THIGH_REG, or LM92_ID_REG)
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int lm92_writeb16(FAR struct lm92_dev_s *priv, uint8_t regaddr,
|
|
|
|
b16_t regval)
|
|
|
|
{
|
|
|
|
uint8_t buffer[3];
|
|
|
|
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("addr: %02x value: %08x\n", regaddr, regval);
|
2015-08-06 19:07:33 +02:00
|
|
|
|
|
|
|
/* Set up a 3-byte message to send */
|
|
|
|
|
|
|
|
buffer[0] = regaddr;
|
|
|
|
buffer[1] = (uint8_t)(((uint32_t)regval & (1 << 31)) >> 24 |
|
|
|
|
((uint32_t)regval & ~(1 << 31)) >> 17);
|
|
|
|
buffer[2] = (uint8_t)(((uint32_t)regval & ~(1 << 31)) >> 9);
|
|
|
|
|
|
|
|
/* Write the register address followed by the data (no RESTART) */
|
|
|
|
|
2016-02-01 15:57:22 +01:00
|
|
|
return lm92_i2c_write(priv, buffer, 3);
|
2015-08-06 19:07:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lm92_readtemp
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read the temperature register with special scaling (LM92_TEMP_REG)
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int lm92_readtemp(FAR struct lm92_dev_s *priv, FAR b16_t *temp)
|
|
|
|
{
|
|
|
|
b16_t temp16;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Read the raw temperature data (b16_t) */
|
|
|
|
|
|
|
|
ret = lm92_readb16(priv, LM92_TEMP_REG, &temp16);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: lm92_readb16 failed: %d\n", ret);
|
2015-08-06 19:07:33 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-30 15:38:37 +02:00
|
|
|
add_sensor_randomness(temp16);
|
|
|
|
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("Centigrade: %08x\n", temp16);
|
2015-08-06 19:07:33 +02:00
|
|
|
|
|
|
|
/* Was Fahrenheit requested? */
|
|
|
|
|
|
|
|
if (priv->fahrenheit)
|
|
|
|
{
|
|
|
|
/* Centigrade to Fahrenheit conversion: F = 9*C/5 + 32 */
|
|
|
|
|
|
|
|
temp16 = b16mulb16(temp16, B16_9DIV5) + B16_32;
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("Fahrenheit: %08x\n", temp16);
|
2015-08-06 19:07:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*temp = temp16;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lm92_readconf
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read the 8-bit LM92 configuration register
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int lm92_readconf(FAR struct lm92_dev_s *priv, FAR uint8_t *conf)
|
|
|
|
{
|
|
|
|
uint8_t buffer;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Write the configuration register address */
|
|
|
|
|
|
|
|
buffer = LM92_CONF_REG;
|
2016-02-01 19:14:31 +01:00
|
|
|
|
2016-02-01 15:57:22 +01:00
|
|
|
ret = lm92_i2c_write(priv, &buffer, 1);
|
2015-08-06 19:07:33 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: i2c_write failed: %d\n", ret);
|
2015-08-06 19:07:33 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Restart and read 8 bits from the register */
|
|
|
|
|
2016-02-01 15:57:22 +01:00
|
|
|
ret = lm92_i2c_read(priv, conf, 1);
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("conf: %02x ret: %d\n", *conf, ret);
|
2015-08-06 19:07:33 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lm92_writeconf
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write to a 8-bit LM92 configuration register.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int lm92_writeconf(FAR struct lm92_dev_s *priv, uint8_t conf)
|
|
|
|
{
|
|
|
|
uint8_t buffer[2];
|
|
|
|
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("conf: %02x\n", conf);
|
2015-08-06 19:07:33 +02:00
|
|
|
|
|
|
|
/* Set up a 2-byte message to send */
|
|
|
|
|
|
|
|
buffer[0] = LM92_CONF_REG;
|
|
|
|
buffer[1] = conf;
|
|
|
|
|
|
|
|
/* Write the register address followed by the data (no RESTART) */
|
|
|
|
|
2016-02-01 15:57:22 +01:00
|
|
|
return lm92_i2c_write(priv, buffer, 2);
|
2015-08-06 19:07:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lm92_readid
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read the 16-bit LM92 identification register
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int lm92_readid(FAR struct lm92_dev_s *priv, FAR uint16_t *id)
|
|
|
|
{
|
|
|
|
uint8_t buffer[2];
|
|
|
|
uint8_t regaddr;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Write the identification register address */
|
|
|
|
|
|
|
|
regaddr = LM92_ID_REG;
|
2016-02-01 19:14:31 +01:00
|
|
|
|
2016-02-01 15:57:22 +01:00
|
|
|
ret = lm92_i2c_write(priv, ®addr, 1);
|
2015-08-06 19:07:33 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: i2c_write failed: %d\n", ret);
|
2015-08-06 19:07:33 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Restart and read 16 bits from the register */
|
|
|
|
|
2016-02-01 15:57:22 +01:00
|
|
|
ret = lm92_i2c_read(priv, buffer, 2);
|
2015-08-06 19:07:33 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: i2c_read failed: %d\n", ret);
|
2015-08-06 19:07:33 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
*id = (uint16_t)buffer[0] << 8 | (uint16_t)buffer[1];
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("id: %04x ret: %d\n", *id, ret);
|
2015-08-06 19:07:33 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lm92_open
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function is called whenever the LM92 device is opened.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int lm92_open(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lm92_close
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function is called whenever the LM92 device is closed.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int lm92_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lm92_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t lm92_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct lm92_dev_s *priv = inode->i_private;
|
|
|
|
FAR b16_t *ptr;
|
|
|
|
ssize_t nsamples;
|
|
|
|
int i;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* How many samples were requested to get? */
|
|
|
|
|
|
|
|
nsamples = buflen / sizeof(b16_t);
|
|
|
|
ptr = (FAR b16_t *)buffer;
|
|
|
|
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("buflen: %d nsamples: %d\n", buflen, nsamples);
|
2015-08-06 19:07:33 +02:00
|
|
|
|
|
|
|
/* Get the requested number of samples */
|
|
|
|
|
|
|
|
for (i = 0; i < nsamples; i++)
|
|
|
|
{
|
|
|
|
b16_t temp = 0;
|
|
|
|
|
|
|
|
/* Read the next b16_t temperature value */
|
|
|
|
|
|
|
|
ret = lm92_readtemp(priv, &temp);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: lm92_readtemp failed: %d\n", ret);
|
2015-08-06 19:07:33 +02:00
|
|
|
return (ssize_t)ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the temperature value in the user buffer */
|
|
|
|
|
|
|
|
*ptr++ = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsamples * sizeof(b16_t);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lm92_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t lm92_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lm92_ioctl
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int lm92_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct lm92_dev_s *priv = inode->i_private;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
/* Read from the configuration register. Arg: uint8_t* pointer */
|
|
|
|
|
|
|
|
case SNIOC_READCONF:
|
|
|
|
{
|
|
|
|
FAR uint8_t *ptr = (FAR uint8_t *)((uintptr_t)arg);
|
2015-08-10 20:59:11 +02:00
|
|
|
DEBUGASSERT(ptr != NULL);
|
2015-08-06 19:07:33 +02:00
|
|
|
ret = lm92_readconf(priv, ptr);
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("conf: %02x ret: %d\n", *ptr, ret);
|
2015-08-06 19:07:33 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Write to the configuration register. Arg: uint8_t value */
|
|
|
|
|
|
|
|
case SNIOC_WRITECONF:
|
|
|
|
ret = lm92_writeconf(priv, (uint8_t)arg);
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("conf: %02x ret: %d\n", *(uint8_t *)arg, ret);
|
2015-08-06 19:07:33 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Shutdown the LM92. Arg: None */
|
|
|
|
|
|
|
|
case SNIOC_SHUTDOWN:
|
|
|
|
{
|
|
|
|
uint8_t conf;
|
|
|
|
ret = lm92_readconf(priv, &conf);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
ret = lm92_writeconf(priv, conf | LM92_CONF_SHUTDOWN);
|
|
|
|
}
|
|
|
|
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("conf: %02x ret: %d\n", conf | LM92_CONF_SHUTDOWN, ret);
|
2015-08-06 19:07:33 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Powerup the LM92. Arg: None */
|
|
|
|
|
|
|
|
case SNIOC_POWERUP:
|
|
|
|
{
|
|
|
|
uint8_t conf;
|
|
|
|
ret = lm92_readconf(priv, &conf);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
ret = lm92_writeconf(priv, conf & ~LM92_CONF_SHUTDOWN);
|
|
|
|
}
|
|
|
|
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("conf: %02x ret: %d\n", conf & ~LM92_CONF_SHUTDOWN, ret);
|
2015-08-06 19:07:33 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Report samples in Fahrenheit. Arg: None */
|
|
|
|
|
|
|
|
case SNIOC_FAHRENHEIT:
|
|
|
|
priv->fahrenheit = true;
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("Fahrenheit\n");
|
2015-08-06 19:07:33 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Report samples in Centigrade. Arg: None */
|
|
|
|
|
|
|
|
case SNIOC_CENTIGRADE:
|
|
|
|
priv->fahrenheit = false;
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("Centigrade\n");
|
2015-08-06 19:07:33 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Read THYS temperature register. Arg: b16_t* pointer */
|
|
|
|
|
|
|
|
case SNIOC_READTHYS:
|
|
|
|
{
|
|
|
|
FAR b16_t *ptr = (FAR b16_t *)((uintptr_t)arg);
|
2015-08-10 20:59:11 +02:00
|
|
|
DEBUGASSERT(ptr != NULL);
|
2015-08-06 19:07:33 +02:00
|
|
|
ret = lm92_readb16(priv, LM92_THYS_REG, ptr);
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("THYS: %08x ret: %d\n", *ptr, ret);
|
2015-08-06 19:07:33 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Write THYS temperature register. Arg: b16_t value */
|
|
|
|
|
|
|
|
case SNIOC_WRITETHYS:
|
|
|
|
ret = lm92_writeb16(priv, LM92_THYS_REG, (b16_t)arg);
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("THYS: %08x ret: %d\n", (b16_t)arg, ret);
|
2015-08-06 19:07:33 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Read TCRIT temperature register. Arg: b16_t* pointer */
|
|
|
|
|
|
|
|
case SNIOC_READTCRIT:
|
|
|
|
{
|
|
|
|
FAR b16_t *ptr = (FAR b16_t *)((uintptr_t)arg);
|
2015-08-10 20:59:11 +02:00
|
|
|
DEBUGASSERT(ptr != NULL);
|
2015-08-06 19:07:33 +02:00
|
|
|
ret = lm92_readb16(priv, LM92_TCRIT_REG, ptr);
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("TCRIT: %08x ret: %d\n", *ptr, ret);
|
2015-08-06 19:07:33 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Write TCRIT temperature register. Arg: b16_t value */
|
|
|
|
|
|
|
|
case SNIOC_WRITETCRIT:
|
|
|
|
ret = lm92_writeb16(priv, LM92_TCRIT_REG, (b16_t)arg);
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("TCRIT: %08x ret: %d\n", (b16_t)arg, ret);
|
2015-08-06 19:07:33 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Read TLOW temperature register. Arg: b16_t* pointer */
|
|
|
|
|
|
|
|
case SNIOC_READTLOW:
|
|
|
|
{
|
|
|
|
FAR b16_t *ptr = (FAR b16_t *)((uintptr_t)arg);
|
2015-08-10 20:59:11 +02:00
|
|
|
DEBUGASSERT(ptr != NULL);
|
2015-08-06 19:07:33 +02:00
|
|
|
ret = lm92_readb16(priv, LM92_TLOW_REG, ptr);
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("TLOW: %08x ret: %d\n", *ptr, ret);
|
2015-08-06 19:07:33 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Write TLOW temperature register. Arg: b16_t value */
|
|
|
|
|
|
|
|
case SNIOC_WRITETLOW:
|
|
|
|
ret = lm92_writeb16(priv, LM92_TLOW_REG, (b16_t)arg);
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("TLOW: %08x ret: %d\n", (b16_t)arg, ret);
|
2015-08-06 19:07:33 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Read THIGH temperature register. Arg: b16_t* pointer */
|
|
|
|
|
|
|
|
case SNIOC_READTHIGH:
|
|
|
|
{
|
|
|
|
FAR b16_t *ptr = (FAR b16_t *)((uintptr_t)arg);
|
2015-08-10 20:59:11 +02:00
|
|
|
DEBUGASSERT(ptr != NULL);
|
2015-08-06 19:07:33 +02:00
|
|
|
ret = lm92_readb16(priv, LM92_THIGH_REG, ptr);
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("THIGH: %08x ret: %d\n", *ptr, ret);
|
2015-08-06 19:07:33 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Write THIGH temperature register. Arg: b16_t value */
|
|
|
|
|
|
|
|
case SNIOC_WRITETHIGH:
|
|
|
|
ret = lm92_writeb16(priv, LM92_THIGH_REG, (b16_t)arg);
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("THIGH: %08x ret: %d\n", (b16_t)arg, ret);
|
2015-08-06 19:07:33 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Read from the identification register. Arg: uint16_t* pointer */
|
|
|
|
|
|
|
|
case SNIOC_READID:
|
|
|
|
{
|
|
|
|
FAR uint16_t *ptr = (FAR uint16_t *)((uintptr_t)arg);
|
2015-08-10 20:59:11 +02:00
|
|
|
DEBUGASSERT(ptr != NULL);
|
2015-08-06 19:07:33 +02:00
|
|
|
ret = lm92_readid(priv, ptr);
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("id: %04x ret: %d\n", *ptr, ret);
|
2015-08-06 19:07:33 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: Unrecognized cmd: %d\n", cmd);
|
2015-08-06 19:07:33 +02:00
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lm92_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register the LM92 character device as 'devpath'
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* devpath - The full path to the driver to register. E.g., "/dev/temp0".
|
|
|
|
* i2c - An instance of the I2C interface to use to communicate
|
|
|
|
* with the LM92.
|
|
|
|
* addr - The I2C address of the LM92. The base I2C address of the LM92
|
|
|
|
* is 0x48. Bits 0-2 can be controlled to get 4 unique addresses
|
|
|
|
* from 0x48 through 0x4b.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; a negated errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-01-30 15:36:47 +01:00
|
|
|
int lm92_register(FAR const char *devpath, FAR struct i2c_master_s *i2c,
|
2015-08-06 19:07:33 +02:00
|
|
|
uint8_t addr)
|
|
|
|
{
|
|
|
|
FAR struct lm92_dev_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
2015-08-10 20:59:11 +02:00
|
|
|
/* Sanity check */
|
|
|
|
|
|
|
|
DEBUGASSERT(i2c != NULL);
|
|
|
|
DEBUGASSERT(addr == CONFIG_LM92_ADDR0 || addr == CONFIG_LM92_ADDR1 ||
|
|
|
|
addr == CONFIG_LM92_ADDR2 || addr == CONFIG_LM92_ADDR3);
|
|
|
|
|
2015-08-06 19:07:33 +02:00
|
|
|
/* Initialize the LM92 device structure */
|
|
|
|
|
|
|
|
priv = (FAR struct lm92_dev_s *)kmm_malloc(sizeof(struct lm92_dev_s));
|
2015-08-10 20:59:11 +02:00
|
|
|
if (priv == NULL)
|
2015-08-06 19:07:33 +02:00
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: Failed to allocate instance\n");
|
2015-08-06 19:07:33 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->i2c = i2c;
|
|
|
|
priv->addr = addr;
|
|
|
|
priv->fahrenheit = false;
|
|
|
|
|
|
|
|
/* Register the character driver */
|
|
|
|
|
|
|
|
ret = register_driver(devpath, &g_lm92fops, 0666, priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: Failed to register driver: %d\n", ret);
|
2015-08-06 19:07:33 +02:00
|
|
|
kmm_free(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2017-08-24 18:10:00 +02:00
|
|
|
#endif /* CONFIG_I2C && CONFIG_SENSORS_LM92 */
|