Skip to content

Commit

Permalink
Merge pull request arduino#3036 from damellis/tft-spi-transactions
Browse files Browse the repository at this point in the history
Add SPI transactions to TFT library.
  • Loading branch information
agdl committed May 29, 2015
2 parents 7d6518f + 0c7f923 commit 394bab0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
48 changes: 47 additions & 1 deletion libraries/TFT/src/utility/Adafruit_ST7735.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,25 +73,37 @@ inline void Adafruit_ST7735::spiwrite(uint8_t c) {


void Adafruit_ST7735::writecommand(uint8_t c) {
#ifdef SPI_HAS_TRANSACTION
if (hwSPI) SPI.beginTransaction(spisettings);
#endif
*rsport &= ~rspinmask;
*csport &= ~cspinmask;

//Serial.print("C ");
spiwrite(c);

*csport |= cspinmask;
#ifdef SPI_HAS_TRANSACTION
if (hwSPI) SPI.endTransaction();
#endif
}


void Adafruit_ST7735::writedata(uint8_t c) {
#ifdef SPI_HAS_TRANSACTION
if (hwSPI) SPI.beginTransaction(spisettings);
#endif
*rsport |= rspinmask;
*csport &= ~cspinmask;

//Serial.print("D ");
spiwrite(c);

*csport |= cspinmask;
}
#ifdef SPI_HAS_TRANSACTION
if (hwSPI) SPI.endTransaction();
#endif
}


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

if(hwSPI) { // Using hardware SPI
SPI.begin();
#ifdef SPI_HAS_TRANSACTION
spisettings = SPISettings(4000000L, MSBFIRST, SPI_MODE0);
#else
#if defined(ARDUINO_ARCH_SAM)
SPI.setClockDivider(24); // 4 MHz (half speed)
#else
SPI.setClockDivider(SPI_CLOCK_DIV4); // 4 MHz (half speed)
#endif
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
#endif // SPI_HAS_TRANSACTION
} else {
pinMode(_sclk, OUTPUT);
pinMode(_sid , OUTPUT);
Expand Down Expand Up @@ -413,6 +429,9 @@ void Adafruit_ST7735::setAddrWindow(uint8_t x0, uint8_t y0, uint8_t x1,


void Adafruit_ST7735::pushColor(uint16_t color) {
#ifdef SPI_HAS_TRANSACTION
if (hwSPI) SPI.beginTransaction(spisettings);
#endif
*rsport |= rspinmask;
*csport &= ~cspinmask;

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

*csport |= cspinmask;
#ifdef SPI_HAS_TRANSACTION
if (hwSPI) SPI.endTransaction();
#endif
}

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

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

#ifdef SPI_HAS_TRANSACTION
if (hwSPI) SPI.beginTransaction(spisettings);
#endif
*rsport |= rspinmask;
*csport &= ~cspinmask;

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

*csport |= cspinmask;
#ifdef SPI_HAS_TRANSACTION
if (hwSPI) SPI.endTransaction();
#endif
}


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

uint8_t hi = color >> 8, lo = color;
#ifdef SPI_HAS_TRANSACTION
if (hwSPI) SPI.beginTransaction(spisettings);
#endif
*rsport |= rspinmask;
*csport &= ~cspinmask;
while (h--) {
spiwrite(hi);
spiwrite(lo);
}
*csport |= cspinmask;
#ifdef SPI_HAS_TRANSACTION
if (hwSPI) SPI.endTransaction();
#endif
}


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

uint8_t hi = color >> 8, lo = color;
#ifdef SPI_HAS_TRANSACTION
if (hwSPI) SPI.beginTransaction(spisettings);
#endif
*rsport |= rspinmask;
*csport &= ~cspinmask;
while (w--) {
spiwrite(hi);
spiwrite(lo);
}
*csport |= cspinmask;
#ifdef SPI_HAS_TRANSACTION
if (hwSPI) SPI.endTransaction();
#endif
}


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

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

*csport |= cspinmask;
#ifdef SPI_HAS_TRANSACTION
if (hwSPI) SPI.endTransaction();
#endif
}


Expand Down
6 changes: 5 additions & 1 deletion libraries/TFT/src/utility/Adafruit_ST7735.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "WProgram.h"
#endif
#include "Adafruit_GFX.h"
#include <SPI.h>
#include <avr/pgmspace.h>

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

boolean hwSPI;
#if defined(ARDUINO_ARCH_SAM)
#ifdef SPI_HAS_TRANSACTION
SPISettings spisettings;
#endif
#if defined(ARDUINO_ARCH_SAM)
volatile uint32_t *dataport, *clkport, *csport, *rsport;
uint32_t _cs, _rs, _rst, _sid, _sclk,
datapinmask, clkpinmask, cspinmask, rspinmask,
Expand Down

0 comments on commit 394bab0

Please sign in to comment.