diff testavr.c @ 13:2c03ec600dbc OWIRE_1_3

Reduce object code size as much as possible.
author darius
date Wed, 01 Sep 2004 17:46:15 +0930
parents ccc39c9f320b
children 0940abdf2b9d
line wrap: on
line diff
--- a/testavr.c	Wed Sep 01 17:45:24 2004 +0930
+++ b/testavr.c	Wed Sep 01 17:46:15 2004 +0930
@@ -28,13 +28,13 @@
  * SUCH DAMAGE.
  */
 
-#include <stdio.h>
 #include <avr/io.h>
 #include <avr/interrupt.h>
 #include <avr/signal.h>
 #include <avr/pgmspace.h>
 #include <string.h>
 #include <ctype.h>
+#include <stdlib.h>
 
 #include "1wire.h"
 
@@ -45,66 +45,16 @@
 void		uart_puts(const char *addr);
 int		uart_putc(char c);
 int		uart_getc(void);
-int		beginswith_P(const char *s1, const char *s2);
-    
-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)
-	return;
-
-    /* Going up */
-    if (dir == 0) {
-	if (ledpos == 9) {
-	    dir = 1;
-	    TCNT0 = 0;
-	    goto doleds;
-	}
-
-	ledpos++;
-    } else {
-	if (ledpos == 0) {
-	    dir = 0;
-	    TCNT0 = 0;
-	    goto doleds;
-	}
-
-	ledpos--;
-    }
-
-  doleds:
-    TCNT0 = 0;
-
-    PORTA = pgm_read_byte(&ledvals[ledpos]);
-}
-
-void
-usleep(uint16_t usec) {
-    /* 4Mhz = 250ns per clock cycle */
-    usec /= 2;
-    if (usec < 1)
-	return;
-    
-    while (usec--) 
-	asm volatile (
-	    ""
-	    ::);
-}
+void		uart_puts_dec(uint8_t a, uint8_t l);
+void		uart_puts_hex(uint8_t a);
 
 int
 main(void) {
     uint8_t	ROM[8];
     char	cmdbuf[40];
-    int		i;
+    int8_t	i, arg;
     
     cli();
-    outp(0xff, DDRA);
-
-    /* Timer Clock divisor - CK/1024 */
-    outp(BV(CS00) | BV(CS02), TCCR0);
 
     outp(0xfc, DDRC);
     outp(0x00, PORTC);
@@ -115,34 +65,6 @@
     /* Enable receiver and transmitter. Turn on transmit interrupts */
     outp(BV(RXEN) | BV(TXEN), UCR);
 
-#if 0
-    int was;
-    uint32_t count;
-    
-    was = inp(PINC) & 0x02;
-
-    count = 0;
-    while (1) {
-	if (was) {
-	    if (!(inp(PINC) & 0x02)) {
-		sprintf_P(cmdbuf, PSTR("high -> low, i = %d\n\r"), count);
-		uart_puts(cmdbuf);
-		count = 0;
-		was = 0x00;
-	    }
-	} else {
-	    if ((inp(PINC) & 0x02)) {
-		sprintf_P(cmdbuf, PSTR("low -> high, i = %d\n\r"), count);
-		uart_puts(cmdbuf);
-		count = 0;
-		was = 0x02;
-	    }
-	}
-
-	count++;
-    }
-#endif
-    
     uart_putsP(PSTR("\n\r\n\r===============\n\r"
 		    "Inited!\n\r\n\r"));
 
@@ -170,9 +92,9 @@
 	    uart_putc(cmdbuf[i]);
 	    i++;
 	    if (i == sizeof(cmdbuf)) {
-		uart_putsP(PSTR("\n\rLine too long\n\r"));
+		uart_putsP(PSTR("\n\rLine too long"));
 		i = 0;
-		continue;
+		break;
 	    }
 	}
 	cmdbuf[i + 1] = '\0';
@@ -188,8 +110,7 @@
 			    "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"
-			    "le               Turn pretty LED cycling on\n\r"));
+			    "te ROMID         Read the temperature from a DS1820\n\r"));
 	    
 	    continue;
 	}
@@ -210,91 +131,69 @@
 	    else
 		uart_putsP(PSTR("Read a 0\n\r"));
 	} else if (cmdbuf[0] == 'r' && cmdbuf[1] == 'b') {
-	    sprintf_P(cmdbuf, PSTR("Read a 0x%02x\n\r"), OWReadByte());
-	    uart_puts(cmdbuf);
+	    uart_putsP(PSTR("Read a 0x"));
+	    uart_puts_hex(OWReadByte());
+	    uart_putsP(PSTR("\n\r"));
 	} 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;
-	    }
-	    
+	    arg = strtol(cmdbuf + 3, (char **)NULL, 10);
 	    OWWriteBit(arg);
-	    sprintf_P(cmdbuf, PSTR("Wrote a %d\n\r"), arg);
-	    uart_puts(cmdbuf);
+	    uart_putsP(PSTR("Wrote a "));
+	    if (arg)
+		uart_putsP(PSTR("1\n\r"));
+	    else
+		uart_putsP(PSTR("0\n\r"));
 	} 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"));
+	    arg = (int)strtol(cmdbuf + 3, (char **)NULL, 16); 
+	    OWWriteByte(arg);
+	} else if (cmdbuf[0] == 'w' && cmdbuf[1] == 'c') {
+	    i = strlen(cmdbuf);
+	    if (i < 5) {
+		uart_putsP(PSTR("No arguments\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;
+	    arg = (int)strtol(cmdbuf + 3, (char **)NULL, 16);
+	    if (arg == 0) {
+		uart_putsP(PSTR("Unparseable command\n\r"));
+		continue;
+	    }
 
-	    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:
+	    if (i == 5) {
 		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;
+		continue;
 	    }
 	    
-	    OWWriteByte(arg);
-	    sprintf_P(cmdbuf, PSTR("Wrote a 0x%02x\n\r"), arg);
-	    uart_puts(cmdbuf);
+	    if (i < 29) {
+		uart_putsP(PSTR("Can't parse ROM ID\n\r"));
+		continue;
+	    }
+	    for (i = 0; i < 8; i++)
+		ROM[i] = (int)strtol(cmdbuf + 6 + (3 * i), (char **)NULL, 16);
+
+	    OWSendCmd(ROM, arg);
 	} else if (cmdbuf[0] == 't' && cmdbuf[1] == 'e') {
-	    uint8_t	crc;
-	    uint16_t	temp;
-	    uint8_t	buf[9];
+	    uint8_t	crc, buf[9];;
+	    int8_t	temp;
+	    uint16_t	tfrac;
 	    
-	    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) {
+	    if (strlen(cmdbuf) < 26) {
 		uart_putsP(PSTR("Unable to parse ROM ID\n\r"));
 		continue;
 	    }
-	    
+
+	    for (i = 0; i < 8; i++)
+		ROM[i] = (int)strtol(cmdbuf + 3 * (i + 1), (char **)NULL, 16);
+
 	    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++) {
@@ -303,19 +202,59 @@
 		    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);
+		uart_putsP(PSTR("CRC mismatch\n\r"));
 		continue;
 	    }
 	    
-	    sprintf_P(cmdbuf, PSTR("temperature %d.%01d\n\r"),
-		      temp >> 4, (temp << 12) / 6553);
-	    uart_puts(cmdbuf);
+#if 0
+	    uart_putsP(PSTR("temperature "));
+	    uart_puts_dec(temp >> 4, 0);
+	    uart_putsP(PSTR("."));
+	    uart_puts_dec((temp << 12) / 6553, 0);
+	    uart_putsP(PSTR("\n\r"));
+#else
+	    /* 0	Temperature LSB
+	     * 1	Temperature MSB
+	     * 2	Th
+	     * 3	Tl
+	     * 4	Reserved
+	     * 5	Reserved
+	     * 6	Count Remain
+	     * 7	Count per C
+	     * 8	CRC
+	     */
+#if 0
+	    for (i = 0; i < 9; i++) {
+		uart_puts_dec(buf[i], 0);
+		uart_putsP(PSTR("\n\r"));
+	    }
+#endif
+	    temp = buf[0];
+	    if (buf[0] & 0x01)
+		temp -= 256;
+	    temp >>= 1;
+
+	    tfrac = buf[7] - buf[6];
+	    tfrac *= (uint16_t)100;
+	    tfrac /= buf[7];
+	    tfrac += 75;
+	    if (tfrac < 100) {
+		temp--;
+	    } else {
+		tfrac -= 100;
+	    }
+	    
+	    if (temp < 0){
+		uart_putc('-');
+		uart_puts_dec(-temp, 0);
+	    } else
+		uart_puts_dec(temp, 0);
+	    uart_putsP(PSTR("."));
+	    uart_puts_dec(tfrac, 1);
+	    uart_putsP(PSTR("\n\r"));
+	    
+#endif
 	} else if (cmdbuf[0] == 's' && cmdbuf[1] == 'r') {
 	    memset(ROM, 0, 8);
 
@@ -345,36 +284,16 @@
 		
 		if (i != OW_FOUND)
 		    break;
-		
-		sprintf_P(cmdbuf, PSTR("%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n\r"), 
-			  ROM[0],
-			  ROM[1],
-			  ROM[2],
-			  ROM[3],
-			  ROM[4],
-			  ROM[5],
-			  ROM[6],
-			  ROM[7]);
-		uart_puts(cmdbuf);
+
+		for (i = 0; i < 7; i++) {
+		    uart_puts_hex(ROM[i]);
+		    uart_putc(':');
+		}
+		uart_puts_hex(ROM[7]);
+		uart_putsP(PSTR("\n\r"));
 
 		i = OWNext(ROM, 1, 0);
 	    } while (1);
-	} else if (cmdbuf[0] == 'l' && cmdbuf[1] == 'e') {
-	    if (leds == 0) {
-		leds = 1;
-		ledpos = 0;
-		outp(0, TCNT0);
-		sbi(TIMSK, TOIE0);
-		sei();
-		uart_putsP(PSTR("Starting\n\r"));
-	    } else {
-		leds = 0;
-		ledpos = 0;
-		PORTA = 0x00;
-		cbi(TIMSK, TOIE0);
-		cli();
-		uart_putsP(PSTR("Stopping\n\r"));
-	    }
 	} else {
 	  badcmd:
 	    uart_putsP(PSTR("Unknown command, ? for a list\n\r"));
@@ -407,6 +326,25 @@
 	uart_putc(*addr++);
 }
 
+void
+uart_puts_dec(uint8_t a, uint8_t l) {
+    char	s[4];
+    
+    if (l && a < 10)
+	uart_putsP(PSTR("0"));
+    uart_puts(utoa(a, s, 10));
+}
+
+void
+uart_puts_hex(uint8_t a) {
+    char	s[3];
+    
+    if (a < 0x10)
+	uart_putc('0');
+    
+    uart_puts(utoa(a, s, 16));
+}
+
 int
 uart_getc(void) {
     while (!(inp(USR) & 0x80))