diff --git a/Marlin/src/libs/MAX31865.cpp b/Marlin/src/libs/MAX31865.cpp index 2ceb8a98cb2e..f08e4f9fd892 100644 --- a/Marlin/src/libs/MAX31865.cpp +++ b/Marlin/src/libs/MAX31865.cpp @@ -51,17 +51,15 @@ //#include // TODO: switch to SPIclass/SoftSPI #include "MAX31865.h" +#ifdef TARGET_LPC1768 + #include +#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_MODE1 // 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 +91,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 +104,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 +126,12 @@ SPISettings MAX31865::spiConfig = SPISettings( #endif // LARGE_PINMAP - /** * * Instance & Class methods * */ - /** * Initialize the SPI interface and set the number of RTD wires used * @@ -152,18 +146,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 - softSpiBegin(); - } else { - // start and configure hardware SPI + softSpiBegin(); // Define pin modes for Software SPI + } + else { #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); @@ -172,25 +161,15 @@ void MAX31865::begin(max31865_numwires_t wires, float zero, float ref) { clearFault(); #ifdef MAX31865_DEBUG_SPI - #ifndef LARGE_PINMAP - SERIAL_ECHOLNPAIR( - "Regular begin call with _cs: ", _cs, - " _miso: ", _miso, - " _sclk: ", _sclk, - " _mosi: ", _mosi - ); - #else - SERIAL_ECHOLNPAIR( - "LARGE_PINMAP begin call with _cs: ", _cs, - " _miso: ", _miso, - " _sclk: ", _sclk, - " _mosi: ", _mosi - ); - #endif // LARGE_PINMAP - - SERIAL_ECHOLNPAIR("config: ", readRegister8(MAX31856_CONFIG_REG)); - SERIAL_EOL(); - #endif // MAX31865_DEBUG_SPI + SERIAL_ECHOLNPAIR( + TERN(LARGE_PINMAP, "LARGE_PINMAP", "Regular") + " begin call with _cs: ", _cs, + " _miso: ", _miso, + " _sclk: ", _sclk, + " _mosi: ", _mosi, + " config: ", readRegister8(MAX31856_CONFIG_REG) + ); + #endif } /** @@ -367,7 +346,6 @@ float MAX31865::temperature(float Rrtd) { // private: // - /** * Set a value in the configuration register. * @@ -483,41 +461,34 @@ uint8_t MAX31865::spixfer(uint8_t x) { return softSpiTransfer(x); } -#endif // HAS_MAX31865 && !LIB_USR_MAX31865 - -#ifdef TARGET_LPC1768 - -#include - -void MAX31865::softSpiBegin() { - swSpiBegin(_sclk, _miso, _mosi); - _spi_speed = swSpiInit(SPI_QUARTER_SPEED, _sclk, _mosi); -} - -uint8_t MAX31865::softSpiTransfer(uint8_t x) { - return swSpiTransfer(x, _spi_speed, _sclk, _miso, _mosi); -} - -#else - void MAX31865::softSpiBegin() { + #ifdef MAX31865_DEBUG + SERIAL_ECHOLNPGM("Initializing MAX31865 Software SPI"); + #endif + #ifdef TARGET_LPC1768 + swSpiBegin(_sclk, _miso, _mosi); + _spi_speed = swSpiInit(SPI_QUARTER_SPEED, _sclk, _mosi); + #else OUT_WRITE(_sclk, LOW); SET_OUTPUT(_mosi); SET_INPUT(_miso); + #endif } uint8_t MAX31865::softSpiTransfer(uint8_t 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; - } - - return reply; + #ifdef TARGET_LPC1768 + return swSpiTransfer(x, _spi_speed, _sclk, _miso, _mosi); + #else + uint8_t reply = 0; + for (int i = 7; i >= 0; i--) { + reply <<= 1; + WRITE(_sclk, HIGH); // TODO: Delay? + WRITE(_mosi, x & (1 << i)); // TODO: Delay? + WRITE(_sclk, LOW); // TODO: Delay? + if (READ(_miso)) reply |= 1; + } + return reply; + #endif } -#endif +#endif // HAS_MAX31865 && !USE_ADAFRUIT_MAX31865