diff --git a/Grbl_Esp32/src/Motors/TrinamicDriver.cpp b/Grbl_Esp32/src/Motors/TrinamicDriver.cpp index a6164e29..901b5a32 100644 --- a/Grbl_Esp32/src/Motors/TrinamicDriver.cpp +++ b/Grbl_Esp32/src/Motors/TrinamicDriver.cpp @@ -54,10 +54,10 @@ void TMC2130Stepper::switchCSpin(bool state) { // We use the _pinCS to figure out which derived class we have switch (this->_pinCS) { case 2130: - static_cast(this)->_cspin.write(state); + static_cast(this)->_cspin.synchronousWrite(state); break; case 5160: - static_cast(this)->_cspin.write(state); + static_cast(this)->_cspin.synchronousWrite(state); break; } } diff --git a/Grbl_Esp32/src/Pin.h b/Grbl_Esp32/src/Pin.h index 6f1b349b..a90c5737 100644 --- a/Grbl_Esp32/src/Pin.h +++ b/Grbl_Esp32/src/Pin.h @@ -125,6 +125,7 @@ public: } inline void write(bool value) const { _detail->write(value); } + inline void synchronousWrite(bool value) const { _detail->synchronousWrite(value); } inline bool read() const { return _detail->read() != 0; } diff --git a/Grbl_Esp32/src/PinMapper.cpp b/Grbl_Esp32/src/PinMapper.cpp index ee3c1a87..8016ad54 100644 --- a/Grbl_Esp32/src/PinMapper.cpp +++ b/Grbl_Esp32/src/PinMapper.cpp @@ -4,9 +4,9 @@ #include -// Pin mapping. Pretty straight forward, it's just a thing that stores pins in an array. -// -// The default behavior of a mapped pin is _undefined pin_, so it does nothing. +// Pin mapping. Pretty straight forward, it's just a thing that stores pins in an array. +// +// The default behavior of a mapped pin is _undefined pin_, so it does nothing. namespace { class Mapping { public: @@ -44,7 +44,7 @@ PinMapper::PinMapper() : _mappedId(0) {} PinMapper::PinMapper(Pin& pin) { _mappedId = Mapping::instance().Claim(&pin); - + // If you reach this assertion, you haven't been using the Pin class like you're supposed to. Assert(_mappedId != 0, "Cannot claim pin. We've reached the limit of 255 mapped pins."); } @@ -77,7 +77,7 @@ PinMapper::~PinMapper() { void IRAM_ATTR digitalWrite(uint8_t pin, uint8_t val) { auto thePin = Mapping::instance()._mapping[pin]; if (thePin) { - thePin->write(val); + thePin->synchronousWrite(val); } } diff --git a/Grbl_Esp32/src/Pins/I2SOPinDetail.cpp b/Grbl_Esp32/src/Pins/I2SOPinDetail.cpp index a79284ef..1981cb58 100644 --- a/Grbl_Esp32/src/Pins/I2SOPinDetail.cpp +++ b/Grbl_Esp32/src/Pins/I2SOPinDetail.cpp @@ -50,11 +50,19 @@ namespace Pins { PinCapabilities I2SOPinDetail::capabilities() const { return PinCapabilities::Output | PinCapabilities::I2S; } - void I2SOPinDetail::write(int high) { + // The write will not happen immediately; the data is queued for + // delivery to the serial shift register chain via DMA and a FIFO + void IRAM_ATTR I2SOPinDetail::write(int high) { int value = _readWriteMask ^ high; i2s_out_write(_index, value); } + // Write and wait for completion. Not suitable for use from an ISR + void I2SOPinDetail::synchronousWrite(int high) { + write(high); + i2s_out_delay(); + } + int I2SOPinDetail::read() { auto raw = i2s_out_read(_index); return raw ^ _readWriteMask; diff --git a/Grbl_Esp32/src/Pins/I2SOPinDetail.h b/Grbl_Esp32/src/Pins/I2SOPinDetail.h index a83e4945..1cbd472d 100644 --- a/Grbl_Esp32/src/Pins/I2SOPinDetail.h +++ b/Grbl_Esp32/src/Pins/I2SOPinDetail.h @@ -37,6 +37,7 @@ namespace Pins { // I/O: void write(int high) override; + void synchronousWrite(int high) override; int read() override; void setAttr(PinAttributes value) override; PinAttributes getAttr() const override; diff --git a/Grbl_Esp32/src/Pins/PinDetail.h b/Grbl_Esp32/src/Pins/PinDetail.h index d6ca4ceb..917f541f 100644 --- a/Grbl_Esp32/src/Pins/PinDetail.h +++ b/Grbl_Esp32/src/Pins/PinDetail.h @@ -44,7 +44,8 @@ namespace Pins { virtual PinCapabilities capabilities() const = 0; // I/O: - virtual void write(int high) = 0; + virtual void write(int high) = 0; + virtual void synchronousWrite(int high) { write(high); } virtual int read() = 0; virtual void setAttr(PinAttributes value) = 0; virtual PinAttributes getAttr() const = 0; diff --git a/Grbl_Esp32/src/Pins/VoidPinDetail.cpp b/Grbl_Esp32/src/Pins/VoidPinDetail.cpp index 1f3e05f6..d78048ba 100644 --- a/Grbl_Esp32/src/Pins/VoidPinDetail.cpp +++ b/Grbl_Esp32/src/Pins/VoidPinDetail.cpp @@ -16,6 +16,7 @@ along with Grbl_ESP32. If not, see . */ +#include // IRAM_ATTR #include "VoidPinDetail.h" namespace Pins { @@ -27,10 +28,10 @@ namespace Pins { return PinCapabilities::Output | PinCapabilities::Input | PinCapabilities::ISR | PinCapabilities::Void; } - void VoidPinDetail::write(int high) {} - int VoidPinDetail::read() { return 0; } - void VoidPinDetail::setAttr(PinAttributes value) {} - PinAttributes VoidPinDetail::getAttr() const { return PinAttributes::None; } + void IRAM_ATTR VoidPinDetail::write(int high) {} + int VoidPinDetail::read() { return 0; } + void VoidPinDetail::setAttr(PinAttributes value) {} + PinAttributes VoidPinDetail::getAttr() const { return PinAttributes::None; } String VoidPinDetail::toString() { return "NO_PIN"; }