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