Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SPI bus sharing (TFT, SD, Touch, TMC, etc...) #255

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
- fixed a compatibility issue in case that setMISO/MOSI/SCLK methods …
…would not exist (different architectures)
  • Loading branch information
quiret committed Oct 20, 2022
commit db00c87f83ea28a236a83a0b1d30de3e0934a0f7
44 changes: 44 additions & 0 deletions src/TMCStepper.h
Original file line number Diff line number Diff line change
@@ -63,6 +63,50 @@

#define TMCSTEPPER_VERSION 0x000703 // v0.7.3

// Since we don't have C++20 we need to do some ugly hacks to ensure compat.
// https://stackoverflow.com/questions/63253287/using-sfinae-to-detect-method-with-gcc
#define __DEF_HAS_METH( methName ) \
template<typename T> \
struct ___has_##methName \
{ \
template<typename C> \
static constexpr auto test(...) -> std::false_type; \
template<typename C> \
static constexpr auto test(int) \
-> decltype(static_cast<void>(std::declval<C>().methName(0)), std::true_type()); \
using result_type = decltype(test<T>(0)); \
static const bool value = result_type::value; \
}
#define __HAS_METH( className, methName ) \
( ___has_##methName <className>::value )

__DEF_HAS_METH( setMISO );
__DEF_HAS_METH( setMOSI );
__DEF_HAS_METH( setSCLK );

#define SPI_SET_PIN_HELPER( pinDescName ) \
template <bool hasPin> \
struct _spiInitHelper_##pinDescName \
{}; \
template <> \
struct _spiInitHelper_##pinDescName <true> \
{ \
template <typename SPIClassT> \
static void spiSet##pinDescName( SPIClassT& spi, uint16_t pin ) { spi.set##pinDescName( pin ); } \
}; \
template <> \
struct _spiInitHelper_##pinDescName <false> \
{ \
template <typename SPIClassT> \
static void spiSet##pinDescName( SPIClassT& spi, uint16_t pin ) {} \
}

SPI_SET_PIN_HELPER( MISO );
SPI_SET_PIN_HELPER( MOSI );
SPI_SET_PIN_HELPER( SCLK );

#define SPI_INIT_PIN( spi, pinDescName, val ) _spiInitHelper_##pinDescName <__HAS_METH(SPIClass, set##pinDescName)> ::spiSet##pinDescName( spi, val )

class TMCStepper {
public:
uint16_t cs2rms(uint8_t CS);
6 changes: 3 additions & 3 deletions src/source/TMC2130Stepper.cpp
Original file line number Diff line number Diff line change
@@ -76,9 +76,9 @@ void TMC2130Stepper::switchCSpin(bool state) {
__attribute__((weak))
void TMC2130Stepper::beginTransaction() {
if (TMC_SW_SPI == nullptr) {
SPI.setMISO(_pinMISO);
SPI.setMOSI(_pinMOSI);
SPI.setSCLK(_pinSCK);
SPI_INIT_PIN( SPI, MISO, _pinMISO );
SPI_INIT_PIN( SPI, MOSI, _pinMOSI );
SPI_INIT_PIN( SPI, SCLK, _pinSCK );
SPI.begin();
SPI.beginTransaction(SPISettings(spi_speed, MSBFIRST, SPI_MODE3));
}
12 changes: 6 additions & 6 deletions src/source/TMC2660Stepper.cpp
Original file line number Diff line number Diff line change
@@ -53,9 +53,9 @@ uint32_t TMC2660Stepper::read() {
response <<= 8;
response |= TMC_SW_SPI->transfer(dummy & 0xFF);
} else {
SPI.setMISO(_pinMISO);
SPI.setMOSI(_pinMOSI);
SPI.setSCLK(_pinSCK);
SPI_INIT_PIN( SPI, MISO, _pinMISO );
SPI_INIT_PIN( SPI, MOSI, _pinMOSI );
SPI_INIT_PIN( SPI, SCLK, _pinSCK );
SPI.begin();
SPI.beginTransaction(SPISettings(spi_speed, MSBFIRST, SPI_MODE3));
switchCSpin(LOW);
@@ -79,9 +79,9 @@ void TMC2660Stepper::write(uint8_t addressByte, uint32_t config) {
TMC_SW_SPI->transfer((data >> 8) & 0xFF);
TMC_SW_SPI->transfer(data & 0xFF);
} else {
SPI.setMISO(_pinMISO);
SPI.setMOSI(_pinMOSI);
SPI.setSCLK(_pinSCK);
SPI_INIT_PIN( SPI, MISO, _pinMISO );
SPI_INIT_PIN( SPI, MOSI, _pinMOSI );
SPI_INIT_PIN( SPI, SCLK, _pinSCK );
SPI.begin();
SPI.beginTransaction(SPISettings(spi_speed, MSBFIRST, SPI_MODE3));
switchCSpin(LOW);