From c4cf5be231c65ec12c33d646397d13569e852fa1 Mon Sep 17 00:00:00 2001 From: Alan Carvalho de Assis Date: Sun, 13 Sep 2015 15:56:48 -0600 Subject: [PATCH] Add support to Maxim MAX31855 Thermocouple-to-Digital --- arch | 2 +- configs | 2 +- drivers/sensors/Kconfig | 7 + drivers/sensors/Make.defs | 4 + drivers/sensors/max31855.c | 299 +++++++++++++++++++++++++++++++ include/nuttx/sensors/max31855.h | 98 ++++++++++ include/nuttx/spi/spi.h | 1 + 7 files changed, 411 insertions(+), 2 deletions(-) create mode 100644 drivers/sensors/max31855.c create mode 100644 include/nuttx/sensors/max31855.h diff --git a/arch b/arch index a46ffe3c4b..b185e7bda9 160000 --- a/arch +++ b/arch @@ -1 +1 @@ -Subproject commit a46ffe3c4bb631117ad9f5efa8ef77a93172e976 +Subproject commit b185e7bda960fe11a2f8f8c6766e6fabb23ff41a diff --git a/configs b/configs index b7d85ef0b4..21767e867c 160000 --- a/configs +++ b/configs @@ -1 +1 @@ -Subproject commit b7d85ef0b42255261ef209e52e503c9302376a89 +Subproject commit 21767e867c11ee5de08be734f4f8dafb90b82243 diff --git a/drivers/sensors/Kconfig b/drivers/sensors/Kconfig index 4afdae7c85..510696bce7 100644 --- a/drivers/sensors/Kconfig +++ b/drivers/sensors/Kconfig @@ -92,6 +92,13 @@ config ADXL345_REGDEBUG endif # SENSORS_ADXL345 +config MAX31855 + bool "Maxim MAX31855 Driver" + default n + select SPI + ---help--- + Enables support for the MAX31855 driver + config I2C_LM75 bool default y if LM75 diff --git a/drivers/sensors/Make.defs b/drivers/sensors/Make.defs index 3625e10e62..9a00862dfa 100644 --- a/drivers/sensors/Make.defs +++ b/drivers/sensors/Make.defs @@ -86,6 +86,10 @@ ifeq ($(CONFIG_ADXL345_SPI),y) CSRCS += adxl345_spi.c endif +ifeq ($(CONFIG_MAX31855),y) + CSRCS += max31855.c +endif + ifeq ($(CONFIG_MPL115A),y) CSRCS += mpl115a.c endif diff --git a/drivers/sensors/max31855.c b/drivers/sensors/max31855.c new file mode 100644 index 0000000000..e7aaa82c02 --- /dev/null +++ b/drivers/sensors/max31855.c @@ -0,0 +1,299 @@ +/**************************************************************************** + * 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 + * + * 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/ +*/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +#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 + ****************************************************************************/ + +/* Character driver methods */ + +static int max31855_open(FAR struct file *filep); +static int max31855_close(FAR struct file *filep); +static ssize_t max31855_read(FAR struct file *, FAR char *, size_t); +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 + ****************************************************************************/ +/**************************************************************************** + * 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; + FAR struct max31855_dev_s *priv = inode->i_private; + FAR uint16_t *temp = (FAR uint16_t *) buffer; + int ret = 2; + int32_t regmsb, regval; + + /* Check for issues */ + + if (!buffer) + { + sndbg("Buffer is null\n"); + return -EINVAL; + } + + if (buflen != 2) + { + sndbg("You can't read something other than 16 bits (2 bytes)\n"); + return -EINVAL; + } + + /* Enable MAX31855's chip select */ + + 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); + + regval = (regmsb & 0xFF000000) >> 24; + regval |= (regmsb & 0xFF0000) >> 8; + regval |= (regmsb & 0xFF00) << 8; + regval |= (regmsb & 0xFF) << 24; + + sndbg("Read from MAX31855 = 0x%08X\n", regval); + + /* If negative, fix signal bits */ + + if (regval & 0x80000000) + { + temp = 0xFFFFC000 | (regval >> 18); + } + else + { + temp = (regval >> 18); + } + + /* Detect any fault */ + + if (regval & MAX31855_FAULT) + { + sndbg("Error: A fault was detected by MAX31855:\n"); + + if (regval & MAX31855_SHORT_VCC) + { + sndbg("The thermocouple input is shorted to VCC!\n"); + } + + if (regval & MAX31855_SHORT_GND) + { + sndbg("The thermocouple input is shorted to GND!\n"); + } + + if (regval & MAX31855_OPEN_CIRCUIT) + { + sndbg("The thermocouple input is not connected!\n"); + } + + 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) + { + sndbg("Failed to allocate instance\n"); + return -ENOMEM; + } + + priv->spi = spi; + priv->temp = 0; + +#ifdef CONFIG_SPI_OWNBUS + /* Configure SPI for the MAX31855 */ + + SPI_SETMODE(priv->spi, SPIDEV_MODE1); + SPI_SETBITS(priv->spi, 8); + SPI_SETFREQUENCY(priv->spi, MAX31855_SPI_MAXFREQ); +#endif + + /* Register the character driver */ + + ret = register_driver(devpath, &g_max31855fops, 0666, priv); + if (ret < 0) + { + sndbg("Failed to register driver: %d\n", ret); + kmm_free(priv); + } + + return ret; +} +#endif /* CONFIG_SPI && CONFIG_MAX31855 */ diff --git a/include/nuttx/sensors/max31855.h b/include/nuttx/sensors/max31855.h new file mode 100644 index 0000000000..4a4a91b9c4 --- /dev/null +++ b/include/nuttx/sensors/max31855.h @@ -0,0 +1,98 @@ +/**************************************************************************** + * include/nuttx/input/max31855.h + * + * Copyright (C) 2014-2015 Gregory Nutt. All rights reserved. + * Author: Alan Carvalho de Assis + * + * 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_MAX31855_H +#define __INCLUDE_NUTTX_SENSORS_MAX31855_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +#if defined(CONFIG_MAX31855) + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +#define MAX31855_SPI_MAXFREQ 4000000 + +struct spi_dev_s; + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Name: max31855_register + * + * Description: + * This function will register the max31855 driver as /dev/tempN + * where N is the minor device number + * + * Input Parameters: + * devpath - The full path to the driver to register. E.g., "/dev/temp0" + * spi - An instance of the SPI interface to use to communicate with + * MAX31855 + * + * Returned Value: + * Zero is returned on success. Otherwise, a negated errno value is + * returned to indicate the nature of the failure. + * + ****************************************************************************/ + +int max31855_register(FAR const char *devpath, FAR struct spi_dev_s *spi); + +#undef EXTERN +#ifdef __cplusplus +} +#endif + +#endif /* CONFIG_MAX31855 */ +#endif /* __INCLUDE_NUTTX_SENSORS_MAX31855_H */ diff --git a/include/nuttx/spi/spi.h b/include/nuttx/spi/spi.h index 4f77e0cb79..ebd4d872e1 100644 --- a/include/nuttx/spi/spi.h +++ b/include/nuttx/spi/spi.h @@ -364,6 +364,7 @@ enum spi_dev_e SPIDEV_EEPROM, /* Select SPI EEPROM device */ SPIDEV_ACCELEROMETER, /* Select SPI Accelerometer device */ SPIDEV_BAROMETER, /* Select SPI Pressure/Barometer device */ + SPIDEV_TEMPERATURE, /* Select SPI Temperature sensor device */ SPIDEV_USER /* Board-specific values start here */ };