Fix compile errors in the UC3 platform support due to a forward reference and an...
[pub/USBasp.git] / LUFA / Platform / UC3 / 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 AVR32 UC3 microcontrollers.
33 *
34 * Clock management driver for the AVR32 UC3 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_UC3Clocks UC3 Clock Management Driver - LUFA/Platform/UC3/ClockManagement.h
40 * \brief Module Clock Driver for the AVR32 UC3 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 AVR32 UC3 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/UC3/ClockManagement.h>
53 *
54 * void main(void)
55 * {
56 * // Start the master external oscillator which will be used as the main clock reference
57 * AVR32CLK_StartExternalOscillator(0, EXOSC_MODE_8MHZ_OR_MORE, EXOSC_START_0CLK);
58 *
59 * // Start the PLL for the CPU clock, switch CPU to it
60 * AVR32CLK_StartPLL(0, CLOCK_SRC_OSC0, 12000000, F_CPU);
61 * AVR32CLK_SetCPUClockSource(CLOCK_SRC_PLL0, F_CPU);
62 *
63 * // Start the PLL for the USB Generic Clock module
64 * AVR32CLK_StartPLL(1, CLOCK_SRC_OSC0, 12000000, 48000000);
65 * }
66 * \endcode
67 *
68 * @{
69 */
70
71 #ifndef _UC3_CLOCK_MANAGEMENT_H_
72 #define _UC3_CLOCK_MANAGEMENT_H_
73
74 /* Includes: */
75 #include <avr32/io.h>
76 #include <stdbool.h>
77 #include <stdint.h>
78
79 #include <LUFA/Common/Common.h>
80
81 /* Enable C linkage for C++ Compilers: */
82 #if defined(__cplusplus)
83 extern "C" {
84 #endif
85
86 /* Public Interface - May be used in end-application: */
87 /* Macros: */
88 /** Enum for the possible external oscillator types. */
89 enum Extern_OSC_ClockTypes_t
90 {
91 EXOSC_MODE_CLOCK = 0, /**< External clock (non-crystal) mode. */
92 EXOSC_MODE_900KHZ_MAX = 1, /**< External crystal oscillator equal to or slower than 900KHz. */
93 EXOSC_MODE_3MHZ_MAX = 2, /**< External crystal oscillator equal to or slower than 3MHz. */
94 EXOSC_MODE_8MHZ_MAX = 3, /**< External crystal oscillator equal to or slower than 8MHz. */
95 EXOSC_MODE_8MHZ_OR_MORE = 4, /**< External crystal oscillator equal to or faster than 8MHz. */
96 };
97
98 /** Enum for the possible external oscillator statup times. */
99 enum Extern_OSC_ClockStartup_t
100 {
101 EXOSC_START_0CLK = 0, /**< Immediate startup, no delay. */
102 EXOSC_START_64CLK = 1, /**< Wait 64 clock cyles before startup for stability. */
103 EXOSC_START_128CLK = 2, /**< Wait 128 clock cyles before startup for stability. */
104 EXOSC_START_2048CLK = 3, /**< Wait 2048 clock cyles before startup for stability. */
105 EXOSC_START_4096CLK = 4, /**< Wait 4096 clock cyles before startup for stability. */
106 EXOSC_START_8192CLK = 5, /**< Wait 8192 clock cyles before startup for stability. */
107 EXOSC_START_16384CLK = 6, /**< Wait 16384 clock cyles before startup for stability. */
108 };
109
110 /** Enum for the possible module clock sources. */
111 enum System_ClockSource_t
112 {
113 CLOCK_SRC_SLOW_CLK = 0, /**< Clock sourced from the internal slow clock. */
114 CLOCK_SRC_OSC0 = 1, /**< Clock sourced from the Oscillator 0 clock. */
115 CLOCK_SRC_OSC1 = 2, /**< Clock sourced from the Oscillator 1 clock. */
116 CLOCK_SRC_PLL0 = 3, /**< Clock sourced from the PLL 0 clock. */
117 CLOCK_SRC_PLL1 = 4, /**< Clock sourced from the PLL 1 clock. */
118 };
119
120 /* Inline Functions: */
121 /** Starts the given external oscillator of the UC3 microcontroller, with the given options. This routine blocks until
122 * the oscillator is ready for use.
123 *
124 * \param[in] Channel Index of the external oscillator to start.
125 * \param[in] Type Type of clock attached to the given oscillator channel, a value from \ref Extern_OSC_ClockTypes_t.
126 * \param[in] Startup Statup time of the external oscillator, a value from \ref Extern_OSC_ClockStartup_t.
127 *
128 * \return Boolean \c true if the external oscillator was successfully started, \c false if invalid parameters specified.
129 */
130 static inline uint8_t AVR32CLK_StartExternalOscillator(const uint8_t Channel,
131 const uint8_t Type,
132 const uint8_t Startup) ATTR_ALWAYS_INLINE;
133 static inline uint8_t AVR32CLK_StartExternalOscillator(const uint8_t Channel,
134 const uint8_t Type,
135 const uint8_t Startup)
136 {
137 switch (Channel)
138 {
139 case 0:
140 AVR32_PM.OSCCTRL0.startup = Startup;
141 AVR32_PM.OSCCTRL0.mode = Type;
142 break;
143 case 1:
144 AVR32_PM.OSCCTRL1.startup = Startup;
145 AVR32_PM.OSCCTRL1.mode = Type;
146 break;
147 default:
148 return false;
149 }
150
151 AVR32_PM.mcctrl |= (1 << (AVR32_PM_MCCTRL_OSC0EN_OFFSET + Channel));
152
153 while (!(AVR32_PM.poscsr & (1 << (AVR32_PM_POSCSR_OSC0RDY_OFFSET + Channel))));
154 return true;
155 }
156
157 /** Stops the given external oscillator of the UC3 microcontroller.
158 *
159 * \param[in] Channel Index of the external oscillator to stop.
160 */
161 static inline void AVR32CLK_StopExternalOscillator(const uint8_t Channel) ATTR_ALWAYS_INLINE;
162 static inline void AVR32CLK_StopExternalOscillator(const uint8_t Channel)
163 {
164 AVR32_PM.mcctrl &= ~(1 << (AVR32_PM_MCCTRL_OSC0EN_OFFSET + Channel));
165 }
166
167 /** Starts the given PLL of the UC3 microcontroller, with the given options. This routine blocks until the PLL is ready for use.
168 *
169 * \param[in] Channel Index of the PLL to start.
170 * \param[in] Source Clock source for the PLL, a value from \ref System_ClockSource_t.
171 * \param[in] SourceFreq Frequency of the PLL's clock source, in Hz.
172 * \param[in] Frequency Target frequency of the PLL's output.
173 *
174 * \return Boolean \c true if the PLL was successfully started, \c false if invalid parameters specified.
175 */
176 static inline bool AVR32CLK_StartPLL(const uint8_t Channel,
177 const uint8_t Source,
178 const uint32_t SourceFreq,
179 const uint32_t Frequency) ATTR_ALWAYS_INLINE;
180 static inline bool AVR32CLK_StartPLL(const uint8_t Channel,
181 const uint8_t Source,
182 const uint32_t SourceFreq,
183 const uint32_t Frequency)
184 {
185 switch (Source)
186 {
187 case CLOCK_SRC_OSC0:
188 AVR32_PM.PLL[Channel].pllosc = 0;
189 break;
190 case CLOCK_SRC_OSC1:
191 AVR32_PM.PLL[Channel].pllosc = 1;
192 break;
193 default:
194 return false;
195 }
196
197 AVR32_PM.PLL[Channel].pllmul = (Frequency / SourceFreq) ? (((Frequency / SourceFreq) - 1) / 2) : 0;
198 AVR32_PM.PLL[Channel].plldiv = 0;
199 AVR32_PM.PLL[Channel].pllen = true;
200
201 while (!(AVR32_PM.poscsr & (1 << (AVR32_PM_POSCSR_LOCK0_OFFSET + Channel))));
202 return true;
203 }
204
205 /** Stops the given PLL of the UC3 microcontroller.
206 *
207 * \param[in] Channel Index of the PLL to stop.
208 */
209 static inline void AVR32CLK_StopPLL(const uint8_t Channel) ATTR_ALWAYS_INLINE;
210 static inline void AVR32CLK_StopPLL(const uint8_t Channel)
211 {
212 AVR32_PM.PLL[Channel].pllen = false;
213 }
214
215 /** Starts the given Generic Clock of the UC3 microcontroller, with the given options.
216 *
217 * \param[in] Channel Index of the Generic Clock to start.
218 * \param[in] Source Clock source for the Generic Clock, a value from \ref System_ClockSource_t.
219 * \param[in] SourceFreq Frequency of the Generic Clock's clock source, in Hz.
220 * \param[in] Frequency Target frequency of the Generic Clock's output.
221 *
222 * \return Boolean \c true if the Generic Clock was successfully started, \c false if invalid parameters specified.
223 */
224 static inline bool AVR32CLK_StartGenericClock(const uint8_t Channel,
225 const uint8_t Source,
226 const uint32_t SourceFreq,
227 const uint32_t Frequency) ATTR_ALWAYS_INLINE;
228 static inline bool AVR32CLK_StartGenericClock(const uint8_t Channel,
229 const uint8_t Source,
230 const uint32_t SourceFreq,
231 const uint32_t Frequency)
232 {
233 switch (Source)
234 {
235 case CLOCK_SRC_OSC0:
236 AVR32_PM.GCCTRL[Channel].pllsel = 0;
237 AVR32_PM.GCCTRL[Channel].oscsel = 0;
238 break;
239 case CLOCK_SRC_OSC1:
240 AVR32_PM.GCCTRL[Channel].pllsel = 0;
241 AVR32_PM.GCCTRL[Channel].oscsel = 1;
242 break;
243 case CLOCK_SRC_PLL0:
244 AVR32_PM.GCCTRL[Channel].pllsel = 1;
245 AVR32_PM.GCCTRL[Channel].oscsel = 0;
246 break;
247 case CLOCK_SRC_PLL1:
248 AVR32_PM.GCCTRL[Channel].pllsel = 1;
249 AVR32_PM.GCCTRL[Channel].oscsel = 1;
250 break;
251 default:
252 return false;
253 }
254
255 if (SourceFreq < Frequency)
256 return false;
257
258 AVR32_PM.GCCTRL[Channel].diven = (SourceFreq > Frequency) ? true : false;
259 AVR32_PM.GCCTRL[Channel].div = (((SourceFreq / Frequency) - 1) / 2);
260 AVR32_PM.GCCTRL[Channel].cen = true;
261
262 return true;
263 }
264
265 /** Stops the given generic clock of the UC3 microcontroller.
266 *
267 * \param[in] Channel Index of the generic clock to stop.
268 */
269 static inline void AVR32CLK_StopGenericClock(const uint8_t Channel) ATTR_ALWAYS_INLINE;
270 static inline void AVR32CLK_StopGenericClock(const uint8_t Channel)
271 {
272 AVR32_PM.GCCTRL[Channel].cen = false;
273 }
274
275 /** Sets the clock source for the main microcontroller core. The given clock source should be configured
276 * and ready for use before this function is called.
277 *
278 * This function will configure the FLASH controller's wait states automatically to suit the given clock source.
279 *
280 * \param[in] Source Clock source for the CPU core, a value from \ref System_ClockSource_t.
281 * \param[in] SourceFreq Frequency of the CPU core's clock source, in Hz.
282 *
283 * \return Boolean \c true if the CPU core clock was sucessfully altered, \c false if invalid parameters specified.
284 */
285 static inline bool AVR32CLK_SetCPUClockSource(const uint8_t Source,
286 const uint32_t SourceFreq) ATTR_ALWAYS_INLINE;
287 static inline bool AVR32CLK_SetCPUClockSource(const uint8_t Source,
288 const uint32_t SourceFreq)
289 {
290 AVR32_FLASHC.FCR.fws = (SourceFreq > 30000000) ? true : false;
291
292 switch (Source)
293 {
294 case CLOCK_SRC_SLOW_CLK:
295 AVR32_PM.MCCTRL.mcsel = 0;
296 break;
297 case CLOCK_SRC_OSC0:
298 AVR32_PM.MCCTRL.mcsel = 1;
299 break;
300 case CLOCK_SRC_PLL0:
301 AVR32_PM.MCCTRL.mcsel = 2;
302 break;
303 default:
304 return false;
305 }
306
307 return true;
308 }
309
310 /* Disable C linkage for C++ Compilers: */
311 #if defined(__cplusplus)
312 }
313 #endif
314
315 #endif
316
317 /** @} */