5bc89bbd91efd74f11b62beb7ce471c7ec850617
[fw/altos] / src / drivers / 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 static uint16_t ao_lcd_time = 3;
112
113 void
114 ao_lcd_delay(void)
115 {
116         volatile uint16_t       count;
117
118         for (count = 0; count < ao_lcd_time; count++)
119                 ;
120 }
121
122 void
123 ao_lcd_send_ins(uint8_t data)
124 {
125 //      printf("send ins %02x\n", data);
126 //      ao_lcd_wait_idle();
127 //      ao_delay(1);
128         ao_lcd_delay();
129         ao_lcd_send_nibble(0, data >> 4);
130         ao_lcd_send_nibble(0, data & 0xf);
131 }
132
133 void
134 ao_lcd_send_data(uint8_t data)
135 {
136 //      printf ("send data %02x\n", data);
137 //      ao_lcd_wait_idle();
138         ao_lcd_delay();
139         ao_lcd_send_nibble(1, data >> 4);
140         ao_lcd_send_nibble(1, data & 0x0f);
141 }
142
143 void
144 ao_lcd_send_string(char *string)
145 {
146         uint8_t c;
147
148         while ((c = (uint8_t) *string++))
149                 ao_lcd_send_data(c);
150 }
151
152 #define AO_LCD_POWER_CONTROL    0x54
153
154 void
155 ao_lcd_contrast_set(uint8_t contrast)
156 {
157         ao_lcd_send_ins(AO_LCD_POWER_CONTROL | ((contrast >> 4) & 0x3));
158         ao_lcd_send_ins(0x70 | (contrast & 0xf));
159 }
160
161 void
162 ao_lcd_clear(void)
163 {
164         ao_lcd_send_ins(0x01);
165         ao_delay(1);
166         /* Entry mode */
167         ao_lcd_send_ins(0x04 | 0x02);
168 }
169
170 void
171 ao_lcd_start(void)
172 {
173         /* get to 4bit mode */
174         ao_lcd_send_nibble(0, 0x3);
175         ao_lcd_send_nibble(0, 0x3);
176         ao_lcd_send_nibble(0, 0x3);
177         ao_lcd_send_nibble(0, 0x2);
178
179         /* function set */
180         ao_lcd_send_ins(0x28);
181         /* function set, instruction table 1 */
182         ao_lcd_send_ins(0x29);
183
184         /* freq set */
185         ao_lcd_send_ins(0x14);
186
187         /* Power/icon/contrast control*/
188         ao_lcd_send_ins(AO_LCD_POWER_CONTROL);
189
190         /* Follower control */
191         ao_lcd_send_ins(0x6d);
192         ao_delay(AO_MS_TO_TICKS(200));
193
194         /* contrast set */
195         ao_lcd_contrast_set(0x18);
196
197         /* Display on */
198         ao_lcd_send_ins(0x08 | 0x04);
199
200         /* Clear */
201         ao_lcd_clear();
202
203 }
204
205 void
206 ao_lcd_contrast(void)
207 {
208         ao_cmd_hex();
209         if (ao_cmd_status == ao_cmd_success) {
210                 printf("setting contrast to %02x\n", ao_cmd_lex_i);
211                 ao_lcd_contrast_set(ao_cmd_lex_i & 0x3f);
212         }
213 }
214
215 static uint8_t
216 ao_cmd_hex_nibble(void)
217 {
218         if ('0' <= ao_cmd_lex_c && ao_cmd_lex_c <= '9')
219                 return ao_cmd_lex_c - '0';
220         if ('a' <= ao_cmd_lex_c && ao_cmd_lex_c <= 'f')
221                 return ao_cmd_lex_c - ('a' - 10);
222         if ('A' <= ao_cmd_lex_c && ao_cmd_lex_c <= 'F')
223                 return ao_cmd_lex_c - ('A' - 10);
224         ao_cmd_status = ao_cmd_syntax_error;
225         return 0;
226 }
227
228 void
229 ao_lcd_string(void)
230 {
231         uint8_t col = 0;
232         uint8_t c;
233
234         ao_cmd_decimal();
235         if (ao_cmd_status != ao_cmd_success)
236                 return;
237         ao_lcd_send_ins(0x80 | (ao_cmd_lex_i ? 0x40 : 0x00));
238         ao_cmd_white();
239         while (ao_cmd_lex_c != '\n') {
240                 c = ao_cmd_lex_c;
241                 if (c == '\\') {
242                         ao_cmd_lex();
243                         c = ao_cmd_hex_nibble() << 4;
244                         ao_cmd_lex();
245                         c |= ao_cmd_hex_nibble();
246                 }
247                 ao_lcd_send_data(c);
248                 ao_cmd_lex();
249                 col++;
250         }
251         while (col < 16) {
252                 ao_lcd_send_data(' ');
253                 col++;
254         }
255 }
256
257 void
258 ao_lcd_delay_set(void)
259 {
260         ao_cmd_decimal();
261         if (ao_cmd_status == ao_cmd_success) {
262                 printf("setting LCD delay to %d\n", ao_cmd_lex_i);
263                 ao_lcd_time = ao_cmd_lex_i;
264         }
265 }
266
267 __code struct ao_cmds ao_lcd_cmds[] = {
268         { ao_lcd_start, "S\0Start LCD" },
269         { ao_lcd_contrast, "C <contrast>\0Set LCD contrast" },
270         { ao_lcd_string, "s <line> <string>\0Send string to LCD" },
271         { ao_lcd_delay_set, "t <delay>\0Set LCD delay" },
272         { 0, NULL },
273 };
274
275 void
276 ao_lcd_init(void)
277 {
278         DDRB = (1 << PIN_RS) | (1 << PIN_E) | (1 << PIN_RW);
279         PORTB = 0;
280         ao_cmd_register(&ao_lcd_cmds[0]);
281 }