altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / src / avr / ao_debug_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 void
22 uart_send(char c)
23 {
24         loop_until_bit_is_set(UCSR1A, UDRE1);
25         UDR1 = c;
26 }
27
28 int
29 uart_put(char c, FILE *stream)
30 {
31         if (c == '\n')
32                 uart_send('\r');
33         uart_send(c);
34         return 0;
35 }
36
37 int
38 uart_get(FILE *stream)
39 {
40         loop_until_bit_is_set(UCSR1A, RXC1);
41         return (int) UDR1 & 0xff;
42 }
43
44 void
45 uart_init(uint16_t baud)
46 {
47         PRR1 &= ~(1 << PRUSART1);
48         UBRR1L = baud;
49         UBRR1H = baud >> 8;
50         UCSR1A = 0;
51         UCSR1B = ((1 << RXEN1) |        /* Enable receiver */
52                   (1 << TXEN1));        /* Enable transmitter */
53         UCSR1C = ((0 << UMSEL10) |      /* Asynchronous mode */
54                   (0 << UPM10) |        /* No parity */
55                   (0 << USBS1) |        /* 1 stop bit */
56                   (3 << UCSZ10) |       /* 8 bit characters */
57                   (0 << UCPOL1));       /* MBZ for async mode */
58 }
59
60 static FILE mystdout = FDEV_SETUP_STREAM(uart_put, NULL, _FDEV_SETUP_WRITE);
61
62 static FILE mystdin = FDEV_SETUP_STREAM(NULL, uart_get, _FDEV_SETUP_READ);
63
64 void    ao_debug_init(void)
65 {
66         uart_init(F_CPU / (16UL * 9600UL) - 1);
67
68         stdout = &mystdout;
69         stdin = &mystdin;
70
71         if (DDRB & AO_LED_RED) {
72                 printf ("oops, starting all over\n");
73                 for (;;)
74                         ;
75         }
76         DDRB |= (1 << 7);
77         PORTB |= (1 << 7);
78         printf ("debug initialized\n");
79 }