Added Discovery F429ZI 8 bit SPI support. Added helper methods to character based locate.

Fork of SPI_TFT_ILI9341 by Peter Drescher

Added Discovery F429ZI 8 bit SPI support to the original lib.

Revision:
14:a1133923c034
Parent:
13:b2b3e5430f81
Child:
15:b7848705d2ab
--- a/SPI_TFT_ILI9341.cpp	Wed Jun 25 16:51:27 2014 +0000
+++ b/SPI_TFT_ILI9341.cpp	Sat Feb 11 18:56:31 2017 +0000
@@ -9,13 +9,13 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
- 
+
 // 12.06.13 fork from SPI_TFT code because controller is different ...
-// 14.07.13 Test with real display and bugfix 
+// 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
-// 23.06.14 switch back to old Version - fork for L152 
+// 23.06.14 switch back to old Version - fork for L152
 // 24.06.14 Add compiler flag for optimized L152 version
 // 25.06.14 Add optimized F103 version
 
@@ -27,19 +27,19 @@
 #include "SPI_TFT_ILI9341.h"
 #include "mbed.h"
 
-#define BPP         16                  // Bits per pixel    
-         
+#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)
     : GraphicsDisplay(name), SPI(mosi, miso, sclk,NC), _cs(cs), _reset(reset), _dc(dc)
 {
-    
+
     orientation = 0;
     char_x = 0;
     SPI::format(8,3);                  // 8 bit spi mode 3
-    SPI::frequency(10000000);          // 10 Mhz SPI clock
+    SPI::frequency(50000000);          // 10 Mhz SPI clock
     tft_reset();
 }
 
@@ -75,9 +75,9 @@
             SPI::write(0xE8);
             break;
     }
-    _cs = 1; 
+    _cs = 1;
     WindowMax();
-} 
+}
 
 
 // write command to tft register
@@ -99,7 +99,7 @@
 
 
 
-// the ILI9341 can read 
+// the ILI9341 can read
 
 char SPI_TFT_ILI9341::rd_byte(unsigned char cmd)
 {
@@ -109,7 +109,7 @@
     SPI::write(cmd);      // mbed lib
     _cs = 1;
     r = SPI::write(0xff);
-    _cs = 1;    
+    _cs = 1;
     return(r);
 }
 
@@ -124,7 +124,7 @@
     d = d << 1;
     SPI::format(9,3);    // we have to add a dummy clock cycle
     SPI::write(d);
-    SPI::format(8,3);   
+    SPI::format(8,3);
     _dc = 1;
     r = SPI::write(0xff);
     d = r;
@@ -134,7 +134,7 @@
     d = (d << 8) | r;
     r = SPI::write(0xff);
     d = (d << 8) | r;
-    _cs = 1;    
+    _cs = 1;
     return(d);
 }
 
@@ -153,157 +153,157 @@
 void SPI_TFT_ILI9341::tft_reset()
 {
     _cs = 1;                           // cs high
-    _dc = 1;                           // dc high 
+    _dc = 1;                           // dc high
     _reset = 0;                        // display reset
 
     wait_us(50);
     _reset = 1;                       // end hardware reset
     wait_ms(5);
-     
-    wr_cmd(0x01);                     // SW reset  
+
+    wr_cmd(0x01);                     // SW reset
     wait_ms(5);
-    wr_cmd(0x28);                     // display off  
+    wr_cmd(0x28);                     // display off
 
     /* Start Initial Sequence ----------------------------------------------------*/
-     wr_cmd(0xCF);                     
+     wr_cmd(0xCF);
      SPI::write(0x00);
      SPI::write(0x83);
      SPI::write(0x30);
      _cs = 1;
-     
-     wr_cmd(0xED);                     
+
+     wr_cmd(0xED);
      SPI::write(0x64);
      SPI::write(0x03);
      SPI::write(0x12);
      SPI::write(0x81);
      _cs = 1;
-     
-     wr_cmd(0xE8);                     
+
+     wr_cmd(0xE8);
      SPI::write(0x85);
      SPI::write(0x01);
      SPI::write(0x79);
      _cs = 1;
-     
-     wr_cmd(0xCB);                     
+
+     wr_cmd(0xCB);
      SPI::write(0x39);
      SPI::write(0x2C);
      SPI::write(0x00);
      SPI::write(0x34);
      SPI::write(0x02);
      _cs = 1;
-           
-     wr_cmd(0xF7);                     
+
+     wr_cmd(0xF7);
      SPI::write(0x20);
      _cs = 1;
-           
-     wr_cmd(0xEA);                     
+
+     wr_cmd(0xEA);
      SPI::write(0x00);
      SPI::write(0x00);
      _cs = 1;
-     
+
      wr_cmd(0xC0);                     // POWER_CONTROL_1
      SPI::write(0x26);
      _cs = 1;
- 
+
      wr_cmd(0xC1);                     // POWER_CONTROL_2
      SPI::write(0x11);
      _cs = 1;
-     
+
      wr_cmd(0xC5);                     // VCOM_CONTROL_1
      SPI::write(0x35);
      SPI::write(0x3E);
      _cs = 1;
-     
+
      wr_cmd(0xC7);                     // VCOM_CONTROL_2
      SPI::write(0xBE);
-     _cs = 1; 
-     
+     _cs = 1;
+
      wr_cmd(0x36);                     // MEMORY_ACCESS_CONTROL
      SPI::write(0x48);
-     _cs = 1; 
-     
+     _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(0x1B);
      _cs = 1;
-     
+
      wr_cmd(0xF2);                     // Gamma Function Disable
      SPI::write(0x08);
-     _cs = 1; 
-     
-     wr_cmd(0x26);                     
+     _cs = 1;
+
+     wr_cmd(0x26);
      SPI::write(0x01);                 // gamma set for curve 01/2/04/08
-     _cs = 1; 
-     
+     _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(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(0x05); 
+     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(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(0x3A);
      SPI::write(0x1F);
      _cs = 1;
-     
+
      WindowMax ();
-     
+
      //wr_cmd(0x34);                     // tearing effect off
      //_cs = 1;
-     
+
      //wr_cmd(0x35);                     // tearing effect on
      //_cs = 1;
-      
+
      wr_cmd(0xB7);                       // entry mode
      SPI::write(0x07);
      _cs = 1;
-     
+
      wr_cmd(0xB6);                       // display function control
      SPI::write(0x0A);
      SPI::write(0x82);
      SPI::write(0x27);
      SPI::write(0x00);
      _cs = 1;
-     
+
      wr_cmd(0x11);                     // sleep out
      _cs = 1;
-     
+
      wait_ms(100);
-     
+
      wr_cmd(0x29);                     // display on
      _cs = 1;
-     
+
      wait_ms(100);
-     
+
  }
 
 
@@ -318,10 +318,10 @@
     SPI::write(y);
     _cs = 1;
     wr_cmd(0x2C);  // send pixel
-    #if defined TARGET_KL25Z  // 8 Bit SPI
+    #if defined TARGET_KL25Z || defined TARGET_DISCO_F429ZI // 8 Bit SPI
     SPI::write(color >> 8);
     SPI::write(color & 0xff);
-    #else 
+    #else
     SPI::format(16,3);                            // switch to 16 bit Mode 3
     SPI::write(color);                              // Write D0..D15
     SPI::format(8,3);
@@ -337,7 +337,7 @@
     SPI::write(x);
     SPI::write((x+w-1) >> 8);
     SPI::write(x+w-1);
-    
+
     _cs = 1;
     wr_cmd(0x2B);
     SPI::write(y >> 8);
@@ -357,7 +357,7 @@
 
 void SPI_TFT_ILI9341::cls (void)
 {
-   // we can use the fillrect function 
+   // we can use the fillrect function
    fillrect(0,0,width()-1,height()-1,_background);
 }
 
@@ -403,13 +403,13 @@
     w = x1 - x0 + 1;
     window(x0,y,w,1);
     wr_cmd(0x2C);  // send pixel
-    #if defined TARGET_KL25Z  // 8 Bit SPI
+    #if defined TARGET_KL25Z  || defined TARGET_DISCO_F429ZI // 8 Bit SPI
     int j;
     for (j=0; j<w; j++) {
         SPI::write(color >> 8);
         SPI::write(color & 0xff);
-    } 
-    #else 
+    }
+    #else
     SPI::format(16,3);                            // switch to 16 bit Mode 3
     int j;
     for (j=0; j<w; j++) {
@@ -428,12 +428,12 @@
     h = y1 - y0 + 1;
     window(x,y0,1,h);
     wr_cmd(0x2C);  // send pixel
-    #if defined TARGET_KL25Z  // 8 Bit SPI
+    #if defined TARGET_KL25Z  || defined TARGET_DISCO_F429ZI // 8 Bit SPI
     for (int y=0; y<h; y++) {
         SPI::write(color >> 8);
         SPI::write(color & 0xff);
-    } 
-    #else 
+    }
+    #else
     SPI::format(16,3);                            // switch to 16 bit Mode 3
     for (int y=0; y<h; y++) {
         SPI::write(color);
@@ -546,8 +546,8 @@
     int w = x1 - x0 + 1;
     int pixel = h * w;
     window(x0,y0,w,h);
-    wr_cmd(0x2C);  // send pixel 
-    #if defined TARGET_KL25Z  // 8 Bit SPI
+    wr_cmd(0x2C);  // send pixel
+    #if defined TARGET_KL25Z  || defined TARGET_DISCO_F429ZI // 8 Bit SPI
     for (int p=0; p<pixel; p++) {
         SPI::write(color >> 8);
         SPI::write(color & 0xff);
@@ -575,14 +575,14 @@
 
 int SPI_TFT_ILI9341::columns()
 {
-    return width() / font[1];
+    return width() / font[FONT_WIDTH_INDEX];
 }
 
 
 
 int SPI_TFT_ILI9341::rows()
 {
-    return height() / font[2];
+    return height() / font[FONT_HEIGHT_INDEX];
 }
 
 
@@ -591,8 +591,8 @@
 {
     if (value == '\n') {    // new line
         char_x = 0;
-        char_y = char_y + font[2];
-        if (char_y >= height() - font[2]) {
+        char_y = char_y + font[FONT_HEIGHT_INDEX];
+        if (char_y >= height() - font[FONT_HEIGHT_INDEX]) {
             char_y = 0;
         }
     } else {
@@ -604,29 +604,29 @@
 
 void SPI_TFT_ILI9341::character(int x, int y, int c)
 {
-    unsigned int hor,vert,offset,bpl,j,i,b;
+    unsigned int hor, vert, offset, bpl, j, i, b;
     unsigned char* zeichen;
-    unsigned char z,w;
+    unsigned char z, w;
 
     if ((c < 31) || (c > 127)) return;   // test char range
 
     // read font parameter from start of array
-    offset = font[0];                    // bytes / char
-    hor = font[1];                       // get hor size of font
-    vert = font[2];                      // get vert size of font
-    bpl = font[3];                       // bytes per line
+    offset = font[FONT_OFFSET_INDEX];                    // bytes / char
+    hor = font[FONT_WIDTH_INDEX];                       // get hor size of font
+    vert = font[FONT_HEIGHT_INDEX];                      // get vert size of font
+    bpl = font[FONT_BSP_INDEX];                       // bytes per line
 
     if (char_x + hor > width()) {
         char_x = 0;
         char_y = char_y + vert;
-        if (char_y >= height() - font[2]) {
+        if (char_y >= height() - font[FONT_HEIGHT_INDEX]) {
             char_y = 0;
         }
     }
     window(char_x, char_y,hor,vert); // char box
     wr_cmd(0x2C);  // send pixel
-    #ifndef TARGET_KL25Z  // 16 Bit SPI 
-    SPI::format(16,3);   
+    #if ! defined TARGET_KL25Z && ! defined TARGET_DISCO_F429ZI  // 16 Bit SPI
+    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
@@ -635,14 +635,14 @@
             z =  zeichen[bpl * i + ((j & 0xF8) >> 3)+1];
             b = 1 << (j & 0x07);
             if (( z & b ) == 0x00) {
-               #ifndef TARGET_KL25Z  // 16 Bit SPI 
+                #if ! defined TARGET_KL25Z && ! defined TARGET_DISCO_F429ZI  // 16 Bit SPI
                 SPI::write(_background);
-               #else
+                #else
                 SPI::write(_background >> 8);
                 SPI::write(_background & 0xff);
                 #endif
             } else {
-                #ifndef TARGET_KL25Z  // 16 Bit SPI
+                #if ! defined TARGET_KL25Z && ! defined TARGET_DISCO_F429ZI  // 16 Bit SPI
                 SPI::write(_foreground);
                 #else
                 SPI::write(_foreground >> 8);
@@ -652,7 +652,7 @@
         }
     }
     _cs = 1;
-    #ifndef TARGET_KL25Z  // 16 Bit SPI
+    #if ! defined TARGET_KL25Z && ! defined TARGET_DISCO_F429ZI  // 16 Bit SPI
     SPI::format(8,3);
     #endif
     WindowMax();
@@ -674,12 +674,12 @@
     unsigned int  j;
     int padd;
     unsigned short *bitmap_ptr = (unsigned short *)bitmap;
-    #if defined TARGET_KL25Z  // 8 Bit SPI
+    #if defined TARGET_KL25Z || defined TARGET_DISCO_F429ZI // 8 Bit SPI
         unsigned short pix_temp;
     #endif
-    
+
     unsigned int i;
-    
+
     // the lines are padded to multiple of 4 bytes in a bitmap
     padd = -1;
     do {
@@ -688,7 +688,7 @@
     window(x, y, w, h);
     bitmap_ptr += ((h - 1)* (w + padd));
     wr_cmd(0x2C);  // send pixel
-    #ifndef TARGET_KL25Z  // 16 Bit SPI 
+    #if ! defined TARGET_KL25Z && ! defined TARGET_DISCO_F429ZI // 16 Bit SPI
     SPI::format(16,3);
     #endif                            // switch to 16 bit Mode 3
     for (j = 0; j < h; j++) {         //Lines
@@ -707,7 +707,7 @@
         bitmap_ptr -= padd;
     }
     _cs = 1;
-    #ifndef TARGET_KL25Z  // 16 Bit SPI 
+    #if ! defined TARGET_KL25Z && ! defined TARGET_DISCO_F429ZI
     SPI::format(8,3);
     #endif
     WindowMax();
@@ -738,8 +738,8 @@
     while (*Name_BMP!='\0') {
         filename[i++]=*Name_BMP++;
     }
-    filename[i] = 0;  
-    
+    filename[i] = 0;
+
     FILE *Image = fopen((const char *)&filename[0], "rb");  // open the bmp file
     if (!Image) {
         return(0);      // error file not found !
@@ -780,21 +780,21 @@
 
     window(x, y,PixelWidth ,PixelHeigh);
     wr_cmd(0x2C);  // send pixel
-    #ifndef TARGET_KL25Z // only 8 Bit SPI 
-    SPI::format(16,3);  
+    #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  
+        #else
             SPI::write(line[i] >> 8);
             SPI::write(line[i]);
-        #endif    
-        } 
+        #endif
+        }
      }
     _cs = 1;
     SPI::format(8,3);
@@ -804,4 +804,30 @@
     return(1);
 }
 
-#endif
\ No newline at end of file
+void SPI_TFT_ILI9341::locateChar(uint16_t row, uint16_t column)
+{
+    locate(getCharLeft(column), getCharTop(row));
+}
+
+uint16_t SPI_TFT_ILI9341::getCharLeft(uint16_t column)
+{
+    return font[FONT_WIDTH_INDEX] * column;
+}
+
+uint16_t SPI_TFT_ILI9341::getCharTop(uint16_t row)
+{
+    return font[FONT_HEIGHT_INDEX] * row;
+}
+
+uint16_t SPI_TFT_ILI9341::getCharBottom(uint16_t row)
+{
+    return font[FONT_HEIGHT_INDEX] * (row + 1) - 1;
+}
+
+uint16_t SPI_TFT_ILI9341::getCharRight(uint16_t column)
+{
+    return font[FONT_WIDTH_INDEX] * (column + 1) - 1;
+}
+
+#endif
+