altos: Make STM clock configuration per-product. Fix 32MHz CPU speed
[fw/altos] / src / avr / ao_lcd_port.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 static void
28 ao_lcd_port_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_port_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_port_set_bits(data);
53         ao_lcd_port_set_bits(data | (1 << PIN_E));
54         n = PINB & 0xf;
55         ao_lcd_port_set_bits(data);
56         return n;
57 }
58
59 void
60 ao_lcd_port_put_nibble(uint8_t rs, uint8_t data)
61 {
62         data = (data & 0xf) | (rs ? (1 << PIN_RS) : 0);
63         DDRB = (0xf) | (1 << PIN_RS) | (1 << PIN_E) | (1 << PIN_RW);
64         ao_lcd_port_set_bits(data);
65         ao_lcd_port_set_bits(data | (1 << PIN_E));
66         ao_lcd_port_set_bits(data);
67 }
68
69 void
70 ao_lcd_port_init(void)
71 {
72         DDRB = (1 << PIN_RS) | (1 << PIN_E) | (1 << PIN_RW);
73         PORTB = 0;
74 }