comparison testavr.c @ 41:5898fba6593c

Add temperature control. - Split out console stuff to cons.[ch]. Set up stdio so we can use printf etc. - Use \r\n as the line terminator consistently. - Add OWGetTemp to get temperatures from a device. - Load/save settings in EEPROM, defaults loaded from flash. Nearly feature complete except you can't edit ROM IDs without a programming tool :) (To be fixed) Needs more testing.
author darius@inchoate.localdomain
date Sun, 06 Jul 2008 22:19:53 +0930
parents 0a148f362097
children 13a68734348b
comparison
equal deleted inserted replaced
40:1061fdbdc44f 41:5898fba6593c
1 /* 1 /*
2 * Test various AVR bits and pieces 2 * Test various AVR bits and pieces
3 * 3 *
4 * $Id$ 4 * Copyright (c) 2008
5 *
6 * Copyright (c) 2004
7 * Daniel O'Connor <darius@dons.net.au>. All rights reserved. 5 * Daniel O'Connor <darius@dons.net.au>. All rights reserved.
8 * 6 *
9 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
11 * are met: 9 * are met:
29 */ 27 */
30 28
31 #include <avr/io.h> 29 #include <avr/io.h>
32 #include <avr/interrupt.h> 30 #include <avr/interrupt.h>
33 #include <avr/pgmspace.h> 31 #include <avr/pgmspace.h>
32 #include <stdio.h>
34 #include <string.h> 33 #include <string.h>
35 #include <ctype.h> 34 #include <ctype.h>
36 #include <stdlib.h> 35 #include <stdlib.h>
37 #include <util/delay.h> 36 #include <util/delay.h>
38 37
38 #include "cons.h"
39 #include "1wire.h" 39 #include "1wire.h"
40 #ifdef WITHUSB 40 #ifdef WITHUSB
41 #include "usb.h" 41 #include "usb.h"
42 #endif 42 #endif
43 43 #include "tempctrl.h"
44 #define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1)
45 #define UART_BAUD_RATE 38400
46 44
47 void process_cmd(void); 45 void process_cmd(void);
48 46
49 void uart_putsP(const char *addr);
50 void uart_puts(const char *addr);
51 int uart_putc(char c);
52 void uart_puts_dec(uint8_t a, uint8_t l);
53 void uart_puts_hex(uint8_t a);
54 char uart_getc(void);
55
56 /* Receive buffer storage */
57 volatile struct {
58 char buf[40];
59 uint8_t state;
60 uint8_t len;
61 } cmd = {
62 .state = 255,
63 .len = 0
64 };
65
66 /* Rx complete */
67 ISR(USART_RXC_vect) {
68 volatile char pit;
69 char c;
70
71 while (UCSRA & _BV(RXC)) {
72 /* 255 means we're waiting for main to process the command,
73 just throw stuff away
74 */
75 if (cmd.state == 255) {
76 pit = UDR;
77 continue;
78 }
79 c = UDR;
80
81 /* End of line? */
82 if (c == '\n' || c == '\r') {
83 cmd.buf[cmd.state + 1] = '\0';
84 uart_putsP(PSTR("\r\n"));
85 cmd.len = cmd.state;
86 cmd.state = 255;
87 continue;
88 }
89
90 /* Backspace/delete */
91 if (c == 0x08 || c == 0x7f) {
92 if (cmd.state > 0) {
93 cmd.state--;
94 uart_putsP(PSTR("\010\040\010"));
95 }
96 continue;
97 }
98
99 /* Anything unprintable just ignore it */
100 if (!isprint(c))
101 continue;
102
103 cmd.buf[cmd.state] = tolower(c);
104
105 /* Echo back to the user */
106 uart_putc(cmd.buf[cmd.state]);
107
108 cmd.state++;
109 /* Over flow? */
110 if (cmd.state == ((sizeof(cmd.buf) / sizeof(cmd.buf[0])) - 1)) {
111 uart_putsP(PSTR("\r\nLine too long"));
112 cmd.state = 0;
113 continue;
114 }
115 }
116 }
117
118 /* Tx complete */
119 ISR(USART_TXC_vect) {
120
121 }
122
123 int 47 int
124 main(void) { 48 main(void) {
125 /* Disable interrupts while we frob stuff */ 49 /* Disable interrupts while we frob stuff */
126 cli(); 50 cli();
127 51
128 #if 1 52 #if 0
129 /* Disable JTAG (yes twice) */ 53 /* Disable JTAG (yes twice) */
130 MCUCSR |= _BV(JTD); 54 MCUCSR |= _BV(JTD);
131 MCUCSR |= _BV(JTD); 55 MCUCSR |= _BV(JTD);
132 #endif 56 #endif
133 57
153 77
154 /* Set up the one wire stuff */ 78 /* Set up the one wire stuff */
155 OWInit(); 79 OWInit();
156 80
157 /* Init UART */ 81 /* Init UART */
158 UBRRH = UART_BAUD_SELECT(UART_BAUD_RATE, F_CPU) >> 8; 82 cons_init();
159 UBRRL = (uint8_t)UART_BAUD_SELECT(UART_BAUD_RATE, F_CPU);
160 83
161 /* Enable receiver and transmitter. Turn on transmit interrupts */ 84 printf_P(PSTR("\r\n\r\n===============\r\n"
162 UCSRA = 0; 85 "Inited!\r\n\r\n"));
163 UCSRB = _BV(RXEN) | _BV(TXEN) | _BV(RXCIE); 86
164 UCSRC = _BV(URSEL) | _BV(UCSZ1) | _BV(UCSZ0); 87 tempctrl_init();
165 uart_putsP(PSTR("\r\n\r\n===============\r\n" 88
166 "Inited!\r\n\r\n"));
167
168 /* Ready to go! */ 89 /* Ready to go! */
169 sei(); 90 sei();
170 91
171 #if 0 92 #if 0
172 DDRA = 0xff; 93 DDRA = 0xff;
173 DDRC = 0xff; 94 DDRC = 0xff;
174 while (1) { 95 while (1) {
175 uart_putsP(PSTR("1\r\n")); 96 printf_P(PSTR("1\r\n"));
176 PORTA = 0xff; 97 PORTA = 0xff;
177 uart_putsP(PSTR("2\r\n")); 98 printf_P(PSTR("2\r\n"));
178 PORTC = 0x00; 99 PORTC = 0x00;
179 uart_putsP(PSTR("3\r\n")); 100 printf_P(PSTR("3\r\n"));
180 _delay_us(1); 101 _delay_us(1);
181 uart_putsP(PSTR("4\r\n")); 102 printf_P(PSTR("4\r\n"));
182 PORTA = 0x80; 103 PORTA = 0x80;
183 uart_putsP(PSTR("5\r\n")); 104 printf_P(PSTR("5\r\n"));
184 PORTC = 0xff; 105 PORTC = 0xff;
185 uart_putsP(PSTR("6\r\n")); 106 printf_P(PSTR("6\r\n"));
186 } 107 }
187 #endif 108 #endif
188 109
189 #ifdef WITHUSB 110 #ifdef WITHUSB
190 uart_putsP(PSTR("Calling usb_init\r\n")); 111 printf_P(PSTR("Calling usb_init\r\n"));
191 usb_init(); 112 usb_init();
113 printf_P(PSTR("done\r\n"));
114 _delay_us(1000);
192 #endif 115 #endif
193 uart_putsP(PSTR("done\r\n")); 116 printf_P(PSTR("> "));
194 _delay_us(1000);
195 uart_putsP(PSTR("> "));
196 cmd.state = 0; 117 cmd.state = 0;
197 118
198 /* Wait for user input or an "interrupt" */ 119 /* Wait for user input or an "interrupt" */
199 while (1) { 120 while (1) {
121 tempctrl_update();
122
200 if (cmd.state == 255) { 123 if (cmd.state == 255) {
201 process_cmd(); 124 process_cmd();
202 uart_putsP(PSTR("> ")); 125 printf_P(PSTR("> "));
203 /* Allow new characters to be processed */ 126 /* Allow new characters to be processed */
204 cmd.state = 0; 127 cmd.state = 0;
205 } 128 }
206 129
207 #ifdef WITHUSB 130 #ifdef WITHUSB
222 /* User just pressed enter */ 145 /* User just pressed enter */
223 if (cmd.len == 0) 146 if (cmd.len == 0)
224 return; 147 return;
225 148
226 if (cmd.buf[0] == '?') { 149 if (cmd.buf[0] == '?') {
227 uart_putsP(PSTR("rs Reset and check for presence\r\n" 150 printf_P(PSTR("rs Reset and check for presence\r\n"
228 "sr Search the bus for ROMs\r\n" 151 "sr Search the bus for ROMs\r\n"
229 "re Read a bit\r\n" 152 "re Read a bit\r\n"
230 "rb Read a byte\r\n" 153 "rb Read a byte\r\n"
231 "wr bit Write a bit\r\n" 154 "wr bit Write a bit\r\n"
232 "wb byte Write a byte (hex)\r\n" 155 "wb byte Write a byte (hex)\r\n"
233 "wc cmd [ROMID] Write command\r\n" 156 "wc cmd [ROMID] Write command\r\n"
234 "te ROMID Read the temperature from a DS1820\r\n" 157 "te ROMID Read the temperature from a DS1820\r\n"
235 "in port Read from a port\r\n" 158 "in port Read from a port\r\n"
236 "out port val Write to a port\r\n" 159 "out port val Write to a port\r\n"
237 "ddr port [val] Read/write DDR for a port\r\n")); 160 "ddr port [val] Read/write DDR for a port\r\n"
238 161 "tc ... Temperature control related (tc help for more)\r\n"));
162
239 return; 163 return;
240 } 164 }
241 165
242 i = strlen((char *)cmd.buf); 166 i = strlen((char *)cmd.buf);
243 if (cmd.len < 2) 167 if (cmd.len < 2)
244 goto badcmd; 168 goto badcmd;
245 169
246 if (cmd.buf[0] == 'r' && cmd.buf[1] == 's') { 170 if (cmd.buf[0] == 'r' && cmd.buf[1] == 's') {
247 uart_putsP(PSTR("Resetting... ")); 171 printf_P(PSTR("Resetting... "));
248 172
249 if (OWTouchReset() == 1) 173 if (OWTouchReset() == 1)
250 uart_putsP(PSTR("No presence pulse found\r\n")); 174 printf_P(PSTR("No presence pulse found\r\n"));
251 else 175 else
252 uart_putsP(PSTR("Presence pulse found\r\n")); 176 printf_P(PSTR("Presence pulse found\r\n"));
253 } else if (cmd.buf[0] == 'r' && cmd.buf[1] == 'e') { 177 } else if (cmd.buf[0] == 'r' && cmd.buf[1] == 'e') {
254 if (OWReadBit()) 178 if (OWReadBit())
255 uart_putsP(PSTR("Read a 1\r\n")); 179 printf_P(PSTR("Read a 1\r\n"));
256 else 180 else
257 uart_putsP(PSTR("Read a 0\r\n")); 181 printf_P(PSTR("Read a 0\r\n"));
258 } else if (cmd.buf[0] == 'r' && cmd.buf[1] == 'b') { 182 } else if (cmd.buf[0] == 'r' && cmd.buf[1] == 'b') {
259 uart_putsP(PSTR("Read a 0x")); 183 printf_P(PSTR("Read a 0x%02x\r\n"), OWReadByte());
260 uart_puts_hex(OWReadByte());
261 uart_putsP(PSTR("\r\n"));
262 } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'r') { 184 } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'r') {
263 arg = strtol((char *)cmd.buf + 3, (char **)NULL, 10); 185 arg = strtol((char *)cmd.buf + 3, (char **)NULL, 10);
264 OWWriteBit(arg); 186 OWWriteBit(arg);
265 uart_putsP(PSTR("Wrote a ")); 187 printf_P(PSTR("Wrote a %c\r\n"), arg ? '1' : '0');
266 if (arg)
267 uart_putsP(PSTR("1\r\n"));
268 else
269 uart_putsP(PSTR("0\r\n"));
270 } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'b') { 188 } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'b') {
271 arg = (int)strtol((char *)cmd.buf + 3, (char **)NULL, 16); 189 arg = (int)strtol((char *)cmd.buf + 3, (char **)NULL, 16);
272 OWWriteByte(arg); 190 OWWriteByte(arg);
273 } else if (cmd.buf[0] == 'r' && cmd.buf[1] == 't') { 191 } else if (cmd.buf[0] == 'r' && cmd.buf[1] == 't') {
274 if (cmd.len < 26) { 192 if (cmd.len < 26) {
275 uart_putsP(PSTR("Unable to parse ROM ID\r\n")); 193 printf_P(PSTR("Unable to parse ROM ID\r\n"));
276 return; 194 return;
277 } 195 }
278 196
279 if (OWTouchReset() != 0) { 197 if (OWTouchReset() != 0) {
280 uart_putsP(PSTR("No presence\r\n")); 198 printf_P(PSTR("No presence\r\n"));
281 return; 199 return;
282 } 200 }
283 201
284 for (i = 0; i < 8; i++) 202 for (i = 0; i < 8; i++)
285 ROM[i] = (int)strtol((char *)cmd.buf + 3 * (i + 1), (char **)NULL, 16); 203 ROM[i] = (int)strtol((char *)cmd.buf + 3 * (i + 1), (char **)NULL, 16);
286 204
287 if (ROM[0] != OW_FAMILY_ROM) { 205 if (ROM[0] != OW_FAMILY_ROM) {
288 uart_putsP(PSTR("ROM specified isn't a DS2502\r\n")); 206 printf_P(PSTR("ROM specified isn't a DS2502\r\n"));
289 return; 207 return;
290 } 208 }
291 209
292 if (OWTouchReset() != 0) { 210 if (OWTouchReset() != 0) {
293 uart_putsP(PSTR("No presence\r\n")); 211 printf_P(PSTR("No presence\r\n"));
294 return; 212 return;
295 } 213 }
296 214
297 crc = 0; 215 crc = 0;
298 216
304 222
305 OWWriteByte(0x00); 223 OWWriteByte(0x00);
306 OWCRC(0x00, &crc); 224 OWCRC(0x00, &crc);
307 225
308 if (crc != OWReadByte()) { 226 if (crc != OWReadByte()) {
309 uart_putsP(PSTR("CRC mismatch on command & address\r\n")); 227 printf_P(PSTR("CRC mismatch on command & address\r\n"));
310 return; 228 return;
311 } 229 }
312 230
313 crc = 0; 231 crc = 0;
314 for (i = 0; i < 8; i++) { 232 for (i = 0; i < 8; i++) {
315 temp = OWReadByte(); 233 temp = OWReadByte();
316 uart_puts_hex(temp); 234 printf_P(PSTR("%02x "), temp);
317 OWCRC(temp, &crc); 235 OWCRC(temp, &crc);
318 uart_putsP(PSTR(" ")); 236 }
319 } 237 printf_P(PSTR("\r\n"));
320 uart_putsP(PSTR("\r\n"));
321 if (crc != OWReadByte()) { 238 if (crc != OWReadByte()) {
322 uart_putsP(PSTR("CRC mismatch on data\r\n")); 239 printf_P(PSTR("CRC mismatch on data\r\n"));
323 return; 240 return;
324 } 241 }
325 } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'e') { 242 } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'e') {
326 if (cmd.len < 26) { 243 if (cmd.len < 26) {
327 uart_putsP(PSTR("Unable to parse ROM ID\r\n")); 244 printf_P(PSTR("Unable to parse ROM ID\r\n"));
328 return; 245 return;
329 } 246 }
330 247
331 for (i = 0; i < 8; i++) 248 for (i = 0; i < 8; i++)
332 ROM[i] = (int)strtol((char *)cmd.buf + 3 * (i + 1), (char **)NULL, 16); 249 ROM[i] = (int)strtol((char *)cmd.buf + 3 * (i + 1), (char **)NULL, 16);
333 250
334 if (ROM[0] != OW_FAMILY_ROM) { 251 if (ROM[0] != OW_FAMILY_ROM) {
335 uart_putsP(PSTR("ROM specified isn't a ROM\r\n")); 252 printf_P(PSTR("ROM specified isn't a ROM\r\n"));
336 return; 253 return;
337 } 254 }
338 255
339 buf[0] = (int)strtol((char *)cmd.buf + 27, (char **)NULL, 16); /* Address */ 256 buf[0] = (int)strtol((char *)cmd.buf + 27, (char **)NULL, 16); /* Address */
340 buf[1] = (int)strtol((char *)cmd.buf + 30, (char **)NULL, 16); /* Data .. */ 257 buf[1] = (int)strtol((char *)cmd.buf + 30, (char **)NULL, 16); /* Data .. */
341 buf[2] = (int)strtol((char *)cmd.buf + 33, (char **)NULL, 16); 258 buf[2] = (int)strtol((char *)cmd.buf + 33, (char **)NULL, 16);
342 259
343 if (OWTouchReset() != 0) { 260 if (OWTouchReset() != 0) {
344 uart_putsP(PSTR("No presence\r\n")); 261 printf_P(PSTR("No presence\r\n"));
345 return; 262 return;
346 } 263 }
347 264
348 i = OWProgROM(ROM, buf[0], 2, &buf[1], 0, 0); 265 i = OWProgROM(ROM, buf[0], 2, &buf[1], 0, 0);
349 uart_putsP(PSTR("OWProgROM returned ")); 266 printf_P(PSTR("OWProgROM returned %d\r\n"), i);
350 uart_puts_dec(i, 0);
351 uart_putsP(PSTR("\r\n"));
352 } else if (cmd.buf[0] == 'r' && cmd.buf[1] == 'r') { 267 } else if (cmd.buf[0] == 'r' && cmd.buf[1] == 'r') {
353 if (cmd.len < 26) { 268 if (cmd.len < 26) {
354 uart_putsP(PSTR("Unable to parse ROM ID\r\n")); 269 printf_P(PSTR("Unable to parse ROM ID\r\n"));
355 return; 270 return;
356 } 271 }
357 272
358 for (i = 0; i < 8; i++) 273 for (i = 0; i < 8; i++)
359 ROM[i] = (int)strtol((char *)cmd.buf + 3 * (i + 1), (char **)NULL, 16); 274 ROM[i] = (int)strtol((char *)cmd.buf + 3 * (i + 1), (char **)NULL, 16);
360 275
361 if (ROM[0] != OW_FAMILY_ROM) { 276 if (ROM[0] != OW_FAMILY_ROM) {
362 uart_putsP(PSTR("ROM specified isn't a ROM\r\n")); 277 printf_P(PSTR("ROM specified isn't a ROM\r\n"));
363 return; 278 return;
364 } 279 }
365 280
366 crc = 0; 281 crc = 0;
367 OWSendCmd(ROM, OW_READ_MEMORY); 282 OWSendCmd(ROM, OW_READ_MEMORY);
372 287
373 OWWriteByte(0x00); 288 OWWriteByte(0x00);
374 OWCRC(0x00, &crc); 289 OWCRC(0x00, &crc);
375 290
376 if (crc != OWReadByte()) { 291 if (crc != OWReadByte()) {
377 uart_putsP(PSTR("CRC mismatch on command & address\r\n")); 292 printf_P(PSTR("CRC mismatch on command & address\r\n"));
378 return; 293 return;
379 } 294 }
380 295
381 crc = 0; 296 crc = 0;
382 for (buf[0] = 0; buf[0] < 128; buf[0]++) { 297 for (buf[0] = 0; buf[0] < 128; buf[0]++) {
383 buf[1] = OWReadByte(); 298 buf[1] = OWReadByte();
384 if (buf[0] > 0) { 299 if (buf[0] > 0) {
385 if (buf[0] % 16 != 0) 300 if (buf[0] % 16 != 0)
386 uart_putsP(PSTR(" ")); 301 printf_P(PSTR(" "));
387 else 302 else
388 uart_putsP(PSTR("\r\n")); 303 printf_P(PSTR("\r\n"));
389 } 304 }
390 305
391 uart_puts_hex(buf[1]); 306 printf_P(PSTR("%02x"), buf[1]);
392 OWCRC(buf[1], &crc); 307 OWCRC(buf[1], &crc);
393 } 308 }
394 uart_putsP(PSTR("\r\n")); 309 printf_P(PSTR("\r\n"));
395 if (crc != OWReadByte()) { 310 if (crc != OWReadByte()) {
396 uart_putsP(PSTR("CRC mismatch on data\r\n")); 311 printf_P(PSTR("CRC mismatch on data\r\n"));
397 return; 312 return;
398 } 313 }
399 314
400 } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'c') { 315 } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'c') {
401 if (cmd.len < 5) { 316 if (cmd.len < 5) {
402 uart_putsP(PSTR("No arguments\r\n")); 317 printf_P(PSTR("No arguments\r\n"));
403 return; 318 return;
404 } 319 }
405 320
406 arg = (int)strtol((char *)cmd.buf + 3, (char **)NULL, 16); 321 arg = (int)strtol((char *)cmd.buf + 3, (char **)NULL, 16);
407 if (arg == 0) { 322 if (arg == 0) {
408 uart_putsP(PSTR("Unparseable command\r\n")); 323 printf_P(PSTR("Unparseable command\r\n"));
409 return; 324 return;
410 } 325 }
411 326
412 if (i == 5) { 327 if (i == 5) {
413 OWSendCmd(NULL, arg); 328 OWSendCmd(NULL, arg);
414 return; 329 return;
415 } 330 }
416 331
417 if (i < 29) { 332 if (i < 29) {
418 uart_putsP(PSTR("Can't parse ROM ID\r\n")); 333 printf_P(PSTR("Can't parse ROM ID\r\n"));
419 return; 334 return;
420 } 335 }
421 for (i = 0; i < 8; i++) 336 for (i = 0; i < 8; i++)
422 ROM[i] = (int)strtol((char *)cmd.buf + 6 + (3 * i), (char **)NULL, 16); 337 ROM[i] = (int)strtol((char *)cmd.buf + 6 + (3 * i), (char **)NULL, 16);
423 338
424 OWSendCmd(ROM, arg); 339 OWSendCmd(ROM, arg);
425 } else if (cmd.buf[0] == 't' && cmd.buf[1] == 'e') { 340 } else if (cmd.buf[0] == 't' && cmd.buf[1] == 'e') {
426 if (cmd.len < 26) { 341 if (cmd.len < 26) {
427 uart_putsP(PSTR("Unable to parse ROM ID\r\n")); 342 printf_P(PSTR("Unable to parse ROM ID\r\n"));
428 return; 343 return;
429 } 344 }
430 345
431 for (i = 0; i < 8; i++) 346 for (i = 0; i < 8; i++)
432 ROM[i] = (int)strtol((char *)cmd.buf + 3 * (i + 1), (char **)NULL, 16); 347 ROM[i] = (int)strtol((char *)cmd.buf + 3 * (i + 1), (char **)NULL, 16);
433 348
434 if (ROM[0] != OW_FAMILY_TEMP) { 349 if (ROM[0] != OW_FAMILY_TEMP) {
435 uart_putsP(PSTR("ROM specified isn't a temperature sensor\r\n")); 350 printf_P(PSTR("ROM specified isn't a temperature sensor\r\n"));
436 return; 351 return;
437 } 352 }
438 353
439 OWSendCmd(ROM, OW_CONVERTT_CMD); 354 OWSendCmd(ROM, OW_CONVERTT_CMD);
440 i = 0; 355 i = 0;
441 /* Wait for the conversion */ 356 /* Wait for the conversion */
442 while (OWReadBit() == 0) { 357 while (OWReadBit() == 0)
443 i++; 358 i = 1;
444 } 359
360
445 OWSendCmd(ROM, OW_RD_SCR_CMD); 361 OWSendCmd(ROM, OW_RD_SCR_CMD);
446 crc = 0; 362 crc = 0;
447 for (i = 0; i < 9; i++) { 363 for (i = 0; i < 9; i++) {
448 buf[i] = OWReadByte(); 364 buf[i] = OWReadByte();
449 if (i < 8) 365 if (i < 8)
450 OWCRC(buf[i], &crc); 366 OWCRC(buf[i], &crc);
451 } 367 }
452 368
453 if (crc != buf[8]) { 369 if (crc != buf[8]) {
454 uart_putsP(PSTR("CRC mismatch\r\n")); 370 printf_P(PSTR("CRC mismatch\r\n"));
455 return; 371 return;
456 } 372 }
457 373
458 #if 0
459 uart_putsP(PSTR("temperature "));
460 uart_puts_dec(temp >> 4, 0);
461 uart_putsP(PSTR("."));
462 uart_puts_dec((temp << 12) / 6553, 0);
463 uart_putsP(PSTR("\r\n"));
464 #else
465 /* 0 Temperature LSB 374 /* 0 Temperature LSB
466 * 1 Temperature MSB 375 * 1 Temperature MSB
467 * 2 Th 376 * 2 Th
468 * 3 Tl 377 * 3 Tl
469 * 4 Reserved 378 * 4 Reserved
471 * 6 Count Remain 380 * 6 Count Remain
472 * 7 Count per C 381 * 7 Count per C
473 * 8 CRC 382 * 8 CRC
474 */ 383 */
475 #if 0 384 #if 0
476 for (i = 0; i < 9; i++) { 385 for (i = 0; i < 9; i++)
477 uart_puts_dec(buf[i], 0); 386 printf_P(PSTR("%d\r\n"), buf[i]);
478 uart_putsP(PSTR("\r\n"));
479 }
480 #endif 387 #endif
481 temp = buf[0]; 388 temp = buf[0];
482 if (buf[1] & 0x80) 389 if (buf[1] & 0x80)
483 temp -= 256; 390 temp -= 256;
484 temp >>= 1; 391 temp >>= 1;
491 temp--; 398 temp--;
492 } else { 399 } else {
493 tfrac -= 100; 400 tfrac -= 100;
494 } 401 }
495 402
496 if (temp < 0){ 403 printf_P(PSTR("%d.%02d\r\n"), temp, tfrac);
497 uart_putc('-');
498 uart_puts_dec(-temp, 0);
499 } else
500 uart_puts_dec(temp, 0);
501 uart_putsP(PSTR("."));
502 uart_puts_dec(tfrac, 1);
503 uart_putsP(PSTR("\r\n"));
504
505 #endif
506 } else if (cmd.buf[0] == 's' && cmd.buf[1] == 'r') { 404 } else if (cmd.buf[0] == 's' && cmd.buf[1] == 'r') {
507 memset(ROM, 0, 8); 405 memset(ROM, 0, 8);
508 406
509 i = OWFirst(ROM, 1, 0); 407 i = OWFirst(ROM, 1, 0);
510 do { 408 do {
511 switch (i) { 409 switch (i) {
512 case OW_BADWIRE: 410 case OW_BADWIRE:
513 uart_putsP(PSTR("Presence pulse, but no module found, bad module/cabling?\r\n")); 411 printf_P(PSTR("Presence pulse, but no module found, bad module/cabling?\r\n"));
514 break; 412 break;
515 413
516 case OW_NOPRESENCE: 414 case OW_NOPRESENCE:
517 uart_putsP(PSTR("No presence pulse found\r\n")); 415 printf_P(PSTR("No presence pulse found\r\n"));
518 break; 416 break;
519 417
520 case OW_BADCRC: 418 case OW_BADCRC:
521 uart_putsP(PSTR("Bad CRC\r\n")); 419 printf_P(PSTR("Bad CRC\r\n"));
522 break; 420 break;
523 421
524 case OW_NOMODULES: 422 case OW_NOMODULES:
525 case OW_FOUND: 423 case OW_FOUND:
526 break; 424 break;
527 425
528 default: 426 default:
529 uart_putsP(PSTR("Unknown error from 1 wire library\r\n")); 427 printf_P(PSTR("Unknown error from 1 wire library\r\n"));
530 break; 428 break;
531 } 429 }
532 430
533 if (i != OW_FOUND) 431 if (i != OW_FOUND)
534 break; 432 break;
535 433
536 for (i = 0; i < 7; i++) { 434 for (i = 0; i < 8; i++)
537 uart_puts_hex(ROM[i]); 435 printf_P(PSTR("%02x%S"), ROM[i], i == 7 ? PSTR("\r\n") : PSTR(":"));
538 uart_putc(':');
539 }
540 uart_puts_hex(ROM[7]);
541 uart_putsP(PSTR("\r\n"));
542 436
543 i = OWNext(ROM, 1, 0); 437 i = OWNext(ROM, 1, 0);
544 } while (1); 438 } while (1);
545 } else if (cmd.buf[0] == 'i' && cmd.buf[1] == 'n') { 439 } else if (cmd.buf[0] == 'i' && cmd.buf[1] == 'n') {
546 switch (tolower(cmd.buf[3])) { 440 switch (tolower(cmd.buf[3])) {
559 case 'd': 453 case 'd':
560 crc = PIND; 454 crc = PIND;
561 break; 455 break;
562 456
563 default: 457 default:
564 uart_putsP(PSTR("Unknown port\r\n")); 458 printf_P(PSTR("Unknown port\r\n"));
565 return; 459 return;
566 } 460 }
567 uart_putsP(PSTR("0x")); 461 printf_P(PSTR("0x%02x\r\n"), crc);
568 uart_puts_hex(crc);
569 uart_putsP(PSTR("\r\n"));
570 } else if (cmd.buf[0] == 'o' && cmd.buf[1] == 'u') { 462 } else if (cmd.buf[0] == 'o' && cmd.buf[1] == 'u') {
571 crc = strtol((char *)cmd.buf + 8, (char **)NULL, 16); 463 crc = strtol((char *)cmd.buf + 8, (char **)NULL, 16);
572 switch (tolower(cmd.buf[4])) { 464 switch (tolower(cmd.buf[4])) {
573 case 'a': 465 case 'a':
574 PORTA = crc; 466 PORTA = crc;
585 case 'd': 477 case 'd':
586 PORTD = crc; 478 PORTD = crc;
587 break; 479 break;
588 480
589 default: 481 default:
590 uart_putsP(PSTR("Unknown port\r\n")); 482 printf_P(PSTR("Unknown port\r\n"));
591 return; 483 return;
592 } 484 }
593 uart_putsP(PSTR("0x")); 485 printf_P(PSTR("PORT%c <= 0x%02x\r\n"), toupper(cmd.buf[4]), crc);
594 uart_puts_hex(crc);
595 uart_putsP(PSTR("\r\n"));
596 } else if (cmd.buf[0] == 'd' && cmd.buf[1] == 'd') { 486 } else if (cmd.buf[0] == 'd' && cmd.buf[1] == 'd') {
597 crc = strtol((char *)cmd.buf + 8, (char **)NULL, 16); 487 crc = strtol((char *)cmd.buf + 8, (char **)NULL, 16);
598 switch (tolower(cmd.buf[4])) { 488 switch (tolower(cmd.buf[4])) {
599 case 'a': 489 case 'a':
600 DDRA = crc; 490 DDRA = crc;
611 case 'd': 501 case 'd':
612 DDRD = crc; 502 DDRD = crc;
613 break; 503 break;
614 504
615 default: 505 default:
616 uart_putsP(PSTR("Unknown port\r\n")); 506 printf_P(PSTR("Unknown port\r\n"));
617 return; 507 return;
618 } 508 }
619 uart_putsP(PSTR("0x")); 509 printf_P(PSTR("0x%02x\r\n"), crc);
620 uart_puts_hex(crc); 510 } else if (cmd.buf[0] == 't' && cmd.buf[1] == 'c') {
621 uart_putsP(PSTR("\r\n")); 511 tempctrl_cmd((char *)cmd.buf);
622 #ifdef WITHUSB 512 #ifdef WITHUSB
623 } else if (cmd.buf[0] == 'u' && cmd.buf[1] == 's') { 513 } else if (cmd.buf[0] == 'u' && cmd.buf[1] == 's') {
624 usb_gendata(); 514 usb_gendata();
625 #endif 515 #endif
626 } else { 516 } else {
627 badcmd: 517 badcmd:
628 uart_putsP(PSTR("Unknown command, ? for a list\r\n")); 518 printf_P(PSTR("Unknown command, ? for a list\r\n"));
629 } 519 }
630 } 520 }
631 521
632 int 522
633 uart_putc(char c) {
634 loop_until_bit_is_set(UCSRA, UDRE);
635 UDR = c;
636
637 return(0);
638 }
639
640 void
641 uart_putsP(const char *addr) {
642 char c;
643
644 while ((c = pgm_read_byte_near(addr++)))
645 uart_putc(c);
646 }
647
648 void
649 uart_puts(const char *addr) {
650 while (*addr)
651 uart_putc(*addr++);
652 }
653
654 void
655 uart_puts_dec(uint8_t a, uint8_t l) {
656 char s[4];
657
658 if (l && a < 10)
659 uart_putsP(PSTR("0"));
660 uart_puts(utoa(a, s, 10));
661 }
662
663 void
664 uart_puts_hex(uint8_t a) {
665 char s[3];
666
667 if (a < 0x10)
668 uart_putc('0');
669
670 uart_puts(utoa(a, s, 16));
671 }
672
673 char
674 uart_getc(void) {
675 while (!(UCSRA & _BV(RXC)))
676 ;
677
678 return (UDR);
679 }
680