Complete initial revision of the XMEGA Clock Management platform driver.
authorDean Camera <dean@fourwalledcubicle.com>
Thu, 14 Jul 2011 11:31:12 +0000 (11:31 +0000)
committerDean Camera <dean@fourwalledcubicle.com>
Thu, 14 Jul 2011 11:31:12 +0000 (11:31 +0000)
Start the USB clock source generator inside USB_ResetInterface() for XMEGA devices.

LUFA/CodeTemplates/makefile_template.uc3
LUFA/CodeTemplates/makefile_template.xmega
LUFA/Drivers/USB/Core/XMEGA/Endpoint_XMEGA.c
LUFA/Drivers/USB/Core/XMEGA/USBController_XMEGA.c
LUFA/Platform/UC3/ClockManagement.h
LUFA/Platform/XMEGA/ClockManagement.h

index 2077ca1..645f6c2 100644 (file)
@@ -70,10 +70,7 @@ F_CPU = ### INSERT PRESCALED SYSTEM CLOCK SPEED HERE, IN HZ ###
 \r
 # USB controller master clock frequency.\r
 #     This will define a symbol, F_USB, in all source code files equal to the\r
-#     input clock frequency (before any prescaling is performed) in Hz. This value may\r
-#     differ from F_CPU, as the USB clock is often sourced from the same oscillator as\r
-#     the CPU clock, but run through multipliers and dividers to achieve the desired\r
-#     clock rate.\r
+#     input clock frequency of the USB controller's clock generator in Hz.\r
 #\r
 #     For the UC3 chips, this should be equal to 48MHz or 96MHz.\r
 F_USB = ### INSERT CLOCK TO USB MODULE HERE, IN HZ ###\r
index 2ceca1f..719a107 100644 (file)
@@ -86,12 +86,10 @@ F_CPU = ### INSERT PRESCALED SYSTEM CLOCK SPEED HERE, IN HZ ###
 \r
 # USB controller master clock frequency.\r
 #     This will define a symbol, F_USB, in all source code files equal to the\r
-#     input clock frequency (before any prescaling is performed) in Hz. This value may\r
-#     differ from F_CPU, as the USB clock is often sourced from the same oscillator as\r
-#     the CPU clock, but run through multipliers and dividers to achieve the desired\r
-#     clock rate.\r
+#     input clock frequency of the USB controller's clock generator in Hz.\r
 #\r
-#     For the UC3 chips, this should be equal to 48MHz or 96MHz.\r
+#     For the XMEGA chips, this should be equal to a multiple of 6MHz for Low\r
+#     Speed USB mode, or a multiple of 48MHz for Full Speed USB mode.\r
 F_USB = ### INSERT CLOCK TO USB MODULE HERE, IN HZ ###\r
 \r
 \r
index 154020c..daace96 100644 (file)
@@ -43,7 +43,7 @@ bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number,
                                     const uint8_t UECFG0XData,\r
                                     const uint8_t UECFG1XData)\r
 {\r
-       // TODO\r
+       return false; // TODO\r
 }\r
 \r
 void Endpoint_ClearEndpoints(void)\r
index 2556ee8..c8839c9 100644 (file)
@@ -78,6 +78,11 @@ void USB_Disable(void)
 \r
 void USB_ResetInterface(void)\r
 {\r
+       if (USB_Options & USB_DEVICE_OPT_LOWSPEED)\r
+         CLK.USBCTRL = ((((F_USB / 6000000) - 1) << CLK_USBPSDIV_gp) | CLK_USBSRC_PLL_gc | CLK_USBEN_bm);\r
+       else\r
+         CLK.USBCTRL = ((((F_USB / 48000000) - 1) << CLK_USBPSDIV_gp) | CLK_USBSRC_PLL_gc | CLK_USBEN_bm);\r
+       \r
        USB_INT_DisableAllInterrupts();\r
        USB_INT_ClearAllInterrupts();\r
 \r
index d6fac35..125a8a9 100644 (file)
                         *\r
                         *  \return Boolean \c true if the external oscillator was successfully started, \c false if invalid parameters specified.\r
                         */\r
-                       static inline uint8_t AVR32CLK_StartExternalOscillator(const uint8_t Channel,\r
-                                                                              const uint8_t Type,\r
-                                                                              const uint8_t Startup) ATTR_ALWAYS_INLINE;\r
-                       static inline uint8_t AVR32CLK_StartExternalOscillator(const uint8_t Channel,\r
-                                                                              const uint8_t Type,\r
-                                                                              const uint8_t Startup)\r
+                       static inline bool AVR32CLK_StartExternalOscillator(const uint8_t Channel,\r
+                                                                           const uint8_t Type,\r
+                                                                           const uint8_t Startup) ATTR_ALWAYS_INLINE;\r
+                       static inline bool AVR32CLK_StartExternalOscillator(const uint8_t Channel,\r
+                                                                           const uint8_t Type,\r
+                                                                           const uint8_t Startup)\r
                        {\r
                                switch (Channel)\r
                                {\r
 \r
                        /** Starts the given PLL of the UC3 microcontroller, with the given options. This routine blocks until the PLL is ready for use.\r
                         *\r
+                        *  \note The output frequency must be equal to or greater than the source frequency.\r
+                        *\r
                         *  \param[in] Channel     Index of the PLL to start.\r
                         *  \param[in] Source      Clock source for the PLL, a value from \ref UC3_System_ClockSource_t.\r
                         *  \param[in] SourceFreq  Frequency of the PLL's clock source, in Hz.\r
                                                             const uint32_t SourceFreq,\r
                                                             const uint32_t Frequency)\r
                        {\r
+                               if (SourceFreq > Frequency)\r
+                                 return false;\r
+                       \r
                                switch (Source)\r
                                {\r
                                        case CLOCK_SRC_OSC0:\r
index de531c2..9e6a0f4 100644 (file)
  *\r
  *             void main(void)\r
  *             {\r
- *                     [TODO]\r
+ *                     // Start the internal 32MHz RC oscillator and switch the CPU core to run from it\r
+ *                     AVR32CLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);\r
+ *                     XMEGACLK_SetCPUClockSource(CLOCK_SRC_INT_RC32MHZ, F_CPU);\r
+ *\r
+ *                     // Start the external oscillator and multiply up the frequency\r
+ *                     AVR32CLK_StartExternalOscillator(EXOSC_FREQ_9MHZ_MAX, EXOSC_START_1KCLK);\r
+ *                     AVR32CLK_StartPLL(CLOCK_SRC_XOSC, 8000000, F_USB);\r
  *             }\r
  *  \endcode\r
  *\r
 \r
        /* Public Interface - May be used in end-application: */\r
                /* Macros: */\r
-                       /** Enum for the possible external oscillator types. */\r
-                       enum XMEGA_Extern_OSC_ClockTypes_t\r
+                       /** Enum for the possible external oscillator frequency ranges. */\r
+                       enum XMEGA_Extern_OSC_ClockFrequency_t\r
                        {\r
-                               EXOSC_MODE_2MHZ_MAX      = OSC_FRQRANGE_04TO2_gc,  /**< External crystal oscillator equal to or slower than 2MHz. */\r
-                               EXOSC_MODE_9MHZ_MAX      = OSC_FRQRANGE_2TO9_gc,   /**< External crystal oscillator equal to or slower than 9MHz. */\r
-                               EXOSC_MODE_12MHZ_MAX     = OSC_FRQRANGE_9TO12_gc,  /**< External crystal oscillator equal to or slower than 12MHz. */\r
-                               EXOSC_MODE_16MHZ_MAX     = OSC_FRQRANGE_12TO16_gc, /**< External crystal oscillator equal to or slower than 16MHz. */   \r
+                               EXOSC_FREQ_2MHZ_MAX      = OSC_FRQRANGE_04TO2_gc,  /**< External crystal oscillator equal to or slower than 2MHz. */\r
+                               EXOSC_FREQ_9MHZ_MAX      = OSC_FRQRANGE_2TO9_gc,   /**< External crystal oscillator equal to or slower than 9MHz. */\r
+                               EXOSC_FREQ_12MHZ_MAX     = OSC_FRQRANGE_9TO12_gc,  /**< External crystal oscillator equal to or slower than 12MHz. */\r
+                               EXOSC_FREQ_16MHZ_MAX     = OSC_FRQRANGE_12TO16_gc, /**< External crystal oscillator equal to or slower than 16MHz. */   \r
                        };\r
 \r
                        /** Enum for the possible external oscillator statup times. */\r
                        };\r
 \r
                /* Inline Functions: */\r
-                       /** Starts the given external oscillator of the UC3 microcontroller, with the given options. This routine blocks until\r
+                       /** Starts the external oscillator of the XMEGA microcontroller, with the given options. This routine blocks until\r
                         *  the oscillator is ready for use.\r
                         *\r
-                        *  \param[in] Channel  Index of the external oscillator to start.\r
-                        *  \param[in] Type     Type of clock attached to the given oscillator channel, a value from \ref XMEGA_Extern_OSC_ClockTypes_t.\r
-                        *  \param[in] Startup  Statup time of the external oscillator, a value from \ref XMEGA_Extern_OSC_ClockStartup_t.\r
+                        *  \param[in] FreqRange  Frequency range of the external oscillator, a value from \ref XMEGA_Extern_OSC_ClockFrequency_t.\r
+                        *  \param[in] Startup    Statup time of the external oscillator, a value from \ref XMEGA_Extern_OSC_ClockStartup_t.\r
                         *\r
                         *  \return Boolean \c true if the external oscillator was successfully started, \c false if invalid parameters specified.\r
                         */\r
-                       static inline uint8_t AVR32CLK_StartExternalOscillator(const uint8_t Channel,\r
-                                                                              const uint8_t Type,\r
-                                                                              const uint8_t Startup) ATTR_ALWAYS_INLINE;\r
-                       static inline uint8_t AVR32CLK_StartExternalOscillator(const uint8_t Channel,\r
-                                                                              const uint8_t Type,\r
-                                                                              const uint8_t Startup)\r
+                       static inline bool AVR32CLK_StartExternalOscillator(const uint8_t FreqRange,\r
+                                                                           const uint8_t Startup) ATTR_ALWAYS_INLINE;\r
+                       static inline bool AVR32CLK_StartExternalOscillator(const uint8_t FreqRange,\r
+                                                                           const uint8_t Startup)\r
                        {\r
-                               return false; // TODO\r
+                               OSC.XOSCCTRL  = (FreqRange | ((Startup == EXOSC_START_32KCLK) ? OSC_X32KLPM_bm : 0) | Startup);\r
+                               OSC.CTRL     |= OSC_XOSCEN_bm;\r
+                               \r
+                               while (!(OSC.STATUS & OSC_XOSCRDY_bm));                         \r
+                               return true;\r
                        }\r
 \r
-                       /** Stops the given external oscillator of the UC3 microcontroller.\r
-                        *\r
-                        *  \param[in] Channel  Index of the external oscillator to stop.\r
-                        */\r
-                       static inline void AVR32CLK_StopExternalOscillator(const uint8_t Channel) ATTR_ALWAYS_INLINE;\r
-                       static inline void AVR32CLK_StopExternalOscillator(const uint8_t Channel)\r
+                       /** Stops the external oscillator of the XMEGA microcontroller. */\r
+                       static inline void AVR32CLK_StopExternalOscillator(void) ATTR_ALWAYS_INLINE;\r
+                       static inline void AVR32CLK_StopExternalOscillator(void)\r
                        {\r
-                               return; // TODO\r
+                               OSC.CTRL     &= ~OSC_XOSCEN_bm;\r
                        }\r
 \r
-                       /** Starts the given PLL of the UC3 microcontroller, with the given options. This routine blocks until the PLL is ready for use.\r
+                       /** Starts the given internal oscillator of the XMEGA microcontroller, with the given options. This routine blocks until\r
+                        *  the oscillator is ready for use.\r
                         *\r
-                        *  \param[in] Channel     Index of the PLL to start.\r
-                        *  \param[in] Source      Clock source for the PLL, a value from \ref XMEGA_System_ClockSource_t.\r
-                        *  \param[in] SourceFreq  Frequency of the PLL's clock source, in Hz.\r
-                        *  \param[in] Frequency   Target frequency of the PLL's output.\r
+                        *  \param[in] Source  Internal oscillator to start, a value from \ref XMEGA_System_ClockSource_t.\r
                         *\r
-                        *  \return Boolean \c true if the PLL was successfully started, \c false if invalid parameters specified.\r
+                        *  \return Boolean \c true if the internal oscillator was successfully started, \c false if invalid parameters specified.\r
                         */\r
-                       static inline bool AVR32CLK_StartPLL(const uint8_t Channel,\r
-                                                            const uint8_t Source,\r
-                                                            const uint32_t SourceFreq,\r
-                                                            const uint32_t Frequency) ATTR_ALWAYS_INLINE;\r
-                       static inline bool AVR32CLK_StartPLL(const uint8_t Channel,\r
-                                                            const uint8_t Source,\r
-                                                            const uint32_t SourceFreq,\r
-                                                            const uint32_t Frequency)\r
+                       static inline uint8_t AVR32CLK_StartInternalOscillator(const uint8_t Source) ATTR_ALWAYS_INLINE;\r
+                       static inline uint8_t AVR32CLK_StartInternalOscillator(const uint8_t Source)\r
                        {\r
-                               return false; // TODO\r
+                               switch (Source)\r
+                               {\r
+                                       case CLOCK_SRC_INT_RC2MHZ:\r
+                                               OSC.CTRL |= OSC_RC2MEN_bm;\r
+                                               while (!(OSC.STATUS & OSC_RC2MRDY_bm));\r
+                                               return true;\r
+                                       case CLOCK_SRC_INT_RC32MHZ:\r
+                                               OSC.CTRL |= OSC_RC32MEN_bm;\r
+                                               while (!(OSC.STATUS & OSC_RC32MRDY_bm));                                        \r
+                                               return true;\r
+                                       case CLOCK_SRC_INT_RC32KHZ:\r
+                                               OSC.CTRL |= OSC_RC32KEN_bm;\r
+                                               while (!(OSC.STATUS & OSC_RC32KRDY_bm));                                        \r
+                                               return true;\r
+                               }\r
+                       \r
+                               return false;\r
                        }\r
 \r
-                       /** Stops the given PLL of the UC3 microcontroller.\r
+                       /** Stops the given internal oscillator of the XMEGA microcontroller.\r
                         *\r
-                        *  \param[in] Channel  Index of the PLL to stop.\r
-                        */\r
-                       static inline void AVR32CLK_StopPLL(const uint8_t Channel) ATTR_ALWAYS_INLINE;\r
-                       static inline void AVR32CLK_StopPLL(const uint8_t Channel)\r
+                        *  \param[in] Source  Internal oscillator to stop, a value from \ref XMEGA_System_ClockSource_t.\r
+                        *\r
+                        *  \return Boolean \c true if the internal oscillator was successfully stopped, \c false if invalid parameters specified.\r
+                        */                     \r
+                       static inline bool AVR32CLK_StopInternalOscillator(const uint8_t Source) ATTR_ALWAYS_INLINE;\r
+                       static inline bool AVR32CLK_StopInternalOscillator(const uint8_t Source)\r
                        {\r
-                               // TODO\r
-                       }\r
+                               switch (Source)\r
+                               {\r
+                                       case CLOCK_SRC_INT_RC2MHZ:\r
+                                               OSC.CTRL &= ~OSC_RC2MEN_bm;\r
+                                               return true;\r
+                                       case CLOCK_SRC_INT_RC32MHZ:\r
+                                               OSC.CTRL &= ~OSC_RC32MEN_bm;\r
+                                               return true;\r
+                                       case CLOCK_SRC_INT_RC32KHZ:\r
+                                               OSC.CTRL &= ~OSC_RC32KEN_bm;\r
+                                               return true;\r
+                               }\r
                        \r
-                       /** Starts the given Generic Clock of the UC3 microcontroller, with the given options.\r
+                               return false;\r
+                       }\r
+\r
+                       /** Starts the PLL of the XMEGA microcontroller, with the given options. This routine blocks until the PLL is ready for use.\r
                         *\r
-                        *  \param[in] Channel     Index of the Generic Clock to start.\r
-                        *  \param[in] Source      Clock source for the Generic Clock, a value from \ref XMEGA_System_ClockSource_t.\r
-                        *  \param[in] SourceFreq  Frequency of the Generic Clock's clock source, in Hz.\r
-                        *  \param[in] Frequency   Target frequency of the Generic Clock's output.\r
+                        *  \note The output frequency must be equal to or greater than the source frequency.\r
                         *\r
-                        *  \return Boolean \c true if the Generic Clock was successfully started, \c false if invalid parameters specified.\r
+                        *  \param[in] Source       Clock source for the PLL, a value from \ref XMEGA_System_ClockSource_t.\r
+                        *  \param[in] SourceFreq   Frequency of the PLL's clock source, in Hz.\r
+                        *  \param[in] Frequency    Target frequency of the PLL's output.\r
+                        *\r
+                        *  \return Boolean \c true if the PLL was successfully started, \c false if invalid parameters specified.\r
                         */\r
-                       static inline bool AVR32CLK_StartGenericClock(const uint8_t Channel,\r
-                                                                     const uint8_t Source,\r
-                                                                     const uint32_t SourceFreq,\r
-                                                                     const uint32_t Frequency) ATTR_ALWAYS_INLINE;\r
-                       static inline bool AVR32CLK_StartGenericClock(const uint8_t Channel,\r
-                                                                     const uint8_t Source,\r
-                                                                     const uint32_t SourceFreq,\r
-                                                                     const uint32_t Frequency)\r
+                       static inline bool AVR32CLK_StartPLL(const uint8_t Source,\r
+                                                            const uint32_t SourceFreq,\r
+                                                            const uint32_t Frequency) ATTR_ALWAYS_INLINE;\r
+                       static inline bool AVR32CLK_StartPLL(const uint8_t Source,\r
+                                                            const uint32_t SourceFreq,\r
+                                                            const uint32_t Frequency)\r
                        {\r
-                               return false; // TODO\r
-                       }\r
+                               uint8_t MulFactor = (Frequency / SourceFreq);\r
+                               \r
+                               if (SourceFreq > Frequency)\r
+                                 return false;                         \r
                        \r
-                       /** Stops the given generic clock of the UC3 microcontroller.\r
-                        *\r
-                        *  \param[in] Channel  Index of the generic clock to stop.\r
-                        */\r
-                       static inline void AVR32CLK_StopGenericClock(const uint8_t Channel) ATTR_ALWAYS_INLINE;\r
-                       static inline void AVR32CLK_StopGenericClock(const uint8_t Channel)\r
+                               switch (Source)\r
+                               {\r
+                                       case CLOCK_SRC_INT_RC2MHZ:\r
+                                               OSC.PLLCTRL = (OSC_PLLSRC_RC2M_gc  | MulFactor);\r
+                                               break;\r
+                                       case CLOCK_SRC_INT_RC32MHZ:\r
+                                               OSC.PLLCTRL = (OSC_PLLSRC_RC32M_gc | MulFactor);\r
+                                               break;\r
+                                       case CLOCK_SRC_XOSC:\r
+                                               OSC.PLLCTRL = (OSC_PLLSRC_XOSC_gc  | MulFactor);\r
+                                               break;\r
+                                       default:\r
+                                               return false;\r
+                               }\r
+\r
+                               OSC.CTRL |= OSC_PLLEN_bm;\r
+                               \r
+                               while (!(OSC.STATUS & OSC_PLLRDY_bm));\r
+                               return true;\r
+                       }\r
+\r
+                       /** Stops the PLL of the XMEGA microcontroller. */\r
+                       static inline void AVR32CLK_StopPLL(void) ATTR_ALWAYS_INLINE;\r
+                       static inline void AVR32CLK_StopPLL(void)\r
                        {\r
-                               // TODO\r
+                               OSC.CTRL &= ~OSC_PLLEN_bm;\r
                        }\r
                        \r
                        /** Sets the clock source for the main microcontroller core. The given clock source should be configured\r
                         *  and ready for use before this function is called.\r
                         *\r
-                        *  This function will configure the FLASH controller's wait states automatically to suit the given clock source.\r
-                        *\r
                         *  \param[in] Source      Clock source for the CPU core, a value from \ref XMEGA_System_ClockSource_t.\r
                         *  \param[in] SourceFreq  Frequency of the CPU core's clock source, in Hz.\r
                         *\r
                         *  \return Boolean \c true if the CPU core clock was sucessfully altered, \c false if invalid parameters specified.\r
                         */\r
-                       static inline bool AVR32CLK_SetCPUClockSource(const uint8_t Source,\r
+                       static inline bool XMEGACLK_SetCPUClockSource(const uint8_t Source,\r
                                                                      const uint32_t SourceFreq) ATTR_ALWAYS_INLINE;\r
-                       static inline bool AVR32CLK_SetCPUClockSource(const uint8_t Source,\r
+                       static inline bool XMEGACLK_SetCPUClockSource(const uint8_t Source,\r
                                                                      const uint32_t SourceFreq)\r
                        {\r
-                               return false // TODO\r
+                               uint8_t ClockSourceMask = 0;\r
+                       \r
+                               switch (Source)\r
+                               {\r
+                                       case CLOCK_SRC_INT_RC2MHZ:\r
+                                               ClockSourceMask = CLK_SCLKSEL_RC2M_gc;\r
+                                               break;\r
+                                       case CLOCK_SRC_INT_RC32MHZ:\r
+                                               ClockSourceMask = CLK_SCLKSEL_RC32M_gc;\r
+                                               break;\r
+                                       case CLOCK_SRC_INT_RC32KHZ:\r
+                                               ClockSourceMask = CLK_SCLKSEL_RC32K_gc;\r
+                                               break;\r
+                                       case CLOCK_SRC_XOSC:\r
+                                               ClockSourceMask = CLK_SCLKSEL_XOSC_gc;\r
+                                               break;\r
+                                       case CLOCK_SRC_PLL:\r
+                                               ClockSourceMask = CLK_SCLKSEL_PLL_gc;\r
+                                               break;\r
+                                       default:\r
+                                               return false;\r
+                               }\r
+                               \r
+                               uint_reg_t CurrentGlobalInt = GetGlobalInterruptMask();\r
+                               GlobalInterruptDisable();\r
+\r
+                               CCP      = CCP_IOREG_gc;\r
+                               CLK.CTRL = ClockSourceMask;\r
+                               \r
+                               SetGlobalInterruptMask(CurrentGlobalInt);\r
+                               \r
+                               Delay_MS(1);                            \r
+                               return (CLK.CTRL == ClockSourceMask);\r
                        }\r
 \r
        /* Disable C linkage for C++ Compilers: */\r