altos: Bring up basic TeleTerra v0.2 UI
[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 static uint16_t ao_lcd_time = 3;
21
22 static __xdata uint8_t  ao_lcd_mutex;
23
24 static void
25 ao_lcd_delay(void)
26 {
27         volatile uint16_t       count;
28
29         for (count = 0; count < ao_lcd_time; count++)
30                 ;
31 }
32
33 static void
34 ao_lcd_send_ins(uint8_t ins)
35 {
36 //      printf("send ins %02x\n", ins);
37 //      ao_lcd_wait_idle();
38 //      ao_delay(1);
39         ao_lcd_delay();
40         ao_lcd_port_put_nibble(0, ins >> 4);
41         ao_lcd_port_put_nibble(0, ins & 0xf);
42 }
43
44 static void
45 ao_lcd_put_byte(uint8_t c)
46 {
47 //      printf ("send data %02x\n", c);
48 //      ao_lcd_wait_idle();
49         ao_lcd_delay();
50         ao_lcd_port_put_nibble(1, c >> 4);
51         ao_lcd_port_put_nibble(1, c & 0x0f);
52 }
53
54 void
55 ao_lcd_putstring(char *string)
56 {
57         char    c;
58
59         ao_mutex_get(&ao_lcd_mutex);
60         while ((c = (uint8_t) *string++))
61                 ao_lcd_put_byte((uint8_t) c);
62         ao_mutex_put(&ao_lcd_mutex);
63 }
64
65 #define AO_LCD_POWER_CONTROL    0x54
66
67 void
68 ao_lcd_contrast_set(uint8_t contrast)
69 {
70         ao_mutex_get(&ao_lcd_mutex);
71         ao_lcd_send_ins(AO_LCD_POWER_CONTROL | ((contrast >> 4) & 0x3));
72         ao_lcd_send_ins(0x70 | (contrast & 0xf));
73         ao_mutex_put(&ao_lcd_mutex);
74 }
75
76 void
77 ao_lcd_clear(void)
78 {
79         ao_mutex_get(&ao_lcd_mutex);
80         ao_lcd_send_ins(0x01);
81         ao_delay(1);
82         /* Entry mode */
83         ao_lcd_send_ins(0x04 | 0x02);
84         ao_mutex_put(&ao_lcd_mutex);
85 }
86
87 void
88 ao_lcd_goto(uint8_t addr)
89 {
90         ao_mutex_get(&ao_lcd_mutex);
91         ao_lcd_send_ins(0x80 | addr);
92         ao_lcd_send_ins(0x04 | 0x02);
93         ao_mutex_put(&ao_lcd_mutex);
94 }
95
96 void
97 ao_lcd_start(void)
98 {
99         /* get to 4bit mode */
100         ao_lcd_port_put_nibble(0, 0x3);
101         ao_lcd_port_put_nibble(0, 0x3);
102         ao_lcd_port_put_nibble(0, 0x3);
103         ao_lcd_port_put_nibble(0, 0x2);
104
105         /* function set */
106         ao_lcd_send_ins(0x28);
107         /* function set, instruction table 1 */
108         ao_lcd_send_ins(0x29);
109
110         /* freq set */
111         ao_lcd_send_ins(0x14);
112
113         /* Power/icon/contrast control*/
114         ao_lcd_send_ins(AO_LCD_POWER_CONTROL);
115
116         /* Follower control */
117         ao_lcd_send_ins(0x6d);
118         ao_delay(AO_MS_TO_TICKS(200));
119
120         /* contrast set */
121         ao_lcd_contrast_set(0x18);
122
123         /* Display on */
124         ao_lcd_send_ins(0x08 | 0x04);
125
126         /* Clear */
127         ao_lcd_clear();
128 }
129
130 void
131 ao_lcd_contrast(void)
132 {
133         ao_cmd_hex();
134         if (ao_cmd_status == ao_cmd_success) {
135                 printf("setting contrast to %02x\n", ao_cmd_lex_i);
136                 ao_lcd_contrast_set(ao_cmd_lex_i & 0x3f);
137         }
138 }
139
140 static uint8_t
141 ao_cmd_hex_nibble(void)
142 {
143         if ('0' <= ao_cmd_lex_c && ao_cmd_lex_c <= '9')
144                 return ao_cmd_lex_c - '0';
145         if ('a' <= ao_cmd_lex_c && ao_cmd_lex_c <= 'f')
146                 return ao_cmd_lex_c - ('a' - 10);
147         if ('A' <= ao_cmd_lex_c && ao_cmd_lex_c <= 'F')
148                 return ao_cmd_lex_c - ('A' - 10);
149         ao_cmd_status = ao_cmd_syntax_error;
150         return 0;
151 }
152
153 void
154 ao_lcd_string(void)
155 {
156         uint8_t col = 0;
157         char    c;
158
159         ao_cmd_decimal();
160         if (ao_cmd_status != ao_cmd_success)
161                 return;
162         ao_lcd_send_ins(0x80 | (ao_cmd_lex_i ? 0x40 : 0x00));
163         ao_cmd_white();
164         while (ao_cmd_lex_c != '\n') {
165                 c = ao_cmd_lex_c;
166                 if (c == '\\') {
167                         ao_cmd_lex();
168                         c = ao_cmd_hex_nibble() << 4;
169                         ao_cmd_lex();
170                         c |= ao_cmd_hex_nibble();
171                 }
172                 ao_lcd_put_byte(c);
173                 ao_cmd_lex();
174                 col++;
175         }
176         while (col < 16) {
177                 ao_lcd_put_byte(' ');
178                 col++;
179         }
180 }
181
182 void
183 ao_lcd_delay_set(void)
184 {
185         ao_cmd_decimal();
186         if (ao_cmd_status == ao_cmd_success) {
187                 printf("setting LCD delay to %d\n", ao_cmd_lex_i);
188                 ao_lcd_time = ao_cmd_lex_i;
189         }
190 }
191
192 __code struct ao_cmds ao_lcd_cmds[] = {
193         { ao_lcd_start, "S\0Start LCD" },
194         { ao_lcd_contrast, "C <contrast>\0Set LCD contrast" },
195         { ao_lcd_string, "s <line> <string>\0Send string to LCD" },
196         { ao_lcd_delay_set, "t <delay>\0Set LCD delay" },
197         { 0, NULL },
198 };
199
200 void
201 ao_lcd_init(void)
202 {
203         ao_lcd_port_init();
204         ao_cmd_register(&ao_lcd_cmds[0]);
205 }