From b68f7f16499a96e85c7cef4afd9da1a5c69e6a08 Mon Sep 17 00:00:00 2001 From: Dean Camera Date: Mon, 25 Jan 2010 00:52:45 +0000 Subject: Added explicit ADC channel masks for the standard set of ADC channels, as the single-ended channel MUX masks are not equal to the channel number on some AVR models. Changed demos to use the new channel masks when using the ADC driver. --- Demos/Device/ClassDriver/AudioInput/AudioInput.c | 2 +- Demos/Device/ClassDriver/AudioInput/AudioInput.h | 3 + Demos/Device/LowLevel/AudioInput/AudioInput.c | 2 +- Demos/Device/LowLevel/AudioInput/AudioInput.h | 3 + LUFA/Drivers/Board/Temperature.c | 2 +- LUFA/Drivers/Board/Temperature.h | 9 ++- LUFA/Drivers/Peripheral/AVRU4U6U7/ADC.h | 83 +++++++++++++++++++++++- LUFA/ManPages/ChangeLog.txt | 2 + LUFA/ManPages/MigrationInformation.txt | 6 ++ Projects/AVRISP-MKII/Lib/V2Protocol.c | 2 +- Projects/AVRISP-MKII/Lib/V2Protocol.h | 14 +++- 11 files changed, 116 insertions(+), 12 deletions(-) diff --git a/Demos/Device/ClassDriver/AudioInput/AudioInput.c b/Demos/Device/ClassDriver/AudioInput/AudioInput.c index eeec921ee..b90ee5125 100644 --- a/Demos/Device/ClassDriver/AudioInput/AudioInput.c +++ b/Demos/Device/ClassDriver/AudioInput/AudioInput.c @@ -86,7 +86,7 @@ void SetupHardware(void) ADC_SetupChannel(MIC_IN_ADC_CHANNEL); /* Start the ADC conversion in free running mode */ - ADC_StartReading(ADC_REFERENCE_AVCC | ADC_RIGHT_ADJUSTED | MIC_IN_ADC_CHANNEL); + ADC_StartReading(ADC_REFERENCE_AVCC | ADC_RIGHT_ADJUSTED | MIC_IN_ADC_MUX_MASK); } /** Processes the next audio sample by reading the last ADC conversion and writing it to the audio diff --git a/Demos/Device/ClassDriver/AudioInput/AudioInput.h b/Demos/Device/ClassDriver/AudioInput/AudioInput.h index ddb643045..98fec7813 100644 --- a/Demos/Device/ClassDriver/AudioInput/AudioInput.h +++ b/Demos/Device/ClassDriver/AudioInput/AudioInput.h @@ -52,6 +52,9 @@ /* Macros: */ /** ADC channel number for the microphone input. */ #define MIC_IN_ADC_CHANNEL 2 + + /** ADC channel MUX mask for the microphone input. */ + #define MIC_IN_ADC_MUX_MASK ADC_CHANNEL2 /** Maximum ADC sample value for the microphone input. */ #define SAMPLE_MAX_RANGE 0xFFFF diff --git a/Demos/Device/LowLevel/AudioInput/AudioInput.c b/Demos/Device/LowLevel/AudioInput/AudioInput.c index 98c2e8b3e..2d718e53c 100644 --- a/Demos/Device/LowLevel/AudioInput/AudioInput.c +++ b/Demos/Device/LowLevel/AudioInput/AudioInput.c @@ -72,7 +72,7 @@ void SetupHardware(void) USB_Init(); /* Start the ADC conversion in free running mode */ - ADC_StartReading(ADC_REFERENCE_AVCC | ADC_RIGHT_ADJUSTED | MIC_IN_ADC_CHANNEL); + ADC_StartReading(ADC_REFERENCE_AVCC | ADC_RIGHT_ADJUSTED | MIC_IN_ADC_MUX_MASK); } /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs, and diff --git a/Demos/Device/LowLevel/AudioInput/AudioInput.h b/Demos/Device/LowLevel/AudioInput/AudioInput.h index ace1e9685..4585ab5b6 100644 --- a/Demos/Device/LowLevel/AudioInput/AudioInput.h +++ b/Demos/Device/LowLevel/AudioInput/AudioInput.h @@ -52,6 +52,9 @@ /** ADC channel number for the microphone input. */ #define MIC_IN_ADC_CHANNEL 2 + /** ADC channel MUX mask for the microphone input. */ + #define MIC_IN_ADC_MUX_MASK ADC_CHANNEL2 + /** Maximum ADC sample value for the microphone input. */ #define SAMPLE_MAX_RANGE 0xFFFF diff --git a/LUFA/Drivers/Board/Temperature.c b/LUFA/Drivers/Board/Temperature.c index 748e0484f..ea12bf766 100644 --- a/LUFA/Drivers/Board/Temperature.c +++ b/LUFA/Drivers/Board/Temperature.c @@ -45,7 +45,7 @@ static const uint16_t PROGMEM Temperature_Lookup[] = { int8_t Temperature_GetTemperature(void) { - uint16_t Temp_ADC = ADC_GetChannelReading(ADC_REFERENCE_AVCC | ADC_RIGHT_ADJUSTED | TEMP_ADC_CHANNEL); + uint16_t Temp_ADC = ADC_GetChannelReading(ADC_REFERENCE_AVCC | ADC_RIGHT_ADJUSTED | TEMP_ADC_CHANNEL_MASK); if (Temp_ADC > pgm_read_word(&Temperature_Lookup[0])) return TEMP_MIN_TEMP; diff --git a/LUFA/Drivers/Board/Temperature.h b/LUFA/Drivers/Board/Temperature.h index f7f00342f..1b2baf2fb 100644 --- a/LUFA/Drivers/Board/Temperature.h +++ b/LUFA/Drivers/Board/Temperature.h @@ -79,13 +79,16 @@ /* Public Interface - May be used in end-application: */ /* Macros: */ /** ADC channel number for the temperature sensor. */ - #define TEMP_ADC_CHANNEL 0 + #define TEMP_ADC_CHANNEL 0 + /** ADC channel MUX mask for the temperature sensor. */ + #define TEMP_ADC_CHANNEL_MASK ADC_CHANNEL0 + /** Minimum returnable temperature from the \ref Temperature_GetTemperature() function. */ - #define TEMP_MIN_TEMP TEMP_TABLE_OFFSET + #define TEMP_MIN_TEMP TEMP_TABLE_OFFSET /** Maximum returnable temperature from the \ref Temperature_GetTemperature() function. */ - #define TEMP_MAX_TEMP ((TEMP_TABLE_SIZE - 1) + TEMP_TABLE_OFFSET) + #define TEMP_MAX_TEMP ((TEMP_TABLE_SIZE - 1) + TEMP_TABLE_OFFSET) /* Pseudo-Function Macros: */ #if defined(__DOXYGEN__) diff --git a/LUFA/Drivers/Peripheral/AVRU4U6U7/ADC.h b/LUFA/Drivers/Peripheral/AVRU4U6U7/ADC.h index 472e1df47..b0a9b9e80 100644 --- a/LUFA/Drivers/Peripheral/AVRU4U6U7/ADC.h +++ b/LUFA/Drivers/Peripheral/AVRU4U6U7/ADC.h @@ -108,7 +108,81 @@ /** Sets the ADC input clock to prescale by a factor of 128 the AVR's system clock. */ #define ADC_PRESCALE_128 ((1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0)) + + //@{ + /** MUX mask define for the ADC0 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. */ + #define ADC_CHANNEL0 0x00 + + /** MUX mask define for the ADC1 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. */ + #define ADC_CHANNEL1 0x01 + + /** MUX mask define for the ADC2 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. */ + #define ADC_CHANNEL2 0x02 + + /** MUX mask define for the ADC3 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. */ + #define ADC_CHANNEL3 0x03 + + /** MUX mask define for the ADC4 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. */ + #define ADC_CHANNEL4 0x04 + /** MUX mask define for the ADC5 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. */ + #define ADC_CHANNEL5 0x05 + + /** MUX mask define for the ADC6 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. */ + #define ADC_CHANNEL6 0x06 + + /** MUX mask define for the ADC7 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. */ + #define ADC_CHANNEL7 0x07 + + /** MUX mask define for the internal 1.1V bandgap channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. */ + #define ADC_1100MV_BANDGAP 0x1E + + #if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__) || defined(__DOXYGEN__)) + /** MUX mask define for the ADC8 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. + * + * \note Note available on all AVR models. + */ + #define ADC_CHANNEL8 0x20 + + /** MUX mask define for the ADC9 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. + * + * \note Note available on all AVR models. + */ + #define ADC_CHANNEL9 0x21 + + /** MUX mask define for the ADC10 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. + * + * \note Note available on all AVR models. + */ + #define ADC_CHANNEL10 0x22 + + /** MUX mask define for the ADC11 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. + * + * \note Note available on all AVR models. + */ + #define ADC_CHANNEL11 0x23 + + /** MUX mask define for the ADC12 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. + * + * \note Note available on all AVR models. + */ + #define ADC_CHANNEL12 0x24 + + /** MUX mask define for the ADC13 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. + * + * \note Note available on all AVR models. + */ + #define ADC_CHANNEL13 0x25 + + /** MUX mask define for the internal temperature sensor channel of the ADC. See \ref ADC_StartReading and + * \ref ADC_GetChannelReading. + * + * \note Note available on all AVR models. + */ + #define ADC_INT_TEMP_SENS 0x27 + #endif + //@} + /* Pseudo-Function Macros: */ #if defined(__DOXYGEN__) /** Initializes the ADC, ready for conversions. This must be called before any other ADC operations. @@ -159,6 +233,11 @@ * associated port pin as an input and disables the digital portion of the I/O to reduce * power consumption. * + * \note This must only be called for ADC channels with are connected to a physical port + * pin of the AVR, denoted by its special alternative function ADCx. + * + * \note The channel number must be specified as an integer, and NOT a ADC_CHANNELx mask. + * * \param[in] Channel ADC channel number to set up for conversions */ static inline void ADC_SetupChannel(const uint8_t Channel) @@ -196,7 +275,7 @@ * Once executed, the conversion status can be determined via the \ref ADC_IsReadingComplete() macro and * the result read via the \ref ADC_GetResult() macro. * - * \param[in] MUXMask Mask comprising of an ADC channel number, reference mask and adjustment mask + * \param[in] MUXMask Mask comprising of an ADC channel mask, reference mask and adjustment mask */ static inline void ADC_StartReading(const uint8_t MUXMask) { @@ -208,7 +287,7 @@ /** Performs a complete single reading from channel, including a polling spin-loop to wait for the * conversion to complete, and the returning of the converted value. * - * \param[in] MUXMask Mask comprising of an ADC channel number, reference mask and adjustment mask + * \param[in] MUXMask Mask comprising of an ADC channel mask, reference mask and adjustment mask */ static inline uint16_t ADC_GetChannelReading(const uint8_t MUXMask) ATTR_WARN_UNUSED_RESULT; static inline uint16_t ADC_GetChannelReading(const uint8_t MUXMask) diff --git a/LUFA/ManPages/ChangeLog.txt b/LUFA/ManPages/ChangeLog.txt index e5bdbb3d1..8a46a5a1b 100644 --- a/LUFA/ManPages/ChangeLog.txt +++ b/LUFA/ManPages/ChangeLog.txt @@ -18,6 +18,8 @@ * sent or received in packed form in a single USB packet * - Added new MIDI send buffer flush routines to the MIDI Device and Host mode Class drivers, to flush packed events * - Added master mode hardware TWI driver + * - Added ADC MUX masks for the standard ADC input channels on all AVR models with an ADC, altered demos to use these masks + * as on some models, the channel number is not identical to its single-ended ADC MUX mask * * Changed: * - Slowed down software USART carried PDI programming in the AVRISP project to prevent transmission errors diff --git a/LUFA/ManPages/MigrationInformation.txt b/LUFA/ManPages/MigrationInformation.txt index cf81901c6..9f211186c 100644 --- a/LUFA/ManPages/MigrationInformation.txt +++ b/LUFA/ManPages/MigrationInformation.txt @@ -12,6 +12,12 @@ * * \section Sec_MigrationXXXXXX Migrating from 091223 to XXXXXX * + * Non-USB Library Components + * - Due to some ADC channels not being identical to their ADC MUX selection masks for single-ended conversions on some AVR models, + * the ADC driver now has explicit masks for each of the standard ADC channels. These masks should be used when calling the ADC + * functions to ensure proper operation across all AVR models. Note that the \ref ADC_SetupChannel() function is an exception, and + * should always be called with a channel number rather than a channel mask. + * * Host Mode * - The MIDI Host Class driver send and receive routines now operate on packed events, where multiple MIDI events may be * packed into a single USB packet. This means that the sending of MIDI events will now be delayed until the MIDI send diff --git a/Projects/AVRISP-MKII/Lib/V2Protocol.c b/Projects/AVRISP-MKII/Lib/V2Protocol.c index d6de77404..8d8f2003f 100644 --- a/Projects/AVRISP-MKII/Lib/V2Protocol.c +++ b/Projects/AVRISP-MKII/Lib/V2Protocol.c @@ -57,7 +57,7 @@ void V2Protocol_Init(void) /* Initialize the ADC converter for VTARGET level detection on supported AVR models */ ADC_Init(ADC_FREE_RUNNING | ADC_PRESCALE_128); ADC_SetupChannel(VTARGET_ADC_CHANNEL); - ADC_StartReading(VTARGET_ADC_CHANNEL | ADC_RIGHT_ADJUSTED | ADC_REFERENCE_AVCC); + ADC_StartReading(VTARGET_ADC_CHANNEL_MASK | ADC_RIGHT_ADJUSTED | ADC_REFERENCE_AVCC); #endif /* Millisecond timer initialization for managing the command timeout counter */ diff --git a/Projects/AVRISP-MKII/Lib/V2Protocol.h b/Projects/AVRISP-MKII/Lib/V2Protocol.h index 862ea48ef..5af9fe9bf 100644 --- a/Projects/AVRISP-MKII/Lib/V2Protocol.h +++ b/Projects/AVRISP-MKII/Lib/V2Protocol.h @@ -56,14 +56,22 @@ #endif /* Macros: */ + #if !defined(__DOXYGEN__) + #define _GETADCMUXMASK2(x, y) x ## y + #define _GETADCMUXMASK(x, y) _GETADCMUXMASK2(x, y) + #endif + /** Programmer ID string, returned to the host during the CMD_SIGN_ON command processing */ - #define PROGRAMMER_ID "AVRISP_MK2" + #define PROGRAMMER_ID "AVRISP_MK2" /** Timeout period for each issued command from the host before it is aborted */ - #define COMMAND_TIMEOUT_MS 200 + #define COMMAND_TIMEOUT_MS 200 /** Command timeout counter register, GPIOR for speed */ - #define TimeoutMSRemaining GPIOR0 + #define TimeoutMSRemaining GPIOR0 + + /** MUX mask for the VTARGET ADC channel number */ + #define VTARGET_ADC_CHANNEL_MASK _GETADCMUXMASK(ADC_CHANNEL, VTARGET_ADC_CHANNEL) /* External Variables: */ extern uint32_t CurrentAddress; -- cgit v1.2.3