2015-09-13 23:56:48 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/sensors/max31855.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-09-13 23:56:48 +02:00
|
|
|
*
|
2021-03-13 10:55:07 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-09-13 23:56:48 +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-09-13 23:56:48 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-13 10:55:07 +01:00
|
|
|
/* Character driver for the Maxim MAX31855 Thermocouple-to-Digital Converter
|
|
|
|
*
|
|
|
|
* NOTE: Some Maxim MAX31855 chips have an issue it report value 25% lower
|
2015-09-13 23:56:48 +02:00
|
|
|
* of real temperature, for more info read this thread:
|
|
|
|
* http://www.eevblog.com/forum/projects/max31855-temperature-error/
|
2015-10-10 18:41:00 +02:00
|
|
|
*/
|
2015-09-13 23:56:48 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2020-11-28 01:58:38 +01:00
|
|
|
#include <inttypes.h>
|
2015-09-13 23:56:48 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fixedmath.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2015-09-13 23:56:48 +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/max31855.h>
|
2017-03-30 15:38:37 +02:00
|
|
|
#include <nuttx/random.h>
|
2015-09-13 23:56:48 +02:00
|
|
|
|
2017-08-24 18:10:00 +02:00
|
|
|
#if defined(CONFIG_SPI) && defined(CONFIG_SENSORS_MAX31855)
|
2015-09-13 23:56:48 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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)
|
2019-12-07 02:07:34 +01:00
|
|
|
#define MAX31855_TEMP_COUPLE 0xffffc000
|
|
|
|
#define MAX31855_TEMP_JUNCTION 0xfff0
|
2015-09-13 23:56:48 +02:00
|
|
|
|
|
|
|
struct max31855_dev_s
|
|
|
|
{
|
|
|
|
FAR struct spi_dev_s *spi; /* Saved SPI driver instance */
|
|
|
|
int16_t temp;
|
2018-04-09 23:36:27 +02:00
|
|
|
uint16_t devid; /* Select minor device number */
|
2015-09-13 23:56:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-05-26 22:56:10 +02:00
|
|
|
static void max31855_lock(FAR struct spi_dev_s *spi);
|
|
|
|
static void max31855_unlock(FAR struct spi_dev_s *spi);
|
|
|
|
|
2015-09-13 23:56:48 +02:00
|
|
|
/* Character driver methods */
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
static ssize_t max31855_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
2015-09-13 23:56:48 +02: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 =
|
|
|
|
{
|
2022-04-12 12:16:13 +02:00
|
|
|
NULL, /* open */
|
|
|
|
NULL, /* close */
|
2022-01-10 15:51:17 +01:00
|
|
|
max31855_read, /* read */
|
|
|
|
max31855_write, /* write */
|
2015-09-13 23:56:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
2016-05-26 22:56:10 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: max31855_lock
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Lock and configure the SPI bus.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void max31855_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);
|
2016-05-26 22:56:10 +02:00
|
|
|
SPI_SETFREQUENCY(spi, 400000);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: max31855_unlock
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Unlock the SPI bus.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void max31855_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-09-13 23:56:48 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: max31855_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-04-09 23:36:27 +02:00
|
|
|
static ssize_t max31855_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen)
|
2015-09-13 23:56:48 +02:00
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
2015-09-14 15:47:21 +02: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-13 23:56:48 +02:00
|
|
|
|
|
|
|
/* Check for issues */
|
|
|
|
|
|
|
|
if (!buffer)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: Buffer is null\n");
|
2015-09-13 23:56:48 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buflen != 2)
|
|
|
|
{
|
2020-11-28 01:58:06 +01:00
|
|
|
snerr("ERROR: You can't read something other than 16 bits "
|
|
|
|
"(2 bytes)\n");
|
2015-09-13 23:56:48 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable MAX31855's chip select */
|
|
|
|
|
2016-05-26 22:56:10 +02:00
|
|
|
max31855_lock(priv->spi);
|
2018-04-09 23:36:27 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_TEMPERATURE(priv->devid), true);
|
2015-09-13 23:56:48 +02:00
|
|
|
|
|
|
|
/* Read temperature */
|
|
|
|
|
|
|
|
SPI_RECVBLOCK(priv->spi, ®msb, 4);
|
|
|
|
|
|
|
|
/* Disable MAX31855's chip select */
|
|
|
|
|
2018-04-09 23:36:27 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_TEMPERATURE(priv->devid), false);
|
2016-05-26 22:56:10 +02:00
|
|
|
max31855_unlock(priv->spi);
|
2015-09-13 23:56:48 +02:00
|
|
|
|
2018-04-09 23:36:27 +02:00
|
|
|
/* Detect any errors during SPI transmission */
|
|
|
|
|
|
|
|
if (!(regmsb) || regmsb == -1)
|
|
|
|
{
|
|
|
|
snerr("ERROR: Data transmission failed on device %d:\n", priv->devid);
|
|
|
|
snerr(" One or more MAX31855 pins are not properly connected!\n\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2019-12-07 02:07:34 +01:00
|
|
|
regval = (regmsb & 0xff000000) >> 24;
|
|
|
|
regval |= (regmsb & 0xff0000) >> 8;
|
|
|
|
regval |= (regmsb & 0xff00) << 8;
|
|
|
|
regval |= (regmsb & 0xff) << 24;
|
2015-09-13 23:56:48 +02:00
|
|
|
|
2020-11-28 01:58:38 +01:00
|
|
|
sninfo("Read from MAX31855 = 0x%08" PRIX32 "\n", regval);
|
2015-09-13 23:56:48 +02:00
|
|
|
|
2017-03-30 15:38:37 +02:00
|
|
|
/* Feed sensor data to entropy pool */
|
|
|
|
|
|
|
|
add_sensor_randomness(regval);
|
|
|
|
|
2015-09-13 23:56:48 +02:00
|
|
|
/* If negative, fix signal bits */
|
|
|
|
|
|
|
|
if (regval & 0x80000000)
|
|
|
|
{
|
2015-09-14 15:47:21 +02:00
|
|
|
*temp = 0xc000 | (regval >> 18);
|
2015-09-13 23:56:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-09-14 15:47:21 +02:00
|
|
|
*temp = (regval >> 18);
|
2015-09-13 23:56:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Detect any fault */
|
|
|
|
|
|
|
|
if (regval & MAX31855_FAULT)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: A fault was detected by MAX31855:\n");
|
2015-09-13 23:56:48 +02:00
|
|
|
|
|
|
|
if (regval & MAX31855_SHORT_VCC)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr(" The thermocouple input is shorted to VCC!\n");
|
2015-09-13 23:56:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (regval & MAX31855_SHORT_GND)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr(" The thermocouple input is shorted to GND!\n");
|
2015-09-13 23:56:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (regval & MAX31855_OPEN_CIRCUIT)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr(" The thermocouple input is not connected!\n");
|
2015-09-13 23:56:48 +02:00
|
|
|
}
|
|
|
|
|
2018-04-09 23:36:27 +02:00
|
|
|
return -EINVAL;
|
2015-09-13 23:56:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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:
|
2018-04-09 23:36:27 +02:00
|
|
|
* This function will register the max31855 driver as /dev/tempN
|
|
|
|
* where N is the minor device number
|
2015-09-13 23:56:48 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2018-04-09 23:36:27 +02:00
|
|
|
* 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
|
|
|
|
* devid - Minor device number. E.g., 0, 1, 2, etc.
|
2015-09-13 23:56:48 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2018-04-09 23:36:27 +02:00
|
|
|
* Zero is returned on success. Otherwise, a negated errno value is
|
|
|
|
* returned to indicate the nature of the failure.
|
2015-09-13 23:56:48 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-04-09 23:36:27 +02:00
|
|
|
int max31855_register(FAR const char *devpath, FAR struct spi_dev_s *spi,
|
|
|
|
uint16_t devid)
|
2015-09-13 23:56:48 +02:00
|
|
|
{
|
|
|
|
FAR struct max31855_dev_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
|
|
|
|
DEBUGASSERT(spi != NULL);
|
|
|
|
|
|
|
|
/* Initialize the MAX31855 device structure */
|
|
|
|
|
2020-11-28 01:58:06 +01:00
|
|
|
priv = (FAR struct max31855_dev_s *)
|
|
|
|
kmm_malloc(sizeof(struct max31855_dev_s));
|
2015-09-13 23:56:48 +02:00
|
|
|
if (priv == NULL)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: Failed to allocate instance\n");
|
2015-09-13 23:56:48 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2018-04-09 23:36:27 +02:00
|
|
|
priv->spi = spi;
|
|
|
|
priv->temp = 0;
|
|
|
|
priv->devid = devid;
|
2015-09-13 23:56:48 +02:00
|
|
|
|
|
|
|
/* Register the character driver */
|
|
|
|
|
|
|
|
ret = register_driver(devpath, &g_max31855fops, 0666, priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: Failed to register driver: %d\n", ret);
|
2015-09-13 23:56:48 +02:00
|
|
|
kmm_free(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2017-08-24 18:10:00 +02:00
|
|
|
#endif /* CONFIG_SPI && CONFIG_SENSORS_MAX31855 */
|