3 Copyright (C) Dean Camera, 2011.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
31 #ifndef _CLOCK_MANAGEMENT_H_
32 #define _CLOCK_MANAGEMENT_H_
39 #include <LUFA/Common/Common.h>
42 enum Extern_OSC_ClockStartup_t
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,
53 enum Extern_OSC_ClockMode_t
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,
64 CLOCK_SRC_SLOW_CLK
= 0,
71 /* Inline Functions: */
72 static inline bool AVR32CLK_StartExternalOscillator(const uint8_t Channel
,
74 const uint8_t Startup
)
76 AVR32_PM
.OSCCTRL0
.startup
= Startup
;
77 AVR32_PM
.OSCCTRL0
.mode
= Type
;
78 AVR32_PM
.mcctrl
|= (1 << (AVR32_PM_MCCTRL_OSC0EN_OFFSET
+ Channel
));
80 while (!(AVR32_PM
.poscsr
& (1 << (AVR32_PM_POSCSR_OSC0RDY_OFFSET
+ Channel
))));
84 static inline void AVR32CLK_StopExternalOscillator(const uint8_t Channel
)
86 AVR32_PM
.mcctrl
&= ~(1 << (AVR32_PM_MCCTRL_OSC0EN_OFFSET
+ Channel
));
89 static inline bool AVR32CLK_StartPLL(const uint8_t Channel
,
91 const uint32_t SourceFreq
,
92 const uint32_t Frequency
)
97 AVR32_PM
.PLL
[Channel
].pllosc
= 0;
100 AVR32_PM
.PLL
[Channel
].pllosc
= 1;
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;
110 while (!(AVR32_PM
.poscsr
& (1 << (AVR32_PM_POSCSR_LOCK0_OFFSET
+ Channel
))));
114 static inline void AVR32CLK_StopPLL(const uint8_t Channel
)
116 AVR32_PM
.PLL
[Channel
].pllen
= false;
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
)
127 AVR32_PM
.GCCTRL
[Channel
].pllsel
= 0;
128 AVR32_PM
.GCCTRL
[Channel
].oscsel
= 0;
131 AVR32_PM
.GCCTRL
[Channel
].pllsel
= 0;
132 AVR32_PM
.GCCTRL
[Channel
].oscsel
= 1;
135 AVR32_PM
.GCCTRL
[Channel
].pllsel
= 1;
136 AVR32_PM
.GCCTRL
[Channel
].oscsel
= 0;
139 AVR32_PM
.GCCTRL
[Channel
].pllsel
= 1;
140 AVR32_PM
.GCCTRL
[Channel
].oscsel
= 1;
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;
153 static inline void AVR32CLK_StopGenericClock(const uint8_t Channel
)
155 AVR32_PM
.GCCTRL
[Channel
].cen
= false;
158 static inline bool AVR32CLK_SetCPUClockSource(const uint8_t Source
,
159 const uint32_t SourceFreq
)
161 AVR32_FLASHC
.FCR
.fws
= (SourceFreq
> 30000000) ?
true : false;
165 case CLOCK_SRC_SLOW_CLK
:
166 AVR32_PM
.MCCTRL
.mcsel
= 0;
169 AVR32_PM
.MCCTRL
.mcsel
= 1;
172 AVR32_PM
.MCCTRL
.mcsel
= 2;