diff 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
line wrap: on
line diff
--- a/testavr.c	Thu Nov 22 16:02:40 2007 +0000
+++ b/testavr.c	Sun Jul 06 22:19:53 2008 +0930
@@ -1,9 +1,7 @@
 /*
  * Test various AVR bits and pieces
  *
- * $Id$
- *
- * Copyright (c) 2004
+ * Copyright (c) 2008
  *      Daniel O'Connor <darius@dons.net.au>.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -31,101 +29,27 @@
 #include <avr/io.h>
 #include <avr/interrupt.h>
 #include <avr/pgmspace.h>
+#include <stdio.h>
 #include <string.h>
 #include <ctype.h>
 #include <stdlib.h>
 #include <util/delay.h>
 
+#include "cons.h"
 #include "1wire.h"
 #ifdef WITHUSB
 #include "usb.h"
 #endif
-
-#define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1)
-#define UART_BAUD_RATE		38400
+#include "tempctrl.h"
 
 void		process_cmd(void);
 
-void		uart_putsP(const char *addr);
-void		uart_puts(const char *addr);
-int		uart_putc(char c);
-void		uart_puts_dec(uint8_t a, uint8_t l);
-void		uart_puts_hex(uint8_t a);
-char		uart_getc(void);
-
-/* Receive buffer storage */
-volatile struct {
-    char	buf[40];
-    uint8_t	state;
-    uint8_t	len;
-} cmd = {
-    .state = 255,
-    .len = 0
-};
-
-/* Rx complete */
-ISR(USART_RXC_vect) {
-    volatile char pit;
-    char c;
-    
-    while (UCSRA & _BV(RXC)) {
-	/* 255 means we're waiting for main to process the command,
-	   just throw stuff away
-	*/
-	if (cmd.state == 255) {
-	    pit = UDR;
-	    continue;
-	}
-	c = UDR;
-	
-	/* End of line? */
-	if (c == '\n' || c == '\r') {
-	    cmd.buf[cmd.state + 1] = '\0';
-	    uart_putsP(PSTR("\r\n"));
-	    cmd.len = cmd.state;
-	    cmd.state = 255;
-	    continue;
-	}
-	
-	/* Backspace/delete */
-	if (c == 0x08 || c == 0x7f) {
-	    if (cmd.state > 0) {
-		cmd.state--;
-		uart_putsP(PSTR("\010\040\010"));
-	    }
-	    continue;
-	}
-	
-	/* Anything unprintable just ignore it */
-	if (!isprint(c))
-	    continue;
-
-	cmd.buf[cmd.state] = tolower(c);
-
-	/* Echo back to the user */
-	uart_putc(cmd.buf[cmd.state]);
-	
-	cmd.state++;
-	/* Over flow? */
-	if (cmd.state == ((sizeof(cmd.buf) / sizeof(cmd.buf[0])) - 1)) {
-	    uart_putsP(PSTR("\r\nLine too long"));
-	    cmd.state = 0;
-	    continue;
-	}
-    }
-}
-
-/* Tx complete */
-ISR(USART_TXC_vect) {
-	
-}
-    
 int
 main(void) {
     /* Disable interrupts while we frob stuff */
     cli();
 
-#if 1
+#if 0
     /* Disable JTAG (yes twice) */
     MCUCSR |= _BV(JTD);
     MCUCSR |= _BV(JTD);
@@ -155,16 +79,13 @@
     OWInit();
 
     /* Init UART */
-    UBRRH = UART_BAUD_SELECT(UART_BAUD_RATE, F_CPU) >> 8;
-    UBRRL = (uint8_t)UART_BAUD_SELECT(UART_BAUD_RATE, F_CPU);
+    cons_init();
     
-    /* Enable receiver and transmitter. Turn on transmit interrupts */
-    UCSRA = 0;
-    UCSRB = _BV(RXEN) | _BV(TXEN) | _BV(RXCIE);
-    UCSRC = _BV(URSEL) | _BV(UCSZ1) | _BV(UCSZ0);
-    uart_putsP(PSTR("\r\n\r\n===============\r\n"
-		    "Inited!\r\n\r\n"));
+    printf_P(PSTR("\r\n\r\n===============\r\n"
+		  "Inited!\r\n\r\n"));
 
+    tempctrl_init();
+    
     /* Ready to go! */
     sei();
 
@@ -172,34 +93,36 @@
     DDRA = 0xff;
     DDRC = 0xff;
     while (1) {
-	uart_putsP(PSTR("1\r\n"));
+	printf_P(PSTR("1\r\n"));
 	PORTA = 0xff;
-	uart_putsP(PSTR("2\r\n"));
+	printf_P(PSTR("2\r\n"));
 	PORTC = 0x00;
-	uart_putsP(PSTR("3\r\n"));
+	printf_P(PSTR("3\r\n"));
 	_delay_us(1);
-	uart_putsP(PSTR("4\r\n"));
+	printf_P(PSTR("4\r\n"));
 	PORTA = 0x80;
-	uart_putsP(PSTR("5\r\n"));
+	printf_P(PSTR("5\r\n"));
 	PORTC = 0xff;
-	uart_putsP(PSTR("6\r\n"));
+	printf_P(PSTR("6\r\n"));
     }
 #endif    
     
 #ifdef WITHUSB
-    uart_putsP(PSTR("Calling usb_init\r\n"));
+    printf_P(PSTR("Calling usb_init\r\n"));
     usb_init();
+    printf_P(PSTR("done\r\n"));
+    _delay_us(1000);
 #endif
-    uart_putsP(PSTR("done\r\n"));
-    _delay_us(1000);
-    uart_putsP(PSTR("> "));
+    printf_P(PSTR("> "));
     cmd.state = 0;
     
     /* Wait for user input or an "interrupt" */
     while (1) {
+	tempctrl_update();
+	
 	if (cmd.state == 255) {
 	    process_cmd();
-	    uart_putsP(PSTR("> "));
+	    printf_P(PSTR("> "));
 	    /* Allow new characters to be processed */
 	    cmd.state = 0;
 	}
@@ -224,18 +147,19 @@
 	return;
 	     
     if (cmd.buf[0] == '?') {
-        uart_putsP(PSTR("rs               Reset and check for presence\r\n"
-                        "sr               Search the bus for ROMs\r\n"
-                        "re               Read a bit\r\n"
-                        "rb               Read a byte\r\n"
-                        "wr  bit          Write a bit\r\n"
-                        "wb  byte         Write a byte (hex)\r\n"
-                        "wc  cmd [ROMID]  Write command\r\n"
-                        "te  ROMID        Read the temperature from a DS1820\r\n"
-                        "in  port         Read from a port\r\n"
-                        "out port val     Write to a port\r\n"
-                        "ddr port [val]   Read/write DDR for a port\r\n"));
-	    
+        printf_P(PSTR("rs               Reset and check for presence\r\n"
+		      "sr               Search the bus for ROMs\r\n"
+		      "re               Read a bit\r\n"
+		      "rb               Read a byte\r\n"
+		      "wr  bit          Write a bit\r\n"
+		      "wb  byte         Write a byte (hex)\r\n"
+		      "wc  cmd [ROMID]  Write command\r\n"
+		      "te  ROMID        Read the temperature from a DS1820\r\n"
+		      "in  port         Read from a port\r\n"
+		      "out port val     Write to a port\r\n"
+		      "ddr port [val]   Read/write DDR for a port\r\n"
+		      "tc ...		Temperature control related (tc help for more)\r\n"));
+	
 	return;
     }
 	
@@ -244,40 +168,34 @@
 	goto badcmd;
 	
     if (cmd.buf[0] == 'r' && cmd.buf[1] == 's') {
-	uart_putsP(PSTR("Resetting... "));
+	printf_P(PSTR("Resetting... "));
 	    
 	if (OWTouchReset() == 1)
-	    uart_putsP(PSTR("No presence pulse found\r\n"));
+	    printf_P(PSTR("No presence pulse found\r\n"));
 	else
-	    uart_putsP(PSTR("Presence pulse found\r\n"));
+	    printf_P(PSTR("Presence pulse found\r\n"));
     } else if (cmd.buf[0] == 'r' && cmd.buf[1] == 'e') {
 	if (OWReadBit())
-	    uart_putsP(PSTR("Read a 1\r\n"));
+	    printf_P(PSTR("Read a 1\r\n"));
 	else
-	    uart_putsP(PSTR("Read a 0\r\n"));
+	    printf_P(PSTR("Read a 0\r\n"));
     } else if (cmd.buf[0] == 'r' && cmd.buf[1] == 'b') {
-	uart_putsP(PSTR("Read a 0x"));
-	uart_puts_hex(OWReadByte());
-	uart_putsP(PSTR("\r\n"));
+	printf_P(PSTR("Read a 0x%02x\r\n"), OWReadByte());
     } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'r') {
 	arg = strtol((char *)cmd.buf + 3, (char **)NULL, 10);
 	OWWriteBit(arg);
-	uart_putsP(PSTR("Wrote a "));
-	if (arg)
-	    uart_putsP(PSTR("1\r\n"));
-	else
-	    uart_putsP(PSTR("0\r\n"));
+	printf_P(PSTR("Wrote a %c\r\n"), arg ? '1' : '0');
     } 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\r\n"));
+	    printf_P(PSTR("Unable to parse ROM ID\r\n"));
 	    return;
 	}
 
 	if (OWTouchReset() != 0) {
-	    uart_putsP(PSTR("No presence\r\n"));
+	    printf_P(PSTR("No presence\r\n"));
 	    return;
 	}
 
@@ -285,12 +203,12 @@
 	    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\r\n"));
+	    printf_P(PSTR("ROM specified isn't a DS2502\r\n"));
 	    return;
 	}
 
 	if (OWTouchReset() != 0) {
-	    uart_putsP(PSTR("No presence\r\n"));
+	    printf_P(PSTR("No presence\r\n"));
 	    return;
 	}
 	
@@ -306,25 +224,24 @@
 	OWCRC(0x00, &crc);
 	
 	if (crc != OWReadByte()) {
-	    uart_putsP(PSTR("CRC mismatch on command & address\r\n"));
+	    printf_P(PSTR("CRC mismatch on command & address\r\n"));
 	    return;
 	}
 	
 	crc = 0;
 	for (i = 0; i < 8; i++) {
 	    temp = OWReadByte();
-	    uart_puts_hex(temp);
+	    printf_P(PSTR("%02x "), temp);
 	    OWCRC(temp, &crc);
-	    uart_putsP(PSTR(" "));
 	}
-	uart_putsP(PSTR("\r\n"));
+	printf_P(PSTR("\r\n"));
 	if (crc != OWReadByte()) {
-	    uart_putsP(PSTR("CRC mismatch on data\r\n"));
+	    printf_P(PSTR("CRC mismatch on data\r\n"));
 	    return;
 	}
     } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'e') {
 	if (cmd.len < 26) {
-	    uart_putsP(PSTR("Unable to parse ROM ID\r\n"));
+	    printf_P(PSTR("Unable to parse ROM ID\r\n"));
 	    return;
 	}
 
@@ -332,7 +249,7 @@
 	    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\r\n"));
+	    printf_P(PSTR("ROM specified isn't a ROM\r\n"));
 	    return;
 	}
 
@@ -341,17 +258,15 @@
 	buf[2] = (int)strtol((char *)cmd.buf + 33, (char **)NULL, 16);
 	
 	if (OWTouchReset() != 0) {
-	    uart_putsP(PSTR("No presence\r\n"));
+	    printf_P(PSTR("No presence\r\n"));
 	    return;
 	}
 
 	i = OWProgROM(ROM, buf[0], 2, &buf[1], 0, 0);
-	uart_putsP(PSTR("OWProgROM returned "));
-	uart_puts_dec(i, 0);
-	uart_putsP(PSTR("\r\n"));
+	printf_P(PSTR("OWProgROM returned %d\r\n"), i);
     } else if (cmd.buf[0] == 'r' && cmd.buf[1] == 'r') {
 	if (cmd.len < 26) {
-	    uart_putsP(PSTR("Unable to parse ROM ID\r\n"));
+	    printf_P(PSTR("Unable to parse ROM ID\r\n"));
 	    return;
 	}
 
@@ -359,7 +274,7 @@
 	    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\r\n"));
+	    printf_P(PSTR("ROM specified isn't a ROM\r\n"));
 	    return;
 	}
 
@@ -374,7 +289,7 @@
 	OWCRC(0x00, &crc);
 
 	if (crc != OWReadByte()) {
-	    uart_putsP(PSTR("CRC mismatch on command & address\r\n"));
+	    printf_P(PSTR("CRC mismatch on command & address\r\n"));
 	    return;
 	}
 
@@ -383,29 +298,29 @@
 	    buf[1] = OWReadByte();
 	    if (buf[0] > 0) {
 		if (buf[0] % 16 != 0)
-		    uart_putsP(PSTR(" "));
+		    printf_P(PSTR(" "));
 		else
-		    uart_putsP(PSTR("\r\n"));
+		    printf_P(PSTR("\r\n"));
 	    }
 	    
-	    uart_puts_hex(buf[1]);
+	    printf_P(PSTR("%02x"), buf[1]);
 	    OWCRC(buf[1], &crc);
 	}
-	uart_putsP(PSTR("\r\n"));
+	printf_P(PSTR("\r\n"));
 	if (crc != OWReadByte()) {
-	    uart_putsP(PSTR("CRC mismatch on data\r\n"));
+	    printf_P(PSTR("CRC mismatch on data\r\n"));
 	    return;
 	}
 	
     } else if (cmd.buf[0] == 'w' && cmd.buf[1] == 'c') {
 	if (cmd.len < 5) {
-	    uart_putsP(PSTR("No arguments\r\n"));
+	    printf_P(PSTR("No arguments\r\n"));
 	    return;
 	}
 	    
 	arg = (int)strtol((char *)cmd.buf + 3, (char **)NULL, 16);
 	if (arg == 0) {
-	    uart_putsP(PSTR("Unparseable command\r\n"));
+	    printf_P(PSTR("Unparseable command\r\n"));
 	    return;
 	}
 
@@ -415,7 +330,7 @@
 	}
 	    
 	if (i < 29) {
-	    uart_putsP(PSTR("Can't parse ROM ID\r\n"));
+	    printf_P(PSTR("Can't parse ROM ID\r\n"));
 	    return;
 	}
 	for (i = 0; i < 8; i++)
@@ -424,7 +339,7 @@
 	OWSendCmd(ROM, arg);
     } else if (cmd.buf[0] == 't' && cmd.buf[1] == 'e') {
 	if (cmd.len < 26) {
-	    uart_putsP(PSTR("Unable to parse ROM ID\r\n"));
+	    printf_P(PSTR("Unable to parse ROM ID\r\n"));
 	    return;
 	}
 
@@ -432,16 +347,17 @@
 	    ROM[i] = (int)strtol((char *)cmd.buf + 3 * (i + 1), (char **)NULL, 16);
 
 	if (ROM[0] != OW_FAMILY_TEMP) {
-	    uart_putsP(PSTR("ROM specified isn't a temperature sensor\r\n"));
+	    printf_P(PSTR("ROM specified isn't a temperature sensor\r\n"));
 	    return;
 	}
 	    
 	OWSendCmd(ROM, OW_CONVERTT_CMD);
 	i = 0;
 	/* Wait for the conversion */
-	while (OWReadBit() == 0) {
-	    i++;
-	}
+	while (OWReadBit() == 0)
+	    i = 1;
+
+
 	OWSendCmd(ROM, OW_RD_SCR_CMD);
 	crc = 0;
 	for (i = 0; i < 9; i++) {
@@ -451,17 +367,10 @@
 	}
 	    
 	if (crc != buf[8]) {
-	    uart_putsP(PSTR("CRC mismatch\r\n"));
+	    printf_P(PSTR("CRC mismatch\r\n"));
 	    return;
 	}
 	    
-#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("\r\n"));
-#else
 	/* 0	Temperature LSB
 	 * 1	Temperature MSB
 	 * 2	Th
@@ -473,10 +382,8 @@
 	 * 8	CRC
 	 */
 #if 0
-	for (i = 0; i < 9; i++) {
-	    uart_puts_dec(buf[i], 0);
-	    uart_putsP(PSTR("\r\n"));
-	}
+	for (i = 0; i < 9; i++)
+	    printf_P(PSTR("%d\r\n"), buf[i]);
 #endif
 	temp = buf[0];
 	if (buf[1] & 0x80)
@@ -493,16 +400,7 @@
 	    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("\r\n"));
-	    
-#endif
+	printf_P(PSTR("%d.%02d\r\n"), temp, tfrac);
     } else if (cmd.buf[0] == 's' && cmd.buf[1] == 'r') {
 	memset(ROM, 0, 8);
 
@@ -510,15 +408,15 @@
 	do {
 	    switch (i) {
 		case OW_BADWIRE:
-		    uart_putsP(PSTR("Presence pulse, but no module found, bad module/cabling?\r\n"));
+		    printf_P(PSTR("Presence pulse, but no module found, bad module/cabling?\r\n"));
 		    break;
 
 		case OW_NOPRESENCE:
-		    uart_putsP(PSTR("No presence pulse found\r\n"));
+		    printf_P(PSTR("No presence pulse found\r\n"));
 		    break;
 		    
 		case OW_BADCRC:
-		    uart_putsP(PSTR("Bad CRC\r\n"));
+		    printf_P(PSTR("Bad CRC\r\n"));
 		    break;
 
 		case OW_NOMODULES:
@@ -526,19 +424,15 @@
 		    break;
 		    
 		default:
-		    uart_putsP(PSTR("Unknown error from 1 wire library\r\n"));
+		    printf_P(PSTR("Unknown error from 1 wire library\r\n"));
 		    break;
 	    }
 		
 	    if (i != OW_FOUND)
 		break;
 
-	    for (i = 0; i < 7; i++) {
-		uart_puts_hex(ROM[i]);
-		uart_putc(':');
-	    }
-	    uart_puts_hex(ROM[7]);
-	    uart_putsP(PSTR("\r\n"));
+	    for (i = 0; i < 8; i++)
+		printf_P(PSTR("%02x%S"), ROM[i], i == 7 ? PSTR("\r\n") : PSTR(":"));
 
 	    i = OWNext(ROM, 1, 0);
 	} while (1);
@@ -561,12 +455,10 @@
 		break;
 		
 	    default:
-		uart_putsP(PSTR("Unknown port\r\n"));
+		printf_P(PSTR("Unknown port\r\n"));
 		return;
 	}
-	uart_putsP(PSTR("0x"));
-	uart_puts_hex(crc);
-	uart_putsP(PSTR("\r\n"));
+	printf_P(PSTR("0x%02x\r\n"), crc);
     } else if (cmd.buf[0] == 'o' && cmd.buf[1] == 'u') {
 	crc = strtol((char *)cmd.buf + 8, (char **)NULL, 16);
 	switch (tolower(cmd.buf[4])) {
@@ -587,12 +479,10 @@
 		break;
 		
 	    default:
-		uart_putsP(PSTR("Unknown port\r\n"));
+		printf_P(PSTR("Unknown port\r\n"));
 		return;
 	}
-	uart_putsP(PSTR("0x"));
-	uart_puts_hex(crc);
-	uart_putsP(PSTR("\r\n")); 
+	printf_P(PSTR("PORT%c <= 0x%02x\r\n"), toupper(cmd.buf[4]), crc);
     } else if (cmd.buf[0] == 'd' && cmd.buf[1] == 'd') {
 	crc = strtol((char *)cmd.buf + 8, (char **)NULL, 16);
 	switch (tolower(cmd.buf[4])) {
@@ -613,68 +503,20 @@
 		break;
 		
 	    default:
-		uart_putsP(PSTR("Unknown port\r\n"));
+		printf_P(PSTR("Unknown port\r\n"));
 		return;
 	}
-	uart_putsP(PSTR("0x"));
-	uart_puts_hex(crc);
-	uart_putsP(PSTR("\r\n")); 
+	printf_P(PSTR("0x%02x\r\n"), crc);
+    } else if (cmd.buf[0] == 't' && cmd.buf[1] == 'c') {
+	tempctrl_cmd((char *)cmd.buf);
 #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\r\n"));
+	printf_P(PSTR("Unknown command, ? for a list\r\n"));
     }
 }
     
-int
-uart_putc(char c) {
-    loop_until_bit_is_set(UCSRA, UDRE);
-    UDR = c;
 
-    return(0);
-}
-
-void
-uart_putsP(const char *addr) {
-    char c;
-
-    while ((c = pgm_read_byte_near(addr++)))
-	uart_putc(c);
-}
-
-void
-uart_puts(const char *addr) {
-    while (*addr)
-	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));
-}
-
-char
-uart_getc(void) {
-    while (!(UCSRA & _BV(RXC)))
-	;
-    
-    return (UDR);
-}
-