Lib for the new LCD Display with ILI9341 controller
Diff: SPI_TFT_ILI9341.cpp
- Revision:
- 11:59eca2723ec5
- Parent:
- 10:50f88bd5557f
- Child:
- 12:98cc5c193ecd
--- a/SPI_TFT_ILI9341.cpp Sun Jun 22 22:32:01 2014 +0000 +++ b/SPI_TFT_ILI9341.cpp Tue Jun 24 15:37:52 2014 +0000 @@ -16,6 +16,10 @@ // 22.10.13 Fixes for Kinetis Board - 8 bit spi // 26.01.14 Change interface for BMP_16 to also use SD-cards // 23.06.14 switch back to old Version - fork for L152 +// 24.06.14 Add compiler flag for optimized L152 version + +// exclude this file for platforms with optimized version +#ifndef TARGET_NUCLEO_L152RE #include "SPI_TFT_ILI9341.h" #include "mbed.h" @@ -26,11 +30,13 @@ //extern DigitalOut xx; // debug !! SPI_TFT_ILI9341::SPI_TFT_ILI9341(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset, PinName dc, const char *name) - : GraphicsDisplay(name), _spi(mosi, miso, sclk), _cs(cs), _reset(reset), _dc(dc) + : GraphicsDisplay(name), SPI(mosi, miso, sclk,NC), _cs(cs), _reset(reset), _dc(dc) { - clk = sclk; + orientation = 0; char_x = 0; + SPI::format(8,3); // 8 bit spi mode 3 + SPI::frequency(10000000); // 10 Mhz SPI clock tft_reset(); } @@ -54,16 +60,16 @@ wr_cmd(0x36); // MEMORY_ACCESS_CONTROL switch (orientation) { case 0: - _spi.write(0x48); + SPI::write(0x48); break; case 1: - _spi.write(0x28); + SPI::write(0x28); break; case 2: - _spi.write(0x88); + SPI::write(0x88); break; case 3: - _spi.write(0xE8); + SPI::write(0xE8); break; } _cs = 1; @@ -77,7 +83,7 @@ { _dc = 0; _cs = 0; - _spi.write(cmd); // mbed lib + SPI::write(cmd); // mbed lib _dc = 1; } @@ -85,7 +91,7 @@ void SPI_TFT_ILI9341::wr_dat(unsigned char dat) { - _spi.write(dat); // mbed lib + SPI::write(dat); // mbed lib } @@ -97,9 +103,9 @@ char r; _dc = 0; _cs = 0; - _spi.write(cmd); // mbed lib + SPI::write(cmd); // mbed lib _cs = 1; - r = _spi.write(0xff); + r = SPI::write(0xff); _cs = 1; return(r); } @@ -113,17 +119,17 @@ _cs = 0; d = cmd; d = d << 1; - _spi.format(9,3); // we have to add a dummy clock cycle - _spi.write(d); - _spi.format(8,3); + SPI::format(9,3); // we have to add a dummy clock cycle + SPI::write(d); + SPI::format(8,3); _dc = 1; - r = _spi.write(0xff); + r = SPI::write(0xff); d = r; - r = _spi.write(0xff); + r = SPI::write(0xff); d = (d << 8) | r; - r = _spi.write(0xff); + r = SPI::write(0xff); d = (d << 8) | r; - r = _spi.write(0xff); + r = SPI::write(0xff); d = (d << 8) | r; _cs = 1; return(d); @@ -143,8 +149,6 @@ void SPI_TFT_ILI9341::tft_reset() { - _spi.format(8,3); // 8 bit spi mode 3 - _spi.frequency(10000000); // 10 Mhz SPI clock _cs = 1; // cs high _dc = 1; // dc high _reset = 0; // display reset @@ -159,113 +163,113 @@ /* Start Initial Sequence ----------------------------------------------------*/ wr_cmd(0xCF); - _spi.write(0x00); - _spi.write(0x83); - _spi.write(0x30); + SPI::write(0x00); + SPI::write(0x83); + SPI::write(0x30); _cs = 1; wr_cmd(0xED); - _spi.write(0x64); - _spi.write(0x03); - _spi.write(0x12); - _spi.write(0x81); + SPI::write(0x64); + SPI::write(0x03); + SPI::write(0x12); + SPI::write(0x81); _cs = 1; wr_cmd(0xE8); - _spi.write(0x85); - _spi.write(0x01); - _spi.write(0x79); + SPI::write(0x85); + SPI::write(0x01); + SPI::write(0x79); _cs = 1; wr_cmd(0xCB); - _spi.write(0x39); - _spi.write(0x2C); - _spi.write(0x00); - _spi.write(0x34); - _spi.write(0x02); + SPI::write(0x39); + SPI::write(0x2C); + SPI::write(0x00); + SPI::write(0x34); + SPI::write(0x02); _cs = 1; wr_cmd(0xF7); - _spi.write(0x20); + SPI::write(0x20); _cs = 1; wr_cmd(0xEA); - _spi.write(0x00); - _spi.write(0x00); + SPI::write(0x00); + SPI::write(0x00); _cs = 1; wr_cmd(0xC0); // POWER_CONTROL_1 - _spi.write(0x26); + SPI::write(0x26); _cs = 1; wr_cmd(0xC1); // POWER_CONTROL_2 - _spi.write(0x11); + SPI::write(0x11); _cs = 1; wr_cmd(0xC5); // VCOM_CONTROL_1 - _spi.write(0x35); - _spi.write(0x3E); + SPI::write(0x35); + SPI::write(0x3E); _cs = 1; wr_cmd(0xC7); // VCOM_CONTROL_2 - _spi.write(0xBE); + SPI::write(0xBE); _cs = 1; wr_cmd(0x36); // MEMORY_ACCESS_CONTROL - _spi.write(0x48); + SPI::write(0x48); _cs = 1; wr_cmd(0x3A); // COLMOD_PIXEL_FORMAT_SET - _spi.write(0x55); // 16 bit pixel + SPI::write(0x55); // 16 bit pixel _cs = 1; wr_cmd(0xB1); // Frame Rate - _spi.write(0x00); - _spi.write(0x1B); + SPI::write(0x00); + SPI::write(0x1B); _cs = 1; wr_cmd(0xF2); // Gamma Function Disable - _spi.write(0x08); + SPI::write(0x08); _cs = 1; wr_cmd(0x26); - _spi.write(0x01); // gamma set for curve 01/2/04/08 + SPI::write(0x01); // gamma set for curve 01/2/04/08 _cs = 1; wr_cmd(0xE0); // positive gamma correction - _spi.write(0x1F); - _spi.write(0x1A); - _spi.write(0x18); - _spi.write(0x0A); - _spi.write(0x0F); - _spi.write(0x06); - _spi.write(0x45); - _spi.write(0x87); - _spi.write(0x32); - _spi.write(0x0A); - _spi.write(0x07); - _spi.write(0x02); - _spi.write(0x07); - _spi.write(0x05); - _spi.write(0x00); + SPI::write(0x1F); + SPI::write(0x1A); + SPI::write(0x18); + SPI::write(0x0A); + SPI::write(0x0F); + SPI::write(0x06); + SPI::write(0x45); + SPI::write(0x87); + SPI::write(0x32); + SPI::write(0x0A); + SPI::write(0x07); + SPI::write(0x02); + SPI::write(0x07); + SPI::write(0x05); + SPI::write(0x00); _cs = 1; wr_cmd(0xE1); // negativ gamma correction - _spi.write(0x00); - _spi.write(0x25); - _spi.write(0x27); - _spi.write(0x05); - _spi.write(0x10); - _spi.write(0x09); - _spi.write(0x3A); - _spi.write(0x78); - _spi.write(0x4D); - _spi.write(0x05); - _spi.write(0x18); - _spi.write(0x0D); - _spi.write(0x38); - _spi.write(0x3A); - _spi.write(0x1F); + SPI::write(0x00); + SPI::write(0x25); + SPI::write(0x27); + SPI::write(0x05); + SPI::write(0x10); + SPI::write(0x09); + SPI::write(0x3A); + SPI::write(0x78); + SPI::write(0x4D); + SPI::write(0x05); + SPI::write(0x18); + SPI::write(0x0D); + SPI::write(0x38); + SPI::write(0x3A); + SPI::write(0x1F); _cs = 1; WindowMax (); @@ -277,14 +281,14 @@ //_cs = 1; wr_cmd(0xB7); // entry mode - _spi.write(0x07); + SPI::write(0x07); _cs = 1; wr_cmd(0xB6); // display function control - _spi.write(0x0A); - _spi.write(0x82); - _spi.write(0x27); - _spi.write(0x00); + SPI::write(0x0A); + SPI::write(0x82); + SPI::write(0x27); + SPI::write(0x00); _cs = 1; wr_cmd(0x11); // sleep out @@ -303,21 +307,21 @@ void SPI_TFT_ILI9341::pixel(int x, int y, int color) { wr_cmd(0x2A); - _spi.write(x >> 8); - _spi.write(x); + SPI::write(x >> 8); + SPI::write(x); _cs = 1; wr_cmd(0x2B); - _spi.write(y >> 8); - _spi.write(y); + SPI::write(y >> 8); + SPI::write(y); _cs = 1; wr_cmd(0x2C); // send pixel #if defined TARGET_KL25Z // 8 Bit SPI - _spi.write(color >> 8); - _spi.write(color & 0xff); + SPI::write(color >> 8); + SPI::write(color & 0xff); #else - _spi.format(16,3); // switch to 16 bit Mode 3 - _spi.write(color); // Write D0..D15 - _spi.format(8,3); + SPI::format(16,3); // switch to 16 bit Mode 3 + SPI::write(color); // Write D0..D15 + SPI::format(8,3); #endif _cs = 1; } @@ -326,17 +330,17 @@ void SPI_TFT_ILI9341::window (unsigned int x, unsigned int y, unsigned int w, unsigned int h) { wr_cmd(0x2A); - _spi.write(x >> 8); - _spi.write(x); - _spi.write((x+w-1) >> 8); - _spi.write(x+w-1); + SPI::write(x >> 8); + SPI::write(x); + SPI::write((x+w-1) >> 8); + SPI::write(x+w-1); _cs = 1; wr_cmd(0x2B); - _spi.write(y >> 8); - _spi.write(y); - _spi.write((y+h-1) >> 8); - _spi.write(y+h-1); + SPI::write(y >> 8); + SPI::write(y); + SPI::write((y+h-1) >> 8); + SPI::write(y+h-1); _cs = 1; } @@ -356,16 +360,16 @@ #if defined TARGET_KL25Z // 8 Bit SPI unsigned int i; for (i = 0; i < ( width() * height()); i++){ - _spi.write(_background >> 8); - _spi.write(_background & 0xff); + SPI::write(_background >> 8); + SPI::write(_background & 0xff); } #else - _spi.format(16,3); // switch to 16 bit Mode 3 + SPI::format(16,3); // switch to 16 bit Mode 3 unsigned int i; for (i = 0; i < ( width() * height()); i++) - _spi.write(_background); - _spi.format(8,3); + SPI::write(_background); + SPI::format(8,3); #endif _cs = 1; } @@ -415,16 +419,16 @@ #if defined TARGET_KL25Z // 8 Bit SPI int j; for (j=0; j<w; j++) { - _spi.write(color >> 8); - _spi.write(color & 0xff); + SPI::write(color >> 8); + SPI::write(color & 0xff); } #else - _spi.format(16,3); // switch to 16 bit Mode 3 + SPI::format(16,3); // switch to 16 bit Mode 3 int j; for (j=0; j<w; j++) { - _spi.write(color); + SPI::write(color); } - _spi.format(8,3); + SPI::format(8,3); #endif _cs = 1; WindowMax(); @@ -439,15 +443,15 @@ wr_cmd(0x2C); // send pixel #if defined TARGET_KL25Z // 8 Bit SPI for (int y=0; y<h; y++) { - _spi.write(color >> 8); - _spi.write(color & 0xff); + SPI::write(color >> 8); + SPI::write(color & 0xff); } #else - _spi.format(16,3); // switch to 16 bit Mode 3 + SPI::format(16,3); // switch to 16 bit Mode 3 for (int y=0; y<h; y++) { - _spi.write(color); + SPI::write(color); } - _spi.format(8,3); + SPI::format(8,3); #endif _cs = 1; WindowMax(); @@ -558,15 +562,15 @@ wr_cmd(0x2C); // send pixel #if defined TARGET_KL25Z // 8 Bit SPI for (int p=0; p<pixel; p++) { - _spi.write(color >> 8); - _spi.write(color & 0xff); + SPI::write(color >> 8); + SPI::write(color & 0xff); } #else - _spi.format(16,3); // switch to 16 bit Mode 3 + SPI::format(16,3); // switch to 16 bit Mode 3 for (int p=0; p<pixel; p++) { - _spi.write(color); + SPI::write(color); } - _spi.format(8,3); + SPI::format(8,3); #endif _cs = 1; WindowMax(); @@ -635,7 +639,7 @@ window(char_x, char_y,hor,vert); // char box wr_cmd(0x2C); // send pixel #ifndef TARGET_KL25Z // 16 Bit SPI - _spi.format(16,3); + SPI::format(16,3); #endif // switch to 16 bit Mode 3 zeichen = &font[((c -32) * offset) + 4]; // start of char bitmap w = zeichen[0]; // width of actual char @@ -645,24 +649,24 @@ b = 1 << (j & 0x07); if (( z & b ) == 0x00) { #ifndef TARGET_KL25Z // 16 Bit SPI - _spi.write(_background); + SPI::write(_background); #else - _spi.write(_background >> 8); - _spi.write(_background & 0xff); + SPI::write(_background >> 8); + SPI::write(_background & 0xff); #endif } else { #ifndef TARGET_KL25Z // 16 Bit SPI - _spi.write(_foreground); + SPI::write(_foreground); #else - _spi.write(_foreground >> 8); - _spi.write(_foreground & 0xff); + SPI::write(_foreground >> 8); + SPI::write(_foreground & 0xff); #endif } } } _cs = 1; #ifndef TARGET_KL25Z // 16 Bit SPI - _spi.format(8,3); + SPI::format(8,3); #endif WindowMax(); if ((w + 2) < hor) { // x offset to next char @@ -698,17 +702,17 @@ bitmap_ptr += ((h - 1)* (w + padd)); wr_cmd(0x2C); // send pixel #ifndef TARGET_KL25Z // 16 Bit SPI - _spi.format(16,3); + SPI::format(16,3); #endif // switch to 16 bit Mode 3 for (j = 0; j < h; j++) { //Lines for (i = 0; i < w; i++) { // one line #if defined TARGET_KL25Z // 8 Bit SPI pix_temp = *bitmap_ptr; - _spi.write(pix_temp >> 8); - _spi.write(pix_temp); + SPI::write(pix_temp >> 8); + SPI::write(pix_temp); bitmap_ptr++; #else - _spi.write(*bitmap_ptr); // one line + SPI::write(*bitmap_ptr); // one line bitmap_ptr++; #endif } @@ -717,7 +721,7 @@ } _cs = 1; #ifndef TARGET_KL25Z // 16 Bit SPI - _spi.format(8,3); + SPI::format(8,3); #endif WindowMax(); } @@ -790,7 +794,7 @@ window(x, y,PixelWidth ,PixelHeigh); wr_cmd(0x2C); // send pixel #ifndef TARGET_KL25Z // only 8 Bit SPI - _spi.format(16,3); + SPI::format(16,3); #endif // switch to 16 bit Mode 3 for (j = PixelHeigh - 1; j >= 0; j--) { //Lines bottom up off = j * (PixelWidth * 2 + padd) + start_data; // start of line @@ -798,17 +802,19 @@ fread(line,1,PixelWidth * 2,Image); // read a line - slow for (i = 0; i < PixelWidth; i++) { // copy pixel data to TFT #ifndef TARGET_KL25Z // only 8 Bit SPI - _spi.write(line[i]); // one 16 bit pixel + SPI::write(line[i]); // one 16 bit pixel #else - _spi.write(line[i] >> 8); - _spi.write(line[i]); + SPI::write(line[i] >> 8); + SPI::write(line[i]); #endif } } _cs = 1; - _spi.format(8,3); + SPI::format(8,3); free (line); fclose(Image); WindowMax(); return(1); } + +#endif \ No newline at end of file