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
This commit is contained in:
Michael Miller
2019-02-01 15:37:45 -08:00
committed by GitHub
parent eb405efef5
commit 5ad70b7bc5

View File

@@ -55,24 +55,33 @@ public:
void Initialize(int8_t sck, int8_t miso, int8_t mosi, int8_t ss) void Initialize(int8_t sck, int8_t miso, int8_t mosi, int8_t ss)
{ {
SPI.begin(sck, miso, mosi, ss); SPI.begin(sck, miso, mosi, ss);
init();
} }
#endif #endif
void Initialize() void Initialize()
{ {
SPI.begin(); SPI.begin();
init();
} }
void Update() 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) #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); SPI.writeBytes(_sendBuffer, _sizeSendBuffer);
#else #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 #endif
SPI.endTransaction();
} }
uint8_t* getPixels() const uint8_t* getPixels() const
@@ -92,21 +101,6 @@ private:
uint8_t* _sendBuffer; // Holds SPI send Buffer, including LED color values 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 size_t calcBufferSize(size_t sizePixels) const
{ {
const size_t countEndFrameBytes = calcEndFrameSize(sizePixels); const size_t countEndFrameBytes = calcEndFrameSize(sizePixels);