Skip to content

Commit 1bd0463

Browse files
Add non-blocking DMA option in writePixels()
1 parent b8fd9b4 commit 1bd0463

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

Adafruit_SPITFT.cpp

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -865,8 +865,16 @@ void Adafruit_SPITFT::writePixel(int16_t x, int16_t y, uint16_t color) {
865865
@param colors Pointer to array of 16-bit pixel values in '565' RGB
866866
format.
867867
@param len Number of elements in 'colors' array.
868+
@param block If true (default case if unspecified), function blocks
869+
until DMA transfer is complete. This is simply IGNORED
870+
if DMA is not enabled. If false, the function returns
871+
immediately after the last DMA transfer is started,
872+
and one should use the dmaWait() function before
873+
doing ANY other display-related activities (or even any
874+
SPI-related activities, if using an SPI display that
875+
shares the bus with other devices).
868876
*/
869-
void Adafruit_SPITFT::writePixels(uint16_t *colors, uint32_t len) {
877+
void Adafruit_SPITFT::writePixels(uint16_t *colors, uint32_t len, bool block) {
870878

871879
if(!len) return; // Avoid 0-byte transfers
872880

@@ -917,15 +925,17 @@ void Adafruit_SPITFT::writePixels(uint16_t *colors, uint32_t len) {
917925
}
918926
lastFillColor = 0x0000; // pixelBuf has been sullied
919927
lastFillLen = 0;
920-
while(dma_busy); // Wait for last line to complete
928+
if(block) {
929+
while(dma_busy); // Wait for last line to complete
921930
#if defined(__SAMD51__)
922-
if(connection == TFT_HARD_SPI) {
923-
// See SAMD51 note in writeColor()
924-
hwspi._spi->setDataMode(SPI_MODE0);
925-
} else {
926-
pinPeripheral(tft8._wr, PIO_OUTPUT); // Switch WR back to GPIO
927-
}
931+
if(connection == TFT_HARD_SPI) {
932+
// See SAMD51 note in writeColor()
933+
hwspi._spi->setDataMode(SPI_MODE0);
934+
} else {
935+
pinPeripheral(tft8._wr, PIO_OUTPUT); // Switch WR back to GPIO
936+
}
928937
#endif // end __SAMD51__
938+
}
929939
return;
930940
}
931941
#endif // end USE_SPI_DMA
@@ -937,6 +947,26 @@ void Adafruit_SPITFT::writePixels(uint16_t *colors, uint32_t len) {
937947
}
938948
}
939949

950+
/*!
951+
@brief Wait for the last DMA transfer in a prior non-blocking
952+
writePixels() call to complete. This does nothing if DMA
953+
is not enabled, and is not needed if blocking writePixels()
954+
was used (as is the default case).
955+
*/
956+
void Adafruit_SPITFT::dmaWait(void) {
957+
#if defined(USE_SPI_DMA)
958+
while(dma_busy);
959+
#if defined(__SAMD51__)
960+
if(connection == TFT_HARD_SPI) {
961+
// See SAMD51 note in writeColor()
962+
hwspi._spi->setDataMode(SPI_MODE0);
963+
} else {
964+
pinPeripheral(tft8._wr, PIO_OUTPUT); // Switch WR back to GPIO
965+
}
966+
#endif // end __SAMD51__
967+
#endif
968+
}
969+
940970
/*!
941971
@brief Issue a series of pixels, all the same color. Not self-
942972
contained; should follow startWrite() and setAddrWindow() calls.

Adafruit_SPITFT.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class Adafruit_SPITFT : public Adafruit_GFX {
191191
// before ending the transaction. It's more efficient than starting a
192192
// transaction every time.
193193
void writePixel(int16_t x, int16_t y, uint16_t color);
194-
void writePixels(uint16_t *colors, uint32_t len);
194+
void writePixels(uint16_t *colors, uint32_t len, bool block=true);
195195
void writeColor(uint16_t color, uint32_t len);
196196
void writeFillRect(int16_t x, int16_t y, int16_t w, int16_t h,
197197
uint16_t color);
@@ -206,6 +206,10 @@ class Adafruit_SPITFT : public Adafruit_GFX {
206206
// CALLING THIS WITH UNCLIPPED OR NEGATIVE VALUES COULD BE DISASTROUS.
207207
inline void writeFillRectPreclipped(int16_t x, int16_t y,
208208
int16_t w, int16_t h, uint16_t color);
209+
// Another new function, companion to the new non-blocking
210+
// writePixels() variant.
211+
void dmaWait(void);
212+
209213

210214
// These functions are similar to the 'write' functions above, but with
211215
// a chip-select and/or SPI transaction built-in. They're typically used

0 commit comments

Comments
 (0)