2015-09-14 05:56:48 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/sensors/max31855.c
|
|
|
|
* Character driver for the Maxim MAX31855 Thermocouple-to-Digital Converter
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 Alan Carvalho de Assis. All rights reserved.
|
|
|
|
* Author: Alan Carvalho de Assis <acassis@extern.io>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* NOTE: Some Maxim MAX31855 chips have an issue it report value 25% lower
|
|
|
|
* of real temperature, for more info read this thread:
|
|
|
|
* http://www.eevblog.com/forum/projects/max31855-temperature-error/
|
2015-10-11 00:41:00 +08:00
|
|
|
*/
|
2015-09-14 05:56:48 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fixedmath.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/spi/spi.h>
|
|
|
|
#include <nuttx/sensors/max31855.h>
|
2017-03-30 21:38:37 +08:00
|
|
|
#include <nuttx/random.h>
|
2015-09-14 05:56:48 +08:00
|
|
|
|
|
|
|
#if defined(CONFIG_SPI) && defined(CONFIG_MAX31855)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define MAX31855_FAULT (1 << 16)
|
|
|
|
#define MAX31855_SHORT_VCC (1 << 2)
|
|
|
|
#define MAX31855_SHORT_GND (1 << 1)
|
|
|
|
#define MAX31855_OPEN_CIRCUIT (1 << 0)
|
|
|
|
#define MAX31855_TEMP_COUPLE 0xFFFFC000
|
|
|
|
#define MAX31855_TEMP_JUNCTION 0xFFF0
|
|
|
|
|
|
|
|
struct max31855_dev_s
|
|
|
|
{
|
|
|
|
FAR struct spi_dev_s *spi; /* Saved SPI driver instance */
|
|
|
|
int16_t temp;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-05-27 04:56:10 +08:00
|
|
|
static void max31855_lock(FAR struct spi_dev_s *spi);
|
|
|
|
static void max31855_unlock(FAR struct spi_dev_s *spi);
|
|
|
|
|
2015-09-14 05:56:48 +08:00
|
|
|
/* Character driver methods */
|
|
|
|
|
|
|
|
static int max31855_open(FAR struct file *filep);
|
|
|
|
static int max31855_close(FAR struct file *filep);
|
2015-10-11 00:41:00 +08:00
|
|
|
static ssize_t max31855_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
2015-09-14 05:56:48 +08:00
|
|
|
static ssize_t max31855_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations g_max31855fops =
|
|
|
|
{
|
|
|
|
max31855_open,
|
|
|
|
max31855_close,
|
|
|
|
max31855_read,
|
|
|
|
max31855_write,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
|
|
|
, NULL
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
2016-05-27 04:56:10 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: max31855_lock
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Lock and configure the SPI bus.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void max31855_lock(FAR struct spi_dev_s *spi)
|
|
|
|
{
|
|
|
|
(void)SPI_LOCK(spi, true);
|
|
|
|
SPI_SETMODE(spi, SPIDEV_MODE0);
|
|
|
|
SPI_SETBITS(spi, 8);
|
|
|
|
(void)SPI_HWFEATURES(spi, 0);
|
|
|
|
SPI_SETFREQUENCY(spi, 400000);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: max31855_unlock
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Unlock the SPI bus.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void max31855_unlock(FAR struct spi_dev_s *spi)
|
|
|
|
{
|
|
|
|
(void)SPI_LOCK(spi, false);
|
|
|
|
}
|
|
|
|
|
2015-09-14 05:56:48 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: max31855_open
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function is called whenever the MAX31855 device is opened.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int max31855_open(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: max31855_close
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This routine is called when the MAX31855 device is closed.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int max31855_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: max31855_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t max31855_read(FAR struct file *filep, FAR char *buffer, size_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
2015-09-14 21:47:21 +08:00
|
|
|
FAR struct max31855_dev_s *priv = inode->i_private;
|
|
|
|
FAR uint16_t *temp = (FAR uint16_t *) buffer;
|
|
|
|
int ret = 2;
|
|
|
|
int32_t regmsb;
|
|
|
|
int32_t regval;
|
2015-09-14 05:56:48 +08:00
|
|
|
|
|
|
|
/* Check for issues */
|
|
|
|
|
|
|
|
if (!buffer)
|
|
|
|
{
|
2016-06-12 08:05:29 +08:00
|
|
|
snerr("ERROR: Buffer is null\n");
|
2015-09-14 05:56:48 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buflen != 2)
|
|
|
|
{
|
2016-06-12 08:05:29 +08:00
|
|
|
snerr("ERROR: You can't read something other than 16 bits (2 bytes)\n");
|
2015-09-14 05:56:48 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable MAX31855's chip select */
|
|
|
|
|
2016-05-27 04:56:10 +08:00
|
|
|
max31855_lock(priv->spi);
|
2015-09-14 05:56:48 +08:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_TEMPERATURE, true);
|
|
|
|
|
|
|
|
/* Read temperature */
|
|
|
|
|
|
|
|
SPI_RECVBLOCK(priv->spi, ®msb, 4);
|
|
|
|
|
|
|
|
/* Disable MAX31855's chip select */
|
|
|
|
|
|
|
|
SPI_SELECT(priv->spi, SPIDEV_TEMPERATURE, false);
|
2016-05-27 04:56:10 +08:00
|
|
|
max31855_unlock(priv->spi);
|
2015-09-14 05:56:48 +08:00
|
|
|
|
|
|
|
regval = (regmsb & 0xFF000000) >> 24;
|
|
|
|
regval |= (regmsb & 0xFF0000) >> 8;
|
|
|
|
regval |= (regmsb & 0xFF00) << 8;
|
|
|
|
regval |= (regmsb & 0xFF) << 24;
|
|
|
|
|
2016-06-12 08:05:29 +08:00
|
|
|
sninfo("Read from MAX31855 = 0x%08X\n", regval);
|
2015-09-14 05:56:48 +08:00
|
|
|
|
2017-03-30 21:38:37 +08:00
|
|
|
/* Feed sensor data to entropy pool */
|
|
|
|
|
|
|
|
add_sensor_randomness(regval);
|
|
|
|
|
2015-09-14 05:56:48 +08:00
|
|
|
/* If negative, fix signal bits */
|
|
|
|
|
|
|
|
if (regval & 0x80000000)
|
|
|
|
{
|
2015-09-14 21:47:21 +08:00
|
|
|
*temp = 0xc000 | (regval >> 18);
|
2015-09-14 05:56:48 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-09-14 21:47:21 +08:00
|
|
|
*temp = (regval >> 18);
|
2015-09-14 05:56:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Detect any fault */
|
|
|
|
|
|
|
|
if (regval & MAX31855_FAULT)
|
|
|
|
{
|
2016-06-12 08:05:29 +08:00
|
|
|
snerr("ERROR: A fault was detected by MAX31855:\n");
|
2015-09-14 05:56:48 +08:00
|
|
|
|
|
|
|
if (regval & MAX31855_SHORT_VCC)
|
|
|
|
{
|
2016-06-12 08:05:29 +08:00
|
|
|
snerr(" The thermocouple input is shorted to VCC!\n");
|
2015-09-14 05:56:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (regval & MAX31855_SHORT_GND)
|
|
|
|
{
|
2016-06-12 08:05:29 +08:00
|
|
|
snerr(" The thermocouple input is shorted to GND!\n");
|
2015-09-14 05:56:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (regval & MAX31855_OPEN_CIRCUIT)
|
|
|
|
{
|
2016-06-12 08:05:29 +08:00
|
|
|
snerr(" The thermocouple input is not connected!\n");
|
2015-09-14 05:56:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return two bytes, the temperature is fixed point Q12.2, then divide by 4
|
|
|
|
* in your application in other to get real temperature in Celsius degrees.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: max31855_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t max31855_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: max31855_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register the MAX31855 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 wit
|
|
|
|
* MAX31855 addr - The I2C address of the MAX31855. The base I2C address
|
|
|
|
* of the MAX31855 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int max31855_register(FAR const char *devpath, FAR struct spi_dev_s *spi)
|
|
|
|
{
|
|
|
|
FAR struct max31855_dev_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
|
|
|
|
DEBUGASSERT(spi != NULL);
|
|
|
|
|
|
|
|
/* Initialize the MAX31855 device structure */
|
|
|
|
|
|
|
|
priv = (FAR struct max31855_dev_s *)kmm_malloc(sizeof(struct max31855_dev_s));
|
|
|
|
if (priv == NULL)
|
|
|
|
{
|
2016-06-12 08:05:29 +08:00
|
|
|
snerr("ERROR: Failed to allocate instance\n");
|
2015-09-14 05:56:48 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->spi = spi;
|
|
|
|
priv->temp = 0;
|
|
|
|
|
|
|
|
/* Register the character driver */
|
|
|
|
|
|
|
|
ret = register_driver(devpath, &g_max31855fops, 0666, priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 08:05:29 +08:00
|
|
|
snerr("ERROR: Failed to register driver: %d\n", ret);
|
2015-09-14 05:56:48 +08:00
|
|
|
kmm_free(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_SPI && CONFIG_MAX31855 */
|