src-avr: Add LCD driver for NHD-C0216CU-FN-GWB-3V display
[fw/altos] / src-avr / ao_lcd.c
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao.h"
19
20 #define LCD_PORT        PORTB
21 #define LCD_DDR         DDRB
22
23 #define PIN_RS          4
24 #define PIN_E           5
25 #define PIN_RW          6
26
27 void
28 ao_lcd_set_bits(uint8_t bits)
29 {
30 #if 0
31         printf("\tLCD data %x RS %d R/W %d E %d\n",
32                bits & 0xf,
33                (bits & (1 << PIN_RS)) ? 1 : 0,
34                (bits & (1 << PIN_RW)) ? 1 : 0,
35                (bits & (1 << PIN_E)) ? 1 : 0);
36 #endif
37         LCD_PORT = bits;
38 #if 0
39         ao_delay(1);
40         if (bits & (1 << PIN_RW))
41                 printf("\tLCD input %x\n", PINB);
42 #endif
43 }
44
45 uint8_t
46 ao_lcd_get_nibble(uint8_t rs)
47 {
48         uint8_t data = (rs ? (1 << PIN_RS) : 0) | (1 << PIN_RW);
49         uint8_t n;
50
51         DDRB = (1 << PIN_RS) | (1 << PIN_E) | (1 << PIN_RW);
52         ao_lcd_set_bits(data);
53         ao_lcd_set_bits(data | (1 << PIN_E));
54         n = PINB & 0xf;
55         ao_lcd_set_bits(data);
56         return n;
57 }
58
59 uint8_t
60 ao_lcd_get_status(void)
61 {
62         uint8_t high, low;
63         uint8_t data;
64
65         high = ao_lcd_get_nibble(0);
66         low = ao_lcd_get_nibble(0);
67         data = (high << 4) | low;
68         printf ("\tLCD status %02x\n", data);
69         return data;
70 }
71
72 uint8_t
73 ao_lcd_get_data(void)
74 {
75         uint8_t high, low;
76         uint8_t data;
77
78         high = ao_lcd_get_nibble(1);
79         low = ao_lcd_get_nibble(1);
80         data = (high << 4) | low;
81         printf ("\tLCD data %02x\n", data);
82         return data;
83 }
84
85 void
86 ao_lcd_wait_idle(void)
87 {
88         uint8_t status;
89         uint8_t count = 0;
90
91         do {
92                 status = ao_lcd_get_status();
93                 count++;
94                 if (count > 100) {
95                         printf("idle timeout\n");
96                         break;
97                 }
98         } while (0);    /* status & 0x80); */
99 }
100
101 void
102 ao_lcd_send_nibble(uint8_t rs, uint8_t data)
103 {
104         data = (data & 0xf) | (rs ? (1 << PIN_RS) : 0);
105         DDRB = (0xf) | (1 << PIN_RS) | (1 << PIN_E) | (1 << PIN_RW);
106         ao_lcd_set_bits(data);
107         ao_lcd_set_bits(data | (1 << PIN_E));
108         ao_lcd_set_bits(data);
109 }
110
111 void
112 ao_lcd_send_ins(uint8_t data)
113 {
114         printf("send ins %02x\n", data);
115         ao_lcd_wait_idle();
116         ao_lcd_send_nibble(0, data >> 4);
117         ao_lcd_send_nibble(0, data & 0xf);
118 }
119
120 void
121 ao_lcd_send_data(uint8_t data)
122 {
123         printf ("send data %02x\n", data);
124         ao_lcd_wait_idle();
125         ao_lcd_send_nibble(1, data >> 4);
126         ao_lcd_send_nibble(1, data & 0x0f);
127 }
128
129 void
130 ao_lcd_send_string(char *string)
131 {
132         uint8_t c;
133
134         while ((c = (uint8_t) *string++))
135                 ao_lcd_send_data(c);
136 }
137
138 #define AO_LCD_POWER_CONTROL    0x54
139
140 void
141 ao_lcd_contrast_set(uint8_t contrast)
142 {
143         ao_lcd_send_ins(AO_LCD_POWER_CONTROL | ((contrast >> 4) & 0x3));
144         ao_lcd_send_ins(0x70 | (contrast & 0xf));
145 }
146
147 void
148 ao_lcd_clear(void)
149 {
150         ao_lcd_send_ins(0x01);
151         ao_delay(1);
152         /* Entry mode */
153         ao_lcd_send_ins(0x04 | 0x02);
154 }
155
156 void
157 ao_lcd_start(void)
158 {
159         /* get to 4bit mode */
160         ao_lcd_send_nibble(0, 0x3);
161         ao_lcd_send_nibble(0, 0x3);
162         ao_lcd_send_nibble(0, 0x3);
163         ao_lcd_send_nibble(0, 0x2);
164
165         /* function set */
166         ao_lcd_send_ins(0x28);
167         /* function set, instruction table 1 */
168         ao_lcd_send_ins(0x29);
169
170         /* freq set */
171         ao_lcd_send_ins(0x14);
172
173         /* Power/icon/contrast control*/
174         ao_lcd_send_ins(AO_LCD_POWER_CONTROL);
175
176         /* Follower control */
177         ao_lcd_send_ins(0x6d);
178         ao_delay(AO_MS_TO_TICKS(200));
179
180         /* contrast set */
181         ao_lcd_contrast_set(0x18);
182
183         /* Display on */
184         ao_lcd_send_ins(0x08 | 0x04);
185
186         /* Clear */
187         ao_lcd_clear();
188
189 }
190
191 void
192 ao_lcd_contrast(void)
193 {
194         ao_cmd_hex();
195         if (ao_cmd_status == ao_cmd_success) {
196                 printf("setting contrast to %02x\n", ao_cmd_lex_i);
197                 ao_lcd_contrast_set(ao_cmd_lex_i & 0x3f);
198         }
199 }
200
201 void
202 ao_lcd_string(void)
203 {
204         ao_lcd_clear();
205         ao_cmd_white();
206         while (ao_cmd_lex_c != '\n') {
207                 ao_lcd_send_data(ao_cmd_lex_c);
208                 ao_cmd_lex();
209         }
210 }
211
212 __code struct ao_cmds ao_lcd_cmds[] = {
213         { ao_lcd_start, "S\0Start LCD" },
214         { ao_lcd_contrast, "C\0Set LCD contrast" },
215         { ao_lcd_string, "s\0Send string to LCD" },
216         { 0, NULL },
217 };
218
219 void
220 ao_lcd_init(void)
221 {
222         DDRB = (1 << PIN_RS) | (1 << PIN_E) | (1 << PIN_RW);
223         PORTB = 0;
224         ao_cmd_register(&ao_lcd_cmds[0]);
225 }