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.5 2006/07/19 12:56:25 bernie
20 *#* Convert to new Doxygen style.
22 *#* Revision 1.4 2006/04/27 05:40:11 bernie
23 *#* Naming convention fixes; Partial merge from project_grl.
25 *#* Revision 1.3 2006/02/10 12:35:31 bernie
26 *#* Enforce CONFIG_* definitions.
28 *#* Revision 1.2 2006/01/23 23:11:27 bernie
29 *#* Use RASTER_SIZE() to compute... err... the raster size.
31 *#* Revision 1.1 2006/01/16 03:50:57 bernie
32 *#* Import into DevLib.
38 #include <drv/timer.h>
42 #include <cfg/macros.h> /* BV() */
43 #include <cfg/debug.h>
49 /* Configuration sanity checks */
50 #if !defined(CONFIG_LCD_SOFTINT_REFRESH) || (CONFIG_LCD_SOFTINT_REFRESH != 0 && CONFIG_LCD_SOFTINT_REFRESH != 1)
51 #error CONFIG_LCD_SOFTINT_REFRESH must be defined to either 0 or 1
53 #if !defined(CONFIG_LCD_SOFTINT_REFRESH) || (CONFIG_LCD_SOFTINT_REFRESH != 0 && CONFIG_LCD_SOFTINT_REFRESH != 1)
54 #error CONFIG_LCD_SOFTINT_REFRESH must be defined to either 0 or 1
58 #if CONFIG_LCD_SOFTINT_REFRESH
60 /** Interval between softint driven lcd refresh */
61 # define LCD_REFRESH_INTERVAL 20 /* 20ms -> 50fps */
63 #endif /* CONFIG_LCD_SOFTINT_REFRESH */
65 /** Number of LCD pages */
68 /** Width of an LCD page */
69 #define LCD_PAGESIZE (LCD_WIDTH / 2)
72 * \name LCD I/O pins/ports
75 #define LCD_PF_DB0 PF4
76 #define LCD_PF_DB1 PF5
77 #define LCD_PF_DB2 PF6
78 #define LCD_PF_DB3 PF7
79 #define LCD_PD_DB4 PD4
80 #define LCD_PD_DB5 PD5
81 #define LCD_PD_DB6 PD6
82 #define LCD_PD_DB7 PD7
90 * \name DB high nibble (DB[4-7])
93 #define LCD_DATA_HI_PORT PORTD
94 #define LCD_DATA_HI_PIN PIND
95 #define LCD_DATA_HI_DDR DDRD
96 #define LCD_DATA_HI_SHIFT 0
97 #define LCD_DATA_HI_MASK 0xF0
101 * \name DB low nibble (DB[0-3])
104 #define LCD_DATA_LO_PORT PORTF
105 #define LCD_DATA_LO_PIN PINF
106 #define LCD_DATA_LO_DDR DDRF
107 #define LCD_DATA_LO_SHIFT 4
108 #define LCD_DATA_LO_MASK 0xF0
112 * \name LCD bus control macros
115 #define LCD_CLR_A0 (PORTB &= ~BV(LCD_PB_A0))
116 #define LCD_SET_A0 (PORTB |= BV(LCD_PB_A0))
117 #define LCD_CLR_RD (PORTE &= ~BV(LCD_PE_RW))
118 #define LCD_SET_RD (PORTE |= BV(LCD_PE_RW))
119 #define LCD_CLR_E1 (PORTE &= ~BV(LCD_PE_E1))
120 #define LCD_SET_E1 (PORTE |= BV(LCD_PE_E1))
121 #define LCD_CLR_E2 (PORTE &= ~BV(LCD_PE_E2))
122 #define LCD_SET_E2 (PORTE |= BV(LCD_PE_E2))
123 #define LCD_SET_E(x) (PORTE |= (x))
124 #define LCD_CLR_E(x) (PORTE &= ~(x))
128 * \name Chip select bits for LCD_SET_E()
131 #define LCDF_E1 (BV(LCD_PE_E1))
132 #define LCDF_E2 (BV(LCD_PE_E2))
135 /** Read from the LCD data bus (DB[0-7]) */
137 ((LCD_DATA_LO_PIN & LCD_DATA_LO_MASK) >> LCD_DATA_LO_SHIFT) | \
138 ((LCD_DATA_HI_PIN & LCD_DATA_HI_MASK) >> LCD_DATA_HI_SHIFT) \
141 /** Write to the LCD data bus (DB[0-7]) */
142 #define LCD_WRITE(d) \
144 LCD_DATA_LO_PORT = (LCD_DATA_LO_PORT & ~LCD_DATA_LO_MASK) | (((d)<<LCD_DATA_LO_SHIFT) & LCD_DATA_LO_MASK); \
145 LCD_DATA_HI_PORT = (LCD_DATA_HI_PORT & ~LCD_DATA_HI_MASK) | (((d)<<LCD_DATA_HI_SHIFT) & LCD_DATA_HI_MASK); \
148 /** Set data bus direction to output (write to display) */
151 LCD_DATA_LO_DDR |= LCD_DATA_LO_MASK; \
152 LCD_DATA_HI_DDR |= LCD_DATA_HI_MASK; \
155 /** Set data bus direction to input (read from display) */
158 LCD_DATA_LO_DDR &= ~LCD_DATA_LO_MASK; \
159 LCD_DATA_HI_DDR &= ~LCD_DATA_HI_MASK; \
162 /** Delay for tEW (160ns) */
163 #define LCD_DELAY_WRITE \
169 /** Delay for tACC6 (180ns) */
170 #define LCD_DELAY_READ \
179 * \name 32122A Commands
182 #define LCD_CMD_DISPLAY_ON 0xAF
183 #define LCD_CMD_DISPLAY_OFF 0xAE
184 #define LCD_CMD_STARTLINE 0xC0
185 #define LCD_CMD_PAGEADDR 0xB8
186 #define LCD_CMD_COLADDR 0x00
187 #define LCD_CMD_ADC_LEFT 0xA1
188 #define LCD_CMD_ADC_RIGHT 0xA0
189 #define LCD_CMD_STATIC_OFF 0xA4
190 #define LCD_CMD_STATIC_ON 0xA5
191 #define LCD_CMD_DUTY_32 0xA9
192 #define LCD_CMD_DUTY_16 0xA8
193 #define LCD_CMD_RMW_ON 0xE0
194 #define LCD_CMD_RMW_OFF 0xEE
195 #define LCD_CMD_RESET 0xE2
202 #define LCDF_BUSY BV(7)
208 * RS __\____________/__
231 } while (status & LCDF_BUSY); \
235 #else /* CONFIG_LCD_WAIT */
237 #define WAIT_LCD do {} while(0)
239 #endif /* CONFIG_LCD_WAIT */
243 * Raster buffer to draw into.
245 * Bits in the bitmap bytes have vertical orientation,
246 * as required by the LCD driver.
248 DECLARE_WALL(wall_before_raster, WALL_SIZE)
249 static uint8_t lcd_raster[RASTER_SIZE(LCD_WIDTH, LCD_HEIGHT)];
250 DECLARE_WALL(wall_after_raster, WALL_SIZE)
252 /** Default LCD bitmap */
253 struct Bitmap lcd_bitmap;
256 #if CONFIG_LCD_SOFTINT_REFRESH
258 /** Timer for regular LCD refresh */
259 static Timer *lcd_refresh_timer;
261 #endif /* CONFIG_LCD_SOFTINT_REFRESH */
265 static bool lcd_check(void)
268 uint16_t retries = 32768;
272 cbi(PORTC, PCB_LCD_RS);
273 sbi(PORTC, PCB_LCD_RW);
274 sbi(PORTC, PCB_LCD_E);
278 cbi(PORTC, PCB_LCD_E);
279 cbi(PORTC, PCB_LCD_RW);
280 } while ((status & LCDF_BUSY) && retries);
282 return (retries != 0);
287 static inline void lcd_cmd(uint8_t cmd, uint8_t chip)
292 * A0 __\____________/__
294 * R/W __________________
298 * DATA --<============>--
311 static inline uint8_t lcd_read(uint8_t chip)
326 * DATA -------<=====>----
345 static inline void lcd_write(uint8_t c, uint8_t chip)
354 * R/W __________________
358 * DATA -<==============>-
374 * Set LCD contrast PWM.
376 void lcd_setPwm(int duty)
378 ASSERT(duty >= LCD_MIN_PWM);
379 ASSERT(duty <= LCD_MAX_PWM);
385 static void lcd_clear(void)
389 for (page = 0; page < LCD_PAGES; ++page)
391 lcd_cmd(LCD_CMD_COLADDR | 0, LCDF_E1 | LCDF_E2);
392 lcd_cmd(LCD_CMD_PAGEADDR | page, LCDF_E1 | LCDF_E2);
393 for (j = 0; j < LCD_PAGESIZE; j++)
394 lcd_write(0, LCDF_E1 | LCDF_E2);
399 static void lcd_writeRaster(const uint8_t *raster)
402 const uint8_t *right_raster;
404 CHECK_WALL(wall_before_raster);
405 CHECK_WALL(wall_after_raster);
407 for (page = 0; page < LCD_PAGES; ++page)
409 lcd_cmd(LCD_CMD_PAGEADDR | page, LCDF_E1 | LCDF_E2);
410 lcd_cmd(LCD_CMD_COLADDR | 0, LCDF_E1 | LCDF_E2);
412 /* Super optimized lamer loop */
413 right_raster = raster + LCD_PAGESIZE;
417 lcd_write(*raster++, LCDF_E1);
418 lcd_write(*right_raster++, LCDF_E2);
421 raster = right_raster;
426 * Update the LCD display with data from the provided bitmap.
428 void lcd_blitBitmap(Bitmap *bm)
431 lcd_writeRaster(bm->raster);
435 #if CONFIG_LCD_SOFTINT_REFRESH
437 static void lcd_refreshSoftint(void)
439 lcd_blit_bitmap(&lcd_bitmap);
440 timer_add(lcd_refresh_timer);
443 #endif /* CONFIG_LCD_SOFTINT_REFRESH */
447 * Initialize LCD subsystem.
449 * \note The PWM used for LCD contrast is initialized in drv/pwm.c
450 * because it is the same PWM used for output attenuation.
456 // FIXME: interrupts are already disabled when we get here?!?
458 IRQ_SAVE_DISABLE(flags);
460 PORTB |= BV(LCD_PB_A0);
461 DDRB |= BV(LCD_PB_A0);
463 PORTE &= ~(BV(LCD_PE_RW) | BV(LCD_PE_E1) | BV(LCD_PE_E2));
464 DDRE |= BV(LCD_PE_RW) | BV(LCD_PE_E1) | BV(LCD_PE_E2);
467 LCD_RESET_PORT |= BV(LCD_RESET_BIT);
468 LCD_RESET_DDR |= BV(LCD_RESET_BIT);
471 LCD_RESET_PORT &= ~BV(LCD_RESET_BIT);
474 LCD_RESET_PORT |= BV(LCD_RESET_BIT);
477 * Data bus is in output state most of the time:
478 * LCD r/w functions assume it is left in output state
482 // Wait for RST line to stabilize at Vcc.
485 IRQ_SAVE_DISABLE(flags);
487 lcd_cmd(LCD_CMD_RESET, LCDF_E1 | LCDF_E2);
488 lcd_cmd(LCD_CMD_DISPLAY_ON, LCDF_E1 | LCDF_E2);
489 lcd_cmd(LCD_CMD_STARTLINE | 0, LCDF_E1 | LCDF_E2);
491 /* Initialize anti-corruption walls for raster */
492 INIT_WALL(wall_before_raster);
493 INIT_WALL(wall_after_raster);
498 lcd_setpwm(LCD_DEF_PWM);
500 gfx_bitmapInit(&lcd_bitmap, lcd_raster, LCD_WIDTH, LCD_HEIGHT);
501 gfx_bitmapClear(&lcd_bitmap);
503 #if CONFIG_LCD_SOFTINT_REFRESH
505 /* Init IRQ driven LCD refresh */
506 lcd_refresh_timer = timer_new();
507 ASSERT(lcd_refresh_timer != NULL);
508 INITEVENT_INT(&lcd_refresh_timer->expire, (Hook)lcd_refresh_softint, 0);
509 lcd_refresh_timer->delay = LCD_REFRESH_INTERVAL;
510 timer_add(lcd_refresh_timer);
512 #endif /* CONFIG_LCD_SOFTINT_REFRESH */