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 2005 Develer S.r.l. (http://www.develer.com/)
33 * \brief LM044L type LCD hardware module (impl.)
36 * \author Bernardo Innocenti <bernie@develer.com>
37 * \author Stefano Fedrigo <aleph@develer.com>
42 #include <cfg/arch_config.h>
44 #include <drv/lcd_hd44.h>
45 #include <drv/timer.h>
47 #if defined(LCD_READ_H) && defined(LCD_READ_L) && defined(LCD_WRITE_H) && defined(LCD_WRITE_L)
48 #define CONFIG_LCD_4BIT 1
49 #elif defined(LCD_READ) && defined(LCD_WRITE)
50 #define CONFIG_LCD_4BIT 0
52 #error Incomplete or missing LCD_READ/LCD_WRITE macros
55 /** Flag di stato del display */
56 #define LCDF_BUSY BV(7)
58 #if CONFIG_LCD_ADDRESS_FAST == 1
59 #define lcd_address(x) lcd_address[x]
61 * Addresses of LCD display character positions, expanded
62 * for faster access (DB7 = 1).
64 static const uint8_t lcd_address[] =
67 0x80, 0x81, 0x82, 0x83,
68 0x84, 0x85, 0x86, 0x87,
69 0x88, 0x89, 0x8A, 0x8B,
70 0x8C, 0x8D, 0x8E, 0x8F,
72 0x90, 0x91, 0x92, 0x93,
76 0xC0, 0xC1, 0xC2, 0xC3,
77 0xC4, 0xC5, 0xC6, 0xC7,
78 0xC8, 0xC9, 0xCA, 0xCB,
79 0xCC, 0xCD, 0xCE, 0xCF,
81 0xD0, 0xD1, 0xD2, 0xD3,
86 0x94, 0x95, 0x96, 0x97,
87 0x98, 0x99, 0x9A, 0x9B,
88 0x9C, 0x9D, 0x9E, 0x9F,
89 0xA0, 0xA1, 0xA2, 0xA3,
91 0xA4, 0xA5, 0xA6, 0xA7,
95 0xD4, 0xD5, 0xD6, 0xD7,
96 0xD8, 0xD9, 0xDA, 0xDB,
97 0xDC, 0xDD, 0xDE, 0xDF,
98 0xE0, 0xE1, 0xE2, 0xE3,
100 0xE4, 0xE5, 0xE6, 0xE7,
103 #endif /* LCD_ROWS > 2 */
106 STATIC_ASSERT(countof(lcd_address) == LCD_ROWS * LCD_COLS);
107 #else /* CONFIG_LCD_ADDRESS_FAST == 0 */
109 static const uint8_t col_address[] =
118 STATIC_ASSERT(countof(col_address) == LCD_ROWS);
120 * Addresses of LCD display character positions, calculated runtime to save RAM
122 static uint8_t lcd_address(uint8_t addr)
124 return col_address[addr / LCD_COLS] + addr % LCD_COLS;
126 #endif /* CONFIG_LCD_ADDRESS_FAST */
129 * Current display position. We remember this to optimize
130 * LCD output by avoiding to set the address every time.
132 static lcdpos_t lcd_current_addr;
135 #if !defined(ARCH_EMUL) || !(ARCH & ARCH_EMUL)
136 /* __________________
139 * R/W __________________
143 * DATA -<================
145 INLINE void lcd_dataWrite(uint8_t data)
148 /* Write high nibble */
155 /* Write low nibble */
162 #else /* !CONFIG_LCD_4BIT */
171 #endif /* !CONFIG_LCD_4BIT */
174 /* __________________
183 INLINE uint8_t lcd_dataRead(void)
188 LCD_DB_IN; /* Set bus as input! */
193 /* Read high nibble */
200 /* Read low nibble */
207 #else /* !CONFIG_LCD_4BIT */
216 #endif /* !CONFIG_LCD_4BIT */
219 LCD_DB_OUT; /* Reset bus as output! */
227 * READ __________________
231 * DATA --<===============
233 INLINE void lcd_regWrite(uint8_t data)
249 INLINE uint8_t lcd_regRead(void)
254 data = lcd_dataRead();
261 INLINE void lcd_mode4Bit(void)
265 LCD_WRITE_H(LCD_CMD_SETFUNC);
274 #endif /* CONFIG_LCD_4BIT */
276 #else /* ARCH_EMUL */
278 extern void Emul_LCDWriteReg(uint8_t d);
279 extern uint8_t Emul_LCDReadReg(void);
280 extern void Emul_LCDWriteData(uint8_t d);
281 extern uint8_t Emul_LCDReadData(void);
283 #define lcd_regWrite(d) Emul_LCDWriteReg(d)
284 #define lcd_regRead(d) Emul_LCDReadReg()
285 #define lcd_dataWrite(d) Emul_LCDWriteData(d)
286 #define lcd_dataRead(d) Emul_LCDReadData()
288 #endif /* ARCH_EMUL */
292 * Wait until the LCD busy flag clears.
294 void lcd_waitBusy(void)
298 uint8_t val = lcd_regRead();
299 if (!(val & LCDF_BUSY))
306 * Move the cursor to \a addr, only if not already there.
308 void lcd_moveTo(uint8_t addr)
310 if (addr != lcd_current_addr)
313 lcd_regWrite(lcd_address(addr));
314 lcd_current_addr = addr;
320 * Write a value in LCD data register, waiting for the busy flag.
322 void lcd_setReg(uint8_t val)
328 #include <cfg/debug.h>
330 * Write the character \a c on display address \a addr.
332 * NOTE: argh, the HD44 lcd type is a bad beast: our
333 * move/write -> write optimization requires this mess
334 * because display lines are interleaved!
336 void lcd_putc(uint8_t addr, uint8_t c)
338 if (addr != lcd_current_addr)
339 lcd_setReg(lcd_address(addr));
343 lcd_current_addr = addr + 1;
345 /* If we are at end of display wrap the address to 0 */
346 if (lcd_current_addr == LCD_COLS * LCD_ROWS)
347 lcd_current_addr = 0;
349 /* If we are at the end of a row put the cursor at the beginning of the next */
350 if (!(lcd_current_addr % LCD_COLS))
351 lcd_setReg(lcd_address(lcd_current_addr));
356 * Remap the glyph of a character.
358 * glyph - bitmap of 8x8 bits.
359 * code - must be 0-7 for the Hitachi LCD-II controller.
361 void lcd_remapChar(const char *glyph, char code)
365 /* Set CG RAM address */
366 lcd_setReg((uint8_t)((1<<6) | (code << 3)));
368 /* Write bitmap data */
369 for (i = 0; i < 8; i++)
372 lcd_dataWrite(glyph[i]);
375 /* Move back to original address */
376 lcd_setReg(lcd_address(lcd_current_addr));
381 void lcd_remapfont(void)
383 static const char lcd_glyphs[8] =
385 0x04, 0x0E, 0x15, 0x04, 0x04, 0x04, 0x04, 0x00 /* up arrow */
389 for (i = 0; i < 15; i++)
390 lcd_remapChar(i, bernie_char);
393 lcd_setAddr(lcd_DefLayer, 0);
394 for (i = 0; i < 80; i++)
395 lcd_putCharUnlocked(i);
399 void lcd_hw_init(void)
408 #endif /* CONFIG_LCD_4BIT */
410 lcd_regWrite(LCD_CMD_SETFUNC);
413 lcd_regWrite(LCD_CMD_DISPLAY_ON);
416 lcd_regWrite(LCD_CMD_CLEAR);
420 lcd_regWrite(LCD_CMD_RESET_DDRAM); // 4 bit mode doesn't allow char reprogramming
422 lcd_regWrite(LCD_CMD_DISPLAYMODE);