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