Add LUFA-side channel open/close routines, add signalling handlers for the creation...
[pub/USBasp.git] / LUFA / Drivers / Peripheral / AVRU4U6U7 / ADC.h
index a7414ec..e307c4e 100644 (file)
@@ -29,6 +29,7 @@
 */\r
 \r
 /** \file\r
+ *  \brief ADC peripheral driver for the U7, U6 and U4 USB AVRs.\r
  *\r
  *  ADC driver for the AT90USB1287, AT90USB1286, AT90USB647, AT90USB646, ATMEGA16U4 and ATMEGA32U4 AVRs.\r
  *\r
@@ -62,7 +63,7 @@
                #endif\r
 \r
        /* Preprocessor Checks: */\r
-               #if !defined(INCLUDE_FROM_ADC_H)\r
+               #if !defined(__INCLUDE_FROM_ADC_H)\r
                        #error Do not include this file directly. Include LUFA/Drivers/Peripheral/ADC.h instead.\r
                #endif\r
 \r
                        \r
                        //@{\r
                        /** MUX mask define for the ADC0 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. */\r
-                       #define  ADC_CHANNEL0                    0x00\r
+                       #define  ADC_CHANNEL0                    (0x00 << MUX0)\r
 \r
                        /** MUX mask define for the ADC1 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. */\r
-                       #define  ADC_CHANNEL1                    0x01\r
+                       #define  ADC_CHANNEL1                    (0x01 << MUX0)\r
 \r
                        #if !(defined(__AVR_ATmega16U4__)  || defined(__AVR_ATmega32U4__) || defined(__DOXYGEN__))\r
                                /** MUX mask define for the ADC2 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading.\r
                                 *\r
                                 *  \note Note available on all AVR models.\r
                                 */\r
-                               #define  ADC_CHANNEL2                    0x02\r
+                               #define  ADC_CHANNEL2                (0x02 << MUX0)\r
 \r
                                /** MUX mask define for the ADC3 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading.\r
                                 *\r
                                 *  \note Note available on all AVR models.\r
                                 */\r
-                               #define  ADC_CHANNEL3                    0x03\r
+                               #define  ADC_CHANNEL3                (0x03 << MUX0)\r
                        #endif\r
 \r
                        /** MUX mask define for the ADC4 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. */\r
-                       #define  ADC_CHANNEL4                    0x04\r
+                       #define  ADC_CHANNEL4                    (0x04 << MUX0)\r
 \r
                        /** MUX mask define for the ADC5 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. */\r
-                       #define  ADC_CHANNEL5                    0x05\r
+                       #define  ADC_CHANNEL5                    (0x05 << MUX0)\r
 \r
                        /** MUX mask define for the ADC6 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. */\r
-                       #define  ADC_CHANNEL6                    0x06\r
+                       #define  ADC_CHANNEL6                    (0x06 << MUX0)\r
 \r
                        /** MUX mask define for the ADC7 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. */\r
-                       #define  ADC_CHANNEL7                    0x07\r
+                       #define  ADC_CHANNEL7                    (0x07 << MUX0)\r
 \r
                        /** MUX mask define for the internal 1.1V bandgap channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading. */\r
-                       #define  ADC_1100MV_BANDGAP              0x1E\r
+                       #define  ADC_1100MV_BANDGAP              (0x1E << MUX0)\r
                        \r
                        #if (defined(__AVR_ATmega16U4__)  || defined(__AVR_ATmega32U4__) || defined(__DOXYGEN__))\r
                                /** MUX mask define for the ADC8 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading.\r
                                 *\r
                                 *  \note Note available on all AVR models.\r
                                 */\r
-                               #define  ADC_CHANNEL8                0x20\r
+                               #define  ADC_CHANNEL8                ((1 << 8) | (0x00 << MUX0))\r
 \r
                                /** MUX mask define for the ADC9 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading.\r
                                 *\r
                                 *  \note Note available on all AVR models.\r
                                 */\r
-                               #define  ADC_CHANNEL9                0x21\r
+                               #define  ADC_CHANNEL9                ((1 << 8) | (0x01 << MUX0))\r
 \r
                                /** MUX mask define for the ADC10 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading.\r
                                 *\r
                                 *  \note Note available on all AVR models.\r
                                 */\r
-                               #define  ADC_CHANNEL10               0x22\r
+                               #define  ADC_CHANNEL10               ((1 << 8) | (0x02 << MUX0))\r
 \r
                                /** MUX mask define for the ADC11 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading.\r
                                 *\r
                                 *  \note Note available on all AVR models.\r
                                 */\r
-                               #define  ADC_CHANNEL11               0x23\r
+                               #define  ADC_CHANNEL11               ((1 << 8) | (0x03 << MUX0))\r
 \r
                                /** MUX mask define for the ADC12 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading.\r
                                 *\r
                                 *  \note Note available on all AVR models.\r
                                 */\r
-                               #define  ADC_CHANNEL12               0x24\r
+                               #define  ADC_CHANNEL12               ((1 << 8) | (0x04 << MUX0))\r
 \r
                                /** MUX mask define for the ADC13 channel of the ADC. See \ref ADC_StartReading and \ref ADC_GetChannelReading.\r
                                 *\r
                                 *  \note Note available on all AVR models.\r
                                 */\r
-                               #define  ADC_CHANNEL13               0x25\r
+                               #define  ADC_CHANNEL13               ((1 << 8) | (0x05 << MUX0))\r
 \r
                                /** MUX mask define for the internal temperature sensor channel of the ADC. See \ref ADC_StartReading and\r
                                 *  \ref ADC_GetChannelReading.\r
                                 *\r
                                 *  \note Note available on all AVR models.\r
                                 */\r
-                               #define  ADC_INT_TEMP_SENS           0x27                       \r
+                               #define  ADC_INT_TEMP_SENS           ((1 << 8) | (0x07 << MUX0))\r
                        #endif\r
                        //@}\r
                \r
                                 */\r
                                static inline bool ADC_IsReadingComplete(void);\r
                                \r
-                               /** Retrieves the conversion value of the last completed ADC conversion.\r
+                               /** Retrieves the conversion value of the last completed ADC conversion and clears the reading\r
+                                *  completion flag.\r
                                 *\r
                                 *  \return The result of the last ADC conversion\r
                                 */\r
                                \r
                                #define  ADC_GetStatus()               ((ADCSRA & (1 << ADEN)) ? true : false)\r
 \r
-                               #define  ADC_IsReadingComplete()        (ADCSRA & (1 << ADSC))\r
+                               #define  ADC_IsReadingComplete()       ((ADCSRA & (1 << ADIF)) ? true : false)\r
                                \r
-                               #define  ADC_GetResult()                 ADC                    \r
+                               #define  ADC_GetResult()                (ADCSRA |= (1 << ADIF), ADC)\r
                        #endif\r
                        \r
                /* Inline Functions: */\r
                         *  Once executed, the conversion status can be determined via the \ref ADC_IsReadingComplete() macro and\r
                         *  the result read via the \ref ADC_GetResult() macro.\r
                         *\r
+                        *  If the ADC has been initialized in free running mode, calling this function once will begin the repeated\r
+                        *  conversions. If the ADC is in single conversion mode (or the channel to convert from is to be changed),\r
+                        *  this function must be called each time a conversion is to take place.\r
+                        *\r
                         *  \param[in] MUXMask  Mask comprising of an ADC channel mask, reference mask and adjustment mask\r
                         */\r
-                       static inline void ADC_StartReading(const uint8_t MUXMask)\r
+                       static inline void ADC_StartReading(const uint16_t MUXMask)\r
                        {\r
                                ADMUX = MUXMask;\r
+                               \r
+                               #if (defined(__AVR_ATmega16U4__)  || defined(__AVR_ATmega32U4__) || defined(__DOXYGEN__))\r
+                               if (MUXMask & (1 << 8))\r
+                                 ADCSB |=  (1 << MUX5);\r
+                               else\r
+                                 ADCSB &= ~(1 << MUX5);\r
+                               #endif\r
                        \r
                                ADCSRA |= (1 << ADSC);\r
                        }\r
                        /** Performs a complete single reading from channel, including a polling spin-loop to wait for the\r
                         *  conversion to complete, and the returning of the converted value.\r
                         *\r
+                        *  \note For free running mode, the automated conversions should be initialized with a single call\r
+                        *        to \ref ADC_StartReading() to select the channel and begin the automated conversions, and\r
+                        *        the results read directly from the \ref ADC_GetResult() instead to reduce overhead.\r
+                        *\r
                         *  \param[in] MUXMask  Mask comprising of an ADC channel mask, reference mask and adjustment mask\r
                         */\r
-                       static inline uint16_t ADC_GetChannelReading(const uint8_t MUXMask) ATTR_WARN_UNUSED_RESULT;\r
-                       static inline uint16_t ADC_GetChannelReading(const uint8_t MUXMask)\r
+                       static inline uint16_t ADC_GetChannelReading(const uint16_t MUXMask) ATTR_WARN_UNUSED_RESULT;\r
+                       static inline uint16_t ADC_GetChannelReading(const uint16_t MUXMask)\r
                        {\r
                                ADC_StartReading(MUXMask);\r
        \r