annotate testavr.c @ 5:4fe81c3b1f4d

Ignore file.
author darius
date Mon, 12 Jul 2004 15:41:41 +0930
parents 81e2f85e02ce
children a940431af6f5
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"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
157 "re Read a bit\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
158 "rb Read a byte\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
159 "wr bit Write a bit\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
160 "wb byte Write a byte (hex)\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
161 "wc cmd [ROMID] Write command\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
162 "te ROMID Read the temperature from a DS1820\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
163 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
164 }
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
165
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
166 if (i < 2)
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
167 goto badcmd;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
168
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
169 if (cmdbuf[0] == 'r' && cmdbuf[1] == 's') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
170 uart_putsP(PSTR("Resetting... "));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
171
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
172 if (OWTouchReset() == 1)
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
173 uart_putsP(PSTR("No presense\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
174 else
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
175 uart_putsP(PSTR("Presense\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
176 } else if (cmdbuf[0] == 'r' && cmdbuf[1] == 'e') {
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
177 if (OWReadBit())
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
178 uart_putsP(PSTR("Read a 1\n\r"));
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
179 else
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
180 uart_putsP(PSTR("Read a 0\n\r"));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
181 } else if (cmdbuf[0] == 'r' && cmdbuf[1] == 'b') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
182 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
183 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
184 } else if (cmdbuf[0] == 'w' && cmdbuf[1] == 'r') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
185 int arg;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
186
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
187 if (sscanf_P(cmdbuf + 3, PSTR("%d"), &arg) != 1) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
188 uart_putsP(PSTR("Unable to parse wr command\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
189 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
190 }
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 OWWriteBit(arg);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
193 sprintf_P(cmdbuf, PSTR("Wrote a %d\n\r"), arg);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
194 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
195 } else if (cmdbuf[0] == 'w' && cmdbuf[1] == 'b') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
196 int arg;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
197
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
198 if (sscanf_P(cmdbuf + 3, PSTR("%2x"), &arg) != 1) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
199 uart_putsP(PSTR("Unable to parse wb command\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
200 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
201 }
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 OWWriteByte(arg);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
204 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
205 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
206 } else if (cmdbuf[0] == 'w' && cmdbuf[1] == 'c') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
207 int arg;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
208
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
209 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
210 &ROM[0], &ROM[1], &ROM[2], &ROM[3],
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
211 &ROM[4], &ROM[5], &ROM[6], &ROM[7])) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
212 break;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
213
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
214 case 1:
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
215 OWSendCmd(NULL, arg);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
216 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
217 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
218 break;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
219
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
220 case 9:
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
221 OWSendCmd(ROM, arg);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
222 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
223 arg, ROM[0], ROM[1], ROM[2], ROM[3],
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
224 ROM[4], ROM[5], ROM[6], ROM[7]);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
225 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
226
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
227 break;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
228
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
229 case 0:
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
230 default:
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
231 uart_putsP(PSTR("Unable to parse wc command\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
232 break;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
233 }
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 OWWriteByte(arg);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
236 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
237 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
238 } else if (cmdbuf[0] == 't' && cmdbuf[1] == 'e') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
239 uint8_t crc;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
240 uint16_t temp;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
241 uint8_t buf[9];
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
242
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
243 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
244 &ROM[0], &ROM[1], &ROM[2], &ROM[3],
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
245 &ROM[4], &ROM[5], &ROM[6], &ROM[7]) != 8) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
246 uart_putsP(PSTR("Unable to parse ROM ID\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
247 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
248 }
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 if (ROM[0] != OW_FAMILY_TEMP) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
251 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
252 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
253 }
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 OWSendCmd(ROM, OW_CONVERTT_CMD);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
256 #if OW_DEBUG
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
257 uart_putsP(PSTR("Command sent, waiting\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
258 #endif
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
259 i = 0;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
260 while (OWReadBit() == 0) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
261 i++;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
262 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
263 #if OW_DEBUG
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
264 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
265 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
266 #endif
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
267 OWSendCmd(ROM, OW_RD_SCR_CMD);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
268 crc = 0;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
269 for (i = 0; i < 9; i++) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
270 buf[i] = OWReadByte();
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
271 if (i < 8)
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
272 OWCRC(buf[i], &crc);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
273 }
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 temp = buf[0];
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
276 temp |= (uint16_t)buf[1] << 8;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
277 temp <<= 3;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
278
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
279 if (crc != buf[8]) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
280 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
281 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
282 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
283 }
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 sprintf_P(cmdbuf, PSTR("temperature %d.%01d\n\r"),
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
286 temp >> 4, (temp << 12) / 6553);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
287 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
288 } else if (cmdbuf[0] == 's' && cmdbuf[1] == 'r') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
289 memset(ROM, 0, 8);
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
290 if (OWTouchReset()) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
291 uart_putsP(PSTR("No devices on bus\n\r"));
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
292 break;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
293 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
294 if (OWFirst(ROM, 1, 0) == 0) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
295 uart_putsP(PSTR("No module found\n\r"));
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
296 break;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
297 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
298 do {
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
299 sprintf_P(cmdbuf, PSTR("%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n\r"),
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
300 ROM[0],
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
301 ROM[1],
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
302 ROM[2],
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
303 ROM[3],
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
304 ROM[4],
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
305 ROM[5],
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
306 ROM[6],
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
307 ROM[7]);
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
308 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
309 } while (OWNext(ROM, 1, 0));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
310 #if 0
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
311
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
312 } else if (cmdbuf[0] == 'l' && cmdbuf[1] == 'e' && cmdbuf[2] == 'd') {
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
313 if (leds == 0) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
314 leds = 1;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
315 ledpos = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
316 outp(0, TCNT0);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
317 sbi(TIMSK, TOIE0);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
318 sei();
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
319 uart_putsP(PSTR("Starting\n\r"));
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
320 } else {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
321 leds = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
322 ledpos = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
323 PORTA = 0x00;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
324 cbi(TIMSK, TOIE0);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
325 cli();
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
326 uart_putsP(PSTR("Stopping\n\r"));
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
327 }
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
328 #endif
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
329 } else {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
330 badcmd:
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
331 uart_putsP(PSTR("Unknown command, ? for a list\n\r"));
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
332 }
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 return(0);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
337 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
338
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
339 int
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
340 uart_putc(char c) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
341 loop_until_bit_is_set(USR, UDRE);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
342 outp(c, UDR);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
343
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
344 return(0);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
345 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
346
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
347 void
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
348 uart_putsP(const char *addr) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
349 char c;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
350
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
351 while ((c = PRG_RDB((unsigned short)addr++)))
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
352 uart_putc(c);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
353 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
354
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
355 void
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
356 uart_puts(const char *addr) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
357 while (*addr)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
358 uart_putc(*addr++);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
359 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
360
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
361 int
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
362 uart_getc(void) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
363 while (!(inp(USR) & 0x80))
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
364 ;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
365
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
366 return (inp(UDR));
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
367 }
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
368