annotate testavr.c @ 35:fed32b382de2

Tidy up, hide details behind macros to make it more obvious what we talk to do do things. Convert constants to my preferred format.
author darius
date Tue, 23 Oct 2007 10:54:01 +0930
parents 48056516b3eb
children 25fa387ef7e9
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"
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
40 #include "usb.h"
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
41
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
42 #define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1)
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
43 #define UART_BAUD_RATE 38400
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
44
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
45 void process_cmd(void);
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
46
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
47 void uart_putsP(const char *addr);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
48 void uart_puts(const char *addr);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
49 int uart_putc(char c);
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
50 void uart_puts_dec(uint8_t a, uint8_t l);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
51 void uart_puts_hex(uint8_t a);
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
52 char uart_getc(void);
11
ccc39c9f320b Enable pretty LED command.
darius
parents: 10
diff changeset
53
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
54 /* Receive buffer storage */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
55 volatile struct {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
56 char buf[40];
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
57 uint8_t state;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
58 uint8_t len;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
59 } cmd = {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
60 .state = 255,
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
61 .len = 0
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
62 };
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
63
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
64 /* Rx complete */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
65 ISR(USART_RXC_vect) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
66 volatile char pit;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
67 char c;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
68
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
69 while (UCSRA & _BV(RXC)) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
70 /* 255 means we're waiting for main to process the command,
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
71 just throw stuff away
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
72 */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
73 if (cmd.state == 255) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
74 pit = UDR;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
75 continue;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
76 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
77 c = UDR;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
78
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
79 /* End of line? */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
80 if (c == '\n' || c == '\r') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
81 cmd.buf[cmd.state + 1] = '\0';
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
82 uart_putsP(PSTR("\n\r"));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
83 cmd.len = cmd.state;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
84 cmd.state = 255;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
85 continue;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
86 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
87
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
88 /* Backspace/delete */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
89 if (c == 0x08 || c == 0x7f) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
90 if (cmd.state > 0) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
91 cmd.state--;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
92 uart_putsP(PSTR("\010\040\010"));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
93 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
94 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
95 }
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
96
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
97 /* Anything unprintable just ignore it */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
98 if (!isprint(c))
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
99 continue;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
100
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
101 cmd.buf[cmd.state] = tolower(c);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
102
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
103 /* Echo back to the user */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
104 uart_putc(cmd.buf[cmd.state]);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
105
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
106 cmd.state++;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
107 /* Over flow? */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
108 if (cmd.state == ((sizeof(cmd.buf) / sizeof(cmd.buf[0])) - 1)) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
109 uart_putsP(PSTR("\n\rLine too long"));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
110 cmd.state = 0;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
111 continue;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
112 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
113 }
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 /* Tx complete */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
117 ISR(USART_TXC_vect) {
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
118
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
119 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
120
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
121 int
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
122 main(void) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
123 /* Disable interrupts while we frob stuff */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
124 cli();
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
125
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
126 /* Disable JTAG (yes twice) */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
127 MCUCSR |= _BV(JTD);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
128 MCUCSR |= _BV(JTD);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
129
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
130 /* USB data bus (7:0) */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
131 DDRA = 0x00;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
132 PORTA = 0x00;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
133
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
134 /* USB control (3:0) */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
135 DDRB = 0x0e;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
136 PORTB = 0x00;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
137
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
138 /* GPIO (0:7) */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
139 DDRC = 0xff;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
140 PORTC = 0x00;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
141
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
142 /* GPIO (2:7) */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
143 DDRD = 0xfc;
30
48056516b3eb Make sure our outputs are pulled high.
darius
parents: 24
diff changeset
144 PORTD = 0xfc;
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
145
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
146 /* Init UART */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
147 UBRRH = UART_BAUD_SELECT(UART_BAUD_RATE, F_CPU) >> 8;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
148 UBRRL = (uint8_t)UART_BAUD_SELECT(UART_BAUD_RATE, F_CPU);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
149
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
150 /* Enable receiver and transmitter. Turn on transmit interrupts */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
151 UCSRA = 0;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
152 UCSRB = _BV(RXEN) | _BV(TXEN) | _BV(RXCIE);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
153 UCSRC = _BV(URSEL) | _BV(UCSZ1) | _BV(UCSZ0);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
154 uart_putsP(PSTR("\n\r\n\r===============\n\r"
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
155 "Inited!\n\r\n\r"));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
156
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
157 /* Ready to go! */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
158 sei();
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
159
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
160 usb_init();
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
161 _delay_us(1000);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
162
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
163 uart_putsP(PSTR("> "));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
164 cmd.state = 0;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
165
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
166 /* Wait for user input or an "interrupt" */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
167 while (1) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
168 if (cmd.state == 255) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
169 process_cmd();
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
170 uart_putsP(PSTR("> "));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
171 /* Allow new characters to be processed */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
172 cmd.state = 0;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
173 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
174
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
175 if (!(PINB & _BV(PB0)))
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
176 usb_intr();
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
177 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
178 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
179
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
180 void
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
181 process_cmd(void) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
182 uint8_t ROM[8];
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
183 int8_t i, arg;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
184 uint8_t crc, buf[9];
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
185 int8_t temp;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
186 uint16_t tfrac;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
187
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
188 /* User just pressed enter */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
189 if (cmd.len == 0)
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
190 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
191
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
192 if (cmd.buf[0] == '?') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
193 uart_putsP(PSTR("rs Reset and check for presence\n\r"
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
194 "sr Search the bus for ROMs\n\r"
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
195 "re Read a bit\n\r"
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
196 "rb Read a byte\n\r"
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
197 "wr bit Write a bit\n\r"
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
198 "wb byte Write a byte (hex)\n\r"
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
199 "wc cmd [ROMID] Write command\n\r"
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
200 "te ROMID Read the temperature from a DS1820\n\r"
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
201 "in port Read from a port\n\r"
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
202 "out port val Write to a port\n\r"
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
203 "ddr port [val] Read/write DDR for a port\n\r"));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
204
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
205 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
206 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
207
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
208 i = strlen((char *)cmd.buf);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
209 if (cmd.len < 2)
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
210 goto badcmd;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
211
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
212 if (cmd.buf[0] == 'r' && cmd.buf[1] == 's') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
213 uart_putsP(PSTR("Resetting... "));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
214
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
215 if (OWTouchReset() == 1)
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
216 uart_putsP(PSTR("No presence pulse found\n\r"));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
217 else
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
218 uart_putsP(PSTR("Presence pulse found\n\r"));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
219 } else if (cmd.buf[0] == 'r' && cmd.buf[1] == 'e') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
220 if (OWReadBit())
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
221 uart_putsP(PSTR("Read a 1\n\r"));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
222 else
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
223 uart_putsP(PSTR("Read a 0\n\r"));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
224 } else if (cmd.buf[0] == 'r' && cmd.buf[1] == 'b') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
225 uart_putsP(PSTR("Read a 0x"));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
226 uart_puts_hex(OWReadByte());
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
227 uart_putsP(PSTR("\n\r"));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
228 } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'r') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
229 arg = strtol((char *)cmd.buf + 3, (char **)NULL, 10);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
230 OWWriteBit(arg);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
231 uart_putsP(PSTR("Wrote a "));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
232 if (arg)
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
233 uart_putsP(PSTR("1\n\r"));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
234 else
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
235 uart_putsP(PSTR("0\n\r"));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
236 } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'b') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
237 arg = (int)strtol((char *)cmd.buf + 3, (char **)NULL, 16);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
238 OWWriteByte(arg);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
239 } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'c') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
240 if (cmd.len < 5) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
241 uart_putsP(PSTR("No arguments\n\r"));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
242 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
243 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
244
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
245 arg = (int)strtol((char *)cmd.buf + 3, (char **)NULL, 16);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
246 if (arg == 0) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
247 uart_putsP(PSTR("Unparseable command\n\r"));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
248 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
249 }
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
250
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
251 if (i == 5) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
252 OWSendCmd(NULL, arg);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
253 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
254 }
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
255
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
256 if (i < 29) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
257 uart_putsP(PSTR("Can't parse ROM ID\n\r"));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
258 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
259 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
260 for (i = 0; i < 8; i++)
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
261 ROM[i] = (int)strtol((char *)cmd.buf + 6 + (3 * i), (char **)NULL, 16);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
262
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
263 OWSendCmd(ROM, arg);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
264 } else if (cmd.buf[0] == 't' && cmd.buf[1] == 'e') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
265 if (cmd.len < 26) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
266 uart_putsP(PSTR("Unable to parse ROM ID\n\r"));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
267 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
268 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
269
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
270 for (i = 0; i < 8; i++)
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
271 ROM[i] = (int)strtol((char *)cmd.buf + 3 * (i + 1), (char **)NULL, 16);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
272
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
273 if (ROM[0] != OW_FAMILY_TEMP) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
274 uart_putsP(PSTR("ROM specified isn't a temperature sensor\n\r"));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
275 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
276 }
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
277
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
278 OWSendCmd(ROM, OW_CONVERTT_CMD);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
279 i = 0;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
280 while (OWReadBit() == 0) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
281 i++;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
282 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
283 OWSendCmd(ROM, OW_RD_SCR_CMD);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
284 crc = 0;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
285 for (i = 0; i < 9; i++) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
286 buf[i] = OWReadByte();
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
287 if (i < 8)
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
288 OWCRC(buf[i], &crc);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
289 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
290
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
291 if (crc != buf[8]) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
292 uart_putsP(PSTR("CRC mismatch\n\r"));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
293 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
294 }
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
295
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
296 #if 0
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
297 uart_putsP(PSTR("temperature "));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
298 uart_puts_dec(temp >> 4, 0);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
299 uart_putsP(PSTR("."));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
300 uart_puts_dec((temp << 12) / 6553, 0);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
301 uart_putsP(PSTR("\n\r"));
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
302 #else
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
303 /* 0 Temperature LSB
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
304 * 1 Temperature MSB
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
305 * 2 Th
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
306 * 3 Tl
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
307 * 4 Reserved
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
308 * 5 Reserved
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
309 * 6 Count Remain
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
310 * 7 Count per C
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
311 * 8 CRC
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
312 */
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
313 #if 0
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
314 for (i = 0; i < 9; i++) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
315 uart_puts_dec(buf[i], 0);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
316 uart_putsP(PSTR("\n\r"));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
317 }
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
318 #endif
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
319 temp = buf[0];
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
320 if (buf[1] & 0x80)
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
321 temp -= 256;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
322 temp >>= 1;
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
323
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
324 tfrac = buf[7] - buf[6];
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
325 tfrac *= (uint16_t)100;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
326 tfrac /= buf[7];
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
327 tfrac += 75;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
328 if (tfrac < 100) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
329 temp--;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
330 } else {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
331 tfrac -= 100;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
332 }
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
333
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
334 if (temp < 0){
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
335 uart_putc('-');
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
336 uart_puts_dec(-temp, 0);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
337 } else
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
338 uart_puts_dec(temp, 0);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
339 uart_putsP(PSTR("."));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
340 uart_puts_dec(tfrac, 1);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
341 uart_putsP(PSTR("\n\r"));
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
342
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
343 #endif
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
344 } else if (cmd.buf[0] == 's' && cmd.buf[1] == 'r') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
345 memset(ROM, 0, 8);
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
346
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
347 i = OWFirst(ROM, 1, 0);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
348 do {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
349 switch (i) {
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
350 case OW_BADWIRE:
16
026dc24d85e0 Spell presence correctly.
darius
parents: 15
diff changeset
351 uart_putsP(PSTR("Presence pulse, but no module found, bad module/cabling?\n\r"));
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
352 break;
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
353
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
354 case OW_NOPRESENCE:
16
026dc24d85e0 Spell presence correctly.
darius
parents: 15
diff changeset
355 uart_putsP(PSTR("No presence pulse found\n\r"));
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
356 break;
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
357
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
358 case OW_BADCRC:
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
359 uart_putsP(PSTR("Bad CRC\n\r"));
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
360 break;
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
361
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
362 case OW_NOMODULES:
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
363 case OW_FOUND:
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
364 break;
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
365
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
366 default:
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
367 uart_putsP(PSTR("Unknown error from 1 wire library\n\r"));
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
368 break;
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
369 }
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
370
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
371 if (i != OW_FOUND)
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
372 break;
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
373
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
374 for (i = 0; i < 7; i++) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
375 uart_puts_hex(ROM[i]);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
376 uart_putc(':');
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
377 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
378 uart_puts_hex(ROM[7]);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
379 uart_putsP(PSTR("\n\r"));
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
380
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
381 i = OWNext(ROM, 1, 0);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
382 } while (1);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
383 } else if (cmd.buf[0] == 'i' && cmd.buf[1] == 'n') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
384 switch (tolower(cmd.buf[3])) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
385 case 'a':
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
386 crc = PINA;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
387 break;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
388
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
389 case 'b':
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
390 crc = PINB;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
391 break;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
392
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
393 case 'c':
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
394 crc = PINC;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
395 break;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
396
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
397 case 'd':
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
398 crc = PIND;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
399 break;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
400
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
401 default:
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
402 uart_putsP(PSTR("Unknown port\n\r"));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
403 return;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
404 }
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
405 uart_putsP(PSTR("0x"));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
406 uart_puts_hex(crc);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
407 uart_putsP(PSTR("\n\r"));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
408 } else if (cmd.buf[0] == 'o' && cmd.buf[1] == 'u') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
409 crc = strtol((char *)cmd.buf + 8, (char **)NULL, 16);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
410 switch (tolower(cmd.buf[4])) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
411 case 'a':
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
412 PORTA = crc;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
413 break;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
414
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
415 case 'b':
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
416 PORTB = crc;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
417 break;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
418
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
419 case 'c':
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
420 PORTC = crc;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
421 break;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
422
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
423 case 'd':
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
424 PORTD = crc;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
425 break;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
426
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
427 default:
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
428 uart_putsP(PSTR("Unknown port\n\r"));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
429 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
430 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
431 uart_putsP(PSTR("0x"));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
432 uart_puts_hex(crc);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
433 uart_putsP(PSTR("\n\r"));
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
434 } else if (cmd.buf[0] == 'u' && cmd.buf[1] == 's') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
435 usb_gendata();
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
436 } else {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
437 badcmd:
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
438 uart_putsP(PSTR("Unknown command, ? for a list\n\r"));
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
439 }
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
440 }
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
441
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
442 int
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
443 uart_putc(char c) {
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
444 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
445 UDR = c;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
446
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
447 return(0);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
448 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
449
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
450 void
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
451 uart_putsP(const char *addr) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
452 char c;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
453
17
a58b41b7d15c Covert to a later version of avr-libc. Stuff like not using inb/outb,
darius
parents: 16
diff changeset
454 while ((c = pgm_read_byte_near(addr++)))
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
455 uart_putc(c);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
456 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
457
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
458 void
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
459 uart_puts(const char *addr) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
460 while (*addr)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
461 uart_putc(*addr++);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
462 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
463
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
464 void
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
465 uart_puts_dec(uint8_t a, uint8_t l) {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
466 char s[4];
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
467
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
468 if (l && a < 10)
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
469 uart_putsP(PSTR("0"));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
470 uart_puts(utoa(a, s, 10));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
471 }
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
472
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
473 void
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
474 uart_puts_hex(uint8_t a) {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
475 char s[3];
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
476
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
477 if (a < 0x10)
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
478 uart_putc('0');
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
479
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
480 uart_puts(utoa(a, s, 16));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
481 }
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
482
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
483 char
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
484 uart_getc(void) {
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
485 while (!(UCSRA & _BV(RXC)))
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
486 ;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
487
17
a58b41b7d15c Covert to a later version of avr-libc. Stuff like not using inb/outb,
darius
parents: 16
diff changeset
488 return (UDR);
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
489 }
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
490