4 * Copyright 2003, 2004, 2005, 2006 Develer S.r.l. (http://www.develer.com/)
5 * Copyright 2001 Bernardo Innocenti <bernie@codewiz.org>
6 * This file is part of DevLib - See README.devlib for information.
11 * \author Bernardo Innocenti <bernie@develer.com>
12 * \author Stefano Fedrigo <aleph@develer.com>
14 * \brief Displaytech 32122A LCD driver
19 *#* Revision 1.2 2006/01/23 23:11:27 bernie
20 *#* Use RASTER_SIZE() to compute... err... the raster size.
22 *#* Revision 1.1 2006/01/16 03:50:57 bernie
23 *#* Import into DevLib.
29 #include <drv/timer.h>
33 #include <cfg/macros.h> /* BV() */
34 #include <cfg/debug.h>
40 #ifdef CONFIG_LCD_SOFTINT_REFRESH
42 /*! Interval between softint driven lcd refresh */
43 # define LCD_REFRESH_INTERVAL 20 /* 20ms -> 50fps */
45 #endif /* CONFIG_LCD_SOFTINT_REFRESH */
47 /*! Number of LCD pages */
50 /*! Width of an LCD page */
51 #define LCD_PAGESIZE (LCD_WIDTH / 2)
54 * \name LCD I/O pins/ports
57 #define LCD_PF_DB0 PF4
58 #define LCD_PF_DB1 PF5
59 #define LCD_PF_DB2 PF6
60 #define LCD_PF_DB3 PF7
61 #define LCD_PD_DB4 PD4
62 #define LCD_PD_DB5 PD5
63 #define LCD_PD_DB6 PD6
64 #define LCD_PD_DB7 PD7
72 * \name DB high nibble (DB[4-7])
75 #define LCD_DATA_HI_PORT PORTD
76 #define LCD_DATA_HI_PIN PIND
77 #define LCD_DATA_HI_DDR DDRD
78 #define LCD_DATA_HI_SHIFT 0
79 #define LCD_DATA_HI_MASK 0xF0
83 * \name DB low nibble (DB[0-3])
86 #define LCD_DATA_LO_PORT PORTF
87 #define LCD_DATA_LO_PIN PINF
88 #define LCD_DATA_LO_DDR DDRF
89 #define LCD_DATA_LO_SHIFT 4
90 #define LCD_DATA_LO_MASK 0xF0
94 * \name LCD bus control macros
97 #define LCD_CLR_A0 (PORTB &= ~BV(LCD_PB_A0))
98 #define LCD_SET_A0 (PORTB |= BV(LCD_PB_A0))
99 #define LCD_CLR_RD (PORTE &= ~BV(LCD_PE_RW))
100 #define LCD_SET_RD (PORTE |= BV(LCD_PE_RW))
101 #define LCD_CLR_E1 (PORTE &= ~BV(LCD_PE_E1))
102 #define LCD_SET_E1 (PORTE |= BV(LCD_PE_E1))
103 #define LCD_CLR_E2 (PORTE &= ~BV(LCD_PE_E2))
104 #define LCD_SET_E2 (PORTE |= BV(LCD_PE_E2))
105 #define LCD_SET_E(x) (PORTE |= (x))
106 #define LCD_CLR_E(x) (PORTE &= ~(x))
110 * \name Chip select bits for LCD_SET_E()
113 #define LCDF_E1 (BV(LCD_PE_E1))
114 #define LCDF_E2 (BV(LCD_PE_E2))
117 /*! Read from the LCD data bus (DB[0-7]) */
119 ((LCD_DATA_LO_PIN & LCD_DATA_LO_MASK) >> LCD_DATA_LO_SHIFT) | \
120 ((LCD_DATA_HI_PIN & LCD_DATA_HI_MASK) >> LCD_DATA_HI_SHIFT) \
123 /*! Write to the LCD data bus (DB[0-7]) */
124 #define LCD_WRITE(d) \
126 LCD_DATA_LO_PORT = (LCD_DATA_LO_PORT & ~LCD_DATA_LO_MASK) | (((d)<<LCD_DATA_LO_SHIFT) & LCD_DATA_LO_MASK); \
127 LCD_DATA_HI_PORT = (LCD_DATA_HI_PORT & ~LCD_DATA_HI_MASK) | (((d)<<LCD_DATA_HI_SHIFT) & LCD_DATA_HI_MASK); \
130 /*! Set data bus direction to output (write to display) */
133 LCD_DATA_LO_DDR |= LCD_DATA_LO_MASK; \
134 LCD_DATA_HI_DDR |= LCD_DATA_HI_MASK; \
137 /*! Set data bus direction to input (read from display) */
140 LCD_DATA_LO_DDR &= ~LCD_DATA_LO_MASK; \
141 LCD_DATA_HI_DDR &= ~LCD_DATA_HI_MASK; \
144 /*! Delay for tEW (160ns) */
145 #define LCD_DELAY_WRITE \
151 /*! Delay for tACC6 (180ns) */
152 #define LCD_DELAY_READ \
161 * \name 32122A Commands
164 #define LCD_CMD_DISPLAY_ON 0xAF
165 #define LCD_CMD_DISPLAY_OFF 0xAE
166 #define LCD_CMD_STARTLINE 0xC0
167 #define LCD_CMD_PAGEADDR 0xB8
168 #define LCD_CMD_COLADDR 0x00
169 #define LCD_CMD_ADC_LEFT 0xA1
170 #define LCD_CMD_ADC_RIGHT 0xA0
171 #define LCD_CMD_STATIC_OFF 0xA4
172 #define LCD_CMD_STATIC_ON 0xA5
173 #define LCD_CMD_DUTY_32 0xA9
174 #define LCD_CMD_DUTY_16 0xA8
175 #define LCD_CMD_RMW_ON 0xE0
176 #define LCD_CMD_RMW_OFF 0xEE
177 #define LCD_CMD_RESET 0xE2
181 #define LCDF_BUSY BV(7)
183 #ifdef CONFIG_LCD_WAIT
187 * RS __\____________/__
210 } while (status & LCDF_BUSY); \
214 #else /* CONFIG_LCD_WAIT */
216 #define WAIT_LCD do {} while(0)
218 #endif /* CONFIG_LCD_WAIT */
222 * Raster buffer to draw into.
224 * Bits in the bitmap bytes have vertical orientation,
225 * as required by the LCD driver.
227 DECLARE_WALL(wall_before_raster, WALL_SIZE)
228 static uint8_t lcd_raster[RASTER_SIZE(LCD_WIDTH, LCD_HEIGHT)];
229 DECLARE_WALL(wall_after_raster, WALL_SIZE)
231 /*! Default LCD bitmap */
232 struct Bitmap lcd_bitmap;
235 #ifdef CONFIG_LCD_SOFTINT_REFRESH
237 /*! Timer for regular LCD refresh */
238 static Timer *lcd_refresh_timer;
240 #endif /* CONFIG_LCD_SOFTINT_REFRESH */
244 static bool lcd_check(void)
247 uint16_t retries = 32768;
251 cbi(PORTC, PCB_LCD_RS);
252 sbi(PORTC, PCB_LCD_RW);
253 sbi(PORTC, PCB_LCD_E);
257 cbi(PORTC, PCB_LCD_E);
258 cbi(PORTC, PCB_LCD_RW);
259 } while ((status & LCDF_BUSY) && retries);
261 return (retries != 0);
266 static inline void lcd_cmd(uint8_t cmd, uint8_t chip)
271 * A0 __\____________/__
273 * R/W __________________
277 * DATA --<============>--
290 static inline uint8_t lcd_read(uint8_t chip)
305 * DATA -------<=====>----
324 static inline void lcd_write(uint8_t c, uint8_t chip)
333 * R/W __________________
337 * DATA -<==============>-
353 * Set LCD contrast PWM.
355 void lcd_setpwm(int duty)
357 ASSERT(duty >= LCD_MIN_PWM);
358 ASSERT(duty <= LCD_MAX_PWM);
364 static void lcd_clear(void)
368 for (page = 0; page < LCD_PAGES; ++page)
370 lcd_cmd(LCD_CMD_COLADDR | 0, LCDF_E1 | LCDF_E2);
371 lcd_cmd(LCD_CMD_PAGEADDR | page, LCDF_E1 | LCDF_E2);
372 for (j = 0; j < LCD_PAGESIZE; j++)
373 lcd_write(0, LCDF_E1 | LCDF_E2);
378 static void lcd_writeraster(const uint8_t *raster)
381 const uint8_t *right_raster;
383 CHECK_WALL(wall_before_raster);
384 CHECK_WALL(wall_after_raster);
386 for (page = 0; page < LCD_PAGES; ++page)
388 lcd_cmd(LCD_CMD_PAGEADDR | page, LCDF_E1 | LCDF_E2);
389 lcd_cmd(LCD_CMD_COLADDR | 0, LCDF_E1 | LCDF_E2);
391 /* Super optimized lamer loop */
392 right_raster = raster + LCD_PAGESIZE;
396 lcd_write(*raster++, LCDF_E1);
397 lcd_write(*right_raster++, LCDF_E2);
400 raster = right_raster;
405 void lcd_blit_bitmap(Bitmap *bm)
407 lcd_writeraster(bm->raster);
411 #ifdef CONFIG_LCD_SOFTINT_REFRESH
413 static void lcd_refresh_softint(void)
415 lcd_blit_bitmap(&lcd_bitmap);
416 timer_add(lcd_refresh_timer);
419 #endif /* CONFIG_LCD_SOFTINT_REFRESH */
423 * Initialize LCD subsystem.
425 * \note The PWM used for LCD contrast is initialized in drv/pwm.c
426 * because it is the same PWM used for output attenuation.
430 // FIXME: interrupts are already disabled when we get here?!?
432 IRQ_SAVE_DISABLE(flags);
434 PORTB |= BV(LCD_PB_A0);
435 DDRB |= BV(LCD_PB_A0);
437 PORTE &= ~(BV(LCD_PE_RW) | BV(LCD_PE_E1) | BV(LCD_PE_E2));
438 DDRE |= BV(LCD_PE_RW) | BV(LCD_PE_E1) | BV(LCD_PE_E2);
441 LCD_RESET_PORT |= BV(LCD_RESET_BIT);
442 LCD_RESET_DDR |= BV(LCD_RESET_BIT);
445 LCD_RESET_PORT &= ~BV(LCD_RESET_BIT);
448 LCD_RESET_PORT |= BV(LCD_RESET_BIT);
451 * Data bus is in output state most of the time:
452 * LCD r/w functions assume it is left in output state
456 // Wait for RST line to stabilize at Vcc.
459 IRQ_SAVE_DISABLE(flags);
461 lcd_cmd(LCD_CMD_RESET, LCDF_E1 | LCDF_E2);
462 lcd_cmd(LCD_CMD_DISPLAY_ON, LCDF_E1 | LCDF_E2);
463 lcd_cmd(LCD_CMD_STARTLINE | 0, LCDF_E1 | LCDF_E2);
465 /* Initialize anti-corruption walls for raster */
466 INIT_WALL(wall_before_raster);
467 INIT_WALL(wall_after_raster);
472 lcd_setpwm(LCD_DEF_PWM);
474 gfx_bitmapInit(&lcd_bitmap, lcd_raster, LCD_WIDTH, LCD_HEIGHT);
475 gfx_bitmapClear(&lcd_bitmap);
477 #ifdef CONFIG_LCD_SOFTINT_REFRESH
479 /* Init IRQ driven LCD refresh */
480 lcd_refresh_timer = timer_new();
481 ASSERT(lcd_refresh_timer != NULL);
482 INITEVENT_INT(&lcd_refresh_timer->expire, (Hook)lcd_refresh_softint, 0);
483 lcd_refresh_timer->delay = LCD_REFRESH_INTERVAL;
484 timer_add(lcd_refresh_timer);
486 #endif /* CONFIG_LCD_SOFTINT_REFRESH */