Skip to content

Commit

Permalink
Add option for a different TFT read SPI frequency
Browse files Browse the repository at this point in the history
#define SPI_READ_FREQUENCY  20000000 // Optional reduced SPI frequency
for reading TFT

Also weeded out some compiler warnings
  • Loading branch information
Bodmer committed Jul 29, 2018
1 parent 40e2c8d commit 73c1831
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 24 deletions.
5 changes: 4 additions & 1 deletion Extensions/Smooth_font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,10 @@ uint16_t TFT_eSPI::decodeUTF8(uint8_t *buf, uint16_t *index, uint16_t remaining)

// 16 bit Unicode
if (((c & 0xF0) == 0xE0) && (remaining > 2))
return ((c & 0x0F)<<12) | ((buf[(*index)++]&0x3F)<<6) | ((buf[(*index)++]&0x3F));
{
c = ((c & 0x0F)<<12) | ((buf[(*index)++]&0x3F)<<6);
return c | ((buf[(*index)++]&0x3F));
}

// 21 bit Unicode not supported so fall-back to extended ASCII
// if ((c & 0xF8) == 0xF0) return c;
Expand Down
12 changes: 6 additions & 6 deletions Extensions/Sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,13 +517,14 @@ void TFT_eSprite::pushColor(uint32_t color, uint16_t len)
if (!_created ) return;

uint16_t pixelColor;

if (_bpp == 16)
pixelColor = (uint16_t) (color >> 8) | (color << 8);

else if (_bpp == 8)
pixelColor = (color & 0xE000)>>8 | (color & 0x0700)>>6 | (color & 0x0018)>>3;

// else Nothing to do for 1bpp
else pixelColor = (uint16_t) color; // for 1bpp

while(len--) writeColor(pixelColor);
}
Expand Down Expand Up @@ -1213,11 +1214,11 @@ void TFT_eSprite::drawChar(int32_t x, int32_t y, unsigned char c, uint32_t color

uint16_t bo = pgm_read_word(&glyph->bitmapOffset);
uint8_t w = pgm_read_byte(&glyph->width),
h = pgm_read_byte(&glyph->height),
xa = pgm_read_byte(&glyph->xAdvance);
h = pgm_read_byte(&glyph->height);
//xa = pgm_read_byte(&glyph->xAdvance);
int8_t xo = pgm_read_byte(&glyph->xOffset),
yo = pgm_read_byte(&glyph->yOffset);
uint8_t xx, yy, bits, bit=0;
uint8_t xx, yy, bits=0, bit=0;
int16_t xo16 = 0, yo16 = 0;

if(size > 1) {
Expand Down Expand Up @@ -1609,7 +1610,6 @@ void TFT_eSprite::printToSprite(char *cbuffer, int len) //String string)
}

createSprite(sWidth, this->gFont.yAdvance);
uint16_t transparent = TFT_BLACK;

if (this->textbgcolor != TFT_BLACK) fillSprite(this->textbgcolor);
}
Expand Down Expand Up @@ -1646,7 +1646,7 @@ int16_t TFT_eSprite::printToSprite(int16_t x, int16_t y, uint16_t index)
if (newSprite)
{
createSprite(sWidth, this->gFont.yAdvance);
uint16_t transparent = TFT_BLACK;

if (this->textbgcolor != TFT_BLACK) fillSprite(this->textbgcolor);

drawGlyph(this->gUnicode[index]);
Expand Down
48 changes: 35 additions & 13 deletions TFT_eSPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ inline void TFT_eSPI::spi_end(void){
#endif
}

inline void TFT_eSPI::spi_begin_read(void){
#if defined (SPI_HAS_TRANSACTION) && defined (SUPPORT_TRANSACTIONS) && !defined(ESP32_PARALLEL)
if (locked) {locked = false; SPI.beginTransaction(SPISettings(SPI_READ_FREQUENCY, MSBFIRST, TFT_SPI_MODE));}
#else
#if !defined(ESP32_PARALLEL)
SPI.setFrequency(SPI_READ_FREQUENCY);
#endif
#endif
}

inline void TFT_eSPI::spi_end_read(void){
#if defined (SPI_HAS_TRANSACTION) && defined (SUPPORT_TRANSACTIONS) && !defined(ESP32_PARALLEL)
if(!inTransaction) {if (!locked) {locked = true; SPI.endTransaction();}}
#else
#if !defined(ESP32_PARALLEL)
SPI.setFrequency(SPI_FREQUENCY);
#endif
#endif
}

#if defined (TOUCH_CS) && defined (SPI_TOUCH_FREQUENCY) // && !defined(ESP32_PARALLEL)

inline void TFT_eSPI::spi_begin_touch(void){
Expand Down Expand Up @@ -495,7 +515,7 @@ uint8_t TFT_eSPI::readcommand8(uint8_t cmd_function, uint8_t index)

#else
// for ILI9341 Interface II i.e. IM [3:0] = "1101"
spi_begin();
spi_begin_read();
index = 0x10 + (index & 0x0F);

DC_C;
Expand All @@ -512,7 +532,7 @@ uint8_t TFT_eSPI::readcommand8(uint8_t cmd_function, uint8_t index)
reg = tft_Write_8(0);
CS_H;

spi_end();
spi_end_read();
#endif
return reg;
}
Expand All @@ -526,7 +546,7 @@ uint16_t TFT_eSPI::readcommand16(uint8_t cmd_function, uint8_t index)
{
uint32_t reg;

reg |= (readcommand8(cmd_function, index + 0) << 8);
reg = (readcommand8(cmd_function, index + 0) << 8);
reg |= (readcommand8(cmd_function, index + 1) << 0);

return reg;
Expand Down Expand Up @@ -597,7 +617,7 @@ uint16_t TFT_eSPI::readPixel(int32_t x0, int32_t y0)

#else // Not ESP32_PARALLEL

spi_begin();
spi_begin_read();

readAddrWindow(x0, y0, x0, y0); // Sets CS low

Expand All @@ -620,7 +640,7 @@ uint16_t TFT_eSPI::readPixel(int32_t x0, int32_t y0)

CS_H;

spi_end();
spi_end_read();

return color565(r, g, b);

Expand Down Expand Up @@ -733,7 +753,7 @@ void TFT_eSPI::readRect(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint16_t

#else // Not ESP32_PARALLEL

spi_begin();
spi_begin_read();

readAddrWindow(x, y, x + w - 1, y + h - 1); // Sets CS low

Expand Down Expand Up @@ -764,7 +784,7 @@ void TFT_eSPI::readRect(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint16_t

CS_H;

spi_end();
spi_end_read();
#endif
}

Expand Down Expand Up @@ -1183,7 +1203,7 @@ void TFT_eSPI::pushImage(int32_t x, int32_t y, uint32_t w, uint32_t h, uint8_t *
uint8_t msbColor = 0;
uint8_t lsbColor = 0;

int32_t spx = x, spy = y;
//int32_t spx = x, spy = y;

while (dh--)
{
Expand Down Expand Up @@ -1317,7 +1337,7 @@ bool TFT_eSPI::getSwapBytes(void)
void TFT_eSPI::readRectRGB(int32_t x0, int32_t y0, int32_t w, int32_t h, uint8_t *data)
{
#if !defined(ESP32_PARALLEL)
spi_begin();
spi_begin_read();

readAddrWindow(x0, y0, x0 + w - 1, y0 + h - 1); // Sets CS low

Expand Down Expand Up @@ -1347,7 +1367,7 @@ void TFT_eSPI::readRectRGB(int32_t x0, int32_t y0, int32_t w, int32_t h, uint8_
}
CS_H;

spi_end();
spi_end_read();
#endif
}

Expand Down Expand Up @@ -2301,11 +2321,11 @@ void TFT_eSPI::drawChar(int32_t x, int32_t y, unsigned char c, uint32_t color, u

uint16_t bo = pgm_read_word(&glyph->bitmapOffset);
uint8_t w = pgm_read_byte(&glyph->width),
h = pgm_read_byte(&glyph->height),
xa = pgm_read_byte(&glyph->xAdvance);
h = pgm_read_byte(&glyph->height);
//xa = pgm_read_byte(&glyph->xAdvance);
int8_t xo = pgm_read_byte(&glyph->xOffset),
yo = pgm_read_byte(&glyph->yOffset);
uint8_t xx, yy, bits, bit=0;
uint8_t xx, yy, bits=0, bit=0;
int16_t xo16 = 0, yo16 = 0;

if(size > 1) {
Expand Down Expand Up @@ -4054,8 +4074,10 @@ int16_t TFT_eSPI::drawChar(unsigned int uniCode, int x, int y, int font)
//spi_begin();
setAddrWindow(x, y, x + width - 1, y + height - 1);

#ifdef RPI_WRITE_STROBE
uint8_t textcolorBin[] = { (uint8_t) (textcolor >> 8), (uint8_t) textcolor };
uint8_t textbgcolorBin[] = { (uint8_t) (textbgcolor >> 8), (uint8_t) textbgcolor };
#endif

// Maximum font size is equivalent to 180x180 pixels in area
while (w > 0)
Expand Down
13 changes: 11 additions & 2 deletions TFT_eSPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
#define SPI_FREQUENCY 20000000
#endif

// If the frequency is not defined, set a default
#ifndef SPI_READ_FREQUENCY
#define SPI_READ_FREQUENCY SPI_FREQUENCY
#endif

#ifdef ST7789_DRIVER
#define TFT_SPI_MODE SPI_MODE3
#else
Expand Down Expand Up @@ -213,8 +218,9 @@
#define set_mask(C) xset_mask[C] // 63fps Sprite rendering test 33% faster, graphicstest only 1.8% faster than shifting in real time

// Real-time shifting alternative to above to save 1KByte RAM, 47 fps Sprite rendering test
//#define set_mask(C) ((C&0x80)>>7)<<TFT_D7 | ((C&0x40)>>6)<<TFT_D6 | ((C&0x20)>>5)<<TFT_D5 | ((C&0x10)>>4)<<TFT_D4 | \
/*#define set_mask(C) ((C&0x80)>>7)<<TFT_D7 | ((C&0x40)>>6)<<TFT_D6 | ((C&0x20)>>5)<<TFT_D5 | ((C&0x10)>>4)<<TFT_D4 | \
((C&0x08)>>3)<<TFT_D3 | ((C&0x04)>>2)<<TFT_D2 | ((C&0x02)>>1)<<TFT_D1 | ((C&0x01)>>0)<<TFT_D0
//*/

// Write 8 bits to TFT
#define tft_Write_8(C) GPIO.out_w1tc = clr_mask; GPIO.out_w1ts = set_mask((uint8_t)C); WR_H
Expand Down Expand Up @@ -681,6 +687,9 @@ class TFT_eSPI : public Print {
inline void spi_begin() __attribute__((always_inline));
inline void spi_end() __attribute__((always_inline));

inline void spi_begin_read() __attribute__((always_inline));
inline void spi_end_read() __attribute__((always_inline));

void readAddrWindow(int32_t xs, int32_t ys, int32_t xe, int32_t ye);

uint8_t tabcolor,
Expand Down Expand Up @@ -716,7 +725,7 @@ class TFT_eSPI : public Print {

bool _booted;

int32_t _lastColor;
uint32_t _lastColor;

#ifdef LOAD_GFXFF
GFXfont *gfxFont;
Expand Down
2 changes: 2 additions & 0 deletions User_Setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@
// #define SPI_FREQUENCY 40000000 // Maximum to use SPIFFS
// #define SPI_FREQUENCY 80000000

#define SPI_READ_FREQUENCY 20000000 // Optional reduced SPI frequency for reading TFT

// The XPT2046 requires a lower SPI clock rate of 2.5MHz so we define that here:
#define SPI_TOUCH_FREQUENCY 2500000

Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "TFT_eSPI",
"version": "0.20.17",
"version": "0.20.18",
"keywords": "tft, ePaper, display, ESP8266, NodeMCU, ESP32, M5Stack, ILI9341, ST7735, ILI9163, S6D02A1, ILI9486, ST7789",
"description": "A TFT and ePaper SPI graphics library for ESP8266 and ESP32",
"repository":
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=TFT_eSPI
version=0.20.17
version=0.20.18
author=Bodmer
maintainer=Bodmer
sentence=A fast TFT library for ESP8266 processors and the Arduino IDE
Expand Down

0 comments on commit 73c1831

Please sign in to comment.