Skip to content

Commit

Permalink
Format, consolidate
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Sep 3, 2021
1 parent 5c30e6a commit 6cc1371
Showing 1 changed file with 45 additions and 74 deletions.
119 changes: 45 additions & 74 deletions Marlin/src/libs/MAX31865.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,15 @@
//#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_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
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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
*
Expand All @@ -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);
Expand All @@ -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
}

/**
Expand Down Expand Up @@ -367,7 +346,6 @@ float MAX31865::temperature(float Rrtd) {
// private:
//


/**
* Set a value in the configuration register.
*
Expand Down Expand Up @@ -483,41 +461,34 @@ uint8_t MAX31865::spixfer(uint8_t x) {
return softSpiTransfer(x);
}

#endif // HAS_MAX31865 && !LIB_USR_MAX31865

#ifdef TARGET_LPC1768

#include <SoftwareSPI.h>

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

0 comments on commit 6cc1371

Please sign in to comment.