annotate testavr.c @ 47:13a68734348b

Add watchdog timer.
author darius@Inchoate
date Mon, 20 Oct 2008 22:22:15 +1030
parents 5898fba6593c
children cb184206344d
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 *
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
4 * Copyright (c) 2008
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
5 * Daniel O'Connor <darius@dons.net.au>. All rights reserved.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
6 *
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
7 * Redistribution and use in source and binary forms, with or without
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
8 * modification, are permitted provided that the following conditions
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
9 * are met:
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
10 * 1. Redistributions of source code must retain the above copyright
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
11 * notice, this list of conditions and the following disclaimer.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
15 *
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
26 * SUCH DAMAGE.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
27 */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
28
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
29 #include <avr/io.h>
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
30 #include <avr/interrupt.h>
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
31 #include <avr/pgmspace.h>
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
32 #include <stdio.h>
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
33 #include <string.h>
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
34 #include <ctype.h>
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
35 #include <stdlib.h>
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
36 #include <util/delay.h>
47
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
37 #include <avr/wdt.h>
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
38
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
39 #include "cons.h"
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
40 #include "1wire.h"
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
41 #ifdef WITHUSB
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
42 #include "usb.h"
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
43 #endif
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
44 #include "tempctrl.h"
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
45
47
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
46 /*
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
47 * Mirror of the MCUCSR register, taken early during startup.
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
48 */
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
49 uint8_t mcucsr __attribute__((section(".noinit")));
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
50
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
51 void process_cmd(void);
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
52
47
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
53 /*
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
54 * Read out and reset MCUCSR early during startup.
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
55 */
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
56 void handle_mcucsr(void)
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
57 __attribute__((section(".init3")))
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
58 __attribute__((naked));
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
59 void handle_mcucsr(void) {
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
60 mcucsr = MCUCSR;
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
61 MCUCSR = 0;
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
62 }
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
63
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
64 int
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
65 main(void) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
66 /* Disable interrupts while we frob stuff */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
67 cli();
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
68
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
69 #if 0
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
70 /* Disable JTAG (yes twice) */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
71 MCUCSR |= _BV(JTD);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
72 MCUCSR |= _BV(JTD);
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
73 #endif
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
74
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
75 #ifdef WITHUSB
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
76 /* USB data bus (7:0) */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
77 DDRA = 0x00;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
78 PORTA = 0x00;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
79
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
80 /* USB control (4:0) */
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
81 DDRB = 0x0d;
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
82 PORTB = 0x00;
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
83 #else
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
84 DDRA = 0xff;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
85 PORTA = 0x00;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
86 #endif
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
87 /* GPIO (0:7) */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
88 DDRC = 0xff;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
89 PORTC = 0x00;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
90
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
91 /* USART (0:1), IDBus (2:5), 485 (6:6), GPIO (7:7) */
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
92 DDRD = 0xf7;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
93 PORTD = 0xf7;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
94
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
95 /* Set up the one wire stuff */
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
96 OWInit();
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
97
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
98 /* Init UART */
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
99 cons_init();
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
100
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
101 printf_P(PSTR("\r\n\r\n===============\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
102 "Inited!\r\n\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
103
47
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
104 if ((mcucsr & _BV(PORF)) == _BV(PORF))
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
105 printf_P(PSTR("Power on reset\r\n"));
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
106
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
107 if ((mcucsr & _BV(EXTRF)) == _BV(EXTRF))
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
108 printf_P(PSTR("External reset\r\n"));
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
109
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
110 if ((mcucsr & _BV(BORF)) == _BV(BORF))
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
111 printf_P(PSTR("Brown-out reset\r\n"));
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
112
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
113 if ((mcucsr & _BV(WDRF)) == _BV(WDRF))
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
114 printf_P(PSTR("Watchdog reset\r\n"));
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
115
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
116 if ((mcucsr & _BV(JTRF)) == _BV(JTRF))
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
117 printf_P(PSTR("JTAG reset\r\n"));
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
118
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
119 tempctrl_init();
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
120
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
121 /* Ready to go! */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
122 sei();
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
123
47
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
124 /*
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
125 * Enable the watchdog with the largest prescaler. Will cause a
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
126 * watchdog reset after approximately 2 s @ Vcc = 5 V
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
127 *
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
128 * Gets reset in the loop below and in the tempctrl.c timer IRQ
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
129 */
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
130 wdt_enable(WDTO_2S);
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
131
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
132 #ifdef WITHUSB
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
133 printf_P(PSTR("Calling usb_init\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
134 usb_init();
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
135 printf_P(PSTR("done\r\n"));
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
136 _delay_us(1000);
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
137 #endif
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
138 printf_P(PSTR("> "));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
139 cmd.state = 0;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
140
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
141 /* Wait for user input or an "interrupt" */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
142 while (1) {
47
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
143 wdt_reset();
13a68734348b Add watchdog timer.
darius@Inchoate
parents: 41
diff changeset
144
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
145 tempctrl_update();
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
146
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
147 if (cmd.state == 255) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
148 process_cmd();
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
149 printf_P(PSTR("> "));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
150 /* Allow new characters to be processed */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
151 cmd.state = 0;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
152 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
153
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
154 #ifdef WITHUSB
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
155 if (!(PDICTL & _BV(PDIINT)))
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
156 usb_intr();
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
157 #endif
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
158 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
159 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
160
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
161 void
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
162 process_cmd(void) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
163 uint8_t ROM[8];
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
164 int8_t i, arg;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
165 uint8_t crc, buf[9];
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
166 int8_t temp;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
167 uint16_t tfrac;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
168
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
169 /* User just pressed enter */
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
170 if (cmd.len == 0)
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
171 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
172
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
173 if (cmd.buf[0] == '?') {
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
174 printf_P(PSTR("rs Reset and check for presence\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
175 "sr Search the bus for ROMs\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
176 "re Read a bit\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
177 "rb Read a byte\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
178 "wr bit Write a bit\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
179 "wb byte Write a byte (hex)\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
180 "wc cmd [ROMID] Write command\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
181 "te ROMID Read the temperature from a DS1820\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
182 "in port Read from a port\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
183 "out port val Write to a port\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
184 "ddr port [val] Read/write DDR for a port\r\n"
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
185 "tc ... Temperature control related (tc help for more)\r\n"));
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
186
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
187 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
188 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
189
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
190 i = strlen((char *)cmd.buf);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
191 if (cmd.len < 2)
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
192 goto badcmd;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
193
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
194 if (cmd.buf[0] == 'r' && cmd.buf[1] == 's') {
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
195 printf_P(PSTR("Resetting... "));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
196
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
197 if (OWTouchReset() == 1)
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
198 printf_P(PSTR("No presence pulse found\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
199 else
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
200 printf_P(PSTR("Presence pulse found\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
201 } else if (cmd.buf[0] == 'r' && cmd.buf[1] == 'e') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
202 if (OWReadBit())
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
203 printf_P(PSTR("Read a 1\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
204 else
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
205 printf_P(PSTR("Read a 0\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
206 } else if (cmd.buf[0] == 'r' && cmd.buf[1] == 'b') {
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
207 printf_P(PSTR("Read a 0x%02x\r\n"), OWReadByte());
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
208 } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'r') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
209 arg = strtol((char *)cmd.buf + 3, (char **)NULL, 10);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
210 OWWriteBit(arg);
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
211 printf_P(PSTR("Wrote a %c\r\n"), arg ? '1' : '0');
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
212 } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'b') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
213 arg = (int)strtol((char *)cmd.buf + 3, (char **)NULL, 16);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
214 OWWriteByte(arg);
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
215 } else if (cmd.buf[0] == 'r' && cmd.buf[1] == 't') {
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
216 if (cmd.len < 26) {
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
217 printf_P(PSTR("Unable to parse ROM ID\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
218 return;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
219 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
220
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
221 if (OWTouchReset() != 0) {
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
222 printf_P(PSTR("No presence\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
223 return;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
224 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
225
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
226 for (i = 0; i < 8; i++)
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
227 ROM[i] = (int)strtol((char *)cmd.buf + 3 * (i + 1), (char **)NULL, 16);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
228
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
229 if (ROM[0] != OW_FAMILY_ROM) {
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
230 printf_P(PSTR("ROM specified isn't a DS2502\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
231 return;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
232 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
233
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
234 if (OWTouchReset() != 0) {
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
235 printf_P(PSTR("No presence\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
236 return;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
237 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
238
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
239 crc = 0;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
240
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
241 OWCRC(OW_READ_STATUS, &crc);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
242 OWSendCmd(ROM, OW_READ_STATUS);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
243
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
244 OWWriteByte(0x00);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
245 OWCRC(0x00, &crc);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
246
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
247 OWWriteByte(0x00);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
248 OWCRC(0x00, &crc);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
249
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
250 if (crc != OWReadByte()) {
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
251 printf_P(PSTR("CRC mismatch on command & address\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
252 return;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
253 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
254
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
255 crc = 0;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
256 for (i = 0; i < 8; i++) {
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
257 temp = OWReadByte();
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
258 printf_P(PSTR("%02x "), temp);
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
259 OWCRC(temp, &crc);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
260 }
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
261 printf_P(PSTR("\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
262 if (crc != OWReadByte()) {
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
263 printf_P(PSTR("CRC mismatch on data\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
264 return;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
265 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
266 } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'e') {
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
267 if (cmd.len < 26) {
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
268 printf_P(PSTR("Unable to parse ROM ID\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
269 return;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
270 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
271
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
272 for (i = 0; i < 8; i++)
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
273 ROM[i] = (int)strtol((char *)cmd.buf + 3 * (i + 1), (char **)NULL, 16);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
274
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
275 if (ROM[0] != OW_FAMILY_ROM) {
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
276 printf_P(PSTR("ROM specified isn't a ROM\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
277 return;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
278 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
279
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
280 buf[0] = (int)strtol((char *)cmd.buf + 27, (char **)NULL, 16); /* Address */
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
281 buf[1] = (int)strtol((char *)cmd.buf + 30, (char **)NULL, 16); /* Data .. */
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
282 buf[2] = (int)strtol((char *)cmd.buf + 33, (char **)NULL, 16);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
283
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
284 if (OWTouchReset() != 0) {
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
285 printf_P(PSTR("No presence\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
286 return;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
287 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
288
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
289 i = OWProgROM(ROM, buf[0], 2, &buf[1], 0, 0);
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
290 printf_P(PSTR("OWProgROM returned %d\r\n"), i);
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
291 } else if (cmd.buf[0] == 'r' && cmd.buf[1] == 'r') {
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
292 if (cmd.len < 26) {
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
293 printf_P(PSTR("Unable to parse ROM ID\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
294 return;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
295 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
296
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
297 for (i = 0; i < 8; i++)
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
298 ROM[i] = (int)strtol((char *)cmd.buf + 3 * (i + 1), (char **)NULL, 16);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
299
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
300 if (ROM[0] != OW_FAMILY_ROM) {
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
301 printf_P(PSTR("ROM specified isn't a ROM\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
302 return;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
303 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
304
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
305 crc = 0;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
306 OWSendCmd(ROM, OW_READ_MEMORY);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
307 OWCRC(OW_READ_MEMORY, &crc);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
308
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
309 OWWriteByte(0x00);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
310 OWCRC(0x00, &crc);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
311
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
312 OWWriteByte(0x00);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
313 OWCRC(0x00, &crc);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
314
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
315 if (crc != OWReadByte()) {
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
316 printf_P(PSTR("CRC mismatch on command & address\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
317 return;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
318 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
319
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
320 crc = 0;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
321 for (buf[0] = 0; buf[0] < 128; buf[0]++) {
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
322 buf[1] = OWReadByte();
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
323 if (buf[0] > 0) {
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
324 if (buf[0] % 16 != 0)
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
325 printf_P(PSTR(" "));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
326 else
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
327 printf_P(PSTR("\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
328 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
329
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
330 printf_P(PSTR("%02x"), buf[1]);
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
331 OWCRC(buf[1], &crc);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
332 }
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
333 printf_P(PSTR("\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
334 if (crc != OWReadByte()) {
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
335 printf_P(PSTR("CRC mismatch on data\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
336 return;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
337 }
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
338
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
339 } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'c') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
340 if (cmd.len < 5) {
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
341 printf_P(PSTR("No arguments\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
342 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
343 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
344
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
345 arg = (int)strtol((char *)cmd.buf + 3, (char **)NULL, 16);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
346 if (arg == 0) {
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
347 printf_P(PSTR("Unparseable command\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
348 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
349 }
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
350
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
351 if (i == 5) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
352 OWSendCmd(NULL, arg);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
353 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
354 }
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
355
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
356 if (i < 29) {
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
357 printf_P(PSTR("Can't parse ROM ID\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
358 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
359 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
360 for (i = 0; i < 8; i++)
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
361 ROM[i] = (int)strtol((char *)cmd.buf + 6 + (3 * i), (char **)NULL, 16);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
362
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
363 OWSendCmd(ROM, arg);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
364 } else if (cmd.buf[0] == 't' && cmd.buf[1] == 'e') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
365 if (cmd.len < 26) {
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
366 printf_P(PSTR("Unable to parse ROM ID\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
367 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
368 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
369
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
370 for (i = 0; i < 8; i++)
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
371 ROM[i] = (int)strtol((char *)cmd.buf + 3 * (i + 1), (char **)NULL, 16);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
372
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
373 if (ROM[0] != OW_FAMILY_TEMP) {
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
374 printf_P(PSTR("ROM specified isn't a temperature sensor\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
375 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
376 }
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
377
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
378 OWSendCmd(ROM, OW_CONVERTT_CMD);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
379 i = 0;
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
380 /* Wait for the conversion */
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
381 while (OWReadBit() == 0)
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
382 i = 1;
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
383
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
384
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
385 OWSendCmd(ROM, OW_RD_SCR_CMD);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
386 crc = 0;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
387 for (i = 0; i < 9; i++) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
388 buf[i] = OWReadByte();
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
389 if (i < 8)
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
390 OWCRC(buf[i], &crc);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
391 }
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
392
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
393 if (crc != buf[8]) {
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
394 printf_P(PSTR("CRC mismatch\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
395 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
396 }
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
397
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
398 /* 0 Temperature LSB
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
399 * 1 Temperature MSB
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
400 * 2 Th
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
401 * 3 Tl
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
402 * 4 Reserved
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
403 * 5 Reserved
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
404 * 6 Count Remain
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
405 * 7 Count per C
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
406 * 8 CRC
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
407 */
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
408 #if 0
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
409 for (i = 0; i < 9; i++)
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
410 printf_P(PSTR("%d\r\n"), buf[i]);
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
411 #endif
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
412 temp = buf[0];
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
413 if (buf[1] & 0x80)
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
414 temp -= 256;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
415 temp >>= 1;
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
416
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
417 tfrac = buf[7] - buf[6];
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
418 tfrac *= (uint16_t)100;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
419 tfrac /= buf[7];
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
420 tfrac += 75;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
421 if (tfrac < 100) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
422 temp--;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
423 } else {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
424 tfrac -= 100;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
425 }
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
426
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
427 printf_P(PSTR("%d.%02d\r\n"), temp, tfrac);
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
428 } else if (cmd.buf[0] == 's' && cmd.buf[1] == 'r') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
429 memset(ROM, 0, 8);
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
430
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
431 i = OWFirst(ROM, 1, 0);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
432 do {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
433 switch (i) {
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
434 case OW_BADWIRE:
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
435 printf_P(PSTR("Presence pulse, but no module found, bad module/cabling?\r\n"));
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
436 break;
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
437
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
438 case OW_NOPRESENCE:
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
439 printf_P(PSTR("No presence pulse found\r\n"));
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
440 break;
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
441
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
442 case OW_BADCRC:
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
443 printf_P(PSTR("Bad CRC\r\n"));
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
444 break;
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
445
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
446 case OW_NOMODULES:
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
447 case OW_FOUND:
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
448 break;
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
449
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
450 default:
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
451 printf_P(PSTR("Unknown error from 1 wire library\r\n"));
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
452 break;
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
453 }
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
454
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
455 if (i != OW_FOUND)
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
456 break;
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
457
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
458 for (i = 0; i < 8; i++)
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
459 printf_P(PSTR("%02x%S"), ROM[i], i == 7 ? PSTR("\r\n") : PSTR(":"));
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
460
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
461 i = OWNext(ROM, 1, 0);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
462 } while (1);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
463 } else if (cmd.buf[0] == 'i' && cmd.buf[1] == 'n') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
464 switch (tolower(cmd.buf[3])) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
465 case 'a':
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
466 crc = PINA;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
467 break;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
468
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
469 case 'b':
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
470 crc = PINB;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
471 break;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
472
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
473 case 'c':
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
474 crc = PINC;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
475 break;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
476
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
477 case 'd':
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
478 crc = PIND;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
479 break;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
480
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
481 default:
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
482 printf_P(PSTR("Unknown port\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
483 return;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
484 }
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
485 printf_P(PSTR("0x%02x\r\n"), crc);
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
486 } else if (cmd.buf[0] == 'o' && cmd.buf[1] == 'u') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
487 crc = strtol((char *)cmd.buf + 8, (char **)NULL, 16);
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
488 switch (tolower(cmd.buf[4])) {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
489 case 'a':
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
490 PORTA = crc;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
491 break;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
492
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
493 case 'b':
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
494 PORTB = crc;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
495 break;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
496
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
497 case 'c':
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
498 PORTC = crc;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
499 break;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
500
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
501 case 'd':
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
502 PORTD = crc;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
503 break;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
504
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
505 default:
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
506 printf_P(PSTR("Unknown port\r\n"));
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
507 return;
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
508 }
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
509 printf_P(PSTR("PORT%c <= 0x%02x\r\n"), toupper(cmd.buf[4]), crc);
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
510 } else if (cmd.buf[0] == 'd' && cmd.buf[1] == 'd') {
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
511 crc = strtol((char *)cmd.buf + 8, (char **)NULL, 16);
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
512 switch (tolower(cmd.buf[4])) {
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
513 case 'a':
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
514 DDRA = crc;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
515 break;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
516
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
517 case 'b':
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
518 DDRB = crc;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
519 break;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
520
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
521 case 'c':
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
522 DDRC = crc;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
523 break;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
524
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
525 case 'd':
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
526 DDRD = crc;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
527 break;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
528
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
529 default:
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
530 printf_P(PSTR("Unknown port\r\n"));
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
531 return;
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
532 }
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
533 printf_P(PSTR("0x%02x\r\n"), crc);
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
534 } else if (cmd.buf[0] == 't' && cmd.buf[1] == 'c') {
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
535 tempctrl_cmd((char *)cmd.buf);
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
536 #ifdef WITHUSB
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
537 } else if (cmd.buf[0] == 'u' && cmd.buf[1] == 's') {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
538 usb_gendata();
37
25fa387ef7e9 - Conditionalise USB support.
darius
parents: 30
diff changeset
539 #endif
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
540 } else {
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
541 badcmd:
41
5898fba6593c Add temperature control.
darius@inchoate.localdomain
parents: 38
diff changeset
542 printf_P(PSTR("Unknown command, ? for a list\r\n"));
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
543 }
24
2b0ed085b95b - Change to 38400 baud.
darius
parents: 18
diff changeset
544 }
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
545
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
546