2015-10-18 02:00:06 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/sensors/max6675.c
|
|
|
|
*
|
2021-03-13 10:55:07 +01: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
|
2015-10-18 02:00:06 +02:00
|
|
|
*
|
2021-03-13 10:55:07 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-10-18 02:00:06 +02:00
|
|
|
*
|
2021-03-13 10:55:07 +01: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.
|
2015-10-18 02:00:06 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-13 10:55:07 +01:00
|
|
|
/* * Character driver for the Maxim MAX6675 Thermocouple-to-Digital Converter
|
|
|
|
*
|
|
|
|
* NOTE: Some Maxim MAX6675 chips have an issue it report value 25% lower
|
2015-10-18 02:00:06 +02:00
|
|
|
* of real temperature, for more info read this thread:
|
|
|
|
* http://www.eevblog.com/forum/projects/max6675-temperature-error/
|
2019-12-07 02:07:34 +01:00
|
|
|
*/
|
2015-10-18 02:00:06 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fixedmath.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2015-10-18 02:00:06 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/spi/spi.h>
|
|
|
|
#include <nuttx/sensors/max6675.h>
|
2017-03-30 15:38:37 +02:00
|
|
|
#include <nuttx/random.h>
|
2015-10-18 02:00:06 +02:00
|
|
|
|
2017-08-24 18:10:00 +02:00
|
|
|
#if defined(CONFIG_SPI) && defined(CONFIG_SENSORS_MAX6675)
|
2015-10-18 02:00:06 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-03-05 14:04:07 +01:00
|
|
|
static void max6675_lock(FAR struct spi_dev_s *spi);
|
|
|
|
static void max6675_unlock(FAR struct spi_dev_s *spi);
|
2016-05-26 22:56:10 +02:00
|
|
|
|
2015-10-18 02:00:06 +02:00
|
|
|
/* 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,
|
2019-05-22 02:57:54 +02:00
|
|
|
NULL,
|
2015-10-18 02:00:06 +02:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
2016-05-26 22:56:10 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: max6675_lock
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Lock and configure the SPI bus.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void max6675_lock(FAR struct spi_dev_s *spi)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(spi, true);
|
2016-05-26 22:56:10 +02:00
|
|
|
SPI_SETMODE(spi, SPIDEV_MODE0);
|
|
|
|
SPI_SETBITS(spi, 8);
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_HWFEATURES(spi, 0);
|
2018-06-16 21:48:19 +02:00
|
|
|
SPI_SETFREQUENCY(spi, 4000000);
|
2016-05-26 22:56:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: max6675_unlock
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Unlock the SPI bus.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void max6675_unlock(FAR struct spi_dev_s *spi)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(spi, false);
|
2016-05-26 22:56:10 +02:00
|
|
|
}
|
|
|
|
|
2015-10-18 02:00:06 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* 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
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-13 11:55:37 +01:00
|
|
|
static ssize_t max6675_read(FAR struct file *filep,
|
|
|
|
FAR char *buffer, size_t buflen)
|
2015-10-18 02:00:06 +02:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: Buffer is null\n");
|
2015-10-18 02:00:06 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buflen != 2)
|
|
|
|
{
|
2021-03-13 11:55:37 +01:00
|
|
|
snerr("ERROR:");
|
|
|
|
snerr(" You can't read something other than 16 bits (2 bytes)\n");
|
2015-10-18 02:00:06 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable MAX6675's chip select */
|
|
|
|
|
2016-05-26 22:56:10 +02:00
|
|
|
max6675_lock(priv->spi);
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_TEMPERATURE(0), true);
|
2015-10-18 02:00:06 +02:00
|
|
|
|
|
|
|
/* Read temperature */
|
|
|
|
|
|
|
|
SPI_RECVBLOCK(priv->spi, ®msb, 2);
|
|
|
|
|
|
|
|
/* Disable MAX6675's chip select */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_TEMPERATURE(0), false);
|
2016-05-26 22:56:10 +02:00
|
|
|
max6675_unlock(priv->spi);
|
2015-10-18 02:00:06 +02:00
|
|
|
|
2019-12-07 02:07:34 +01:00
|
|
|
regval = (regmsb & 0xff00) >> 8;
|
|
|
|
regval |= (regmsb & 0xff) << 8;
|
2015-10-18 02:00:06 +02:00
|
|
|
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("Read from MAX6675 = 0x%04X\n", regval);
|
2015-10-18 02:00:06 +02:00
|
|
|
|
|
|
|
/* Verify if the device ID bit is really zero */
|
|
|
|
|
|
|
|
if (regval & MAX6675_DEV_ID)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
snerr("ERROR: The Device ID bit needs to be 0 !\n");
|
2015-10-18 02:00:06 +02:00
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Detect if termocople input is open */
|
|
|
|
|
|
|
|
if (regval & MAX6675_OPEN_CIRCUIT)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: The thermocouple input is not connected!\n");
|
2015-10-18 02:00:06 +02:00
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-03-30 15:38:37 +02:00
|
|
|
/* Feed sensor data to entropy pool */
|
|
|
|
|
|
|
|
add_sensor_randomness(regval);
|
|
|
|
|
2015-10-18 02:00:06 +02:00
|
|
|
/* 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 */
|
|
|
|
|
2021-03-13 11:55:37 +01:00
|
|
|
priv = (FAR struct max6675_dev_s *)
|
|
|
|
kmm_malloc(sizeof(struct max6675_dev_s));
|
2015-10-18 02:00:06 +02:00
|
|
|
if (priv == NULL)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: Failed to allocate instance\n");
|
2015-10-18 02:00:06 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->spi = spi;
|
|
|
|
priv->temp = 0;
|
|
|
|
|
|
|
|
/* Register the character driver */
|
|
|
|
|
|
|
|
ret = register_driver(devpath, &g_max6675fops, 0666, priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: Failed to register driver: %d\n", ret);
|
2015-10-18 02:00:06 +02:00
|
|
|
kmm_free(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2017-08-24 18:10:00 +02:00
|
|
|
#endif /* CONFIG_SPI && CONFIG_SENSORS_MAX6675 */
|