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.3 2006/02/10 12:35:31 bernie
20 *#* Enforce CONFIG_* definitions.
22 *#* Revision 1.2 2006/01/23 23:11:27 bernie
23 *#* Use RASTER_SIZE() to compute... err... the raster size.
25 *#* Revision 1.1 2006/01/16 03:50:57 bernie
26 *#* Import into DevLib.
32 #include <drv/timer.h>
36 #include <cfg/macros.h> /* BV() */
37 #include <cfg/debug.h>
43 /* Configuration sanity checks */
44 #if !defined(CONFIG_LCD_SOFTINT_REFRESH) || (CONFIG_LCD_SOFTINT_REFRESH != 0 && CONFIG_LCD_SOFTINT_REFRESH != 1)
45 #error CONFIG_LCD_SOFTINT_REFRESH must be defined to either 0 or 1
47 #if !defined(CONFIG_LCD_SOFTINT_REFRESH) || (CONFIG_LCD_SOFTINT_REFRESH != 0 && CONFIG_LCD_SOFTINT_REFRESH != 1)
48 #error CONFIG_LCD_SOFTINT_REFRESH must be defined to either 0 or 1
52 #if CONFIG_LCD_SOFTINT_REFRESH
54 /*! Interval between softint driven lcd refresh */
55 # define LCD_REFRESH_INTERVAL 20 /* 20ms -> 50fps */
57 #endif /* CONFIG_LCD_SOFTINT_REFRESH */
59 /*! Number of LCD pages */
62 /*! Width of an LCD page */
63 #define LCD_PAGESIZE (LCD_WIDTH / 2)
66 * \name LCD I/O pins/ports
69 #define LCD_PF_DB0 PF4
70 #define LCD_PF_DB1 PF5
71 #define LCD_PF_DB2 PF6
72 #define LCD_PF_DB3 PF7
73 #define LCD_PD_DB4 PD4
74 #define LCD_PD_DB5 PD5
75 #define LCD_PD_DB6 PD6
76 #define LCD_PD_DB7 PD7
84 * \name DB high nibble (DB[4-7])
87 #define LCD_DATA_HI_PORT PORTD
88 #define LCD_DATA_HI_PIN PIND
89 #define LCD_DATA_HI_DDR DDRD
90 #define LCD_DATA_HI_SHIFT 0
91 #define LCD_DATA_HI_MASK 0xF0
95 * \name DB low nibble (DB[0-3])
98 #define LCD_DATA_LO_PORT PORTF
99 #define LCD_DATA_LO_PIN PINF
100 #define LCD_DATA_LO_DDR DDRF
101 #define LCD_DATA_LO_SHIFT 4
102 #define LCD_DATA_LO_MASK 0xF0
106 * \name LCD bus control macros
109 #define LCD_CLR_A0 (PORTB &= ~BV(LCD_PB_A0))
110 #define LCD_SET_A0 (PORTB |= BV(LCD_PB_A0))
111 #define LCD_CLR_RD (PORTE &= ~BV(LCD_PE_RW))
112 #define LCD_SET_RD (PORTE |= BV(LCD_PE_RW))
113 #define LCD_CLR_E1 (PORTE &= ~BV(LCD_PE_E1))
114 #define LCD_SET_E1 (PORTE |= BV(LCD_PE_E1))
115 #define LCD_CLR_E2 (PORTE &= ~BV(LCD_PE_E2))
116 #define LCD_SET_E2 (PORTE |= BV(LCD_PE_E2))
117 #define LCD_SET_E(x) (PORTE |= (x))
118 #define LCD_CLR_E(x) (PORTE &= ~(x))
122 * \name Chip select bits for LCD_SET_E()
125 #define LCDF_E1 (BV(LCD_PE_E1))
126 #define LCDF_E2 (BV(LCD_PE_E2))
129 /*! Read from the LCD data bus (DB[0-7]) */
131 ((LCD_DATA_LO_PIN & LCD_DATA_LO_MASK) >> LCD_DATA_LO_SHIFT) | \
132 ((LCD_DATA_HI_PIN & LCD_DATA_HI_MASK) >> LCD_DATA_HI_SHIFT) \
135 /*! Write to the LCD data bus (DB[0-7]) */
136 #define LCD_WRITE(d) \
138 LCD_DATA_LO_PORT = (LCD_DATA_LO_PORT & ~LCD_DATA_LO_MASK) | (((d)<<LCD_DATA_LO_SHIFT) & LCD_DATA_LO_MASK); \
139 LCD_DATA_HI_PORT = (LCD_DATA_HI_PORT & ~LCD_DATA_HI_MASK) | (((d)<<LCD_DATA_HI_SHIFT) & LCD_DATA_HI_MASK); \
142 /*! Set data bus direction to output (write to display) */
145 LCD_DATA_LO_DDR |= LCD_DATA_LO_MASK; \
146 LCD_DATA_HI_DDR |= LCD_DATA_HI_MASK; \
149 /*! Set data bus direction to input (read from display) */
152 LCD_DATA_LO_DDR &= ~LCD_DATA_LO_MASK; \
153 LCD_DATA_HI_DDR &= ~LCD_DATA_HI_MASK; \
156 /*! Delay for tEW (160ns) */
157 #define LCD_DELAY_WRITE \
163 /*! Delay for tACC6 (180ns) */
164 #define LCD_DELAY_READ \
173 * \name 32122A Commands
176 #define LCD_CMD_DISPLAY_ON 0xAF
177 #define LCD_CMD_DISPLAY_OFF 0xAE
178 #define LCD_CMD_STARTLINE 0xC0
179 #define LCD_CMD_PAGEADDR 0xB8
180 #define LCD_CMD_COLADDR 0x00
181 #define LCD_CMD_ADC_LEFT 0xA1
182 #define LCD_CMD_ADC_RIGHT 0xA0
183 #define LCD_CMD_STATIC_OFF 0xA4
184 #define LCD_CMD_STATIC_ON 0xA5
185 #define LCD_CMD_DUTY_32 0xA9
186 #define LCD_CMD_DUTY_16 0xA8
187 #define LCD_CMD_RMW_ON 0xE0
188 #define LCD_CMD_RMW_OFF 0xEE
189 #define LCD_CMD_RESET 0xE2
193 #define LCDF_BUSY BV(7)
199 * RS __\____________/__
222 } while (status & LCDF_BUSY); \
226 #else /* CONFIG_LCD_WAIT */
228 #define WAIT_LCD do {} while(0)
230 #endif /* CONFIG_LCD_WAIT */
234 * Raster buffer to draw into.
236 * Bits in the bitmap bytes have vertical orientation,
237 * as required by the LCD driver.
239 DECLARE_WALL(wall_before_raster, WALL_SIZE)
240 static uint8_t lcd_raster[RASTER_SIZE(LCD_WIDTH, LCD_HEIGHT)];
241 DECLARE_WALL(wall_after_raster, WALL_SIZE)
243 /*! Default LCD bitmap */
244 struct Bitmap lcd_bitmap;
247 #if CONFIG_LCD_SOFTINT_REFRESH
249 /*! Timer for regular LCD refresh */
250 static Timer *lcd_refresh_timer;
252 #endif /* CONFIG_LCD_SOFTINT_REFRESH */
256 static bool lcd_check(void)
259 uint16_t retries = 32768;
263 cbi(PORTC, PCB_LCD_RS);
264 sbi(PORTC, PCB_LCD_RW);
265 sbi(PORTC, PCB_LCD_E);
269 cbi(PORTC, PCB_LCD_E);
270 cbi(PORTC, PCB_LCD_RW);
271 } while ((status & LCDF_BUSY) && retries);
273 return (retries != 0);
278 static inline void lcd_cmd(uint8_t cmd, uint8_t chip)
283 * A0 __\____________/__
285 * R/W __________________
289 * DATA --<============>--
302 static inline uint8_t lcd_read(uint8_t chip)
317 * DATA -------<=====>----
336 static inline void lcd_write(uint8_t c, uint8_t chip)
345 * R/W __________________
349 * DATA -<==============>-
365 * Set LCD contrast PWM.
367 void lcd_setpwm(int duty)
369 ASSERT(duty >= LCD_MIN_PWM);
370 ASSERT(duty <= LCD_MAX_PWM);
376 static void lcd_clear(void)
380 for (page = 0; page < LCD_PAGES; ++page)
382 lcd_cmd(LCD_CMD_COLADDR | 0, LCDF_E1 | LCDF_E2);
383 lcd_cmd(LCD_CMD_PAGEADDR | page, LCDF_E1 | LCDF_E2);
384 for (j = 0; j < LCD_PAGESIZE; j++)
385 lcd_write(0, LCDF_E1 | LCDF_E2);
390 static void lcd_writeraster(const uint8_t *raster)
393 const uint8_t *right_raster;
395 CHECK_WALL(wall_before_raster);
396 CHECK_WALL(wall_after_raster);
398 for (page = 0; page < LCD_PAGES; ++page)
400 lcd_cmd(LCD_CMD_PAGEADDR | page, LCDF_E1 | LCDF_E2);
401 lcd_cmd(LCD_CMD_COLADDR | 0, LCDF_E1 | LCDF_E2);
403 /* Super optimized lamer loop */
404 right_raster = raster + LCD_PAGESIZE;
408 lcd_write(*raster++, LCDF_E1);
409 lcd_write(*right_raster++, LCDF_E2);
412 raster = right_raster;
417 void lcd_blit_bitmap(Bitmap *bm)
419 lcd_writeraster(bm->raster);
423 #if CONFIG_LCD_SOFTINT_REFRESH
425 static void lcd_refresh_softint(void)
427 lcd_blit_bitmap(&lcd_bitmap);
428 timer_add(lcd_refresh_timer);
431 #endif /* CONFIG_LCD_SOFTINT_REFRESH */
435 * Initialize LCD subsystem.
437 * \note The PWM used for LCD contrast is initialized in drv/pwm.c
438 * because it is the same PWM used for output attenuation.
442 // FIXME: interrupts are already disabled when we get here?!?
444 IRQ_SAVE_DISABLE(flags);
446 PORTB |= BV(LCD_PB_A0);
447 DDRB |= BV(LCD_PB_A0);
449 PORTE &= ~(BV(LCD_PE_RW) | BV(LCD_PE_E1) | BV(LCD_PE_E2));
450 DDRE |= BV(LCD_PE_RW) | BV(LCD_PE_E1) | BV(LCD_PE_E2);
453 LCD_RESET_PORT |= BV(LCD_RESET_BIT);
454 LCD_RESET_DDR |= BV(LCD_RESET_BIT);
457 LCD_RESET_PORT &= ~BV(LCD_RESET_BIT);
460 LCD_RESET_PORT |= BV(LCD_RESET_BIT);
463 * Data bus is in output state most of the time:
464 * LCD r/w functions assume it is left in output state
468 // Wait for RST line to stabilize at Vcc.
471 IRQ_SAVE_DISABLE(flags);
473 lcd_cmd(LCD_CMD_RESET, LCDF_E1 | LCDF_E2);
474 lcd_cmd(LCD_CMD_DISPLAY_ON, LCDF_E1 | LCDF_E2);
475 lcd_cmd(LCD_CMD_STARTLINE | 0, LCDF_E1 | LCDF_E2);
477 /* Initialize anti-corruption walls for raster */
478 INIT_WALL(wall_before_raster);
479 INIT_WALL(wall_after_raster);
484 lcd_setpwm(LCD_DEF_PWM);
486 gfx_bitmapInit(&lcd_bitmap, lcd_raster, LCD_WIDTH, LCD_HEIGHT);
487 gfx_bitmapClear(&lcd_bitmap);
489 #if CONFIG_LCD_SOFTINT_REFRESH
491 /* Init IRQ driven LCD refresh */
492 lcd_refresh_timer = timer_new();
493 ASSERT(lcd_refresh_timer != NULL);
494 INITEVENT_INT(&lcd_refresh_timer->expire, (Hook)lcd_refresh_softint, 0);
495 lcd_refresh_timer->delay = LCD_REFRESH_INTERVAL;
496 timer_add(lcd_refresh_timer);
498 #endif /* CONFIG_LCD_SOFTINT_REFRESH */