2011-09-10 18:20:09 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/sensors/lm75.c
|
|
|
|
*
|
2021-04-02 12:39:20 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2011-09-10 18:20:09 +02:00
|
|
|
*
|
2021-04-02 12:39:20 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-09-10 18:20:09 +02:00
|
|
|
*
|
2021-04-02 12:39:20 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2011-09-10 18:20:09 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2020-11-30 07:45:22 +01:00
|
|
|
#include <inttypes.h>
|
2011-09-10 18:20:09 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fixedmath.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2011-09-10 18:20:09 +02:00
|
|
|
#include <errno.h>
|
2011-09-11 16:55:31 +02:00
|
|
|
#include <debug.h>
|
2011-09-10 18:20:09 +02:00
|
|
|
|
2013-03-09 22:12:20 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2016-01-30 15:00:16 +01:00
|
|
|
#include <nuttx/i2c/i2c_master.h>
|
2011-09-10 18:20:09 +02:00
|
|
|
#include <nuttx/sensors/lm75.h>
|
2017-03-30 15:38:37 +02:00
|
|
|
#include <nuttx/random.h>
|
2011-09-10 18:20:09 +02:00
|
|
|
|
2017-08-24 18:10:00 +02:00
|
|
|
#if defined(CONFIG_I2C) && defined(CONFIG_LM75_I2C)
|
2011-09-10 18:20:09 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-02-01 15:57:22 +01:00
|
|
|
#ifndef CONFIG_LM75_I2C_FREQUENCY
|
|
|
|
# define CONFIG_LM75_I2C_FREQUENCY 100000
|
|
|
|
#endif
|
|
|
|
|
2011-09-10 18:20:09 +02:00
|
|
|
/* Centigrade to Fahrenheit conversion: F = 9*C/5 + 32 */
|
|
|
|
|
|
|
|
#define B16_9DIV5 (9 * 65536 / 5)
|
2011-09-11 16:55:31 +02:00
|
|
|
#define B16_32 (32 * 65536)
|
|
|
|
|
2011-09-10 18:20:09 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct lm75_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 */
|
2011-09-10 18:20:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
2019-12-07 02:07:34 +01:00
|
|
|
|
2011-09-10 18:20:09 +02:00
|
|
|
/* I2C Helpers */
|
|
|
|
|
2016-02-01 15:57:22 +01:00
|
|
|
static int lm75_i2c_write(FAR struct lm75_dev_s *priv,
|
|
|
|
FAR const uint8_t *buffer, int buflen);
|
|
|
|
static int lm75_i2c_read(FAR struct lm75_dev_s *priv,
|
|
|
|
FAR uint8_t *buffer, int buflen);
|
|
|
|
static int lm75_readb16(FAR struct lm75_dev_s *priv, uint8_t regaddr,
|
|
|
|
FAR b16_t *regvalue);
|
|
|
|
static int lm75_writeb16(FAR struct lm75_dev_s *priv, uint8_t regaddr,
|
|
|
|
b16_t regval);
|
|
|
|
static int lm75_readtemp(FAR struct lm75_dev_s *priv, FAR b16_t *temp);
|
|
|
|
static int lm75_readconf(FAR struct lm75_dev_s *priv, FAR uint8_t *conf);
|
|
|
|
static int lm75_writeconf(FAR struct lm75_dev_s *priv, uint8_t conf);
|
2011-09-10 18:20:09 +02:00
|
|
|
|
|
|
|
/* Character driver methods */
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
static ssize_t lm75_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static ssize_t lm75_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen);
|
2019-12-07 02:07:34 +01:00
|
|
|
static int lm75_ioctl(FAR struct file *filep, int cmd,
|
|
|
|
unsigned long arg);
|
2011-09-10 18:20:09 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-05-09 00:10:29 +02:00
|
|
|
static const struct file_operations g_lm75fops =
|
2011-09-10 18:20:09 +02:00
|
|
|
{
|
2022-04-12 12:16:13 +02:00
|
|
|
NULL, /* open */
|
|
|
|
NULL, /* close */
|
2022-01-10 15:51:17 +01:00
|
|
|
lm75_read, /* read */
|
|
|
|
lm75_write, /* write */
|
|
|
|
NULL, /* seek */
|
|
|
|
lm75_ioctl, /* ioctl */
|
2011-09-10 18:20:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
2016-02-01 15:57:22 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lm75_i2c_write
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write to the I2C device.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int lm75_i2c_write(FAR struct lm75_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_LM75_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: lm75_i2c_read
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read from the I2C device.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int lm75_i2c_read(FAR struct lm75_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_LM75_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
|
|
|
}
|
|
|
|
|
2011-09-10 18:20:09 +02:00
|
|
|
/****************************************************************************
|
2011-09-11 16:55:31 +02:00
|
|
|
* Name: lm75_readb16
|
2011-09-10 18:20:09 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read a 16-bit register (LM75_TEMP_REG, LM75_THYS_REG, or LM75_TOS_REG)
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-09-11 16:55:31 +02:00
|
|
|
static int lm75_readb16(FAR struct lm75_dev_s *priv, uint8_t regaddr,
|
|
|
|
FAR b16_t *regvalue)
|
2011-09-10 18:20:09 +02:00
|
|
|
{
|
|
|
|
uint8_t buffer[2];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Write the register address */
|
|
|
|
|
2016-02-01 15:57:22 +01:00
|
|
|
ret = lm75_i2c_write(priv, ®addr, 1);
|
2011-09-10 18:20:09 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: i2c_write failed: %d\n", ret);
|
2011-09-10 18:20:09 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Restart and read 16-bits from the register (discarding 7) */
|
|
|
|
|
2016-02-01 15:57:22 +01:00
|
|
|
ret = lm75_i2c_read(priv, buffer, 2);
|
2011-09-10 18:20:09 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: i2c_read failed: %d\n", ret);
|
2011-09-10 18:20:09 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-09-11 16:55:31 +02:00
|
|
|
/* Data format is: TTTTTTTT Txxxxxxx where TTTTTTTTT is a nine-bit,
|
|
|
|
* signed temperature value with LSB = 0.5 degrees centigrade. So the
|
|
|
|
* raw data is b8_t
|
2011-09-10 18:20:09 +02:00
|
|
|
*/
|
|
|
|
|
2011-09-11 16:55:31 +02:00
|
|
|
*regvalue = b8tob16((b8_t)buffer[0] << 8 | (b8_t)buffer[1]);
|
2020-11-30 07:45:22 +01:00
|
|
|
sninfo("addr: %02x value: %08" PRIx32 " ret: %d\n",
|
|
|
|
regaddr, *regvalue, ret);
|
2011-09-10 18:20:09 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2011-09-11 16:55:31 +02:00
|
|
|
* Name: lm75_writeb16
|
2011-09-10 18:20:09 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2020-11-30 07:40:55 +01:00
|
|
|
* Write to a 16-bit register (LM75_TEMP_REG, LM75_THYS_REG, or
|
|
|
|
* LM75_TOS_REG)
|
2011-09-10 18:20:09 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
2011-09-11 16:55:31 +02:00
|
|
|
|
|
|
|
static int lm75_writeb16(FAR struct lm75_dev_s *priv, uint8_t regaddr,
|
|
|
|
b16_t regval)
|
2011-09-10 18:20:09 +02:00
|
|
|
{
|
|
|
|
uint8_t buffer[3];
|
2011-09-11 16:55:31 +02:00
|
|
|
b8_t regb8;
|
|
|
|
|
2020-11-30 07:45:22 +01:00
|
|
|
sninfo("addr: %02x value: %08" PRIx32 "\n", regaddr, regval);
|
2011-09-10 18:20:09 +02:00
|
|
|
|
|
|
|
/* Set up a 3 byte message to send */
|
|
|
|
|
|
|
|
buffer[0] = regaddr;
|
2011-09-11 16:55:31 +02:00
|
|
|
|
|
|
|
regb8 = b16tob8(regval);
|
|
|
|
buffer[1] = (uint8_t)(regb8 >> 8);
|
|
|
|
buffer[2] = (uint8_t)regb8;
|
2011-09-10 18:20:09 +02:00
|
|
|
|
|
|
|
/* Write the register address followed by the data (no RESTART) */
|
|
|
|
|
2016-02-01 15:57:22 +01:00
|
|
|
return lm75_i2c_write(priv, buffer, 3);
|
2011-09-10 18:20:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lm75_readtemp
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read the temperature register with special scaling (LM75_TEMP_REG)
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-09-11 16:55:31 +02:00
|
|
|
static int lm75_readtemp(FAR struct lm75_dev_s *priv, FAR b16_t *temp)
|
2011-09-10 18:20:09 +02:00
|
|
|
{
|
|
|
|
b16_t temp16;
|
|
|
|
int ret;
|
|
|
|
|
2011-09-11 16:55:31 +02:00
|
|
|
/* Read the raw temperature data (b16_t) */
|
|
|
|
|
|
|
|
ret = lm75_readb16(priv, LM75_TEMP_REG, &temp16);
|
2011-09-10 18:20:09 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: lm75_readb16 failed: %d\n", ret);
|
2011-09-10 18:20:09 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2014-12-16 16:54:32 +01:00
|
|
|
|
2017-03-30 15:38:37 +02:00
|
|
|
add_sensor_randomness(temp16);
|
|
|
|
|
2020-11-30 07:45:22 +01:00
|
|
|
sninfo("Centigrade: %08" PRIx32 "\n", temp16);
|
2011-09-10 18:20:09 +02:00
|
|
|
|
|
|
|
/* Was fahrenheit requested? */
|
|
|
|
|
|
|
|
if (priv->fahrenheit)
|
|
|
|
{
|
|
|
|
/* Centigrade to Fahrenheit conversion: F = 9*C/5 + 32 */
|
|
|
|
|
|
|
|
temp16 = b16mulb16(temp16, B16_9DIV5) + B16_32;
|
2020-11-30 07:45:22 +01:00
|
|
|
sninfo("Fahrenheit: %08" PRIx32 "\n", temp16);
|
2011-09-10 18:20:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*temp = temp16;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lm75_readconf
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read the 8-bit LM75 configuration register
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-09-11 16:55:31 +02:00
|
|
|
static int lm75_readconf(FAR struct lm75_dev_s *priv, FAR uint8_t *conf)
|
2011-09-10 18:20:09 +02:00
|
|
|
{
|
|
|
|
uint8_t buffer;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Write the configuration register address */
|
|
|
|
|
|
|
|
buffer = LM75_CONF_REG;
|
2016-02-01 19:14:31 +01:00
|
|
|
|
2016-02-01 15:57:22 +01:00
|
|
|
ret = lm75_i2c_write(priv, &buffer, 1);
|
2011-09-10 18:20:09 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: i2c_write failed: %d\n", ret);
|
2011-09-10 18:20:09 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Restart and read 8-bits from the register */
|
|
|
|
|
2016-02-01 15:57:22 +01:00
|
|
|
ret = lm75_i2c_read(priv, conf, 1);
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("conf: %02x ret: %d\n", *conf, ret);
|
2011-09-10 18:20:09 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lm75_writeconf
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write to a 8-bit LM75 configuration register.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int lm75_writeconf(FAR struct lm75_dev_s *priv, uint8_t conf)
|
|
|
|
{
|
|
|
|
uint8_t buffer[2];
|
|
|
|
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("conf: %02x\n", conf);
|
2011-09-11 16:55:31 +02:00
|
|
|
|
2011-09-10 18:20:09 +02:00
|
|
|
/* Set up a 2 byte message to send */
|
|
|
|
|
|
|
|
buffer[0] = LM75_CONF_REG;
|
|
|
|
buffer[1] = conf;
|
|
|
|
|
|
|
|
/* Write the register address followed by the data (no RESTART) */
|
|
|
|
|
2016-02-01 15:57:22 +01:00
|
|
|
return lm75_i2c_write(priv, buffer, 2);
|
2011-09-10 18:20:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lm75_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-11-30 07:40:55 +01:00
|
|
|
static ssize_t lm75_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen)
|
2011-09-10 18:20:09 +02:00
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct lm75_dev_s *priv = inode->i_private;
|
|
|
|
FAR b16_t *ptr;
|
|
|
|
ssize_t nsamples;
|
2011-09-11 16:55:31 +02:00
|
|
|
int i;
|
2011-09-10 18:20:09 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* How many samples were requested to get? */
|
|
|
|
|
2011-09-11 16:55:31 +02:00
|
|
|
nsamples = buflen / sizeof(b16_t);
|
2011-09-10 18:20:09 +02:00
|
|
|
ptr = (FAR b16_t *)buffer;
|
|
|
|
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("buflen: %d nsamples: %d\n", buflen, nsamples);
|
2011-09-11 16:55:31 +02:00
|
|
|
|
2011-09-10 18:20:09 +02:00
|
|
|
/* Get the requested number of samples */
|
|
|
|
|
2011-09-11 16:55:31 +02:00
|
|
|
for (i = 0; i < nsamples; i++)
|
2011-09-10 18:20:09 +02:00
|
|
|
{
|
2015-01-06 18:50:56 +01:00
|
|
|
b16_t temp = 0;
|
2011-09-10 18:20:09 +02:00
|
|
|
|
|
|
|
/* Read the next b16_t temperature value */
|
|
|
|
|
|
|
|
ret = lm75_readtemp(priv, &temp);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: lm75_readtemp failed: %d\n", ret);
|
2011-09-10 18:20:09 +02:00
|
|
|
return (ssize_t)ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the temperature value in the user buffer */
|
|
|
|
|
|
|
|
*ptr++ = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsamples * sizeof(b16_t);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lm75_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t lm75_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lm75_ioctl
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int lm75_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct lm75_dev_s *priv = inode->i_private;
|
|
|
|
int ret = OK;
|
|
|
|
|
2011-09-11 16:55:31 +02:00
|
|
|
switch (cmd)
|
2011-09-10 18:20:09 +02:00
|
|
|
{
|
|
|
|
/* 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);
|
2011-09-10 18:20:09 +02:00
|
|
|
ret = lm75_readconf(priv, ptr);
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("conf: %02x ret: %d\n", *ptr, ret);
|
2011-09-10 18:20:09 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-04-15 15:56:53 +02:00
|
|
|
/* Write to the configuration register. Arg: uint8_t value */
|
2011-09-10 18:20:09 +02:00
|
|
|
|
|
|
|
case SNIOC_WRITECONF:
|
|
|
|
ret = lm75_writeconf(priv, (uint8_t)arg);
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("conf: %02x ret: %d\n", *(FAR uint8_t *)arg, ret);
|
2011-09-10 18:20:09 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Shutdown the LM75, Arg: None */
|
|
|
|
|
|
|
|
case SNIOC_SHUTDOWN:
|
|
|
|
{
|
|
|
|
uint8_t conf;
|
|
|
|
ret = lm75_readconf(priv, &conf);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
ret = lm75_writeconf(priv, conf | LM75_CONF_SHUTDOWN);
|
|
|
|
}
|
2014-12-16 16:54:32 +01:00
|
|
|
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("conf: %02x ret: %d\n", conf | LM75_CONF_SHUTDOWN, ret);
|
2011-09-10 18:20:09 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Powerup the LM75, Arg: None */
|
|
|
|
|
|
|
|
case SNIOC_POWERUP:
|
|
|
|
{
|
|
|
|
uint8_t conf;
|
|
|
|
ret = lm75_readconf(priv, &conf);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
ret = lm75_writeconf(priv, conf & ~LM75_CONF_SHUTDOWN);
|
|
|
|
}
|
2014-12-16 16:54:32 +01:00
|
|
|
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("conf: %02x ret: %d\n", conf & ~LM75_CONF_SHUTDOWN, ret);
|
2011-09-10 18:20:09 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Report samples in Fahrenheit */
|
|
|
|
|
|
|
|
case SNIOC_FAHRENHEIT:
|
|
|
|
priv->fahrenheit = true;
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("Fahrenheit\n");
|
2011-09-10 18:20:09 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Report Samples in Centigrade */
|
|
|
|
|
|
|
|
case SNIOC_CENTIGRADE:
|
|
|
|
priv->fahrenheit = false;
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("Centigrade\n");
|
2011-09-11 16:55:31 +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);
|
2011-09-11 16:55:31 +02:00
|
|
|
ret = lm75_readb16(priv, LM75_THYS_REG, ptr);
|
2020-11-30 07:45:22 +01:00
|
|
|
sninfo("THYS: %08" PRIx32 " ret: %d\n", *ptr, ret);
|
2011-09-11 16:55:31 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Write THYS temperature register. Arg: b16_t value */
|
|
|
|
|
|
|
|
case SNIOC_WRITETHYS:
|
|
|
|
ret = lm75_writeb16(priv, LM75_THYS_REG, (b16_t)arg);
|
2020-11-30 07:45:22 +01:00
|
|
|
sninfo("THYS: %08" PRIx32 " ret: %d\n", (b16_t)arg, ret);
|
2011-09-11 16:55:31 +02:00
|
|
|
break;
|
|
|
|
|
2020-11-30 07:40:55 +01:00
|
|
|
/* Read TOS (Over-temp Shutdown Threshold) Register.
|
|
|
|
* Arg: b16_t* pointer
|
|
|
|
*/
|
2011-09-11 16:55:31 +02:00
|
|
|
|
|
|
|
case SNIOC_READTOS:
|
|
|
|
{
|
|
|
|
FAR b16_t *ptr = (FAR b16_t *)((uintptr_t)arg);
|
2015-08-10 20:59:11 +02:00
|
|
|
DEBUGASSERT(ptr != NULL);
|
2011-09-11 16:55:31 +02:00
|
|
|
ret = lm75_readb16(priv, LM75_TOS_REG, ptr);
|
2020-11-30 07:45:22 +01:00
|
|
|
sninfo("TOS: %08" PRIx32 " ret: %d\n", *ptr, ret);
|
2011-09-11 16:55:31 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-11-30 07:40:55 +01:00
|
|
|
/* Write TOS (Over-temp Shutdown Threshold) Register.
|
|
|
|
* Arg: b16_t value
|
|
|
|
*/
|
2011-09-11 16:55:31 +02:00
|
|
|
|
2015-08-10 20:40:36 +02:00
|
|
|
case SNIOC_WRITETOS:
|
2011-09-11 16:55:31 +02:00
|
|
|
ret = lm75_writeb16(priv, LM75_TOS_REG, (b16_t)arg);
|
2020-11-30 07:45:22 +01:00
|
|
|
sninfo("TOS: %08" PRIx32 " ret: %d\n", (b16_t)arg, ret);
|
2011-09-10 18:20:09 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("Unrecognized cmd: %d\n", cmd);
|
2011-09-10 18:20:09 +02:00
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lm75_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register the LM-75 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 LM75
|
|
|
|
* addr - The I2C address of the LM-75. The base I2C address of the LM75
|
|
|
|
* is 0x48. Bits 0-3 can be controlled to get 8 unique addresses from 0x48
|
|
|
|
* through 0x4f.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; a negated errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-11-30 07:40:55 +01:00
|
|
|
int lm75_register(FAR const char *devpath, FAR struct i2c_master_s *i2c,
|
|
|
|
uint8_t addr)
|
2011-09-10 18:20:09 +02:00
|
|
|
{
|
|
|
|
FAR struct lm75_dev_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
2015-08-10 20:59:11 +02:00
|
|
|
/* Sanity check */
|
|
|
|
|
|
|
|
DEBUGASSERT(i2c != NULL);
|
|
|
|
DEBUGASSERT(addr == CONFIG_LM75_ADDR0 || addr == CONFIG_LM75_ADDR1 ||
|
|
|
|
addr == CONFIG_LM75_ADDR2 || addr == CONFIG_LM75_ADDR3 ||
|
|
|
|
addr == CONFIG_LM75_ADDR4 || addr == CONFIG_LM75_ADDR5 ||
|
|
|
|
addr == CONFIG_LM75_ADDR6 || addr == CONFIG_LM75_ADDR7);
|
|
|
|
|
2011-09-10 18:20:09 +02:00
|
|
|
/* Initialize the LM-75 device structure */
|
|
|
|
|
2014-09-01 01:26:36 +02:00
|
|
|
priv = (FAR struct lm75_dev_s *)kmm_malloc(sizeof(struct lm75_dev_s));
|
2015-08-10 20:59:11 +02:00
|
|
|
if (priv == NULL)
|
2011-09-10 18:20:09 +02:00
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: Failed to allocate instance\n");
|
2011-09-10 18:20:09 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->i2c = i2c;
|
|
|
|
priv->addr = addr;
|
|
|
|
priv->fahrenheit = false;
|
|
|
|
|
|
|
|
/* Register the character driver */
|
|
|
|
|
2012-05-09 00:10:29 +02:00
|
|
|
ret = register_driver(devpath, &g_lm75fops, 0666, priv);
|
2011-09-10 18:20:09 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: Failed to register driver: %d\n", ret);
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(priv);
|
2011-09-10 18:20:09 +02:00
|
|
|
}
|
2013-03-09 22:12:20 +01:00
|
|
|
|
2011-09-10 18:20:09 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2017-08-24 18:10:00 +02:00
|
|
|
#endif /* CONFIG_I2C && CONFIG_LM75_I2C */
|