This is a port of Henning Kralsen's UTFT library for Arduino/chipKIT to mbed, refactored to make full use of C inheritance and access control, in order to reduce work when implementing new drivers and at the same time make the code more readable and easier to maintain. As of now supported are SSD1289 (16-bit interface), HX8340-B (serial interface) and ST7735 (serial interface). Drivers for other controllers will be added as time and resources to acquire the displays to test the code permit.

Dependents:   UTFT_SSD1289

Fork of TFTLCD by Todor Todorov

Revision:
23:eca4414196ca
Child:
24:ac6e35658037
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ili9328.cpp	Thu Jun 13 03:47:51 2013 +0000
@@ -0,0 +1,263 @@
+/*
+ * Copyright (C)2010-2012 Henning Karlsen. All right reserved.
+ * Copyright (C)2012-2013 Todor Todorov.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to:
+ *
+ * Free Software Foundation, Inc.
+ * 51 Franklin St, 5th Floor, Boston, MA 02110-1301, USA
+ *
+ *********************************************************************/
+#include "ili9328.h"
+#include "helpers.h"
+
+#define REG_START_OSC                   0x00
+
+#define REG_DRIV_OUT_CTRL               0x01
+#define REG_DRIV_WAV_CTRL               0x02
+#define REG_ENTRY_MOD                   0x03
+#define REG_RESIZE_CTRL                 0x04
+#define REG_DISP_CTRL1                  0x07
+#define REG_DISP_CTRL2                  0x08
+#define REG_DISP_CTRL3                  0x09
+#define REG_DISP_CTRL4                  0x0A
+#define REG_RGB_DISP_IF_CTRL1           0x0C
+#define REG_FRM_MARKER_POS              0x0D
+#define REG_RGB_DISP_IF_CTRL2           0x0F
+#define REG_PWR_CTRL1                   0x10
+#define REG_PWR_CTRL2                   0x11
+#define REG_PWR_CTRL3                   0x12
+#define REG_PWR_CTRL4                   0x13
+#define REG_GRAM_HORIZONTAL_ADDR        0x20
+#define REG_GRAM_VERTICAL_ADDR          0x21
+#define REG_GRAM_READWRITE              0x22
+#define REG_PWR_CTRL7                   0x29
+#define REG_FRM_RATE_COL_CTRL           0x2B
+#define REG_GAMMA_CTRL1                 0x30
+#define REG_GAMMA_CTRL2                 0x31
+#define REG_GAMMA_CTRL3                 0x32
+#define REG_GAMMA_CTRL4                 0x35
+#define REG_GAMMA_CTRL5                 0x36
+#define REG_GAMMA_CTRL6                 0x37
+#define REG_GAMMA_CTRL7                 0x38
+#define REG_GAMMA_CTRL8                 0x39
+#define REG_GAMMA_CTRL9                 0x3C
+#define REG_GAMMA_CTRL10                0x3D
+#define REG_HORIZONTAL_START_ADDR       0x50
+#define REG_HORIZONTAL_END_ADDR         0x51
+#define REG_VERTICAL_START_ADDR         0x52
+#define REG_VERTICAL_END_ADDR           0x53
+#define REG_GATE_SCAN_CTRL1             0x60
+#define REG_GATE_SCAN_CTRL2             0x61
+#define REG_GATE_SCAN_CTRL3             0x6A
+#define REG_PART_IMG1_DISP_POS          0x80
+#define REG_PART_IMG1_START_ADDR        0x81
+#define REG_PART_IMG1_END_ADDR          0x82
+#define REG_PART_IMG2_DISP_POS          0x83
+#define REG_PART_IMG2_START_ADDR        0x84
+#define REG_PART_IMG2_END_ADDR          0x85
+#define REG_PANEL_IF_CTRL1              0x90
+#define REG_PANEL_IF_CTRL2              0x92
+#define REG_PANEL_IF_CTRL3              0x93
+#define REG_PANEL_IF_CTRL4              0x95
+#define REG_PANEL_IF_CTRL5              0x97
+#define REG_PANEL_IF_CTRL6              0x98
+
+ILI9328_LCD::ILI9328_LCD( PinName CS, PinName RESET, PinName RS, PinName WR, BusOut* DATA_PORT, PinName BL, PinName RD, backlight_t blType, float defaultBackLightLevel )
+    : LCD( 240, 320, CS, RS, RESET, BL, blType, defaultBackLightLevel ), _lcd_pin_wr( WR )
+{
+    _lcd_port = DATA_PORT;
+    if ( RD != NC ) _lcd_pin_rd = new DigitalOut( RD );
+    else _lcd_pin_rd = 0;
+}
+
+void ILI9328_LCD::Initialize( orientation_t orientation, colordepth_t colors )
+{
+    _orientation = orientation;
+    _colorDepth = colors;
+    
+    _lcd_pin_reset = HIGH;
+    wait_ms( 50 );
+    _lcd_pin_reset = LOW;
+    wait_ms( 100 );
+    _lcd_pin_reset = HIGH;
+    wait_ms( 1000 );
+    _lcd_pin_cs = HIGH;
+    if ( _lcd_pin_bl != 0 )
+        *_lcd_pin_bl = HIGH;
+    else if ( _bl_pwm != 0 )
+        *_bl_pwm = _bl_pwm_default;
+    if ( _lcd_pin_rd != 0 )
+        *_lcd_pin_rd = HIGH;
+    _lcd_pin_wr = HIGH;
+    wait_ms( 15 );
+    
+    Activate();
+    //WriteCmdData( 0x00, 0x0001 ); // oscillator: 1 = on, 0 = off
+    //wait_ms( 1 );
+    WriteCmdData( REG_DRIV_OUT_CTRL, 0x0100 );  // Driver Output Control Register (R01h)
+    WriteCmdData( REG_DRIV_WAV_CTRL, 0x0700 );  // LCD Driving Waveform Control (R02h)
+    WriteCmdData( REG_ENTRY_MOD, 0x1030 );      // Entry Mode (R03h) (BGR=on;ID1=on;ID2=on;AM=0)
+    WriteCmdData( REG_DISP_CTRL2, 0x0302 );
+    WriteCmdData( REG_DISP_CTRL3, 0x0000 );
+    WriteCmdData( REG_DISP_CTRL4, 0x0000 );     // Fmark On
+    WriteCmdData( REG_PWR_CTRL1, 0x0000 );      // Power Control 1 (R10h)
+    WriteCmdData( REG_PWR_CTRL2, 0x0007 );      // Power Control 2 (R11h)
+    WriteCmdData( REG_PWR_CTRL3, 0x0000 );      // Power Control 3 (R12h)
+    WriteCmdData( REG_PWR_CTRL4, 0x0000 );      // Power Control 4 (R13h)
+    wait_ms( 1000 );  
+    WriteCmdData( REG_PWR_CTRL1, 0x14B0 );      // Power Control 1 (R10h)  
+    wait_ms( 500 );  
+    WriteCmdData( REG_PWR_CTRL2, 0x0007 );      // Power Control 2 (R11h)  
+    wait_ms( 500 );  
+    WriteCmdData( REG_PWR_CTRL3, 0x008E );      // Power Control 3 (R12h)
+    WriteCmdData( REG_PWR_CTRL4, 0x0C00 );      // Power Control 4 (R13h)
+    WriteCmdData( REG_PWR_CTRL7, 0x0015 );      // NVM read data 2 (R29h)
+    wait_ms( 500 );
+    WriteCmdData( REG_GAMMA_CTRL1, 0x0000 );         // Gamma Control 1
+    WriteCmdData( REG_GAMMA_CTRL2, 0x0107 );         // Gamma Control 2
+    WriteCmdData( REG_GAMMA_CTRL3, 0x0000 );         // Gamma Control 3
+    WriteCmdData( REG_GAMMA_CTRL4, 0x0203 );         // Gamma Control 4
+    WriteCmdData( REG_GAMMA_CTRL5, 0x0402 );         // Gamma Control 5
+    WriteCmdData( REG_GAMMA_CTRL6, 0x0000 );         // Gamma Control 6
+    WriteCmdData( REG_GAMMA_CTRL7, 0x0207 );         // Gamma Control 7
+    WriteCmdData( REG_GAMMA_CTRL8, 0x0000 );         // Gamma Control 8
+    WriteCmdData( REG_GAMMA_CTRL9, 0x0203 );         // Gamma Control 9
+    WriteCmdData( REG_GAMMA_CTRL10, 0x0403 );        // Gamma Control 10
+    WriteCmdData( REG_HORIZONTAL_START_ADDR, 0x0000 );                      // Window Horizontal RAM Address Start (R50h)
+    WriteCmdData( REG_HORIZONTAL_END_ADDR, _disp_width - 1);   // Window Horizontal RAM Address End (R51h)
+    WriteCmdData( REG_VERTICAL_START_ADDR, 0x0000 );                        // Window Vertical RAM Address Start (R52h)
+    WriteCmdData( REG_VERTICAL_END_ADDR, _disp_height - 1);    // Window Vertical RAM Address End (R53h)
+    WriteCmdData( REG_GATE_SCAN_CTRL1, 0xa700 );    // Driver Output Control (R60h)
+    WriteCmdData( REG_GATE_SCAN_CTRL2, 0x0003 ); // Driver Output Control (R61h) - enable VLE
+    WriteCmdData( REG_PANEL_IF_CTRL1, 0x0010 );  // Panel Interface Control 1 (R90h)
+
+    // Display On
+    WriteCmdData( REG_DISP_CTRL1, 0x0133 );     // Display Control (R07h)
+    wait_ms( 500 );
+    WriteCmd( REG_GRAM_READWRITE );
+  
+    Deactivate();
+}
+
+void ILI9328_LCD::Sleep( void )
+{
+    //WriteCmdData( 0x10, 0x0001 ); // sleep mode: 0 = exit, 1 = enter
+    LCD::Sleep();
+}
+
+void ILI9328_LCD::WakeUp( void )
+{
+    //WriteCmdData( 0x10, 0x0000 ); // sleep mode: 0 = exit, 1 = enter
+    LCD::WakeUp();
+}
+
+void ILI9328_LCD::WriteCmd( unsigned short cmd )
+{
+    _lcd_pin_rs = LOW;
+    _lcd_port->write( cmd );
+    pulseLow( _lcd_pin_wr );
+}
+
+void ILI9328_LCD::WriteData( unsigned short data )
+{
+    _lcd_pin_rs = HIGH;
+    _lcd_port->write( data );
+    pulseLow( _lcd_pin_wr );
+}
+
+void ILI9328_LCD::SetXY( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2 )
+{
+    /*
+    if ( _orientation == PORTRAIT || _orientation == PORTRAIT_REV )
+    {
+        WriteCmdData( 0x44, ( x2 << 8 ) + x1 );
+        WriteCmdData( 0x45, y1 );
+        WriteCmdData( 0x46, y2 );
+        WriteCmdData( 0x4e, x1 );
+        WriteCmdData( 0x4f, y1 );
+    }
+    else
+    {
+        WriteCmdData( 0x44, ( y2 << 8 ) + y1 );
+        WriteCmdData( 0x45, x1 );
+        WriteCmdData( 0x46, x2 );
+        WriteCmdData( 0x4e, y1 );
+        WriteCmdData( 0x4f, x1 );
+    }
+    */
+    WriteCmdData( REG_HORIZONTAL_START_ADDR, x1 );
+    WriteCmdData( REG_HORIZONTAL_END_ADDR, x2 );
+    WriteCmdData( REG_VERTICAL_START_ADDR, y1 );
+    WriteCmdData( REG_VERTICAL_END_ADDR, y2 );
+    WriteCmdData( REG_GRAM_HORIZONTAL_ADDR, x1 );
+    WriteCmdData( REG_GRAM_VERTICAL_ADDR, y1 );
+    WriteCmd( 0x22 );
+}
+
+void ILI9328_LCD::SetPixelColor( unsigned int color, colordepth_t mode )
+{
+    unsigned char r, g, b;
+    unsigned short clr;
+    if ( _colorDepth == RGB16 )
+    {
+        switch ( mode )
+        {
+            case RGB16:
+                WriteData( color & 0xFFFF );
+                break;
+            case RGB18:
+                r = ( color >> 10 ) & 0xF8;
+                g = ( color >> 4 ) & 0xFC;
+                b = ( color >> 1 ) & 0x1F;
+                clr = ( ( r | ( g >> 5 ) ) << 8 ) | ( ( g << 3 ) | b );
+                WriteData( clr );
+                break;
+            case RGB24:
+                r = ( color >> 16 ) & 0xF8;
+                g = ( color >> 8 ) & 0xFC;
+                b = color & 0xF8;
+                clr = ( ( r | ( g >> 5 ) ) << 8 ) | ( ( g << 3 ) | ( b >> 3 ) );
+                WriteData( clr );
+                break;
+        }
+    }
+    else if ( _colorDepth == RGB18 )
+    {
+        switch ( mode )
+        {
+            case RGB16:
+                r = ( ( color >> 8 ) & 0xF8 ) | ( ( color & 0x8000 ) >> 13 );
+                g = ( color >> 3 ) & 0xFC;
+                b = ( ( color << 3 ) & 0xFC ) | ( ( color >> 3 ) & 0x01 );
+                WriteData( ( r << 8 ) | g );
+                WriteData( b );
+                break;
+            case RGB18:
+                b = ( color << 2 ) & 0xFC;
+                g = ( color >> 4 ) & 0xFC;
+                r = ( color >> 10 ) & 0xFC;
+                WriteData( ( r << 8 ) | g );
+                WriteData( b );
+                break;
+            case RGB24:
+                r = ( color >> 16 ) & 0xFC;
+                g = ( color >> 8 ) & 0xFC;
+                b = color & 0xFC;
+                WriteData( ( r << 8 ) | g );
+                WriteData( b );
+                break;
+        }
+    }
+}