/*
LUFA Library
- Copyright (C) Dean Camera, 2012.
+ Copyright (C) Dean Camera, 2018.
dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/
/*
- Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
+ Copyright 2018 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
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
- The author disclaim all warranties with regard to this
+ The author disclaims 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
* \defgroup Group_PlatformDrivers_UC3Clocks Clock Management Driver - LUFA/Platform/UC3/ClockManagement.h
* \brief Module Clock Driver for the AVR32 UC3 microcontrollers.
*
- * \section Sec_Dependencies Module Source Dependencies
+ * \section Sec_PlatformDrivers_UC3Clocks_Dependencies Module Source Dependencies
* The following files must be built with any user project that uses this module:
* - None
*
- * \section Sec_ModDescription Module Description
+ * \section Sec_PlatformDrivers_UC3Clocks_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.
*
* 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);
+ * UC3CLK_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);
+ * UC3CLK_StartPLL(0, CLOCK_SRC_OSC0, 12000000, F_CPU);
+ * UC3CLK_SetCPUClockSource(CLOCK_SRC_PLL0, F_CPU);
*
* // Start the PLL for the USB Generic Clock module
- * AVR32CLK_StartPLL(1, CLOCK_SRC_OSC0, 12000000, 48000000);
+ * UC3CLK_StartPLL(1, CLOCK_SRC_OSC0, 12000000, 48000000);
* }
* \endcode
*
#define _UC3_CLOCK_MANAGEMENT_H_
/* Includes: */
- #include <LUFA/Common/Common.h>
+ #include "../../Common/Common.h"
/* Enable C linkage for C++ Compilers: */
#if defined(__cplusplus)
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 for the possible external oscillator startup 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. */
+ EXOSC_START_64CLK = AVR32_PM_OSCCTRL0_STARTUP_64_RCOSC, /**< Wait 64 clock cycles before startup for stability. */
+ EXOSC_START_128CLK = AVR32_PM_OSCCTRL0_STARTUP_128_RCOSC, /**< Wait 128 clock cycles before startup for stability. */
+ EXOSC_START_2048CLK = AVR32_PM_OSCCTRL0_STARTUP_2048_RCOSC, /**< Wait 2048 clock cycles before startup for stability. */
+ EXOSC_START_4096CLK = AVR32_PM_OSCCTRL0_STARTUP_4096_RCOSC, /**< Wait 4096 clock cycles before startup for stability. */
+ EXOSC_START_8192CLK = AVR32_PM_OSCCTRL0_STARTUP_8192_RCOSC, /**< Wait 8192 clock cycles before startup for stability. */
+ EXOSC_START_16384CLK = AVR32_PM_OSCCTRL0_STARTUP_16384_RCOSC, /**< Wait 16384 clock cycles before startup for stability. */
};
/** Enum for the possible module clock sources. */
*
* \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.
+ * \param[in] Startup Startup 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)
+ static inline bool UC3CLK_StartExternalOscillator(const uint8_t Channel,
+ const uint8_t Type,
+ const uint8_t Startup) ATTR_ALWAYS_INLINE;
+ static inline bool UC3CLK_StartExternalOscillator(const uint8_t Channel,
+ const uint8_t Type,
+ const uint8_t Startup)
{
switch (Channel)
{
*
* \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)
+ static inline void UC3CLK_StopExternalOscillator(const uint8_t Channel) ATTR_ALWAYS_INLINE;
+ static inline void UC3CLK_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.
+ * \attention 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.
*
* \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)
+ static inline bool UC3CLK_StartPLL(const uint8_t Channel,
+ const uint8_t Source,
+ const uint32_t SourceFreq,
+ const uint32_t Frequency) ATTR_ALWAYS_INLINE;
+ static inline bool UC3CLK_StartPLL(const uint8_t Channel,
+ const uint8_t Source,
+ const uint32_t SourceFreq,
+ const uint32_t Frequency)
{
if (SourceFreq > Frequency)
return false;
*
* \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)
+ static inline void UC3CLK_StopPLL(const uint8_t Channel) ATTR_ALWAYS_INLINE;
+ static inline void UC3CLK_StopPLL(const uint8_t Channel)
{
AVR32_PM.PLL[Channel].pllen = false;
}
*
* \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)
+ static inline bool UC3CLK_StartGenericClock(const uint8_t Channel,
+ const uint8_t Source,
+ const uint32_t SourceFreq,
+ const uint32_t Frequency) ATTR_ALWAYS_INLINE;
+ static inline bool UC3CLK_StartGenericClock(const uint8_t Channel,
+ const uint8_t Source,
+ const uint32_t SourceFreq,
+ const uint32_t Frequency)
{
+ if (Channel >= AVR32_PM_GCLK_NUM)
+ return false;
+
+ if (SourceFreq < Frequency)
+ return false;
+
switch (Source)
{
case CLOCK_SRC_OSC0:
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;
/** Stops the given generic clock of the UC3 microcontroller.
*
* \param[in] Channel Index of the generic clock to stop.
+ *
+ * \return Boolean \c true if the generic clock was successfully stopped, \c false if invalid parameters specified.
*/
- static inline void AVR32CLK_StopGenericClock(const uint8_t Channel) ATTR_ALWAYS_INLINE;
- static inline void AVR32CLK_StopGenericClock(const uint8_t Channel)
+ static inline bool UC3CLK_StopGenericClock(const uint8_t Channel) ATTR_ALWAYS_INLINE;
+ static inline bool UC3CLK_StopGenericClock(const uint8_t Channel)
{
+ if (Channel >= AVR32_PM_GCLK_NUM)
+ return false;
+
AVR32_PM.GCCTRL[Channel].cen = false;
+
+ return true;
}
/** Sets the clock source for the main microcontroller core. The given clock source should be configured
* \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.
+ * \return Boolean \c true if the CPU core clock was successfully 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)
+ static inline bool UC3CLK_SetCPUClockSource(const uint8_t Source,
+ const uint32_t SourceFreq) ATTR_ALWAYS_INLINE;
+ static inline bool UC3CLK_SetCPUClockSource(const uint8_t Source,
+ const uint32_t SourceFreq)
{
- AVR32_FLASHC.FCR.fws = (SourceFreq > 30000000) ? true : false;
+ if (SourceFreq > AVR32_PM_CPU_MAX_FREQ)
+ return false;
+
+ AVR32_FLASHC.FCR.fws = (SourceFreq > AVR32_FLASHC_FWS_0_MAX_FREQ) ? true : false;
switch (Source)
{