From b4bf07decd9b30b2859d1fde9fb819716ff234f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henryk=20Pl=C3=B6tz?= Date: Wed, 1 Feb 2012 04:55:27 +0100 Subject: [PATCH 1/5] Add comments based on my understanding of the PCF8833 controller datasheet --- firmware/lcd/display.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/firmware/lcd/display.c b/firmware/lcd/display.c index 5d0301d..6d5dab1 100644 --- a/firmware/lcd/display.c +++ b/firmware/lcd/display.c @@ -175,7 +175,29 @@ void lcdInit(void) { delayms(5); // actually only needed after the first } }else{ /* displayType==DISPLAY_N1600 */ - static uint8_t initseq_d[] = { + static uint8_t initseq_d[] = { + /* Decoded: + * CMD 36: MADCTL (argument missing!) + * CMD 29: DISPON + * CMD BA: Data order (1) + * DAT 07: ignored? + * CMD 15: undefined? + * DAT 25: ignored? + * DAT 3F: ignored? + * CMD 11: sleep out + * CMD 13: normal display mode on + * CMD 37: set scroll entry point + * DAT 00: scroll entry point + * CMD 3A: interface pixel format + * DAT 05: 16 bit/pixel + * CMD 2A: column address set + * DAT 0 : xs + * DAT 98-1 : xe + * CMD 2B: page address set + * DAT 0 : ys + * DAT 70-1 : ye + */ + 0x36, 0x29, 0xBA, 0x07, 0x15, 0x25, 0x3f, From e1de1b4167020ba32abcbffc06dffa428bd98297 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henryk=20Pl=C3=B6tz?= Date: Wed, 1 Feb 2012 05:08:24 +0100 Subject: [PATCH 2/5] Add comments based on my understanding of the PCF8814 controller datasheet --- firmware/lcd/display.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/firmware/lcd/display.c b/firmware/lcd/display.c index 6d5dab1..8025dfe 100644 --- a/firmware/lcd/display.c +++ b/firmware/lcd/display.c @@ -166,6 +166,15 @@ void lcdInit(void) { lcd_select(); if(displayType==DISPLAY_N1200){ + /* Decoded: + * E2: Internal reset + * AF: Display on/off: DON = 1 + * A1: undefined? + * A4: all on/normal: DAL = 0 + * 2F: charge pump on/off: PC = 1 + * B0: set y address: Y[0-3] = 0 + * 10: set x address (upper bits): X[6-4] = 0 + */ static uint8_t initseq[]= { 0xE2,0xAF, // Display ON 0xA1, // Mirror-X 0xA4, 0x2F, 0xB0, 0x10}; From ab0c2ab4e780db89b8c46a747105a4f7d7b98a79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henryk=20Pl=C3=B6tz?= Date: Wed, 1 Feb 2012 05:53:33 +0100 Subject: [PATCH 3/5] Refactor to make the LCD initialization be data driven code --- firmware/lcd/display.c | 217 +++++++++++++++++++++++------------------ 1 file changed, 123 insertions(+), 94 deletions(-) diff --git a/firmware/lcd/display.c b/firmware/lcd/display.c index 8025dfe..66b2505 100644 --- a/firmware/lcd/display.c +++ b/firmware/lcd/display.c @@ -9,9 +9,106 @@ #include "basic/config.h" #include "usb/usbmsc.h" +enum display_type { + DISPLAY_TYPE_N1200 = 0, + DISPLAY_TYPE_N1600 = 1, +}; -#define DISPLAY_N1200 0 -#define DISPLAY_N1600 1 +enum command_type { + COMMAND_TYPE_CMD = 0, + COMMAND_TYPE_DATA = 1, +}; + +struct display_macro { + enum command_type type:1; + unsigned int data:8; + unsigned int delay_after:7; +} __attribute__((packed)); + +/* Small Nokia 1200 LCD docs: + * clear/ set + * on 0xae / 0xaf + * invert 0xa6 / 0xa7 + * mirror-x 0xA0 / 0xA1 + * mirror-y 0xc7 / 0xc8 + * + * 0x20+x contrast (0=black - 0x2e) + * 0x40+x offset in rows from top (-0x7f) + * 0x80+x contrast? (0=black -0x9f?) + * 0xd0+x black lines from top? (-0xdf?) + * + */ +/* Decoded: + * E2: Internal reset + * AF: Display on/off: DON = 1 + * A1: undefined? + * A4: all on/normal: DAL = 0 + * 2F: charge pump on/off: PC = 1 + * B0: set y address: Y[0-3] = 0 + * 10: set x address (upper bits): X[6-4] = 0 + */ +static const struct display_macro INIT_N1200[] = { + {COMMAND_TYPE_CMD, 0xE2, 5}, + {COMMAND_TYPE_CMD, 0xAF}, + {COMMAND_TYPE_CMD, 0xA1}, + {COMMAND_TYPE_CMD, 0x2F}, + {COMMAND_TYPE_CMD, 0xB0}, + {COMMAND_TYPE_CMD, 0x10}, +}; + +/* Decoded: + * CMD 36: MADCTL (argument missing!) + * CMD 29: DISPON + * CMD BA: Data order (1) + * DAT 07: ignored? + * CMD 15: undefined? + * DAT 25: ignored? + * DAT 3F: ignored? + * CMD 11: sleep out + * CMD 13: normal display mode on + * CMD 37: set scroll entry point + * DAT 00: scroll entry point + * CMD 3A: interface pixel format + * DAT 05: 16 bit/pixel + * CMD 2A: column address set + * DAT 0 : xs + * DAT 98-1 : xe + * CMD 2B: page address set + * DAT 0 : ys + * DAT 70-1 : ye + */ +static const struct display_macro INIT_N1600[] = { + {COMMAND_TYPE_CMD, 0x01, 10}, + {COMMAND_TYPE_CMD, 0x36}, + {COMMAND_TYPE_CMD, 0x29}, + {COMMAND_TYPE_CMD, 0xBA}, + {COMMAND_TYPE_DATA, 0x07}, + {COMMAND_TYPE_CMD, 0x15}, + {COMMAND_TYPE_DATA, 0x25}, + {COMMAND_TYPE_DATA, 0x3F}, + {COMMAND_TYPE_CMD, 0x11}, + {COMMAND_TYPE_CMD, 0x13}, + {COMMAND_TYPE_CMD, 0x37}, + {COMMAND_TYPE_DATA, 0x00}, + {COMMAND_TYPE_CMD, 0x3A}, + {COMMAND_TYPE_DATA, 0x05}, + {COMMAND_TYPE_CMD, 0x2A}, + {COMMAND_TYPE_DATA, 0x00}, + {COMMAND_TYPE_DATA, 98-1}, + {COMMAND_TYPE_CMD, 0x2B}, + {COMMAND_TYPE_DATA, 0}, + {COMMAND_TYPE_DATA, 70-1}, +}; + +struct display_descriptor { + const struct display_macro * init_macro; + int init_macro_length; +}; + +const struct display_descriptor DISPLAY_DESCRIPTORS[] = { + [DISPLAY_TYPE_N1200] = { INIT_N1200, sizeof(INIT_N1200)/sizeof(INIT_N1200[0]) }, + [DISPLAY_TYPE_N1600] = { INIT_N1600, sizeof(INIT_N1600)/sizeof(INIT_N1600[0]) }, +}; /**************************************************************************/ /* Utility routines to manage nokia display */ @@ -20,10 +117,9 @@ uint8_t lcdBuffer[RESX*RESY_B]; uint32_t intstatus; // Caches USB interrupt state // (need to disable MSC while displaying) -uint8_t displayType; +enum display_type displayType; +const struct display_descriptor *displayDescriptor; -#define TYPE_CMD 0 -#define TYPE_DATA 1 static void lcd_select() { #if CFG_USBMSC @@ -146,83 +242,16 @@ void lcdInit(void) { id=lcdRead(220); // ID3 if(id==14) - displayType=DISPLAY_N1600; + displayType=DISPLAY_TYPE_N1600; else /* ID3 == 48 */ - displayType=DISPLAY_N1200; + displayType=DISPLAY_TYPE_N1200; -/* Small Nokia 1200 LCD docs: - * clear/ set - * on 0xae / 0xaf - * invert 0xa6 / 0xa7 - * mirror-x 0xA0 / 0xA1 - * mirror-y 0xc7 / 0xc8 - * - * 0x20+x contrast (0=black - 0x2e) - * 0x40+x offset in rows from top (-0x7f) - * 0x80+x contrast? (0=black -0x9f?) - * 0xd0+x black lines from top? (-0xdf?) - * - */ + displayDescriptor = DISPLAY_DESCRIPTORS + displayType; + lcd_select(); - - if(displayType==DISPLAY_N1200){ - /* Decoded: - * E2: Internal reset - * AF: Display on/off: DON = 1 - * A1: undefined? - * A4: all on/normal: DAL = 0 - * 2F: charge pump on/off: PC = 1 - * B0: set y address: Y[0-3] = 0 - * 10: set x address (upper bits): X[6-4] = 0 - */ - static uint8_t initseq[]= { 0xE2,0xAF, // Display ON - 0xA1, // Mirror-X - 0xA4, 0x2F, 0xB0, 0x10}; - int i = 0; - while(i> 1; - } + for(int i=0; iinit_macro_length; i++) { + lcdWrite(displayDescriptor->init_macro[i].type, displayDescriptor->init_macro[i].data); + delayms(displayDescriptor->init_macro[i].delay_after); } lcd_deselect(); } @@ -261,8 +290,8 @@ bool lcdGetPixel(char x, char y){ // Color display hepler functions static void _helper_pixel16(uint16_t color){ - lcdWrite(TYPE_DATA,color>>8); - lcdWrite(TYPE_DATA,color&0xFF); + lcdWrite(COMMAND_TYPE_DATA,color>>8); + lcdWrite(COMMAND_TYPE_DATA,color&0xFF); } static void _helper_hline(uint16_t color){ @@ -278,10 +307,10 @@ void lcdDisplay(void) { char byte; lcd_select(); - if(displayType==DISPLAY_N1200){ - lcdWrite(TYPE_CMD,0xB0); - lcdWrite(TYPE_CMD,0x10); - lcdWrite(TYPE_CMD,0x00); + if(displayType==DISPLAY_TYPE_N1200){ + lcdWrite(COMMAND_TYPE_CMD,0xB0); + lcdWrite(COMMAND_TYPE_CMD,0x10); + lcdWrite(COMMAND_TYPE_CMD,0x00); uint16_t i,page; for(page=0; page> 3); backcolor= ((br&0xF8) << 8) | ((bg&0xFC)<<3) | ((bb&0xF8) >> 3); - lcdWrite(TYPE_CMD,0x2C); + lcdWrite(COMMAND_TYPE_CMD,0x2C); //top line of the frame... _helper_hline(framecolor); @@ -346,13 +375,13 @@ inline void lcdInvert(void) { void lcdSetContrast(int c) { lcd_select(); - if(displayType==DISPLAY_N1200){ + if(displayType==DISPLAY_TYPE_N1200){ if(c<0x1F) - lcdWrite(TYPE_CMD,0x80+c); - }else{ /* displayType==DISPLAY_N1600 */ + lcdWrite(COMMAND_TYPE_CMD,0x80+c); + }else{ /* displayType==DISPLAY_TYPE_N1600 */ if(c<0x40) { - lcdWrite(TYPE_CMD,0x25); - lcdWrite(TYPE_DATA,4*c); + lcdWrite(COMMAND_TYPE_CMD,0x25); + lcdWrite(COMMAND_TYPE_DATA,4*c); }; } lcd_deselect(); @@ -362,7 +391,7 @@ void lcdSetInvert(int c) { lcd_select(); /* it doesn't harm N1600, save space */ // if(displayType==DISPLAY_N1200) - lcdWrite(TYPE_CMD,(c&1)+0xa6); + lcdWrite(COMMAND_TYPE_CMD,(c&1)+0xa6); lcd_deselect(); }; From e66910638d6cade376d87d709fcd0f1ed02d61f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henryk=20Pl=C3=B6tz?= Date: Wed, 1 Feb 2012 06:15:07 +0100 Subject: [PATCH 4/5] Remove unnecessary commands from N1600 initialization; refactor display memory write setup to use macros --- firmware/lcd/display.c | 55 ++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/firmware/lcd/display.c b/firmware/lcd/display.c index 66b2505..e7dcb89 100644 --- a/firmware/lcd/display.c +++ b/firmware/lcd/display.c @@ -56,14 +56,15 @@ static const struct display_macro INIT_N1200[] = { {COMMAND_TYPE_CMD, 0x10}, }; +static const struct display_macro PREPARE_N1200[] = { + {COMMAND_TYPE_CMD, 0xB0}, + {COMMAND_TYPE_CMD, 0x10}, + {COMMAND_TYPE_CMD, 0x00}, +}; + /* Decoded: - * CMD 36: MADCTL (argument missing!) * CMD 29: DISPON * CMD BA: Data order (1) - * DAT 07: ignored? - * CMD 15: undefined? - * DAT 25: ignored? - * DAT 3F: ignored? * CMD 11: sleep out * CMD 13: normal display mode on * CMD 37: set scroll entry point @@ -79,13 +80,8 @@ static const struct display_macro INIT_N1200[] = { */ static const struct display_macro INIT_N1600[] = { {COMMAND_TYPE_CMD, 0x01, 10}, - {COMMAND_TYPE_CMD, 0x36}, {COMMAND_TYPE_CMD, 0x29}, {COMMAND_TYPE_CMD, 0xBA}, - {COMMAND_TYPE_DATA, 0x07}, - {COMMAND_TYPE_CMD, 0x15}, - {COMMAND_TYPE_DATA, 0x25}, - {COMMAND_TYPE_DATA, 0x3F}, {COMMAND_TYPE_CMD, 0x11}, {COMMAND_TYPE_CMD, 0x13}, {COMMAND_TYPE_CMD, 0x37}, @@ -100,14 +96,30 @@ static const struct display_macro INIT_N1600[] = { {COMMAND_TYPE_DATA, 70-1}, }; +static const struct display_macro PREPARE_N1600[] = { + {COMMAND_TYPE_CMD, 0x2C}, +}; + + struct display_descriptor { + /* Macro to execute in order to initialize the display from scratch */ const struct display_macro * init_macro; int init_macro_length; + + /* Macro to execute to prepare the display for sending raw contents */ + const struct display_macro * prepare_macro; + int prepare_macro_length; }; const struct display_descriptor DISPLAY_DESCRIPTORS[] = { - [DISPLAY_TYPE_N1200] = { INIT_N1200, sizeof(INIT_N1200)/sizeof(INIT_N1200[0]) }, - [DISPLAY_TYPE_N1600] = { INIT_N1600, sizeof(INIT_N1600)/sizeof(INIT_N1600[0]) }, + [DISPLAY_TYPE_N1200] = { + INIT_N1200, sizeof(INIT_N1200)/sizeof(INIT_N1200[0]), + PREPARE_N1200, sizeof(PREPARE_N1200)/sizeof(PREPARE_N1200[0]) + }, + [DISPLAY_TYPE_N1600] = { + INIT_N1600, sizeof(INIT_N1600)/sizeof(INIT_N1600[0]), + PREPARE_N1600, sizeof(PREPARE_N1600)/sizeof(PREPARE_N1600[0]) + }, }; /**************************************************************************/ @@ -221,6 +233,13 @@ uint8_t lcdRead(uint8_t data) return ret; } +static void lcdExecuteMacro(const struct display_macro *macro, int macro_length) +{ + for(int i=0; iinit_macro_length; i++) { - lcdWrite(displayDescriptor->init_macro[i].type, displayDescriptor->init_macro[i].data); - delayms(displayDescriptor->init_macro[i].delay_after); - } + lcdExecuteMacro(displayDescriptor->init_macro, displayDescriptor->init_macro_length); lcd_deselect(); } @@ -307,10 +323,9 @@ void lcdDisplay(void) { char byte; lcd_select(); + lcdExecuteMacro(displayDescriptor->prepare_macro, displayDescriptor->prepare_macro_length); + if(displayType==DISPLAY_TYPE_N1200){ - lcdWrite(COMMAND_TYPE_CMD,0xB0); - lcdWrite(COMMAND_TYPE_CMD,0x10); - lcdWrite(COMMAND_TYPE_CMD,0x00); uint16_t i,page; for(page=0; page> 3); backcolor= ((br&0xF8) << 8) | ((bg&0xFC)<<3) | ((bb&0xF8) >> 3); - lcdWrite(COMMAND_TYPE_CMD,0x2C); - //top line of the frame... _helper_hline(framecolor); From 2fdab582be7a2327fbf34184b3c0c8da7efae1d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henryk=20Pl=C3=B6tz?= Date: Wed, 1 Feb 2012 06:27:15 +0100 Subject: [PATCH 5/5] Refactor (to clarify) foreground/background/frame color handling for N1600 display --- firmware/lcd/display.c | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/firmware/lcd/display.c b/firmware/lcd/display.c index e7dcb89..1193942 100644 --- a/firmware/lcd/display.c +++ b/firmware/lcd/display.c @@ -315,9 +315,12 @@ static void _helper_hline(uint16_t color){ _helper_pixel16(color); } -#define THECOLOR_R 0x0 -#define THECOLOR_G 0x60 -#define THECOLOR_B 0x0 +#define COLORPACK_RGB565(r,g,b) (((r&0xF8) << 8) | ((g&0xFC)<<3) | ((b&0xF8) >> 3)) + +static const uint16_t COLOR_FG = COLORPACK_RGB565(0x00, 0x60, 0x00); +static const uint16_t COLOR_BG = COLORPACK_RGB565(0xff, 0xff, 0xff); +static const uint16_t COLOR_FRAME = COLORPACK_RGB565(0x00, 0x00, 0x80); + void lcdDisplay(void) { char byte; @@ -341,23 +344,15 @@ void lcdDisplay(void) { } } } else { /* displayType==DISPLAY_TYPE_N1600 */ - unsigned char r=THECOLOR_R,g=THECOLOR_G,b=THECOLOR_B; - unsigned char br=0xFF, bg=0xFF, bb=0xFF; - unsigned char frame_r=0x00, frame_g=0x00, frame_b=0x80; - uint16_t color,framecolor,backcolor; uint16_t x,y; bool px; - uint16_t actualcolor; - color = ((r&0xF8) << 8) | ((g&0xFC)<<3) | ((b&0xF8) >> 3); - framecolor= ((frame_r&0xF8) << 8) | ((frame_g&0xFC)<<3) | ((frame_b&0xF8) >> 3); - backcolor= ((br&0xF8) << 8) | ((bg&0xFC)<<3) | ((bb&0xF8) >> 3); //top line of the frame... - _helper_hline(framecolor); + _helper_hline(COLOR_FRAME); for(y=RESY;y>0;y--){ //left line of the frame - _helper_pixel16(framecolor); + _helper_pixel16(COLOR_FRAME); for(x=RESX;x>0;x--){ if(GLOBAL(lcdmirror)) @@ -365,17 +360,19 @@ void lcdDisplay(void) { else px=lcdGetPixel(x-1,y-1); - if((!px)^(!GLOBAL(lcdinvert))) actualcolor=color; - else actualcolor=backcolor; /* white */ + if((!px)^(!GLOBAL(lcdinvert))) { + _helper_pixel16(COLOR_FG); /* foreground */ + } else { + _helper_pixel16(COLOR_BG); /* background */ + } - _helper_pixel16(actualcolor); } //right line of the frame - _helper_pixel16(framecolor); + _helper_pixel16(COLOR_FRAME); } //bottom line of the frame - _helper_hline(framecolor); + _helper_hline(COLOR_FRAME); } lcd_deselect(); }