Fix names of XMEGA ClockManagement driver functions.
[pub/USBasp.git] / LUFA / Platform / XMEGA / ClockManagement.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 /** \file
32 * \brief Module Clock Driver for the AVR USB XMEGA microcontrollers.
33 *
34 * Clock management driver for the AVR USB XMEGA microcontrollers. This driver allows for the configuration
35 * of the various clocks within the device to clock the various peripherals.
36 */
37
38 /** \ingroup Group_PlatformDrivers
39 * \defgroup Group_PlatformDrivers_XMEGAClocks AVR USB XMEGA Clock Management Driver - LUFA/Platform/XMEGA/ClockManagement.h
40 * \brief Module Clock Driver for the AVR USB XMEGA microcontrollers.
41 *
42 * \section Sec_Dependencies Module Source Dependencies
43 * The following files must be built with any user project that uses this module:
44 * - None
45 *
46 * \section Sec_ModDescription Module Description
47 * Clock management driver for the AVR USB XMEGA microcontrollers. This driver allows for the configuration
48 * of the various clocks within the device to clock the various peripherals.
49 *
50 * Usage Example:
51 * \code
52 * #include <LUFA/Platform/XMEGA/ClockManagement.h>
53 *
54 * void main(void)
55 * {
56 * // Start the internal 32MHz RC oscillator and switch the CPU core to run from it
57 * AVR32CLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
58 * XMEGACLK_SetCPUClockSource(CLOCK_SRC_INT_RC32MHZ, F_CPU);
59 *
60 * // Start the external oscillator and multiply up the frequency
61 * AVR32CLK_StartExternalOscillator(EXOSC_FREQ_9MHZ_MAX, EXOSC_START_1KCLK);
62 * AVR32CLK_StartPLL(CLOCK_SRC_XOSC, 8000000, F_USB);
63 * }
64 * \endcode
65 *
66 * @{
67 */
68
69 #ifndef _XMEGA_CLOCK_MANAGEMENT_H_
70 #define _XMEGA_CLOCK_MANAGEMENT_H_
71
72 /* Includes: */
73 #include <LUFA/Common/Common.h>
74
75 /* Enable C linkage for C++ Compilers: */
76 #if defined(__cplusplus)
77 extern "C" {
78 #endif
79
80 /* Public Interface - May be used in end-application: */
81 /* Macros: */
82 /** Enum for the possible external oscillator frequency ranges. */
83 enum XMEGA_Extern_OSC_ClockFrequency_t
84 {
85 EXOSC_FREQ_2MHZ_MAX = OSC_FRQRANGE_04TO2_gc, /**< External crystal oscillator equal to or slower than 2MHz. */
86 EXOSC_FREQ_9MHZ_MAX = OSC_FRQRANGE_2TO9_gc, /**< External crystal oscillator equal to or slower than 9MHz. */
87 EXOSC_FREQ_12MHZ_MAX = OSC_FRQRANGE_9TO12_gc, /**< External crystal oscillator equal to or slower than 12MHz. */
88 EXOSC_FREQ_16MHZ_MAX = OSC_FRQRANGE_12TO16_gc, /**< External crystal oscillator equal to or slower than 16MHz. */
89 };
90
91 /** Enum for the possible external oscillator statup times. */
92 enum XMEGA_Extern_OSC_ClockStartup_t
93 {
94 EXOSC_START_6CLK = OSC_XOSCSEL_EXTCLK_gc, /**< Wait 6 clock cycles before startup (external clock). */
95 EXOSC_START_32KCLK = OSC_XOSCSEL_32KHz_gc, /**< Wait 32K clock cycles before startup (32.768KHz crystal). */
96 EXOSC_START_256CLK = OSC_XOSCSEL_XTAL_256CLK_gc, /**< Wait 256 clock cycles before startup. */
97 EXOSC_START_1KCLK = OSC_XOSCSEL_XTAL_1KCLK_gc, /**< Wait 1K clock cycles before startup. */
98 EXOSC_START_16KCLK = OSC_XOSCSEL_XTAL_16KCLK_gc, /**< Wait 16K clock cycles before startup. */
99 };
100
101 /** Enum for the possible module clock sources. */
102 enum XMEGA_System_ClockSource_t
103 {
104 CLOCK_SRC_INT_RC2MHZ = 0, /**< Clock sourced from the Internal 2MHz RC Oscillator clock. */
105 CLOCK_SRC_INT_RC32MHZ = 1, /**< Clock sourced from the Internal 32MHz RC Oscillator clock. */
106 CLOCK_SRC_INT_RC32KHZ = 2, /**< Clock sourced from the Internal 32KHz RC Oscillator clock. */
107 CLOCK_SRC_XOSC = 3, /**< Clock sourced from the External Oscillator clock. */
108 CLOCK_SRC_PLL = 4, /**< Clock sourced from the Internal PLL clock. */
109 };
110
111 /* Inline Functions: */
112 /** Starts the external oscillator of the XMEGA microcontroller, with the given options. This routine blocks until
113 * the oscillator is ready for use.
114 *
115 * \param[in] FreqRange Frequency range of the external oscillator, a value from \ref XMEGA_Extern_OSC_ClockFrequency_t.
116 * \param[in] Startup Statup time of the external oscillator, a value from \ref XMEGA_Extern_OSC_ClockStartup_t.
117 *
118 * \return Boolean \c true if the external oscillator was successfully started, \c false if invalid parameters specified.
119 */
120 static inline bool XMEGACLK_StartExternalOscillator(const uint8_t FreqRange,
121 const uint8_t Startup) ATTR_ALWAYS_INLINE;
122 static inline bool XMEGACLK_StartExternalOscillator(const uint8_t FreqRange,
123 const uint8_t Startup)
124 {
125 OSC.XOSCCTRL = (FreqRange | ((Startup == EXOSC_START_32KCLK) ? OSC_X32KLPM_bm : 0) | Startup);
126 OSC.CTRL |= OSC_XOSCEN_bm;
127
128 while (!(OSC.STATUS & OSC_XOSCRDY_bm));
129 return true;
130 }
131
132 /** Stops the external oscillator of the XMEGA microcontroller. */
133 static inline void XMEGACLK_StopExternalOscillator(void) ATTR_ALWAYS_INLINE;
134 static inline void XMEGACLK_StopExternalOscillator(void)
135 {
136 OSC.CTRL &= ~OSC_XOSCEN_bm;
137 }
138
139 /** Starts the given internal oscillator of the XMEGA microcontroller, with the given options. This routine blocks until
140 * the oscillator is ready for use.
141 *
142 * \param[in] Source Internal oscillator to start, a value from \ref XMEGA_System_ClockSource_t.
143 *
144 * \return Boolean \c true if the internal oscillator was successfully started, \c false if invalid parameters specified.
145 */
146 static inline uint8_t XMEGACLK_StartInternalOscillator(const uint8_t Source) ATTR_ALWAYS_INLINE;
147 static inline uint8_t XMEGACLK_StartInternalOscillator(const uint8_t Source)
148 {
149 switch (Source)
150 {
151 case CLOCK_SRC_INT_RC2MHZ:
152 OSC.CTRL |= OSC_RC2MEN_bm;
153 while (!(OSC.STATUS & OSC_RC2MRDY_bm));
154 return true;
155 case CLOCK_SRC_INT_RC32MHZ:
156 OSC.CTRL |= OSC_RC32MEN_bm;
157 while (!(OSC.STATUS & OSC_RC32MRDY_bm));
158 return true;
159 case CLOCK_SRC_INT_RC32KHZ:
160 OSC.CTRL |= OSC_RC32KEN_bm;
161 while (!(OSC.STATUS & OSC_RC32KRDY_bm));
162 return true;
163 }
164
165 return false;
166 }
167
168 /** Stops the given internal oscillator of the XMEGA microcontroller.
169 *
170 * \param[in] Source Internal oscillator to stop, a value from \ref XMEGA_System_ClockSource_t.
171 *
172 * \return Boolean \c true if the internal oscillator was successfully stopped, \c false if invalid parameters specified.
173 */
174 static inline bool XMEGACLK_StopInternalOscillator(const uint8_t Source) ATTR_ALWAYS_INLINE;
175 static inline bool XMEGACLK_StopInternalOscillator(const uint8_t Source)
176 {
177 switch (Source)
178 {
179 case CLOCK_SRC_INT_RC2MHZ:
180 OSC.CTRL &= ~OSC_RC2MEN_bm;
181 return true;
182 case CLOCK_SRC_INT_RC32MHZ:
183 OSC.CTRL &= ~OSC_RC32MEN_bm;
184 return true;
185 case CLOCK_SRC_INT_RC32KHZ:
186 OSC.CTRL &= ~OSC_RC32KEN_bm;
187 return true;
188 }
189
190 return false;
191 }
192
193 /** Starts the PLL of the XMEGA microcontroller, with the given options. This routine blocks until the PLL is ready for use.
194 *
195 * \note The output frequency must be equal to or greater than the source frequency.
196 *
197 * \param[in] Source Clock source for the PLL, a value from \ref XMEGA_System_ClockSource_t.
198 * \param[in] SourceFreq Frequency of the PLL's clock source, in Hz.
199 * \param[in] Frequency Target frequency of the PLL's output.
200 *
201 * \return Boolean \c true if the PLL was successfully started, \c false if invalid parameters specified.
202 */
203 static inline bool XMEGACLK_StartPLL(const uint8_t Source,
204 const uint32_t SourceFreq,
205 const uint32_t Frequency) ATTR_ALWAYS_INLINE;
206 static inline bool XMEGACLK_StartPLL(const uint8_t Source,
207 const uint32_t SourceFreq,
208 const uint32_t Frequency)
209 {
210 uint8_t MulFactor = (Frequency / SourceFreq);
211
212 if (SourceFreq > Frequency)
213 return false;
214
215 switch (Source)
216 {
217 case CLOCK_SRC_INT_RC2MHZ:
218 OSC.PLLCTRL = (OSC_PLLSRC_RC2M_gc | MulFactor);
219 break;
220 case CLOCK_SRC_INT_RC32MHZ:
221 OSC.PLLCTRL = (OSC_PLLSRC_RC32M_gc | MulFactor);
222 break;
223 case CLOCK_SRC_XOSC:
224 OSC.PLLCTRL = (OSC_PLLSRC_XOSC_gc | MulFactor);
225 break;
226 default:
227 return false;
228 }
229
230 OSC.CTRL |= OSC_PLLEN_bm;
231
232 while (!(OSC.STATUS & OSC_PLLRDY_bm));
233 return true;
234 }
235
236 /** Stops the PLL of the XMEGA microcontroller. */
237 static inline void XMEGACLK_StopPLL(void) ATTR_ALWAYS_INLINE;
238 static inline void XMEGACLK_StopPLL(void)
239 {
240 OSC.CTRL &= ~OSC_PLLEN_bm;
241 }
242
243 /** Sets the clock source for the main microcontroller core. The given clock source should be configured
244 * and ready for use before this function is called.
245 *
246 * \param[in] Source Clock source for the CPU core, a value from \ref XMEGA_System_ClockSource_t.
247 * \param[in] SourceFreq Frequency of the CPU core's clock source, in Hz.
248 *
249 * \return Boolean \c true if the CPU core clock was sucessfully altered, \c false if invalid parameters specified.
250 */
251 static inline bool XMEGACLK_SetCPUClockSource(const uint8_t Source,
252 const uint32_t SourceFreq) ATTR_ALWAYS_INLINE;
253 static inline bool XMEGACLK_SetCPUClockSource(const uint8_t Source,
254 const uint32_t SourceFreq)
255 {
256 uint8_t ClockSourceMask = 0;
257
258 switch (Source)
259 {
260 case CLOCK_SRC_INT_RC2MHZ:
261 ClockSourceMask = CLK_SCLKSEL_RC2M_gc;
262 break;
263 case CLOCK_SRC_INT_RC32MHZ:
264 ClockSourceMask = CLK_SCLKSEL_RC32M_gc;
265 break;
266 case CLOCK_SRC_INT_RC32KHZ:
267 ClockSourceMask = CLK_SCLKSEL_RC32K_gc;
268 break;
269 case CLOCK_SRC_XOSC:
270 ClockSourceMask = CLK_SCLKSEL_XOSC_gc;
271 break;
272 case CLOCK_SRC_PLL:
273 ClockSourceMask = CLK_SCLKSEL_PLL_gc;
274 break;
275 default:
276 return false;
277 }
278
279 uint_reg_t CurrentGlobalInt = GetGlobalInterruptMask();
280 GlobalInterruptDisable();
281
282 CCP = CCP_IOREG_gc;
283 CLK.CTRL = ClockSourceMask;
284
285 SetGlobalInterruptMask(CurrentGlobalInt);
286
287 Delay_MS(1);
288 return (CLK.CTRL == ClockSourceMask);
289 }
290
291 /* Disable C linkage for C++ Compilers: */
292 #if defined(__cplusplus)
293 }
294 #endif
295
296 #endif
297
298 /** @} */