From bdb2652aa5cc20cc6a754ec59a19388a97f7f6fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20=C5=A0karvada?= Date: Wed, 16 Sep 2015 22:34:32 +0200 Subject: [PATCH] Various ILI9341 fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - added delay to reset command - fixed fast V/H lines - added support for extended registers and their correct initialization - added support for ILI9341_MIRROR_X and ILI9341_MIRROR_Y defines to allow customization of mirroring during compile time - added writeRegister40 method - readReg renamed to readReg8 and also added readReg16 and readReg32 - consolidated usage of CS, now is the CS also set inactive in write8inline and read8inline macros - fixed some spurious whitespaces Signed-off-by: Jaroslav Škarvada --- Adafruit_TFTLCD.cpp | 145 ++++++++++++++++++++++++++++++++++---------- Adafruit_TFTLCD.h | 9 ++- pin_magic.h | 8 ++- registers.h | 15 +++-- 4 files changed, 139 insertions(+), 38 deletions(-) diff --git a/Adafruit_TFTLCD.cpp b/Adafruit_TFTLCD.cpp index 69f6a83..f45602f 100644 --- a/Adafruit_TFTLCD.cpp +++ b/Adafruit_TFTLCD.cpp @@ -144,7 +144,7 @@ static const uint8_t HX8347G_regValues[] PROGMEM = { 0x1A , 0x02, 0x24 , 0x61, 0x25 , 0x5C, - + 0x18 , 0x36, 0x19 , 0x01, 0x1F , 0x88, @@ -278,16 +278,38 @@ void Adafruit_TFTLCD::begin(uint16_t id) { delay(50); writeRegister8(ILI9341_DISPLAYOFF, 0); +/* On my ILI9341 the pump ratio control register is set to 0 after + the reset. According to the datasheet this value is 'reserved', + and the register should be set to 0x20 (i.e. 2xVCI). + To be safe also setting other extended registers to their default values + according to the datasheet. If extended registers are not available, + the settings should be harmless and treated as NOP commands + (acording to the datasheet :). +*/ + writeRegister40(ILI9341_POWERCONTROLA, 0x392C0034, 0x02); + writeRegister24(ILI9341_POWERCONTROLB, 0x00A2F0); + writeRegister24(ILI9341_DRVTIMINGCONTROLA, 0x84117A); + writeRegister16(ILI9341_DRVTIMINGCONTROLB, 0x6600); + writeRegister32(ILI9341_PWRONCTRLSEQUENCE, 0x55012301); + + writeRegister8(ILI9341_PUMPRATIOCONTROL, 0x20); writeRegister8(ILI9341_POWERCONTROL1, 0x23); writeRegister8(ILI9341_POWERCONTROL2, 0x10); writeRegister16(ILI9341_VCOMCONTROL1, 0x2B2B); writeRegister8(ILI9341_VCOMCONTROL2, 0xC0); - writeRegister8(ILI9341_MEMCONTROL, ILI9341_MADCTL_MY | ILI9341_MADCTL_BGR); + d = 0; +#ifdef ILI9341_MIRROR_X + d |= ILI9341_MADCTL_MX; +#endif +#ifdef ILI9341_MIRROR_Y + d |= ILI9341_MADCTL_MY; +#endif + writeRegister8(ILI9341_MEMCONTROL, d | ILI9341_MADCTL_BGR); writeRegister8(ILI9341_PIXELFORMAT, 0x55); writeRegister16(ILI9341_FRAMECONTROL, 0x001B); - + writeRegister8(ILI9341_ENTRYMODE, 0x07); - /* writeRegister32(ILI9341_DISPLAYFUNC, 0x0A822700);*/ + // writeRegister32(ILI9341_DISPLAYFUNC, 0x0A822700); writeRegister8(ILI9341_SLEEPOUT, 0); delay(150); @@ -304,25 +326,25 @@ void Adafruit_TFTLCD::begin(uint16_t id) { uint8_t r = pgm_read_byte(&HX8357D_regValues[i++]); uint8_t len = pgm_read_byte(&HX8357D_regValues[i++]); if(r == TFTLCD_DELAY) { - delay(len); + delay(len); } else { - //Serial.print("Register $"); Serial.print(r, HEX); - //Serial.print(" datalen "); Serial.println(len); - - CS_ACTIVE; - CD_COMMAND; - write8(r); - CD_DATA; - for (uint8_t d=0; d> 24); + delayMicroseconds(10); + write8(d1 >> 16); + delayMicroseconds(10); + write8(d1 >> 8); + delayMicroseconds(10); + write8(d1); + delayMicroseconds(10); + write8(d2); + CS_IDLE; } diff --git a/Adafruit_TFTLCD.h b/Adafruit_TFTLCD.h index b6067e5..b6c1453 100644 --- a/Adafruit_TFTLCD.h +++ b/Adafruit_TFTLCD.h @@ -19,6 +19,10 @@ //#define USE_ADAFRUIT_SHIELD_PINOUT 1 +// Some ILI9341 based kits have mirrored X or Y, uncomment/comment respectively +//# define ILI9341_MIRROR_X 1 +# define ILI9341_MIRROR_Y 1 + class Adafruit_TFTLCD : public Adafruit_GFX { public: @@ -43,7 +47,9 @@ class Adafruit_TFTLCD : public Adafruit_GFX { uint16_t color565(uint8_t r, uint8_t g, uint8_t b), readPixel(int16_t x, int16_t y), readID(void); - uint32_t readReg(uint8_t r); + uint8_t readReg8(uint8_t r); + uint16_t readReg16(uint8_t r); + uint32_t readReg32(uint8_t r); private: @@ -67,6 +73,7 @@ class Adafruit_TFTLCD : public Adafruit_GFX { #endif writeRegister24(uint8_t a, uint32_t d), writeRegister32(uint8_t a, uint32_t d), + writeRegister40(uint8_t a, uint32_t d1, uint8_t d2), #ifndef writeRegisterPair writeRegisterPair(uint8_t aH, uint8_t aL, uint16_t d), #endif diff --git a/pin_magic.h b/pin_magic.h index 443dc91..9a08945 100644 --- a/pin_magic.h +++ b/pin_magic.h @@ -108,14 +108,18 @@ #else // Uno w/Breakout board #define write8inline(d) { \ + CS_ACTIVE; \ PORTD = (PORTD & B00000011) | ((d) & B11111100); \ PORTB = (PORTB & B11111100) | ((d) & B00000011); \ - WR_STROBE; } + WR_STROBE; \ + CS_IDLE;} #define read8inline(result) { \ + CS_ACTIVE; \ RD_ACTIVE; \ DELAY7; \ result = (PIND & B11111100) | (PINB & B00000011); \ - RD_IDLE; } + RD_IDLE; \ + CS_IDLE;} #define setWriteDirInline() { DDRD |= B11111100; DDRB |= B00000011; } #define setReadDirInline() { DDRD &= ~B11111100; DDRB &= ~B00000011; } diff --git a/registers.h b/registers.h index 810805d..5b9436b 100644 --- a/registers.h +++ b/registers.h @@ -80,10 +80,17 @@ #define ILI9341_ENTRYMODE 0xB7 #define ILI9341_POWERCONTROL1 0xC0 #define ILI9341_POWERCONTROL2 0xC1 -#define ILI9341_VCOMCONTROL1 0xC5 -#define ILI9341_VCOMCONTROL2 0xC7 -#define ILI9341_MEMCONTROL 0x36 -#define ILI9341_MADCTL 0x36 +#define ILI9341_VCOMCONTROL1 0xC5 +#define ILI9341_VCOMCONTROL2 0xC7 +#define ILI9341_MEMCONTROL 0x36 +#define ILI9341_MADCTL 0x36 +#define ILI9341_POWERCONTROLA 0xCB +#define ILI9341_POWERCONTROLB 0xCF +#define ILI9341_DRVTIMINGCONTROLA 0xE8 +#define ILI9341_DRVTIMINGCONTROLB 0xEA +#define ILI9341_PWRONCTRLSEQUENCE 0xED +#define ILI9341_ENABLE3G 0xF2 +#define ILI9341_PUMPRATIOCONTROL 0xF7 #define ILI9341_MADCTL_MY 0x80 #define ILI9341_MADCTL_MX 0x40