diff --git a/ChangeLog b/ChangeLog index b5fcab5b83..ce7daaffaf 100755 --- a/ChangeLog +++ b/ChangeLog @@ -11033,3 +11033,8 @@ * configs/stm32f4discovery/src/stm32_zerocross.c: Add low level support for the Zero Cross driver for the STM32F4-Discovery. From Alan Carvalho de Assis (2015-10-13). + * drivers/sensors/max6675.c and include/nuttx/sensors/max6675.h: Add + support to Thermocouple-to-Digital converter MAX6675. From Alan + Carvalho de Assis (2015-10-18). + * configs/stm32f4discovery/src: Add board config to support MAX6675. + From Alan Carvalho de Assis (2015-10-18). diff --git a/configs b/configs index 47c4c967a3..d459d2d316 160000 --- a/configs +++ b/configs @@ -1 +1 @@ -Subproject commit 47c4c967a3dd1c9de5fa41ba587468f744f1ca36 +Subproject commit d459d2d3160b0947ed87f8ebd6a0bf7ad04ebd05 diff --git a/drivers/sensors/max6675.c b/drivers/sensors/max6675.c new file mode 100644 index 0000000000..a39544ecea --- /dev/null +++ b/drivers/sensors/max6675.c @@ -0,0 +1,278 @@ +/**************************************************************************** + * drivers/sensors/max6675.c + * Character driver for the Maxim MAX6675 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 MAX6675 chips have an issue it report value 25% lower + * of real temperature, for more info read this thread: + * http://www.eevblog.com/forum/projects/max6675-temperature-error/ +*/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +#if defined(CONFIG_SPI) && defined(CONFIG_MAX6675) + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private + ****************************************************************************/ + +#define MAX6675_THREE_STATE (1 << 0) +#define MAX6675_DEV_ID (1 << 1) +#define MAX6675_OPEN_CIRCUIT (1 << 2) +#define MAX6675_TEMP_COUPLE 0x7ff8 + +struct max6675_dev_s +{ + FAR struct spi_dev_s *spi; /* Saved SPI driver instance */ + int16_t temp; +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/* Character driver methods */ + +static int max6675_open(FAR struct file *filep); +static int max6675_close(FAR struct file *filep); +static ssize_t max6675_read(FAR struct file *, FAR char *, size_t); +static ssize_t max6675_write(FAR struct file *filep, FAR const char *buffer, + size_t buflen); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct file_operations g_max6675fops = +{ + max6675_open, + max6675_close, + max6675_read, + max6675_write, + NULL, + NULL +#ifndef CONFIG_DISABLE_POLL + , NULL +#endif +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ +/**************************************************************************** + * Name: max6675_open + * + * Description: + * This function is called whenever the MAX6675 device is opened. + * + ****************************************************************************/ + +static int max6675_open(FAR struct file *filep) +{ + return OK; +} + +/**************************************************************************** + * Name: max6675_close + * + * Description: + * This routine is called when the MAX6675 device is closed. + * + ****************************************************************************/ + +static int max6675_close(FAR struct file *filep) +{ + return OK; +} + +/**************************************************************************** + * Name: max6675_read + ****************************************************************************/ + +static ssize_t max6675_read(FAR struct file *filep, FAR char *buffer, size_t buflen) +{ + FAR struct inode *inode = filep->f_inode; + FAR struct max6675_dev_s *priv = inode->i_private; + FAR uint16_t *temp = (FAR uint16_t *) buffer; + int ret = 2; + int16_t regmsb; + int16_t 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 MAX6675's chip select */ + + SPI_SELECT(priv->spi, SPIDEV_TEMPERATURE, true); + + /* Read temperature */ + + SPI_RECVBLOCK(priv->spi, ®msb, 2); + + /* Disable MAX6675's chip select */ + + SPI_SELECT(priv->spi, SPIDEV_TEMPERATURE, false); + + regval = (regmsb & 0xFF00) >> 8; + regval |= (regmsb & 0xFF) << 8; + + sndbg("Read from MAX6675 = 0x%04X\n", regval); + + /* Verify if the device ID bit is really zero */ + + if (regval & MAX6675_DEV_ID) + { + sndbg("ERROR: The Device ID bit needs to be 0 !\n"); + ret = -EINVAL; + } + + /* Detect if termocople input is open */ + + if (regval & MAX6675_OPEN_CIRCUIT) + { + sndbg("The thermocouple input is not connected!\n"); + ret = -EINVAL; + } + + /* Get the temperature */ + + *temp = (regval & MAX6675_TEMP_COUPLE) >> 3; + + /* Return two bytes, the temperature is fixed point Q10.2, then divide by 4 + * in your application in order to get real temperature in Celsius degrees. + */ + + return ret; +} + +/**************************************************************************** + * Name: max6675_write + ****************************************************************************/ + +static ssize_t max6675_write(FAR struct file *filep, FAR const char *buffer, + size_t buflen) +{ + return -ENOSYS; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: max6675_register + * + * Description: + * Register the MAX6675 character device as 'devpath' + * + * Input Parameters: + * devpath - The full path to the driver to register. E.g., "/dev/temp0" + * spi - An instance of the SPU interface to use to communicate with + * MAX6675. + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ****************************************************************************/ + +int max6675_register(FAR const char *devpath, FAR struct spi_dev_s *spi) +{ + FAR struct max6675_dev_s *priv; + int ret; + + /* Sanity check */ + + DEBUGASSERT(spi != NULL); + + /* Initialize the MAX6675 device structure */ + + priv = (FAR struct max6675_dev_s *)kmm_malloc(sizeof(struct max6675_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 MAX6675 */ + + SPI_SETMODE(priv->spi, SPIDEV_MODE1); + SPI_SETBITS(priv->spi, 8); + SPI_SETFREQUENCY(priv->spi, MAX6675_SPI_MAXFREQ); +#endif + + /* Register the character driver */ + + ret = register_driver(devpath, &g_max6675fops, 0666, priv); + if (ret < 0) + { + sndbg("Failed to register driver: %d\n", ret); + kmm_free(priv); + } + + return ret; +} +#endif /* CONFIG_SPI && CONFIG_MAX6675 */ diff --git a/include/nuttx/sensors/max6675.h b/include/nuttx/sensors/max6675.h new file mode 100644 index 0000000000..0864c7434b --- /dev/null +++ b/include/nuttx/sensors/max6675.h @@ -0,0 +1,98 @@ +/**************************************************************************** + * include/nuttx/input/max6675.h + * + * Copyright (C) 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_MAX6675_H +#define __INCLUDE_NUTTX_SENSORS_MAX6675_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +#if defined(CONFIG_MAX6675) + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +#define MAX6675_SPI_MAXFREQ 4000000 + +struct spi_dev_s; + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Name: max6675_register + * + * Description: + * This function will register the max6675 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 + * MAX6675 + * + * Returned Value: + * Zero is returned on success. Otherwise, a negated errno value is + * returned to indicate the nature of the failure. + * + ****************************************************************************/ + +int max6675_register(FAR const char *devpath, FAR struct spi_dev_s *spi); + +#undef EXTERN +#ifdef __cplusplus +} +#endif + +#endif /* CONFIG_MAX6675 */ +#endif /* __INCLUDE_NUTTX_SENSORS_MAX6675_H */