Skip to content

Commit 0c7f923

Browse files
committed
Add SPI transactions to TFT library.
1 parent b104175 commit 0c7f923

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

libraries/TFT/src/utility/Adafruit_ST7735.cpp

+47-1
Original file line numberDiff line numberDiff line change
@@ -73,25 +73,37 @@ inline void Adafruit_ST7735::spiwrite(uint8_t c) {
7373

7474

7575
void Adafruit_ST7735::writecommand(uint8_t c) {
76+
#ifdef SPI_HAS_TRANSACTION
77+
if (hwSPI) SPI.beginTransaction(spisettings);
78+
#endif
7679
*rsport &= ~rspinmask;
7780
*csport &= ~cspinmask;
7881

7982
//Serial.print("C ");
8083
spiwrite(c);
8184

8285
*csport |= cspinmask;
86+
#ifdef SPI_HAS_TRANSACTION
87+
if (hwSPI) SPI.endTransaction();
88+
#endif
8389
}
8490

8591

8692
void Adafruit_ST7735::writedata(uint8_t c) {
93+
#ifdef SPI_HAS_TRANSACTION
94+
if (hwSPI) SPI.beginTransaction(spisettings);
95+
#endif
8796
*rsport |= rspinmask;
8897
*csport &= ~cspinmask;
8998

9099
//Serial.print("D ");
91100
spiwrite(c);
92101

93102
*csport |= cspinmask;
94-
}
103+
#ifdef SPI_HAS_TRANSACTION
104+
if (hwSPI) SPI.endTransaction();
105+
#endif
106+
}
95107

96108

97109
// Rather than a bazillion writecommand() and writedata() calls, screen
@@ -331,13 +343,17 @@ void Adafruit_ST7735::commonInit(const uint8_t *cmdList) {
331343

332344
if(hwSPI) { // Using hardware SPI
333345
SPI.begin();
346+
#ifdef SPI_HAS_TRANSACTION
347+
spisettings = SPISettings(4000000L, MSBFIRST, SPI_MODE0);
348+
#else
334349
#if defined(ARDUINO_ARCH_SAM)
335350
SPI.setClockDivider(24); // 4 MHz (half speed)
336351
#else
337352
SPI.setClockDivider(SPI_CLOCK_DIV4); // 4 MHz (half speed)
338353
#endif
339354
SPI.setBitOrder(MSBFIRST);
340355
SPI.setDataMode(SPI_MODE0);
356+
#endif // SPI_HAS_TRANSACTION
341357
} else {
342358
pinMode(_sclk, OUTPUT);
343359
pinMode(_sid , OUTPUT);
@@ -413,6 +429,9 @@ void Adafruit_ST7735::setAddrWindow(uint8_t x0, uint8_t y0, uint8_t x1,
413429

414430

415431
void Adafruit_ST7735::pushColor(uint16_t color) {
432+
#ifdef SPI_HAS_TRANSACTION
433+
if (hwSPI) SPI.beginTransaction(spisettings);
434+
#endif
416435
*rsport |= rspinmask;
417436
*csport &= ~cspinmask;
418437

@@ -421,6 +440,9 @@ void Adafruit_ST7735::pushColor(uint16_t color) {
421440
spiwrite(color);
422441

423442
*csport |= cspinmask;
443+
#ifdef SPI_HAS_TRANSACTION
444+
if (hwSPI) SPI.endTransaction();
445+
#endif
424446
}
425447

426448
void Adafruit_ST7735::drawPixel(int16_t x, int16_t y, uint16_t color) {
@@ -429,6 +451,9 @@ void Adafruit_ST7735::drawPixel(int16_t x, int16_t y, uint16_t color) {
429451

430452
setAddrWindow(x,y,x+1,y+1);
431453

454+
#ifdef SPI_HAS_TRANSACTION
455+
if (hwSPI) SPI.beginTransaction(spisettings);
456+
#endif
432457
*rsport |= rspinmask;
433458
*csport &= ~cspinmask;
434459

@@ -438,6 +463,9 @@ void Adafruit_ST7735::drawPixel(int16_t x, int16_t y, uint16_t color) {
438463
spiwrite(color);
439464

440465
*csport |= cspinmask;
466+
#ifdef SPI_HAS_TRANSACTION
467+
if (hwSPI) SPI.endTransaction();
468+
#endif
441469
}
442470

443471

@@ -452,13 +480,19 @@ void Adafruit_ST7735::drawFastVLine(int16_t x, int16_t y, int16_t h,
452480
if (tabcolor == INITR_BLACKTAB) color = swapcolor(color);
453481

454482
uint8_t hi = color >> 8, lo = color;
483+
#ifdef SPI_HAS_TRANSACTION
484+
if (hwSPI) SPI.beginTransaction(spisettings);
485+
#endif
455486
*rsport |= rspinmask;
456487
*csport &= ~cspinmask;
457488
while (h--) {
458489
spiwrite(hi);
459490
spiwrite(lo);
460491
}
461492
*csport |= cspinmask;
493+
#ifdef SPI_HAS_TRANSACTION
494+
if (hwSPI) SPI.endTransaction();
495+
#endif
462496
}
463497

464498

@@ -473,13 +507,19 @@ void Adafruit_ST7735::drawFastHLine(int16_t x, int16_t y, int16_t w,
473507
if (tabcolor == INITR_BLACKTAB) color = swapcolor(color);
474508

475509
uint8_t hi = color >> 8, lo = color;
510+
#ifdef SPI_HAS_TRANSACTION
511+
if (hwSPI) SPI.beginTransaction(spisettings);
512+
#endif
476513
*rsport |= rspinmask;
477514
*csport &= ~cspinmask;
478515
while (w--) {
479516
spiwrite(hi);
480517
spiwrite(lo);
481518
}
482519
*csport |= cspinmask;
520+
#ifdef SPI_HAS_TRANSACTION
521+
if (hwSPI) SPI.endTransaction();
522+
#endif
483523
}
484524

485525

@@ -504,6 +544,9 @@ void Adafruit_ST7735::fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
504544
setAddrWindow(x, y, x+w-1, y+h-1);
505545

506546
uint8_t hi = color >> 8, lo = color;
547+
#ifdef SPI_HAS_TRANSACTION
548+
if (hwSPI) SPI.beginTransaction(spisettings);
549+
#endif
507550
*rsport |= rspinmask;
508551
*csport &= ~cspinmask;
509552
for(y=h; y>0; y--) {
@@ -514,6 +557,9 @@ void Adafruit_ST7735::fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
514557
}
515558

516559
*csport |= cspinmask;
560+
#ifdef SPI_HAS_TRANSACTION
561+
if (hwSPI) SPI.endTransaction();
562+
#endif
517563
}
518564

519565

libraries/TFT/src/utility/Adafruit_ST7735.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "WProgram.h"
2727
#endif
2828
#include "Adafruit_GFX.h"
29+
#include <SPI.h>
2930
#include <avr/pgmspace.h>
3031

3132
// some flags for initR() :(
@@ -135,7 +136,10 @@ class Adafruit_ST7735 : public Adafruit_GFX {
135136
//uint8_t spiread(void);
136137

137138
boolean hwSPI;
138-
#if defined(ARDUINO_ARCH_SAM)
139+
#ifdef SPI_HAS_TRANSACTION
140+
SPISettings spisettings;
141+
#endif
142+
#if defined(ARDUINO_ARCH_SAM)
139143
volatile uint32_t *dataport, *clkport, *csport, *rsport;
140144
uint32_t _cs, _rs, _rst, _sid, _sclk,
141145
datapinmask, clkpinmask, cspinmask, rspinmask,

0 commit comments

Comments
 (0)