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

Dependents:   TFT_Test1 SourceCodePro31-SB Mandelbrot Mindwave-screen ... more

See http://mbed.org/cookbook/SPI-driven-QVGA-TFT for details.

Revision:
16:2efcbb2814fa
Parent:
15:f5772cffc2b2
Child:
18:52cbeede86f0
--- a/SPI_TFT.cpp	Tue Mar 05 21:58:25 2013 +0000
+++ b/SPI_TFT.cpp	Mon Mar 25 10:11:00 2013 +0000
@@ -23,6 +23,7 @@
 // 11.10.12 patch from Hans Bergles to get SPI1 working again
 // 03.02.13 add a switch to switch off DMA use for LPC11U24
 // 04.03.13 add support for new Kinetis board
+// 25.03.13 fix Bug in bitmap for Kinetis board
 
 #include "SPI_TFT.h"
 #include "mbed.h"
@@ -1100,11 +1101,12 @@
 {
     unsigned int  j;
     int padd;
+  
+    unsigned short *bitmap_ptr = (unsigned short *)bitmap;
     #if defined TARGET_KL25Z  // 8 Bit SPI
-        unsigned char *bitmap_ptr = (unsigned char *)bitmap;
-    #else
-        unsigned short *bitmap_ptr = (unsigned short *)bitmap;
+        unsigned short pix_temp;
     #endif
+  
     // the lines are padded to multiple of 4 bytes in a bitmap
     padd = -1;
     do {
@@ -1115,52 +1117,52 @@
     _cs = 0;
 #if defined NO_MBED_LIB
     if (spi_port == 0) {    // TFT on SSP0
-#if defined USE_DMA
+    #if defined USE_DMA
         LPC_GPDMACH0->DMACCDestAddr = (uint32_t)&LPC_SSP0->DR; // we send to SSP0
         /* Enable SSP0 for DMA. */
         LPC_SSP0->DMACR = 0x2;
-#endif
-        LPC_SSP0->CR0 &= ~(0x08UL); // set to 8 bit
-        LPC_SSP0->DR = 0x72;        // start Data
-        LPC_SSP0->CR0 |= 0x08UL;    // set to 16 bit
+    #endif
+    LPC_SSP0->CR0 &= ~(0x08UL); // set to 8 bit
+    LPC_SSP0->DR = 0x72;        // start Data
+    LPC_SSP0->CR0 |= 0x08UL;    // set to 16 bit
 
     } else {
-#if defined USE_DMA
+    #if defined USE_DMA
         LPC_GPDMACH0->DMACCDestAddr = (uint32_t)&LPC_SSP1->DR; // we send to SSP1
         /* Enable SSP1 for DMA. */
         LPC_SSP1->DMACR = 0x2;
-#endif
-        LPC_SSP1->CR0 &= ~(0x08UL); // set to 8 bit
-        LPC_SSP1->DR = 0x72;        // start Data command
-        LPC_SSP1->CR0 |= 0x08UL;    // set to 16 bit
+    #endif
+    LPC_SSP1->CR0 &= ~(0x08UL); // set to 8 bit
+    LPC_SSP1->DR = 0x72;        // start Data command
+    LPC_SSP1->CR0 |= 0x08UL;    // set to 16 bit
     }
 
     bitmap_ptr += ((h - 1)* (w + padd));
-#if defined USE_DMA
-    for (j = 0; j < h; j++) {        //Lines
-        LPC_GPDMA->DMACIntTCClear = 0x1;
-        LPC_GPDMA->DMACIntErrClr = 0x1;
-        LPC_GPDMACH0->DMACCSrcAddr = (uint32_t)bitmap_ptr;
-        LPC_GPDMACH0->DMACCControl = w | (1UL << 18) | (1UL << 21) | (1UL << 31) |  DMA_CHANNEL_SRC_INC ; // 16 bit transfer , address increment, interrupt
-        LPC_GPDMACH0->DMACCConfig  = DMA_CHANNEL_ENABLE | DMA_TRANSFER_TYPE_M2P | (spi_port ? DMA_DEST_SSP1_TX : DMA_DEST_SSP0_TX);
-        LPC_GPDMA->DMACSoftSReq = 0x1;
-        do {
-        } while ((LPC_GPDMA->DMACRawIntTCStat & 0x01) == 0); // DMA is running
+    #if defined USE_DMA
+        for (j = 0; j < h; j++) {        //Lines
+            LPC_GPDMA->DMACIntTCClear = 0x1;
+            LPC_GPDMA->DMACIntErrClr = 0x1;
+            LPC_GPDMACH0->DMACCSrcAddr = (uint32_t)bitmap_ptr;
+            LPC_GPDMACH0->DMACCControl = w | (1UL << 18) | (1UL << 21) | (1UL << 31) |  DMA_CHANNEL_SRC_INC ; // 16 bit transfer , address increment, interrupt
+            LPC_GPDMACH0->DMACCConfig  = DMA_CHANNEL_ENABLE | DMA_TRANSFER_TYPE_M2P | (spi_port ? DMA_DEST_SSP1_TX : DMA_DEST_SSP0_TX);
+            LPC_GPDMA->DMACSoftSReq = 0x1;
+            do {
+            } while ((LPC_GPDMA->DMACRawIntTCStat & 0x01) == 0); // DMA is running
 
-        bitmap_ptr -= w;
-        bitmap_ptr -= padd;
-    }
-#else
-    unsigned int i;
-    for (j = 0; j < h; j++) {        //Lines
-        for (i = 0; i < w; i++) {     // copy pixel data to TFT
-            _spi.write(*bitmap_ptr);    // one line
-            bitmap_ptr++;
+            bitmap_ptr -= w;
+            bitmap_ptr -= padd;
         }
-        bitmap_ptr -= 2*w;
-        bitmap_ptr -= padd;
-    }
-#endif
+    #else
+        unsigned int i;
+        for (j = 0; j < h; j++) {        //Lines
+            for (i = 0; i < w; i++) {     // copy pixel data to TFT
+                _spi.write(*bitmap_ptr);    // one line
+                bitmap_ptr++;
+            }
+            bitmap_ptr -= 2*w;
+            bitmap_ptr -= padd;
+        }
+    #endif
     if (spi_port == 0) {    // TFT on SSP0
         do {
         } while ((LPC_SSP0->SR & 0x10) == 0x10); // SPI FIFO not empty
@@ -1169,30 +1171,31 @@
         } while ((LPC_SSP1->SR & 0x10) == 0x10); // SPI FIFO not empty
     }
 #else // use mbed lib
-#if defined TARGET_KL25Z  // 8 Bit SPI
-    _spi.write(SPI_START | SPI_WR | SPI_DATA);    // Write : RS = 1, RW = 0
-#else
-    _spi.format(8,3);                             // 8 bit Mode 3
-    _spi.write(SPI_START | SPI_WR | SPI_DATA);    // Write : RS = 1, RW = 0
-    _spi.format(16,3);                            // switch to 16 bit Mode 3
-#endif
+    #if defined TARGET_KL25Z  // 8 Bit SPI
+        _spi.write(SPI_START | SPI_WR | SPI_DATA);    // Write : RS = 1, RW = 0
+    #else
+        _spi.format(8,3);                             // 8 bit Mode 3
+        _spi.write(SPI_START | SPI_WR | SPI_DATA);    // Write : RS = 1, RW = 0
+        _spi.format(16,3);                            // switch to 16 bit Mode 3
+    #endif    
+    bitmap_ptr += ((h - 1)* (w + padd));              
     unsigned int i;
     for (j = 0; j < h; j++) {        //Lines
         for (i = 0; i < w; i++) {     // copy pixel data to TFT
-#if defined TARGET_KL25Z  // 8 Bit SPI
-            _spi.write(*bitmap_ptr);
-            bitmap_ptr++;
-            _spi.write(*bitmap_ptr);
-            bitmap_ptr++;
-#else
-            _spi.write(*bitmap_ptr);    // one line
-            bitmap_ptr++;
-#endif
+            #if defined TARGET_KL25Z  // 8 Bit SPI
+                pix_temp = *bitmap_ptr;
+                _spi.write(pix_temp >> 8);
+                _spi.write(pix_temp);
+                bitmap_ptr++;
+            #else
+                _spi.write(*bitmap_ptr);    // one line
+                bitmap_ptr++;
+            #endif
         }
         bitmap_ptr -= 2*w;
         bitmap_ptr -= padd;
-    }
-#endif
+     }
+#endif // USE MBED LIB
     _cs = 1;
     WindowMax();
 }