[tuxdroid-svn] r583 - firmware/tuxcore/trunk
Status: Beta
Brought to you by:
ks156
From: jaguarondi <c2m...@c2...> - 2007-10-08 11:43:53
|
Author: jaguarondi Date: 2007-10-08 13:43:17 +0200 (Mon, 08 Oct 2007) New Revision: 583 Modified: firmware/tuxcore/trunk/Makefile firmware/tuxcore/trunk/global.h firmware/tuxcore/trunk/hardware.h firmware/tuxcore/trunk/main.c firmware/tuxcore/trunk/sensors.c firmware/tuxcore/trunk/sensors.h firmware/tuxcore/trunk/standalone.c Log: * Added a new ADC module in order to do battery and light measurements with the same ADC. The functionality doesn't change in this commit but functions and files have been reorganised. Modified: firmware/tuxcore/trunk/Makefile =================================================================== --- firmware/tuxcore/trunk/Makefile 2007-10-05 08:27:46 UTC (rev 582) +++ firmware/tuxcore/trunk/Makefile 2007-10-08 11:43:17 UTC (rev 583) @@ -90,8 +90,8 @@ HEADERS = $(wildcards *.h) ## Objects that must be built in order to link -OBJECTS = main.o sensors.o motors.o global.o led.o communication.o i2c.o \ - fifo.o ir.o parser.o config.o standalone.o +OBJECTS = main.o adc.o sensors.o motors.o global.o led.o communication.o \ + i2c.o fifo.o ir.o parser.o config.o standalone.o ## Build all: svnrev.h $(TARGET) tuxcore.hex tuxcore.eep tuxcore.lss size @@ -101,6 +101,9 @@ main.o: main.c $(CC) $(INCLUDES) $(CFLAGS) -c $< +adc.o: adc.c + $(CC) $(INCLUDES) $(CFLAGS) -c $< + sensors.o: sensors.c $(CC) $(INCLUDES) $(CFLAGS) -c $< @@ -219,5 +222,5 @@ -U eeprom:w:$(PROJECT).eep progstk_bl: $(PROJECT).hex $(PROJECT).eep $(PROJECT)_bl.hex - $(AVRDUDE) -p $(MCU) -c stk500v2 -e -U flash:w:$(PROJECT).hex \ + $(AVRDUDE) -p $(MCU) -c stk500v2 -e -U flash:w:$(PROJECT).hex \ -U eeprom:w:$(PROJECT).eep -D -U flash:w:$(PROJECT)_bl.hex Modified: firmware/tuxcore/trunk/global.h =================================================================== --- firmware/tuxcore/trunk/global.h 2007-10-05 08:27:46 UTC (rev 582) +++ firmware/tuxcore/trunk/global.h 2007-10-08 11:43:17 UTC (rev 583) @@ -30,6 +30,17 @@ #include "common/commands.h" /* + * Custom types + */ +/** Union between 16 bits variables and the two 8 bits registers they're made + * of. */ +typedef union +{ + uint16_t w; + uint8_t b[2]; +} union16_t; + +/* * -------------------------------------------------------- * STATUS * -------------------------------------------------------- Modified: firmware/tuxcore/trunk/hardware.h =================================================================== --- firmware/tuxcore/trunk/hardware.h 2007-10-05 08:27:46 UTC (rev 582) +++ firmware/tuxcore/trunk/hardware.h 2007-10-08 11:43:17 UTC (rev 583) @@ -140,6 +140,9 @@ #define LIGHT_PU_DDR DDRC /** Phototransistor extra pull-up mask. */ #define LIGHT_PU_MK _BV(PC0) +/** ADMUX settings for light measurement. + * AREF reference, result right adjusted, MUX to ADC6. */ +#define LIGHT_ADMUX 0x06 /*! @} */ /** @@ -257,6 +260,15 @@ #define MOT_SPIN_MK (MOT_SPIN_L_MK | MOT_SPIN_R_MK) /*! @} */ +/** + * \name Battery + * The battery voltage can be measured on the pin ADC7 through a resistive + * divider. + * @{ */ +/** ADMUX settings for battery measurement. + * AREF reference, result right adjusted, MUX to ADC7. */ +#define BATTERY_ADMUX 0x07 /*! @} */ +/*! @} */ #endif /* _HARDWARE_H_ */ Modified: firmware/tuxcore/trunk/main.c =================================================================== --- firmware/tuxcore/trunk/main.c 2007-10-05 08:27:46 UTC (rev 582) +++ firmware/tuxcore/trunk/main.c 2007-10-08 11:43:17 UTC (rev 583) @@ -144,6 +144,7 @@ initIR(); main_tick_init(); initIO(); + sensors_init(); config_init(); /* I2C communication initialization */ @@ -239,13 +240,6 @@ /* Set output for Led's */ LED_DDR |= LED_MK; - /* Enable ADC, select ADC clock = F_CPU / 128 (i.e. 16 Hz) - * Single conversion mode - */ - ADCSRA = _BV(ADEN) | _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0); - ADMUX = 0x06; /* ADC6 selected XXX change this when adding ADC7 too */ - ADCSRA |= _BV(ADIE) | _BV(ADSC); /* set ADC interrupt and start it */ - /* set external I/O as pull-up */ EXIO_PT |= EXIO_MK; @@ -332,6 +326,7 @@ /* Set power savings configuration. */ cli(); closeIO(); + sensors_disable(); powersave_movements(); stop_spinning(); stop_mouth(); @@ -356,6 +351,7 @@ i2cCommunicationInit(); init_movements(); initIO(); + sensors_init(); /* Trigger the post-sleep event */ cond_flags.sleep = COND_POST_SLEEP; Modified: firmware/tuxcore/trunk/sensors.c =================================================================== --- firmware/tuxcore/trunk/sensors.c 2007-10-05 08:27:46 UTC (rev 582) +++ firmware/tuxcore/trunk/sensors.c 2007-10-08 11:43:17 UTC (rev 583) @@ -26,52 +26,69 @@ #include <avr/interrupt.h> +#include "sensors.h" +#include "adc.h" #include "global.h" #include "hardware.h" /** - \brief ADC conversion interrupt handling light and battery level + * \name Light level + * @{ */ +/** \ingroup sensors */ - The light sensor has 2 different values of pull-up resistors (1M and 10k) to - increase the range. It's necessary to switch from one to the other resistor - depending on the light level. This is done here. - - \todo TODO add battery level measurement - */ -ISR(SIG_ADC) +void save_light(void) { + union16_t adc_value; + adc_value.w = ADC_read(); /* Save light mode before changing it below. */ gStatus.lightM = LIGHT_PU_PORT & LIGHT_PU_MK; /* Check if it's necessary to change the light mode. */ - if ((ADCL <= 0x28) && (ADCH == 0x00) && !(LIGHT_PU_PORT & LIGHT_PU_MK)) + if ((adc_value.b[0] <= 0x28) && (adc_value.b[1] == 0x00) && + !(LIGHT_PU_PORT & LIGHT_PU_MK)) { LIGHT_PU_PORT |= LIGHT_PU_MK; LIGHT_PU_DDR |= LIGHT_PU_MK; } - else if ((ADCL >= 0xE8) && (ADCH == 0x03) && (LIGHT_PU_PORT & LIGHT_PU_MK)) + else if ((adc_value.b[0] >= 0xE8) && (adc_value.b[1] == 0x03) && + (LIGHT_PU_PORT & LIGHT_PU_MK)) { LIGHT_PU_PORT &= ~LIGHT_PU_MK; LIGHT_PU_DDR &= ~LIGHT_PU_MK; } - /* ADCH should be read after ADCL otherwise ADCL stays wite protected (c.f. - * datasheet). */ - gStatus.lightL = ADCL; - gStatus.lightH = ADCH; + /* adc_value.b[1] should be read after adc_value.b[0] otherwise + * adc_value.b[0] stays wite protected (c.f. datasheet). */ + gStatus.lightL = adc_value.b[0]; + gStatus.lightH = adc_value.b[1]; /* There's a new light value. */ light_f = 1; } /** - * \name Light level - * @{ */ -/** \ingroup sensors */ + \brief Initialize the sensors for normal operation. + */ +void sensors_init(void) +{ + ADC_set_ISR_callback(save_light); + ADC_init(); +} + /** \brief Start a light level measurement. - Start a light level conversion. The result will be saved by the ADC interrupt. + Start a light level conversion. The result will be saved by the ADC + interrupt. */ void getLight(void) { - ADCSRA |= _BV(ADSC); + ADC_start(LIGHT_ADMUX); } + +/** + \brief Disable all sensors. + This function should minimize power consumption. + */ +void sensors_disable(void) +{ + ADC_disable(); +} /*! @} */ Modified: firmware/tuxcore/trunk/sensors.h =================================================================== --- firmware/tuxcore/trunk/sensors.h 2007-10-05 08:27:46 UTC (rev 582) +++ firmware/tuxcore/trunk/sensors.h 2007-10-08 11:43:17 UTC (rev 583) @@ -34,6 +34,8 @@ #ifndef _SENSORS_H_ #define _SENSORS_H_ +void sensors_init(void); +void sensors_disable(void); void getLight(void); #endif /* _SENSORS_H_ */ Modified: firmware/tuxcore/trunk/standalone.c =================================================================== --- firmware/tuxcore/trunk/standalone.c 2007-10-05 08:27:46 UTC (rev 582) +++ firmware/tuxcore/trunk/standalone.c 2007-10-08 11:43:17 UTC (rev 583) @@ -86,11 +86,10 @@ /* Startup */ if (cond_flags.startup) { - if (gStatus.lightL == 0) - return; /* wait for a random value at startup */ cond_flags.startup = 0; launchActions((const uint8_t *)&startup_e); - tux_ir_id = gStatus.lightL; /* get some value for the id */ + tux_ir_id = gStatus.lightL; /* XXX remove this when fixing the greeting + function */ } /* Head button */ |