|
@ -48,20 +48,17 @@ |
|
|
|
|
|
|
|
|
#if HAS_MAX31865 && !USE_ADAFRUIT_MAX31865 |
|
|
#if HAS_MAX31865 && !USE_ADAFRUIT_MAX31865 |
|
|
|
|
|
|
|
|
//#include <SoftwareSPI.h> // TODO: switch to SPIclass/SoftSPI
|
|
|
|
|
|
#include "MAX31865.h" |
|
|
#include "MAX31865.h" |
|
|
|
|
|
|
|
|
|
|
|
#ifdef TARGET_LPC1768 |
|
|
|
|
|
#include <SoftwareSPI.h> |
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
// The maximum speed the MAX31865 can do is 5 MHz
|
|
|
// The maximum speed the MAX31865 can do is 5 MHz
|
|
|
SPISettings MAX31865::spiConfig = SPISettings( |
|
|
SPISettings MAX31865::spiConfig = SPISettings( |
|
|
#if defined(TARGET_LPC1768) |
|
|
TERN(TARGET_LPC1768, SPI_QUARTER_SPEED, TERN(ARDUINO_ARCH_STM32, SPI_CLOCK_DIV4, 500000)), |
|
|
SPI_QUARTER_SPEED |
|
|
MSBFIRST, |
|
|
#elif defined(ARDUINO_ARCH_STM32) |
|
|
SPI_MODE1 // CPOL0 CPHA1
|
|
|
SPI_CLOCK_DIV4 |
|
|
|
|
|
#else |
|
|
|
|
|
500000 |
|
|
|
|
|
#endif |
|
|
|
|
|
, MSBFIRST |
|
|
|
|
|
, SPI_MODE_1 // CPOL0 CPHA1
|
|
|
|
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
#ifndef LARGE_PINMAP |
|
|
#ifndef LARGE_PINMAP |
|
@ -93,7 +90,7 @@ SPISettings MAX31865::spiConfig = SPISettings( |
|
|
_sclk = _miso = _mosi = -1; |
|
|
_sclk = _miso = _mosi = -1; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
#else |
|
|
#else // LARGE_PINMAP
|
|
|
|
|
|
|
|
|
/**
|
|
|
/**
|
|
|
* Create the interface object using software (bitbang) SPI for PIN values |
|
|
* Create the interface object using software (bitbang) SPI for PIN values |
|
@ -106,9 +103,7 @@ SPISettings MAX31865::spiConfig = SPISettings( |
|
|
* @param spi_clk the SPI clock pin to use |
|
|
* @param spi_clk the SPI clock pin to use |
|
|
* @param pin_mapping set to 1 for positive pin values |
|
|
* @param pin_mapping set to 1 for positive pin values |
|
|
*/ |
|
|
*/ |
|
|
MAX31865::MAX31865(uint32_t spi_cs, uint32_t spi_mosi, |
|
|
MAX31865::MAX31865(uint32_t spi_cs, uint32_t spi_mosi, uint32_t spi_miso, uint32_t spi_clk, uint8_t pin_mapping) { |
|
|
uint32_t spi_miso, uint32_t spi_clk, |
|
|
|
|
|
uint8_t pin_mapping) { |
|
|
|
|
|
_cs = spi_cs; |
|
|
_cs = spi_cs; |
|
|
_mosi = spi_mosi; |
|
|
_mosi = spi_mosi; |
|
|
_miso = spi_miso; |
|
|
_miso = spi_miso; |
|
@ -130,14 +125,12 @@ SPISettings MAX31865::spiConfig = SPISettings( |
|
|
|
|
|
|
|
|
#endif // LARGE_PINMAP
|
|
|
#endif // LARGE_PINMAP
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
/**
|
|
|
* |
|
|
* |
|
|
* Instance & Class methods |
|
|
* Instance & Class methods |
|
|
* |
|
|
* |
|
|
*/ |
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
/**
|
|
|
* Initialize the SPI interface and set the number of RTD wires used |
|
|
* Initialize the SPI interface and set the number of RTD wires used |
|
|
* |
|
|
* |
|
@ -152,22 +145,13 @@ void MAX31865::begin(max31865_numwires_t wires, float zero, float ref) { |
|
|
OUT_WRITE(_cs, HIGH); |
|
|
OUT_WRITE(_cs, HIGH); |
|
|
|
|
|
|
|
|
if (_sclk != TERN(LARGE_PINMAP, -1UL, -1)) { |
|
|
if (_sclk != TERN(LARGE_PINMAP, -1UL, -1)) { |
|
|
// Define pin modes for Software SPI
|
|
|
softSpiBegin(SPI_QUARTER_SPEED); // Define pin modes for Software SPI
|
|
|
#ifdef MAX31865_DEBUG |
|
|
|
|
|
SERIAL_ECHOLN("Initializing MAX31865 Software SPI"); |
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
OUT_WRITE(_sclk, LOW); |
|
|
|
|
|
SET_OUTPUT(_mosi); |
|
|
|
|
|
SET_INPUT(_miso); |
|
|
|
|
|
} |
|
|
} |
|
|
else { |
|
|
else { |
|
|
// Start and configure hardware SPI
|
|
|
|
|
|
#ifdef MAX31865_DEBUG |
|
|
#ifdef MAX31865_DEBUG |
|
|
SERIAL_ECHOLN("Initializing MAX31865 Hardware SPI"); |
|
|
SERIAL_ECHOLNPGM("Initializing MAX31865 Hardware SPI"); |
|
|
#endif |
|
|
#endif |
|
|
|
|
|
SPI.begin(); // Start and configure hardware SPI
|
|
|
SPI.begin(); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
setWires(wires); |
|
|
setWires(wires); |
|
@ -176,25 +160,15 @@ void MAX31865::begin(max31865_numwires_t wires, float zero, float ref) { |
|
|
clearFault(); |
|
|
clearFault(); |
|
|
|
|
|
|
|
|
#ifdef MAX31865_DEBUG_SPI |
|
|
#ifdef MAX31865_DEBUG_SPI |
|
|
#ifndef LARGE_PINMAP |
|
|
SERIAL_ECHOLNPGM( |
|
|
SERIAL_ECHOLNPGM( |
|
|
TERN(LARGE_PINMAP, "LARGE_PINMAP", "Regular") |
|
|
"Regular begin call with _cs: ", _cs, |
|
|
" begin call with _cs: ", _cs, |
|
|
" _miso: ", _miso, |
|
|
" _miso: ", _miso, |
|
|
" _sclk: ", _sclk, |
|
|
" _sclk: ", _sclk, |
|
|
" _mosi: ", _mosi |
|
|
" _mosi: ", _mosi, |
|
|
); |
|
|
" config: ", readRegister8(MAX31856_CONFIG_REG) |
|
|
#else |
|
|
); |
|
|
SERIAL_ECHOLNPGM( |
|
|
#endif |
|
|
"LARGE_PINMAP begin call with _cs: ", _cs, |
|
|
|
|
|
" _miso: ", _miso, |
|
|
|
|
|
" _sclk: ", _sclk, |
|
|
|
|
|
" _mosi: ", _mosi |
|
|
|
|
|
); |
|
|
|
|
|
#endif // LARGE_PINMAP
|
|
|
|
|
|
|
|
|
|
|
|
SERIAL_ECHOLNPGM("config: ", readRegister8(MAX31856_CONFIG_REG)); |
|
|
|
|
|
SERIAL_EOL(); |
|
|
|
|
|
#endif // MAX31865_DEBUG_SPI
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/**
|
|
|
/**
|
|
@ -371,7 +345,6 @@ float MAX31865::temperature(float Rrtd) { |
|
|
// private:
|
|
|
// private:
|
|
|
//
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
/**
|
|
|
* Set a value in the configuration register. |
|
|
* Set a value in the configuration register. |
|
|
* |
|
|
* |
|
@ -432,10 +405,15 @@ void MAX31865::readRegisterN(uint8_t addr, uint8_t buffer[], uint8_t n) { |
|
|
WRITE(_sclk, LOW); |
|
|
WRITE(_sclk, LOW); |
|
|
|
|
|
|
|
|
WRITE(_cs, LOW); |
|
|
WRITE(_cs, LOW); |
|
|
spixfer(addr); |
|
|
|
|
|
|
|
|
#ifdef TARGET_LPC1768 |
|
|
|
|
|
DELAY_CYCLES(_spi_speed); |
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
spiTransfer(addr); |
|
|
|
|
|
|
|
|
while (n--) { |
|
|
while (n--) { |
|
|
buffer[0] = spixfer(0xFF); |
|
|
buffer[0] = spiTransfer(0xFF); |
|
|
#ifdef MAX31865_DEBUG_SPI |
|
|
#ifdef MAX31865_DEBUG_SPI |
|
|
SERIAL_ECHOLNPGM("buffer read ", n, " data: ", buffer[0]); |
|
|
SERIAL_ECHOLNPGM("buffer read ", n, " data: ", buffer[0]); |
|
|
#endif |
|
|
#endif |
|
@ -462,8 +440,12 @@ void MAX31865::writeRegister8(uint8_t addr, uint8_t data) { |
|
|
|
|
|
|
|
|
WRITE(_cs, LOW); |
|
|
WRITE(_cs, LOW); |
|
|
|
|
|
|
|
|
spixfer(addr | 0x80); // make sure top bit is set
|
|
|
#ifdef TARGET_LPC1768 |
|
|
spixfer(data); |
|
|
DELAY_CYCLES(_spi_speed); |
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
spiTransfer(addr | 0x80); // make sure top bit is set
|
|
|
|
|
|
spiTransfer(data); |
|
|
|
|
|
|
|
|
if (_sclk == TERN(LARGE_PINMAP, -1UL, -1)) |
|
|
if (_sclk == TERN(LARGE_PINMAP, -1UL, -1)) |
|
|
SPI.endTransaction(); |
|
|
SPI.endTransaction(); |
|
@ -480,21 +462,38 @@ void MAX31865::writeRegister8(uint8_t addr, uint8_t data) { |
|
|
* @param x an 8-bit chunk of data to write |
|
|
* @param x an 8-bit chunk of data to write |
|
|
* @return the 8-bit response |
|
|
* @return the 8-bit response |
|
|
*/ |
|
|
*/ |
|
|
uint8_t MAX31865::spixfer(uint8_t x) { |
|
|
uint8_t MAX31865::spiTransfer(uint8_t x) { |
|
|
if (_sclk == TERN(LARGE_PINMAP, -1UL, -1)) |
|
|
if (_sclk == TERN(LARGE_PINMAP, -1UL, -1)) |
|
|
return SPI.transfer(x); |
|
|
return SPI.transfer(x); |
|
|
|
|
|
|
|
|
uint8_t reply = 0; |
|
|
#ifdef TARGET_LPC1768 |
|
|
for (int i = 7; i >= 0; i--) { |
|
|
return swSpiTransfer(x, _spi_speed, _sclk, _miso, _mosi); |
|
|
reply <<= 1; |
|
|
#else |
|
|
WRITE(_sclk, HIGH); |
|
|
uint8_t reply = 0; |
|
|
WRITE(_mosi, x & (1 << i)); |
|
|
for (int i = 7; i >= 0; i--) { |
|
|
WRITE(_sclk, LOW); |
|
|
WRITE(_sclk, HIGH); DELAY_NS_VAR(_spi_delay); |
|
|
if (READ(_miso)) |
|
|
reply <<= 1; |
|
|
reply |= 1; |
|
|
WRITE(_mosi, x & _BV(i)); DELAY_NS_VAR(_spi_delay); |
|
|
} |
|
|
if (READ(_miso)) reply |= 1; |
|
|
|
|
|
WRITE(_sclk, LOW); DELAY_NS_VAR(_spi_delay); |
|
|
|
|
|
} |
|
|
|
|
|
return reply; |
|
|
|
|
|
#endif |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return reply; |
|
|
void MAX31865::softSpiBegin(const uint8_t spi_speed) { |
|
|
|
|
|
#ifdef MAX31865_DEBUG |
|
|
|
|
|
SERIAL_ECHOLNPGM("Initializing MAX31865 Software SPI"); |
|
|
|
|
|
#endif |
|
|
|
|
|
#ifdef TARGET_LPC1768 |
|
|
|
|
|
swSpiBegin(_sclk, _miso, _mosi); |
|
|
|
|
|
_spi_speed = swSpiInit(spi_speed, _sclk, _mosi); |
|
|
|
|
|
#else |
|
|
|
|
|
_spi_delay = (100UL << spi_speed) / 3; // Calculate delay in ns. Top speed is ~10MHz, or 100ns delay between bits.
|
|
|
|
|
|
OUT_WRITE(_sclk, LOW); |
|
|
|
|
|
SET_OUTPUT(_mosi); |
|
|
|
|
|
SET_INPUT(_miso); |
|
|
|
|
|
#endif |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
#endif // HAS_MAX31865 && !USE_ADAFRUIT_MAX31865
|
|
|
#endif // HAS_MAX31865 && !USE_ADAFRUIT_MAX31865
|
|
|