annotate cons.c @ 2:43d3b2bef999

Make the programmer code smarter, default to Dragon.
author darius@Inchoate
date Wed, 11 Mar 2009 17:10:13 +1030
parents 3879f487b661
children 15d89caaf516
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
1 /*
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
2 * Console code for AVR board
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
3 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
4 * Copyright (c) 2008
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
5 * Daniel O'Connor <darius@dons.net.au>. All rights reserved.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
6 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
7 * Redistribution and use in source and binary forms, with or without
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
8 * modification, are permitted provided that the following conditions
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
9 * are met:
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
10 * 1. Redistributions of source code must retain the above copyright
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
11 * notice, this list of conditions and the following disclaimer.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
15 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
26 * SUCH DAMAGE.
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
27 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
28
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
29 #include <ctype.h>
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
30 #include <stdio.h>
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
31 #include <stdint.h>
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
32 #include <stdlib.h>
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
33 #include <avr/interrupt.h>
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
34 #include <avr/pgmspace.h>
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
35 #include "cons.h"
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
36
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
37 #define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
38
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
39 /* Receive buffer storage */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
40 consbuf_t cmd;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
41
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
42 /*
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
43 * Stub to use with fdevopen
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
44 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
45 * We ignore f and always succeed
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
46 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
47 static int _putc(char c, FILE *f) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
48 cons_putc(c);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
49 return(0);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
50 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
51
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
52 /*
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
53 * Stub to use with fdevopen
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
54 *
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
55 * We ignore f and always succeed
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
56 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
57 static int _getc(FILE *f) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
58 return(cons_getc());
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
59 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
60
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
61 void
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
62 cons_init(void) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
63 UBRR0 = UART_BAUD_SELECT(38400, F_CPU);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
64
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
65 /* Enable receiver and transmitter. Turn on rx interrupts */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
66 UCSR0A = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
67 UCSR0B = _BV(RXEN0) | _BV(TXEN0) | _BV(RXCIE0);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
68 UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
69
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
70 fdevopen(_putc, NULL); /* Open stdout */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
71 fdevopen(NULL, _getc); /* Open stdin */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
72 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
73
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
74 int
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
75 cons_putc(char c) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
76 loop_until_bit_is_set(UCSR0A, UDRE0);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
77 UDR0 = c;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
78
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
79 return(0);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
80 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
81
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
82 void
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
83 cons_putsP(const char *addr) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
84 char c;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
85
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
86 while ((c = pgm_read_byte_near(addr++)))
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
87 cons_putc(c);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
88 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
89
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
90 void
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
91 cons_puts(const char *addr) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
92 while (*addr)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
93 cons_putc(*addr++);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
94 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
95
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
96 void
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
97 cons_puts_dec(uint8_t a, uint8_t l) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
98 char s[4];
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
99
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
100 if (l && a < 10)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
101 cons_putsP(PSTR("0"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
102 cons_puts(utoa(a, s, 10));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
103 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
104
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
105 void
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
106 cons_puts_hex(uint8_t a) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
107 char s[3];
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
108
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
109 if (a < 0x10)
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
110 cons_putc('0');
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
111
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
112 cons_puts(utoa(a, s, 16));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
113 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
114
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
115 char
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
116 cons_getc(void) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
117 while (!(UCSR0A & _BV(RXC0)))
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
118 ;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
119
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
120 return (UDR0);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
121 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
122
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
123 /* Rx complete */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
124 ISR(USART0_RX_vect) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
125 volatile char pit;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
126 char c;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
127
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
128 while (UCSR0A & _BV(RXC0)) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
129 /* 255 means we're waiting for main to process the command,
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
130 just throw stuff away
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
131 */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
132 if (cmd.state == 255) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
133 pit = UDR0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
134 continue;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
135 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
136 c = UDR0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
137
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
138 /* End of line? */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
139 if (c == '\n' || c == '\r') {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
140 cmd.buf[cmd.state] = '\0';
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
141 printf_P(PSTR("\r\n"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
142 cmd.len = cmd.state;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
143 cmd.state = 255;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
144 continue;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
145 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
146
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
147 /* Backspace/delete */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
148 if (c == 0x08 || c == 0x7f) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
149 if (cmd.state > 0) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
150 cmd.state--;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
151 printf_P(PSTR("\010\040\010"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
152 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
153 continue;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
154 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
155
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
156 /* Anything unprintable just ignore it */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
157 if (!isprint(c))
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
158 continue;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
159
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
160 cmd.buf[cmd.state] = tolower(c);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
161
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
162 /* Echo back to the user */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
163 cons_putc(cmd.buf[cmd.state]);
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
164
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
165 cmd.state++;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
166 /* Over flow? */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
167 if (cmd.state == ((sizeof(cmd.buf) / sizeof(cmd.buf[0])) - 1)) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
168 printf_P(PSTR("\r\nLine too long"));
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
169 cmd.state = 0;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
170 continue;
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
171 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
172 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
173 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
174
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
175 /* Tx complete */
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
176 ISR(USART0_TX_vect) {
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
177
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
178 }
3879f487b661 Initial commit of routines I copy and paste.
darius@Inchoate
parents:
diff changeset
179