c35093bced0fa909c1d1b3d690a778cc61358145
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "ao.h"
20
21 static uint16_t ao_lcd_time = 3;
22
23 static __xdata uint8_t  ao_lcd_mutex;
24
25 static void
26 ao_lcd_delay(void)
27 {
28         volatile uint16_t       count;
29
30         for (count = 0; count < ao_lcd_time; count++)
31                 ;
32 }
33
34 static void
35 ao_lcd_send_ins(uint8_t ins)
36 {
37 //      printf("send ins %02x\n", ins);
38 //      ao_lcd_wait_idle();
39 //      ao_delay(1);
40         ao_lcd_delay();
41         ao_lcd_port_put_nibble(0, ins >> 4);
42         ao_lcd_port_put_nibble(0, ins & 0xf);
43 }
44
45 static void
46 ao_lcd_put_byte(uint8_t c)
47 {
48 //      printf ("send data %02x\n", c);
49 //      ao_lcd_wait_idle();
50         ao_lcd_delay();
51         ao_lcd_port_put_nibble(1, c >> 4);
52         ao_lcd_port_put_nibble(1, c & 0x0f);
53 }
54
55 void
56 ao_lcd_putstring(char *string)
57 {
58         char    c;
59
60         ao_mutex_get(&ao_lcd_mutex);
61         while ((c = (uint8_t) *string++))
62                 ao_lcd_put_byte((uint8_t) c);
63         ao_mutex_put(&ao_lcd_mutex);
64 }
65
66 #define AO_LCD_POWER_CONTROL    0x54
67
68 void
69 ao_lcd_contrast_set(uint8_t contrast)
70 {
71         ao_mutex_get(&ao_lcd_mutex);
72         ao_lcd_send_ins(AO_LCD_POWER_CONTROL | ((contrast >> 4) & 0x3));
73         ao_lcd_send_ins(0x70 | (contrast & 0xf));
74         ao_mutex_put(&ao_lcd_mutex);
75 }
76
77 void
78 ao_lcd_cursor_on(void)
79 {
80         ao_mutex_get(&ao_lcd_mutex);
81         ao_lcd_send_ins(0x08 | 0x04 | 0x02 | 0x01);
82         ao_mutex_put(&ao_lcd_mutex);
83 }
84
85 void
86 ao_lcd_cursor_off(void)
87 {
88         ao_mutex_get(&ao_lcd_mutex);
89         ao_lcd_send_ins(0x08 | 0x04);
90         ao_mutex_put(&ao_lcd_mutex);
91 }
92
93 void
94 ao_lcd_clear(void)
95 {
96         ao_mutex_get(&ao_lcd_mutex);
97         ao_lcd_send_ins(0x01);
98         ao_delay(1);
99         /* Entry mode */
100         ao_lcd_send_ins(0x04 | 0x02);
101         ao_mutex_put(&ao_lcd_mutex);
102 }
103
104 void
105 ao_lcd_goto(uint8_t addr)
106 {
107         ao_mutex_get(&ao_lcd_mutex);
108         ao_lcd_send_ins(0x80 | addr);
109         ao_lcd_send_ins(0x04 | 0x02);
110         ao_mutex_put(&ao_lcd_mutex);
111 }
112
113 void
114 ao_lcd_start(void)
115 {
116         /* get to 4bit mode */
117         ao_lcd_port_put_nibble(0, 0x3);
118         ao_lcd_port_put_nibble(0, 0x3);
119         ao_lcd_port_put_nibble(0, 0x3);
120         ao_lcd_port_put_nibble(0, 0x2);
121
122         /* function set */
123         ao_lcd_send_ins(0x28);
124         /* function set, instruction table 1 */
125         ao_lcd_send_ins(0x29);
126
127         /* freq set */
128         ao_lcd_send_ins(0x14);
129
130         /* Power/icon/contrast control*/
131         ao_lcd_send_ins(AO_LCD_POWER_CONTROL);
132
133         /* Follower control */
134         ao_lcd_send_ins(0x6d);
135         ao_delay(AO_MS_TO_TICKS(200));
136
137         /* contrast set */
138         ao_lcd_contrast_set(0x18);
139
140         /* Display on */
141         ao_lcd_send_ins(0x08 | 0x04);
142
143         /* Clear */
144         ao_lcd_clear();
145 }
146
147 void
148 ao_lcd_init(void)
149 {
150         ao_lcd_port_init();
151 }