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.4 2006/04/27 05:40:11 bernie
20 *#* Naming convention fixes; Partial merge from project_grl.
22 *#* Revision 1.3 2006/02/10 12:35:31 bernie
23 *#* Enforce CONFIG_* definitions.
25 *#* Revision 1.2 2006/01/23 23:11:27 bernie
26 *#* Use RASTER_SIZE() to compute... err... the raster size.
28 *#* Revision 1.1 2006/01/16 03:50:57 bernie
29 *#* Import into DevLib.
35 #include <drv/timer.h>
39 #include <cfg/macros.h> /* BV() */
40 #include <cfg/debug.h>
46 /* Configuration sanity checks */
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
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
55 #if CONFIG_LCD_SOFTINT_REFRESH
57 /*! Interval between softint driven lcd refresh */
58 # define LCD_REFRESH_INTERVAL 20 /* 20ms -> 50fps */
60 #endif /* CONFIG_LCD_SOFTINT_REFRESH */
62 /*! Number of LCD pages */
65 /*! Width of an LCD page */
66 #define LCD_PAGESIZE (LCD_WIDTH / 2)
69 * \name LCD I/O pins/ports
72 #define LCD_PF_DB0 PF4
73 #define LCD_PF_DB1 PF5
74 #define LCD_PF_DB2 PF6
75 #define LCD_PF_DB3 PF7
76 #define LCD_PD_DB4 PD4
77 #define LCD_PD_DB5 PD5
78 #define LCD_PD_DB6 PD6
79 #define LCD_PD_DB7 PD7
87 * \name DB high nibble (DB[4-7])
90 #define LCD_DATA_HI_PORT PORTD
91 #define LCD_DATA_HI_PIN PIND
92 #define LCD_DATA_HI_DDR DDRD
93 #define LCD_DATA_HI_SHIFT 0
94 #define LCD_DATA_HI_MASK 0xF0
98 * \name DB low nibble (DB[0-3])
101 #define LCD_DATA_LO_PORT PORTF
102 #define LCD_DATA_LO_PIN PINF
103 #define LCD_DATA_LO_DDR DDRF
104 #define LCD_DATA_LO_SHIFT 4
105 #define LCD_DATA_LO_MASK 0xF0
109 * \name LCD bus control macros
112 #define LCD_CLR_A0 (PORTB &= ~BV(LCD_PB_A0))
113 #define LCD_SET_A0 (PORTB |= BV(LCD_PB_A0))
114 #define LCD_CLR_RD (PORTE &= ~BV(LCD_PE_RW))
115 #define LCD_SET_RD (PORTE |= BV(LCD_PE_RW))
116 #define LCD_CLR_E1 (PORTE &= ~BV(LCD_PE_E1))
117 #define LCD_SET_E1 (PORTE |= BV(LCD_PE_E1))
118 #define LCD_CLR_E2 (PORTE &= ~BV(LCD_PE_E2))
119 #define LCD_SET_E2 (PORTE |= BV(LCD_PE_E2))
120 #define LCD_SET_E(x) (PORTE |= (x))
121 #define LCD_CLR_E(x) (PORTE &= ~(x))
125 * \name Chip select bits for LCD_SET_E()
128 #define LCDF_E1 (BV(LCD_PE_E1))
129 #define LCDF_E2 (BV(LCD_PE_E2))
132 /*! Read from the LCD data bus (DB[0-7]) */
134 ((LCD_DATA_LO_PIN & LCD_DATA_LO_MASK) >> LCD_DATA_LO_SHIFT) | \
135 ((LCD_DATA_HI_PIN & LCD_DATA_HI_MASK) >> LCD_DATA_HI_SHIFT) \
138 /*! Write to the LCD data bus (DB[0-7]) */
139 #define LCD_WRITE(d) \
141 LCD_DATA_LO_PORT = (LCD_DATA_LO_PORT & ~LCD_DATA_LO_MASK) | (((d)<<LCD_DATA_LO_SHIFT) & LCD_DATA_LO_MASK); \
142 LCD_DATA_HI_PORT = (LCD_DATA_HI_PORT & ~LCD_DATA_HI_MASK) | (((d)<<LCD_DATA_HI_SHIFT) & LCD_DATA_HI_MASK); \
145 /*! Set data bus direction to output (write to display) */
148 LCD_DATA_LO_DDR |= LCD_DATA_LO_MASK; \
149 LCD_DATA_HI_DDR |= LCD_DATA_HI_MASK; \
152 /*! Set data bus direction to input (read from display) */
155 LCD_DATA_LO_DDR &= ~LCD_DATA_LO_MASK; \
156 LCD_DATA_HI_DDR &= ~LCD_DATA_HI_MASK; \
159 /*! Delay for tEW (160ns) */
160 #define LCD_DELAY_WRITE \
166 /*! Delay for tACC6 (180ns) */
167 #define LCD_DELAY_READ \
176 * \name 32122A Commands
179 #define LCD_CMD_DISPLAY_ON 0xAF
180 #define LCD_CMD_DISPLAY_OFF 0xAE
181 #define LCD_CMD_STARTLINE 0xC0
182 #define LCD_CMD_PAGEADDR 0xB8
183 #define LCD_CMD_COLADDR 0x00
184 #define LCD_CMD_ADC_LEFT 0xA1
185 #define LCD_CMD_ADC_RIGHT 0xA0
186 #define LCD_CMD_STATIC_OFF 0xA4
187 #define LCD_CMD_STATIC_ON 0xA5
188 #define LCD_CMD_DUTY_32 0xA9
189 #define LCD_CMD_DUTY_16 0xA8
190 #define LCD_CMD_RMW_ON 0xE0
191 #define LCD_CMD_RMW_OFF 0xEE
192 #define LCD_CMD_RESET 0xE2
199 #define LCDF_BUSY BV(7)
205 * RS __\____________/__
228 } while (status & LCDF_BUSY); \
232 #else /* CONFIG_LCD_WAIT */
234 #define WAIT_LCD do {} while(0)
236 #endif /* CONFIG_LCD_WAIT */
240 * Raster buffer to draw into.
242 * Bits in the bitmap bytes have vertical orientation,
243 * as required by the LCD driver.
245 DECLARE_WALL(wall_before_raster, WALL_SIZE)
246 static uint8_t lcd_raster[RASTER_SIZE(LCD_WIDTH, LCD_HEIGHT)];
247 DECLARE_WALL(wall_after_raster, WALL_SIZE)
249 /*! Default LCD bitmap */
250 struct Bitmap lcd_bitmap;
253 #if CONFIG_LCD_SOFTINT_REFRESH
255 /*! Timer for regular LCD refresh */
256 static Timer *lcd_refresh_timer;
258 #endif /* CONFIG_LCD_SOFTINT_REFRESH */
262 static bool lcd_check(void)
265 uint16_t retries = 32768;
269 cbi(PORTC, PCB_LCD_RS);
270 sbi(PORTC, PCB_LCD_RW);
271 sbi(PORTC, PCB_LCD_E);
275 cbi(PORTC, PCB_LCD_E);
276 cbi(PORTC, PCB_LCD_RW);
277 } while ((status & LCDF_BUSY) && retries);
279 return (retries != 0);
284 static inline void lcd_cmd(uint8_t cmd, uint8_t chip)
289 * A0 __\____________/__
291 * R/W __________________
295 * DATA --<============>--
308 static inline uint8_t lcd_read(uint8_t chip)
323 * DATA -------<=====>----
342 static inline void lcd_write(uint8_t c, uint8_t chip)
351 * R/W __________________
355 * DATA -<==============>-
371 * Set LCD contrast PWM.
373 void lcd_setPwm(int duty)
375 ASSERT(duty >= LCD_MIN_PWM);
376 ASSERT(duty <= LCD_MAX_PWM);
382 static void lcd_clear(void)
386 for (page = 0; page < LCD_PAGES; ++page)
388 lcd_cmd(LCD_CMD_COLADDR | 0, LCDF_E1 | LCDF_E2);
389 lcd_cmd(LCD_CMD_PAGEADDR | page, LCDF_E1 | LCDF_E2);
390 for (j = 0; j < LCD_PAGESIZE; j++)
391 lcd_write(0, LCDF_E1 | LCDF_E2);
396 static void lcd_writeRaster(const uint8_t *raster)
399 const uint8_t *right_raster;
401 CHECK_WALL(wall_before_raster);
402 CHECK_WALL(wall_after_raster);
404 for (page = 0; page < LCD_PAGES; ++page)
406 lcd_cmd(LCD_CMD_PAGEADDR | page, LCDF_E1 | LCDF_E2);
407 lcd_cmd(LCD_CMD_COLADDR | 0, LCDF_E1 | LCDF_E2);
409 /* Super optimized lamer loop */
410 right_raster = raster + LCD_PAGESIZE;
414 lcd_write(*raster++, LCDF_E1);
415 lcd_write(*right_raster++, LCDF_E2);
418 raster = right_raster;
423 * Update the LCD display with data from the provided bitmap.
425 void lcd_blitBitmap(Bitmap *bm)
428 lcd_writeRaster(bm->raster);
432 #if CONFIG_LCD_SOFTINT_REFRESH
434 static void lcd_refreshSoftint(void)
436 lcd_blit_bitmap(&lcd_bitmap);
437 timer_add(lcd_refresh_timer);
440 #endif /* CONFIG_LCD_SOFTINT_REFRESH */
444 * Initialize LCD subsystem.
446 * \note The PWM used for LCD contrast is initialized in drv/pwm.c
447 * because it is the same PWM used for output attenuation.
453 // FIXME: interrupts are already disabled when we get here?!?
455 IRQ_SAVE_DISABLE(flags);
457 PORTB |= BV(LCD_PB_A0);
458 DDRB |= BV(LCD_PB_A0);
460 PORTE &= ~(BV(LCD_PE_RW) | BV(LCD_PE_E1) | BV(LCD_PE_E2));
461 DDRE |= BV(LCD_PE_RW) | BV(LCD_PE_E1) | BV(LCD_PE_E2);
464 LCD_RESET_PORT |= BV(LCD_RESET_BIT);
465 LCD_RESET_DDR |= BV(LCD_RESET_BIT);
468 LCD_RESET_PORT &= ~BV(LCD_RESET_BIT);
471 LCD_RESET_PORT |= BV(LCD_RESET_BIT);
474 * Data bus is in output state most of the time:
475 * LCD r/w functions assume it is left in output state
479 // Wait for RST line to stabilize at Vcc.
482 IRQ_SAVE_DISABLE(flags);
484 lcd_cmd(LCD_CMD_RESET, LCDF_E1 | LCDF_E2);
485 lcd_cmd(LCD_CMD_DISPLAY_ON, LCDF_E1 | LCDF_E2);
486 lcd_cmd(LCD_CMD_STARTLINE | 0, LCDF_E1 | LCDF_E2);
488 /* Initialize anti-corruption walls for raster */
489 INIT_WALL(wall_before_raster);
490 INIT_WALL(wall_after_raster);
495 lcd_setpwm(LCD_DEF_PWM);
497 gfx_bitmapInit(&lcd_bitmap, lcd_raster, LCD_WIDTH, LCD_HEIGHT);
498 gfx_bitmapClear(&lcd_bitmap);
500 #if CONFIG_LCD_SOFTINT_REFRESH
502 /* Init IRQ driven LCD refresh */
503 lcd_refresh_timer = timer_new();
504 ASSERT(lcd_refresh_timer != NULL);
505 INITEVENT_INT(&lcd_refresh_timer->expire, (Hook)lcd_refresh_softint, 0);
506 lcd_refresh_timer->delay = LCD_REFRESH_INTERVAL;
507 timer_add(lcd_refresh_timer);
509 #endif /* CONFIG_LCD_SOFTINT_REFRESH */