Skip to content

Commit

Permalink
Remove <SoftwareSPI.h> and use DELAY_NS
Browse files Browse the repository at this point in the history
  • Loading branch information
jinhong- committed Sep 3, 2021
1 parent 1c4894d commit 9854cf4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 32 deletions.
53 changes: 25 additions & 28 deletions Marlin/src/libs/MAX31865.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@
//#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(
TERN(TARGET_LPC1768, SPI_QUARTER_SPEED, TERN(ARDUINO_ARCH_STM32, SPI_CLOCK_DIV4, 500000)),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
6 changes: 2 additions & 4 deletions Marlin/src/libs/MAX31865.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand Down

0 comments on commit 9854cf4

Please sign in to comment.