diff --git a/Marlin/src/libs/MAX31865.cpp b/Marlin/src/libs/MAX31865.cpp index f08e4f9fd892..5b09450a8799 100644 --- a/Marlin/src/libs/MAX31865.cpp +++ b/Marlin/src/libs/MAX31865.cpp @@ -51,10 +51,6 @@ //#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( TERN(TARGET_LPC1768, SPI_QUARTER_SPEED, TERN(ARDUINO_ARCH_STM32, SPI_CLOCK_DIV4, 500000)), @@ -146,7 +142,7 @@ void MAX31865::begin(max31865_numwires_t wires, float zero, float ref) { OUT_WRITE(_cs, HIGH); if (_sclk != TERN(LARGE_PINMAP, -1UL, -1)) { - softSpiBegin(); // Define pin modes for Software SPI + softSpiBegin(SPI_QUARTER_SPEED); // Define pin modes for Software SPI } else { #ifdef MAX31865_DEBUG @@ -461,34 +457,35 @@ uint8_t MAX31865::spixfer(uint8_t x) { return softSpiTransfer(x); } -void MAX31865::softSpiBegin() { +void MAX31865::softSpiBegin(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_QUARTER_SPEED, _sclk, _mosi); - #else - OUT_WRITE(_sclk, LOW); - SET_OUTPUT(_mosi); - SET_INPUT(_miso); - #endif + + // Calculate the actual clock speed + uint8_t target_clock_speed = 10000000/(2^spi_speed); + + // Calculate delay in ns + _spi_speed = 1000000000/target_clock_speed; + + OUT_WRITE(_sclk, LOW); + SET_OUTPUT(_mosi); + SET_INPUT(_miso); } -uint8_t MAX31865::softSpiTransfer(uint8_t x) { - #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 +uint8_t MAX31865::softSpiTransfer(uint8_t x) { + uint8_t reply = 0; + for (int i = 7; i >= 0; i--) { + WRITE(_mosi, x & (1 << i)); + DELAY_NS(_spi_speed); + WRITE(_sclk, HIGH); + DELAY_NS(_spi_speed); + reply <<= 1; + if (READ(_miso)) reply |= 1; + WRITE(_sclk, LOW); + DELAY_NS(_spi_speed); + } + return reply; } #endif // HAS_MAX31865 && !USE_ADAFRUIT_MAX31865 diff --git a/Marlin/src/libs/MAX31865.h b/Marlin/src/libs/MAX31865.h index f5d3aacaad6b..3345357ea62a 100644 --- a/Marlin/src/libs/MAX31865.h +++ b/Marlin/src/libs/MAX31865.h @@ -90,9 +90,7 @@ class MAX31865 { static SPISettings spiConfig; TERN(LARGE_PINMAP, uint32_t, uint8_t) _sclk, _miso, _mosi, _cs; - #ifdef TARGET_LPC1768 - uint8_t _spi_speed; - #endif + uint8_t _spi_speed; float Rzero, Rref; void setConfig(uint8_t config, bool enable); @@ -104,7 +102,7 @@ class MAX31865 { void writeRegister8(uint8_t addr, uint8_t reg); uint8_t spixfer(uint8_t addr); - void softSpiBegin(); + void softSpiBegin(uint8_t spi_speed); uint8_t softSpiTransfer(uint8_t x);