From 9411f259330967f9ad4e0cccd98ef37e811ba257 Mon Sep 17 00:00:00 2001 From: bernie Date: Mon, 16 Jan 2006 03:50:57 +0000 Subject: [PATCH] Import into DevLib. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@468 38d2e660-2303-0410-9eaa-f027e97ec537 --- drv/lcd_gfx.c | 484 ++++++++++++++++++++++++++++++++++++++++++++++++++ drv/lcd_gfx.h | 84 +++++++++ 2 files changed, 568 insertions(+) create mode 100755 drv/lcd_gfx.c create mode 100755 drv/lcd_gfx.h diff --git a/drv/lcd_gfx.c b/drv/lcd_gfx.c new file mode 100755 index 00000000..9a25cf09 --- /dev/null +++ b/drv/lcd_gfx.c @@ -0,0 +1,484 @@ +/** + * \file + * + * + * \version $Id$ + * + * \author Bernardo Innocenti + * \author Stefano Fedrigo + * + * \brief Displaytech 32122A LCD driver + */ + +/*#* + *#* $Log$ + *#* Revision 1.1 2006/01/16 03:50:57 bernie + *#* Import into DevLib. + *#* + *#*/ + +#include "lcd.h" +#include +#include + +#include +#include +#include /* BV() */ +#include + +#include +#include +#include + +#ifdef CONFIG_LCD_SOFTINT_REFRESH + + /*! Interval between softint driven lcd refresh */ +# define LCD_REFRESH_INTERVAL 20 /* 20ms -> 50fps */ + +#endif /* CONFIG_LCD_SOFTINT_REFRESH */ + +/*! Number of LCD pages */ +#define LCD_PAGES 4 + +/*! Width of an LCD page */ +#define LCD_PAGESIZE (LCD_WIDTH / 2) + +/*! + * \name LCD I/O pins/ports + * @{ + */ +#define LCD_PF_DB0 PF4 +#define LCD_PF_DB1 PF5 +#define LCD_PF_DB2 PF6 +#define LCD_PF_DB3 PF7 +#define LCD_PD_DB4 PD4 +#define LCD_PD_DB5 PD5 +#define LCD_PD_DB6 PD6 +#define LCD_PD_DB7 PD7 +#define LCD_PB_A0 PB0 +#define LCD_PE_RW PE7 +#define LCD_PE_E1 PE2 +#define LCD_PE_E2 PE6 +/*@}*/ + +/*! + * \name DB high nibble (DB[4-7]) + * @{ + */ +#define LCD_DATA_HI_PORT PORTD +#define LCD_DATA_HI_PIN PIND +#define LCD_DATA_HI_DDR DDRD +#define LCD_DATA_HI_SHIFT 0 +#define LCD_DATA_HI_MASK 0xF0 +/*@}*/ + +/*! + * \name DB low nibble (DB[0-3]) + * @{ + */ +#define LCD_DATA_LO_PORT PORTF +#define LCD_DATA_LO_PIN PINF +#define LCD_DATA_LO_DDR DDRF +#define LCD_DATA_LO_SHIFT 4 +#define LCD_DATA_LO_MASK 0xF0 +/*@}*/ + +/*! + * \name LCD bus control macros + * @{ + */ +#define LCD_CLR_A0 (PORTB &= ~BV(LCD_PB_A0)) +#define LCD_SET_A0 (PORTB |= BV(LCD_PB_A0)) +#define LCD_CLR_RD (PORTE &= ~BV(LCD_PE_RW)) +#define LCD_SET_RD (PORTE |= BV(LCD_PE_RW)) +#define LCD_CLR_E1 (PORTE &= ~BV(LCD_PE_E1)) +#define LCD_SET_E1 (PORTE |= BV(LCD_PE_E1)) +#define LCD_CLR_E2 (PORTE &= ~BV(LCD_PE_E2)) +#define LCD_SET_E2 (PORTE |= BV(LCD_PE_E2)) +#define LCD_SET_E(x) (PORTE |= (x)) +#define LCD_CLR_E(x) (PORTE &= ~(x)) +/*@}*/ + +/*! + * \name Chip select bits for LCD_SET_E() + * @{ + */ +#define LCDF_E1 (BV(LCD_PE_E1)) +#define LCDF_E2 (BV(LCD_PE_E2)) +/*@}*/ + +/*! Read from the LCD data bus (DB[0-7]) */ +#define LCD_READ ( \ + ((LCD_DATA_LO_PIN & LCD_DATA_LO_MASK) >> LCD_DATA_LO_SHIFT) | \ + ((LCD_DATA_HI_PIN & LCD_DATA_HI_MASK) >> LCD_DATA_HI_SHIFT) \ + ) + +/*! Write to the LCD data bus (DB[0-7]) */ +#define LCD_WRITE(d) \ + do { \ + LCD_DATA_LO_PORT = (LCD_DATA_LO_PORT & ~LCD_DATA_LO_MASK) | (((d)<-- + */ + LCD_WRITE(cmd); + //LCD_DB_OUT; + LCD_CLR_A0; + LCD_SET_E(chip); + LCD_DELAY_WRITE; + LCD_CLR_E(chip); + LCD_SET_A0; + //LCD_DB_IN; +} + + +static inline uint8_t lcd_read(uint8_t chip) +{ + uint8_t data; + + WAIT_LCD; + + /*! + * \code + * __________________ + * A0 __/ \__ + * ____________ + * R/W __/ \__ + * _______ + * E1 _____/ \____ + * + * DATA -------<=====>---- + * + * \endcode + */ + LCD_DB_IN; + //LCD_SET_A0; + LCD_SET_RD; + LCD_SET_E(chip); + LCD_DELAY_READ; + data = LCD_READ; + LCD_CLR_E(chip); + LCD_CLR_RD; + //LCD_CLR_A0; + LCD_DB_OUT; + + return data; +} + + +static inline void lcd_write(uint8_t c, uint8_t chip) +{ + WAIT_LCD; + + /*! + * \code + * __________________ + * A0 ___/ \___ + * + * R/W __________________ + * ______ + * E1 _____/ \_____ + * + * DATA -<==============>- + * + * \endcode + */ + LCD_WRITE(c); + //LCD_DB_OUT; + //LCD_SET_A0; + LCD_SET_E(chip); + LCD_DELAY_WRITE; + LCD_CLR_E(chip); + //LCD_CLR_A0; + //LCD_DB_IN; +} + + +/*! + * Set LCD contrast PWM. + */ +void lcd_setpwm(int duty) +{ + ASSERT(duty >= LCD_MIN_PWM); + ASSERT(duty <= LCD_MAX_PWM); + + OCR3C = duty; +} + + +static void lcd_clear(void) +{ + uint8_t page, j; + + for (page = 0; page < LCD_PAGES; ++page) + { + lcd_cmd(LCD_CMD_COLADDR | 0, LCDF_E1 | LCDF_E2); + lcd_cmd(LCD_CMD_PAGEADDR | page, LCDF_E1 | LCDF_E2); + for (j = 0; j < LCD_PAGESIZE; j++) + lcd_write(0, LCDF_E1 | LCDF_E2); + } +} + + +static void lcd_writeraster(const uint8_t *raster) +{ + uint8_t page, rows; + const uint8_t *right_raster; + + CHECK_WALL(wall_before_raster); + CHECK_WALL(wall_after_raster); + + for (page = 0; page < LCD_PAGES; ++page) + { + lcd_cmd(LCD_CMD_PAGEADDR | page, LCDF_E1 | LCDF_E2); + lcd_cmd(LCD_CMD_COLADDR | 0, LCDF_E1 | LCDF_E2); + + /* Super optimized lamer loop */ + right_raster = raster + LCD_PAGESIZE; + rows = LCD_PAGESIZE; + do + { + lcd_write(*raster++, LCDF_E1); + lcd_write(*right_raster++, LCDF_E2); + } + while (--rows); + raster = right_raster; + } +} + + +void lcd_blit_bitmap(Bitmap *bm) +{ + lcd_writeraster(bm->raster); +} + + +#ifdef CONFIG_LCD_SOFTINT_REFRESH + +static void lcd_refresh_softint(void) +{ + lcd_blit_bitmap(&lcd_bitmap); + timer_add(lcd_refresh_timer); +} + +#endif /* CONFIG_LCD_SOFTINT_REFRESH */ + + +/*! + * Initialize LCD subsystem. + * + * \note The PWM used for LCD contrast is initialized in drv/pwm.c + * because it is the same PWM used for output attenuation. + */ +void lcd_init(void) +{ + // FIXME: interrupts are already disabled when we get here?!? + cpuflags_t flags; + IRQ_SAVE_DISABLE(flags); + + PORTB |= BV(LCD_PB_A0); + DDRB |= BV(LCD_PB_A0); + + PORTE &= ~(BV(LCD_PE_RW) | BV(LCD_PE_E1) | BV(LCD_PE_E2)); + DDRE |= BV(LCD_PE_RW) | BV(LCD_PE_E1) | BV(LCD_PE_E2); + +/* LCD hw reset + LCD_RESET_PORT |= BV(LCD_RESET_BIT); + LCD_RESET_DDR |= BV(LCD_RESET_BIT); + LCD_DELAY_WRITE; + LCD_DELAY_WRITE; + LCD_RESET_PORT &= ~BV(LCD_RESET_BIT); + LCD_DELAY_WRITE; + LCD_DELAY_WRITE; + LCD_RESET_PORT |= BV(LCD_RESET_BIT); +*/ + /* + * Data bus is in output state most of the time: + * LCD r/w functions assume it is left in output state + */ + LCD_DB_OUT; + + // Wait for RST line to stabilize at Vcc. + IRQ_ENABLE; + timer_delay(20); + IRQ_SAVE_DISABLE(flags); + + lcd_cmd(LCD_CMD_RESET, LCDF_E1 | LCDF_E2); + lcd_cmd(LCD_CMD_DISPLAY_ON, LCDF_E1 | LCDF_E2); + lcd_cmd(LCD_CMD_STARTLINE | 0, LCDF_E1 | LCDF_E2); + + /* Initialize anti-corruption walls for raster */ + INIT_WALL(wall_before_raster); + INIT_WALL(wall_after_raster); + + IRQ_RESTORE(flags); + + lcd_clear(); + lcd_setpwm(LCD_DEF_PWM); + + gfx_bitmapInit(&lcd_bitmap, lcd_raster, LCD_WIDTH, LCD_HEIGHT); + gfx_bitmapClear(&lcd_bitmap); + +#ifdef CONFIG_LCD_SOFTINT_REFRESH + + /* Init IRQ driven LCD refresh */ + lcd_refresh_timer = timer_new(); + ASSERT(lcd_refresh_timer != NULL); + INITEVENT_INT(&lcd_refresh_timer->expire, (Hook)lcd_refresh_softint, 0); + lcd_refresh_timer->delay = LCD_REFRESH_INTERVAL; + timer_add(lcd_refresh_timer); + +#endif /* CONFIG_LCD_SOFTINT_REFRESH */ +} diff --git a/drv/lcd_gfx.h b/drv/lcd_gfx.h new file mode 100755 index 00000000..f22196a6 --- /dev/null +++ b/drv/lcd_gfx.h @@ -0,0 +1,84 @@ +/*! + * \file + * + * + * \version $Id$ + * + * \author Bernardo Innocenti + * \author Stefano Fedrigo + * + * \brief Displaytech 32122A LCD driver + */ + +/*#* + *#* $Log$ + *#* Revision 1.1 2006/01/16 03:50:57 bernie + *#* Import into DevLib. + *#* + *#* Revision 1.13 2005/11/27 22:41:08 bernie + *#* Remove trailing whitespace. + *#* + *#* Revision 1.12 2004/08/29 21:46:11 bernie + *#* CVSSILENT: Mark CVS log blocks. + *#* + *#* Revision 1.11 2004/06/11 12:40:11 customer_pw + *#* Changes for new board_kf + *#* + *#* Revision 1.10 2004/03/24 15:37:03 bernie + *#* Remove Copyright messages from Doxygen output + *#* + *#* Revision 1.9 2004/02/26 18:48:52 aleph + *#* Remove generic setting function which live better in levels.c + *#* + *#* Revision 1.8 2004/02/11 11:14:35 aleph + *#* Adeguate lcd_setcontrast to new leveledit callback interface + *#* + *#* Revision 1.7 2004/02/10 21:45:13 aleph + *#* Values for contrast setting are logical ascending (1->10) instead of raw PWM (240->50) + *#* + *#* Revision 1.6 2004/02/09 15:52:09 aleph + *#* Add reasonable min and max level #define for contrast + *#* + *#* Revision 1.5 2004/01/13 00:04:10 aleph + *#* change lcd_clear() to static and minor var/define name enhancements + *#* + *#* Revision 1.4 2004/01/07 19:06:48 aleph + *#* Some LCD bus optmizations and add bitmap rendering funcs + *#* + *#* Revision 1.3 2003/12/22 15:27:26 aleph + *#* Add E2 chip select and some code cosmetic changes + *#* + *#* Revision 1.2 2003/12/19 00:20:17 aleph + *#* Add contrast PWM + *#* + *#* Revision 1.1 2003/12/08 03:46:42 aleph + *#* Add lcd driver + *#* + *#*/ + +#ifndef LCD_GFX_H +#define LCD_GFX_H + +/* Predefined LCD PWM contrast values */ +#define LCD_DEF_PWM 145 +#define LCD_MAX_PWM 505 +#define LCD_MIN_PWM 130 + +/* Display bitmap dims */ +#define LCD_WIDTH 122 +#define LCD_HEIGHT 32 + +/* fwd decl */ +struct Bitmap; + +extern struct Bitmap lcd_bitmap; + +void lcd_init(void); +void lcd_setpwm(int duty); +void lcd_blit_bitmap(struct Bitmap *bm); + +#endif // LCD_GFX_H -- 2.25.1