altos: Flush the on-board mega log after every sample interval.
[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_cursor_on(void)
78 {
79         ao_mutex_get(&ao_lcd_mutex);
80         ao_lcd_send_ins(0x08 | 0x04 | 0x02 | 0x01);
81         ao_mutex_put(&ao_lcd_mutex);
82 }
83
84 void
85 ao_lcd_cursor_off(void)
86 {
87         ao_mutex_get(&ao_lcd_mutex);
88         ao_lcd_send_ins(0x08 | 0x04);
89         ao_mutex_put(&ao_lcd_mutex);
90 }
91
92 void
93 ao_lcd_clear(void)
94 {
95         ao_mutex_get(&ao_lcd_mutex);
96         ao_lcd_send_ins(0x01);
97         ao_delay(1);
98         /* Entry mode */
99         ao_lcd_send_ins(0x04 | 0x02);
100         ao_mutex_put(&ao_lcd_mutex);
101 }
102
103 void
104 ao_lcd_goto(uint8_t addr)
105 {
106         ao_mutex_get(&ao_lcd_mutex);
107         ao_lcd_send_ins(0x80 | addr);
108         ao_lcd_send_ins(0x04 | 0x02);
109         ao_mutex_put(&ao_lcd_mutex);
110 }
111
112 void
113 ao_lcd_start(void)
114 {
115         /* get to 4bit mode */
116         ao_lcd_port_put_nibble(0, 0x3);
117         ao_lcd_port_put_nibble(0, 0x3);
118         ao_lcd_port_put_nibble(0, 0x3);
119         ao_lcd_port_put_nibble(0, 0x2);
120
121         /* function set */
122         ao_lcd_send_ins(0x28);
123         /* function set, instruction table 1 */
124         ao_lcd_send_ins(0x29);
125
126         /* freq set */
127         ao_lcd_send_ins(0x14);
128
129         /* Power/icon/contrast control*/
130         ao_lcd_send_ins(AO_LCD_POWER_CONTROL);
131
132         /* Follower control */
133         ao_lcd_send_ins(0x6d);
134         ao_delay(AO_MS_TO_TICKS(200));
135
136         /* contrast set */
137         ao_lcd_contrast_set(0x18);
138
139         /* Display on */
140         ao_lcd_send_ins(0x08 | 0x04);
141
142         /* Clear */
143         ao_lcd_clear();
144 }
145
146 void
147 ao_lcd_init(void)
148 {
149         ao_lcd_port_init();
150 }