comparison testavr.c @ 37:25fa387ef7e9

- Conditionalise USB support. - Add ROM write command (we). - Add command to read the EEPROM status field of a DS2502 (rt). - Add command to dump the EEPROM data of a DS2502 (rr). - Add command to change data direction registers (dd).
author darius
date Tue, 23 Oct 2007 11:03:22 +0930
parents 48056516b3eb
children 0a148f362097
comparison
equal deleted inserted replaced
36:e40e919721b0 37:25fa387ef7e9
35 #include <ctype.h> 35 #include <ctype.h>
36 #include <stdlib.h> 36 #include <stdlib.h>
37 #include <util/delay.h> 37 #include <util/delay.h>
38 38
39 #include "1wire.h" 39 #include "1wire.h"
40 #ifdef WITHUSB
40 #include "usb.h" 41 #include "usb.h"
42 #endif
41 43
42 #define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1) 44 #define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1)
43 #define UART_BAUD_RATE 38400 45 #define UART_BAUD_RATE 38400
44 46
45 void process_cmd(void); 47 void process_cmd(void);
121 int 123 int
122 main(void) { 124 main(void) {
123 /* Disable interrupts while we frob stuff */ 125 /* Disable interrupts while we frob stuff */
124 cli(); 126 cli();
125 127
128 #if 1
126 /* Disable JTAG (yes twice) */ 129 /* Disable JTAG (yes twice) */
127 MCUCSR |= _BV(JTD); 130 MCUCSR |= _BV(JTD);
128 MCUCSR |= _BV(JTD); 131 MCUCSR |= _BV(JTD);
129 132 #endif
133
134 #ifdef WITHUSB
130 /* USB data bus (7:0) */ 135 /* USB data bus (7:0) */
131 DDRA = 0x00; 136 DDRA = 0x00;
132 PORTA = 0x00; 137 PORTA = 0x00;
133 138
134 /* USB control (3:0) */ 139 /* USB control (4:0) */
135 DDRB = 0x0e; 140 DDRB = 0x0d;
136 PORTB = 0x00; 141 PORTB = 0x00;
137 142 #else
143 DDRA = 0xff;
144 PORTA = 0x00;
145 #endif
138 /* GPIO (0:7) */ 146 /* GPIO (0:7) */
139 DDRC = 0xff; 147 DDRC = 0xff;
140 PORTC = 0x00; 148 PORTC = 0x00;
141 149
142 /* GPIO (2:7) */ 150 /* USART (0:1), IDBus (2:5), 485 (6:6), GPIO (7:7) */
143 DDRD = 0xfc; 151 DDRD = 0xf7;
144 PORTD = 0xfc; 152 PORTD = 0xf7;
153
154 /* Set up the one wire stuff */
155 OWInit();
145 156
146 /* Init UART */ 157 /* Init UART */
147 UBRRH = UART_BAUD_SELECT(UART_BAUD_RATE, F_CPU) >> 8; 158 UBRRH = UART_BAUD_SELECT(UART_BAUD_RATE, F_CPU) >> 8;
148 UBRRL = (uint8_t)UART_BAUD_SELECT(UART_BAUD_RATE, F_CPU); 159 UBRRL = (uint8_t)UART_BAUD_SELECT(UART_BAUD_RATE, F_CPU);
149 160
155 "Inited!\n\r\n\r")); 166 "Inited!\n\r\n\r"));
156 167
157 /* Ready to go! */ 168 /* Ready to go! */
158 sei(); 169 sei();
159 170
171 #if 0
172 DDRA = 0xff;
173 DDRC = 0xff;
174 while (1) {
175 uart_putsP(PSTR("1\n\r"));
176 PORTA = 0xff;
177 uart_putsP(PSTR("2\n\r"));
178 PORTC = 0x00;
179 uart_putsP(PSTR("3\n\r"));
180 _delay_us(1);
181 uart_putsP(PSTR("4\n\r"));
182 PORTA = 0x80;
183 uart_putsP(PSTR("5\n\r"));
184 PORTC = 0xff;
185 uart_putsP(PSTR("6\n\r"));
186 }
187 #endif
188
189 #ifdef WITHUSB
190 uart_putsP(PSTR("Calling usb_init\n\r"));
160 usb_init(); 191 usb_init();
192 #endif
193 uart_putsP(PSTR("done\n\r"));
161 _delay_us(1000); 194 _delay_us(1000);
162
163 uart_putsP(PSTR("> ")); 195 uart_putsP(PSTR("> "));
164 cmd.state = 0; 196 cmd.state = 0;
165 197
166 /* Wait for user input or an "interrupt" */ 198 /* Wait for user input or an "interrupt" */
167 while (1) { 199 while (1) {
170 uart_putsP(PSTR("> ")); 202 uart_putsP(PSTR("> "));
171 /* Allow new characters to be processed */ 203 /* Allow new characters to be processed */
172 cmd.state = 0; 204 cmd.state = 0;
173 } 205 }
174 206
175 if (!(PINB & _BV(PB0))) 207 #ifdef WITHUSB
208 if (!(PDICTL & _BV(PDIINT)))
176 usb_intr(); 209 usb_intr();
210 #endif
177 } 211 }
178 } 212 }
179 213
180 void 214 void
181 process_cmd(void) { 215 process_cmd(void) {
234 else 268 else
235 uart_putsP(PSTR("0\n\r")); 269 uart_putsP(PSTR("0\n\r"));
236 } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'b') { 270 } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'b') {
237 arg = (int)strtol((char *)cmd.buf + 3, (char **)NULL, 16); 271 arg = (int)strtol((char *)cmd.buf + 3, (char **)NULL, 16);
238 OWWriteByte(arg); 272 OWWriteByte(arg);
273 } else if (cmd.buf[0] == 'r' && cmd.buf[1] == 't') {
274 if (cmd.len < 26) {
275 uart_putsP(PSTR("Unable to parse ROM ID\n\r"));
276 return;
277 }
278
279 if (OWTouchReset() != 0) {
280 uart_putsP(PSTR("No presence\n\r"));
281 return;
282 }
283
284 for (i = 0; i < 8; i++)
285 ROM[i] = (int)strtol((char *)cmd.buf + 3 * (i + 1), (char **)NULL, 16);
286
287 if (ROM[0] != OW_FAMILY_ROM) {
288 uart_putsP(PSTR("ROM specified isn't a DS2502\n\r"));
289 return;
290 }
291
292 if (OWTouchReset() != 0) {
293 uart_putsP(PSTR("No presence\n\r"));
294 return;
295 }
296
297 crc = 0;
298
299 OWCRC(OW_READ_STATUS, &crc);
300 OWSendCmd(ROM, OW_READ_STATUS);
301
302 OWWriteByte(0x00);
303 OWCRC(0x00, &crc);
304
305 OWWriteByte(0x00);
306 OWCRC(0x00, &crc);
307
308 if (crc != OWReadByte()) {
309 uart_putsP(PSTR("CRC mismatch on command & address\n\r"));
310 return;
311 }
312
313 crc = 0;
314 for (i = 0; i < 8; i++) {
315 temp = OWReadByte();
316 uart_puts_hex(temp);
317 OWCRC(temp, &crc);
318 uart_putsP(PSTR(" "));
319 }
320 uart_putsP(PSTR("\n\r"));
321 if (crc != OWReadByte()) {
322 uart_putsP(PSTR("CRC mismatch on data\n\r"));
323 return;
324 }
325 } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'e') {
326 if (cmd.len < 26) {
327 uart_putsP(PSTR("Unable to parse ROM ID\n\r"));
328 return;
329 }
330
331 for (i = 0; i < 8; i++)
332 ROM[i] = (int)strtol((char *)cmd.buf + 3 * (i + 1), (char **)NULL, 16);
333
334 if (ROM[0] != OW_FAMILY_ROM) {
335 uart_putsP(PSTR("ROM specified isn't a ROM\n\r"));
336 return;
337 }
338
339 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 .. */
341 buf[2] = (int)strtol((char *)cmd.buf + 33, (char **)NULL, 16);
342
343 if (OWTouchReset() != 0) {
344 uart_putsP(PSTR("No presence\n\r"));
345 return;
346 }
347
348 i = OWProgROM(ROM, buf[0], 2, &buf[1], 0, 0);
349 uart_putsP(PSTR("OWProgROM returned "));
350 uart_puts_dec(i, 0);
351 uart_putsP(PSTR("\n\r"));
352 } else if (cmd.buf[0] == 'r' && cmd.buf[1] == 'r') {
353 if (cmd.len < 26) {
354 uart_putsP(PSTR("Unable to parse ROM ID\n\r"));
355 return;
356 }
357
358 for (i = 0; i < 8; i++)
359 ROM[i] = (int)strtol((char *)cmd.buf + 3 * (i + 1), (char **)NULL, 16);
360
361 if (ROM[0] != OW_FAMILY_ROM) {
362 uart_putsP(PSTR("ROM specified isn't a ROM\n\r"));
363 return;
364 }
365
366 crc = 0;
367 OWSendCmd(ROM, OW_READ_MEMORY);
368 OWCRC(OW_READ_MEMORY, &crc);
369
370 OWWriteByte(0x00);
371 OWCRC(0x00, &crc);
372
373 OWWriteByte(0x00);
374 OWCRC(0x00, &crc);
375
376 if (crc != OWReadByte()) {
377 uart_putsP(PSTR("CRC mismatch on command & address\n\r"));
378 return;
379 }
380
381 crc = 0;
382 for (buf[0] = 0; buf[0] < 128; buf[0]++) {
383 buf[1] = OWReadByte();
384 if (buf[0] > 0) {
385 if (buf[0] % 16 != 0)
386 uart_putsP(PSTR(" "));
387 else
388 uart_putsP(PSTR("\n\r"));
389 }
390
391 uart_puts_hex(buf[1]);
392 OWCRC(buf[1], &crc);
393 }
394 uart_putsP(PSTR("\n\r"));
395 if (crc != OWReadByte()) {
396 uart_putsP(PSTR("CRC mismatch on data\n\r"));
397 return;
398 }
399
239 } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'c') { 400 } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'c') {
240 if (cmd.len < 5) { 401 if (cmd.len < 5) {
241 uart_putsP(PSTR("No arguments\n\r")); 402 uart_putsP(PSTR("No arguments\n\r"));
242 return; 403 return;
243 } 404 }
275 return; 436 return;
276 } 437 }
277 438
278 OWSendCmd(ROM, OW_CONVERTT_CMD); 439 OWSendCmd(ROM, OW_CONVERTT_CMD);
279 i = 0; 440 i = 0;
441 /* Wait for the conversion */
280 while (OWReadBit() == 0) { 442 while (OWReadBit() == 0) {
281 i++; 443 i++;
282 } 444 }
283 OWSendCmd(ROM, OW_RD_SCR_CMD); 445 OWSendCmd(ROM, OW_RD_SCR_CMD);
284 crc = 0; 446 crc = 0;
429 return; 591 return;
430 } 592 }
431 uart_putsP(PSTR("0x")); 593 uart_putsP(PSTR("0x"));
432 uart_puts_hex(crc); 594 uart_puts_hex(crc);
433 uart_putsP(PSTR("\n\r")); 595 uart_putsP(PSTR("\n\r"));
596 } else if (cmd.buf[0] == 'd' && cmd.buf[1] == 'd') {
597 crc = strtol((char *)cmd.buf + 8, (char **)NULL, 16);
598 switch (tolower(cmd.buf[4])) {
599 case 'a':
600 DDRA = crc;
601 break;
602
603 case 'b':
604 DDRB = crc;
605 break;
606
607 case 'c':
608 DDRC = crc;
609 break;
610
611 case 'd':
612 DDRD = crc;
613 break;
614
615 default:
616 uart_putsP(PSTR("Unknown port\n\r"));
617 return;
618 }
619 uart_putsP(PSTR("0x"));
620 uart_puts_hex(crc);
621 uart_putsP(PSTR("\n\r"));
622 #ifdef WITHUSB
434 } else if (cmd.buf[0] == 'u' && cmd.buf[1] == 's') { 623 } else if (cmd.buf[0] == 'u' && cmd.buf[1] == 's') {
435 usb_gendata(); 624 usb_gendata();
625 #endif
436 } else { 626 } else {
437 badcmd: 627 badcmd:
438 uart_putsP(PSTR("Unknown command, ? for a list\n\r")); 628 uart_putsP(PSTR("Unknown command, ? for a list\n\r"));
439 } 629 }
440 } 630 }