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