From 09402b6bde18b49e746093b107f64dc25df79e9e Mon Sep 17 00:00:00 2001 From: inmarket Date: Thu, 18 May 2017 19:24:53 +1000 Subject: [PATCH] KS0108 multi-chip driver thanks to doc_rob. --- changelog.txt | 1 + drivers/gdisp/KS0108/board_KS0108_example.h | 394 ++++++++++++++++++++ drivers/gdisp/KS0108/driver.mk | 2 + drivers/gdisp/KS0108/gdisp_lld_KS0108.c | 327 ++++++++++++++++ drivers/gdisp/KS0108/gdisp_lld_config.h | 18 + 5 files changed, 742 insertions(+) create mode 100644 drivers/gdisp/KS0108/board_KS0108_example.h create mode 100644 drivers/gdisp/KS0108/driver.mk create mode 100644 drivers/gdisp/KS0108/gdisp_lld_KS0108.c create mode 100644 drivers/gdisp/KS0108/gdisp_lld_config.h diff --git a/changelog.txt b/changelog.txt index be586e22..9de5bdd7 100644 --- a/changelog.txt +++ b/changelog.txt @@ -35,6 +35,7 @@ FEATURE: Added UC1601s driver FIX: Fixed issues with the STM746-Discovery board with ChibiOS FEATURE: Added partial definition for the STM32F469i-Discovery board FIX: Fixed issue where the variable type of the syncflags of the STM32LTDC driver was too small +FEATURE: Added KS0108 driver *** Release 2.7 *** diff --git a/drivers/gdisp/KS0108/board_KS0108_example.h b/drivers/gdisp/KS0108/board_KS0108_example.h new file mode 100644 index 00000000..86541f73 --- /dev/null +++ b/drivers/gdisp/KS0108/board_KS0108_example.h @@ -0,0 +1,394 @@ +/* + * This file is subject to the terms of the GFX License. If a copy of + * the license was not distributed with this file, you can obtain one at: + * + * http://ugfx.org/license.html + */ +/* KS0107/0108 Driver + * Pinout: There are at least two different Pinouts! + * Version 1: + * 1 - CS1 High Active + * 2 - CS2 High Active + * 3 - GND + * 4 - 5V + * 5 - CONTRAST + * 6 - RS(=DC) High = Data, Low = Command + * 7 - R/W High = Read, Low = Write + * 8 - E Data is KS0108_latched at the falling Edge + * 9..16 - D0..7 + * Version 2: + * 1 - GND + * 2 - 5V + * 3 - Vo (Operating Voltage for LCD) + * 4 - RS High = Data, Low = Command + * 5 - R/W High = Read, Low = Write + * 6 - E Data is KS0108_latched at the falling Edge + * 7..14 - D0..7 + * 15 - CS1 High Active + * 16 - CS2 High Active + * 17 - /RST Low Active + * 18 - Vout (Output voltage for LCD) + * 19 - LEDA + * 20 - LEDK + * + */ +#ifndef _GDISP_LLD_BOARD_H +#define _GDISP_LLD_BOARD_H + +// My Settings +#define GDISP_SCREEN_HEIGHT 64 +#define GDISP_SCREEN_WIDTH 128 +#define KS0108_NEED_READ FALSE +#define KS0108_HAS_RESET FALSE +#define KS0108_NEED_BACKLIGHT FALSE +#define KS0108_NEED_PWMBACKLIGHT FALSE +#define KS0108_NOP_DLY TRUE //doesn't work for me without! + +#define LINE_D0 PAL_LINE(GPIOB, 0U) +#define LINE_D1 PAL_LINE(GPIOB, 1U) +#define LINE_D2 PAL_LINE(GPIOB, 2U) +#define LINE_D3 PAL_LINE(GPIOB, 3U) +#define LINE_D4 PAL_LINE(GPIOB, 4U) +#define LINE_D5 PAL_LINE(GPIOB, 5U) +#define LINE_D6 PAL_LINE(GPIOB, 6U) +#define LINE_D7 PAL_LINE(GPIOB, 7U) +#define LINE_BUSY PAL_LINE(GPIOB, 7U) +#define LINE_CS1 PAL_LINE(GPIOB, 8U) +#define LINE_CS2 PAL_LINE(GPIOB, 9U) +#define LINE_E PAL_LINE(GPIOB, 10U) +#define LINE_DC PAL_LINE(GPIOB, 11U) +#if KS0108_NEED_READ + #define LINE_RW PAL_LINE(GPIOB, 12U) +#endif +#if KS0108_NEED_BACKLIGHT + #define LINE_BL PAL_LINE(GPIOB, 13U) +#endif +#if KS0108_HAS_RESET + #define LINE_RESET PAL_LINE(GPIOB, 13U) +#endif + +typedef struct { + ioline_t CS1; // Chip Select1 PIN + ioline_t CS2; // Chip Select2 PIN + ioline_t DC; // Register selector PIN + ioline_t E; // Enable PIN + ioline_t BUSY; // Busy PIN (Same as D[7]) + #if KS0108_NEED_READ + ioline_t RW; // Read/Write PIN + #endif + #if KS0108_HAS_RESET + ioline_t RST; // Reset PIN + #endif + #if KS0108_NEED_BACKLIGHT + ioline_t BL; // Backlight PIN + #endif + ioline_t D[8]; + ioline_t CS[CHIPS]; + } lcd_pins_t; + +/*===========================================================================*/ +/* Driver local variables. */ +/*===========================================================================*/ +static const lcd_pins_t lcdpins = { + LINE_CS1, + LINE_CS2, + LINE_DC, + LINE_E, + LINE_BUSY, + #if KS0108_NEED_READ + LINE_RW, + #endif + #if KS0108_HAS_RESET + LINE_RST, + #endif + #if KS0108_NEED_BACKLIGHT + LINE_BL, + #endif + { + LINE_D0, + LINE_D1, + LINE_D2, + LINE_D3, + LINE_D4, + LINE_D5, + LINE_D6, + LINE_D7 + }, + { + LINE_CS1, + LINE_CS2 + } +}; + +static GFXINLINE void init_board(GDisplay* g){ + (void) g; + g->board = 0; + int ii; + + for (ii = 0; ii < CHIPS; ii++){ + palSetLineMode(lcdpins.CS[ii], PAL_MODE_OUTPUT_PUSHPULL); + palClearLine(lcdpins.CS[ii]); + } + palSetLineMode(lcdpins.DC, PAL_MODE_OUTPUT_PUSHPULL); + palClearLine(lcdpins.DC); + palSetLineMode(lcdpins.E, PAL_MODE_OUTPUT_PUSHPULL); + palSetLine(lcdpins.E); + #if KS0108_NEED_READ + palSetLineMode(lcdpins.RW, PAL_MODE_OUTPUT_PUSHPULL); + palClearLine(lcdpins.RW); + #endif + #if KS0108_HAS_RESET + palSetLineMode(lcdpins.RST, PAL_MODE_OUTPUT_PUSHPULL); + palSetLine(lcdpins.RST); + #endif + for(ii = 0; ii < 8; ii++) { + palSetLineMode(lcdpins.D[ii], PAL_MODE_OUTPUT_PUSHPULL); + palClearLine(lcdpins.D[ii]); + } + #if KS0108_NEED_BACKLIGHT + #if KS0108_NEED_PWMBACKLIGHT + palSetLineMode(lcdpins.BL, PAL_MODE_ALTERNATE(1)); + #else + palSetLineMode(lcdpins.BL, PAL_MODE_OUTPUT_PUSHPULL); + #endif + #endif +} + +static GFXINLINE void post_init_board(GDisplay *g) { + (void) g; +} + +static GFXINLINE void setpin_reset(GDisplay *g, bool_t state) { + (void) g; + (void) state; + + #if KS0108_HAS_RESET //Make Hardware Reset + if (state) + palClearLine(lcdpins.RST); + else + palSetLine(lcdpins.RST); + #endif +} + +static GFXINLINE void set_backlight(GDisplay *g, uint8_t percent) { + (void) g; + (void) percent; +} + +static GFXINLINE void acquire_bus(GDisplay *g) { + (void) g; +} + +static GFXINLINE void release_bus(GDisplay *g) { + (void) g; +} + +void KS0108_delay(uint16_t microsec){ + #if KS0108_NOP_DLY + uint16_t cn; + for (cn=0;cn< microsec;cn++){ + + asm("nop");asm("nop");asm("nop");asm("nop"); + asm("nop");asm("nop");asm("nop");asm("nop"); + asm("nop");asm("nop");asm("nop");asm("nop"); + asm("nop");asm("nop");asm("nop");asm("nop"); + + asm("nop");asm("nop");asm("nop");asm("nop"); + asm("nop");asm("nop");asm("nop");asm("nop"); + asm("nop");asm("nop");asm("nop");asm("nop"); + asm("nop");asm("nop");asm("nop");asm("nop"); + + asm("nop");asm("nop");asm("nop");asm("nop"); + asm("nop");asm("nop");asm("nop");asm("nop"); + asm("nop");asm("nop");asm("nop");asm("nop"); + asm("nop");asm("nop");asm("nop");asm("nop"); + + asm("nop");asm("nop");asm("nop");asm("nop"); + asm("nop");asm("nop");asm("nop");asm("nop"); + asm("nop");asm("nop");asm("nop");asm("nop"); + asm("nop");asm("nop");asm("nop");asm("nop"); + + //asm("nop");asm("nop");asm("nop");asm("nop"); + //asm("nop");asm("nop");asm("nop");asm("nop"); + //1us + } + #else + gfxSleepMicroseconds(microsec); + #endif +} + +static GFXINLINE void KS0108_latch(void){ + palClearLine(lcdpins.E); + KS0108_delay(1); + palSetLine(lcdpins.E); +} + +static GFXINLINE void KS0108_write(uint8_t value){ + int ii; + + for(ii = 0; ii < 8; ii++){ + if(value & (1 << ii)) + palSetLine(lcdpins.D[ii]); + else + palClearLine(lcdpins.D[ii]); + } + KS0108_delay(1); +} + +static GFXINLINE void KS0108_select(uint8_t chip){ + uint8_t ii; + KS0108_delay(1); + for (ii = 0; ii < CHIPS; ii++){ + if (ii == chip) + palSetLine(lcdpins.CS[ii]); + } + KS0108_delay(1); +} + +static GFXINLINE void KS0108_unselect(void){ + uint8_t ii; + + KS0108_delay(1); + for (ii = 0; ii < CHIPS; ii++){ + palClearLine(lcdpins.CS[ii]); + } +} + +/* +#if KS0108_NEED_READ //Hardware Read ############## WORKS more or less +static GFXINLINE uint8_t read_KS0108(void) { + uint8_t ii, data=0; + + for(ii = 0; ii < 8; ii++) + palSetLineMode(lcdpins.D[ii], PAL_MODE_INPUT); //Set pads to input + palSetLine(lcdpins.RW); + KS0108_delay(1); + palClearLine(lcdpins.E); + KS0108_delay(1); + palSetLine(lcdpins.E); + KS0108_delay(1); +// palClearLine(lcdpins.RW); +// KS0108_delay(1); +// palSetLine(lcdpins.RW); +// KS0108_delay(1); + for(ii = 0; ii < 8; ii++){ + if (palReadLine(lcdpins.D[ii]) == PAL_HIGH){ //Read output + data |= (1<>8)); + #if KS0108_NEED_READ + displayData=read_KS0108(); + #else + p = RAM(g) + (GDISP_SCREEN_WIDTH*(g->p.y >> 3)) + g->p.x; + displayData = *p; + #endif + bit = 1 << (g->p.y & 7); //Get Page bit + + if ((uint8_t)data){ //set bit + KS0108_write(displayData | bit); + #if !KS0108_NEED_READ + *p = (displayData | bit); + #endif + KS0108_latch(); + } else { + KS0108_write(displayData & ~bit); + #if !KS0108_NEED_READ + *p = (displayData & ~bit); + #endif + KS0108_latch(); + } + KS0108_delay(2); //one is too short! + KS0108_unselect(); //Important +} + +static GFXINLINE void write_cmd(GDisplay* g, uint16_t cmd){ + (void)g; + palClearLine(lcdpins.DC); + KS0108_select((uint8_t)(cmd>>8)); + KS0108_write((uint8_t)cmd); + KS0108_latch(); + KS0108_unselect(); //Important +} + +static GFXINLINE void setreadmode(GDisplay *g) { + (void) g; +} + +static GFXINLINE void setwritemode(GDisplay *g) { + (void) g; +} + +static GFXINLINE uint16_t read_data(GDisplay *g) { + (void) g; + return 0; +} +#endif /* _GDISP_LLD_BOARD_H */ diff --git a/drivers/gdisp/KS0108/driver.mk b/drivers/gdisp/KS0108/driver.mk new file mode 100644 index 00000000..9fa2d2c4 --- /dev/null +++ b/drivers/gdisp/KS0108/driver.mk @@ -0,0 +1,2 @@ +GFXINC += $(GFXLIB)/drivers/gdisp/KS0108 +GFXSRC += $(GFXLIB)/drivers/gdisp/KS0108/gdisp_lld_KS0108.c diff --git a/drivers/gdisp/KS0108/gdisp_lld_KS0108.c b/drivers/gdisp/KS0108/gdisp_lld_KS0108.c new file mode 100644 index 00000000..af058ebb --- /dev/null +++ b/drivers/gdisp/KS0108/gdisp_lld_KS0108.c @@ -0,0 +1,327 @@ +/* + * This file is subject to the terms of the GFX License. If a copy of + * the license was not distributed with this file, you can obtain one at: + * + * http://ugfx.org/license.html + */ + +#include "gfx.h" + +#if GFX_USE_GDISP + +/* Robert Offner 2017 + * This is a driver for the KS0107 Displays. + * It should work with displays up to 240 pixel in width and 64 pixel in height + * ATTENTION some newer displays on ebay look like KS0107 Type but they're not! + * They use a new controller: ST7920. These are easy to identify: NO CS pin but + * a PSB Pin which switches from parallel to serial communication mode. + * If it cost less than 10.- it is probably a ST7920. + * Features: Well not much. For now write pixel to display and + * Read from Display / Buffer in RAM. Buffer in Ram is much faster than + * readback! + * I am trying to get the auto increment feature somehow implemented. It + * basically means if you start at x=0 and continue with x=1, x=2,... you don't + * have to write the address just data. + * + * Version: 0.3 + * + * History: + * v0.4 + * .) Cleanup by uGFX team. Code needs a lot of work. + * + * v0.3 + * .) Removed initialization of g->priv because it is already done by uGFX. + * + * v0.2 + * .) Cleanup, + * .) changed osalThreadSleep to gfxSleepMicroseconds(x) + * ATTENTION: for gfxSleepMicroseconds to work on chibios you have to + * increase CH_CFG_ST_FREQUENCY to 1000000 and probably CH_CFG_ST_RESOLUTION + * to 32 bit (not tested because STM32F103 doesn't have a 32 bit timer + * .) changed lcdbuf to g->priv + * + * v0.1 Initial Release + */ + +#define GDISP_DRIVER_VMT GDISPVMT_KS0108 +#include "gdisp_lld_config.h" +#include "../../../src/gdisp/gdisp_driver.h" + +#include "board_KS0108.h" + +/*===========================================================================*/ +/* Driver local definitions. */ +/*===========================================================================*/ + +#ifndef GDISP_SCREEN_HEIGHT + #define GDISP_SCREEN_HEIGHT 64 +#endif +#ifndef GDISP_SCREEN_WIDTH + #define GDISP_SCREEN_WIDTH 128 +#endif + +#define CHIPS (GDISP_SCREEN_WIDTH/64) + +#if !KS0108_NEED_READ + #define BUFFSZ (GDISP_SCREEN_HEIGHT/8 * GDISP_SCREEN_WIDTH) + #define RAM(g) ((uint8_t *)g->priv) +#endif +#ifndef GDISP_INITIAL_CONTRAST + #define GDISP_INITIAL_CONTRAST 50 +#endif +#ifndef GDISP_INITIAL_BACKLIGHT + #define GDISP_INITIAL_BACKLIGHT 100 +#endif + +// KS0108 Commands +#define KS0108_CHIP1_ON 0x003F +#define KS0108_CHIP2_ON 0x013F +#define KS0108_DISP1START 0x00C0 +#define KS0108_DISP2START 0x01C0 +#define KS0108_DISP1OFF 0x003E +#define KS0108_DISP2OFF 0x013E +#define KS0108_SET_ADDR 0x0040 +#define KS0108_SET_PAGE 0x00B8 + +/*===========================================================================*/ +/* Driver local functions. */ +/*===========================================================================*/ +#ifndef write_data_repeat + #define write_data_repeat(g, data, count) { int i; for (i = 0; i < count; ++i) write_data (g, data); } +#endif + +/*===========================================================================*/ +/* Driver exported functions. */ +/*===========================================================================*/ + +LLDSPEC bool_t gdisp_lld_init(GDisplay *g) { + #if !KS0108_NEED_READ + // The private area is the display surface. + g->priv = gfxAlloc(BUFFSZ); + #else + g->priv = 0; + #endif + // Initialise the board interface + init_board(g); + + #if KS0108_HAS_RESET //Make Hardware Reset + setpin_reset(g, TRUE); + gfxSleepMilliseconds(120); + setpin_reset(g, FALSE); + #endif + gfxSleepMilliseconds(120); + write_cmd(g, KS0108_DISP1OFF); + gfxSleepMilliseconds(1); + write_cmd(g, KS0108_DISP2OFF); + gfxSleepMilliseconds(1); + write_cmd(g, KS0108_CHIP1_ON); //0x3F Chip1 + gfxSleepMilliseconds(1); + write_cmd(g, KS0108_CHIP2_ON); //0x3F Chip2 + gfxSleepMilliseconds(1); + write_cmd(g, KS0108_DISP1START); //0xC0 Chip1 + gfxSleepMilliseconds(1); + write_cmd(g, KS0108_DISP2START); //0xC0 Chip2 + gfxSleepMilliseconds(1); + + // Finish Init + post_init_board(g); + + #if KS0108_NEED_BACKLIGHT + // Turn on the back-light + set_backlight(g, GDISP_INITIAL_BACKLIGHT); + #endif + + // Initialise the GDISP structure + g->g.Width = GDISP_SCREEN_WIDTH; + g->g.Height = GDISP_SCREEN_HEIGHT; + g->g.Orientation = GDISP_ROTATE_0; + g->g.Powermode = powerOn; + g->g.Backlight = GDISP_INITIAL_BACKLIGHT; + g->g.Contrast = GDISP_INITIAL_CONTRAST; + + return TRUE; +} + +GFXINLINE void KS0108_goto(GDisplay* g, ) { +} + +static void set_viewport(GDisplay *g) { + uint16_t pg; + uint16_t chip; + + pg = g->p.y >> 3; + chip = (g->p.x >> 6) << 8; + write_cmd(g, KS0108_SET_PAGE | chip | pg); // (0xB8) - Set page + write_cmd(g, KS0108_SET_ADDR | chip | g->p.x); // (0x40) - Set x Address +} + +LLDSPEC void gdisp_lld_write_color(GDisplay *g) { + uint16_t data; + + data = (g->p.x >> 6) << 8; // Set the chip + if (g->p.color != White) + data |= 0x01; // set dot + write_data(g, data); +} + +LLDSPEC void gdisp_lld_draw_pixel(GDisplay *g) { + set_viewport(g); + gdisp_lld_write_color(g); +} + +#if GDISP_HARDWARE_STREAM_WRITE + LLDSPEC void gdisp_lld_write_start(GDisplay *g) { + acquire_bus(g); + set_viewport(g); + } + + LLDSPEC void gdisp_lld_write_stop(GDisplay *g) { + release_bus(g); + } +#endif + +#if GDISP_HARDWARE_STREAM_READ + LLDSPEC void gdisp_lld_read_start(GDisplay *g) { + acquire_bus(g); + set_viewport(g); + setreadmode(g); + dummy_read(g); + } + + LLDSPEC color_t gdisp_lld_read_color(GDisplay *g) { + uint16_t data; + + data = read_data(g); + return gdispNative2Color(data); + } + + LLDSPEC void gdisp_lld_read_stop(GDisplay *g) { + setwritemode(g); + release_bus(g); + } +#endif + +#if GDISP_HARDWARE_FILLS + LLDSPEC void gdisp_lld_fill_area(GDisplay *g) { + uint8_t data, j; + set_viewport(g); + + if (g->p.color != White) { + data = 0xFF; // set dot + } + else { + data = 0; // clr dot + } + for (j=0; j < (g->p.cy)/8; j++) { + write_data_repeat(g, data, (g->p.cx)); + (g->p.cy) +=8; + set_viewport(g); + } + + } +#endif // GDISP_HARDWARE_FILLS + +#if GDISP_NEED_CONTROL && GDISP_HARDWARE_CONTROL + LLDSPEC void gdisp_lld_control(GDisplay *g) { + switch(g->p.x) { + case GDISP_CONTROL_POWER: + if (g->g.Powermode == (powermode_t)g->p.ptr) + return; + + switch((powermode_t)g->p.ptr) { + case powerOff: + acquire_bus(g); + write_index(g, 0x28); + gfxSleepMilliseconds(10); + write_index(g, 0x10); + release_bus(g); + break; + + case powerOn: + acquire_bus(g); + write_index(g, 0x11); + gfxSleepMilliseconds(120); + write_index(g, 0x29); + release_bus(g); + if (g->g.Powermode != powerSleep) + gdisp_lld_init(g); + break; + + case powerSleep: + acquire_bus(g); + write_index(g, 0x28); + gfxSleepMilliseconds(10); + write_index(g, 0x10); + release_bus(g); + break; + + default: + return; + } + + g->g.Powermode = (powermode_t)g->p.ptr; + return; + + case GDISP_CONTROL_ORIENTATION: + if (g->g.Orientation == (orientation_t)g->p.ptr) + return; + + switch((orientation_t)g->p.ptr) { + case GDISP_ROTATE_0: + acquire_bus(g); + + write_index(g, 0x36); + write_data(g, 0x08); + + release_bus(g); + g->g.Height = GDISP_SCREEN_HEIGHT; + g->g.Width = GDISP_SCREEN_WIDTH; + break; + + case GDISP_ROTATE_90: + acquire_bus(g); + + write_index(g, 0x36); + write_data(g, 0x68); + + release_bus(g); + g->g.Height = GDISP_SCREEN_WIDTH; + g->g.Width = GDISP_SCREEN_HEIGHT; + break; + + case GDISP_ROTATE_180: + acquire_bus(g); + + write_index(g, 0x36); + write_data(g, 0xC8); + + release_bus(g); + g->g.Height = GDISP_SCREEN_HEIGHT; + g->g.Width = GDISP_SCREEN_WIDTH; + break; + + case GDISP_ROTATE_270: + acquire_bus(g); + + write_index(g, 0x36); + write_data(g, 0xA8); + + release_bus(g); + g->g.Height = GDISP_SCREEN_WIDTH; + g->g.Width = GDISP_SCREEN_HEIGHT; + break; + + default: + return; + } + + g->g.Orientation = (orientation_t)g->p.ptr; + return; + + default: + return; + } + } +#endif + +#endif /* GFX_USE_GDISP */ diff --git a/drivers/gdisp/KS0108/gdisp_lld_config.h b/drivers/gdisp/KS0108/gdisp_lld_config.h new file mode 100644 index 00000000..a153e224 --- /dev/null +++ b/drivers/gdisp/KS0108/gdisp_lld_config.h @@ -0,0 +1,18 @@ +/* + * This file is subject to the terms of the GFX License. If a copy of + * the license was not distributed with this file, you can obtain one at: + * + * http://ugfx.org/license.html + */ + +#ifndef _GDISP_LLD_CONFIG_H +#define _GDISP_LLD_CONFIG_H + +/*===========================================================================*/ +/* Driver hardware support. */ +/*===========================================================================*/ + +#define GDISP_HARDWARE_DRAWPIXEL TRUE +#define GDISP_LLD_PIXELFORMAT GDISP_PIXELFORMAT_MONO + +#endif /* _GDISP_LLD_CONFIG_H */