annotate testavr.c @ 9:7ed10c59ba06

Supply clock speed via the command line so 1wire-delay.h can use it.
author darius
date Mon, 12 Jul 2004 17:51:20 +0930
parents a940431af6f5
children eb1faf51968e
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 <stdio.h>
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
32 #include <avr/io.h>
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
33 #include <avr/interrupt.h>
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
34 #include <avr/signal.h>
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
35 #include <avr/pgmspace.h>
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
36 #include <string.h>
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
37 #include <ctype.h>
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
38
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
39 #include "1wire.h"
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
40
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
41 #define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1)
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
42 #define UART_BAUD_RATE 19200
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
43
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
44 void uart_putsP(const char *addr);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
45 void uart_puts(const char *addr);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
46 int uart_putc(char c);
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
47 int uart_getc(void);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
48 int beginswith_P(const char *s1, const char *s2);
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
49
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
50 #if 0
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
51 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
52 static uint8_t dir = 0;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
53 static volatile uint8_t leds = 0;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
54 static uint8_t ledpos = 0;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
55
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
56 INTERRUPT(SIG_OVERFLOW0) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
57 if (!leds)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
58 return;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
59
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
60 /* Going up */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
61 if (dir == 0) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
62 if (ledpos == 9) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
63 dir = 1;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
64 TCNT0 = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
65 goto doleds;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
66 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
67
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
68 ledpos++;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
69 } else {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
70 if (ledpos == 0) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
71 dir = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
72 TCNT0 = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
73 goto doleds;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
74 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
75
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
76 ledpos--;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
77 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
78
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
79 doleds:
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
80 TCNT0 = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
81
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
82 PORTA = pgm_read_byte(&ledvals[ledpos]);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
83 }
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
84 #endif
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
85
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
86 void
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
87 usleep(uint16_t usec) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
88 /* 4Mhz = 250ns per clock cycle */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
89 usec /= 2;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
90 if (usec < 1)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
91 return;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
92
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
93 while (usec--)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
94 asm volatile (
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
95 ""
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
96 ::);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
97 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
98
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
99 int
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
100 main(void) {
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
101 uint8_t ROM[8];
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
102 char cmdbuf[40];
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
103 int i;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
104
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
105 cli();
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
106 #if 0
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
107 outp(0xff, DDRA);
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
108
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
109 /* Timer Clock divisor - CK/1024 */
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
110 outp(BV(CS00) | BV(CS02), TCCR0);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
111 #endif
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
112 outp(0xfe, DDRC);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
113 outp(0x00, PORTC);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
114
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
115 /* Init UART */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
116 outp(UART_BAUD_SELECT(UART_BAUD_RATE,XTAL_CPU), UBRR);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
117
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
118 /* Enable receiver and transmitter. Turn on transmit interrupts */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
119 outp(BV(RXEN) | BV(TXEN), UCR);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
120
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
121 uart_putsP(PSTR("\n\r\n\r===============\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
122 "Inited!\n\r\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
123
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 uart_putsP(PSTR("> "));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
126 i = 0;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
127 while (1) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
128 cmdbuf[i] = tolower(uart_getc());
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
129 if (cmdbuf[i] == '\n' || cmdbuf[i] == '\r')
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
130 break;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
131
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
132 /* Backspace */
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
133 if (cmdbuf[i] == 010) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
134 if (i > 0) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
135 uart_putsP(PSTR("\010\040\010"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
136 i--;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
137 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
138 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
139 }
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
140
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
141 /* Anything unprintable just ignore it */
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
142 if (!isprint(cmdbuf[i]))
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
143 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
144
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
145 uart_putc(cmdbuf[i]);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
146 i++;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
147 if (i == sizeof(cmdbuf)) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
148 uart_putsP(PSTR("\n\rLine too long\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
149 i = 0;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
150 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
151 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
152 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
153 cmdbuf[i + 1] = '\0';
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
154 uart_putsP(PSTR("\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
155 if (i == 0)
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
156 continue;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
157
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
158 if (cmdbuf[0] == '?') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
159 uart_putsP(PSTR("rs Reset and check for presence\n\r"
7
a940431af6f5 Add usage for sr command.
darius
parents: 4
diff changeset
160 "sr Search the bus for ROMs\n\r"
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
161 "re Read a bit\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
162 "rb Read a byte\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
163 "wr bit Write a bit\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
164 "wb byte Write a byte (hex)\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
165 "wc cmd [ROMID] Write command\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
166 "te ROMID Read the temperature from a DS1820\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
167 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
168 }
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
169
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
170 if (i < 2)
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
171 goto badcmd;
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 (cmdbuf[0] == 'r' && cmdbuf[1] == 's') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
174 uart_putsP(PSTR("Resetting... "));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
175
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
176 if (OWTouchReset() == 1)
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
177 uart_putsP(PSTR("No presense\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
178 else
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
179 uart_putsP(PSTR("Presense\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
180 } else if (cmdbuf[0] == 'r' && cmdbuf[1] == 'e') {
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
181 if (OWReadBit())
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
182 uart_putsP(PSTR("Read a 1\n\r"));
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
183 else
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
184 uart_putsP(PSTR("Read a 0\n\r"));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
185 } else if (cmdbuf[0] == 'r' && cmdbuf[1] == 'b') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
186 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
187 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
188 } else if (cmdbuf[0] == 'w' && cmdbuf[1] == 'r') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
189 int arg;
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 if (sscanf_P(cmdbuf + 3, PSTR("%d"), &arg) != 1) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
192 uart_putsP(PSTR("Unable to parse wr command\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
193 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
194 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
195
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
196 OWWriteBit(arg);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
197 sprintf_P(cmdbuf, PSTR("Wrote a %d\n\r"), arg);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
198 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
199 } else if (cmdbuf[0] == 'w' && cmdbuf[1] == 'b') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
200 int arg;
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 if (sscanf_P(cmdbuf + 3, PSTR("%2x"), &arg) != 1) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
203 uart_putsP(PSTR("Unable to parse wb command\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
204 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
205 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
206
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
207 OWWriteByte(arg);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
208 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
209 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
210 } else if (cmdbuf[0] == 'w' && cmdbuf[1] == 'c') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
211 int arg;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
212
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
213 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
214 &ROM[0], &ROM[1], &ROM[2], &ROM[3],
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
215 &ROM[4], &ROM[5], &ROM[6], &ROM[7])) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
216 break;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
217
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
218 case 1:
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
219 OWSendCmd(NULL, arg);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
220 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
221 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
222 break;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
223
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
224 case 9:
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
225 OWSendCmd(ROM, arg);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
226 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
227 arg, ROM[0], ROM[1], ROM[2], ROM[3],
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
228 ROM[4], ROM[5], ROM[6], ROM[7]);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
229 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
230
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
231 break;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
232
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
233 case 0:
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
234 default:
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
235 uart_putsP(PSTR("Unable to parse wc command\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
236 break;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
237 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
238
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
239 OWWriteByte(arg);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
240 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
241 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
242 } else if (cmdbuf[0] == 't' && cmdbuf[1] == 'e') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
243 uint8_t crc;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
244 uint16_t temp;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
245 uint8_t buf[9];
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
246
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
247 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
248 &ROM[0], &ROM[1], &ROM[2], &ROM[3],
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
249 &ROM[4], &ROM[5], &ROM[6], &ROM[7]) != 8) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
250 uart_putsP(PSTR("Unable to parse ROM ID\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
251 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
252 }
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 if (ROM[0] != OW_FAMILY_TEMP) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
255 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
256 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
257 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
258
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
259 OWSendCmd(ROM, OW_CONVERTT_CMD);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
260 #if OW_DEBUG
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
261 uart_putsP(PSTR("Command sent, waiting\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
262 #endif
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
263 i = 0;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
264 while (OWReadBit() == 0) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
265 i++;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
266 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
267 #if OW_DEBUG
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
268 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
269 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
270 #endif
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
271 OWSendCmd(ROM, OW_RD_SCR_CMD);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
272 crc = 0;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
273 for (i = 0; i < 9; i++) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
274 buf[i] = OWReadByte();
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
275 if (i < 8)
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
276 OWCRC(buf[i], &crc);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
277 }
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 temp = buf[0];
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
280 temp |= (uint16_t)buf[1] << 8;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
281 temp <<= 3;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
282
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
283 if (crc != buf[8]) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
284 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
285 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
286 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
287 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
288
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
289 sprintf_P(cmdbuf, PSTR("temperature %d.%01d\n\r"),
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
290 temp >> 4, (temp << 12) / 6553);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
291 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
292 } else if (cmdbuf[0] == 's' && cmdbuf[1] == 'r') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
293 memset(ROM, 0, 8);
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
294 if (OWTouchReset()) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
295 uart_putsP(PSTR("No devices on bus\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 if (OWFirst(ROM, 1, 0) == 0) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
299 uart_putsP(PSTR("No module found\n\r"));
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
300 break;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
301 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
302 do {
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
303 sprintf_P(cmdbuf, PSTR("%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n\r"),
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
304 ROM[0],
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
305 ROM[1],
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
306 ROM[2],
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
307 ROM[3],
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
308 ROM[4],
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
309 ROM[5],
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
310 ROM[6],
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
311 ROM[7]);
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
312 uart_puts(cmdbuf);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
313 } while (OWNext(ROM, 1, 0));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
314 #if 0
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
315
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
316 } else if (cmdbuf[0] == 'l' && cmdbuf[1] == 'e' && cmdbuf[2] == 'd') {
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
317 if (leds == 0) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
318 leds = 1;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
319 ledpos = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
320 outp(0, TCNT0);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
321 sbi(TIMSK, TOIE0);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
322 sei();
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
323 uart_putsP(PSTR("Starting\n\r"));
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
324 } else {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
325 leds = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
326 ledpos = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
327 PORTA = 0x00;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
328 cbi(TIMSK, TOIE0);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
329 cli();
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
330 uart_putsP(PSTR("Stopping\n\r"));
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
331 }
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
332 #endif
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
333 } else {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
334 badcmd:
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
335 uart_putsP(PSTR("Unknown command, ? for a list\n\r"));
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
336 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
337
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
338 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
339
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
340 return(0);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
341 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
342
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
343 int
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
344 uart_putc(char c) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
345 loop_until_bit_is_set(USR, UDRE);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
346 outp(c, UDR);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
347
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
348 return(0);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
349 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
350
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
351 void
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
352 uart_putsP(const char *addr) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
353 char c;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
354
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
355 while ((c = PRG_RDB((unsigned short)addr++)))
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
356 uart_putc(c);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
357 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
358
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
359 void
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
360 uart_puts(const char *addr) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
361 while (*addr)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
362 uart_putc(*addr++);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
363 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
364
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
365 int
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
366 uart_getc(void) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
367 while (!(inp(USR) & 0x80))
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
368 ;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
369
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
370 return (inp(UDR));
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
371 }
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
372