4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2003, 2004, 2005, 2006 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 2001 Bernardo Innocenti <bernie@codewiz.org>
36 * \author Bernardo Innocenti <bernie@develer.com>
37 * \author Stefano Fedrigo <aleph@develer.com>
39 * \brief Displaytech 32122A LCD driver
44 *#* Revision 1.5 2006/07/19 12:56:25 bernie
45 *#* Convert to new Doxygen style.
47 *#* Revision 1.4 2006/04/27 05:40:11 bernie
48 *#* Naming convention fixes; Partial merge from project_grl.
50 *#* Revision 1.3 2006/02/10 12:35:31 bernie
51 *#* Enforce CONFIG_* definitions.
53 *#* Revision 1.2 2006/01/23 23:11:27 bernie
54 *#* Use RASTER_SIZE() to compute... err... the raster size.
56 *#* Revision 1.1 2006/01/16 03:50:57 bernie
57 *#* Import into DevLib.
61 #include "lcd_32122a_avr.h"
63 #include <drv/timer.h>
67 #include <cfg/macros.h> /* BV() */
68 #include <cfg/debug.h>
74 /* Configuration sanity checks */
75 #if !defined(CONFIG_LCD_SOFTINT_REFRESH) || (CONFIG_LCD_SOFTINT_REFRESH != 0 && CONFIG_LCD_SOFTINT_REFRESH != 1)
76 #error CONFIG_LCD_SOFTINT_REFRESH must be defined to either 0 or 1
78 #if !defined(CONFIG_LCD_SOFTINT_REFRESH) || (CONFIG_LCD_SOFTINT_REFRESH != 0 && CONFIG_LCD_SOFTINT_REFRESH != 1)
79 #error CONFIG_LCD_SOFTINT_REFRESH must be defined to either 0 or 1
83 #if CONFIG_LCD_SOFTINT_REFRESH
85 /** Interval between softint driven lcd refresh */
86 # define LCD_REFRESH_INTERVAL 20 /* 20ms -> 50fps */
88 #endif /* CONFIG_LCD_SOFTINT_REFRESH */
90 /** Number of LCD pages */
93 /** Width of an LCD page */
94 #define LCD_PAGESIZE (LCD_WIDTH / 2)
97 * \name LCD I/O pins/ports
100 #define LCD_PF_DB0 PF4
101 #define LCD_PF_DB1 PF5
102 #define LCD_PF_DB2 PF6
103 #define LCD_PF_DB3 PF7
104 #define LCD_PD_DB4 PD4
105 #define LCD_PD_DB5 PD5
106 #define LCD_PD_DB6 PD6
107 #define LCD_PD_DB7 PD7
108 #define LCD_PB_A0 PB0
109 #define LCD_PE_RW PE7
110 #define LCD_PE_E1 PE2
111 #define LCD_PE_E2 PE6
115 * \name DB high nibble (DB[4-7])
118 #define LCD_DATA_HI_PORT PORTD
119 #define LCD_DATA_HI_PIN PIND
120 #define LCD_DATA_HI_DDR DDRD
121 #define LCD_DATA_HI_SHIFT 0
122 #define LCD_DATA_HI_MASK 0xF0
126 * \name DB low nibble (DB[0-3])
129 #define LCD_DATA_LO_PORT PORTF
130 #define LCD_DATA_LO_PIN PINF
131 #define LCD_DATA_LO_DDR DDRF
132 #define LCD_DATA_LO_SHIFT 4
133 #define LCD_DATA_LO_MASK 0xF0
137 * \name LCD bus control macros
140 #define LCD_CLR_A0 (PORTB &= ~BV(LCD_PB_A0))
141 #define LCD_SET_A0 (PORTB |= BV(LCD_PB_A0))
142 #define LCD_CLR_RD (PORTE &= ~BV(LCD_PE_RW))
143 #define LCD_SET_RD (PORTE |= BV(LCD_PE_RW))
144 #define LCD_CLR_E1 (PORTE &= ~BV(LCD_PE_E1))
145 #define LCD_SET_E1 (PORTE |= BV(LCD_PE_E1))
146 #define LCD_CLR_E2 (PORTE &= ~BV(LCD_PE_E2))
147 #define LCD_SET_E2 (PORTE |= BV(LCD_PE_E2))
148 #define LCD_SET_E(x) (PORTE |= (x))
149 #define LCD_CLR_E(x) (PORTE &= ~(x))
153 * \name Chip select bits for LCD_SET_E()
156 #define LCDF_E1 (BV(LCD_PE_E1))
157 #define LCDF_E2 (BV(LCD_PE_E2))
160 /** Read from the LCD data bus (DB[0-7]) */
162 ((LCD_DATA_LO_PIN & LCD_DATA_LO_MASK) >> LCD_DATA_LO_SHIFT) | \
163 ((LCD_DATA_HI_PIN & LCD_DATA_HI_MASK) >> LCD_DATA_HI_SHIFT) \
166 /** Write to the LCD data bus (DB[0-7]) */
167 #define LCD_WRITE(d) \
169 LCD_DATA_LO_PORT = (LCD_DATA_LO_PORT & ~LCD_DATA_LO_MASK) | (((d)<<LCD_DATA_LO_SHIFT) & LCD_DATA_LO_MASK); \
170 LCD_DATA_HI_PORT = (LCD_DATA_HI_PORT & ~LCD_DATA_HI_MASK) | (((d)<<LCD_DATA_HI_SHIFT) & LCD_DATA_HI_MASK); \
173 /** Set data bus direction to output (write to display) */
176 LCD_DATA_LO_DDR |= LCD_DATA_LO_MASK; \
177 LCD_DATA_HI_DDR |= LCD_DATA_HI_MASK; \
180 /** Set data bus direction to input (read from display) */
183 LCD_DATA_LO_DDR &= ~LCD_DATA_LO_MASK; \
184 LCD_DATA_HI_DDR &= ~LCD_DATA_HI_MASK; \
187 /** Delay for tEW (160ns) */
188 #define LCD_DELAY_WRITE \
194 /** Delay for tACC6 (180ns) */
195 #define LCD_DELAY_READ \
204 * \name 32122A Commands
207 #define LCD_CMD_DISPLAY_ON 0xAF
208 #define LCD_CMD_DISPLAY_OFF 0xAE
209 #define LCD_CMD_STARTLINE 0xC0
210 #define LCD_CMD_PAGEADDR 0xB8
211 #define LCD_CMD_COLADDR 0x00
212 #define LCD_CMD_ADC_LEFT 0xA1
213 #define LCD_CMD_ADC_RIGHT 0xA0
214 #define LCD_CMD_STATIC_OFF 0xA4
215 #define LCD_CMD_STATIC_ON 0xA5
216 #define LCD_CMD_DUTY_32 0xA9
217 #define LCD_CMD_DUTY_16 0xA8
218 #define LCD_CMD_RMW_ON 0xE0
219 #define LCD_CMD_RMW_OFF 0xEE
220 #define LCD_CMD_RESET 0xE2
227 #define LCDF_BUSY BV(7)
233 * RS __\____________/__
256 } while (status & LCDF_BUSY); \
260 #else /* CONFIG_LCD_WAIT */
262 #define WAIT_LCD do {} while(0)
264 #endif /* CONFIG_LCD_WAIT */
268 * Raster buffer to draw into.
270 * Bits in the bitmap bytes have vertical orientation,
271 * as required by the LCD driver.
273 DECLARE_WALL(wall_before_raster, WALL_SIZE)
274 static uint8_t lcd_raster[RASTER_SIZE(LCD_WIDTH, LCD_HEIGHT)];
275 DECLARE_WALL(wall_after_raster, WALL_SIZE)
277 /** Default LCD bitmap */
278 struct Bitmap lcd_bitmap;
281 #if CONFIG_LCD_SOFTINT_REFRESH
283 /** Timer for regular LCD refresh */
284 static Timer *lcd_refresh_timer;
286 #endif /* CONFIG_LCD_SOFTINT_REFRESH */
290 static bool lcd_check(void)
293 uint16_t retries = 32768;
297 cbi(PORTC, PCB_LCD_RS);
298 sbi(PORTC, PCB_LCD_RW);
299 sbi(PORTC, PCB_LCD_E);
303 cbi(PORTC, PCB_LCD_E);
304 cbi(PORTC, PCB_LCD_RW);
305 } while ((status & LCDF_BUSY) && retries);
307 return (retries != 0);
312 static inline void lcd_cmd(uint8_t cmd, uint8_t chip)
317 * A0 __\____________/__
319 * R/W __________________
323 * DATA --<============>--
336 static inline uint8_t lcd_read(uint8_t chip)
351 * DATA -------<=====>----
370 static inline void lcd_write(uint8_t c, uint8_t chip)
379 * R/W __________________
383 * DATA -<==============>-
399 * Set LCD contrast PWM.
401 void lcd_setPwm(int duty)
403 ASSERT(duty >= LCD_MIN_PWM);
404 ASSERT(duty <= LCD_MAX_PWM);
410 static void lcd_clear(void)
414 for (page = 0; page < LCD_PAGES; ++page)
416 lcd_cmd(LCD_CMD_COLADDR | 0, LCDF_E1 | LCDF_E2);
417 lcd_cmd(LCD_CMD_PAGEADDR | page, LCDF_E1 | LCDF_E2);
418 for (j = 0; j < LCD_PAGESIZE; j++)
419 lcd_write(0, LCDF_E1 | LCDF_E2);
424 static void lcd_writeRaster(const uint8_t *raster)
427 const uint8_t *right_raster;
429 CHECK_WALL(wall_before_raster);
430 CHECK_WALL(wall_after_raster);
432 for (page = 0; page < LCD_PAGES; ++page)
434 lcd_cmd(LCD_CMD_PAGEADDR | page, LCDF_E1 | LCDF_E2);
435 lcd_cmd(LCD_CMD_COLADDR | 0, LCDF_E1 | LCDF_E2);
437 /* Super optimized lamer loop */
438 right_raster = raster + LCD_PAGESIZE;
442 lcd_write(*raster++, LCDF_E1);
443 lcd_write(*right_raster++, LCDF_E2);
446 raster = right_raster;
451 * Update the LCD display with data from the provided bitmap.
453 void lcd_blitBitmap(Bitmap *bm)
456 lcd_writeRaster(bm->raster);
460 #if CONFIG_LCD_SOFTINT_REFRESH
462 static void lcd_refreshSoftint(void)
464 lcd_blit_bitmap(&lcd_bitmap);
465 timer_add(lcd_refresh_timer);
468 #endif /* CONFIG_LCD_SOFTINT_REFRESH */
472 * Initialize LCD subsystem.
474 * \note The PWM used for LCD contrast is initialized in drv/pwm.c
475 * because it is the same PWM used for output attenuation.
481 // FIXME: interrupts are already disabled when we get here?!?
483 IRQ_SAVE_DISABLE(flags);
485 PORTB |= BV(LCD_PB_A0);
486 DDRB |= BV(LCD_PB_A0);
488 PORTE &= ~(BV(LCD_PE_RW) | BV(LCD_PE_E1) | BV(LCD_PE_E2));
489 DDRE |= BV(LCD_PE_RW) | BV(LCD_PE_E1) | BV(LCD_PE_E2);
492 LCD_RESET_PORT |= BV(LCD_RESET_BIT);
493 LCD_RESET_DDR |= BV(LCD_RESET_BIT);
496 LCD_RESET_PORT &= ~BV(LCD_RESET_BIT);
499 LCD_RESET_PORT |= BV(LCD_RESET_BIT);
502 * Data bus is in output state most of the time:
503 * LCD r/w functions assume it is left in output state
507 // Wait for RST line to stabilize at Vcc.
510 IRQ_SAVE_DISABLE(flags);
512 lcd_cmd(LCD_CMD_RESET, LCDF_E1 | LCDF_E2);
513 lcd_cmd(LCD_CMD_DISPLAY_ON, LCDF_E1 | LCDF_E2);
514 lcd_cmd(LCD_CMD_STARTLINE | 0, LCDF_E1 | LCDF_E2);
516 /* Initialize anti-corruption walls for raster */
517 INIT_WALL(wall_before_raster);
518 INIT_WALL(wall_after_raster);
523 lcd_setpwm(LCD_DEF_PWM);
525 gfx_bitmapInit(&lcd_bitmap, lcd_raster, LCD_WIDTH, LCD_HEIGHT);
526 gfx_bitmapClear(&lcd_bitmap);
528 #if CONFIG_LCD_SOFTINT_REFRESH
530 /* Init IRQ driven LCD refresh */
531 lcd_refresh_timer = timer_new();
532 ASSERT(lcd_refresh_timer != NULL);
533 INITEVENT_INT(&lcd_refresh_timer->expire, (Hook)lcd_refresh_softint, 0);
534 lcd_refresh_timer->delay = LCD_REFRESH_INTERVAL;
535 timer_add(lcd_refresh_timer);
537 #endif /* CONFIG_LCD_SOFTINT_REFRESH */