annotate testavr.c @ 7:a940431af6f5

Add usage for sr command.
author darius
date Mon, 12 Jul 2004 16:49:21 +0930
parents 81e2f85e02ce
children 7ed10c59ba06
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
1 /*
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
2 * Copyright (c) 2004
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
3 * Daniel O'Connor <darius@dons.net.au>. All rights reserved.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
4 *
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
5 * Redistribution and use in source and binary forms, with or without
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
6 * modification, are permitted provided that the following conditions
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
7 * are met:
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
8 * 1. Redistributions of source code must retain the above copyright
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
9 * notice, this list of conditions and the following disclaimer.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
10 * 2. Redistributions in binary form must reproduce the above copyright
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
11 * notice, this list of conditions and the following disclaimer in the
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
12 * documentation and/or other materials provided with the distribution.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
13 *
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
14 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
17 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
24 * SUCH DAMAGE.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
25 */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
26
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
27 #include <stdio.h>
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
28 #include <avr/io.h>
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
29 #include <avr/interrupt.h>
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
30 #include <avr/signal.h>
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
31 #include <avr/pgmspace.h>
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
32 #include <string.h>
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
33 #include <ctype.h>
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
34
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
35 #include "1wire.h"
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
36
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
37 #define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
38 #define XTAL_CPU 4000000 /* 4Mhz */
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
39 #define UART_BAUD_RATE 19200
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
40
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
41 void uart_putsP(const char *addr);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
42 void uart_puts(const char *addr);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
43 int uart_putc(char c);
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
44 int uart_getc(void);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
45 int beginswith_P(const char *s1, const char *s2);
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
46
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
47 #if 0
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
48 uint8_t PROGMEM ledvals[] = {0x01, 0x03, 0x07, 0x0e, 0x1c, 0x38, 0x70, 0xe0, 0xc0, 0x80};
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
49 static uint8_t dir = 0;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
50 static volatile uint8_t leds = 0;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
51 static uint8_t ledpos = 0;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
52
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
53 INTERRUPT(SIG_OVERFLOW0) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
54 if (!leds)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
55 return;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
56
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
57 /* Going up */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
58 if (dir == 0) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
59 if (ledpos == 9) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
60 dir = 1;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
61 TCNT0 = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
62 goto doleds;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
63 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
64
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
65 ledpos++;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
66 } else {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
67 if (ledpos == 0) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
68 dir = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
69 TCNT0 = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
70 goto doleds;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
71 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
72
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
73 ledpos--;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
74 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
75
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
76 doleds:
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
77 TCNT0 = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
78
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
79 PORTA = pgm_read_byte(&ledvals[ledpos]);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
80 }
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
81 #endif
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
82
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
83 void
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
84 usleep(uint16_t usec) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
85 /* 4Mhz = 250ns per clock cycle */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
86 usec /= 2;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
87 if (usec < 1)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
88 return;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
89
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
90 while (usec--)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
91 asm volatile (
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
92 ""
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
93 ::);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
94 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
95
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
96 int
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
97 main(void) {
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
98 uint8_t ROM[8];
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
99 char cmdbuf[40];
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
100 int i;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
101
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
102 cli();
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
103 #if 0
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
104 outp(0xff, DDRA);
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
105
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
106 /* Timer Clock divisor - CK/1024 */
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
107 outp(BV(CS00) | BV(CS02), TCCR0);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
108 #endif
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
109 outp(0xfe, DDRC);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
110 outp(0x00, PORTC);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
111
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
112 /* Init UART */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
113 outp(UART_BAUD_SELECT(UART_BAUD_RATE,XTAL_CPU), UBRR);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
114
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
115 /* Enable receiver and transmitter. Turn on transmit interrupts */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
116 outp(BV(RXEN) | BV(TXEN), UCR);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
117
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
118 uart_putsP(PSTR("\n\r\n\r===============\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
119 "Inited!\n\r\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
120
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
121 while (1) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
122 uart_putsP(PSTR("> "));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
123 i = 0;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
124 while (1) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
125 cmdbuf[i] = tolower(uart_getc());
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
126 if (cmdbuf[i] == '\n' || cmdbuf[i] == '\r')
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
127 break;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
128
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
129 /* Backspace */
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
130 if (cmdbuf[i] == 010) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
131 if (i > 0) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
132 uart_putsP(PSTR("\010\040\010"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
133 i--;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
134 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
135 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
136 }
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
137
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
138 /* Anything unprintable just ignore it */
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
139 if (!isprint(cmdbuf[i]))
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
140 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
141
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
142 uart_putc(cmdbuf[i]);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
143 i++;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
144 if (i == sizeof(cmdbuf)) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
145 uart_putsP(PSTR("\n\rLine too long\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
146 i = 0;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
147 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
148 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
149 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
150 cmdbuf[i + 1] = '\0';
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
151 uart_putsP(PSTR("\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
152 if (i == 0)
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
153 continue;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
154
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
155 if (cmdbuf[0] == '?') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
156 uart_putsP(PSTR("rs Reset and check for presence\n\r"
7
a940431af6f5 Add usage for sr command.
darius
parents: 4
diff changeset
157 "sr Search the bus for ROMs\n\r"
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
158 "re Read a bit\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
159 "rb Read a byte\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
160 "wr bit Write a bit\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
161 "wb byte Write a byte (hex)\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
162 "wc cmd [ROMID] Write command\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
163 "te ROMID Read the temperature from a DS1820\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
164 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
165 }
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
166
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
167 if (i < 2)
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
168 goto badcmd;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
169
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
170 if (cmdbuf[0] == 'r' && cmdbuf[1] == 's') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
171 uart_putsP(PSTR("Resetting... "));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
172
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
173 if (OWTouchReset() == 1)
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
174 uart_putsP(PSTR("No presense\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
175 else
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
176 uart_putsP(PSTR("Presense\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
177 } else if (cmdbuf[0] == 'r' && cmdbuf[1] == 'e') {
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
178 if (OWReadBit())
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
179 uart_putsP(PSTR("Read a 1\n\r"));
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
180 else
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
181 uart_putsP(PSTR("Read a 0\n\r"));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
182 } else if (cmdbuf[0] == 'r' && cmdbuf[1] == 'b') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
183 sprintf_P(cmdbuf, PSTR("Read a 0x%02x\n\r"), OWReadByte());
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
184 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
185 } else if (cmdbuf[0] == 'w' && cmdbuf[1] == 'r') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
186 int arg;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
187
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
188 if (sscanf_P(cmdbuf + 3, PSTR("%d"), &arg) != 1) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
189 uart_putsP(PSTR("Unable to parse wr command\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
190 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
191 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
192
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
193 OWWriteBit(arg);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
194 sprintf_P(cmdbuf, PSTR("Wrote a %d\n\r"), arg);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
195 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
196 } else if (cmdbuf[0] == 'w' && cmdbuf[1] == 'b') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
197 int arg;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
198
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
199 if (sscanf_P(cmdbuf + 3, PSTR("%2x"), &arg) != 1) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
200 uart_putsP(PSTR("Unable to parse wb command\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
201 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
202 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
203
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
204 OWWriteByte(arg);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
205 sprintf_P(cmdbuf, PSTR("Wrote a 0x%02x\n\r"), arg);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
206 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
207 } else if (cmdbuf[0] == 'w' && cmdbuf[1] == 'c') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
208 int arg;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
209
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
210 switch (sscanf_P(cmdbuf + 3, PSTR("%x %x:%x:%x:%x:%x:%x:%x:%x:%x"), &arg,
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
211 &ROM[0], &ROM[1], &ROM[2], &ROM[3],
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
212 &ROM[4], &ROM[5], &ROM[6], &ROM[7])) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
213 break;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
214
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
215 case 1:
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
216 OWSendCmd(NULL, arg);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
217 sprintf_P(cmdbuf, PSTR("Sent 0x%02x to all ROMS\n\r"), arg);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
218 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
219 break;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
220
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
221 case 9:
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
222 OWSendCmd(ROM, arg);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
223 sprintf_P(cmdbuf, PSTR("Sent 0x%02x to ROM %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n\r"),
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
224 arg, ROM[0], ROM[1], ROM[2], ROM[3],
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
225 ROM[4], ROM[5], ROM[6], ROM[7]);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
226 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
227
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
228 break;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
229
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
230 case 0:
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
231 default:
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
232 uart_putsP(PSTR("Unable to parse wc command\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
233 break;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
234 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
235
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
236 OWWriteByte(arg);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
237 sprintf_P(cmdbuf, PSTR("Wrote a 0x%02x\n\r"), arg);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
238 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
239 } else if (cmdbuf[0] == 't' && cmdbuf[1] == 'e') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
240 uint8_t crc;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
241 uint16_t temp;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
242 uint8_t buf[9];
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
243
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
244 if (sscanf_P(cmdbuf + 3, PSTR("%x:%x:%x:%x:%x:%x:%x:%x:%x"),
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
245 &ROM[0], &ROM[1], &ROM[2], &ROM[3],
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
246 &ROM[4], &ROM[5], &ROM[6], &ROM[7]) != 8) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
247 uart_putsP(PSTR("Unable to parse ROM ID\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
248 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
249 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
250
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
251 if (ROM[0] != OW_FAMILY_TEMP) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
252 uart_putsP(PSTR("ROM specified isn't a temperature sensor\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
253 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
254 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
255
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
256 OWSendCmd(ROM, OW_CONVERTT_CMD);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
257 #if OW_DEBUG
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
258 uart_putsP(PSTR("Command sent, waiting\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
259 #endif
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
260 i = 0;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
261 while (OWReadBit() == 0) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
262 i++;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
263 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
264 #if OW_DEBUG
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
265 sprintf_P(cmdbuf, PSTR("Temp comversion took %d cycles\n\r"), i);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
266 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
267 #endif
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
268 OWSendCmd(ROM, OW_RD_SCR_CMD);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
269 crc = 0;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
270 for (i = 0; i < 9; i++) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
271 buf[i] = OWReadByte();
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
272 if (i < 8)
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
273 OWCRC(buf[i], &crc);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
274 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
275
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
276 temp = buf[0];
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
277 temp |= (uint16_t)buf[1] << 8;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
278 temp <<= 3;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
279
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
280 if (crc != buf[8]) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
281 sprintf_P(cmdbuf, PSTR("CRC mismatch got %d vs calcd %d\n\r"), buf[8], crc);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
282 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
283 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
284 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
285
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
286 sprintf_P(cmdbuf, PSTR("temperature %d.%01d\n\r"),
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
287 temp >> 4, (temp << 12) / 6553);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
288 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
289 } else if (cmdbuf[0] == 's' && cmdbuf[1] == 'r') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
290 memset(ROM, 0, 8);
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
291 if (OWTouchReset()) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
292 uart_putsP(PSTR("No devices on bus\n\r"));
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
293 break;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
294 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
295 if (OWFirst(ROM, 1, 0) == 0) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
296 uart_putsP(PSTR("No module found\n\r"));
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
297 break;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
298 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
299 do {
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
300 sprintf_P(cmdbuf, PSTR("%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n\r"),
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
301 ROM[0],
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
302 ROM[1],
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
303 ROM[2],
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
304 ROM[3],
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
305 ROM[4],
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
306 ROM[5],
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
307 ROM[6],
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
308 ROM[7]);
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
309 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
310 } while (OWNext(ROM, 1, 0));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
311 #if 0
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
312
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
313 } else if (cmdbuf[0] == 'l' && cmdbuf[1] == 'e' && cmdbuf[2] == 'd') {
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
314 if (leds == 0) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
315 leds = 1;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
316 ledpos = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
317 outp(0, TCNT0);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
318 sbi(TIMSK, TOIE0);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
319 sei();
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
320 uart_putsP(PSTR("Starting\n\r"));
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
321 } else {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
322 leds = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
323 ledpos = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
324 PORTA = 0x00;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
325 cbi(TIMSK, TOIE0);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
326 cli();
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
327 uart_putsP(PSTR("Stopping\n\r"));
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
328 }
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
329 #endif
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
330 } else {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
331 badcmd:
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
332 uart_putsP(PSTR("Unknown command, ? for a list\n\r"));
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
333 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
334
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
335 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
336
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
337 return(0);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
338 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
339
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
340 int
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
341 uart_putc(char c) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
342 loop_until_bit_is_set(USR, UDRE);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
343 outp(c, UDR);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
344
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
345 return(0);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
346 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
347
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
348 void
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
349 uart_putsP(const char *addr) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
350 char c;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
351
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
352 while ((c = PRG_RDB((unsigned short)addr++)))
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
353 uart_putc(c);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
354 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
355
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
356 void
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
357 uart_puts(const char *addr) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
358 while (*addr)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
359 uart_putc(*addr++);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
360 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
361
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
362 int
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
363 uart_getc(void) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
364 while (!(inp(USR) & 0x80))
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
365 ;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
366
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
367 return (inp(UDR));
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
368 }
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
369