Adapted from Peter Dresche's original for Waveshare 2.8inch TFT Touch Shield Board and Mbed 6. RGB order reversed by changing reg 16 commands, spi write code adjusted as there is no reset pin but there is data command pin. Wait commands changed for new thread_sleep style, Stream class explicitly included. Library to control a QVGA TFT connected to SPI. You can use printf to print text The lib can handle different fonts, draw lines, circles, rect and bmp
Revision 22:4a0f306be8ef, committed 2020-06-17
- Comitter:
- jhd25
- Date:
- Wed Jun 17 22:01:37 2020 +0100
- Parent:
- 21:d7ae9d59eb03
- Child:
- 23:469bf5f3c8ac
- Commit message:
- Now with mutexes !
Changed in this revision
SPI_TFT.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/SPI_TFT.cpp Wed Jun 17 20:42:10 2020 +0100 +++ b/SPI_TFT.cpp Wed Jun 17 22:01:37 2020 +0100 @@ -99,6 +99,7 @@ void SPI_TFT::wr_cmd(unsigned char cmd) { + _spi.lock(); _cs = 0; #if defined NO_MBED_LIB unsigned short spi_d; @@ -115,23 +116,29 @@ } #else // use mbed lib #if defined TARGET_KL25Z // 8 Bit SPI + _spi.write(0x70); _spi.write(cmd); + #else _reset=0; // 16 Bit SPI unsigned short spi_d; spi_d = 0x7000 | cmd ; + _spi.write(spi_d); // mbed lib + _reset=1; #endif #endif _cs = 1; + _spi.unlock(); } // write data to tft register void SPI_TFT::wr_dat(unsigned char dat) { + _spi.lock(); _cs = 0; _reset=1; #if defined NO_MBED_LIB @@ -158,6 +165,7 @@ #endif #endif _cs = 1; + _spi.unlock(); } @@ -302,7 +310,9 @@ wr_reg(0x07, (y >> 0)); wr_reg(0x06, (y >> 8)); wr_cmd(0x22); + _spi.lock(); _cs = 0; + #if defined NO_MBED_LIB if (spi_port == 0) { // TFT on SSP0 LPC_SSP0->CR0 &= ~(0x08UL); // set to 8 bit @@ -335,6 +345,7 @@ #endif #endif _cs = 1; + _spi.unlock(); } // define draw area @@ -373,6 +384,7 @@ #if defined USE_DMA LPC_GPDMACH0->DMACCSrcAddr = (uint32_t)&color; #endif + _spi.lock(); _cs = 0; if (spi_port == 0) { // TFT on SSP0 #if defined USE_DMA @@ -431,6 +443,7 @@ #endif #else // use mbed lib + _spi.lock(); _cs = 0; #if defined TARGET_KL25Z // 8 Bit SPI _spi.write(SPI_START | SPI_WR | SPI_DATA); // Write : RS = 1, RW = 0 @@ -449,6 +462,7 @@ #endif #endif _cs = 1; + _spi.unlock(); } void SPI_TFT::circle(int x0, int y0, int r, int color) @@ -492,6 +506,7 @@ w = x1 - x0 + 1; window(x0,y,w,1); wr_cmd(0x22); + _spi.lock(); _cs = 0; #if defined NO_MBED_LIB if (spi_port == 0) { // TFT on SSP0 @@ -552,6 +567,7 @@ #endif #endif _cs = 1; + _spi.unlock(); WindowMax(); return; } @@ -563,6 +579,7 @@ h = y1 - y0 + 1; window(x,y0,1,h); wr_cmd(0x22); + _spi.lock(); _cs = 0; #if defined NO_MBED_LIB if (spi_port == 0) { // TFT on SSP0 @@ -623,6 +640,7 @@ #endif #endif _cs = 1; + _spi.unlock(); WindowMax(); return; } @@ -732,6 +750,7 @@ #endif window(x0,y0,w,h); wr_cmd(0x22); + _spi.lock(); _cs = 0; #if defined NO_MBED_LIB if (spi_port == 0) { // TFT on SSP0 @@ -805,6 +824,7 @@ #endif #endif _cs = 1; + _spi.unlock(); WindowMax(); return; } @@ -904,6 +924,7 @@ // copy the buffer with DMA SPI to display dma_off = 0; // offset for DMA transfer + _spi.lock(); _cs = 0; if (spi_port == 0) { // TFT on SSP0 LPC_GPDMACH0->DMACCDestAddr = (uint32_t)&LPC_SSP0->DR; // we send to SSP0 @@ -956,6 +977,7 @@ } #else // no dma + _spi.lock(); _cs = 0; #if defined NO_MBED_LIB if (spi_port == 0) { // TFT on SSP0 @@ -1002,6 +1024,7 @@ } #endif // no DMA _cs = 1; + _spi.unlock(); WindowMax(); if ((w + 2) < hor) { // x offset to next char char_x += w + 2; @@ -1032,6 +1055,7 @@ } while (2*(w + padd)%4 != 0); window(x, y, w, h); wr_cmd(0x22); + _spi.lock(); _cs = 0; #if defined NO_MBED_LIB if (spi_port == 0) { // TFT on SSP0 @@ -1115,6 +1139,7 @@ } #endif // USE MBED LIB _cs = 1; + _spi.unlock(); WindowMax(); } @@ -1192,6 +1217,7 @@ //fseek(Image, 70 ,SEEK_SET); window(x, y,PixelWidth ,PixelHeigh); wr_cmd(0x22); + _spi.lock(); _cs = 0; #if defined NO_MBED_LIB if (spi_port == 0) { // TFT on SSP0 @@ -1255,6 +1281,7 @@ } #endif _cs = 1; + _spi.unlock(); free (line); fclose(Image); WindowMax();