Forked from Peter Drescher lib, using always 16bit spi, init values for chinese red pcb, added read cmds and some mess here and there

Fork of SPI_TFT_ILI9341 by Peter Drescher

Revision:
10:4b70a6915f06
Parent:
9:1d3d41128693
Child:
11:365aa15eaa0f
--- a/SPI_TFT_ILI9341.cpp	Tue Aug 19 21:16:56 2014 +0000
+++ b/SPI_TFT_ILI9341.cpp	Sun Aug 31 19:25:33 2014 +0000
@@ -20,6 +20,9 @@
 #include "mbed.h"
 
 #define BPP         16                  // Bits per pixel    
+
+// if display is the only slave into the SPI bus, we can skip driving CS at every cmd
+//#define USE_CS  
          
 //extern Serial pc;
 //extern DigitalOut xx;     // debug !!
@@ -68,7 +71,9 @@
             wr_8(0xE8);
             break;
     }
-    // _cs = 1; 
+#ifdef USE_CS
+    _cs = 1;
+#endif 
     WindowMax();
 } 
 
@@ -77,29 +82,25 @@
 void SPI_TFT_ILI9341::wr_cmd(unsigned char cmd)
 {
     _dc = 0;
- //   _cs = 0;
+#ifdef USE_CS
+    _cs = 0;
+#endif
     _spi.write(cmd);      // write 16bit, 00 (NOP cmd) then 8bit cmd
- //   _cs = 1;
+    _dc = 1;
 }
 
 // write 8bit data/parameter
 void SPI_TFT_ILI9341::wr_8(unsigned char value)
 {
-    _dc = 1;
- //   _cs = 0;
     _spi.format(8,0);   // changing format takes time but wr_8 is used only for init and set orientation
     _spi.write(value);      // mbed lib
     _spi.format(16,0);
- //   _cs = 1;
 }
 
 // write 16bit data/parameter
 void SPI_TFT_ILI9341::wr_16(unsigned int value)
 {
-    _dc = 1;
- //   _cs = 0;
     _spi.write(value);
- //   _cs = 1;
 }
 
 // the ILI9341 can read 
@@ -108,11 +109,15 @@
 {
     unsigned char r;
     _dc = 0;
- //   _cs = 0;
+#ifdef USE_CS
+    _cs = 0;
+#endif
     _spi.write(cmd);      // mbed lib
     _dc = 1;            // fixed
     r = _spi.write(0) >> 8;
- //   _cs = 1;    
+#ifdef USE_CS
+    _cs = 1;
+#endif    
     return(r);
 }
 // for cmd that return 24bits (need a dummy cycle after cmd)
@@ -121,7 +126,9 @@
     unsigned int d = 0;
     unsigned short r;
     _dc = 0;
- //   _cs = 0;
+#ifdef USE_CS
+    _cs = 0;
+#endif
     _spi.write(cmd); 
     _dc = 1;
     r = _spi.write(0); // we get only 15bit valid, first bit was the dummy cycle
@@ -129,10 +136,11 @@
     r = _spi.write(0);
     d = (d << 16) | r;
     d = d >> 7;
-    // we clocked 7 more bit so ILI waiting for 8th, we need to rest spi
+    // we clocked 7 more bit so ILI waiting for 8th, we need to reset spi bus with CS
     _cs = 1;
+#ifndef USE_CS //if CS is not used, force fixed LOW again
     _cs = 0;
-//    _cs = 0;    
+#endif    
     return(d);
 }
 // for cmd that return 32bits (need a dummy cycle after cmd)
@@ -141,7 +149,9 @@
     unsigned int d = 0;
     unsigned short r;
     _dc = 0;
- //    _cs = 0;
+#ifdef USE_CS
+    _cs = 0;
+#endif
     _spi.write(cmd); 
     _dc = 1;
     r = _spi.write(0); // we get only 15bit valid, first bit was the dummy cycle
@@ -152,8 +162,9 @@
     d = (d << 1) | (r >> 15);
     // we clocked 15 more bit so ILI waiting for 16th, we need to rest spi
     _cs = 1;
+#ifndef USE_CS //if CS is not used, force fixed LOW again
     _cs = 0;
-//    _cs = 1;   
+#endif  
     return(d);
 }
 // undocumented cmd 0xD9 found in some code example
@@ -162,6 +173,9 @@
     char data=0;
     wr_cmd(0xd9); /* ext command */
     wr_8(0x10+xParameter); /* 0x11 is the first Parameter */
+#ifdef USE_CS
+    _cs = 1;
+#endif
     data = wr_cmd_rd8(Addr);
     return data;
 }
@@ -336,6 +350,9 @@
      wr_cmd(0x29);                     // display on
      
      wait_ms(150);
+#ifdef USE_CS
+    _cs = 1;
+#endif
      
      WindowMax ();
      
@@ -348,6 +365,9 @@
     wr_16(y);
     wr_cmd(0x2C);  // send pixel
     wr_16(color);
+#ifdef USE_CS
+    _cs = 1;
+#endif
 }
 
 
@@ -360,6 +380,9 @@
     wr_cmd(0x2B);
     wr_16(y);
     wr_16(y+h-1);
+#ifdef USE_CS
+    _cs = 1;
+#endif
 }
 
 
@@ -375,11 +398,13 @@
     int pixels = ( width() * height());
     WindowMax();
     wr_cmd(0x2C);  // send pixel
-    unsigned int i;
-    for (i = 0; i < pixels; i++)
+    for (int i = 0; i < pixels; i++)
         {
         wr_16(_background);
         }
+#ifdef USE_CS
+    _cs = 1;
+#endif
 }
 
 
@@ -640,8 +665,7 @@
             }
         }
     }
-
-    WindowMax();
+    WindowMax();    // maybe we can skip this
     if ((w + 2) < hor) {                   // x offset to next char
         char_x += w + 2;
     } else char_x += hor;