Index: analog.c
===================================================================
--- analog.c	(revision 1344)
+++ analog.c	(revision 1345)
@@ -1,156 +1,346 @@
-/*
- * analog.c - Contains the function implementations for manipulating the ADC
- * on the firefly+ board
- * 
- * author:  CMU Robotics Club, Colony Project
- * code mostly taken from fwr analog file (author: Tom Lauwers)
- */
-
-#include <util/delay.h>
-#include <avr/interrupt.h>
-#include "analog.h"
-
-// Internal Function Prototypes
-void set_adc_mux(int which);
-
-/**
- * @defgroup analog Analog
- * Functions for manipulation the ADC on the dragonfly board.
- * All definitions may be found in analog.h.
- *
- * @{
- **/
-
-
-/**
- * Initializes the ADC.
- * Call analog_init before reading from the analog ports.
- *
- * @see analog8, analog10
- **/
-void analog_init(void)
-{
-	// ADC Status Register A
-	// Bit 7 - ADEN is set (enables analog)
-	// Bit 6 - Start conversion bit is set (must be done once for free-running mode)
-	// Bit 5 - Enable Auto Trigger (for free running mode)
-	// Bit 4 - ADC interrupt flag, 0
-	// Bit 3 - Enable ADC Interrupt (required to run free-running mode)
-	// Bits 2-0 - Set to create a clock divisor of 128, to make ADC clock = 8,000,000/128
-	ADCSRA |= 0xEF;
-
-	// ADC Status Register B
-	// Bit 7, 5-3 - Must be cleared
-	// Bit 2:0 - Set mode, currently cleared for free running operation
-	// Bit 6 - Analog comparator mode - cleared
-//	ADCSRB = 0x00;
-	
-	// ADMUX register
-	// Bit 7,6 - Set voltage reference to AVcc (0b01)
-	// Bit 5 - Set ADLAR bit for left adjust to do simple 8-bit reads
-	// Bit 4 - X
-	// Bit 3:0 - Sets the current channel, set to ADC7 (the external mux)
-	ADMUX = 0x67;
-
-	// Set external mux lines to outputs
-	DDRG |= 0x1C;
-	set_adc_mux(0x07);
-}	
-
-
-/**
- * Reads an eight bit number from an analog port.
- * analog_init must be called before using this function.
- * 
- * @param which the analog port to read from. One of
- * the constants AN0 - AN7.
- *
- * @return the eight bit input to the specified port
- *
- * @see analog_init, analog10
- **/
-unsigned int analog8(int which)
-{
-	if(which < EXT_MUX)
-		ADMUX = 0x60 + which;
-	else if(which == EXT_MUX)
-		return 0;
-	else
-	{
-		ADMUX = 0x60 + EXT_MUX;
-		set_adc_mux(which - 8);
-		_delay_ms(1);
-	}
-	
-	_delay_ms(1); // need at least 130 us between conversions
-	return ADCH;
-}
-
-/**
- * Reads a ten bit number from the specified port.
- * analog_init must be called before using this function.
- *
- * @param which the analog port to read from. Typically
- * a constant, one of AN0 - AN7.
- *
- * @return the ten bit number input to the specified port
- * 
- * @see analog_init, analog8
- **/
-unsigned int analog10(int which)
-{
-	unsigned int adc_h = 0;
-	unsigned int adc_l = 0;
-
-	if(which < EXT_MUX)
-		ADMUX = 0x60 + which;
-	else if(which == EXT_MUX)
-		return 0;
-	else
-	{
-		ADMUX = 0x60 + EXT_MUX;
-		set_adc_mux(which - 8);
-		_delay_ms(1);
-	}
-
-	_delay_ms(1);
-	adc_l = ADCL; /* highest 2 bits of ADCL -> least 2 bits of analog val */
-	adc_h = ADCH; /* ADCH -> 8 highest bits of analog val */
-	
-	return (adc_h << 2) | (adc_l >> 6);
-}
-
-/**
- * Returns the current position of the wheel, as an integer
- * in the range 0 - 255.
- * analog_init must be called before using this function.
- *
- * @return the orientation of the wheel, as an integer in
- * the range 0 - 255.
- *
- * @see analog_init
- **/
-int wheel(void)
-{
-	return analog8(WHEEL_PORT);
-}
-
-/**@}**/ //end defgroup
-
-SIGNAL (SIG_ADC)
-{
-	// This is just here to catch ADC interrupts because ADC is free running.  
-	// No code needs to be in here.
-}
-
-void set_adc_mux(int which)
-{
-  // FIX THIS IN NEXT REVISION
-  // ADDR2 ADDR1 ADDR0
-  // G2.G4.G3 set mux to port 0-7 via binary selection
-  // math would be much cleaner if it was G4.G3.G2
-  
-  // mask so only proper bits are possible.  
-  PORTG = (PORTG & 0xE3) | ((which & 0x03) << 3) | (which & 0x04);
-}
-
+/**
+ * Copyright (c) 2007 Colony Project
+ * 
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ * 
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ **/
+
+/**
+ * @file analog.c
+ * @brief Analog input and output
+ *
+ * Contains functions for manipulating the ADC on the Dragonfly board.
+ * 
+ * @author Colony Project, CMU Robotics Club
+ * code mostly taken from fwr analog file (author: Tom Lauwers)
+ **/
+
+#include <util/delay.h>
+#include <avr/interrupt.h>
+#include "analog.h"
+#include "serial.h"
+// Internal Function Prototypes
+void set_adc_mux(int which);
+
+/**
+ * @defgroup analog Analog
+ * Functions for manipulation the ADC on the dragonfly board.
+ * All definitions may be found in analog.h.
+ *
+ * @{
+ **/
+
+int adc_loop_running = 0;
+int adc_current_port = 0;
+adc_t an_val[11];
+
+/**
+ * Initializes the ADC.
+ * Call analog_init before reading from the analog ports.
+ *
+ * @see analog8, analog10, analog_get8, analog_get10
+ **/
+void analog_init(int start_conversion) {
+	for (int i = 0; i < 11; i++) {
+		an_val[i].adc10 = 0;
+		an_val[i].adc8 = 0;
+	}
+
+	// ADMUX register
+	// Bit 7,6 - Set voltage reference to AVcc (0b01)
+	// Bit 5 - ADLAR set to simplify moving from register
+	// Bit 4 - X
+	// Bit 3:0 - Sets the current channel
+	// Initializes to read from AN1 first (AN0 is reservered for the BOM)
+	ADMUX = 0;
+	ADMUX |= ADMUX_OPT | _BV(MUX0);
+
+	// ADC Status Register A
+	// Bit 7 - ADEN is set (enables analog)
+	// Bit 6 - Start conversion bit is set (must be done once for free-running mode)
+	// Bit 5 - Enable Auto Trigger (for free running mode) NOT DOING THIS RIGHT NOW
+	// Bit 4 - ADC interrupt flag, 0
+	// Bit 3 - Enable ADC Interrupt (required to run free-running mode)
+	// Bits 2-0 - Set to create a clock divisor of 128, to make ADC clock = 8,000,000/64 = 125kHz
+	ADCSRA = 0;
+	ADCSRA |= _BV(ADEN) | _BV(ADIE) | _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0);
+	
+	// Set external mux lines to outputs
+	DDRG |= 0x1C;
+	
+	// Set up first port for conversions
+	set_adc_mux(0x00);
+	adc_current_port = AN1;
+
+	//Start the conversion if requested
+	if (start_conversion)
+		analog_start_loop();
+	else
+		analog_stop_loop();
+}	
+
+/**
+ * Returns the 8-bit analog conversion of which from
+ * the lookup table. If the requested port is the BOM_PORT
+ * you will get an automatic 0 since the BOM_PORT is not
+ * read in the loop and not stored. If you need that port
+ * you should use the functions in bom.c. There is an analog_get8
+ * function which for instant lookups but should be avoided unless
+ * you know what you're doing.
+ *
+ * @param which the port that you want to read
+ *
+ * @bug may cause a seg fault if which is a larger value
+ * than exists in an_val table. Not sure if we should fix
+ * this or not since it would add overhead.
+ *
+ * @return 8-bit analog value for the which port requested
+ *
+ * @see analog10, analog_get8, analog_get10
+ **/
+unsigned int analog8(int which) {
+	if (which == BOM_PORT) {
+		return 0;
+	} else {
+		return an_val[which - 1].adc8;
+	}
+}
+
+/**
+ * Returns the 10-bit analog conversion of which from
+ * the lookup table. If the requested port is the BOM_PORT
+ * you will get an automatic 0 since the BOM_PORT is not
+ * read in the loop and not stored. If you need that port
+ * you should use the functions in bom.c. There is an analog_get10
+ * function which for instant lookups but should be avoided unless
+ * you know what you are doing.
+ *
+ * @param which the port that you want to read
+ *
+ * @bug may cause a seg fault if which is a larger value
+ * than exists in an_val table. Not sure if we should fix
+ * this or not since it would add overhead.
+ *
+ * @return 10-bit analog value for the which port requested
+ *
+ * @see analog8, analog_get8, analog_get10
+ **/
+unsigned int analog10(int which) {
+	if (which == BOM_PORT) {
+		return 0;
+	} else {
+		return an_val[which - 1].adc10;
+	}
+}
+
+/**
+ * Starts the analog update loop. Will continue to run
+ * until analog_stop_loop is called.
+ *
+ * @see analog_stop_loop
+ **/
+void analog_start_loop(void) {
+	//Start the conversion
+	ADCSRA |= _BV(ADSC);
+	adc_loop_running = 0x1;
+}
+
+/**
+ * Stops the analog update loop. If there is a current
+ * read, it will finish up and be stored before the loop
+ * is interrupted. No further updates will be made until
+ * the loop is started again.
+ *
+ * @see analog_start_loop
+ **/
+void analog_stop_loop(void) {
+	//Stop the conversion
+	adc_loop_running = 0x0;
+}
+/**
+ * Reads an 8-bit number from an analog port.
+ * analog_init must be called before using this function.
+ * The analog loop must also be stopped before using this
+ * function or you will mess up the lookup table. You
+ * must also reenabled the loop when you are done unless
+ * you are doing more instant reads. See analog_stop_loop
+ * and analog_start_loop for more information about the loop.
+ * 
+ * @param which the analog port to read from. One of
+ * the constants AN0 - AN7.
+ *
+ * @return the 8-bit input to the specified port
+ *
+ * @see analog_init, analog_get10, analog8, analog_stop_loop,
+ * analog_start_loop
+ **/
+unsigned int analog_get8(int which) {	
+	// Let any previous conversion finish
+	while (ADCSRA & _BV(ADSC));
+	
+	if(which < EXT_MUX) {
+		ADMUX = ADMUX_OPT + which;
+	} else {
+		ADMUX = ADMUX_OPT + EXT_MUX;
+		set_adc_mux(which - 8);
+	}
+	
+	// Start the conversion
+	ADCSRA |= _BV(ADSC);
+
+	// Wait for the conversion to finish
+	while (ADCSRA & _BV(ADSC));
+
+	return ADCH; //since we left aligned the data, ADCH is the 8 MSB.
+}
+
+/**
+ * Reads an 10-bit number from an analog port.
+ * analog_init must be called before using this function.
+ * The analog loop must also be stopped before using this
+ * function or you will mess up the lookup table. You
+ * must also reenabled the loop when you are done unless
+ * you are doing more instant reads. See analog_stop_loop
+ * and analog_start_loop for more information about the loop.
+ * 
+ *
+ * @param which the analog port to read from. Typically
+ * a constant, one of AN0 - AN7.
+ *
+ * @return the 10-bit number input to the specified port
+ * 
+ * @see analog_init, analog_get8, analog10, analog_stop_loop,
+ * analog_start_loop
+ **/
+unsigned int analog_get10(int which) {
+	int adc_h;
+	int adc_l;
+	
+	// Let any previous conversion finish
+	while (ADCSRA & _BV(ADSC));
+
+	if(which < EXT_MUX) {
+		ADMUX = ADMUX_OPT + which;
+	} else {
+		ADMUX = ADMUX_OPT + EXT_MUX;
+		set_adc_mux(which - 8);
+	}
+	
+	// Start the conversion
+	ADCSRA |= _BV(ADSC);
+
+	// Wait for the conversion to finish
+	while (ADCSRA & _BV(ADSC));
+	
+	adc_l = ADCL;
+	adc_h = ADCH;
+
+	return ((adc_h << 2) | (adc_l >> 6));
+}
+
+/**
+ * Returns the current position of the wheel, as an integer
+ * in the range 0 - 255.
+ * analog_init must be called before using this function.
+ *
+ * @return the orientation of the wheel, as an integer in
+ * the range 0 - 255.
+ *
+ * @see analog_init
+ **/
+int wheel(void) {
+	return analog8(WHEEL_PORT);
+}
+
+
+/**
+ * Sets the value of the external analog mux. Values are read
+ * 	on AN7 physical port. (AN8 - AN15 are "virtual" ports).
+ *
+ * @param which which analog mux port (0-7) which corresponds
+ * 		  to AN8-AN15.
+ *
+ * @bug FIX THIS IN THE NEXT BOARD REVISION:
+ *		ADDR2 ADDR1 ADDR0
+ *		G2.G4.G3 set mux to port 0-7 via vinary selection
+ *		math would be much cleaner if it was G4.G3.G2
+ *
+ * @see analog_init
+ **/
+void set_adc_mux(int which) {  
+  // mask so only proper bits are possible.  
+  PORTG = (PORTG & 0xE3) | ((which & 0x03) << 3) | (which & 0x04);
+}
+
+/**@}**/ //end defgroup
+
+
+ISR(ADC_vect) {
+	static volatile int adc_prev_loop_running = 0; 
+	int adc_h = 0;
+	int adc_l = 0;
+
+	//Store the value only if this read isn't for the BOM
+	if (ADMUX != BOM_PORT) {
+		adc_l = ADCL;
+		adc_h = ADCH;
+	
+		an_val[adc_current_port - 1].adc10 = (adc_h << 2) | (adc_l >> 6);
+		an_val[adc_current_port - 1].adc8 = adc_h;
+	}
+	
+	//Save the result only if we just turned off the loop
+	if (!adc_loop_running && !adc_prev_loop_running)
+		return;
+	
+	adc_prev_loop_running = adc_loop_running;
+	
+	//Skip AN7 because it is not a real port
+	if (adc_current_port == AN6) {
+		ADMUX = ADMUX_OPT | EXT_MUX;
+		set_adc_mux(AN8 - 8);
+		adc_current_port = AN8;
+	//Wrap around
+	} else if (adc_current_port == AN11) {
+		adc_current_port = AN1;
+		ADMUX = ADMUX_OPT | adc_current_port;
+	//Normal increment
+	} else {
+		adc_current_port++;
+	
+		if(adc_current_port < EXT_MUX) {
+			ADMUX = ADMUX_OPT | adc_current_port;
+		} else {
+			ADMUX = ADMUX_OPT | EXT_MUX;
+			set_adc_mux(adc_current_port - 8);
+		}
+	}
+
+	//Initiate next conversion only if we are running a loop
+	if (!adc_loop_running) {
+		return;
+    } else {
+    	ADCSRA |= _BV(ADSC);
+	}
+		
+	return;
+}
+
