From 5ad70b7bc567396d8c625e38b4e3725c58ecbb25 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Fri, 1 Feb 2019 15:37:45 -0800 Subject: [PATCH] Dot star spi fixes (#249) * Update DotStarSpiMethod.h fixes for SPI.transfer updated to modern beginTransaction * Update DotStarSpiMethod.h Use beginTransaction as this is the most modern way to use the SPI AVR loops and sends one byte at a time since they don't have a method that doesn't overwrite the out buffer like esp does --- src/internal/DotStarSpiMethod.h | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/src/internal/DotStarSpiMethod.h b/src/internal/DotStarSpiMethod.h index 74220f7..7d4173c 100644 --- a/src/internal/DotStarSpiMethod.h +++ b/src/internal/DotStarSpiMethod.h @@ -55,24 +55,33 @@ public: void Initialize(int8_t sck, int8_t miso, int8_t mosi, int8_t ss) { SPI.begin(sck, miso, mosi, ss); - init(); } #endif void Initialize() { SPI.begin(); - init(); } void Update() { - // due to API inconsistencies need to call different methods on SPI + SPI.beginTransaction(SPISettings(20000000L, MSBFIRST, SPI_MODE0)); #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) + // ESPs have a method to write without inplace overwriting the send buffer + // since we don't care what gets received, use it for performance SPI.writeBytes(_sendBuffer, _sizeSendBuffer); + #else - SPI.transfer(_sendBuffer, _sizeSendBuffer); + // default ARDUINO transfer inplace overwrites the send buffer + // which is bad, so we have to send one byte at a time + uint8_t* out = _sendBuffer; + uint8_t* end = out + _sizeSendBuffer; + while (out < end) + { + SPI.transfer(*out++); + } #endif + SPI.endTransaction(); } uint8_t* getPixels() const @@ -92,21 +101,6 @@ private: uint8_t* _sendBuffer; // Holds SPI send Buffer, including LED color values - void init() - { -#if defined(ARDUINO_ARCH_ESP8266) - SPI.setFrequency(4000000L); // more than 4mhz causes CLK pulse malformation -#elif defined(ARDUINO_ARCH_ESP32) - SPI.setFrequency(4000000L); // more than 4mhz causes CLK pulse malformation -#elif defined(ARDUINO_ARCH_AVR) - SPI.setClockDivider(SPI_CLOCK_DIV2); // 8 MHz (6 MHz on Pro Trinket 3V) -#else - SPI.setClockDivider((F_CPU + 4000000L) / 8000000L); // 8-ish MHz on Due -#endif - SPI.setBitOrder(MSBFIRST); - SPI.setDataMode(SPI_MODE0); - } - size_t calcBufferSize(size_t sizePixels) const { const size_t countEndFrameBytes = calcEndFrameSize(sizePixels);