*\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