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