annotate testavr.c @ 38:0a148f362097

Use \r\n instead of \n\r (keeps pexpect happy)
author darius
date Fri, 23 Nov 2007 12:01:54 +1030
parents 25fa387ef7e9
children 5898fba6593c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
1 /*
9
7ed10c59ba06 Supply clock speed via the command line so 1wire-delay.h can use it.
darius
parents: 7
diff changeset
2 * Test various AVR bits and pieces
7ed10c59ba06 Supply clock speed via the command line so 1wire-delay.h can use it.
darius
parents: 7
diff changeset
3 *
7ed10c59ba06 Supply clock speed via the command line so 1wire-delay.h can use it.
darius
parents: 7
diff changeset
4 * $Id$
7ed10c59ba06 Supply clock speed via the command line so 1wire-delay.h can use it.
darius
parents: 7
diff changeset
5 *
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
6 * Copyright (c) 2004
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
7 * Daniel O'Connor <darius@dons.net.au>. All rights reserved.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
8 *
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
9 * Redistribution and use in source and binary forms, with or without
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
10 * modification, are permitted provided that the following conditions
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
11 * are met:
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
12 * 1. Redistributions of source code must retain the above copyright
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
14 * 2. Redistributions in binary form must reproduce the above copyright
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
15 * notice, this list of conditions and the following disclaimer in the
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
16 * documentation and/or other materials provided with the distribution.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
17 *
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
18 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
21 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
28 * SUCH DAMAGE.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
29 */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
30
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
31 #include <avr/io.h>
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
32 #include <avr/interrupt.h>
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
33 #include <avr/pgmspace.h>
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
34 #include <string.h>
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
35 #include <ctype.h>
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
36 #include <stdlib.h>
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
37 #include <util/delay.h>
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
38
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
39 #include "1wire.h"
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
40 #ifdef WITHUSB
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
41 #include "usb.h"
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
42 #endif
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
43
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
44 #define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1)
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
45 #define UART_BAUD_RATE 38400
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
46
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
47 void process_cmd(void);
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
48
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
49 void uart_putsP(const char *addr);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
50 void uart_puts(const char *addr);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
51 int uart_putc(char c);
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
52 void uart_puts_dec(uint8_t a, uint8_t l);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
53 void uart_puts_hex(uint8_t a);
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
54 char uart_getc(void);
11
ccc39c9f320b Enable pretty LED command.
darius
parents: 10
diff changeset
55
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
56 /* Receive buffer storage */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
57 volatile struct {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
58 char buf[40];
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
59 uint8_t state;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
60 uint8_t len;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
61 } cmd = {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
62 .state = 255,
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
63 .len = 0
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
64 };
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
65
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
66 /* Rx complete */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
67 ISR(USART_RXC_vect) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
68 volatile char pit;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
69 char c;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
70
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
71 while (UCSRA & _BV(RXC)) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
72 /* 255 means we're waiting for main to process the command,
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
73 just throw stuff away
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
74 */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
75 if (cmd.state == 255) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
76 pit = UDR;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
77 continue;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
78 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
79 c = UDR;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
80
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
81 /* End of line? */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
82 if (c == '\n' || c == '\r') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
83 cmd.buf[cmd.state + 1] = '\0';
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
84 uart_putsP(PSTR("\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
85 cmd.len = cmd.state;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
86 cmd.state = 255;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
87 continue;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
88 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
89
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
90 /* Backspace/delete */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
91 if (c == 0x08 || c == 0x7f) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
92 if (cmd.state > 0) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
93 cmd.state--;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
94 uart_putsP(PSTR("\010\040\010"));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
95 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
96 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
97 }
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
98
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
99 /* Anything unprintable just ignore it */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
100 if (!isprint(c))
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
101 continue;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
102
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
103 cmd.buf[cmd.state] = tolower(c);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
104
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
105 /* Echo back to the user */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
106 uart_putc(cmd.buf[cmd.state]);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
107
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
108 cmd.state++;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
109 /* Over flow? */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
110 if (cmd.state == ((sizeof(cmd.buf) / sizeof(cmd.buf[0])) - 1)) {
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
111 uart_putsP(PSTR("\r\nLine too long"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
112 cmd.state = 0;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
113 continue;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
114 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
115 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
116 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
117
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
118 /* Tx complete */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
119 ISR(USART_TXC_vect) {
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
120
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
121 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
122
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
123 int
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
124 main(void) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
125 /* Disable interrupts while we frob stuff */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
126 cli();
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
127
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
128 #if 1
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
129 /* Disable JTAG (yes twice) */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
130 MCUCSR |= _BV(JTD);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
131 MCUCSR |= _BV(JTD);
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
132 #endif
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
133
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
134 #ifdef WITHUSB
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
135 /* USB data bus (7:0) */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
136 DDRA = 0x00;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
137 PORTA = 0x00;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
138
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
139 /* USB control (4:0) */
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
140 DDRB = 0x0d;
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
141 PORTB = 0x00;
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
142 #else
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
143 DDRA = 0xff;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
144 PORTA = 0x00;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
145 #endif
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
146 /* GPIO (0:7) */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
147 DDRC = 0xff;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
148 PORTC = 0x00;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
149
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
150 /* USART (0:1), IDBus (2:5), 485 (6:6), GPIO (7:7) */
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
151 DDRD = 0xf7;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
152 PORTD = 0xf7;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
153
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
154 /* Set up the one wire stuff */
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
155 OWInit();
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
156
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
157 /* Init UART */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
158 UBRRH = UART_BAUD_SELECT(UART_BAUD_RATE, F_CPU) >> 8;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
159 UBRRL = (uint8_t)UART_BAUD_SELECT(UART_BAUD_RATE, F_CPU);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
160
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
161 /* Enable receiver and transmitter. Turn on transmit interrupts */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
162 UCSRA = 0;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
163 UCSRB = _BV(RXEN) | _BV(TXEN) | _BV(RXCIE);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
164 UCSRC = _BV(URSEL) | _BV(UCSZ1) | _BV(UCSZ0);
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
165 uart_putsP(PSTR("\r\n\r\n===============\r\n"
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
166 "Inited!\r\n\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
167
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
168 /* Ready to go! */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
169 sei();
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
170
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
171 #if 0
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
172 DDRA = 0xff;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
173 DDRC = 0xff;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
174 while (1) {
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
175 uart_putsP(PSTR("1\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
176 PORTA = 0xff;
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
177 uart_putsP(PSTR("2\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
178 PORTC = 0x00;
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
179 uart_putsP(PSTR("3\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
180 _delay_us(1);
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
181 uart_putsP(PSTR("4\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
182 PORTA = 0x80;
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
183 uart_putsP(PSTR("5\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
184 PORTC = 0xff;
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
185 uart_putsP(PSTR("6\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
186 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
187 #endif
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
188
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
189 #ifdef WITHUSB
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
190 uart_putsP(PSTR("Calling usb_init\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
191 usb_init();
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
192 #endif
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
193 uart_putsP(PSTR("done\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
194 _delay_us(1000);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
195 uart_putsP(PSTR("> "));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
196 cmd.state = 0;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
197
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
198 /* Wait for user input or an "interrupt" */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
199 while (1) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
200 if (cmd.state == 255) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
201 process_cmd();
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
202 uart_putsP(PSTR("> "));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
203 /* Allow new characters to be processed */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
204 cmd.state = 0;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
205 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
206
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
207 #ifdef WITHUSB
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
208 if (!(PDICTL & _BV(PDIINT)))
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
209 usb_intr();
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
210 #endif
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
211 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
212 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
213
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
214 void
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
215 process_cmd(void) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
216 uint8_t ROM[8];
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
217 int8_t i, arg;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
218 uint8_t crc, buf[9];
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
219 int8_t temp;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
220 uint16_t tfrac;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
221
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
222 /* User just pressed enter */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
223 if (cmd.len == 0)
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
224 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
225
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
226 if (cmd.buf[0] == '?') {
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
227 uart_putsP(PSTR("rs Reset and check for presence\r\n"
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
228 "sr Search the bus for ROMs\r\n"
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
229 "re Read a bit\r\n"
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
230 "rb Read a byte\r\n"
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
231 "wr bit Write a bit\r\n"
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
232 "wb byte Write a byte (hex)\r\n"
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
233 "wc cmd [ROMID] Write command\r\n"
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
234 "te ROMID Read the temperature from a DS1820\r\n"
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
235 "in port Read from a port\r\n"
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
236 "out port val Write to a port\r\n"
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
237 "ddr port [val] Read/write DDR for a port\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
238
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
239 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
240 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
241
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
242 i = strlen((char *)cmd.buf);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
243 if (cmd.len < 2)
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
244 goto badcmd;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
245
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
246 if (cmd.buf[0] == 'r' && cmd.buf[1] == 's') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
247 uart_putsP(PSTR("Resetting... "));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
248
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
249 if (OWTouchReset() == 1)
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
250 uart_putsP(PSTR("No presence pulse found\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
251 else
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
252 uart_putsP(PSTR("Presence pulse found\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
253 } else if (cmd.buf[0] == 'r' && cmd.buf[1] == 'e') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
254 if (OWReadBit())
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
255 uart_putsP(PSTR("Read a 1\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
256 else
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
257 uart_putsP(PSTR("Read a 0\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
258 } else if (cmd.buf[0] == 'r' && cmd.buf[1] == 'b') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
259 uart_putsP(PSTR("Read a 0x"));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
260 uart_puts_hex(OWReadByte());
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
261 uart_putsP(PSTR("\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
262 } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'r') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
263 arg = strtol((char *)cmd.buf + 3, (char **)NULL, 10);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
264 OWWriteBit(arg);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
265 uart_putsP(PSTR("Wrote a "));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
266 if (arg)
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
267 uart_putsP(PSTR("1\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
268 else
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
269 uart_putsP(PSTR("0\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
270 } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'b') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
271 arg = (int)strtol((char *)cmd.buf + 3, (char **)NULL, 16);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
272 OWWriteByte(arg);
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
273 } else if (cmd.buf[0] == 'r' && cmd.buf[1] == 't') {
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
274 if (cmd.len < 26) {
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
275 uart_putsP(PSTR("Unable to parse ROM ID\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
276 return;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
277 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
278
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
279 if (OWTouchReset() != 0) {
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
280 uart_putsP(PSTR("No presence\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
281 return;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
282 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
283
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
284 for (i = 0; i < 8; i++)
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
285 ROM[i] = (int)strtol((char *)cmd.buf + 3 * (i + 1), (char **)NULL, 16);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
286
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
287 if (ROM[0] != OW_FAMILY_ROM) {
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
288 uart_putsP(PSTR("ROM specified isn't a DS2502\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
289 return;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
290 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
291
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
292 if (OWTouchReset() != 0) {
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
293 uart_putsP(PSTR("No presence\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
294 return;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
295 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
296
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
297 crc = 0;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
298
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
299 OWCRC(OW_READ_STATUS, &crc);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
300 OWSendCmd(ROM, OW_READ_STATUS);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
301
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
302 OWWriteByte(0x00);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
303 OWCRC(0x00, &crc);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
304
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
305 OWWriteByte(0x00);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
306 OWCRC(0x00, &crc);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
307
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
308 if (crc != OWReadByte()) {
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
309 uart_putsP(PSTR("CRC mismatch on command & address\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
310 return;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
311 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
312
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
313 crc = 0;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
314 for (i = 0; i < 8; i++) {
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
315 temp = OWReadByte();
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
316 uart_puts_hex(temp);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
317 OWCRC(temp, &crc);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
318 uart_putsP(PSTR(" "));
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
319 }
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
320 uart_putsP(PSTR("\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
321 if (crc != OWReadByte()) {
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
322 uart_putsP(PSTR("CRC mismatch on data\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
323 return;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
324 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
325 } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'e') {
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
326 if (cmd.len < 26) {
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
327 uart_putsP(PSTR("Unable to parse ROM ID\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
328 return;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
329 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
330
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
331 for (i = 0; i < 8; i++)
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
332 ROM[i] = (int)strtol((char *)cmd.buf + 3 * (i + 1), (char **)NULL, 16);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
333
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
334 if (ROM[0] != OW_FAMILY_ROM) {
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
335 uart_putsP(PSTR("ROM specified isn't a ROM\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
336 return;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
337 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
338
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
339 buf[0] = (int)strtol((char *)cmd.buf + 27, (char **)NULL, 16); /* Address */
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
340 buf[1] = (int)strtol((char *)cmd.buf + 30, (char **)NULL, 16); /* Data .. */
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
341 buf[2] = (int)strtol((char *)cmd.buf + 33, (char **)NULL, 16);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
342
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
343 if (OWTouchReset() != 0) {
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
344 uart_putsP(PSTR("No presence\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
345 return;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
346 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
347
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
348 i = OWProgROM(ROM, buf[0], 2, &buf[1], 0, 0);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
349 uart_putsP(PSTR("OWProgROM returned "));
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
350 uart_puts_dec(i, 0);
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
351 uart_putsP(PSTR("\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
352 } else if (cmd.buf[0] == 'r' && cmd.buf[1] == 'r') {
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
353 if (cmd.len < 26) {
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
354 uart_putsP(PSTR("Unable to parse ROM ID\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
355 return;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
356 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
357
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
358 for (i = 0; i < 8; i++)
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
359 ROM[i] = (int)strtol((char *)cmd.buf + 3 * (i + 1), (char **)NULL, 16);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
360
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
361 if (ROM[0] != OW_FAMILY_ROM) {
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
362 uart_putsP(PSTR("ROM specified isn't a ROM\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
363 return;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
364 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
365
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
366 crc = 0;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
367 OWSendCmd(ROM, OW_READ_MEMORY);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
368 OWCRC(OW_READ_MEMORY, &crc);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
369
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
370 OWWriteByte(0x00);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
371 OWCRC(0x00, &crc);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
372
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
373 OWWriteByte(0x00);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
374 OWCRC(0x00, &crc);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
375
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
376 if (crc != OWReadByte()) {
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
377 uart_putsP(PSTR("CRC mismatch on command & address\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
378 return;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
379 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
380
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
381 crc = 0;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
382 for (buf[0] = 0; buf[0] < 128; buf[0]++) {
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
383 buf[1] = OWReadByte();
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
384 if (buf[0] > 0) {
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
385 if (buf[0] % 16 != 0)
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
386 uart_putsP(PSTR(" "));
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
387 else
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
388 uart_putsP(PSTR("\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
389 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
390
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
391 uart_puts_hex(buf[1]);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
392 OWCRC(buf[1], &crc);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
393 }
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
394 uart_putsP(PSTR("\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
395 if (crc != OWReadByte()) {
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
396 uart_putsP(PSTR("CRC mismatch on data\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
397 return;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
398 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
399
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
400 } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'c') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
401 if (cmd.len < 5) {
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
402 uart_putsP(PSTR("No arguments\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
403 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
404 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
405
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
406 arg = (int)strtol((char *)cmd.buf + 3, (char **)NULL, 16);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
407 if (arg == 0) {
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
408 uart_putsP(PSTR("Unparseable command\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
409 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
410 }
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
411
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
412 if (i == 5) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
413 OWSendCmd(NULL, arg);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
414 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
415 }
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
416
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
417 if (i < 29) {
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
418 uart_putsP(PSTR("Can't parse ROM ID\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
419 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
420 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
421 for (i = 0; i < 8; i++)
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
422 ROM[i] = (int)strtol((char *)cmd.buf + 6 + (3 * i), (char **)NULL, 16);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
423
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
424 OWSendCmd(ROM, arg);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
425 } else if (cmd.buf[0] == 't' && cmd.buf[1] == 'e') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
426 if (cmd.len < 26) {
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
427 uart_putsP(PSTR("Unable to parse ROM ID\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
428 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
429 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
430
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
431 for (i = 0; i < 8; i++)
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
432 ROM[i] = (int)strtol((char *)cmd.buf + 3 * (i + 1), (char **)NULL, 16);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
433
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
434 if (ROM[0] != OW_FAMILY_TEMP) {
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
435 uart_putsP(PSTR("ROM specified isn't a temperature sensor\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
436 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
437 }
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
438
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
439 OWSendCmd(ROM, OW_CONVERTT_CMD);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
440 i = 0;
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
441 /* Wait for the conversion */
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
442 while (OWReadBit() == 0) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
443 i++;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
444 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
445 OWSendCmd(ROM, OW_RD_SCR_CMD);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
446 crc = 0;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
447 for (i = 0; i < 9; i++) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
448 buf[i] = OWReadByte();
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
449 if (i < 8)
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
450 OWCRC(buf[i], &crc);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
451 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
452
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
453 if (crc != buf[8]) {
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
454 uart_putsP(PSTR("CRC mismatch\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
455 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
456 }
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
457
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
458 #if 0
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
459 uart_putsP(PSTR("temperature "));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
460 uart_puts_dec(temp >> 4, 0);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
461 uart_putsP(PSTR("."));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
462 uart_puts_dec((temp << 12) / 6553, 0);
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
463 uart_putsP(PSTR("\r\n"));
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
464 #else
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
465 /* 0 Temperature LSB
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
466 * 1 Temperature MSB
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
467 * 2 Th
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
468 * 3 Tl
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
469 * 4 Reserved
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
470 * 5 Reserved
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
471 * 6 Count Remain
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
472 * 7 Count per C
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
473 * 8 CRC
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
474 */
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
475 #if 0
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
476 for (i = 0; i < 9; i++) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
477 uart_puts_dec(buf[i], 0);
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
478 uart_putsP(PSTR("\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
479 }
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
480 #endif
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
481 temp = buf[0];
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
482 if (buf[1] & 0x80)
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
483 temp -= 256;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
484 temp >>= 1;
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
485
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
486 tfrac = buf[7] - buf[6];
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
487 tfrac *= (uint16_t)100;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
488 tfrac /= buf[7];
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
489 tfrac += 75;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
490 if (tfrac < 100) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
491 temp--;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
492 } else {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
493 tfrac -= 100;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
494 }
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
495
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
496 if (temp < 0){
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
497 uart_putc('-');
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
498 uart_puts_dec(-temp, 0);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
499 } else
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
500 uart_puts_dec(temp, 0);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
501 uart_putsP(PSTR("."));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
502 uart_puts_dec(tfrac, 1);
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
503 uart_putsP(PSTR("\r\n"));
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
504
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
505 #endif
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
506 } else if (cmd.buf[0] == 's' && cmd.buf[1] == 'r') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
507 memset(ROM, 0, 8);
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
508
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
509 i = OWFirst(ROM, 1, 0);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
510 do {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
511 switch (i) {
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
512 case OW_BADWIRE:
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
513 uart_putsP(PSTR("Presence pulse, but no module found, bad module/cabling?\r\n"));
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
514 break;
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
515
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
516 case OW_NOPRESENCE:
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
517 uart_putsP(PSTR("No presence pulse found\r\n"));
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
518 break;
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
519
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
520 case OW_BADCRC:
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
521 uart_putsP(PSTR("Bad CRC\r\n"));
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
522 break;
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
523
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
524 case OW_NOMODULES:
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
525 case OW_FOUND:
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
526 break;
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
527
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
528 default:
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
529 uart_putsP(PSTR("Unknown error from 1 wire library\r\n"));
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
530 break;
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
531 }
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
532
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
533 if (i != OW_FOUND)
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
534 break;
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
535
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
536 for (i = 0; i < 7; i++) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
537 uart_puts_hex(ROM[i]);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
538 uart_putc(':');
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
539 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
540 uart_puts_hex(ROM[7]);
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
541 uart_putsP(PSTR("\r\n"));
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
542
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
543 i = OWNext(ROM, 1, 0);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
544 } while (1);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
545 } else if (cmd.buf[0] == 'i' && cmd.buf[1] == 'n') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
546 switch (tolower(cmd.buf[3])) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
547 case 'a':
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
548 crc = PINA;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
549 break;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
550
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
551 case 'b':
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
552 crc = PINB;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
553 break;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
554
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
555 case 'c':
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
556 crc = PINC;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
557 break;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
558
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
559 case 'd':
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
560 crc = PIND;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
561 break;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
562
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
563 default:
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
564 uart_putsP(PSTR("Unknown port\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
565 return;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
566 }
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
567 uart_putsP(PSTR("0x"));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
568 uart_puts_hex(crc);
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
569 uart_putsP(PSTR("\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
570 } else if (cmd.buf[0] == 'o' && cmd.buf[1] == 'u') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
571 crc = strtol((char *)cmd.buf + 8, (char **)NULL, 16);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
572 switch (tolower(cmd.buf[4])) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
573 case 'a':
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
574 PORTA = crc;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
575 break;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
576
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
577 case 'b':
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
578 PORTB = crc;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
579 break;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
580
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
581 case 'c':
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
582 PORTC = crc;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
583 break;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
584
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
585 case 'd':
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
586 PORTD = crc;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
587 break;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
588
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
589 default:
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
590 uart_putsP(PSTR("Unknown port\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
591 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
592 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
593 uart_putsP(PSTR("0x"));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
594 uart_puts_hex(crc);
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
595 uart_putsP(PSTR("\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
596 } else if (cmd.buf[0] == 'd' && cmd.buf[1] == 'd') {
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
597 crc = strtol((char *)cmd.buf + 8, (char **)NULL, 16);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
598 switch (tolower(cmd.buf[4])) {
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
599 case 'a':
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
600 DDRA = crc;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
601 break;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
602
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
603 case 'b':
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
604 DDRB = crc;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
605 break;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
606
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
607 case 'c':
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
608 DDRC = crc;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
609 break;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
610
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
611 case 'd':
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
612 DDRD = crc;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
613 break;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
614
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
615 default:
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
616 uart_putsP(PSTR("Unknown port\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
617 return;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
618 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
619 uart_putsP(PSTR("0x"));
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
620 uart_puts_hex(crc);
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
621 uart_putsP(PSTR("\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
622 #ifdef WITHUSB
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
623 } else if (cmd.buf[0] == 'u' && cmd.buf[1] == 's') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
624 usb_gendata();
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
625 #endif
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
626 } else {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
627 badcmd:
38
0a148f362097 Use \r\n instead of \n\r (keeps pexpect happy)
darius
parents: 37
diff changeset
628 uart_putsP(PSTR("Unknown command, ? for a list\r\n"));
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
629 }
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
630 }
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
631
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
632 int
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
633 uart_putc(char c) {
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
634 loop_until_bit_is_set(UCSRA, UDRE);
17
a58b41b7d15c Covert to a later version of avr-libc. Stuff like not using inb/outb,
darius
parents: 16
diff changeset
635 UDR = c;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
636
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
637 return(0);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
638 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
639
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
640 void
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
641 uart_putsP(const char *addr) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
642 char c;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
643
17
a58b41b7d15c Covert to a later version of avr-libc. Stuff like not using inb/outb,
darius
parents: 16
diff changeset
644 while ((c = pgm_read_byte_near(addr++)))
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
645 uart_putc(c);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
646 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
647
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
648 void
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
649 uart_puts(const char *addr) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
650 while (*addr)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
651 uart_putc(*addr++);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
652 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
653
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
654 void
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
655 uart_puts_dec(uint8_t a, uint8_t l) {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
656 char s[4];
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
657
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
658 if (l && a < 10)
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
659 uart_putsP(PSTR("0"));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
660 uart_puts(utoa(a, s, 10));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
661 }
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
662
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
663 void
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
664 uart_puts_hex(uint8_t a) {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
665 char s[3];
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
666
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
667 if (a < 0x10)
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
668 uart_putc('0');
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
669
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
670 uart_puts(utoa(a, s, 16));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
671 }
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
672
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
673 char
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
674 uart_getc(void) {
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
675 while (!(UCSRA & _BV(RXC)))
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
676 ;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
677
17
a58b41b7d15c Covert to a later version of avr-libc. Stuff like not using inb/outb,
darius
parents: 16
diff changeset
678 return (UDR);
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
679 }
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
680