# HG changeset patch # User darius # Date 1193103202 -34200 # Node ID 25fa387ef7e980a586cb4dcc75c9e14568cdea69 # Parent e40e919721b04b20147c83b5c7a72451241d577b - 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). diff -r e40e919721b0 -r 25fa387ef7e9 testavr.c --- a/testavr.c Tue Oct 23 10:55:51 2007 +0930 +++ b/testavr.c Tue Oct 23 11:03:22 2007 +0930 @@ -37,7 +37,9 @@ #include #include "1wire.h" +#ifdef WITHUSB #include "usb.h" +#endif #define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1) #define UART_BAUD_RATE 38400 @@ -123,25 +125,34 @@ /* Disable interrupts while we frob stuff */ cli(); +#if 1 /* Disable JTAG (yes twice) */ MCUCSR |= _BV(JTD); MCUCSR |= _BV(JTD); - +#endif + +#ifdef WITHUSB /* USB data bus (7:0) */ DDRA = 0x00; PORTA = 0x00; - /* USB control (3:0) */ - DDRB = 0x0e; + /* USB control (4:0) */ + DDRB = 0x0d; PORTB = 0x00; - +#else + DDRA = 0xff; + PORTA = 0x00; +#endif /* GPIO (0:7) */ DDRC = 0xff; PORTC = 0x00; - /* GPIO (2:7) */ - DDRD = 0xfc; - PORTD = 0xfc; + /* USART (0:1), IDBus (2:5), 485 (6:6), GPIO (7:7) */ + DDRD = 0xf7; + PORTD = 0xf7; + + /* Set up the one wire stuff */ + OWInit(); /* Init UART */ UBRRH = UART_BAUD_SELECT(UART_BAUD_RATE, F_CPU) >> 8; @@ -157,9 +168,30 @@ /* Ready to go! */ sei(); +#if 0 + DDRA = 0xff; + DDRC = 0xff; + while (1) { + uart_putsP(PSTR("1\n\r")); + PORTA = 0xff; + uart_putsP(PSTR("2\n\r")); + PORTC = 0x00; + uart_putsP(PSTR("3\n\r")); + _delay_us(1); + uart_putsP(PSTR("4\n\r")); + PORTA = 0x80; + uart_putsP(PSTR("5\n\r")); + PORTC = 0xff; + uart_putsP(PSTR("6\n\r")); + } +#endif + +#ifdef WITHUSB + uart_putsP(PSTR("Calling usb_init\n\r")); usb_init(); +#endif + uart_putsP(PSTR("done\n\r")); _delay_us(1000); - uart_putsP(PSTR("> ")); cmd.state = 0; @@ -172,8 +204,10 @@ cmd.state = 0; } - if (!(PINB & _BV(PB0))) +#ifdef WITHUSB + if (!(PDICTL & _BV(PDIINT))) usb_intr(); +#endif } } @@ -236,6 +270,133 @@ } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'b') { arg = (int)strtol((char *)cmd.buf + 3, (char **)NULL, 16); OWWriteByte(arg); + } else if (cmd.buf[0] == 'r' && cmd.buf[1] == 't') { + if (cmd.len < 26) { + uart_putsP(PSTR("Unable to parse ROM ID\n\r")); + return; + } + + if (OWTouchReset() != 0) { + uart_putsP(PSTR("No presence\n\r")); + return; + } + + for (i = 0; i < 8; i++) + ROM[i] = (int)strtol((char *)cmd.buf + 3 * (i + 1), (char **)NULL, 16); + + if (ROM[0] != OW_FAMILY_ROM) { + uart_putsP(PSTR("ROM specified isn't a DS2502\n\r")); + return; + } + + if (OWTouchReset() != 0) { + uart_putsP(PSTR("No presence\n\r")); + return; + } + + crc = 0; + + OWCRC(OW_READ_STATUS, &crc); + OWSendCmd(ROM, OW_READ_STATUS); + + OWWriteByte(0x00); + OWCRC(0x00, &crc); + + OWWriteByte(0x00); + OWCRC(0x00, &crc); + + if (crc != OWReadByte()) { + uart_putsP(PSTR("CRC mismatch on command & address\n\r")); + return; + } + + crc = 0; + for (i = 0; i < 8; i++) { + temp = OWReadByte(); + uart_puts_hex(temp); + OWCRC(temp, &crc); + uart_putsP(PSTR(" ")); + } + uart_putsP(PSTR("\n\r")); + if (crc != OWReadByte()) { + uart_putsP(PSTR("CRC mismatch on data\n\r")); + return; + } + } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'e') { + if (cmd.len < 26) { + uart_putsP(PSTR("Unable to parse ROM ID\n\r")); + return; + } + + for (i = 0; i < 8; i++) + ROM[i] = (int)strtol((char *)cmd.buf + 3 * (i + 1), (char **)NULL, 16); + + if (ROM[0] != OW_FAMILY_ROM) { + uart_putsP(PSTR("ROM specified isn't a ROM\n\r")); + return; + } + + buf[0] = (int)strtol((char *)cmd.buf + 27, (char **)NULL, 16); /* Address */ + buf[1] = (int)strtol((char *)cmd.buf + 30, (char **)NULL, 16); /* Data .. */ + buf[2] = (int)strtol((char *)cmd.buf + 33, (char **)NULL, 16); + + if (OWTouchReset() != 0) { + uart_putsP(PSTR("No presence\n\r")); + return; + } + + i = OWProgROM(ROM, buf[0], 2, &buf[1], 0, 0); + uart_putsP(PSTR("OWProgROM returned ")); + uart_puts_dec(i, 0); + uart_putsP(PSTR("\n\r")); + } else if (cmd.buf[0] == 'r' && cmd.buf[1] == 'r') { + if (cmd.len < 26) { + uart_putsP(PSTR("Unable to parse ROM ID\n\r")); + return; + } + + for (i = 0; i < 8; i++) + ROM[i] = (int)strtol((char *)cmd.buf + 3 * (i + 1), (char **)NULL, 16); + + if (ROM[0] != OW_FAMILY_ROM) { + uart_putsP(PSTR("ROM specified isn't a ROM\n\r")); + return; + } + + crc = 0; + OWSendCmd(ROM, OW_READ_MEMORY); + OWCRC(OW_READ_MEMORY, &crc); + + OWWriteByte(0x00); + OWCRC(0x00, &crc); + + OWWriteByte(0x00); + OWCRC(0x00, &crc); + + if (crc != OWReadByte()) { + uart_putsP(PSTR("CRC mismatch on command & address\n\r")); + return; + } + + crc = 0; + for (buf[0] = 0; buf[0] < 128; buf[0]++) { + buf[1] = OWReadByte(); + if (buf[0] > 0) { + if (buf[0] % 16 != 0) + uart_putsP(PSTR(" ")); + else + uart_putsP(PSTR("\n\r")); + } + + uart_puts_hex(buf[1]); + OWCRC(buf[1], &crc); + } + uart_putsP(PSTR("\n\r")); + if (crc != OWReadByte()) { + uart_putsP(PSTR("CRC mismatch on data\n\r")); + return; + } + } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'c') { if (cmd.len < 5) { uart_putsP(PSTR("No arguments\n\r")); @@ -277,6 +438,7 @@ OWSendCmd(ROM, OW_CONVERTT_CMD); i = 0; + /* Wait for the conversion */ while (OWReadBit() == 0) { i++; } @@ -431,8 +593,36 @@ uart_putsP(PSTR("0x")); uart_puts_hex(crc); uart_putsP(PSTR("\n\r")); + } else if (cmd.buf[0] == 'd' && cmd.buf[1] == 'd') { + crc = strtol((char *)cmd.buf + 8, (char **)NULL, 16); + switch (tolower(cmd.buf[4])) { + case 'a': + DDRA = crc; + break; + + case 'b': + DDRB = crc; + break; + + case 'c': + DDRC = crc; + break; + + case 'd': + DDRD = crc; + break; + + default: + uart_putsP(PSTR("Unknown port\n\r")); + return; + } + uart_putsP(PSTR("0x")); + uart_puts_hex(crc); + uart_putsP(PSTR("\n\r")); +#ifdef WITHUSB } else if (cmd.buf[0] == 'u' && cmd.buf[1] == 's') { usb_gendata(); +#endif } else { badcmd: uart_putsP(PSTR("Unknown command, ? for a list\n\r"));