Updated all host mode demos and projects to use the EVENT_USB_Host_DeviceEnumerationC...
[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 #ifndef _CLOCK_MANAGEMENT_H_
32 #define _CLOCK_MANAGEMENT_H_
33
34 /* Includes: */
35 #include <avr32/io.h>
36 #include <stdbool.h>
37 #include <stdint.h>
38
39 #include <LUFA/Common/Common.h>
40
41 /* Macros: */
42 enum Extern_OSC_ClockStartup_t
43 {
44 EXOSC_START_0CLK = 0,
45 EXOSC_START_64CLK = 1,
46 EXOSC_START_128CLK = 2,
47 EXOSC_START_2048CLK = 3,
48 EXOSC_START_4096CLK = 4,
49 EXOSC_START_8192CLK = 5,
50 EXOSC_START_16384CLK = 6,
51 };
52
53 enum Extern_OSC_ClockMode_t
54 {
55 EXOSC_MODE_CLOCK = 0,
56 EXOSC_MODE_900KHZ_MAX = 1,
57 EXOSC_MODE_3MHZ_MAX = 2,
58 EXOSC_MODE_8MHZ_MAX = 3,
59 EXOSC_MODE_8MHZ_OR_MORE = 4,
60 };
61
62 enum
63 {
64 CLOCK_SRC_SLOW_CLK = 0,
65 CLOCK_SRC_OSC0 = 1,
66 CLOCK_SRC_OSC1 = 2,
67 CLOCK_SRC_PLL0 = 3,
68 CLOCK_SRC_PLL1 = 4,
69 };
70
71 /* Inline Functions: */
72 static inline bool AVR32CLK_StartExternalOscillator(const uint8_t Channel,
73 const uint8_t Type,
74 const uint8_t Startup)
75 {
76 AVR32_PM.OSCCTRL0.startup = Startup;
77 AVR32_PM.OSCCTRL0.mode = Type;
78 AVR32_PM.mcctrl |= (1 << (AVR32_PM_MCCTRL_OSC0EN_OFFSET + Channel));
79
80 while (!(AVR32_PM.poscsr & (1 << (AVR32_PM_POSCSR_OSC0RDY_OFFSET + Channel))));
81 return true;
82 }
83
84 static inline void AVR32CLK_StopExternalOscillator(const uint8_t Channel)
85 {
86 AVR32_PM.mcctrl &= ~(1 << (AVR32_PM_MCCTRL_OSC0EN_OFFSET + Channel));
87 }
88
89 static inline bool AVR32CLK_StartPLL(const uint8_t Channel,
90 const uint8_t Source,
91 const uint32_t SourceFreq,
92 const uint32_t Frequency)
93 {
94 switch (Source)
95 {
96 case CLOCK_SRC_OSC0:
97 AVR32_PM.PLL[Channel].pllosc = 0;
98 break;
99 case CLOCK_SRC_OSC1:
100 AVR32_PM.PLL[Channel].pllosc = 1;
101 break;
102 default:
103 return false;
104 }
105
106 AVR32_PM.PLL[Channel].pllmul = (Frequency / SourceFreq) ? (((Frequency / SourceFreq) - 1) / 2) : 0;
107 AVR32_PM.PLL[Channel].plldiv = 0;
108 AVR32_PM.PLL[Channel].pllen = true;
109
110 while (!(AVR32_PM.poscsr & (1 << (AVR32_PM_POSCSR_LOCK0_OFFSET + Channel))));
111 return true;
112 }
113
114 static inline void AVR32CLK_StopPLL(const uint8_t Channel)
115 {
116 AVR32_PM.PLL[Channel].pllen = false;
117 }
118
119 static inline bool AVR32CLK_StartGenericClock(const uint8_t Channel,
120 const uint8_t Source,
121 const uint32_t SourceFreq,
122 const uint32_t Frequency)
123 {
124 switch (Source)
125 {
126 case CLOCK_SRC_OSC0:
127 AVR32_PM.GCCTRL[Channel].pllsel = 0;
128 AVR32_PM.GCCTRL[Channel].oscsel = 0;
129 break;
130 case CLOCK_SRC_OSC1:
131 AVR32_PM.GCCTRL[Channel].pllsel = 0;
132 AVR32_PM.GCCTRL[Channel].oscsel = 1;
133 break;
134 case CLOCK_SRC_PLL0:
135 AVR32_PM.GCCTRL[Channel].pllsel = 1;
136 AVR32_PM.GCCTRL[Channel].oscsel = 0;
137 break;
138 case CLOCK_SRC_PLL1:
139 AVR32_PM.GCCTRL[Channel].pllsel = 1;
140 AVR32_PM.GCCTRL[Channel].oscsel = 1;
141 break;
142 default:
143 return false;
144 }
145
146 AVR32_PM.GCCTRL[Channel].diven = (SourceFreq > Frequency) ? true : false;
147 AVR32_PM.GCCTRL[Channel].div = (((SourceFreq / Frequency) - 1) / 2);
148 AVR32_PM.GCCTRL[Channel].cen = true;
149
150 return true;
151 }
152
153 static inline void AVR32CLK_StopGenericClock(const uint8_t Channel)
154 {
155 AVR32_PM.GCCTRL[Channel].cen = false;
156 }
157
158 static inline bool AVR32CLK_SetCPUClockSource(const uint8_t Source,
159 const uint32_t SourceFreq)
160 {
161 AVR32_FLASHC.FCR.fws = (SourceFreq > 30000000) ? true : false;
162
163 switch (Source)
164 {
165 case CLOCK_SRC_SLOW_CLK:
166 AVR32_PM.MCCTRL.mcsel = 0;
167 break;
168 case CLOCK_SRC_OSC0:
169 AVR32_PM.MCCTRL.mcsel = 1;
170 break;
171 case CLOCK_SRC_PLL0:
172 AVR32_PM.MCCTRL.mcsel = 2;
173 break;
174 default:
175 return false;
176 }
177
178 return true;
179 }
180
181 #endif