Updated copiright notice.
[bertos.git] / hw / hw_lcd.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2001 Bernardo Innocenti <bernie@codewiz.org>
31  * This file is part of DevLib - See README.devlib for information.
32  * -->
33  *
34  * \version $Id$
35  *
36  * \author Bernardo Innocenti <bernie@develer.com>
37  * \author Stefano Fedrigo <aleph@develer.com>
38  *
39  * \brief LCD low-level hardware macros
40  */
41
42 /*#*
43  *#* $Log$
44  *#* Revision 1.1  2006/09/20 17:39:24  marco
45  *#* Low level lcd for avr.
46  *#*
47  *#*/
48
49 #ifndef HW_LCD_H
50 #define HW_LCD_H
51
52 #include <appconfig.h>
53 //#include <hw.h>
54
55 #include <cfg/cpu.h>
56 #include <cfg/macros.h> /* BV() */
57 #include <cfg/debug.h>
58
59 #include <avr/io.h>
60 #include <stdbool.h>
61 #include <inttypes.h>
62
63 /**
64  * \name LCD I/O pins/ports
65  * @{
66  */
67 #define LCD_RS    BV(PG3)
68 #define LCD_RW    BV(PG0)
69 #define LCD_E     BV(PG2)
70 #define LCD_DB0   BV(PA0)
71 #define LCD_DB1   BV(PA1)
72 #define LCD_DB2   BV(PA2)
73 #define LCD_DB3   BV(PA3)
74 #define LCD_DB4   BV(PA4)
75 #define LCD_DB5   BV(PA5)
76 #define LCD_DB6   BV(PA6)
77 #define LCD_DB7   BV(PA7)
78 /*@}*/
79
80 /**
81  * \name DB high nibble (DB[4-7])
82  * @{
83  */
84 #define LCD_PORT     PORTG
85 #define LCD_DB_PORT  PORTA
86 #define LCD_PIN      PING
87 #define LCD_DB_PIN   PINA
88 #define LCD_DDR      DDRG
89 #define LCD_DB_DDR   DDRA
90
91 #if CONFIG_LCD_4BIT
92         #define LCD_MASK    (LCD_DB7 | LCD_DB6 | LCD_DB5 | LCD_DB4)
93         #define LCD_SHIFT   4
94 #else
95         #define LCD_MASK (uint8_t)0xff
96         #define LCD_SHIFT 0
97 #endif
98 /*@}*/
99
100 /**
101  * \name LCD bus control macros
102  * @{
103  */
104 #define LCD_CLR_RS      (LCD_PORT &= ~LCD_RS)
105 #define LCD_SET_RS      (LCD_PORT |=  LCD_RS)
106 #define LCD_CLR_RD      (LCD_PORT &= ~LCD_RW)
107 #define LCD_SET_RD      (LCD_PORT |=  LCD_RW)
108 #define LCD_CLR_E       (LCD_PORT &= ~LCD_E)
109 #define LCD_SET_E       (LCD_PORT |=  LCD_E)
110
111 #if CONFIG_LCD_4BIT
112         #define LCD_WRITE_H(x)  (LCD_DB_PORT = (LCD_DB_PORT & ~LCD_MASK) | (((x) >> (4 - LCD_SHIFT)) & LCD_MASK))
113         #define LCD_WRITE_L(x)  (LCD_DB_PORT = (LCD_DB_PORT & ~LCD_MASK) | (((x) << LCD_SHIFT) & LCD_MASK))
114         #define LCD_READ_H      ((LCD_DB_PIN & LCD_MASK) >> (4 - LCD_SHIFT))
115         #define LCD_READ_L      ((LCD_DB_PIN & LCD_MASK) >> LCD_SHIFT)
116 #else
117         #define LCD_WRITE(x) (LCD_DB_PORT = (x))
118         #define LCD_READ (LCD_DB_PIN)
119 #endif
120 /*@}*/
121
122 /** Set data bus direction to output (write to display) */
123 #define LCD_DB_OUT  (LCD_DB_DDR |= LCD_MASK)
124
125 /** Set data bus direction to input (read from display) */
126 #define LCD_DB_IN   (LCD_DB_DDR &= ~LCD_MASK)
127
128 /** Delay for write (Enable pulse width, 220ns) */
129 #define LCD_DELAY_WRITE \
130         do { \
131                 NOP; \
132                 NOP; \
133                 NOP; \
134                 NOP; \
135                 NOP; \
136         } while (0)
137
138 /** Delay for read (Data ouput delay time, 120ns) */
139 #define LCD_DELAY_READ \
140         do { \
141                 NOP; \
142                 NOP; \
143                 NOP; \
144                 NOP; \
145         } while (0)
146
147
148 INLINE void lcd_bus_init(void)
149 {
150         cpuflags_t flags;
151         IRQ_SAVE_DISABLE(flags);
152
153         LCD_PORT = (LCD_PORT & ~(LCD_E | LCD_RW)) | LCD_RS;
154         LCD_DDR  |= LCD_RS | LCD_RW | LCD_E;
155
156         /*
157          * Data bus is in output state most of the time:
158          * LCD r/w functions assume it is left in output state
159          */
160         LCD_DB_OUT;
161
162
163         IRQ_RESTORE(flags);
164 }
165
166 #endif /* HW_LCD_H */