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