Mercurial > ~darius > hgwebdir.cgi > avr
changeset 4:81e2f85e02ce
Implement a command driven 1 wire bus tool.
author | darius |
---|---|
date | Mon, 12 Jul 2004 15:41:35 +0930 |
parents | ee3c9a16099e |
children | 4fe81c3b1f4d |
files | testavr.c |
diffstat | 1 files changed, 191 insertions(+), 97 deletions(-) [+] |
line wrap: on
line diff
--- a/testavr.c Mon Jul 12 15:39:27 2004 +0930 +++ b/testavr.c Mon Jul 12 15:41:35 2004 +0930 @@ -30,23 +30,25 @@ #include <avr/signal.h> #include <avr/pgmspace.h> #include <string.h> +#include <ctype.h> #include "1wire.h" #define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1) #define XTAL_CPU 4000000 /* 4Mhz */ -#define UART_BAUD_RATE 9600 /* 9600 baud */ - -static uint8_t dir = 0; -static volatile uint8_t leds = 0; -static uint8_t ledpos = 0; +#define UART_BAUD_RATE 19200 void uart_putsP(const char *addr); void uart_puts(const char *addr); int uart_putc(char c); -uint8_t uart_getc(void); +int uart_getc(void); +int beginswith_P(const char *s1, const char *s2); +#if 0 uint8_t PROGMEM ledvals[] = {0x01, 0x03, 0x07, 0x0e, 0x1c, 0x38, 0x70, 0xe0, 0xc0, 0x80}; +static uint8_t dir = 0; +static volatile uint8_t leds = 0; +static uint8_t ledpos = 0; INTERRUPT(SIG_OVERFLOW0) { if (!leds) @@ -76,6 +78,7 @@ PORTA = pgm_read_byte(&ledvals[ledpos]); } +#endif void usleep(uint16_t usec) { @@ -92,12 +95,17 @@ int main(void) { - uint8_t ROM[8], count; - char foo[40]; + uint8_t ROM[8]; + char cmdbuf[40]; int i; cli(); +#if 0 outp(0xff, DDRA); + + /* Timer Clock divisor - CK/1024 */ + outp(BV(CS00) | BV(CS02), TCCR0); +#endif outp(0xfe, DDRC); outp(0x00, PORTC); @@ -107,47 +115,178 @@ /* Enable receiver and transmitter. Turn on transmit interrupts */ outp(BV(RXEN) | BV(TXEN), UCR); - /* Timer Clock divisor - CK/1024 */ - outp(BV(CS00) | BV(CS02), TCCR0); + uart_putsP(PSTR("\n\r\n\r===============\n\r" + "Inited!\n\r\n\r")); + + while (1) { + uart_putsP(PSTR("> ")); + i = 0; + while (1) { + cmdbuf[i] = tolower(uart_getc()); + if (cmdbuf[i] == '\n' || cmdbuf[i] == '\r') + break; + + /* Backspace */ + if (cmdbuf[i] == 010) { + if (i > 0) { + uart_putsP(PSTR("\010\040\010")); + i--; + } + continue; + } - uart_putsP(PSTR("\n\r\n\r===============\n\r")); - uart_putsP(PSTR("Inited!\n\r\n\r")); - uart_putsP(PSTR("Test message 1\n\r")); + /* Anything unprintable just ignore it */ + if (!isprint(cmdbuf[i])) + continue; + + uart_putc(cmdbuf[i]); + i++; + if (i == sizeof(cmdbuf)) { + uart_putsP(PSTR("\n\rLine too long\n\r")); + i = 0; + continue; + } + } + cmdbuf[i + 1] = '\0'; + uart_putsP(PSTR("\n\r")); + if (i == 0) + continue; - count = 0; - while (1) { - char cmd; + if (cmdbuf[0] == '?') { + uart_putsP(PSTR("rs Reset and check for presence\n\r" + "re Read a bit\n\r" + "rb Read a byte\n\r" + "wr bit Write a bit\n\r" + "wb byte Write a byte (hex)\n\r" + "wc cmd [ROMID] Write command\n\r" + "te ROMID Read the temperature from a DS1820\n\r")); + continue; + } - cmd = uart_getc(); - switch (cmd) { - case 'r': - uart_putsP(PSTR("Resetting... ")); - - if (OWTouchReset() == 1) - uart_putsP(PSTR("No presense\n\r")); - else - uart_putsP(PSTR("Presense\n\r")); - break; - - case 'R': + if (i < 2) + goto badcmd; + + if (cmdbuf[0] == 'r' && cmdbuf[1] == 's') { + uart_putsP(PSTR("Resetting... ")); + + if (OWTouchReset() == 1) + uart_putsP(PSTR("No presense\n\r")); + else + uart_putsP(PSTR("Presense\n\r")); + } else if (cmdbuf[0] == 'r' && cmdbuf[1] == 'e') { if (OWReadBit()) uart_putsP(PSTR("Read a 1\n\r")); else uart_putsP(PSTR("Read a 0\n\r")); - break; - - case 'w': - OWWriteBit(0); - uart_putsP(PSTR("Wote a 0\n\r")); - break; + } else if (cmdbuf[0] == 'r' && cmdbuf[1] == 'b') { + sprintf_P(cmdbuf, PSTR("Read a 0x%02x\n\r"), OWReadByte()); + uart_puts(cmdbuf); + } else if (cmdbuf[0] == 'w' && cmdbuf[1] == 'r') { + int arg; + + if (sscanf_P(cmdbuf + 3, PSTR("%d"), &arg) != 1) { + uart_putsP(PSTR("Unable to parse wr command\n\r")); + continue; + } + + OWWriteBit(arg); + sprintf_P(cmdbuf, PSTR("Wrote a %d\n\r"), arg); + uart_puts(cmdbuf); + } else if (cmdbuf[0] == 'w' && cmdbuf[1] == 'b') { + int arg; + + if (sscanf_P(cmdbuf + 3, PSTR("%2x"), &arg) != 1) { + uart_putsP(PSTR("Unable to parse wb command\n\r")); + continue; + } + + OWWriteByte(arg); + sprintf_P(cmdbuf, PSTR("Wrote a 0x%02x\n\r"), arg); + uart_puts(cmdbuf); + } else if (cmdbuf[0] == 'w' && cmdbuf[1] == 'c') { + int arg; - case 'W': - OWWriteBit(1); - uart_putsP(PSTR("Wote a 1\n\r")); - break; - - case 'd': - bzero(ROM, 8); + switch (sscanf_P(cmdbuf + 3, PSTR("%x %x:%x:%x:%x:%x:%x:%x:%x:%x"), &arg, + &ROM[0], &ROM[1], &ROM[2], &ROM[3], + &ROM[4], &ROM[5], &ROM[6], &ROM[7])) { + break; + + case 1: + OWSendCmd(NULL, arg); + sprintf_P(cmdbuf, PSTR("Sent 0x%02x to all ROMS\n\r"), arg); + uart_puts(cmdbuf); + break; + + case 9: + OWSendCmd(ROM, arg); + sprintf_P(cmdbuf, PSTR("Sent 0x%02x to ROM %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n\r"), + arg, ROM[0], ROM[1], ROM[2], ROM[3], + ROM[4], ROM[5], ROM[6], ROM[7]); + uart_puts(cmdbuf); + + break; + + case 0: + default: + uart_putsP(PSTR("Unable to parse wc command\n\r")); + break; + } + + OWWriteByte(arg); + sprintf_P(cmdbuf, PSTR("Wrote a 0x%02x\n\r"), arg); + uart_puts(cmdbuf); + } else if (cmdbuf[0] == 't' && cmdbuf[1] == 'e') { + uint8_t crc; + uint16_t temp; + uint8_t buf[9]; + + if (sscanf_P(cmdbuf + 3, PSTR("%x:%x:%x:%x:%x:%x:%x:%x:%x"), + &ROM[0], &ROM[1], &ROM[2], &ROM[3], + &ROM[4], &ROM[5], &ROM[6], &ROM[7]) != 8) { + uart_putsP(PSTR("Unable to parse ROM ID\n\r")); + continue; + } + + if (ROM[0] != OW_FAMILY_TEMP) { + uart_putsP(PSTR("ROM specified isn't a temperature sensor\n\r")); + continue; + } + + OWSendCmd(ROM, OW_CONVERTT_CMD); +#if OW_DEBUG + uart_putsP(PSTR("Command sent, waiting\n\r")); +#endif + i = 0; + while (OWReadBit() == 0) { + i++; + } +#if OW_DEBUG + sprintf_P(cmdbuf, PSTR("Temp comversion took %d cycles\n\r"), i); + uart_puts(cmdbuf); +#endif + OWSendCmd(ROM, OW_RD_SCR_CMD); + crc = 0; + for (i = 0; i < 9; i++) { + buf[i] = OWReadByte(); + if (i < 8) + OWCRC(buf[i], &crc); + } + + temp = buf[0]; + temp |= (uint16_t)buf[1] << 8; + temp <<= 3; + + if (crc != buf[8]) { + sprintf_P(cmdbuf, PSTR("CRC mismatch got %d vs calcd %d\n\r"), buf[8], crc); + uart_puts(cmdbuf); + continue; + } + + sprintf_P(cmdbuf, PSTR("temperature %d.%01d\n\r"), + temp >> 4, (temp << 12) / 6553); + uart_puts(cmdbuf); + } else if (cmdbuf[0] == 's' && cmdbuf[1] == 'r') { + memset(ROM, 0, 8); if (OWTouchReset()) { uart_putsP(PSTR("No devices on bus\n\r")); break; @@ -157,8 +296,7 @@ break; } do { - uart_putsP(PSTR("Found a module ")); - sprintf_P(foo, PSTR("%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x"), + sprintf_P(cmdbuf, PSTR("%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n\r"), ROM[0], ROM[1], ROM[2], @@ -167,54 +305,11 @@ ROM[5], ROM[6], ROM[7]); - uart_puts(foo); - if (ROM[0] == OW_FAMILY_TEMP) { - uint8_t tempbuf[9]; - uint8_t crc; - uint16_t temp; + uart_puts(cmdbuf); + } while (OWNext(ROM, 1, 0)); +#if 0 - uart_putsP(PSTR(" is a temp sensor\n\r")); - OWSendCmd(ROM, OW_CONVERTT_CMD); -#if OW_DEBUG - uart_putsP(PSTR("Command sent, waiting\n\r")); -#endif - i = 0; - while (OWReadBit() == 0) { - i++; - } -#if OW_DEBUG - sprintf_P(foo, PSTR("Temp comversion took %d cycles\n\r"), i); - uart_puts(foo); -#endif - OWSendCmd(ROM, OW_RD_SCR_CMD); - crc = 0; - for (i = 0; i < 9; i++) { - tempbuf[i] = OWReadByte(); - if (i < 8) - OWCRC(tempbuf[i], &crc); - } - - temp = tempbuf[0]; - temp |= (uint16_t)tempbuf[1] << 8; - temp <<= 3; - - if (crc != tempbuf[9]) { - sprintf_P(foo, PSTR("CRC mismatch got %d vs calcd %d\n\r"), tempbuf[9], crc); - uart_puts(foo); - } - - sprintf_P(foo, PSTR("temperature %d.%01d\n\r"), - temp >> 4, (temp << 12) / 6553); - uart_puts(foo); - } - - if (ROM[0] == OW_FAMILY_ROM) { - uart_putsP(PSTR(" is a ROM\n\r")); - } - } while (OWNext(ROM, 1, 0)); - break; - - case 'l': + } else if (cmdbuf[0] == 'l' && cmdbuf[1] == 'e' && cmdbuf[2] == 'd') { if (leds == 0) { leds = 1; ledpos = 0; @@ -230,12 +325,10 @@ cli(); uart_putsP(PSTR("Stopping\n\r")); } - - break; - - default: - uart_putsP(PSTR("Unknown command\n\r")); - break; +#endif + } else { + badcmd: + uart_putsP(PSTR("Unknown command, ? for a list\n\r")); } } @@ -265,10 +358,11 @@ uart_putc(*addr++); } -uint8_t +int uart_getc(void) { while (!(inp(USR) & 0x80)) ; return (inp(UDR)); } +