2021-07-06 19:36:41 -05:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
|
|
|
* Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Based on Based on Adafruit MAX31865 library:
|
|
|
|
*
|
|
|
|
* This is a library for the Adafruit PT100/P1000 RTD Sensor w/MAX31865
|
|
|
|
* Designed specifically to work with the Adafruit RTD Sensor
|
|
|
|
* https://www.adafruit.com/products/3328
|
|
|
|
*
|
|
|
|
* This sensor uses SPI to communicate, 4 pins are required to interface.
|
|
|
|
*
|
|
|
|
* Adafruit invests time and resources providing this open source code,
|
|
|
|
* please support Adafruit and open-source hardware by purchasing
|
|
|
|
* products from Adafruit!
|
|
|
|
*
|
|
|
|
* Written by Limor Fried/Ladyada for Adafruit Industries.
|
|
|
|
*
|
|
|
|
* Modifications by JoAnn Manges (@GadgetAngel)
|
|
|
|
* Copyright (c) 2020, JoAnn Manges
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../inc/MarlinConfig.h"
|
|
|
|
|
2021-08-30 22:40:49 -05:00
|
|
|
#if HAS_MAX31865 && !USE_ADAFRUIT_MAX31865
|
2021-07-06 19:36:41 -05:00
|
|
|
|
|
|
|
#include "MAX31865.h"
|
|
|
|
|
2021-12-08 12:55:09 -06:00
|
|
|
#ifndef MAX31865_MIN_SAMPLING_TIME_MSEC
|
|
|
|
#define MAX31865_MIN_SAMPLING_TIME_MSEC 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define DEBUG_OUT ENABLED(DEBUG_MAX31865)
|
|
|
|
#include "../core/debug_out.h"
|
|
|
|
|
2021-07-06 19:36:41 -05:00
|
|
|
// The maximum speed the MAX31865 can do is 5 MHz
|
|
|
|
SPISettings MAX31865::spiConfig = SPISettings(
|
2021-11-06 22:53:36 -05:00
|
|
|
TERN(TARGET_LPC1768, SPI_QUARTER_SPEED, TERN(ARDUINO_ARCH_STM32, SPI_CLOCK_DIV4, 500000)),
|
|
|
|
MSBFIRST,
|
|
|
|
SPI_MODE1 // CPOL0 CPHA1
|
2021-07-06 19:36:41 -05:00
|
|
|
);
|
|
|
|
|
2021-12-08 12:55:09 -06:00
|
|
|
#if DISABLED(LARGE_PINMAP)
|
2021-07-06 19:36:41 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the interface object using software (bitbang) SPI for PIN values
|
|
|
|
* less than or equal to 127.
|
|
|
|
*
|
|
|
|
* @param spi_cs the SPI CS pin to use
|
|
|
|
* @param spi_mosi the SPI MOSI pin to use
|
|
|
|
* @param spi_miso the SPI MISO pin to use
|
|
|
|
* @param spi_clk the SPI clock pin to use
|
|
|
|
*/
|
|
|
|
MAX31865::MAX31865(int8_t spi_cs, int8_t spi_mosi, int8_t spi_miso, int8_t spi_clk) {
|
2021-12-08 12:40:23 -06:00
|
|
|
cselPin = spi_cs;
|
|
|
|
mosiPin = spi_mosi;
|
|
|
|
misoPin = spi_miso;
|
|
|
|
sclkPin = spi_clk;
|
2021-07-06 19:36:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the interface object using hardware SPI for PIN for PIN values less
|
|
|
|
* than or equal to 127.
|
|
|
|
*
|
|
|
|
* @param spi_cs the SPI CS pin to use along with the default SPI device
|
|
|
|
*/
|
|
|
|
MAX31865::MAX31865(int8_t spi_cs) {
|
2021-12-08 12:40:23 -06:00
|
|
|
cselPin = spi_cs;
|
|
|
|
sclkPin = misoPin = mosiPin = -1;
|
2021-07-06 19:36:41 -05:00
|
|
|
}
|
|
|
|
|
2021-11-06 22:53:36 -05:00
|
|
|
#else // LARGE_PINMAP
|
2021-07-06 19:36:41 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the interface object using software (bitbang) SPI for PIN values
|
|
|
|
* which are larger than 127. If you have PIN values less than or equal to
|
|
|
|
* 127 use the other call for SW SPI.
|
|
|
|
*
|
|
|
|
* @param spi_cs the SPI CS pin to use
|
|
|
|
* @param spi_mosi the SPI MOSI pin to use
|
|
|
|
* @param spi_miso the SPI MISO pin to use
|
|
|
|
* @param spi_clk the SPI clock pin to use
|
|
|
|
* @param pin_mapping set to 1 for positive pin values
|
|
|
|
*/
|
2021-11-06 22:53:36 -05:00
|
|
|
MAX31865::MAX31865(uint32_t spi_cs, uint32_t spi_mosi, uint32_t spi_miso, uint32_t spi_clk, uint8_t pin_mapping) {
|
2021-12-08 12:40:23 -06:00
|
|
|
cselPin = spi_cs;
|
|
|
|
mosiPin = spi_mosi;
|
|
|
|
misoPin = spi_miso;
|
|
|
|
sclkPin = spi_clk;
|
2021-07-06 19:36:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the interface object using hardware SPI for PIN values which are
|
|
|
|
* larger than 127. If you have PIN values less than or equal to 127 use
|
|
|
|
* the other call for HW SPI.
|
|
|
|
*
|
|
|
|
* @param spi_cs the SPI CS pin to use along with the default SPI device
|
|
|
|
* @param pin_mapping set to 1 for positive pin values
|
|
|
|
*/
|
|
|
|
MAX31865::MAX31865(uint32_t spi_cs, uint8_t pin_mapping) {
|
2021-12-08 12:40:23 -06:00
|
|
|
cselPin = spi_cs;
|
|
|
|
sclkPin = misoPin = mosiPin = -1UL; //-1UL or 0xFFFFFFFF or 4294967295
|
2021-07-06 19:36:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // LARGE_PINMAP
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Instance & Class methods
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the SPI interface and set the number of RTD wires used
|
|
|
|
*
|
2022-06-28 17:53:05 -05:00
|
|
|
* @param wires The number of wires as an enum: MAX31865_2WIRE, MAX31865_3WIRE, or MAX31865_4WIRE.
|
|
|
|
* @param zero_res The resistance of the RTD at 0°C, in ohms.
|
|
|
|
* @param ref_res The resistance of the reference resistor, in ohms.
|
|
|
|
* @param wire_res The resistance of the wire connecting the sensor to the RTD, in ohms.
|
2021-07-06 19:36:41 -05:00
|
|
|
*/
|
2022-06-28 17:53:05 -05:00
|
|
|
void MAX31865::begin(max31865_numwires_t wires, const_float_t zero_res, const_float_t ref_res, const_float_t wire_res) {
|
|
|
|
resNormalizer = 100.0f / zero_res; // reciprocal of resistance, scaled by 100
|
2021-12-08 12:55:09 -06:00
|
|
|
refRes = ref_res;
|
|
|
|
wireRes = wire_res;
|
2021-07-06 19:36:41 -05:00
|
|
|
|
2021-12-08 12:55:09 -06:00
|
|
|
pinMode(cselPin, OUTPUT);
|
|
|
|
digitalWrite(cselPin, HIGH);
|
2021-07-06 19:36:41 -05:00
|
|
|
|
2021-12-08 12:55:09 -06:00
|
|
|
if (sclkPin != TERN(LARGE_PINMAP, -1UL, 255))
|
2022-02-04 19:10:59 -06:00
|
|
|
softSpiInit(); // Define pin modes for Software SPI
|
2021-08-30 22:40:49 -05:00
|
|
|
else {
|
2022-02-04 19:10:59 -06:00
|
|
|
DEBUG_ECHOLNPGM("Init MAX31865 Hardware SPI");
|
2021-11-06 22:53:36 -05:00
|
|
|
SPI.begin(); // Start and configure hardware SPI
|
2021-07-06 19:36:41 -05:00
|
|
|
}
|
|
|
|
|
2021-12-08 12:55:09 -06:00
|
|
|
initFixedFlags(wires);
|
|
|
|
|
2022-02-04 19:10:59 -06:00
|
|
|
DEBUG_ECHOLNPGM("MAX31865 Regs: CFG ", readRegister8(MAX31865_CONFIG_REG),
|
|
|
|
"|RTD ", readRegister16(MAX31865_RTDMSB_REG),
|
|
|
|
"|HTHRS ", readRegister16(MAX31865_HFAULTMSB_REG),
|
|
|
|
"|LTHRS ", readRegister16(MAX31865_LFAULTMSB_REG),
|
|
|
|
"|FLT ", readRegister8(MAX31865_FAULTSTAT_REG));
|
|
|
|
|
|
|
|
// fault detection cycle seems to initialize the sensor better
|
|
|
|
runAutoFaultDetectionCycle(); // also initializes flags
|
|
|
|
|
|
|
|
if (lastFault)
|
|
|
|
SERIAL_ECHOLNPGM("MAX31865 init fault ", lastFault);
|
|
|
|
|
|
|
|
writeRegister16(MAX31865_HFAULTMSB_REG, 0xFFFF);
|
|
|
|
writeRegister16(MAX31865_LFAULTMSB_REG, 0);
|
|
|
|
|
|
|
|
#if ENABLED(MAX31865_USE_AUTO_MODE) // make a proper first read to initialize _lastRead
|
|
|
|
|
|
|
|
uint16_t rtd = readRegister16(MAX31865_RTDMSB_REG);
|
|
|
|
|
|
|
|
#if MAX31865_IGNORE_INITIAL_FAULTY_READS > 0
|
|
|
|
rtd = fixFault(rtd);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (rtd & 1) {
|
|
|
|
lastRead = 0xFFFF; // some invalid value
|
|
|
|
lastFault = readRegister8(MAX31865_FAULTSTAT_REG);
|
|
|
|
clearFault(); // also clears the bias voltage flag, so no further action is required
|
|
|
|
|
|
|
|
DEBUG_ECHOLNPGM("MAX31865 read fault: ", rtd);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
DEBUG_ECHOLNPGM("RTD MSB:", (rtd >> 8), " RTD LSB:", (rtd & 0x00FF));
|
|
|
|
lastRead = rtd;
|
|
|
|
TERN_(MAX31865_USE_READ_ERROR_DETECTION, lastReadStamp = millis());
|
|
|
|
}
|
2021-12-08 12:55:09 -06:00
|
|
|
|
2022-02-04 19:10:59 -06:00
|
|
|
#else
|
2021-12-08 12:55:09 -06:00
|
|
|
|
|
|
|
enableBias();
|
2022-02-04 19:10:59 -06:00
|
|
|
DELAY_US(2000); // according to the datasheet, 10.5τ+1msec (see below)
|
2021-12-08 12:55:09 -06:00
|
|
|
oneShot();
|
2022-02-04 19:10:59 -06:00
|
|
|
DELAY_US(63000);
|
2021-12-08 12:55:09 -06:00
|
|
|
uint16_t rtd = readRegister16(MAX31865_RTDMSB_REG);
|
|
|
|
|
2022-02-04 19:10:59 -06:00
|
|
|
#if MAX31865_IGNORE_INITIAL_FAULTY_READS > 0
|
|
|
|
rtd = fixFault(rtd);
|
|
|
|
#endif
|
|
|
|
|
2021-12-08 12:55:09 -06:00
|
|
|
if (rtd & 1) {
|
|
|
|
lastRead = 0xFFFF; // some invalid value
|
|
|
|
lastFault = readRegister8(MAX31865_FAULTSTAT_REG);
|
|
|
|
clearFault(); // also clears the bias voltage flag, so no further action is required
|
|
|
|
|
|
|
|
DEBUG_ECHOLNPGM("MAX31865 read fault: ", rtd);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
DEBUG_ECHOLNPGM("RTD MSB:", (rtd >> 8), " RTD LSB:", (rtd & 0x00FF));
|
|
|
|
|
|
|
|
resetFlags();
|
|
|
|
|
|
|
|
lastRead = rtd;
|
|
|
|
nextEvent = SETUP_BIAS_VOLTAGE;
|
|
|
|
millis_t now = millis();
|
|
|
|
nextEventStamp = now + MAX31865_MIN_SAMPLING_TIME_MSEC;
|
|
|
|
|
|
|
|
TERN_(MAX31865_USE_READ_ERROR_DETECTION, lastReadStamp = now);
|
|
|
|
}
|
|
|
|
|
2022-02-04 19:10:59 -06:00
|
|
|
#endif // MAX31865_USE_AUTO_MODE
|
2021-12-08 12:55:09 -06:00
|
|
|
|
|
|
|
DEBUG_ECHOLNPGM(
|
|
|
|
TERN(LARGE_PINMAP, "LARGE_PINMAP", "Regular")
|
|
|
|
" begin call with cselPin: ", cselPin,
|
|
|
|
" misoPin: ", misoPin,
|
|
|
|
" sclkPin: ", sclkPin,
|
|
|
|
" mosiPin: ", mosiPin,
|
|
|
|
" config: ", readRegister8(MAX31865_CONFIG_REG)
|
2022-02-04 19:10:59 -06:00
|
|
|
);
|
2021-07-06 19:36:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-08 12:55:09 -06:00
|
|
|
* Return and clear the last fault value
|
2021-07-06 19:36:41 -05:00
|
|
|
*
|
2021-12-08 12:55:09 -06:00
|
|
|
* @return The raw unsigned 8-bit FAULT status register or spike fault
|
2021-07-06 19:36:41 -05:00
|
|
|
*/
|
|
|
|
uint8_t MAX31865::readFault() {
|
2021-12-08 12:55:09 -06:00
|
|
|
uint8_t r = lastFault;
|
|
|
|
lastFault = 0;
|
|
|
|
return r;
|
2021-07-06 19:36:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-08 12:55:09 -06:00
|
|
|
* Clear last fault
|
2021-07-06 19:36:41 -05:00
|
|
|
*/
|
|
|
|
void MAX31865::clearFault() {
|
2021-12-06 20:18:50 -06:00
|
|
|
setConfig(MAX31865_CONFIG_FAULTSTAT, 1);
|
2021-07-06 19:36:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-08 12:55:09 -06:00
|
|
|
* Reset flags
|
2021-07-06 19:36:41 -05:00
|
|
|
*/
|
2021-12-08 12:55:09 -06:00
|
|
|
void MAX31865::resetFlags() {
|
|
|
|
writeRegister8(MAX31865_CONFIG_REG, stdFlags);
|
2021-07-06 19:36:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable the bias voltage on the RTD sensor
|
|
|
|
*/
|
2021-12-08 12:55:09 -06:00
|
|
|
void MAX31865::enableBias() {
|
|
|
|
setConfig(MAX31865_CONFIG_BIAS, 1);
|
2021-07-06 19:36:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start a one-shot temperature reading.
|
|
|
|
*/
|
|
|
|
void MAX31865::oneShot() {
|
2021-12-08 12:55:09 -06:00
|
|
|
setConfig(MAX31865_CONFIG_1SHOT | MAX31865_CONFIG_BIAS, 1);
|
2021-07-06 19:36:41 -05:00
|
|
|
}
|
|
|
|
|
2022-02-04 19:10:59 -06:00
|
|
|
void MAX31865::runAutoFaultDetectionCycle() {
|
|
|
|
writeRegister8(MAX31865_CONFIG_REG, (stdFlags & 0x11) | 0x84 ); // cfg reg = 100X010Xb
|
|
|
|
DELAY_US(600);
|
|
|
|
for (int i = 0; i < 10 && (readRegister8(MAX31865_CONFIG_REG) & 0xC) > 0; i++) DELAY_US(100); // Fault det completes when bits 2 and 3 are zero (or after 10 tries)
|
|
|
|
readFault();
|
|
|
|
clearFault();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a value in the configuration register.
|
|
|
|
*
|
|
|
|
* @param config 8-bit value for the config item
|
|
|
|
* @param enable whether to enable or disable the value
|
|
|
|
*/
|
|
|
|
void MAX31865::setConfig(uint8_t config, bool enable) {
|
|
|
|
uint8_t t = stdFlags;
|
|
|
|
if (enable)
|
|
|
|
t |= config;
|
|
|
|
else
|
|
|
|
t &= ~config;
|
|
|
|
writeRegister8(MAX31865_CONFIG_REG, t);
|
|
|
|
}
|
|
|
|
|
2021-07-06 19:36:41 -05:00
|
|
|
/**
|
2021-12-08 12:55:09 -06:00
|
|
|
* Initialize standard flags with flags that will not change during operation (Hz, polling mode and no. of wires)
|
2021-07-06 19:36:41 -05:00
|
|
|
*
|
|
|
|
* @param wires The number of wires in enum format
|
|
|
|
*/
|
2021-12-08 12:55:09 -06:00
|
|
|
void MAX31865::initFixedFlags(max31865_numwires_t wires) {
|
|
|
|
|
|
|
|
// set config-defined flags (same for all sensors)
|
|
|
|
stdFlags = TERN(MAX31865_50HZ_FILTER, MAX31865_CONFIG_FILT50HZ, MAX31865_CONFIG_FILT60HZ) |
|
2022-02-04 19:10:59 -06:00
|
|
|
TERN(MAX31865_USE_AUTO_MODE, MAX31865_CONFIG_MODEAUTO | MAX31865_CONFIG_BIAS, MAX31865_CONFIG_MODEOFF);
|
2021-12-08 12:55:09 -06:00
|
|
|
|
2021-07-06 19:36:41 -05:00
|
|
|
if (wires == MAX31865_3WIRE)
|
2022-02-04 19:10:59 -06:00
|
|
|
stdFlags |= MAX31865_CONFIG_3WIRE; // 3 wire
|
|
|
|
else
|
|
|
|
stdFlags &= ~MAX31865_CONFIG_3WIRE; // 2 or 4 wire
|
|
|
|
}
|
|
|
|
|
|
|
|
#if MAX31865_IGNORE_INITIAL_FAULTY_READS > 0
|
|
|
|
|
|
|
|
inline uint16_t MAX31865::fixFault(uint16_t rtd) {
|
|
|
|
if (!ignore_faults || !(rtd & 1))
|
|
|
|
return rtd;
|
|
|
|
|
|
|
|
ignore_faults--;
|
|
|
|
clearFault();
|
|
|
|
|
|
|
|
DEBUG_ECHOLNPGM("MAX31865 ignoring fault ", (MAX31865_IGNORE_INITIAL_FAULTY_READS) - ignore_faults);
|
|
|
|
|
|
|
|
return rtd & ~1; // 0xFFFE
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
inline uint16_t MAX31865::readRawImmediate() {
|
|
|
|
uint16_t rtd = readRegister16(MAX31865_RTDMSB_REG);
|
|
|
|
DEBUG_ECHOLNPGM("MAX31865 RTD MSB:", (rtd >> 8), " LSB:", (rtd & 0x00FF));
|
|
|
|
|
|
|
|
#if MAX31865_IGNORE_INITIAL_FAULTY_READS > 0
|
|
|
|
rtd = fixFault(rtd);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (rtd & 1) {
|
|
|
|
lastFault = readRegister8(MAX31865_FAULTSTAT_REG);
|
|
|
|
lastRead |= 1;
|
|
|
|
clearFault(); // also clears the bias voltage flag, so no further action is required
|
|
|
|
DEBUG_ECHOLNPGM("MAX31865 read fault: ", lastFault);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
TERN_(MAX31865_USE_READ_ERROR_DETECTION, const millis_t ms = millis());
|
|
|
|
if (TERN0(MAX31865_USE_READ_ERROR_DETECTION, ABS((int)(lastRead - rtd)) > 500 && PENDING(ms, lastReadStamp + 1000))) {
|
|
|
|
// If 2 readings within 1s differ too much (~20°C) it's a read error.
|
|
|
|
lastFault = 0x01;
|
|
|
|
lastRead |= 1;
|
|
|
|
DEBUG_ECHOLNPGM("MAX31865 read error: ", rtd);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lastRead = rtd;
|
|
|
|
TERN_(MAX31865_USE_READ_ERROR_DETECTION, lastReadStamp = ms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rtd;
|
2021-07-06 19:36:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read the raw 16-bit value from the RTD_REG in one shot mode. This will include
|
|
|
|
* the fault bit, D0.
|
|
|
|
*
|
|
|
|
* @return The raw unsigned 16-bit register value with ERROR bit attached, NOT temperature!
|
|
|
|
*/
|
|
|
|
uint16_t MAX31865::readRaw() {
|
|
|
|
|
2021-12-08 12:55:09 -06:00
|
|
|
#if ENABLED(MAX31865_USE_AUTO_MODE)
|
2021-07-06 19:36:41 -05:00
|
|
|
|
2022-02-04 19:10:59 -06:00
|
|
|
readRawImmediate();
|
2021-07-06 19:36:41 -05:00
|
|
|
|
2021-12-08 12:55:09 -06:00
|
|
|
#else
|
|
|
|
|
2022-02-04 19:10:59 -06:00
|
|
|
const millis_t ms = millis();
|
|
|
|
|
|
|
|
if (PENDING(ms, nextEventStamp)) {
|
2021-12-08 12:55:09 -06:00
|
|
|
DEBUG_ECHOLNPGM("MAX31865 waiting for event ", nextEvent);
|
|
|
|
return lastRead;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (nextEvent) {
|
|
|
|
case SETUP_BIAS_VOLTAGE:
|
|
|
|
enableBias();
|
2022-02-04 19:10:59 -06:00
|
|
|
nextEventStamp = ms + 2; // wait at least 10.5*τ (τ = 100nF*430Ω max for PT100 / 10nF*4.3ΚΩ for PT1000 = 43μsec) + 1msec
|
2021-12-08 12:55:09 -06:00
|
|
|
nextEvent = SETUP_1_SHOT_MODE;
|
2022-01-19 02:13:47 -06:00
|
|
|
DEBUG_ECHOLNPGM("MAX31865 bias voltage enabled");
|
2021-12-08 12:55:09 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SETUP_1_SHOT_MODE:
|
|
|
|
oneShot();
|
2022-02-04 19:10:59 -06:00
|
|
|
nextEventStamp = ms + TERN(MAX31865_50HZ_FILTER, 63, 52); // wait at least 52msec for 60Hz (63msec for 50Hz) before reading RTD register
|
2021-12-08 12:55:09 -06:00
|
|
|
nextEvent = READ_RTD_REG;
|
2022-01-19 02:13:47 -06:00
|
|
|
DEBUG_ECHOLNPGM("MAX31865 1 shot mode enabled");
|
2021-12-08 12:55:09 -06:00
|
|
|
break;
|
|
|
|
|
2022-02-04 19:10:59 -06:00
|
|
|
case READ_RTD_REG:
|
|
|
|
|
|
|
|
if (!(readRawImmediate() & 1)) // if clearFault() was not invoked, need to clear the bias voltage and 1-shot flags
|
2021-12-08 12:55:09 -06:00
|
|
|
resetFlags();
|
|
|
|
|
|
|
|
nextEvent = SETUP_BIAS_VOLTAGE;
|
2022-02-04 19:10:59 -06:00
|
|
|
nextEventStamp = ms + (MAX31865_MIN_SAMPLING_TIME_MSEC); // next step should not occur within less than MAX31865_MIN_SAMPLING_TIME_MSEC from the last one
|
|
|
|
break;
|
2021-12-08 12:55:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2021-07-06 19:36:41 -05:00
|
|
|
|
2021-12-08 12:55:09 -06:00
|
|
|
return lastRead;
|
2021-07-06 19:36:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-08-03 19:02:34 -05:00
|
|
|
* Calculate and return the resistance value of the connected RTD.
|
2021-07-06 19:36:41 -05:00
|
|
|
*
|
|
|
|
* @return The raw RTD resistance value, NOT temperature!
|
|
|
|
*/
|
|
|
|
float MAX31865::readResistance() {
|
|
|
|
// Strip the error bit (D0) and convert to a float ratio.
|
2021-12-08 12:40:23 -06:00
|
|
|
// less precise method: (readRaw() * refRes) >> 16
|
2021-12-08 12:55:09 -06:00
|
|
|
return ((readRaw() * RECIPROCAL(65536.0f)) * refRes - wireRes);
|
2021-07-06 19:36:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read the RTD and pass it to temperature(float) for calculation.
|
|
|
|
*
|
|
|
|
* @return Temperature in C
|
|
|
|
*/
|
|
|
|
float MAX31865::temperature() {
|
|
|
|
return temperature(readResistance());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given the 15-bit ADC value, calculate the resistance and pass it to temperature(float) for calculation.
|
|
|
|
*
|
|
|
|
* @return Temperature in C
|
|
|
|
*/
|
2022-06-28 17:53:05 -05:00
|
|
|
float MAX31865::temperature(const uint16_t adc_val) {
|
2021-12-08 12:55:09 -06:00
|
|
|
return temperature(((adc_val) * RECIPROCAL(32768.0f)) * refRes - wireRes);
|
2021-07-06 19:36:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the temperature in C from the RTD resistance.
|
|
|
|
*
|
2021-12-08 12:40:23 -06:00
|
|
|
* @param rtd_res the resistance value in ohms
|
2022-06-28 17:53:05 -05:00
|
|
|
* @return the temperature in °C
|
2021-07-06 19:36:41 -05:00
|
|
|
*/
|
2021-12-08 12:40:23 -06:00
|
|
|
float MAX31865::temperature(float rtd_res) {
|
2022-06-28 17:53:05 -05:00
|
|
|
|
|
|
|
rtd_res *= resNormalizer; // normalize to 100 ohm
|
|
|
|
|
|
|
|
// Constants for calculating temperature from the measured RTD resistance.
|
|
|
|
// http://www.analog.com/media/en/technical-documentation/application-notes/AN709_0.pdf
|
|
|
|
constexpr float RTD_Z1 = -0.0039083,
|
|
|
|
RTD_Z2 = +1.758480889e-5,
|
|
|
|
RTD_Z3 = -2.31e-8,
|
|
|
|
RTD_Z4 = -1.155e-6;
|
|
|
|
|
|
|
|
// Callender-Van Dusen equation
|
2021-12-08 12:40:23 -06:00
|
|
|
float temp = (RTD_Z1 + sqrt(RTD_Z2 + (RTD_Z3 * rtd_res))) * RECIPROCAL(RTD_Z4);
|
2021-07-06 19:36:41 -05:00
|
|
|
|
|
|
|
//
|
|
|
|
// The previous equation is valid only for temperatures of 0°C and above.
|
|
|
|
// The equation for RRTD(t) that defines negative temperature behavior is a
|
|
|
|
// fourth-order polynomial (after expanding the third term) and is quite
|
|
|
|
// impractical to solve for a single expression of temperature as a function
|
2022-06-28 17:53:05 -05:00
|
|
|
// of resistance. So here we use a Linear Approximation instead.
|
2021-07-06 19:36:41 -05:00
|
|
|
//
|
|
|
|
if (temp < 0) {
|
2022-06-28 17:53:05 -05:00
|
|
|
#ifndef MAX31865_APPROX
|
|
|
|
#define MAX31865_APPROX 5
|
|
|
|
#endif
|
|
|
|
|
|
|
|
constexpr float RTD_C[] = {
|
|
|
|
#if MAX31865_APPROX == 5
|
|
|
|
-242.02, +2.2228, +2.5859e-3, -4.8260e-6, -2.8183e-8, +1.5243e-10
|
|
|
|
#elif MAX31865_APPROX == 4
|
|
|
|
-241.96, +2.2163, +2.8541e-3, -9.9121e-6, -1.7152e-8
|
|
|
|
#elif MAX31865_APPROX == 3
|
|
|
|
-242.09, +2.2276, +2.5178e-3, -5.8620e-6
|
|
|
|
#else
|
|
|
|
-242.97, +2.2838, +1.4727e-3
|
|
|
|
#endif
|
|
|
|
};
|
2021-07-06 19:36:41 -05:00
|
|
|
|
2022-06-28 17:53:05 -05:00
|
|
|
float rpoly = rtd_res;
|
|
|
|
temp = RTD_C[0];
|
|
|
|
temp += rpoly * RTD_C[1];
|
|
|
|
rpoly *= rtd_res; temp += rpoly * RTD_C[2];
|
|
|
|
if (MAX31865_APPROX >= 3) rpoly *= rtd_res; temp += rpoly * RTD_C[3];
|
|
|
|
if (MAX31865_APPROX >= 4) rpoly *= rtd_res; temp += rpoly * RTD_C[4];
|
|
|
|
if (MAX31865_APPROX >= 5) rpoly *= rtd_res; temp += rpoly * RTD_C[5];
|
2021-07-06 19:36:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-02-04 19:10:59 -06:00
|
|
|
* MAX31865 SPI Timing constants
|
|
|
|
* See MAX31865 datasheet (https://datasheets.maximintegrated.com/en/ds/MAX31865.pdf)
|
|
|
|
* All timings in nsec, minimum values.
|
2021-07-06 19:36:41 -05:00
|
|
|
*/
|
2022-02-04 19:10:59 -06:00
|
|
|
|
|
|
|
#define MAX31865_SPI_TIMING_TCC 400 // CS to SCLK setup
|
|
|
|
#define MAX31865_SPI_TIMING_TDC 35 // Data to SCLK setup
|
|
|
|
#define MAX31865_SPI_TIMING_TCL 100 // SCK half period
|
|
|
|
#define MAX31865_SPI_TIMING_TCCH 100 // SCK to CS hold
|
|
|
|
#define MAX31865_SPI_TIMING_TCWH 400 // CS inactive time (min)
|
2021-07-06 19:36:41 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a single byte from the specified register address.
|
|
|
|
*
|
|
|
|
* @param addr the register address
|
|
|
|
* @return the register contents
|
|
|
|
*/
|
|
|
|
uint8_t MAX31865::readRegister8(uint8_t addr) {
|
|
|
|
uint8_t ret = 0;
|
|
|
|
readRegisterN(addr, &ret, 1);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read two bytes: 1 from the specified register address, and 1 from the next address.
|
|
|
|
*
|
|
|
|
* @param addr the first register address
|
|
|
|
* @return both register contents as a single 16-bit int
|
|
|
|
*/
|
|
|
|
uint16_t MAX31865::readRegister16(uint8_t addr) {
|
2021-12-08 12:55:09 -06:00
|
|
|
uint8_t buffer[2] = { 0 };
|
2021-07-06 19:36:41 -05:00
|
|
|
readRegisterN(addr, buffer, 2);
|
2021-12-08 12:55:09 -06:00
|
|
|
return uint16_t(buffer[0]) << 8 | buffer[1];
|
2021-07-06 19:36:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read +n+ bytes from a specified address into +buffer+. Set D7 to 0 to specify a read.
|
|
|
|
*
|
|
|
|
* @param addr the first register address
|
|
|
|
* @param buffer storage for the read bytes
|
|
|
|
* @param n the number of bytes to read
|
|
|
|
*/
|
|
|
|
void MAX31865::readRegisterN(uint8_t addr, uint8_t buffer[], uint8_t n) {
|
|
|
|
|
2022-02-04 19:10:59 -06:00
|
|
|
addr &= 0x7F; // make sure top bit is not set
|
2021-11-06 22:53:36 -05:00
|
|
|
|
2022-02-04 19:10:59 -06:00
|
|
|
spiBeginTransaction();
|
2021-11-06 22:53:36 -05:00
|
|
|
spiTransfer(addr);
|
2021-07-06 19:36:41 -05:00
|
|
|
|
|
|
|
while (n--) {
|
2021-11-06 22:53:36 -05:00
|
|
|
buffer[0] = spiTransfer(0xFF);
|
2021-07-06 19:36:41 -05:00
|
|
|
buffer++;
|
|
|
|
}
|
|
|
|
|
2022-02-04 19:10:59 -06:00
|
|
|
spiEndTransaction();
|
|
|
|
}
|
2021-07-06 19:36:41 -05:00
|
|
|
|
2022-02-04 19:10:59 -06:00
|
|
|
void MAX31865::writeRegister16(uint8_t addr, uint16_t data) {
|
|
|
|
spiBeginTransaction();
|
|
|
|
spiTransfer(addr | 0x80); // make sure top bit is set
|
|
|
|
spiTransfer(data >> 8);
|
|
|
|
spiTransfer(data & 0xFF);
|
|
|
|
spiEndTransaction();
|
2021-07-06 19:36:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write an 8-bit value to a register. Set D7 to 1 to specify a write.
|
|
|
|
*
|
|
|
|
* @param addr the address to write to
|
|
|
|
* @param data the data to write
|
|
|
|
*/
|
|
|
|
void MAX31865::writeRegister8(uint8_t addr, uint8_t data) {
|
2022-02-04 19:10:59 -06:00
|
|
|
spiBeginTransaction();
|
|
|
|
spiTransfer(addr | 0x80); // make sure top bit is set
|
|
|
|
spiTransfer(data);
|
|
|
|
spiEndTransaction();
|
|
|
|
}
|
2021-07-06 19:36:41 -05:00
|
|
|
|
2022-02-04 19:10:59 -06:00
|
|
|
void MAX31865::spiBeginTransaction() {
|
|
|
|
digitalWrite(sclkPin, LOW); // ensure CPOL0
|
|
|
|
DELAY_NS_VAR(MAX31865_SPI_TIMING_TCWH); // ensure minimum time of CS inactivity after previous operation
|
2021-12-08 12:55:09 -06:00
|
|
|
digitalWrite(cselPin, LOW);
|
2022-02-04 19:10:59 -06:00
|
|
|
DELAY_NS_VAR(MAX31865_SPI_TIMING_TCC);
|
2021-07-06 19:36:41 -05:00
|
|
|
|
2022-02-04 19:10:59 -06:00
|
|
|
if (sclkPin == TERN(LARGE_PINMAP, -1UL, 255))
|
|
|
|
SPI.beginTransaction(spiConfig);
|
|
|
|
else
|
|
|
|
digitalWrite(sclkPin, HIGH);
|
|
|
|
}
|
2021-07-06 19:36:41 -05:00
|
|
|
|
2022-02-04 19:10:59 -06:00
|
|
|
void MAX31865::spiEndTransaction() {
|
2021-12-08 12:55:09 -06:00
|
|
|
if (sclkPin == TERN(LARGE_PINMAP, -1UL, 255))
|
2021-07-06 19:36:41 -05:00
|
|
|
SPI.endTransaction();
|
2022-02-04 19:10:59 -06:00
|
|
|
else
|
|
|
|
digitalWrite(sclkPin, LOW);
|
|
|
|
|
|
|
|
DELAY_NS_VAR(MAX31865_SPI_TIMING_TCCH);
|
2021-07-06 19:36:41 -05:00
|
|
|
|
2021-12-08 12:55:09 -06:00
|
|
|
digitalWrite(cselPin, HIGH);
|
2021-07-06 19:36:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transfer SPI data +x+ and read the response. From the datasheet...
|
|
|
|
* Input data (SDI) is latched on the internal strobe edge and output data (SDO) is
|
|
|
|
* shifted out on the shift edge. There is one clock for each bit transferred.
|
|
|
|
* Address and data bits are transferred in groups of eight, MSB first.
|
|
|
|
*
|
|
|
|
* @param x an 8-bit chunk of data to write
|
|
|
|
* @return the 8-bit response
|
|
|
|
*/
|
2021-11-06 22:53:36 -05:00
|
|
|
uint8_t MAX31865::spiTransfer(uint8_t x) {
|
2021-12-08 12:55:09 -06:00
|
|
|
if (sclkPin == TERN(LARGE_PINMAP, -1UL, 255))
|
2021-07-06 19:36:41 -05:00
|
|
|
return SPI.transfer(x);
|
|
|
|
|
2022-02-04 19:10:59 -06:00
|
|
|
uint8_t reply = 0;
|
|
|
|
for (int i = 7; i >= 0; i--) {
|
|
|
|
digitalWrite(mosiPin, x & _BV(i));
|
|
|
|
DELAY_NS_VAR(MAX31865_SPI_TIMING_TDC);
|
|
|
|
digitalWrite(sclkPin, LOW);
|
|
|
|
DELAY_NS_VAR(MAX31865_SPI_TIMING_TCL - MAX31865_SPI_TIMING_TDC);
|
|
|
|
reply <<= 1;
|
|
|
|
if (digitalRead(misoPin)) reply |= 1;
|
|
|
|
DELAY_NS_VAR(MAX31865_SPI_TIMING_TDC);
|
|
|
|
digitalWrite(sclkPin, HIGH);
|
|
|
|
DELAY_NS_VAR(MAX31865_SPI_TIMING_TCL - MAX31865_SPI_TIMING_TDC);
|
|
|
|
}
|
|
|
|
return reply;
|
2021-11-06 22:53:36 -05:00
|
|
|
}
|
2021-08-30 22:40:49 -05:00
|
|
|
|
2022-02-04 19:10:59 -06:00
|
|
|
void MAX31865::softSpiInit() {
|
2021-12-08 12:55:09 -06:00
|
|
|
DEBUG_ECHOLNPGM("Initializing MAX31865 Software SPI");
|
2022-02-04 19:10:59 -06:00
|
|
|
pinMode(sclkPin, OUTPUT);
|
|
|
|
digitalWrite(sclkPin, LOW);
|
|
|
|
pinMode(mosiPin, OUTPUT);
|
|
|
|
pinMode(misoPin, INPUT);
|
2021-07-06 19:36:41 -05:00
|
|
|
}
|
|
|
|
|
2021-08-30 22:40:49 -05:00
|
|
|
#endif // HAS_MAX31865 && !USE_ADAFRUIT_MAX31865
|