Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 19 additions & 24 deletions Marlin/src/libs/MAX31865.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
//#define MAX31865_DEBUG
//#define MAX31865_DEBUG_SPI

//TODO: switch to SPIclass/SoftSPI
#include <SoftwareSPI.h>

#include "../inc/MarlinConfig.h"

Expand All @@ -62,7 +62,7 @@ SPISettings MAX31865::spiConfig = SPISettings(
500000
#endif
, MSBFIRST
, SPI_MODE_1 // CPOL0 CPHA1
, SPI_MODE1 // CPOL0 CPHA1
);

#ifndef LARGE_PINMAP
Expand All @@ -81,6 +81,7 @@ SPISettings MAX31865::spiConfig = SPISettings(
_mosi = spi_mosi;
_miso = spi_miso;
_sclk = spi_clk;
_spi_speed = swSpiInit(SPI_QUARTER_SPEED, SD_SCK_PIN, SD_MOSI_PIN);
}

/**
Expand All @@ -92,6 +93,7 @@ SPISettings MAX31865::spiConfig = SPISettings(
MAX31865::MAX31865(int8_t spi_cs) {
_cs = spi_cs;
_sclk = _miso = _mosi = -1;
_spi_speed = swSpiInit(SPI_QUARTER_SPEED, SD_SCK_PIN, SD_MOSI_PIN);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

}

#else
Expand Down Expand Up @@ -150,17 +152,16 @@ void MAX31865::begin(max31865_numwires_t wires, float zero, float ref) {
Rzero = zero;
Rref = ref;

OUT_WRITE(_cs, HIGH);
SET_OUTPUT(_cs);
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);

swSpiBegin(_sclk, _miso, _mosi);
} else {
// start and configure hardware SPI
#ifdef MAX31865_DEBUG
Expand Down Expand Up @@ -429,9 +430,9 @@ void MAX31865::readRegisterN(uint8_t addr, uint8_t buffer[], uint8_t n) {
if (_sclk == TERN(LARGE_PINMAP, -1UL, -1))
SPI.beginTransaction(spiConfig);
else
WRITE(_sclk, LOW);
write(_sclk, LOW);

WRITE(_cs, LOW);
write(_cs, LOW);
spixfer(addr);

while (n--) {
Expand All @@ -445,7 +446,7 @@ void MAX31865::readRegisterN(uint8_t addr, uint8_t buffer[], uint8_t n) {
if (_sclk == TERN(LARGE_PINMAP, -1UL, -1))
SPI.endTransaction();

WRITE(_cs, HIGH);
write(_cs, HIGH);
}

/**
Expand All @@ -458,19 +459,23 @@ void MAX31865::writeRegister8(uint8_t addr, uint8_t data) {
if (_sclk == TERN(LARGE_PINMAP, -1UL, -1))
SPI.beginTransaction(spiConfig);
else
WRITE(_sclk, LOW);
write(_sclk, LOW);

WRITE(_cs, LOW);
write(_cs, LOW);

spixfer(addr | 0x80); // make sure top bit is set
spixfer(data);

if (_sclk == TERN(LARGE_PINMAP, -1UL, -1))
SPI.endTransaction();

WRITE(_cs, HIGH);
write(_cs, HIGH);
}

void MAX31865::write(pin_t pin, bool value){
for (uint8_t i = 0; i < _spi_speed; i++)
WRITE(pin, value);
}
/**
* Transfer SPI data +x+ and read the response. From the datasheet...
* Input data (SDI) is latched on the internal strobe edge and output data (SDO) is
Expand All @@ -484,17 +489,7 @@ uint8_t MAX31865::spixfer(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;
}

return reply;
return swSpiTransfer(x, _spi_speed, _sclk, _miso, _mosi);
}

#endif // HAS_MAX31865 && !LIB_USR_MAX31865
4 changes: 4 additions & 0 deletions Marlin/src/libs/MAX31865.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class MAX31865 {
static SPISettings spiConfig;

TERN(LARGE_PINMAP, uint32_t, uint8_t) _sclk, _miso, _mosi, _cs;
uint8_t _spi_speed;
float Rzero, Rref;

void setConfig(uint8_t config, bool enable);
Expand All @@ -101,6 +102,9 @@ class MAX31865 {
void writeRegister8(uint8_t addr, uint8_t reg);
uint8_t spixfer(uint8_t addr);

// Writes value to pin, also factoring in speed of SPI transmission
void write(pin_t pin, bool value);

public:
#ifdef LARGE_PINMAP
MAX31865(uint32_t spi_cs, uint8_t pin_mapping);
Expand Down