From ffc451fbd9631b4e0a45bce36dda3e34facee2de Mon Sep 17 00:00:00 2001 From: Dean Date: Wed, 21 Sep 2011 06:35:28 +0000 Subject: Add LUFA-111009-BETA tag. git-svn-id: http://lufa-lib.googlecode.com/svn/tags/LUFA-111009-BETA@1929 d5102386-fcda-11dd-9fdb-3debd5008f28 --- LUFA/Platform/UC3/ClockManagement.h | 643 ++++++++++++++++---------------- LUFA/Platform/UC3/Exception.S | 250 ++++++------- LUFA/Platform/UC3/InterruptManagement.c | 124 +++--- LUFA/Platform/UC3/InterruptManagement.h | 324 ++++++++-------- LUFA/Platform/XMEGA/ClockManagement.h | 520 +++++++++++++++----------- 5 files changed, 971 insertions(+), 890 deletions(-) (limited to 'LUFA/Platform') diff --git a/LUFA/Platform/UC3/ClockManagement.h b/LUFA/Platform/UC3/ClockManagement.h index d6fac358..bb5679ad 100644 --- a/LUFA/Platform/UC3/ClockManagement.h +++ b/LUFA/Platform/UC3/ClockManagement.h @@ -1,319 +1,324 @@ -/* - LUFA Library - Copyright (C) Dean Camera, 2011. - - dean [at] fourwalledcubicle [dot] com - www.lufa-lib.org -*/ - -/* - Copyright 2011 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 - software, including all implied warranties of merchantability - and fitness. In no event shall the author be liable for any - special, indirect or consequential damages or any damages - whatsoever resulting from loss of use, data or profits, whether - in an action of contract, negligence or other tortious action, - arising out of or in connection with the use or performance of - this software. -*/ - -/** \file - * \brief Module Clock Driver for the AVR32 UC3 microcontrollers. - * - * Clock management driver for the AVR32 UC3 microcontrollers. This driver allows for the configuration - * of the various clocks within the device to clock the various peripherals. - */ - -/** \ingroup Group_PlatformDrivers - * \defgroup Group_PlatformDrivers_UC3Clocks UC3 Clock Management Driver - LUFA/Platform/UC3/ClockManagement.h - * \brief Module Clock Driver for the AVR32 UC3 microcontrollers. - * - * \section Sec_Dependencies Module Source Dependencies - * The following files must be built with any user project that uses this module: - * - None - * - * \section Sec_ModDescription Module Description - * Clock management driver for the AVR32 UC3 microcontrollers. This driver allows for the configuration - * of the various clocks within the device to clock the various peripherals. - * - * Usage Example: - * \code - * #include - * - * void main(void) - * { - * // Start the master external oscillator which will be used as the main clock reference - * AVR32CLK_StartExternalOscillator(0, EXOSC_MODE_8MHZ_OR_MORE, EXOSC_START_0CLK); - * - * // Start the PLL for the CPU clock, switch CPU to it - * AVR32CLK_StartPLL(0, CLOCK_SRC_OSC0, 12000000, F_CPU); - * AVR32CLK_SetCPUClockSource(CLOCK_SRC_PLL0, F_CPU); - * - * // Start the PLL for the USB Generic Clock module - * AVR32CLK_StartPLL(1, CLOCK_SRC_OSC0, 12000000, 48000000); - * } - * \endcode - * - * @{ - */ - -#ifndef _UC3_CLOCK_MANAGEMENT_H_ -#define _UC3_CLOCK_MANAGEMENT_H_ - - /* Includes: */ - #include - - /* Enable C linkage for C++ Compilers: */ - #if defined(__cplusplus) - extern "C" { - #endif - - /* Public Interface - May be used in end-application: */ - /* Macros: */ - /** Enum for the possible external oscillator types. */ - enum UC3_Extern_OSC_ClockTypes_t - { - EXOSC_MODE_CLOCK = AVR32_PM_OSCCTRL0_MODE_EXT_CLOCK, /**< External clock (non-crystal) mode. */ - EXOSC_MODE_900KHZ_MAX = AVR32_PM_OSCCTRL0_MODE_CRYSTAL_G0, /**< External crystal oscillator equal to or slower than 900KHz. */ - EXOSC_MODE_3MHZ_MAX = AVR32_PM_OSCCTRL0_MODE_CRYSTAL_G1, /**< External crystal oscillator equal to or slower than 3MHz. */ - EXOSC_MODE_8MHZ_MAX = AVR32_PM_OSCCTRL0_MODE_CRYSTAL_G2, /**< External crystal oscillator equal to or slower than 8MHz. */ - EXOSC_MODE_8MHZ_OR_MORE = AVR32_PM_OSCCTRL0_MODE_CRYSTAL_G3, /**< External crystal oscillator equal to or faster than 8MHz. */ - }; - - /** Enum for the possible external oscillator statup times. */ - enum UC3_Extern_OSC_ClockStartup_t - { - EXOSC_START_0CLK = AVR32_PM_OSCCTRL0_STARTUP_0_RCOSC, /**< Immediate startup, no delay. */ - EXOSC_START_64CLK = AVR32_PM_OSCCTRL0_STARTUP_64_RCOSC, /**< Wait 64 clock cyles before startup for stability. */ - EXOSC_START_128CLK = AVR32_PM_OSCCTRL0_STARTUP_128_RCOSC, /**< Wait 128 clock cyles before startup for stability. */ - EXOSC_START_2048CLK = AVR32_PM_OSCCTRL0_STARTUP_2048_RCOSC, /**< Wait 2048 clock cyles before startup for stability. */ - EXOSC_START_4096CLK = AVR32_PM_OSCCTRL0_STARTUP_4096_RCOSC, /**< Wait 4096 clock cyles before startup for stability. */ - EXOSC_START_8192CLK = AVR32_PM_OSCCTRL0_STARTUP_8192_RCOSC, /**< Wait 8192 clock cyles before startup for stability. */ - EXOSC_START_16384CLK = AVR32_PM_OSCCTRL0_STARTUP_16384_RCOSC, /**< Wait 16384 clock cyles before startup for stability. */ - }; - - /** Enum for the possible module clock sources. */ - enum UC3_System_ClockSource_t - { - CLOCK_SRC_SLOW_CLK = 0, /**< Clock sourced from the internal slow clock. */ - CLOCK_SRC_OSC0 = 1, /**< Clock sourced from the Oscillator 0 clock. */ - CLOCK_SRC_OSC1 = 2, /**< Clock sourced from the Oscillator 1 clock. */ - CLOCK_SRC_PLL0 = 3, /**< Clock sourced from the PLL 0 clock. */ - CLOCK_SRC_PLL1 = 4, /**< Clock sourced from the PLL 1 clock. */ - }; - - /* Inline Functions: */ - /** Starts the given external oscillator of the UC3 microcontroller, with the given options. This routine blocks until - * the oscillator is ready for use. - * - * \param[in] Channel Index of the external oscillator to start. - * \param[in] Type Type of clock attached to the given oscillator channel, a value from \ref UC3_Extern_OSC_ClockTypes_t. - * \param[in] Startup Statup time of the external oscillator, a value from \ref UC3_Extern_OSC_ClockStartup_t. - * - * \return Boolean \c true if the external oscillator was successfully started, \c false if invalid parameters specified. - */ - static inline uint8_t AVR32CLK_StartExternalOscillator(const uint8_t Channel, - const uint8_t Type, - const uint8_t Startup) ATTR_ALWAYS_INLINE; - static inline uint8_t AVR32CLK_StartExternalOscillator(const uint8_t Channel, - const uint8_t Type, - const uint8_t Startup) - { - switch (Channel) - { - case 0: - AVR32_PM.OSCCTRL0.startup = Startup; - AVR32_PM.OSCCTRL0.mode = Type; - break; - case 1: - AVR32_PM.OSCCTRL1.startup = Startup; - AVR32_PM.OSCCTRL1.mode = Type; - break; - default: - return false; - } - - AVR32_PM.mcctrl |= (1 << (AVR32_PM_MCCTRL_OSC0EN_OFFSET + Channel)); - - while (!(AVR32_PM.poscsr & (1 << (AVR32_PM_POSCSR_OSC0RDY_OFFSET + Channel)))); - return true; - } - - /** Stops the given external oscillator of the UC3 microcontroller. - * - * \param[in] Channel Index of the external oscillator to stop. - */ - static inline void AVR32CLK_StopExternalOscillator(const uint8_t Channel) ATTR_ALWAYS_INLINE; - static inline void AVR32CLK_StopExternalOscillator(const uint8_t Channel) - { - AVR32_PM.mcctrl &= ~(1 << (AVR32_PM_MCCTRL_OSC0EN_OFFSET + Channel)); - } - - /** Starts the given PLL of the UC3 microcontroller, with the given options. This routine blocks until the PLL is ready for use. - * - * \param[in] Channel Index of the PLL to start. - * \param[in] Source Clock source for the PLL, a value from \ref UC3_System_ClockSource_t. - * \param[in] SourceFreq Frequency of the PLL's clock source, in Hz. - * \param[in] Frequency Target frequency of the PLL's output. - * - * \return Boolean \c true if the PLL was successfully started, \c false if invalid parameters specified. - */ - static inline bool AVR32CLK_StartPLL(const uint8_t Channel, - const uint8_t Source, - const uint32_t SourceFreq, - const uint32_t Frequency) ATTR_ALWAYS_INLINE; - static inline bool AVR32CLK_StartPLL(const uint8_t Channel, - const uint8_t Source, - const uint32_t SourceFreq, - const uint32_t Frequency) - { - switch (Source) - { - case CLOCK_SRC_OSC0: - AVR32_PM.PLL[Channel].pllosc = 0; - break; - case CLOCK_SRC_OSC1: - AVR32_PM.PLL[Channel].pllosc = 1; - break; - default: - return false; - } - - AVR32_PM.PLL[Channel].pllmul = (Frequency / SourceFreq) ? (((Frequency / SourceFreq) - 1) / 2) : 0; - AVR32_PM.PLL[Channel].plldiv = 0; - AVR32_PM.PLL[Channel].pllen = true; - - while (!(AVR32_PM.poscsr & (1 << (AVR32_PM_POSCSR_LOCK0_OFFSET + Channel)))); - return true; - } - - /** Stops the given PLL of the UC3 microcontroller. - * - * \param[in] Channel Index of the PLL to stop. - */ - static inline void AVR32CLK_StopPLL(const uint8_t Channel) ATTR_ALWAYS_INLINE; - static inline void AVR32CLK_StopPLL(const uint8_t Channel) - { - AVR32_PM.PLL[Channel].pllen = false; - } - - /** Starts the given Generic Clock of the UC3 microcontroller, with the given options. - * - * \param[in] Channel Index of the Generic Clock to start. - * \param[in] Source Clock source for the Generic Clock, a value from \ref UC3_System_ClockSource_t. - * \param[in] SourceFreq Frequency of the Generic Clock's clock source, in Hz. - * \param[in] Frequency Target frequency of the Generic Clock's output. - * - * \return Boolean \c true if the Generic Clock was successfully started, \c false if invalid parameters specified. - */ - static inline bool AVR32CLK_StartGenericClock(const uint8_t Channel, - const uint8_t Source, - const uint32_t SourceFreq, - const uint32_t Frequency) ATTR_ALWAYS_INLINE; - static inline bool AVR32CLK_StartGenericClock(const uint8_t Channel, - const uint8_t Source, - const uint32_t SourceFreq, - const uint32_t Frequency) - { - switch (Source) - { - case CLOCK_SRC_OSC0: - AVR32_PM.GCCTRL[Channel].pllsel = false; - AVR32_PM.GCCTRL[Channel].oscsel = 0; - break; - case CLOCK_SRC_OSC1: - AVR32_PM.GCCTRL[Channel].pllsel = false; - AVR32_PM.GCCTRL[Channel].oscsel = 1; - break; - case CLOCK_SRC_PLL0: - AVR32_PM.GCCTRL[Channel].pllsel = true; - AVR32_PM.GCCTRL[Channel].oscsel = 0; - break; - case CLOCK_SRC_PLL1: - AVR32_PM.GCCTRL[Channel].pllsel = true; - AVR32_PM.GCCTRL[Channel].oscsel = 1; - break; - default: - return false; - } - - if (SourceFreq < Frequency) - return false; - - AVR32_PM.GCCTRL[Channel].diven = (SourceFreq > Frequency) ? true : false; - AVR32_PM.GCCTRL[Channel].div = (((SourceFreq / Frequency) - 1) / 2); - AVR32_PM.GCCTRL[Channel].cen = true; - - return true; - } - - /** Stops the given generic clock of the UC3 microcontroller. - * - * \param[in] Channel Index of the generic clock to stop. - */ - static inline void AVR32CLK_StopGenericClock(const uint8_t Channel) ATTR_ALWAYS_INLINE; - static inline void AVR32CLK_StopGenericClock(const uint8_t Channel) - { - AVR32_PM.GCCTRL[Channel].cen = false; - } - - /** Sets the clock source for the main microcontroller core. The given clock source should be configured - * and ready for use before this function is called. - * - * This function will configure the FLASH controller's wait states automatically to suit the given clock source. - * - * \param[in] Source Clock source for the CPU core, a value from \ref UC3_System_ClockSource_t. - * \param[in] SourceFreq Frequency of the CPU core's clock source, in Hz. - * - * \return Boolean \c true if the CPU core clock was sucessfully altered, \c false if invalid parameters specified. - */ - static inline bool AVR32CLK_SetCPUClockSource(const uint8_t Source, - const uint32_t SourceFreq) ATTR_ALWAYS_INLINE; - static inline bool AVR32CLK_SetCPUClockSource(const uint8_t Source, - const uint32_t SourceFreq) - { - AVR32_FLASHC.FCR.fws = (SourceFreq > 30000000) ? true : false; - - switch (Source) - { - #if defined(AVR32_PM_MCCTRL_MCSEL_SLOW) - case CLOCK_SRC_SLOW_CLK: - AVR32_PM.MCCTRL.mcsel = AVR32_PM_MCCTRL_MCSEL_SLOW; - break; - #endif - #if defined(AVR32_PM_MCCTRL_MCSEL_OSC0) - case CLOCK_SRC_OSC0: - AVR32_PM.MCCTRL.mcsel = AVR32_PM_MCCTRL_MCSEL_OSC0; - break; - #endif - #if defined(AVR32_PM_MCCTRL_MCSEL_PLL0) - case CLOCK_SRC_PLL0: - AVR32_PM.MCCTRL.mcsel = AVR32_PM_MCCTRL_MCSEL_PLL0; - break; - #endif - default: - return false; - } - - return true; - } - - /* Disable C linkage for C++ Compilers: */ - #if defined(__cplusplus) - } - #endif - -#endif - -/** @} */ +/* + LUFA Library + Copyright (C) Dean Camera, 2011. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2011 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 + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +/** \file + * \brief Module Clock Driver for the AVR32 UC3 microcontrollers. + * + * Clock management driver for the AVR32 UC3 microcontrollers. This driver allows for the configuration + * of the various clocks within the device to clock the various peripherals. + */ + +/** \ingroup Group_PlatformDrivers + * \defgroup Group_PlatformDrivers_UC3Clocks UC3 Clock Management Driver - LUFA/Platform/UC3/ClockManagement.h + * \brief Module Clock Driver for the AVR32 UC3 microcontrollers. + * + * \section Sec_Dependencies Module Source Dependencies + * The following files must be built with any user project that uses this module: + * - None + * + * \section Sec_ModDescription Module Description + * Clock management driver for the AVR32 UC3 microcontrollers. This driver allows for the configuration + * of the various clocks within the device to clock the various peripherals. + * + * Usage Example: + * \code + * #include + * + * void main(void) + * { + * // Start the master external oscillator which will be used as the main clock reference + * AVR32CLK_StartExternalOscillator(0, EXOSC_MODE_8MHZ_OR_MORE, EXOSC_START_0CLK); + * + * // Start the PLL for the CPU clock, switch CPU to it + * AVR32CLK_StartPLL(0, CLOCK_SRC_OSC0, 12000000, F_CPU); + * AVR32CLK_SetCPUClockSource(CLOCK_SRC_PLL0, F_CPU); + * + * // Start the PLL for the USB Generic Clock module + * AVR32CLK_StartPLL(1, CLOCK_SRC_OSC0, 12000000, 48000000); + * } + * \endcode + * + * @{ + */ + +#ifndef _UC3_CLOCK_MANAGEMENT_H_ +#define _UC3_CLOCK_MANAGEMENT_H_ + + /* Includes: */ + #include + + /* Enable C linkage for C++ Compilers: */ + #if defined(__cplusplus) + extern "C" { + #endif + + /* Public Interface - May be used in end-application: */ + /* Macros: */ + /** Enum for the possible external oscillator types. */ + enum UC3_Extern_OSC_ClockTypes_t + { + EXOSC_MODE_CLOCK = AVR32_PM_OSCCTRL0_MODE_EXT_CLOCK, /**< External clock (non-crystal) mode. */ + EXOSC_MODE_900KHZ_MAX = AVR32_PM_OSCCTRL0_MODE_CRYSTAL_G0, /**< External crystal oscillator equal to or slower than 900KHz. */ + EXOSC_MODE_3MHZ_MAX = AVR32_PM_OSCCTRL0_MODE_CRYSTAL_G1, /**< External crystal oscillator equal to or slower than 3MHz. */ + EXOSC_MODE_8MHZ_MAX = AVR32_PM_OSCCTRL0_MODE_CRYSTAL_G2, /**< External crystal oscillator equal to or slower than 8MHz. */ + EXOSC_MODE_8MHZ_OR_MORE = AVR32_PM_OSCCTRL0_MODE_CRYSTAL_G3, /**< External crystal oscillator equal to or faster than 8MHz. */ + }; + + /** Enum for the possible external oscillator statup times. */ + enum UC3_Extern_OSC_ClockStartup_t + { + EXOSC_START_0CLK = AVR32_PM_OSCCTRL0_STARTUP_0_RCOSC, /**< Immediate startup, no delay. */ + EXOSC_START_64CLK = AVR32_PM_OSCCTRL0_STARTUP_64_RCOSC, /**< Wait 64 clock cyles before startup for stability. */ + EXOSC_START_128CLK = AVR32_PM_OSCCTRL0_STARTUP_128_RCOSC, /**< Wait 128 clock cyles before startup for stability. */ + EXOSC_START_2048CLK = AVR32_PM_OSCCTRL0_STARTUP_2048_RCOSC, /**< Wait 2048 clock cyles before startup for stability. */ + EXOSC_START_4096CLK = AVR32_PM_OSCCTRL0_STARTUP_4096_RCOSC, /**< Wait 4096 clock cyles before startup for stability. */ + EXOSC_START_8192CLK = AVR32_PM_OSCCTRL0_STARTUP_8192_RCOSC, /**< Wait 8192 clock cyles before startup for stability. */ + EXOSC_START_16384CLK = AVR32_PM_OSCCTRL0_STARTUP_16384_RCOSC, /**< Wait 16384 clock cyles before startup for stability. */ + }; + + /** Enum for the possible module clock sources. */ + enum UC3_System_ClockSource_t + { + CLOCK_SRC_SLOW_CLK = 0, /**< Clock sourced from the internal slow clock. */ + CLOCK_SRC_OSC0 = 1, /**< Clock sourced from the Oscillator 0 clock. */ + CLOCK_SRC_OSC1 = 2, /**< Clock sourced from the Oscillator 1 clock. */ + CLOCK_SRC_PLL0 = 3, /**< Clock sourced from the PLL 0 clock. */ + CLOCK_SRC_PLL1 = 4, /**< Clock sourced from the PLL 1 clock. */ + }; + + /* Inline Functions: */ + /** Starts the given external oscillator of the UC3 microcontroller, with the given options. This routine blocks until + * the oscillator is ready for use. + * + * \param[in] Channel Index of the external oscillator to start. + * \param[in] Type Type of clock attached to the given oscillator channel, a value from \ref UC3_Extern_OSC_ClockTypes_t. + * \param[in] Startup Statup time of the external oscillator, a value from \ref UC3_Extern_OSC_ClockStartup_t. + * + * \return Boolean \c true if the external oscillator was successfully started, \c false if invalid parameters specified. + */ + static inline bool AVR32CLK_StartExternalOscillator(const uint8_t Channel, + const uint8_t Type, + const uint8_t Startup) ATTR_ALWAYS_INLINE; + static inline bool AVR32CLK_StartExternalOscillator(const uint8_t Channel, + const uint8_t Type, + const uint8_t Startup) + { + switch (Channel) + { + case 0: + AVR32_PM.OSCCTRL0.startup = Startup; + AVR32_PM.OSCCTRL0.mode = Type; + break; + case 1: + AVR32_PM.OSCCTRL1.startup = Startup; + AVR32_PM.OSCCTRL1.mode = Type; + break; + default: + return false; + } + + AVR32_PM.mcctrl |= (1 << (AVR32_PM_MCCTRL_OSC0EN_OFFSET + Channel)); + + while (!(AVR32_PM.poscsr & (1 << (AVR32_PM_POSCSR_OSC0RDY_OFFSET + Channel)))); + return true; + } + + /** Stops the given external oscillator of the UC3 microcontroller. + * + * \param[in] Channel Index of the external oscillator to stop. + */ + static inline void AVR32CLK_StopExternalOscillator(const uint8_t Channel) ATTR_ALWAYS_INLINE; + static inline void AVR32CLK_StopExternalOscillator(const uint8_t Channel) + { + AVR32_PM.mcctrl &= ~(1 << (AVR32_PM_MCCTRL_OSC0EN_OFFSET + Channel)); + } + + /** Starts the given PLL of the UC3 microcontroller, with the given options. This routine blocks until the PLL is ready for use. + * + * \note The output frequency must be equal to or greater than the source frequency. + * + * \param[in] Channel Index of the PLL to start. + * \param[in] Source Clock source for the PLL, a value from \ref UC3_System_ClockSource_t. + * \param[in] SourceFreq Frequency of the PLL's clock source, in Hz. + * \param[in] Frequency Target frequency of the PLL's output. + * + * \return Boolean \c true if the PLL was successfully started, \c false if invalid parameters specified. + */ + static inline bool AVR32CLK_StartPLL(const uint8_t Channel, + const uint8_t Source, + const uint32_t SourceFreq, + const uint32_t Frequency) ATTR_ALWAYS_INLINE; + static inline bool AVR32CLK_StartPLL(const uint8_t Channel, + const uint8_t Source, + const uint32_t SourceFreq, + const uint32_t Frequency) + { + if (SourceFreq > Frequency) + return false; + + switch (Source) + { + case CLOCK_SRC_OSC0: + AVR32_PM.PLL[Channel].pllosc = 0; + break; + case CLOCK_SRC_OSC1: + AVR32_PM.PLL[Channel].pllosc = 1; + break; + default: + return false; + } + + AVR32_PM.PLL[Channel].pllmul = (Frequency / SourceFreq) ? (((Frequency / SourceFreq) - 1) / 2) : 0; + AVR32_PM.PLL[Channel].plldiv = 0; + AVR32_PM.PLL[Channel].pllen = true; + + while (!(AVR32_PM.poscsr & (1 << (AVR32_PM_POSCSR_LOCK0_OFFSET + Channel)))); + return true; + } + + /** Stops the given PLL of the UC3 microcontroller. + * + * \param[in] Channel Index of the PLL to stop. + */ + static inline void AVR32CLK_StopPLL(const uint8_t Channel) ATTR_ALWAYS_INLINE; + static inline void AVR32CLK_StopPLL(const uint8_t Channel) + { + AVR32_PM.PLL[Channel].pllen = false; + } + + /** Starts the given Generic Clock of the UC3 microcontroller, with the given options. + * + * \param[in] Channel Index of the Generic Clock to start. + * \param[in] Source Clock source for the Generic Clock, a value from \ref UC3_System_ClockSource_t. + * \param[in] SourceFreq Frequency of the Generic Clock's clock source, in Hz. + * \param[in] Frequency Target frequency of the Generic Clock's output. + * + * \return Boolean \c true if the Generic Clock was successfully started, \c false if invalid parameters specified. + */ + static inline bool AVR32CLK_StartGenericClock(const uint8_t Channel, + const uint8_t Source, + const uint32_t SourceFreq, + const uint32_t Frequency) ATTR_ALWAYS_INLINE; + static inline bool AVR32CLK_StartGenericClock(const uint8_t Channel, + const uint8_t Source, + const uint32_t SourceFreq, + const uint32_t Frequency) + { + switch (Source) + { + case CLOCK_SRC_OSC0: + AVR32_PM.GCCTRL[Channel].pllsel = false; + AVR32_PM.GCCTRL[Channel].oscsel = 0; + break; + case CLOCK_SRC_OSC1: + AVR32_PM.GCCTRL[Channel].pllsel = false; + AVR32_PM.GCCTRL[Channel].oscsel = 1; + break; + case CLOCK_SRC_PLL0: + AVR32_PM.GCCTRL[Channel].pllsel = true; + AVR32_PM.GCCTRL[Channel].oscsel = 0; + break; + case CLOCK_SRC_PLL1: + AVR32_PM.GCCTRL[Channel].pllsel = true; + AVR32_PM.GCCTRL[Channel].oscsel = 1; + break; + default: + return false; + } + + if (SourceFreq < Frequency) + return false; + + AVR32_PM.GCCTRL[Channel].diven = (SourceFreq > Frequency) ? true : false; + AVR32_PM.GCCTRL[Channel].div = (((SourceFreq / Frequency) - 1) / 2); + AVR32_PM.GCCTRL[Channel].cen = true; + + return true; + } + + /** Stops the given generic clock of the UC3 microcontroller. + * + * \param[in] Channel Index of the generic clock to stop. + */ + static inline void AVR32CLK_StopGenericClock(const uint8_t Channel) ATTR_ALWAYS_INLINE; + static inline void AVR32CLK_StopGenericClock(const uint8_t Channel) + { + AVR32_PM.GCCTRL[Channel].cen = false; + } + + /** Sets the clock source for the main microcontroller core. The given clock source should be configured + * and ready for use before this function is called. + * + * This function will configure the FLASH controller's wait states automatically to suit the given clock source. + * + * \param[in] Source Clock source for the CPU core, a value from \ref UC3_System_ClockSource_t. + * \param[in] SourceFreq Frequency of the CPU core's clock source, in Hz. + * + * \return Boolean \c true if the CPU core clock was sucessfully altered, \c false if invalid parameters specified. + */ + static inline bool AVR32CLK_SetCPUClockSource(const uint8_t Source, + const uint32_t SourceFreq) ATTR_ALWAYS_INLINE; + static inline bool AVR32CLK_SetCPUClockSource(const uint8_t Source, + const uint32_t SourceFreq) + { + AVR32_FLASHC.FCR.fws = (SourceFreq > 30000000) ? true : false; + + switch (Source) + { + #if defined(AVR32_PM_MCCTRL_MCSEL_SLOW) + case CLOCK_SRC_SLOW_CLK: + AVR32_PM.MCCTRL.mcsel = AVR32_PM_MCCTRL_MCSEL_SLOW; + break; + #endif + #if defined(AVR32_PM_MCCTRL_MCSEL_OSC0) + case CLOCK_SRC_OSC0: + AVR32_PM.MCCTRL.mcsel = AVR32_PM_MCCTRL_MCSEL_OSC0; + break; + #endif + #if defined(AVR32_PM_MCCTRL_MCSEL_PLL0) + case CLOCK_SRC_PLL0: + AVR32_PM.MCCTRL.mcsel = AVR32_PM_MCCTRL_MCSEL_PLL0; + break; + #endif + default: + return false; + } + + return true; + } + + /* Disable C linkage for C++ Compilers: */ + #if defined(__cplusplus) + } + #endif + +#endif + +/** @} */ diff --git a/LUFA/Platform/UC3/Exception.S b/LUFA/Platform/UC3/Exception.S index 9509fd7f..ddfe0098 100644 --- a/LUFA/Platform/UC3/Exception.S +++ b/LUFA/Platform/UC3/Exception.S @@ -1,125 +1,125 @@ -/* - LUFA Library - Copyright (C) Dean Camera, 2011. - - dean [at] fourwalledcubicle [dot] com - www.lufa-lib.org -*/ - -/* - Copyright 2011 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 - software, including all implied warranties of merchantability - and fitness. In no event shall the author be liable for any - special, indirect or consequential damages or any damages - whatsoever resulting from loss of use, data or profits, whether - in an action of contract, negligence or other tortious action, - arising out of or in connection with the use or performance of - this software. -*/ - -#include - -.section .exception_handlers, "ax", @progbits - -// ================= EXCEPTION TABLE ================ -.balign 0x200 -.global EVBA_Table -EVBA_Table: - -.org 0x000 -Exception_Unrecoverable_Exception: - rjmp $ -.org 0x004 -Exception_TLB_Multiple_Hit: - rjmp $ -.org 0x008 -Exception_Bus_Error_Data_Fetch: - rjmp $ -.org 0x00C -Exception_Bus_Error_Instruction_Fetch: - rjmp $ -.org 0x010 -Exception_NMI: - rjmp $ -.org 0x014 -Exception_Instruction_Address: - rjmp $ -.org 0x018 -Exception_ITLB_Protection: - rjmp $ -.org 0x01C -Exception_OCD_Breakpoint: - rjmp $ -.org 0x020 -Exception_Illegal_Opcode: - rjmp $ -.org 0x024 -Exception_Unimplemented_Instruction: - rjmp $ -.org 0x028 -Exception_Privilege_Violation: - rjmp $ -.org 0x02C -Exception_Floating_Point: - rjmp $ -.org 0x030 -Exception_Coprocessor_Absent: - rjmp $ -.org 0x034 -Exception_Data_Address_Read: - rjmp $ -.org 0x038 -Exception_Data_Address_Write: - rjmp $ -.org 0x03C -Exception_DTLB_Protection_Read: - rjmp $ -.org 0x040 -Exception_DTLB_Protection_Write: - rjmp $ -.org 0x044 -Exception_DTLB_Modified: - rjmp $ -.org 0x050 -Exception_ITLB_Miss: - rjmp $ -.org 0x060 -Exception_DTLB_Miss_Read: - rjmp $ -.org 0x070 -Exception_DTLB_Miss_Write: - rjmp $ -.org 0x100 -Exception_Supervisor_Call: - rjmp $ -// ============== END OF EXCEPTION TABLE ============= - -// ============= GENERAL INTERRUPT HANDLER =========== -.balign 4 -.irp Level, 0, 1, 2, 3 -Exception_INT\Level: - mov r12, \Level - call INTC_GetInterruptHandler - mov pc, r12 -.endr -// ========= END OF GENERAL INTERRUPT HANDLER ======== - -// ====== GENERAL INTERRUPT HANDLER OFFSET TABLE ====== -.balign 4 -.global Autovector_Table -Autovector_Table: -.irp Level, 0, 1, 2, 3 - .word ((AVR32_INTC_INT0 + \Level) << AVR32_INTC_IPR_INTLEVEL_OFFSET) | (Exception_INT\Level - EVBA_Table) -.endr -// === END OF GENERAL INTERRUPT HANDLER OFFSET TABLE === +/* + LUFA Library + Copyright (C) Dean Camera, 2011. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2011 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 + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +#include + +.section .exception_handlers, "ax", @progbits + +// ================= EXCEPTION TABLE ================ +.balign 0x200 +.global EVBA_Table +EVBA_Table: + +.org 0x000 +Exception_Unrecoverable_Exception: + rjmp $ +.org 0x004 +Exception_TLB_Multiple_Hit: + rjmp $ +.org 0x008 +Exception_Bus_Error_Data_Fetch: + rjmp $ +.org 0x00C +Exception_Bus_Error_Instruction_Fetch: + rjmp $ +.org 0x010 +Exception_NMI: + rjmp $ +.org 0x014 +Exception_Instruction_Address: + rjmp $ +.org 0x018 +Exception_ITLB_Protection: + rjmp $ +.org 0x01C +Exception_OCD_Breakpoint: + rjmp $ +.org 0x020 +Exception_Illegal_Opcode: + rjmp $ +.org 0x024 +Exception_Unimplemented_Instruction: + rjmp $ +.org 0x028 +Exception_Privilege_Violation: + rjmp $ +.org 0x02C +Exception_Floating_Point: + rjmp $ +.org 0x030 +Exception_Coprocessor_Absent: + rjmp $ +.org 0x034 +Exception_Data_Address_Read: + rjmp $ +.org 0x038 +Exception_Data_Address_Write: + rjmp $ +.org 0x03C +Exception_DTLB_Protection_Read: + rjmp $ +.org 0x040 +Exception_DTLB_Protection_Write: + rjmp $ +.org 0x044 +Exception_DTLB_Modified: + rjmp $ +.org 0x050 +Exception_ITLB_Miss: + rjmp $ +.org 0x060 +Exception_DTLB_Miss_Read: + rjmp $ +.org 0x070 +Exception_DTLB_Miss_Write: + rjmp $ +.org 0x100 +Exception_Supervisor_Call: + rjmp $ +// ============== END OF EXCEPTION TABLE ============= + +// ============= GENERAL INTERRUPT HANDLER =========== +.balign 4 +.irp Level, 0, 1, 2, 3 +Exception_INT\Level: + mov r12, \Level + call INTC_GetInterruptHandler + mov pc, r12 +.endr +// ========= END OF GENERAL INTERRUPT HANDLER ======== + +// ====== GENERAL INTERRUPT HANDLER OFFSET TABLE ====== +.balign 4 +.global Autovector_Table +Autovector_Table: +.irp Level, 0, 1, 2, 3 + .word ((AVR32_INTC_INT0 + \Level) << AVR32_INTC_IPR_INTLEVEL_OFFSET) | (Exception_INT\Level - EVBA_Table) +.endr +// === END OF GENERAL INTERRUPT HANDLER OFFSET TABLE === diff --git a/LUFA/Platform/UC3/InterruptManagement.c b/LUFA/Platform/UC3/InterruptManagement.c index 2e260b38..98d1e45f 100644 --- a/LUFA/Platform/UC3/InterruptManagement.c +++ b/LUFA/Platform/UC3/InterruptManagement.c @@ -1,62 +1,62 @@ -/* - LUFA Library - Copyright (C) Dean Camera, 2011. - - dean [at] fourwalledcubicle [dot] com - www.lufa-lib.org -*/ - -/* - Copyright 2011 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 - software, including all implied warranties of merchantability - and fitness. In no event shall the author be liable for any - special, indirect or consequential damages or any damages - whatsoever resulting from loss of use, data or profits, whether - in an action of contract, negligence or other tortious action, - arising out of or in connection with the use or performance of - this software. -*/ - -#include "InterruptManagement.h" - -/** Interrupt vector table, containing the ISR to call for each interrupt group */ -InterruptHandlerPtr_t InterruptHandlers[AVR32_INTC_NUM_INT_GRPS]; - -/** ISR for unhandled interrupt groups */ -ISR(Unhandled_Interrupt) -{ - for (;;); -} - -/** Retrieves the associated interrupt handler for the interrupt group currently being fired. This - * is called directly from the exception handler routine before dispatching to the ISR. - */ -InterruptHandlerPtr_t INTC_GetInterruptHandler(const uint_reg_t InterruptLevel) -{ - return InterruptHandlers[AVR32_INTC.icr[AVR32_INTC_INT3 - InterruptLevel]]; -} - -/** Initializes the interrupt controller ready to handle interrupts. This must be called at the - * start of the user program before any interrupts are registered or enabled. - */ -void INTC_Init(void) -{ - for (uint8_t InterruptGroup = 0; InterruptGroup < AVR32_INTC_NUM_INT_GRPS; InterruptGroup++) - { - InterruptHandlers[InterruptGroup] = Unhandled_Interrupt; - AVR32_INTC.ipr[InterruptGroup] = Autovector_Table[AVR32_INTC_INT0]; - } - - __builtin_mtsr(AVR32_EVBA, (uintptr_t)&EVBA_Table); -} +/* + LUFA Library + Copyright (C) Dean Camera, 2011. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2011 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 + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +#include "InterruptManagement.h" + +/** Interrupt vector table, containing the ISR to call for each interrupt group */ +InterruptHandlerPtr_t InterruptHandlers[AVR32_INTC_NUM_INT_GRPS]; + +/** ISR for unhandled interrupt groups */ +ISR(Unhandled_Interrupt) +{ + for (;;); +} + +/** Retrieves the associated interrupt handler for the interrupt group currently being fired. This + * is called directly from the exception handler routine before dispatching to the ISR. + */ +InterruptHandlerPtr_t INTC_GetInterruptHandler(const uint_reg_t InterruptLevel) +{ + return InterruptHandlers[AVR32_INTC.icr[AVR32_INTC_INT3 - InterruptLevel]]; +} + +/** Initializes the interrupt controller ready to handle interrupts. This must be called at the + * start of the user program before any interrupts are registered or enabled. + */ +void INTC_Init(void) +{ + for (uint8_t InterruptGroup = 0; InterruptGroup < AVR32_INTC_NUM_INT_GRPS; InterruptGroup++) + { + InterruptHandlers[InterruptGroup] = Unhandled_Interrupt; + AVR32_INTC.ipr[InterruptGroup] = Autovector_Table[AVR32_INTC_INT0]; + } + + __builtin_mtsr(AVR32_EVBA, (uintptr_t)&EVBA_Table); +} diff --git a/LUFA/Platform/UC3/InterruptManagement.h b/LUFA/Platform/UC3/InterruptManagement.h index 67dc3ea1..dd70675c 100644 --- a/LUFA/Platform/UC3/InterruptManagement.h +++ b/LUFA/Platform/UC3/InterruptManagement.h @@ -1,162 +1,162 @@ -/* - LUFA Library - Copyright (C) Dean Camera, 2011. - - dean [at] fourwalledcubicle [dot] com - www.lufa-lib.org -*/ - -/* - Copyright 2011 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 - software, including all implied warranties of merchantability - and fitness. In no event shall the author be liable for any - special, indirect or consequential damages or any damages - whatsoever resulting from loss of use, data or profits, whether - in an action of contract, negligence or other tortious action, - arising out of or in connection with the use or performance of - this software. -*/ - -/** \file - * \brief Interrupt Controller Driver for the AVR32 UC3 microcontrollers. - * - * Interrupt controller driver for the AVR32 UC3 microcontrollers, for the configuration of interrupt - * handlers within the device. - */ - -/** \ingroup Group_PlatformDrivers - * \defgroup Group_PlatformDrivers_UC3Interrupts UC3 Interrupt Controller Driver - LUFA/Platform/UC3/InterruptManagement.h - * \brief Interrupt Controller Driver for the AVR32 UC3 microcontrollers. - * - * \section Sec_Dependencies Module Source Dependencies - * The following files must be built with any user project that uses this module: - * - LUFA/Platform/UC3/InterruptManagement.c - * - LUFA/Platform/UC3/Exception.S - * - * \section Sec_ModDescription Module Description - * Interrupt controller driver for the AVR32 UC3 microcontrollers, for the configuration of interrupt - * handlers within the device. - * - * Usage Example: - * \code - * #include - * - * ISR(USB_Group_IRQ_Handler) - * { - * // USB group handler code here - * } - * - * void main(void) - * { - * INTC_Init(); - * INTC_RegisterGroupHandler(INTC_IRQ_GROUP(AVR32_USBB_IRQ), AVR32_INTC_INT0, USB_Group_IRQ_Handler); - * } - * \endcode - * - * @{ - */ - -#ifndef _UC3_INTERRUPT_MANAGEMENT_H_ -#define _UC3_INTERRUPT_MANAGEMENT_H_ - - /* Includes: */ - #include - - /* Enable C linkage for C++ Compilers: */ - #if defined(__cplusplus) - extern "C" { - #endif - - /* Private Interface - For use in library only: */ - #if !defined(__DOXYGEN__) - /* Type Defines: */ - typedef void (*InterruptHandlerPtr_t)(void); - - /* External Variables: */ - extern const void EVBA_Table; - extern const uint32_t Autovector_Table[]; - extern InterruptHandlerPtr_t InterruptHandlers[AVR32_INTC_NUM_INT_GRPS]; - #endif - - /* Public Interface - May be used in end-application: */ - /* Macros: */ - /** Converts a given interrupt index into its assocated interrupt group. - * - * \param[in] IRQIndex Index of the interrupt request to convert. - * - * \return Interrupt group number associated with the interrupt index. - */ - #define INTC_IRQ_GROUP(IRQIndex) (IRQIndex / 32) - - /** Converts a given interrupt index into its assocated interrupt line. - * - * \param[in] IRQIndex Index of the interrupt request to convert. - * - * \return Interrupt line number associated with the interrupt index. - */ - #define INTC_IRQ_LINE(IRQIndex) (IRQIndex % 32) - - /* Function Prototypes: */ - /** Initializes the interrupt controller, nulling out all interrupt handlers ready for new registration. This - * function should be called once on startup to ensure the interrupt controller is ready for use. - */ - void INTC_Init(void); - - /* Inline Functions: */ - /** Registers a handler for a given interrupt group. On the AVR32 UC3 devices, interrupts are grouped by - * peripheral. To save on SRAM used, a single ISR handles all interrupt lines within a single group - to - * determine the exact line that has interrupted within the group ISR handler, use \ref INTC_GetGroupInterrupts(). - * - * If multiple interrupts with the same group are registered, the last registered handler will become the - * handler called for interrupts raised within that group. - * - * To obtain the group number of a specific interrupt index, use the \ref INTC_IRQ_GROUP() macro. - * - * \param[in] GroupNumber Group number of the interrupt group to register a handler for. - * \param[in] InterruptLevel Priority level for the specified interrupt, a \c AVR32_INTC_INT* mask. - * \param[in] Handler Address of the ISR handler for the interrupt group. - */ - static inline void INTC_RegisterGroupHandler(const uint16_t GroupNumber, - const uint8_t InterruptLevel, - const InterruptHandlerPtr_t Handler) ATTR_ALWAYS_INLINE; - static inline void INTC_RegisterGroupHandler(const uint16_t GroupNumber, - const uint8_t InterruptLevel, - const InterruptHandlerPtr_t Handler) - { - InterruptHandlers[GroupNumber] = Handler; - AVR32_INTC.ipr[GroupNumber] = Autovector_Table[InterruptLevel]; - } - - /** Retrieves the pending interrupts for a given interrupt group. The result of this function should be masked - * against interrupt request indexes converted to a request line number via the \ref INTC_IRQ_LINE() macro. To - * obtain the group number of a given interrupt request, use the \ref INTC_IRQ_GROUP() macro. - * - * \param[in] GroupNumber Group number of the interrupt group to check. - * - * \return Mask of pending interrupt lines for the given interrupt group. - */ - static inline uint_reg_t INTC_GetGroupInterrupts(const uint16_t GroupNumber) ATTR_ALWAYS_INLINE; - static inline uint_reg_t INTC_GetGroupInterrupts(const uint16_t GroupNumber) - { - return AVR32_INTC.irr[GroupNumber]; - } - - /* Disable C linkage for C++ Compilers: */ - #if defined(__cplusplus) - } - #endif - -#endif - -/** @} */ +/* + LUFA Library + Copyright (C) Dean Camera, 2011. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2011 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 + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +/** \file + * \brief Interrupt Controller Driver for the AVR32 UC3 microcontrollers. + * + * Interrupt controller driver for the AVR32 UC3 microcontrollers, for the configuration of interrupt + * handlers within the device. + */ + +/** \ingroup Group_PlatformDrivers + * \defgroup Group_PlatformDrivers_UC3Interrupts UC3 Interrupt Controller Driver - LUFA/Platform/UC3/InterruptManagement.h + * \brief Interrupt Controller Driver for the AVR32 UC3 microcontrollers. + * + * \section Sec_Dependencies Module Source Dependencies + * The following files must be built with any user project that uses this module: + * - LUFA/Platform/UC3/InterruptManagement.c + * - LUFA/Platform/UC3/Exception.S + * + * \section Sec_ModDescription Module Description + * Interrupt controller driver for the AVR32 UC3 microcontrollers, for the configuration of interrupt + * handlers within the device. + * + * Usage Example: + * \code + * #include + * + * ISR(USB_Group_IRQ_Handler) + * { + * // USB group handler code here + * } + * + * void main(void) + * { + * INTC_Init(); + * INTC_RegisterGroupHandler(INTC_IRQ_GROUP(AVR32_USBB_IRQ), AVR32_INTC_INT0, USB_Group_IRQ_Handler); + * } + * \endcode + * + * @{ + */ + +#ifndef _UC3_INTERRUPT_MANAGEMENT_H_ +#define _UC3_INTERRUPT_MANAGEMENT_H_ + + /* Includes: */ + #include + + /* Enable C linkage for C++ Compilers: */ + #if defined(__cplusplus) + extern "C" { + #endif + + /* Private Interface - For use in library only: */ + #if !defined(__DOXYGEN__) + /* Type Defines: */ + typedef void (*InterruptHandlerPtr_t)(void); + + /* External Variables: */ + extern const void EVBA_Table; + extern const uint32_t Autovector_Table[]; + extern InterruptHandlerPtr_t InterruptHandlers[AVR32_INTC_NUM_INT_GRPS]; + #endif + + /* Public Interface - May be used in end-application: */ + /* Macros: */ + /** Converts a given interrupt index into its assocated interrupt group. + * + * \param[in] IRQIndex Index of the interrupt request to convert. + * + * \return Interrupt group number associated with the interrupt index. + */ + #define INTC_IRQ_GROUP(IRQIndex) (IRQIndex / 32) + + /** Converts a given interrupt index into its assocated interrupt line. + * + * \param[in] IRQIndex Index of the interrupt request to convert. + * + * \return Interrupt line number associated with the interrupt index. + */ + #define INTC_IRQ_LINE(IRQIndex) (IRQIndex % 32) + + /* Function Prototypes: */ + /** Initializes the interrupt controller, nulling out all interrupt handlers ready for new registration. This + * function should be called once on startup to ensure the interrupt controller is ready for use. + */ + void INTC_Init(void); + + /* Inline Functions: */ + /** Registers a handler for a given interrupt group. On the AVR32 UC3 devices, interrupts are grouped by + * peripheral. To save on SRAM used, a single ISR handles all interrupt lines within a single group - to + * determine the exact line that has interrupted within the group ISR handler, use \ref INTC_GetGroupInterrupts(). + * + * If multiple interrupts with the same group are registered, the last registered handler will become the + * handler called for interrupts raised within that group. + * + * To obtain the group number of a specific interrupt index, use the \ref INTC_IRQ_GROUP() macro. + * + * \param[in] GroupNumber Group number of the interrupt group to register a handler for. + * \param[in] InterruptLevel Priority level for the specified interrupt, a \c AVR32_INTC_INT* mask. + * \param[in] Handler Address of the ISR handler for the interrupt group. + */ + static inline void INTC_RegisterGroupHandler(const uint16_t GroupNumber, + const uint8_t InterruptLevel, + const InterruptHandlerPtr_t Handler) ATTR_ALWAYS_INLINE; + static inline void INTC_RegisterGroupHandler(const uint16_t GroupNumber, + const uint8_t InterruptLevel, + const InterruptHandlerPtr_t Handler) + { + InterruptHandlers[GroupNumber] = Handler; + AVR32_INTC.ipr[GroupNumber] = Autovector_Table[InterruptLevel]; + } + + /** Retrieves the pending interrupts for a given interrupt group. The result of this function should be masked + * against interrupt request indexes converted to a request line number via the \ref INTC_IRQ_LINE() macro. To + * obtain the group number of a given interrupt request, use the \ref INTC_IRQ_GROUP() macro. + * + * \param[in] GroupNumber Group number of the interrupt group to check. + * + * \return Mask of pending interrupt lines for the given interrupt group. + */ + static inline uint_reg_t INTC_GetGroupInterrupts(const uint16_t GroupNumber) ATTR_ALWAYS_INLINE; + static inline uint_reg_t INTC_GetGroupInterrupts(const uint16_t GroupNumber) + { + return AVR32_INTC.irr[GroupNumber]; + } + + /* Disable C linkage for C++ Compilers: */ + #if defined(__cplusplus) + } + #endif + +#endif + +/** @} */ diff --git a/LUFA/Platform/XMEGA/ClockManagement.h b/LUFA/Platform/XMEGA/ClockManagement.h index de531c29..dd8d60cc 100644 --- a/LUFA/Platform/XMEGA/ClockManagement.h +++ b/LUFA/Platform/XMEGA/ClockManagement.h @@ -1,222 +1,298 @@ -/* - LUFA Library - Copyright (C) Dean Camera, 2011. - - dean [at] fourwalledcubicle [dot] com - www.lufa-lib.org -*/ - -/* - Copyright 2011 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 - software, including all implied warranties of merchantability - and fitness. In no event shall the author be liable for any - special, indirect or consequential damages or any damages - whatsoever resulting from loss of use, data or profits, whether - in an action of contract, negligence or other tortious action, - arising out of or in connection with the use or performance of - this software. -*/ - -/** \file - * \brief Module Clock Driver for the AVR USB XMEGA microcontrollers. - * - * Clock management driver for the AVR USB XMEGA microcontrollers. This driver allows for the configuration - * of the various clocks within the device to clock the various peripherals. - */ - -/** \ingroup Group_PlatformDrivers - * \defgroup Group_PlatformDrivers_XMEGAClocks AVR USB XMEGA Clock Management Driver - LUFA/Platform/XMEGA/ClockManagement.h - * \brief Module Clock Driver for the AVR USB XMEGA microcontrollers. - * - * \section Sec_Dependencies Module Source Dependencies - * The following files must be built with any user project that uses this module: - * - None - * - * \section Sec_ModDescription Module Description - * Clock management driver for the AVR USB XMEGA microcontrollers. This driver allows for the configuration - * of the various clocks within the device to clock the various peripherals. - * - * Usage Example: - * \code - * #include - * - * void main(void) - * { - * [TODO] - * } - * \endcode - * - * @{ - */ - -#ifndef _XMEGA_CLOCK_MANAGEMENT_H_ -#define _XMEGA_CLOCK_MANAGEMENT_H_ - - /* Includes: */ - #include - - /* Enable C linkage for C++ Compilers: */ - #if defined(__cplusplus) - extern "C" { - #endif - - /* Public Interface - May be used in end-application: */ - /* Macros: */ - /** Enum for the possible external oscillator types. */ - enum XMEGA_Extern_OSC_ClockTypes_t - { - EXOSC_MODE_2MHZ_MAX = OSC_FRQRANGE_04TO2_gc, /**< External crystal oscillator equal to or slower than 2MHz. */ - EXOSC_MODE_9MHZ_MAX = OSC_FRQRANGE_2TO9_gc, /**< External crystal oscillator equal to or slower than 9MHz. */ - EXOSC_MODE_12MHZ_MAX = OSC_FRQRANGE_9TO12_gc, /**< External crystal oscillator equal to or slower than 12MHz. */ - EXOSC_MODE_16MHZ_MAX = OSC_FRQRANGE_12TO16_gc, /**< External crystal oscillator equal to or slower than 16MHz. */ - }; - - /** Enum for the possible external oscillator statup times. */ - enum XMEGA_Extern_OSC_ClockStartup_t - { - EXOSC_START_6CLK = OSC_XOSCSEL_EXTCLK_gc, /**< Wait 6 clock cycles before startup (external clock). */ - EXOSC_START_32KCLK = OSC_XOSCSEL_32KHz_gc, /**< Wait 32K clock cycles before startup (32.768KHz crystal). */ - EXOSC_START_256CLK = OSC_XOSCSEL_XTAL_256CLK_gc, /**< Wait 256 clock cycles before startup. */ - EXOSC_START_1KCLK = OSC_XOSCSEL_XTAL_1KCLK_gc, /**< Wait 1K clock cycles before startup. */ - EXOSC_START_16KCLK = OSC_XOSCSEL_XTAL_16KCLK_gc, /**< Wait 16K clock cycles before startup. */ - }; - - /** Enum for the possible module clock sources. */ - enum XMEGA_System_ClockSource_t - { - CLOCK_SRC_INT_RC2MHZ = 0, /**< Clock sourced from the Internal 2MHz RC Oscillator clock. */ - CLOCK_SRC_INT_RC32MHZ = 1, /**< Clock sourced from the Internal 32MHz RC Oscillator clock. */ - CLOCK_SRC_INT_RC32KHZ = 2, /**< Clock sourced from the Internal 32KHz RC Oscillator clock. */ - CLOCK_SRC_XOSC = 3, /**< Clock sourced from the External Oscillator clock. */ - CLOCK_SRC_PLL = 4, /**< Clock sourced from the Internal PLL clock. */ - }; - - /* Inline Functions: */ - /** Starts the given external oscillator of the UC3 microcontroller, with the given options. This routine blocks until - * the oscillator is ready for use. - * - * \param[in] Channel Index of the external oscillator to start. - * \param[in] Type Type of clock attached to the given oscillator channel, a value from \ref XMEGA_Extern_OSC_ClockTypes_t. - * \param[in] Startup Statup time of the external oscillator, a value from \ref XMEGA_Extern_OSC_ClockStartup_t. - * - * \return Boolean \c true if the external oscillator was successfully started, \c false if invalid parameters specified. - */ - static inline uint8_t AVR32CLK_StartExternalOscillator(const uint8_t Channel, - const uint8_t Type, - const uint8_t Startup) ATTR_ALWAYS_INLINE; - static inline uint8_t AVR32CLK_StartExternalOscillator(const uint8_t Channel, - const uint8_t Type, - const uint8_t Startup) - { - return false; // TODO - } - - /** Stops the given external oscillator of the UC3 microcontroller. - * - * \param[in] Channel Index of the external oscillator to stop. - */ - static inline void AVR32CLK_StopExternalOscillator(const uint8_t Channel) ATTR_ALWAYS_INLINE; - static inline void AVR32CLK_StopExternalOscillator(const uint8_t Channel) - { - return; // TODO - } - - /** Starts the given PLL of the UC3 microcontroller, with the given options. This routine blocks until the PLL is ready for use. - * - * \param[in] Channel Index of the PLL to start. - * \param[in] Source Clock source for the PLL, a value from \ref XMEGA_System_ClockSource_t. - * \param[in] SourceFreq Frequency of the PLL's clock source, in Hz. - * \param[in] Frequency Target frequency of the PLL's output. - * - * \return Boolean \c true if the PLL was successfully started, \c false if invalid parameters specified. - */ - static inline bool AVR32CLK_StartPLL(const uint8_t Channel, - const uint8_t Source, - const uint32_t SourceFreq, - const uint32_t Frequency) ATTR_ALWAYS_INLINE; - static inline bool AVR32CLK_StartPLL(const uint8_t Channel, - const uint8_t Source, - const uint32_t SourceFreq, - const uint32_t Frequency) - { - return false; // TODO - } - - /** Stops the given PLL of the UC3 microcontroller. - * - * \param[in] Channel Index of the PLL to stop. - */ - static inline void AVR32CLK_StopPLL(const uint8_t Channel) ATTR_ALWAYS_INLINE; - static inline void AVR32CLK_StopPLL(const uint8_t Channel) - { - // TODO - } - - /** Starts the given Generic Clock of the UC3 microcontroller, with the given options. - * - * \param[in] Channel Index of the Generic Clock to start. - * \param[in] Source Clock source for the Generic Clock, a value from \ref XMEGA_System_ClockSource_t. - * \param[in] SourceFreq Frequency of the Generic Clock's clock source, in Hz. - * \param[in] Frequency Target frequency of the Generic Clock's output. - * - * \return Boolean \c true if the Generic Clock was successfully started, \c false if invalid parameters specified. - */ - static inline bool AVR32CLK_StartGenericClock(const uint8_t Channel, - const uint8_t Source, - const uint32_t SourceFreq, - const uint32_t Frequency) ATTR_ALWAYS_INLINE; - static inline bool AVR32CLK_StartGenericClock(const uint8_t Channel, - const uint8_t Source, - const uint32_t SourceFreq, - const uint32_t Frequency) - { - return false; // TODO - } - - /** Stops the given generic clock of the UC3 microcontroller. - * - * \param[in] Channel Index of the generic clock to stop. - */ - static inline void AVR32CLK_StopGenericClock(const uint8_t Channel) ATTR_ALWAYS_INLINE; - static inline void AVR32CLK_StopGenericClock(const uint8_t Channel) - { - // TODO - } - - /** Sets the clock source for the main microcontroller core. The given clock source should be configured - * and ready for use before this function is called. - * - * This function will configure the FLASH controller's wait states automatically to suit the given clock source. - * - * \param[in] Source Clock source for the CPU core, a value from \ref XMEGA_System_ClockSource_t. - * \param[in] SourceFreq Frequency of the CPU core's clock source, in Hz. - * - * \return Boolean \c true if the CPU core clock was sucessfully altered, \c false if invalid parameters specified. - */ - static inline bool AVR32CLK_SetCPUClockSource(const uint8_t Source, - const uint32_t SourceFreq) ATTR_ALWAYS_INLINE; - static inline bool AVR32CLK_SetCPUClockSource(const uint8_t Source, - const uint32_t SourceFreq) - { - return false // TODO - } - - /* Disable C linkage for C++ Compilers: */ - #if defined(__cplusplus) - } - #endif - -#endif - -/** @} */ +/* + LUFA Library + Copyright (C) Dean Camera, 2011. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2011 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 + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +/** \file + * \brief Module Clock Driver for the AVR USB XMEGA microcontrollers. + * + * Clock management driver for the AVR USB XMEGA microcontrollers. This driver allows for the configuration + * of the various clocks within the device to clock the various peripherals. + */ + +/** \ingroup Group_PlatformDrivers + * \defgroup Group_PlatformDrivers_XMEGAClocks AVR USB XMEGA Clock Management Driver - LUFA/Platform/XMEGA/ClockManagement.h + * \brief Module Clock Driver for the AVR USB XMEGA microcontrollers. + * + * \section Sec_Dependencies Module Source Dependencies + * The following files must be built with any user project that uses this module: + * - None + * + * \section Sec_ModDescription Module Description + * Clock management driver for the AVR USB XMEGA microcontrollers. This driver allows for the configuration + * of the various clocks within the device to clock the various peripherals. + * + * Usage Example: + * \code + * #include + * + * void main(void) + * { + * // Start the internal 32MHz RC oscillator and switch the CPU core to run from it + * AVR32CLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ); + * XMEGACLK_SetCPUClockSource(CLOCK_SRC_INT_RC32MHZ, F_CPU); + * + * // Start the external oscillator and multiply up the frequency + * AVR32CLK_StartExternalOscillator(EXOSC_FREQ_9MHZ_MAX, EXOSC_START_1KCLK); + * AVR32CLK_StartPLL(CLOCK_SRC_XOSC, 8000000, F_USB); + * } + * \endcode + * + * @{ + */ + +#ifndef _XMEGA_CLOCK_MANAGEMENT_H_ +#define _XMEGA_CLOCK_MANAGEMENT_H_ + + /* Includes: */ + #include + + /* Enable C linkage for C++ Compilers: */ + #if defined(__cplusplus) + extern "C" { + #endif + + /* Public Interface - May be used in end-application: */ + /* Macros: */ + /** Enum for the possible external oscillator frequency ranges. */ + enum XMEGA_Extern_OSC_ClockFrequency_t + { + EXOSC_FREQ_2MHZ_MAX = OSC_FRQRANGE_04TO2_gc, /**< External crystal oscillator equal to or slower than 2MHz. */ + EXOSC_FREQ_9MHZ_MAX = OSC_FRQRANGE_2TO9_gc, /**< External crystal oscillator equal to or slower than 9MHz. */ + EXOSC_FREQ_12MHZ_MAX = OSC_FRQRANGE_9TO12_gc, /**< External crystal oscillator equal to or slower than 12MHz. */ + EXOSC_FREQ_16MHZ_MAX = OSC_FRQRANGE_12TO16_gc, /**< External crystal oscillator equal to or slower than 16MHz. */ + }; + + /** Enum for the possible external oscillator statup times. */ + enum XMEGA_Extern_OSC_ClockStartup_t + { + EXOSC_START_6CLK = OSC_XOSCSEL_EXTCLK_gc, /**< Wait 6 clock cycles before startup (external clock). */ + EXOSC_START_32KCLK = OSC_XOSCSEL_32KHz_gc, /**< Wait 32K clock cycles before startup (32.768KHz crystal). */ + EXOSC_START_256CLK = OSC_XOSCSEL_XTAL_256CLK_gc, /**< Wait 256 clock cycles before startup. */ + EXOSC_START_1KCLK = OSC_XOSCSEL_XTAL_1KCLK_gc, /**< Wait 1K clock cycles before startup. */ + EXOSC_START_16KCLK = OSC_XOSCSEL_XTAL_16KCLK_gc, /**< Wait 16K clock cycles before startup. */ + }; + + /** Enum for the possible module clock sources. */ + enum XMEGA_System_ClockSource_t + { + CLOCK_SRC_INT_RC2MHZ = 0, /**< Clock sourced from the Internal 2MHz RC Oscillator clock. */ + CLOCK_SRC_INT_RC32MHZ = 1, /**< Clock sourced from the Internal 32MHz RC Oscillator clock. */ + CLOCK_SRC_INT_RC32KHZ = 2, /**< Clock sourced from the Internal 32KHz RC Oscillator clock. */ + CLOCK_SRC_XOSC = 3, /**< Clock sourced from the External Oscillator clock. */ + CLOCK_SRC_PLL = 4, /**< Clock sourced from the Internal PLL clock. */ + }; + + /* Inline Functions: */ + /** Starts the external oscillator of the XMEGA microcontroller, with the given options. This routine blocks until + * the oscillator is ready for use. + * + * \param[in] FreqRange Frequency range of the external oscillator, a value from \ref XMEGA_Extern_OSC_ClockFrequency_t. + * \param[in] Startup Statup time of the external oscillator, a value from \ref XMEGA_Extern_OSC_ClockStartup_t. + * + * \return Boolean \c true if the external oscillator was successfully started, \c false if invalid parameters specified. + */ + static inline bool XMEGACLK_StartExternalOscillator(const uint8_t FreqRange, + const uint8_t Startup) ATTR_ALWAYS_INLINE; + static inline bool XMEGACLK_StartExternalOscillator(const uint8_t FreqRange, + const uint8_t Startup) + { + OSC.XOSCCTRL = (FreqRange | ((Startup == EXOSC_START_32KCLK) ? OSC_X32KLPM_bm : 0) | Startup); + OSC.CTRL |= OSC_XOSCEN_bm; + + while (!(OSC.STATUS & OSC_XOSCRDY_bm)); + return true; + } + + /** Stops the external oscillator of the XMEGA microcontroller. */ + static inline void XMEGACLK_StopExternalOscillator(void) ATTR_ALWAYS_INLINE; + static inline void XMEGACLK_StopExternalOscillator(void) + { + OSC.CTRL &= ~OSC_XOSCEN_bm; + } + + /** Starts the given internal oscillator of the XMEGA microcontroller, with the given options. This routine blocks until + * the oscillator is ready for use. + * + * \param[in] Source Internal oscillator to start, a value from \ref XMEGA_System_ClockSource_t. + * + * \return Boolean \c true if the internal oscillator was successfully started, \c false if invalid parameters specified. + */ + static inline uint8_t XMEGACLK_StartInternalOscillator(const uint8_t Source) ATTR_ALWAYS_INLINE; + static inline uint8_t XMEGACLK_StartInternalOscillator(const uint8_t Source) + { + switch (Source) + { + case CLOCK_SRC_INT_RC2MHZ: + OSC.CTRL |= OSC_RC2MEN_bm; + while (!(OSC.STATUS & OSC_RC2MRDY_bm)); + return true; + case CLOCK_SRC_INT_RC32MHZ: + OSC.CTRL |= OSC_RC32MEN_bm; + while (!(OSC.STATUS & OSC_RC32MRDY_bm)); + return true; + case CLOCK_SRC_INT_RC32KHZ: + OSC.CTRL |= OSC_RC32KEN_bm; + while (!(OSC.STATUS & OSC_RC32KRDY_bm)); + return true; + } + + return false; + } + + /** Stops the given internal oscillator of the XMEGA microcontroller. + * + * \param[in] Source Internal oscillator to stop, a value from \ref XMEGA_System_ClockSource_t. + * + * \return Boolean \c true if the internal oscillator was successfully stopped, \c false if invalid parameters specified. + */ + static inline bool XMEGACLK_StopInternalOscillator(const uint8_t Source) ATTR_ALWAYS_INLINE; + static inline bool XMEGACLK_StopInternalOscillator(const uint8_t Source) + { + switch (Source) + { + case CLOCK_SRC_INT_RC2MHZ: + OSC.CTRL &= ~OSC_RC2MEN_bm; + return true; + case CLOCK_SRC_INT_RC32MHZ: + OSC.CTRL &= ~OSC_RC32MEN_bm; + return true; + case CLOCK_SRC_INT_RC32KHZ: + OSC.CTRL &= ~OSC_RC32KEN_bm; + return true; + } + + return false; + } + + /** Starts the PLL of the XMEGA microcontroller, with the given options. This routine blocks until the PLL is ready for use. + * + * \note The output frequency must be equal to or greater than the source frequency. + * + * \param[in] Source Clock source for the PLL, a value from \ref XMEGA_System_ClockSource_t. + * \param[in] SourceFreq Frequency of the PLL's clock source, in Hz. + * \param[in] Frequency Target frequency of the PLL's output. + * + * \return Boolean \c true if the PLL was successfully started, \c false if invalid parameters specified. + */ + static inline bool XMEGACLK_StartPLL(const uint8_t Source, + const uint32_t SourceFreq, + const uint32_t Frequency) ATTR_ALWAYS_INLINE; + static inline bool XMEGACLK_StartPLL(const uint8_t Source, + const uint32_t SourceFreq, + const uint32_t Frequency) + { + uint8_t MulFactor = (Frequency / SourceFreq); + + if (SourceFreq > Frequency) + return false; + + switch (Source) + { + case CLOCK_SRC_INT_RC2MHZ: + OSC.PLLCTRL = (OSC_PLLSRC_RC2M_gc | MulFactor); + break; + case CLOCK_SRC_INT_RC32MHZ: + OSC.PLLCTRL = (OSC_PLLSRC_RC32M_gc | MulFactor); + break; + case CLOCK_SRC_XOSC: + OSC.PLLCTRL = (OSC_PLLSRC_XOSC_gc | MulFactor); + break; + default: + return false; + } + + OSC.CTRL |= OSC_PLLEN_bm; + + while (!(OSC.STATUS & OSC_PLLRDY_bm)); + return true; + } + + /** Stops the PLL of the XMEGA microcontroller. */ + static inline void XMEGACLK_StopPLL(void) ATTR_ALWAYS_INLINE; + static inline void XMEGACLK_StopPLL(void) + { + OSC.CTRL &= ~OSC_PLLEN_bm; + } + + /** Sets the clock source for the main microcontroller core. The given clock source should be configured + * and ready for use before this function is called. + * + * \param[in] Source Clock source for the CPU core, a value from \ref XMEGA_System_ClockSource_t. + * \param[in] SourceFreq Frequency of the CPU core's clock source, in Hz. + * + * \return Boolean \c true if the CPU core clock was sucessfully altered, \c false if invalid parameters specified. + */ + static inline bool XMEGACLK_SetCPUClockSource(const uint8_t Source, + const uint32_t SourceFreq) ATTR_ALWAYS_INLINE; + static inline bool XMEGACLK_SetCPUClockSource(const uint8_t Source, + const uint32_t SourceFreq) + { + uint8_t ClockSourceMask = 0; + + switch (Source) + { + case CLOCK_SRC_INT_RC2MHZ: + ClockSourceMask = CLK_SCLKSEL_RC2M_gc; + break; + case CLOCK_SRC_INT_RC32MHZ: + ClockSourceMask = CLK_SCLKSEL_RC32M_gc; + break; + case CLOCK_SRC_INT_RC32KHZ: + ClockSourceMask = CLK_SCLKSEL_RC32K_gc; + break; + case CLOCK_SRC_XOSC: + ClockSourceMask = CLK_SCLKSEL_XOSC_gc; + break; + case CLOCK_SRC_PLL: + ClockSourceMask = CLK_SCLKSEL_PLL_gc; + break; + default: + return false; + } + + uint_reg_t CurrentGlobalInt = GetGlobalInterruptMask(); + GlobalInterruptDisable(); + + CCP = CCP_IOREG_gc; + CLK.CTRL = ClockSourceMask; + + SetGlobalInterruptMask(CurrentGlobalInt); + + Delay_MS(1); + return (CLK.CTRL == ClockSourceMask); + } + + /* Disable C linkage for C++ Compilers: */ + #if defined(__cplusplus) + } + #endif + +#endif + +/** @} */ -- cgit v1.2.3