diff --git a/src/TMCStepper.h b/src/TMCStepper.h index 6ea5661c..6a39627d 100644 --- a/src/TMCStepper.h +++ b/src/TMCStepper.h @@ -8,7 +8,9 @@ #if defined(ARDUINO) && ARDUINO >= 100 #include - #include + #ifndef TMC_NO_GENERIC_SPI + #include + #endif #include #elif defined(bcm2835) #include @@ -18,8 +20,12 @@ #if __has_include() #include #endif - #if __has_include() - #include + #ifndef TMC_NO_GENERIC_SPI + #if __has_include() + #include + #else + #define TMC_NO_GENERIC_SPI + #endif #endif #if __has_include() #include @@ -63,6 +69,8 @@ #define TMCSTEPPER_VERSION 0x000703 // v0.7.3 +#include "TMCStepper_fixing.h" + class TMCStepper { public: uint16_t cs2rms(uint8_t CS); @@ -149,11 +157,20 @@ class TMCStepper { float holdMultiplier = 0.5; }; +#include "TMCStepper_SPI.h" + +#ifndef TMC_NO_GENERIC_SPI +#define _TMC_SOFTSPI_DEFAULT false +#else +#define _TMC_SOFTSPI_DEFAULT true +#endif + class TMC2130Stepper : public TMCStepper { public: TMC2130Stepper(uint16_t pinCS, float RS = default_RS, int8_t link_index = -1); - TMC2130Stepper(uint16_t pinCS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link_index = -1); - TMC2130Stepper(uint16_t pinCS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link_index = -1); + TMC2130Stepper(uint16_t pinCS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link_index = -1, bool softSPI = _TMC_SOFTSPI_DEFAULT); + TMC2130Stepper(uint16_t pinCS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link_index = -1, bool softSPI = _TMC_SOFTSPI_DEFAULT); + TMC2130Stepper(uint16_t pinCS, float RS, TMCSPIInterface *spiMan, int8_t link_index = -1); void begin(); void defaults(); void setSPISpeed(uint32_t speed); @@ -369,7 +386,12 @@ class TMC2130Stepper : public TMCStepper { static uint32_t spi_speed; // Default 2MHz const uint16_t _pinCS; + const uint16_t _pinMISO; + const uint16_t _pinMOSI; + const uint16_t _pinSCK; + const bool _has_pins; SW_SPIClass * TMC_SW_SPI = nullptr; + TMCSPIInterface* _spiMan; static constexpr float default_RS = 0.11; int8_t link_index; @@ -379,8 +401,8 @@ class TMC2130Stepper : public TMCStepper { class TMC2160Stepper : public TMC2130Stepper { public: TMC2160Stepper(uint16_t pinCS, float RS = default_RS, int8_t link_index = -1); - TMC2160Stepper(uint16_t pinCS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link_index = -1); - TMC2160Stepper(uint16_t pinCS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link_index = -1); + TMC2160Stepper(uint16_t pinCS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link_index = -1, bool softSPI = _TMC_SOFTSPI_DEFAULT); + TMC2160Stepper(uint16_t pinCS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link_index = -1, bool softSPI = _TMC_SOFTSPI_DEFAULT); void begin(); void defaults(); void push(); @@ -478,8 +500,8 @@ class TMC2160Stepper : public TMC2130Stepper { class TMC5130Stepper : public TMC2160Stepper { public: TMC5130Stepper(uint16_t pinCS, float RS = default_RS, int8_t link_index = -1); - TMC5130Stepper(uint16_t pinCS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link_index = -1); - TMC5130Stepper(uint16_t pinCS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link_index = -1); + TMC5130Stepper(uint16_t pinCS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link_index = -1, bool softSPI = _TMC_SOFTSPI_DEFAULT); + TMC5130Stepper(uint16_t pinCS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link_index = -1, bool softSPI = _TMC_SOFTSPI_DEFAULT); void begin(); void defaults(); @@ -722,8 +744,8 @@ class TMC5130Stepper : public TMC2160Stepper { class TMC5160Stepper : public TMC5130Stepper { public: TMC5160Stepper(uint16_t pinCS, float RS = default_RS, int8_t link_index = -1); - TMC5160Stepper(uint16_t pinCS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link_index = -1); - TMC5160Stepper(uint16_t pinCS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link_index = -1); + TMC5160Stepper(uint16_t pinCS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link_index = -1, bool softSPI = _TMC_SOFTSPI_DEFAULT); + TMC5160Stepper(uint16_t pinCS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link_index = -1, bool softSPI = _TMC_SOFTSPI_DEFAULT); void rms_current(uint16_t mA) { TMC2160Stepper::rms_current(mA); } void rms_current(uint16_t mA, float mult) { TMC2160Stepper::rms_current(mA, mult); } @@ -815,11 +837,12 @@ class TMC5160Stepper : public TMC5130Stepper { class TMC5161Stepper : public TMC5160Stepper { public: - TMC5161Stepper(uint16_t pinCS, float RS = default_RS, int8_t link_index = -1) : TMC5160Stepper(pinCS, RS, link_index) {} - TMC5161Stepper(uint16_t pinCS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link_index = -1) : - TMC5160Stepper(pinCS, pinMOSI, pinMISO, pinSCK, link_index) {} - TMC5161Stepper(uint16_t pinCS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link_index = -1) : - TMC5160Stepper(pinCS, RS, pinMOSI, pinMISO, pinSCK, link_index) {} + TMC5161Stepper(uint16_t pinCS, float RS = default_RS, int8_t link_index = -1) : + TMC5160Stepper(pinCS, RS, link_index) {} + TMC5161Stepper(uint16_t pinCS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link_index = -1, bool softSPI = _TMC_SOFTSPI_DEFAULT) : + TMC5160Stepper(pinCS, pinMOSI, pinMISO, pinSCK, link_index, softSPI) {} + TMC5161Stepper(uint16_t pinCS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link_index = -1, bool softSPI = _TMC_SOFTSPI_DEFAULT) : + TMC5160Stepper(pinCS, RS, pinMOSI, pinMISO, pinSCK, link_index, softSPI) {} }; class TMC2208Stepper : public TMCStepper { @@ -1104,8 +1127,9 @@ class TMC2224Stepper : public TMC2208Stepper { class TMC2660Stepper { public: TMC2660Stepper(uint16_t pinCS, float RS = default_RS); - TMC2660Stepper(uint16_t pinCS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK); - TMC2660Stepper(uint16_t pinCS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK); + TMC2660Stepper(uint16_t pinCS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, bool softSPI = _TMC_SOFTSPI_DEFAULT); + TMC2660Stepper(uint16_t pinCS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, bool softSPI = _TMC_SOFTSPI_DEFAULT); + TMC2660Stepper(uint16_t pinCS, float RS, TMCSPIInterface *spiMan); void write(uint8_t addressByte, uint32_t config); uint32_t read(); void switchCSpin(bool state); @@ -1255,6 +1279,11 @@ class TMC2660Stepper { INIT_REGISTER(READ_RDSEL10){{.sr=0}}; const uint16_t _pinCS; + const uint16_t _pinMISO; + const uint16_t _pinMOSI; + const uint16_t _pinSCK; + const bool _has_pins; + TMCSPIInterface *_spiMan; const float Rsense; static constexpr float default_RS = 0.1; float holdMultiplier = 0.5; diff --git a/src/TMCStepper_SPI.h b/src/TMCStepper_SPI.h new file mode 100644 index 00000000..b000a400 --- /dev/null +++ b/src/TMCStepper_SPI.h @@ -0,0 +1,16 @@ +#pragma once + +#define TMCSPI_BITORDER_LSB 0 +#define TMCSPI_BITORDER_MSB 1 + +#define TMCSPI_CLKMODE_0 0 +#define TMCSPI_CLKMODE_1 1 +#define TMCSPI_CLKMODE_2 2 +#define TMCSPI_CLKMODE_3 3 + +struct TMCSPIInterface { + virtual void begin(uint32_t maxClockFreq, int bitOrder, int clkMode) = 0; + virtual void end() = 0; + virtual uint8_t transfer(uint8_t txval) = 0; + virtual void sendRepeat(uint8_t val, uint16_t repcnt) = 0; +}; \ No newline at end of file diff --git a/src/TMCStepper_fixing.h b/src/TMCStepper_fixing.h new file mode 100644 index 00000000..0c37f089 --- /dev/null +++ b/src/TMCStepper_fixing.h @@ -0,0 +1,107 @@ +#pragma once + +#ifndef TMC_NO_GENERIC_SPI + +namespace _TMC_FIXING +{ + +// BEGIN https://en.cppreference.com/w/cpp/types/add_reference +namespace detail { + +template +struct type_identity { using type = T; }; + +template +auto try_add_rvalue_reference(int) -> type_identity; +template +auto try_add_rvalue_reference(...) -> type_identity; + +} // namespace detail + +template +struct add_rvalue_reference : decltype(detail::try_add_rvalue_reference(0)) {}; + +// END https://en.cppreference.com/w/cpp/types/add_reference + +// BEGIN https://en.cppreference.com/w/cpp/utility/declval (inspired) +template +struct always_false +{ + enum { value = false }; +}; + +template +typename add_rvalue_reference::type declval() { + static_assert(always_false::value, "declval not allowed in an evaluated context"); +} + +// END https://en.cppreference.com/w/cpp/utility/declval + +struct true_type { + enum { value = true }; +}; +struct false_type { + enum { value = false }; +}; + +} // namespace TMC_FIXING + +// 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 +// Did you know that ATMEL AVR does not support standard C++ headers??? +#define __DEF_HAS_METH( methName ) \ + template \ + struct ___has_##methName \ + { \ + template \ + static constexpr auto test(...) -> _TMC_FIXING::false_type; \ + template \ + static constexpr auto test(int) \ + -> decltype(static_cast(_TMC_FIXING::declval().methName(0)), _TMC_FIXING::true_type()); \ + using result_type = decltype(test(0)); \ + static const bool value = result_type::value; \ + } +#define __HAS_METH( className, methName ) \ + ( ___has_##methName ::value ) + +__DEF_HAS_METH( setMISO ); +__DEF_HAS_METH( setMOSI ); +__DEF_HAS_METH( setSCLK ); + +#define SPI_SET_PIN_HELPER( pinDescName ) \ + template \ + struct _spiInitHelper_##pinDescName \ + {}; \ + template <> \ + struct _spiInitHelper_##pinDescName \ + { \ + template \ + static void spiSet##pinDescName( SPIClassT& spi, uint16_t pin ) { spi.set##pinDescName( pin ); } \ + }; \ + template <> \ + struct _spiInitHelper_##pinDescName \ + { \ + template \ + 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 ) + +// Speciality of the ESP32 platform. +#ifdef ESP_PLATFORM +// https://github.com/espressif/arduino-esp32/blob/master/libraries/SPI/src/SPI.cpp SPIClass::begin method. +#define SPI_BEGIN( spi, sck, miso, mosi ) ( spi.begin( sck, miso, mosi ) ) +#else +// Other platforms. +#define SPI_BEGIN( spi, sck, miso, mosi ) \ + { SPI_INIT_PIN( spi, MISO, miso ); \ + SPI_INIT_PIN( SPI, MOSI, mosi ); \ + SPI_INIT_PIN( SPI, SCLK, sck ); \ + spi.begin(); } +#endif + +#endif //TMC_NO_GENERIC_SPI \ No newline at end of file diff --git a/src/source/TMC2130Stepper.cpp b/src/source/TMC2130Stepper.cpp index 9d01c4fc..a4573efb 100644 --- a/src/source/TMC2130Stepper.cpp +++ b/src/source/TMC2130Stepper.cpp @@ -4,37 +4,84 @@ int8_t TMC2130Stepper::chain_length = 0; uint32_t TMC2130Stepper::spi_speed = 16000000/8; -TMC2130Stepper::TMC2130Stepper(uint16_t pinCS, float RS, int8_t link) : +TMC2130Stepper::TMC2130Stepper(uint16_t pinCS, float RS, int8_t link_index) : TMCStepper(RS), _pinCS(pinCS), + _pinMISO(0), + _pinMOSI(0), + _pinSCK(0), + _has_pins(false), + TMC_SW_SPI(nullptr), + _spiMan(nullptr), + link_index(link_index) + { + defaults(); + + if (link_index > chain_length) + chain_length = link_index; + } + +TMC2130Stepper::TMC2130Stepper(uint16_t pinCS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link, bool softSPI) : + TMCStepper(default_RS), + _pinCS(pinCS), + _pinMISO(pinMISO), + _pinMOSI(pinMOSI), + _pinSCK(pinSCK), + _has_pins(true), + _spiMan(nullptr), link_index(link) { + if (softSPI) + { + SW_SPIClass *SW_SPI_Obj = new SW_SPIClass(pinMOSI, pinMISO, pinSCK); + TMC_SW_SPI = SW_SPI_Obj; + } + else + { + TMC_SW_SPI = nullptr; + } defaults(); if (link > chain_length) chain_length = link; } -TMC2130Stepper::TMC2130Stepper(uint16_t pinCS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link) : - TMCStepper(default_RS), +TMC2130Stepper::TMC2130Stepper(uint16_t pinCS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link, bool softSPI) : + TMCStepper(RS), _pinCS(pinCS), + _pinMISO(pinMISO), + _pinMOSI(pinMOSI), + _pinSCK(pinSCK), + _has_pins(true), + _spiMan(nullptr), link_index(link) { - SW_SPIClass *SW_SPI_Obj = new SW_SPIClass(pinMOSI, pinMISO, pinSCK); - TMC_SW_SPI = SW_SPI_Obj; + if (softSPI) + { + SW_SPIClass *SW_SPI_Obj = new SW_SPIClass(pinMOSI, pinMISO, pinSCK); + TMC_SW_SPI = SW_SPI_Obj; + } + else + { + TMC_SW_SPI = nullptr; + } defaults(); if (link > chain_length) chain_length = link; } -TMC2130Stepper::TMC2130Stepper(uint16_t pinCS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link) : +TMC2130Stepper::TMC2130Stepper(uint16_t pinCS, float RS, TMCSPIInterface *spiMan, int8_t link) : TMCStepper(RS), _pinCS(pinCS), + _pinMISO(0), + _pinMOSI(0), + _pinSCK(0), + _has_pins(false), + TMC_SW_SPI(nullptr), + _spiMan(spiMan), link_index(link) { - SW_SPIClass *SW_SPI_Obj = new SW_SPIClass(pinMOSI, pinMISO, pinSCK); - TMC_SW_SPI = SW_SPI_Obj; defaults(); if (link > chain_length) @@ -66,32 +113,59 @@ void TMC2130Stepper::switchCSpin(bool state) { __attribute__((weak)) void TMC2130Stepper::beginTransaction() { - if (TMC_SW_SPI == nullptr) { + if (_spiMan) { + _spiMan->begin(spi_speed, TMCSPI_BITORDER_MSB, TMCSPI_CLKMODE_3); + } +#ifndef TMC_NO_GENERIC_SPI + else if (TMC_SW_SPI == nullptr) { + if (_has_pins) + { + SPI_BEGIN( SPI, _pinSCK, _pinMISO, _pinMOSI ); + } + else + SPI.begin(); SPI.beginTransaction(SPISettings(spi_speed, MSBFIRST, SPI_MODE3)); } +#endif } __attribute__((weak)) void TMC2130Stepper::endTransaction() { - if (TMC_SW_SPI == nullptr) { + if (_spiMan) { + _spiMan->end(); + } +#ifndef TMC_NO_GENERIC_SPI + else if (TMC_SW_SPI == nullptr) { SPI.endTransaction(); + SPI.end(); } +#endif } __attribute__((weak)) uint8_t TMC2130Stepper::transfer(const uint8_t data) { uint8_t out = 0; - if (TMC_SW_SPI != nullptr) { + if (_spiMan) { + out = _spiMan->transfer(data); + } + else if (TMC_SW_SPI != nullptr) { out = TMC_SW_SPI->transfer(data); } +#ifndef TMC_NO_GENERIC_SPI else { out = SPI.transfer(data); } +#endif return out; } void TMC2130Stepper::transferEmptyBytes(const uint8_t n) { - for (uint8_t i = 0; i < n; i++) { - transfer(0x00); + if (_spiMan) { + _spiMan->sendRepeat(0, n); + } + else { + for (uint8_t i = 0; i < n; i++) { + transfer(0x00); + } } } diff --git a/src/source/TMC2160Stepper.cpp b/src/source/TMC2160Stepper.cpp index f97487a9..7122e991 100644 --- a/src/source/TMC2160Stepper.cpp +++ b/src/source/TMC2160Stepper.cpp @@ -1,13 +1,14 @@ #include "TMCStepper.h" #include "TMC_MACROS.h" -TMC2160Stepper::TMC2160Stepper(uint16_t pinCS, float RS, int8_t link) : TMC2130Stepper(pinCS, RS, link) +TMC2160Stepper::TMC2160Stepper(uint16_t pinCS, float RS, int8_t link_index) : + TMC2130Stepper(pinCS, RS, link_index) { defaults(); } -TMC2160Stepper::TMC2160Stepper(uint16_t pinCS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link) : - TMC2130Stepper(pinCS, RS, pinMOSI, pinMISO, pinSCK, link) +TMC2160Stepper::TMC2160Stepper(uint16_t pinCS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link, bool softSPI) : + TMC2130Stepper(pinCS, RS, pinMOSI, pinMISO, pinSCK, link, softSPI) { defaults(); } -TMC2160Stepper::TMC2160Stepper(uint16_t pinCS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link) : - TMC2130Stepper(pinCS, default_RS, pinMOSI, pinMISO, pinSCK, link) +TMC2160Stepper::TMC2160Stepper(uint16_t pinCS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link, bool softSPI) : + TMC2130Stepper(pinCS, default_RS, pinMOSI, pinMISO, pinSCK, link, softSPI) { defaults(); } void TMC2160Stepper::begin() { diff --git a/src/source/TMC2660Stepper.cpp b/src/source/TMC2660Stepper.cpp index 316f7fa8..859501e9 100644 --- a/src/source/TMC2660Stepper.cpp +++ b/src/source/TMC2660Stepper.cpp @@ -3,23 +3,68 @@ TMC2660Stepper::TMC2660Stepper(uint16_t pinCS, float RS) : _pinCS(pinCS), - Rsense(RS) - {} + _pinMISO(0), + _pinMOSI(0), + _pinSCK(0), + _has_pins(false), + _spiMan(nullptr), + Rsense(RS), + TMC_SW_SPI(nullptr) + { + return; + } -TMC2660Stepper::TMC2660Stepper(uint16_t pinCS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK) : +TMC2660Stepper::TMC2660Stepper(uint16_t pinCS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, bool softSPI) : _pinCS(pinCS), + _pinMISO(pinMISO), + _pinMOSI(pinMOSI), + _pinSCK(pinSCK), + _has_pins(true), + _spiMan(nullptr), Rsense(default_RS) { - SW_SPIClass *SW_SPI_Obj = new SW_SPIClass(pinMOSI, pinMISO, pinSCK); - TMC_SW_SPI = SW_SPI_Obj; + if (softSPI) + { + SW_SPIClass *SW_SPI_Obj = new SW_SPIClass(pinMOSI, pinMISO, pinSCK); + TMC_SW_SPI = SW_SPI_Obj; + } + else + { + TMC_SW_SPI = nullptr; + } } -TMC2660Stepper::TMC2660Stepper(uint16_t pinCS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK) : +TMC2660Stepper::TMC2660Stepper(uint16_t pinCS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, bool softSPI) : _pinCS(pinCS), + _pinMISO(pinMISO), + _pinMOSI(pinMOSI), + _pinSCK(pinSCK), + _has_pins(true), + _spiMan(nullptr), Rsense(RS) { - SW_SPIClass *SW_SPI_Obj = new SW_SPIClass(pinMOSI, pinMISO, pinSCK); - TMC_SW_SPI = SW_SPI_Obj; + if (softSPI) + { + SW_SPIClass *SW_SPI_Obj = new SW_SPIClass(pinMOSI, pinMISO, pinSCK); + TMC_SW_SPI = SW_SPI_Obj; + } + else + { + TMC_SW_SPI = nullptr; + } + } + +TMC2660Stepper::TMC2660Stepper(uint16_t pinCS, float RS, TMCSPIInterface *spiMan) : + _pinCS(pinCS), + _pinMISO(0), + _pinMOSI(0), + _pinSCK(0), + _has_pins(false), + _spiMan(spiMan), + Rsense(RS), + TMC_SW_SPI(nullptr) + { + return; } void TMC2660Stepper::switchCSpin(bool state) { @@ -30,14 +75,32 @@ void TMC2660Stepper::switchCSpin(bool state) { uint32_t TMC2660Stepper::read() { uint32_t response = 0UL; uint32_t dummy = ((uint32_t)DRVCONF_register.address<<17) | DRVCONF_register.sr; - if (TMC_SW_SPI != nullptr) { + if (_spiMan) { + _spiMan->begin(spi_speed, TMCSPI_BITORDER_MSB, TMCSPI_CLKMODE_3); + switchCSpin(LOW); + response |= _spiMan->transfer((dummy >> 16) & 0xFF); + response <<= 8; + response |= _spiMan->transfer((dummy >> 8) & 0xFF); + response <<= 8; + response |= _spiMan->transfer(dummy & 0xFF); + _spiMan->end(); + } + else if (TMC_SW_SPI != nullptr) { switchCSpin(LOW); response |= TMC_SW_SPI->transfer((dummy >> 16) & 0xFF); response <<= 8; response |= TMC_SW_SPI->transfer((dummy >> 8) & 0xFF); response <<= 8; response |= TMC_SW_SPI->transfer(dummy & 0xFF); - } else { + } +#ifndef TMC_NO_GENERIC_SPI + else { + if (_has_pins) + { + SPI_BEGIN( SPI, _pinSCK, _pinMISO, _pinMOSI ); + } + else + SPI.begin(); SPI.beginTransaction(SPISettings(spi_speed, MSBFIRST, SPI_MODE3)); switchCSpin(LOW); response |= SPI.transfer((dummy >> 16) & 0xFF); @@ -46,26 +109,46 @@ uint32_t TMC2660Stepper::read() { response <<= 8; response |= SPI.transfer(dummy & 0xFF); SPI.endTransaction(); + SPI.end(); } +#endif switchCSpin(HIGH); return response >> 4; } void TMC2660Stepper::write(uint8_t addressByte, uint32_t config) { uint32_t data = (uint32_t)addressByte<<17 | config; - if (TMC_SW_SPI != nullptr) { + if (_spiMan) { + _spiMan->begin(spi_speed, TMCSPI_BITORDER_MSB, TMCSPI_CLKMODE_3); + switchCSpin(LOW); + _spiMan->transfer((data >> 16) & 0xFF); + _spiMan->transfer((data >> 8) & 0xFF); + _spiMan->transfer(data & 0xFF); + _spiMan->end(); + } + else if (TMC_SW_SPI != nullptr) { switchCSpin(LOW); TMC_SW_SPI->transfer((data >> 16) & 0xFF); TMC_SW_SPI->transfer((data >> 8) & 0xFF); TMC_SW_SPI->transfer(data & 0xFF); - } else { + } +#ifndef TMC_NO_GENERIC_SPI + else { + if (_has_pins) + { + SPI_BEGIN( SPI, _pinSCK, _pinMISO, _pinMOSI ); + } + else + SPI.begin(); SPI.beginTransaction(SPISettings(spi_speed, MSBFIRST, SPI_MODE3)); switchCSpin(LOW); SPI.transfer((data >> 16) & 0xFF); SPI.transfer((data >> 8) & 0xFF); SPI.transfer(data & 0xFF); SPI.endTransaction(); + SPI.end(); } +#endif switchCSpin(HIGH); } diff --git a/src/source/TMC5130Stepper.cpp b/src/source/TMC5130Stepper.cpp index efc95bef..06423f2b 100644 --- a/src/source/TMC5130Stepper.cpp +++ b/src/source/TMC5130Stepper.cpp @@ -1,13 +1,14 @@ #include "TMCStepper.h" #include "TMC_MACROS.h" -TMC5130Stepper::TMC5130Stepper(uint16_t pinCS, float RS, int8_t link) : TMC2160Stepper(pinCS, RS, link) +TMC5130Stepper::TMC5130Stepper(uint16_t pinCS, float RS, int8_t link_index) : + TMC2160Stepper(pinCS, RS, link_index) { defaults(); } -TMC5130Stepper::TMC5130Stepper(uint16_t pinCS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link): - TMC2160Stepper(pinCS, RS, pinMOSI, pinMISO, pinSCK, link) +TMC5130Stepper::TMC5130Stepper(uint16_t pinCS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link, bool softSPI): + TMC2160Stepper(pinCS, RS, pinMOSI, pinMISO, pinSCK, link, softSPI) { defaults(); } -TMC5130Stepper::TMC5130Stepper(uint16_t pinCS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link) : - TMC2160Stepper(pinCS, default_RS, pinMOSI, pinMISO, pinSCK, link) +TMC5130Stepper::TMC5130Stepper(uint16_t pinCS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link, bool softSPI) : + TMC2160Stepper(pinCS, default_RS, pinMOSI, pinMISO, pinSCK, link, softSPI) { defaults(); } void TMC5130Stepper::begin() { diff --git a/src/source/TMC5160Stepper.cpp b/src/source/TMC5160Stepper.cpp index d9b4221b..fe77a099 100644 --- a/src/source/TMC5160Stepper.cpp +++ b/src/source/TMC5160Stepper.cpp @@ -1,13 +1,14 @@ #include "TMCStepper.h" #include "TMC_MACROS.h" -TMC5160Stepper::TMC5160Stepper(uint16_t pinCS, float RS, int8_t link) : TMC5130Stepper(pinCS, RS, link) +TMC5160Stepper::TMC5160Stepper(uint16_t pinCS, float RS, int8_t link_index) : + TMC5130Stepper(pinCS, RS, link_index) { defaults(); } -TMC5160Stepper::TMC5160Stepper(uint16_t pinCS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link) : - TMC5130Stepper(pinCS, RS, pinMOSI, pinMISO, pinSCK, link) +TMC5160Stepper::TMC5160Stepper(uint16_t pinCS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link, bool softSPI) : + TMC5130Stepper(pinCS, RS, pinMOSI, pinMISO, pinSCK, link, softSPI) { defaults(); } -TMC5160Stepper::TMC5160Stepper(uint16_t pinCS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link) : - TMC5130Stepper(pinCS, default_RS, pinMOSI, pinMISO, pinSCK, link) +TMC5160Stepper::TMC5160Stepper(uint16_t pinCS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link, bool softSPI) : + TMC5130Stepper(pinCS, default_RS, pinMOSI, pinMISO, pinSCK, link, softSPI) { defaults(); } void TMC5160Stepper::defaults() {