Lib for the new LCD Display with ILI9341 controller
Diff: SPI_TFT_ILI9341.cpp
- Revision:
- 6:fe07ae8329f7
- Parent:
- 5:55aed13f2630
- Child:
- 7:4c30bea883bc
--- a/SPI_TFT_ILI9341.cpp Tue Oct 22 20:55:39 2013 +0000 +++ b/SPI_TFT_ILI9341.cpp Sun Jan 26 16:58:45 2014 +0000 @@ -14,19 +14,20 @@ // 14.07.13 Test with real display and bugfix // 18.10.13 Better Circle function from Michael Ammann // 22.10.13 Fixes for Kinetis Board - 8 bit spi +// 26.01.14 Change interface for BMP_16 to also use SD-cards #include "SPI_TFT_ILI9341.h" #include "mbed.h" #define BPP 16 // Bits per pixel - - + //extern Serial pc; //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) : _spi(mosi, miso, sclk), _cs(cs), _reset(reset), _dc(dc), GraphicsDisplay(name) { + clk = sclk; orientation = 0; char_x = 0; tft_reset(); @@ -88,18 +89,53 @@ -// the ILI9341 can read - has to be implemented later -// A read will return 0 at the moment +// the ILI9341 can read + +char SPI_TFT_ILI9341::rd_byte(unsigned char cmd) +{ + char r; + _dc = 0; + _cs = 0; + _spi.write(cmd); // mbed lib + _cs = 1; + r = _spi.write(0xff); + _cs = 1; + return(r); +} -//unsigned short SPI_TFT_ILI9341::rd_dat (void) -//{ -// unsigned short val = 0; +// read 32 bit +int SPI_TFT_ILI9341::rd_32(unsigned char cmd) +{ + int d; + char r; + _dc = 0; + _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); + _dc = 1; + r = _spi.write(0xff); + d = r; + r = _spi.write(0xff); + d = (d << 8) | r; + r = _spi.write(0xff); + d = (d << 8) | r; + r = _spi.write(0xff); + d = (d << 8) | r; + _cs = 1; + return(d); +} - //val = _spi.write(0x73ff); /* Dummy read 1 */ - //val = _spi.write(0x0000); /* Read D8..D15 */ -// return (val); -//} - +int SPI_TFT_ILI9341::Read_ID(void){ + int r; + r = rd_byte(0x0A); + r = rd_byte(0x0A); + r = rd_byte(0x0A); + r = rd_byte(0x0A); + return(r); +} // Init code based on MI0283QT datasheet @@ -107,7 +143,7 @@ void SPI_TFT_ILI9341::tft_reset() { _spi.format(8,3); // 8 bit spi mode 3 - _spi.frequency(10000000); // 10 Mhz SPI clock + _spi.frequency(20000000); // 10 Mhz SPI clock _cs = 1; // cs high _dc = 1; // dc high _reset = 0; // display reset @@ -686,8 +722,7 @@ } -// local filesystem is not implemented in kinetis board -#ifndef TARGET_KL25Z +// local filesystem is not implemented in kinetis board , but you can add a SD card int SPI_TFT_ILI9341::BMP_16(unsigned int x, unsigned int y, const char *Name_BMP) { @@ -707,15 +742,12 @@ unsigned short *line; // get the filename - LocalFileSystem local("local"); - sprintf(&filename[0],"/local/"); - i=7; + i=0; while (*Name_BMP!='\0') { filename[i++]=*Name_BMP++; } - - fprintf(stderr, "filename : %s \n\r",filename); - + filename[i] = 0; + FILE *Image = fopen((const char *)&filename[0], "rb"); // open the bmp file if (!Image) { return(0); // error file not found ! @@ -754,17 +786,22 @@ padd ++; } while ((PixelWidth * 2 + padd)%4 != 0); - -//fseek(Image, 70 ,SEEK_SET); window(x, y,PixelWidth ,PixelHeigh); - wr_cmd(0x2C); // send pixel - _spi.format(16,3); // switch to 16 bit Mode 3 + wr_cmd(0x2C); // send pixel + #ifndef TARGET_KL25Z // only 8 Bit SPI + _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 fseek(Image, off ,SEEK_SET); - fread(line,1,PixelWidth * 2,Image); // read a line - slow ! + 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 + #else + _spi.write(line[i] >> 8); + _spi.write(line[i]); + #endif } } _cs = 1; @@ -774,4 +811,3 @@ WindowMax(); return(1); } -#endif \ No newline at end of file