landtiger with ili9325 lcd controller based on the TFTLCD libary from Todor Todorov

Committer:
casval
Date:
Mon Jun 08 09:59:17 2015 +0000
Revision:
1:1a3dd94e13cf
Parent:
0:0099ad246127
Mutex form for other project was still there

Who changed what in which revision?

UserRevisionLine numberNew contents of line
casval 0:0099ad246127 1 /*
casval 0:0099ad246127 2 * Copyright (C)2010-2012 Henning Karlsen. All right reserved.
casval 0:0099ad246127 3 * Copyright (C)2012-2013 Todor Todorov.
casval 0:0099ad246127 4 *
casval 0:0099ad246127 5 * This library is free software; you can redistribute it and/or
casval 0:0099ad246127 6 * modify it under the terms of the GNU Lesser General Public
casval 0:0099ad246127 7 * License as published by the Free Software Foundation; either
casval 0:0099ad246127 8 * version 2.1 of the License, or (at your option) any later version.
casval 0:0099ad246127 9 *
casval 0:0099ad246127 10 * This library is distributed in the hope that it will be useful,
casval 0:0099ad246127 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
casval 0:0099ad246127 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
casval 0:0099ad246127 13 * Lesser General Public License for more details.
casval 0:0099ad246127 14 *
casval 0:0099ad246127 15 * You should have received a copy of the GNU Lesser General Public
casval 0:0099ad246127 16 * License along with this library; if not, write to:
casval 0:0099ad246127 17 *
casval 0:0099ad246127 18 * Free Software Foundation, Inc.
casval 0:0099ad246127 19 * 51 Franklin St, 5th Floor, Boston, MA 02110-1301, USA
casval 0:0099ad246127 20 *
casval 0:0099ad246127 21 *********************************************************************/
casval 0:0099ad246127 22 #include "ili9328.h"
casval 0:0099ad246127 23 #include "helpers.h"
casval 0:0099ad246127 24
casval 0:0099ad246127 25
casval 0:0099ad246127 26 ILI9328_LCD::ILI9328_LCD( PinName CS, PinName RS, PinName WR, PinName DIR, PinName EN, PinName LE, BusOut* DATA_PORT, PinName RD , PinName BL, backlight_t blType, float defaultBackLightLevel )
casval 0:0099ad246127 27 : LCD( 240, 320, CS, RS, DIR, EN, LE, BL, blType, defaultBackLightLevel ), _lcd_pin_wr( WR )
casval 0:0099ad246127 28 {
casval 0:0099ad246127 29 _lcd_port = DATA_PORT;
casval 0:0099ad246127 30 if ( RD != NC ) _lcd_pin_rd = new DigitalOut( RD );
casval 0:0099ad246127 31 else _lcd_pin_rd = 0;
casval 0:0099ad246127 32 }
casval 0:0099ad246127 33
casval 0:0099ad246127 34 void ILI9328_LCD::Initialize( orientation_t orientation, colordepth_t colors )
casval 0:0099ad246127 35 {
casval 0:0099ad246127 36 _orientation = orientation;
casval 0:0099ad246127 37 _colorDepth = colors;
casval 0:0099ad246127 38
casval 0:0099ad246127 39 /* _lcd_pin_reset = HIGH;
casval 0:0099ad246127 40 wait_ms( 50 );
casval 0:0099ad246127 41 _lcd_pin_reset = LOW;
casval 0:0099ad246127 42 wait_ms( 100 );
casval 0:0099ad246127 43 _lcd_pin_reset = HIGH;
casval 0:0099ad246127 44 wait_ms( 1000 );
casval 0:0099ad246127 45 */
casval 0:0099ad246127 46 _lcd_pin_cs = HIGH;
casval 0:0099ad246127 47 if ( _lcd_pin_bl != 0 )
casval 0:0099ad246127 48 *_lcd_pin_bl = HIGH;
casval 0:0099ad246127 49 else if ( _bl_pwm != 0 )
casval 0:0099ad246127 50 *_bl_pwm = _bl_pwm_default;
casval 0:0099ad246127 51 if ( _lcd_pin_rd != 0 )
casval 0:0099ad246127 52 *_lcd_pin_rd = HIGH;
casval 0:0099ad246127 53 _lcd_pin_wr = HIGH;
casval 0:0099ad246127 54 wait_ms( 50 );
casval 0:0099ad246127 55
casval 0:0099ad246127 56 Activate();
casval 0:0099ad246127 57
casval 0:0099ad246127 58 short drivOut = 0;
casval 0:0099ad246127 59 short entryMod = 0;
casval 0:0099ad246127 60 short gateScan = 0x2700;
casval 0:0099ad246127 61 switch ( _orientation )
casval 0:0099ad246127 62 {
casval 0:0099ad246127 63 case LANDSCAPE:
casval 0:0099ad246127 64 drivOut = 0x0100;
casval 0:0099ad246127 65 entryMod |= 0x0038;
casval 0:0099ad246127 66 gateScan |= 0x0000;
casval 0:0099ad246127 67 break;
casval 0:0099ad246127 68
casval 0:0099ad246127 69 case LANDSCAPE_REV:
casval 0:0099ad246127 70 drivOut = 0x0000;
casval 0:0099ad246127 71 entryMod |= 0x0038;
casval 0:0099ad246127 72 gateScan |= 0x8000;
casval 0:0099ad246127 73 break;
casval 0:0099ad246127 74
casval 0:0099ad246127 75 case PORTRAIT_REV:
casval 0:0099ad246127 76 drivOut = 0x0000;
casval 0:0099ad246127 77 entryMod |= 0x0030;
casval 0:0099ad246127 78 gateScan |= 0x0000;
casval 0:0099ad246127 79 break;
casval 0:0099ad246127 80
casval 0:0099ad246127 81 case PORTRAIT:
casval 0:0099ad246127 82 default:
casval 0:0099ad246127 83 drivOut = 0x0100;
casval 0:0099ad246127 84 entryMod |= 0x0030;
casval 0:0099ad246127 85 gateScan |= 0x8000;
casval 0:0099ad246127 86 break;
casval 0:0099ad246127 87 }
casval 0:0099ad246127 88 switch ( _colorDepth )
casval 0:0099ad246127 89 {
casval 0:0099ad246127 90 case RGB18:
casval 0:0099ad246127 91 entryMod |= 0x9000;
casval 0:0099ad246127 92 break;
casval 0:0099ad246127 93
casval 0:0099ad246127 94 case RGB16:
casval 0:0099ad246127 95 default:
casval 0:0099ad246127 96 entryMod |= 0x1000;
casval 0:0099ad246127 97 break;
casval 0:0099ad246127 98 }
casval 0:0099ad246127 99
casval 0:0099ad246127 100 // WriteCmdData( 0xE5, 0x78F0 ); // set SRAM internal timing
casval 0:0099ad246127 101 WriteCmdData( 0xE5, 0x8000 ); // set SRAM internal timing
casval 0:0099ad246127 102 WriteCmdData( 0x00, 0x0000 ); // set Driver Output Control
casval 0:0099ad246127 103 WriteCmdData( 0x01, drivOut ); // set Driver Output Control
casval 0:0099ad246127 104 // WriteCmdData( 0x02, 0x0200 ); // set 1 line inversion
casval 0:0099ad246127 105 WriteCmdData( 0x02, 0x0700 ); // set 1 line inversion
casval 0:0099ad246127 106 WriteCmdData( 0x03, entryMod ); // set GRAM write direction and BGR=1.
casval 0:0099ad246127 107 WriteCmdData( 0x04, 0x0000 ); // Resize register
casval 0:0099ad246127 108 // WriteCmdData( 0x08, 0x0207 ); // set the back porch and front porch
casval 0:0099ad246127 109 WriteCmdData( 0x08, 0x0202 ); // set the back porch and front porch
casval 0:0099ad246127 110
casval 0:0099ad246127 111 WriteCmdData( 0x09, 0x0000 ); // set non-display area refresh cycle ISC[3:0]
casval 0:0099ad246127 112 WriteCmdData( 0x0A, 0x0000 ); // FMARK function
casval 0:0099ad246127 113 WriteCmdData( 0x0C, 0x0000 ); // RGB interface setting
casval 0:0099ad246127 114 WriteCmdData( 0x0D, 0x0000 ); // Frame marker Position
casval 0:0099ad246127 115 WriteCmdData( 0x0F, 0x0000 ); // RGB interface polarity
casval 0:0099ad246127 116 // ----------- Power On sequence ----------- //
casval 0:0099ad246127 117 WriteCmdData( 0x10, 0x0000 ); // SAP, BT[3:0], AP, DSTB, SLP, STB
casval 0:0099ad246127 118 // WriteCmdData( 0x11, 0x0007 ); // DC1[2:0], DC0[2:0], VC[2:0]
casval 0:0099ad246127 119 WriteCmdData( 0x11, 0x0000 ); // DC1[2:0], DC0[2:0], VC[2:0]
casval 0:0099ad246127 120 WriteCmdData( 0x12, 0x0000 ); // VREG1OUT voltage
casval 0:0099ad246127 121 WriteCmdData( 0x13, 0x0000 ); // VDV[4:0] for VCOM amplitude
casval 0:0099ad246127 122 // WriteCmdData( 0x07, 0x0001 );
casval 0:0099ad246127 123 wait_ms( 200 ); // Dis-charge capacitor power voltage
casval 0:0099ad246127 124
casval 0:0099ad246127 125 // WriteCmdData( 0x10, 0x1690 ); // SAP, BT[3:0], AP, DSTB, SLP, STB
casval 0:0099ad246127 126 WriteCmdData( 0x10, 0x17B0 ); // SAP, BT[3:0], AP, DSTB, SLP, STB
casval 0:0099ad246127 127 // WriteCmdData( 0x11, 0x0227 ); // Set DC1[2:0], DC0[2:0], VC[2:0]
casval 0:0099ad246127 128 WriteCmdData( 0x11, 0x0037 ); // Set DC1[2:0], DC0[2:0], VC[2:0]
casval 0:0099ad246127 129 wait_ms( 50 ); // Delay 50ms
casval 0:0099ad246127 130
casval 0:0099ad246127 131 // WriteCmdData( 0x12, 0x000D ); // 0012
casval 0:0099ad246127 132 WriteCmdData( 0x12, 0x0138 ); // 0012
casval 0:0099ad246127 133 wait_ms( 50 ); // Delay 50ms
casval 0:0099ad246127 134
casval 0:0099ad246127 135 // WriteCmdData( 0x13, 0x1200 ); // VDV[4:0] for VCOM amplitude
casval 0:0099ad246127 136 WriteCmdData( 0x13, 0x1700 ); // VDV[4:0] for VCOM amplitude
casval 0:0099ad246127 137 // WriteCmdData( 0x29, 0x000A ); // 04 VCM[5:0] for VCOMH
casval 0:0099ad246127 138 WriteCmdData( 0x29, 0x000D ); // 04 VCM[5:0] for VCOMH
casval 0:0099ad246127 139 WriteCmdData( 0x2B, 0x000D ); // Set Frame Rate
casval 0:0099ad246127 140 wait_ms( 50 ); // Delay 50ms
casval 0:0099ad246127 141
casval 0:0099ad246127 142 WriteCmdData( 0x20, 0x0000 ); // GRAM horizontal Address
casval 0:0099ad246127 143 WriteCmdData( 0x21, 0x0000 ); // GRAM Vertical Address
casval 0:0099ad246127 144
casval 0:0099ad246127 145 // ----------- Adjust the Gamma Curve ----------//
casval 0:0099ad246127 146 // WriteCmdData( 0x30, 0x0000 );
casval 0:0099ad246127 147 WriteCmdData( 0x30, 0x0001 );
casval 0:0099ad246127 148 // WriteCmdData( 0x31, 0x0404 );
casval 0:0099ad246127 149 WriteCmdData( 0x31, 0x0606 );
casval 0:0099ad246127 150 // WriteCmdData( 0x32, 0x0003 );
casval 0:0099ad246127 151 WriteCmdData( 0x32, 0x0304 );
casval 0:0099ad246127 152 //new line
casval 0:0099ad246127 153 WriteCmdData( 0x33, 0x0202 );
casval 0:0099ad246127 154 WriteCmdData( 0x34, 0x0202 );
casval 0:0099ad246127 155 // WriteCmdData( 0x35, 0x0405 );
casval 0:0099ad246127 156 WriteCmdData( 0x35, 0x0103 );
casval 0:0099ad246127 157
casval 0:0099ad246127 158 // WriteCmdData( 0x36, 0x0808 );
casval 0:0099ad246127 159 WriteCmdData( 0x36, 0x011D );
casval 0:0099ad246127 160 // WriteCmdData( 0x37, 0x0407 );
casval 0:0099ad246127 161 WriteCmdData( 0x37, 0x0404 );
casval 0:0099ad246127 162 // WriteCmdData( 0x38, 0x0303 );
casval 0:0099ad246127 163 WriteCmdData( 0x38, 0x0404 );
casval 0:0099ad246127 164 // WriteCmdData( 0x39, 0x0707 );
casval 0:0099ad246127 165 WriteCmdData( 0x39, 0x0404 );
casval 0:0099ad246127 166 // WriteCmdData( 0x3C, 0x0504 );
casval 0:0099ad246127 167 WriteCmdData( 0x3C, 0x0700 );
casval 0:0099ad246127 168 // WriteCmdData( 0x3D, 0x0808 );
casval 0:0099ad246127 169 WriteCmdData( 0x3D, 0x0A1F );
casval 0:0099ad246127 170
casval 0:0099ad246127 171 //------------------ Set GRAM area ---------------//
casval 0:0099ad246127 172 WriteCmdData( 0x50, 0x0000 ); // Horizontal GRAM Start Address
casval 0:0099ad246127 173 WriteCmdData( 0x51, 0x00EF ); // Horizontal GRAM End Address
casval 0:0099ad246127 174 WriteCmdData( 0x52, 0x0000 ); // Vertical GRAM Start Address
casval 0:0099ad246127 175 WriteCmdData( 0x53, 0x013F ); // Vertical GRAM Start Address
casval 0:0099ad246127 176 WriteCmdData( 0x60, gateScan ); // Gate Scan Line (0xA700)
casval 0:0099ad246127 177 // WriteCmdData( 0x61, 0x0000 ); // NDL,VLE, REV
casval 0:0099ad246127 178 WriteCmdData( 0x61, 0x0001 ); // NDL,VLE, REV
casval 0:0099ad246127 179 WriteCmdData( 0x6A, 0x0000 ); // set scrolling line
casval 0:0099ad246127 180 //-------------- Partial Display Control ---------//
casval 0:0099ad246127 181 WriteCmdData( 0x80, 0x0000 );
casval 0:0099ad246127 182 WriteCmdData( 0x81, 0x0000 );
casval 0:0099ad246127 183 WriteCmdData( 0x82, 0x0000 );
casval 0:0099ad246127 184 WriteCmdData( 0x83, 0x0000 );
casval 0:0099ad246127 185 WriteCmdData( 0x84, 0x0000 );
casval 0:0099ad246127 186 WriteCmdData( 0x85, 0x0000 );
casval 0:0099ad246127 187 //-------------- Panel Control -------------------//
casval 0:0099ad246127 188 WriteCmdData( 0x90, 0x0010 );
casval 0:0099ad246127 189 WriteCmdData( 0x92, 0x0000 );
casval 0:0099ad246127 190 //new lines
casval 0:0099ad246127 191 WriteCmdData( 0x93, 0x0003 );
casval 0:0099ad246127 192 WriteCmdData( 0x95, 0x0101 );
casval 0:0099ad246127 193 WriteCmdData( 0x97, 0x0000 );
casval 0:0099ad246127 194 WriteCmdData( 0x98, 0x0000 );
casval 0:0099ad246127 195
casval 0:0099ad246127 196 // WriteCmdData( 0x07, 0x0133 ); // 262K color and display ON
casval 0:0099ad246127 197 WriteCmdData( 0x07, 0x0021 ); // 262K color and display ON
casval 0:0099ad246127 198 WriteCmdData( 0x07, 0x0031 ); // 262K color and display ON
casval 0:0099ad246127 199 WriteCmdData( 0x07, 0x0173 ); // 262K color and display ON
casval 0:0099ad246127 200 //new line
casval 0:0099ad246127 201
casval 0:0099ad246127 202 wait_ms( 50 ); // Delay 50ms
casval 0:0099ad246127 203 Deactivate();
casval 0:0099ad246127 204 }
casval 0:0099ad246127 205
casval 0:0099ad246127 206 void ILI9328_LCD::Sleep( void )
casval 0:0099ad246127 207 {
casval 0:0099ad246127 208 Activate();
casval 0:0099ad246127 209 WriteCmdData( 0x10, 0x1692 ); // enter sleep mode
casval 0:0099ad246127 210 wait_ms( 200 );
casval 0:0099ad246127 211 LCD::Sleep();
casval 0:0099ad246127 212 Deactivate();
casval 0:0099ad246127 213 }
casval 0:0099ad246127 214
casval 0:0099ad246127 215 void ILI9328_LCD::WakeUp( void )
casval 0:0099ad246127 216 {
casval 0:0099ad246127 217 Activate();
casval 0:0099ad246127 218 WriteCmdData( 0x10, 0x1690 ); // exit sleep mode
casval 0:0099ad246127 219 wait_ms( 200 );
casval 0:0099ad246127 220 LCD::WakeUp();
casval 0:0099ad246127 221 Deactivate();
casval 0:0099ad246127 222 }
casval 0:0099ad246127 223
casval 0:0099ad246127 224 void ILI9328_LCD::WriteCmd( unsigned short cmd )
casval 0:0099ad246127 225 {
casval 0:0099ad246127 226 _lcd_pin_cs = LOW;
casval 0:0099ad246127 227 _lcd_pin_rs = LOW; // Control Reg
casval 0:0099ad246127 228 _lcd_pin_dir = HIGH;
casval 0:0099ad246127 229 _lcd_pin_en = LOW;
casval 0:0099ad246127 230 _lcd_port->write(cmd & 0xFF);
casval 0:0099ad246127 231 _lcd_pin_le = HIGH;
casval 0:0099ad246127 232 _lcd_pin_le = LOW;
casval 0:0099ad246127 233 _lcd_port->write((cmd >>8) & 0xFF );
casval 0:0099ad246127 234 pulseLow( _lcd_pin_wr );
casval 0:0099ad246127 235 _lcd_pin_cs = HIGH;
casval 0:0099ad246127 236 }
casval 0:0099ad246127 237
casval 0:0099ad246127 238 void ILI9328_LCD::WriteData( unsigned short data )
casval 0:0099ad246127 239 {
casval 0:0099ad246127 240 _lcd_pin_cs = LOW;
casval 0:0099ad246127 241 _lcd_pin_rs = HIGH; // Select Data
casval 0:0099ad246127 242 _lcd_pin_dir = HIGH;
casval 0:0099ad246127 243 _lcd_pin_en = LOW;
casval 0:0099ad246127 244 _lcd_port->write(data & 0xFF);
casval 0:0099ad246127 245 _lcd_pin_le = HIGH;
casval 0:0099ad246127 246 _lcd_pin_le = LOW;
casval 0:0099ad246127 247 _lcd_port->write((data >> 8) & 0xFF);
casval 0:0099ad246127 248 pulseLow( _lcd_pin_wr );
casval 0:0099ad246127 249 _lcd_pin_cs = HIGH;
casval 0:0099ad246127 250 }
casval 0:0099ad246127 251
casval 0:0099ad246127 252 void ILI9328_LCD::SetXY( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2 )
casval 0:0099ad246127 253 {
casval 0:0099ad246127 254 switch ( _orientation )
casval 0:0099ad246127 255 {
casval 0:0099ad246127 256 case LANDSCAPE:
casval 0:0099ad246127 257 case LANDSCAPE_REV:
casval 0:0099ad246127 258 WriteCmdData( 0x20, y1 );
casval 0:0099ad246127 259 WriteCmdData( 0x21, x1 );
casval 0:0099ad246127 260 WriteCmdData( 0x50, y1 );
casval 0:0099ad246127 261 WriteCmdData( 0x52, x1 );
casval 0:0099ad246127 262 WriteCmdData( 0x51, y2 );
casval 0:0099ad246127 263 WriteCmdData( 0x53, x2 );
casval 0:0099ad246127 264 break;
casval 0:0099ad246127 265
casval 0:0099ad246127 266 case PORTRAIT_REV:
casval 0:0099ad246127 267 case PORTRAIT:
casval 0:0099ad246127 268 default:
casval 0:0099ad246127 269 WriteCmdData( 0x20, x1 );
casval 0:0099ad246127 270 WriteCmdData( 0x21, y1 );
casval 0:0099ad246127 271 WriteCmdData( 0x50, x1 );
casval 0:0099ad246127 272 WriteCmdData( 0x52, y1 );
casval 0:0099ad246127 273 WriteCmdData( 0x51, x2 );
casval 0:0099ad246127 274 WriteCmdData( 0x53, y2 );
casval 0:0099ad246127 275 break;
casval 0:0099ad246127 276 }
casval 0:0099ad246127 277 WriteCmd( 0x22 );
casval 0:0099ad246127 278 }
casval 0:0099ad246127 279
casval 0:0099ad246127 280 void ILI9328_LCD::SetPixelColor( unsigned int color, colordepth_t mode )
casval 0:0099ad246127 281 {
casval 0:0099ad246127 282 unsigned char r, g, b;
casval 0:0099ad246127 283 unsigned short clr;
casval 0:0099ad246127 284 r = g = b = 0;
casval 0:0099ad246127 285 if ( _colorDepth == RGB16 )
casval 0:0099ad246127 286 {
casval 0:0099ad246127 287 switch ( mode )
casval 0:0099ad246127 288 {
casval 0:0099ad246127 289 case RGB16:
casval 0:0099ad246127 290 WriteData( color & 0xFFFF );
casval 0:0099ad246127 291 break;
casval 0:0099ad246127 292 case RGB18:
casval 0:0099ad246127 293 r = ( color >> 10 ) & 0xF8;
casval 0:0099ad246127 294 g = ( color >> 4 ) & 0xFC;
casval 0:0099ad246127 295 b = ( color >> 1 ) & 0x1F;
casval 0:0099ad246127 296 clr = ( ( r | ( g >> 5 ) ) << 8 ) | ( ( g << 3 ) | b );
casval 0:0099ad246127 297 WriteData( clr );
casval 0:0099ad246127 298 break;
casval 0:0099ad246127 299 case RGB24:
casval 0:0099ad246127 300 r = ( color >> 16 ) & 0xF8;
casval 0:0099ad246127 301 g = ( color >> 8 ) & 0xFC;
casval 0:0099ad246127 302 b = color & 0xF8;
casval 0:0099ad246127 303 clr = ( ( r | ( g >> 5 ) ) << 8 ) | ( ( g << 3 ) | ( b >> 3 ) );
casval 0:0099ad246127 304 WriteData( clr );
casval 0:0099ad246127 305 break;
casval 0:0099ad246127 306 }
casval 0:0099ad246127 307 }
casval 0:0099ad246127 308 else if ( _colorDepth == RGB18 )
casval 0:0099ad246127 309 {
casval 0:0099ad246127 310 switch ( mode )
casval 0:0099ad246127 311 {
casval 0:0099ad246127 312 case RGB16:
casval 0:0099ad246127 313 r = ( ( color >> 8 ) & 0xF8 ) | ( ( color & 0x8000 ) >> 13 );
casval 0:0099ad246127 314 g = ( color >> 3 ) & 0xFC;
casval 0:0099ad246127 315 b = ( ( color << 3 ) & 0xFC ) | ( ( color >> 3 ) & 0x01 );
casval 0:0099ad246127 316 break;
casval 0:0099ad246127 317 case RGB18:
casval 0:0099ad246127 318 b = ( color << 2 ) & 0xFC;
casval 0:0099ad246127 319 g = ( color >> 4 ) & 0xFC;
casval 0:0099ad246127 320 r = ( color >> 10 ) & 0xFC;
casval 0:0099ad246127 321 break;
casval 0:0099ad246127 322 case RGB24:
casval 0:0099ad246127 323 r = ( color >> 16 ) & 0xFC;
casval 0:0099ad246127 324 g = ( color >> 8 ) & 0xFC;
casval 0:0099ad246127 325 b = color & 0xFC;
casval 0:0099ad246127 326 break;
casval 0:0099ad246127 327 }
casval 0:0099ad246127 328 clr = ( r << 8 ) | ( g << 2 ) | ( b >> 4 );
casval 0:0099ad246127 329 WriteData( clr );
casval 0:0099ad246127 330 WriteData( b << 4 );
casval 0:0099ad246127 331 }
casval 0:0099ad246127 332 }