X-Git-Url: http://git.linex4red.de/pub/lufa.git/blobdiff_plain/5832bab02f9dedee5fae540f0e38b7417cddfb9d..fd96b288824caaa3ee4e5e03887f016de9df80cf:/LUFA/Drivers/Peripheral/AVRU4U6U7/ADC.h diff --git a/LUFA/Drivers/Peripheral/AVRU4U6U7/ADC.h b/LUFA/Drivers/Peripheral/AVRU4U6U7/ADC.h index 3824145bf..e307c4e09 100644 --- a/LUFA/Drivers/Peripheral/AVRU4U6U7/ADC.h +++ b/LUFA/Drivers/Peripheral/AVRU4U6U7/ADC.h @@ -1,21 +1,21 @@ /* LUFA Library - Copyright (C) Dean Camera, 2009. + Copyright (C) Dean Camera, 2010. dean [at] fourwalledcubicle [dot] com www.fourwalledcubicle.com */ /* - Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com) - - Permission to use, copy, modify, and distribute this software - and its documentation for any purpose and without fee is hereby - granted, provided that the above copyright notice appear in all - copies and that both that the copyright notice and this - permission notice and warranty disclaimer appear in supporting - documentation, and that the name of the author not be used in - advertising or publicity pertaining to distribution of the + Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the software without specific, written prior permission. The author disclaim all warranties with regard to this @@ -29,6 +29,7 @@ */ /** \file + * \brief ADC peripheral driver for the U7, U6 and U4 USB AVRs. * * ADC driver for the AT90USB1287, AT90USB1286, AT90USB647, AT90USB646, ATMEGA16U4 and ATMEGA32U4 AVRs. * @@ -39,6 +40,11 @@ /** \ingroup Group_ADC * @defgroup Group_ADC_AVRU4U6U7 Series U4, U6 and U7 Model ADC Driver * + * ADC driver for the AT90USB1287, AT90USB1286, AT90USB647, AT90USB646, ATMEGA16U4 and ATMEGA32U4 AVRs. + * + * \note This file should not be included directly. It is automatically included as needed by the ADC driver + * dispatch header located in LUFA/Drivers/Peripheral/ADC.h. + * * @{ */ @@ -57,7 +63,7 @@ #endif /* Preprocessor Checks: */ - #if !defined(INCLUDE_FROM_ADC_H) + #if !defined(__INCLUDE_FROM_ADC_H) #error Do not include this file directly. Include LUFA/Drivers/Peripheral/ADC.h instead. #endif @@ -108,7 +114,89 @@ /** 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 << MUX0) + /** MUX mask define for the ADC1 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. */ + #define ADC_CHANNEL1 (0x01 << MUX0) + + #if !(defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__) || defined(__DOXYGEN__)) + /** MUX mask define for the ADC2 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. + * + * \note Note available on all AVR models. + */ + #define ADC_CHANNEL2 (0x02 << MUX0) + + /** MUX mask define for the ADC3 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. + * + * \note Note available on all AVR models. + */ + #define ADC_CHANNEL3 (0x03 << MUX0) + #endif + + /** MUX mask define for the ADC4 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. */ + #define ADC_CHANNEL4 (0x04 << MUX0) + + /** MUX mask define for the ADC5 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. */ + #define ADC_CHANNEL5 (0x05 << MUX0) + + /** MUX mask define for the ADC6 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. */ + #define ADC_CHANNEL6 (0x06 << MUX0) + + /** MUX mask define for the ADC7 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. */ + #define ADC_CHANNEL7 (0x07 << MUX0) + + /** 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 << MUX0) + + #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 ((1 << 8) | (0x00 << MUX0)) + + /** 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 ((1 << 8) | (0x01 << MUX0)) + + /** 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 ((1 << 8) | (0x02 << MUX0)) + + /** 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 ((1 << 8) | (0x03 << MUX0)) + + /** 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 ((1 << 8) | (0x04 << MUX0)) + + /** 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 ((1 << 8) | (0x05 << MUX0)) + + /** 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 ((1 << 8) | (0x07 << MUX0)) + #endif + //@} + /* Pseudo-Function Macros: */ #if defined(__DOXYGEN__) /** Initializes the ADC, ready for conversions. This must be called before any other ADC operations. @@ -137,7 +225,8 @@ */ static inline bool ADC_IsReadingComplete(void); - /** Retrieves the conversion value of the last completed ADC conversion. + /** Retrieves the conversion value of the last completed ADC conversion and clears the reading + * completion flag. * * \return The result of the last ADC conversion */ @@ -149,9 +238,9 @@ #define ADC_GetStatus() ((ADCSRA & (1 << ADEN)) ? true : false) - #define ADC_IsReadingComplete() (!(ADCSRA & (1 << ADSC))) + #define ADC_IsReadingComplete() ((ADCSRA & (1 << ADIF)) ? true : false) - #define ADC_GetResult() ADC + #define ADC_GetResult() (ADCSRA |= (1 << ADIF), ADC) #endif /* Inline Functions: */ @@ -159,6 +248,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,22 +290,37 @@ * 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 + * If the ADC has been initialized in free running mode, calling this function once will begin the repeated + * conversions. If the ADC is in single conversion mode (or the channel to convert from is to be changed), + * this function must be called each time a conversion is to take place. + * + * \param[in] MUXMask Mask comprising of an ADC channel mask, reference mask and adjustment mask */ - static inline void ADC_StartReading(const uint8_t MUXMask) + static inline void ADC_StartReading(const uint16_t MUXMask) { ADMUX = MUXMask; + + #if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__) || defined(__DOXYGEN__)) + if (MUXMask & (1 << 8)) + ADCSB |= (1 << MUX5); + else + ADCSB &= ~(1 << MUX5); + #endif ADCSRA |= (1 << ADSC); } - /** Performs a complete single reading from channel, including a polling spinloop to wait for the + /** 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 + * \note For free running mode, the automated conversions should be initialized with a single call + * to \ref ADC_StartReading() to select the channel and begin the automated conversions, and + * the results read directly from the \ref ADC_GetResult() instead to reduce overhead. + * + * \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) + static inline uint16_t ADC_GetChannelReading(const uint16_t MUXMask) ATTR_WARN_UNUSED_RESULT; + static inline uint16_t ADC_GetChannelReading(const uint16_t MUXMask) { ADC_StartReading(MUXMask);