|
@@ -48,20 +48,17 @@
|
|
|
|
|
|
#if HAS_MAX31865 && !USE_ADAFRUIT_MAX31865
|
|
|
|
|
|
-//#include <SoftwareSPI.h> // TODO: switch to SPIclass/SoftSPI
|
|
|
#include "MAX31865.h"
|
|
|
|
|
|
+#ifdef TARGET_LPC1768
|
|
|
+ #include <SoftwareSPI.h>
|
|
|
+#endif
|
|
|
+
|
|
|
// The maximum speed the MAX31865 can do is 5 MHz
|
|
|
SPISettings MAX31865::spiConfig = SPISettings(
|
|
|
- #if defined(TARGET_LPC1768)
|
|
|
- SPI_QUARTER_SPEED
|
|
|
- #elif defined(ARDUINO_ARCH_STM32)
|
|
|
- SPI_CLOCK_DIV4
|
|
|
- #else
|
|
|
- 500000
|
|
|
- #endif
|
|
|
- , MSBFIRST
|
|
|
- , SPI_MODE_1 // CPOL0 CPHA1
|
|
|
+ TERN(TARGET_LPC1768, SPI_QUARTER_SPEED, TERN(ARDUINO_ARCH_STM32, SPI_CLOCK_DIV4, 500000)),
|
|
|
+ MSBFIRST,
|
|
|
+ SPI_MODE1 // CPOL0 CPHA1
|
|
|
);
|
|
|
|
|
|
#ifndef LARGE_PINMAP
|
|
@@ -93,7 +90,7 @@ SPISettings MAX31865::spiConfig = SPISettings(
|
|
|
_sclk = _miso = _mosi = -1;
|
|
|
}
|
|
|
|
|
|
-#else
|
|
|
+#else // LARGE_PINMAP
|
|
|
|
|
|
/**
|
|
|
* 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 pin_mapping set to 1 for positive pin values
|
|
|
*/
|
|
|
- MAX31865::MAX31865(uint32_t spi_cs, uint32_t spi_mosi,
|
|
|
- uint32_t spi_miso, uint32_t spi_clk,
|
|
|
- uint8_t pin_mapping) {
|
|
|
+ MAX31865::MAX31865(uint32_t spi_cs, uint32_t spi_mosi, uint32_t spi_miso, uint32_t spi_clk, uint8_t pin_mapping) {
|
|
|
_cs = spi_cs;
|
|
|
_mosi = spi_mosi;
|
|
|
_miso = spi_miso;
|
|
@@ -130,14 +125,12 @@ SPISettings MAX31865::spiConfig = SPISettings(
|
|
|
|
|
|
#endif // LARGE_PINMAP
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
*
|
|
|
* Instance & Class methods
|
|
|
*
|
|
|
*/
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 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);
|
|
|
|
|
|
if (_sclk != TERN(LARGE_PINMAP, -1UL, -1)) {
|
|
|
- // 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);
|
|
|
+ softSpiBegin(SPI_QUARTER_SPEED); // Define pin modes for Software SPI
|
|
|
}
|
|
|
else {
|
|
|
- // Start and configure hardware SPI
|
|
|
#ifdef MAX31865_DEBUG
|
|
|
- SERIAL_ECHOLN("Initializing MAX31865 Hardware SPI");
|
|
|
+ SERIAL_ECHOLNPGM("Initializing MAX31865 Hardware SPI");
|
|
|
#endif
|
|
|
-
|
|
|
- SPI.begin();
|
|
|
+ SPI.begin(); // Start and configure hardware SPI
|
|
|
}
|
|
|
|
|
|
setWires(wires);
|
|
@@ -176,25 +160,15 @@ void MAX31865::begin(max31865_numwires_t wires, float zero, float ref) {
|
|
|
clearFault();
|
|
|
|
|
|
#ifdef MAX31865_DEBUG_SPI
|
|
|
- #ifndef LARGE_PINMAP
|
|
|
- SERIAL_ECHOLNPGM(
|
|
|
- "Regular begin call with _cs: ", _cs,
|
|
|
- " _miso: ", _miso,
|
|
|
- " _sclk: ", _sclk,
|
|
|
- " _mosi: ", _mosi
|
|
|
- );
|
|
|
- #else
|
|
|
- SERIAL_ECHOLNPGM(
|
|
|
- "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
|
|
|
+ SERIAL_ECHOLNPGM(
|
|
|
+ TERN(LARGE_PINMAP, "LARGE_PINMAP", "Regular")
|
|
|
+ " begin call with _cs: ", _cs,
|
|
|
+ " _miso: ", _miso,
|
|
|
+ " _sclk: ", _sclk,
|
|
|
+ " _mosi: ", _mosi,
|
|
|
+ " config: ", readRegister8(MAX31856_CONFIG_REG)
|
|
|
+ );
|
|
|
+ #endif
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -371,7 +345,6 @@ float MAX31865::temperature(float Rrtd) {
|
|
|
// private:
|
|
|
//
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 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(_cs, LOW);
|
|
|
- spixfer(addr);
|
|
|
+
|
|
|
+ #ifdef TARGET_LPC1768
|
|
|
+ DELAY_CYCLES(_spi_speed);
|
|
|
+ #endif
|
|
|
+
|
|
|
+ spiTransfer(addr);
|
|
|
|
|
|
while (n--) {
|
|
|
- buffer[0] = spixfer(0xFF);
|
|
|
+ buffer[0] = spiTransfer(0xFF);
|
|
|
#ifdef MAX31865_DEBUG_SPI
|
|
|
SERIAL_ECHOLNPGM("buffer read ", n, " data: ", buffer[0]);
|
|
|
#endif
|
|
@@ -462,8 +440,12 @@ void MAX31865::writeRegister8(uint8_t addr, uint8_t data) {
|
|
|
|
|
|
WRITE(_cs, LOW);
|
|
|
|
|
|
- spixfer(addr | 0x80); // make sure top bit is set
|
|
|
- spixfer(data);
|
|
|
+ #ifdef TARGET_LPC1768
|
|
|
+ DELAY_CYCLES(_spi_speed);
|
|
|
+ #endif
|
|
|
+
|
|
|
+ spiTransfer(addr | 0x80); // make sure top bit is set
|
|
|
+ spiTransfer(data);
|
|
|
|
|
|
if (_sclk == TERN(LARGE_PINMAP, -1UL, -1))
|
|
|
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
|
|
|
* @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))
|
|
|
return SPI.transfer(x);
|
|
|
|
|
|
- uint8_t reply = 0;
|
|
|
- for (int i = 7; i >= 0; i--) {
|
|
|
- reply <<= 1;
|
|
|
- WRITE(_sclk, HIGH);
|
|
|
- WRITE(_mosi, x & (1 << i));
|
|
|
- WRITE(_sclk, LOW);
|
|
|
- if (READ(_miso))
|
|
|
- reply |= 1;
|
|
|
- }
|
|
|
+ #ifdef TARGET_LPC1768
|
|
|
+ return swSpiTransfer(x, _spi_speed, _sclk, _miso, _mosi);
|
|
|
+ #else
|
|
|
+ uint8_t reply = 0;
|
|
|
+ for (int i = 7; i >= 0; i--) {
|
|
|
+ WRITE(_sclk, HIGH); DELAY_NS_VAR(_spi_delay);
|
|
|
+ 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
|