src-avr: Make avr-demo objects depend on ao_usb.h
[fw/altos] / src-avr / ao_serial_avr.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 __xdata struct ao_fifo  ao_usart1_rx_fifo;
21 __xdata struct ao_fifo  ao_usart1_tx_fifo;
22
23 ISR(USART1_RX_vect)
24 {
25         if (!ao_fifo_full(ao_usart1_rx_fifo))
26                 ao_fifo_insert(ao_usart1_rx_fifo, UDR1);
27         ao_wakeup(&ao_usart1_rx_fifo);
28 #if USE_SERIAL_STDIN
29         ao_wakeup(&ao_stdin_ready);
30 #endif
31 }
32
33 static __xdata uint8_t ao_serial_tx1_started;
34
35 static void
36 ao_serial_tx1_start(void)
37 {
38         if (!ao_fifo_empty(ao_usart1_tx_fifo) &&
39             !ao_serial_tx1_started)
40         {
41                 ao_serial_tx1_started = 1;
42                 ao_fifo_remove(ao_usart1_tx_fifo, UDR1);
43         }
44 }
45
46 ISR(USART1_UDRE_vect)
47 {
48         ao_serial_tx1_started = 0;
49         ao_serial_tx1_start();
50         ao_wakeup(&ao_usart1_tx_fifo);
51 }
52
53 char
54 ao_serial_getchar(void) __critical
55 {
56         char    c;
57         cli();
58         while (ao_fifo_empty(ao_usart1_rx_fifo))
59                 ao_sleep(&ao_usart1_rx_fifo);
60         ao_fifo_remove(ao_usart1_rx_fifo, c);
61         sei();
62         return c;
63 }
64
65 #if USE_SERIAL_STDIN
66 char
67 ao_serial_pollchar(void) __critical
68 {
69         char    c;
70         cli();
71         if (ao_fifo_empty(ao_usart1_rx_fifo)) {
72                 sei();
73                 return AO_READ_AGAIN;
74         }
75         ao_fifo_remove(ao_usart1_rx_fifo,c);
76         sei();
77         return c;
78 }
79 #endif
80
81 void
82 ao_serial_putchar(char c) __critical
83 {
84         cli();
85         while (ao_fifo_full(ao_usart1_tx_fifo))
86                 ao_sleep(&ao_usart1_tx_fifo);
87         ao_fifo_insert(ao_usart1_tx_fifo, c);
88         ao_serial_tx1_start();
89         sei();
90 }
91
92 void
93 ao_serial_drain(void) __critical
94 {
95         cli();
96         while (!ao_fifo_empty(ao_usart1_tx_fifo))
97                 ao_sleep(&ao_usart1_tx_fifo);
98         sei();
99 }
100
101 static const struct {
102         uint16_t ubrr;
103 } ao_serial_speeds[] = {
104         /* [AO_SERIAL_SPEED_4800] = */ {
105                 F_CPU / (16UL * 4800UL) - 1
106         },
107         /* [AO_SERIAL_SPEED_9600] = */ {
108                 F_CPU / (16UL * 9600UL) - 1
109         },
110         /* [AO_SERIAL_SPEED_19200] = */ {
111                 F_CPU / (16UL * 19200UL) - 1
112         },
113         /* [AO_SERIAL_SPEED_57600] = */ {
114                 F_CPU / (16UL * 57600UL) - 1
115         },
116 };
117
118 void
119 ao_serial_set_speed(uint8_t speed)
120 {
121         ao_serial_drain();
122         if (speed > AO_SERIAL_SPEED_57600)
123                 return;
124         UBRR1L = ao_serial_speeds[speed].ubrr;
125         UBRR1H = ao_serial_speeds[speed].ubrr >> 8;
126 }
127
128 void
129 ao_serial_init(void)
130 {
131         /* Ensure the uart is powered up */
132
133         PRR1 &= ~(1 << PRUSART1);
134
135         /* Pick a 9600 baud rate */
136         ao_serial_set_speed(AO_SERIAL_SPEED_9600);
137
138         UCSR1A = 0;
139         UCSR1C = ((0 << UMSEL10) |      /* Asynchronous mode */
140                   (0 << UPM10) |        /* No parity */
141                   (0 << USBS1) |        /* 1 stop bit */
142                   (3 << UCSZ10) |       /* 8 bit characters */
143                   (0 << UCPOL1));       /* MBZ for async mode */
144         UCSR1B = ((1 << RXEN1) |        /* Enable receiver */
145                   (1 << TXEN1) |        /* Enable transmitter */
146                   (1 << RXCIE1) |       /* Enable receive interrupts */
147                   (1 << UDRIE1));       /* Enable transmit empty interrupts */
148 #if USE_SERIAL_STDIN
149         int8_t  i;
150         i = ao_add_stdio(ao_serial_pollchar,
151                          ao_serial_putchar,
152                          NULL);
153         printf("Register serial stdio as %d\n", i);
154 #endif
155 }