+/*\r
+ LUFA Library\r
+ Copyright (C) Dean Camera, 2011.\r
+\r
+ dean [at] fourwalledcubicle [dot] com\r
+ www.lufa-lib.org\r
+*/\r
+\r
+/*\r
+ Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
+\r
+ Permission to use, copy, modify, distribute, and sell this\r
+ software and its documentation for any purpose is hereby granted\r
+ without fee, provided that the above copyright notice appear in\r
+ all copies and that both that the copyright notice and this\r
+ permission notice and warranty disclaimer appear in supporting\r
+ documentation, and that the name of the author not be used in\r
+ advertising or publicity pertaining to distribution of the\r
+ software without specific, written prior permission.\r
+\r
+ The author disclaim all warranties with regard to this\r
+ software, including all implied warranties of merchantability\r
+ and fitness. In no event shall the author be liable for any\r
+ special, indirect or consequential damages or any damages\r
+ whatsoever resulting from loss of use, data or profits, whether\r
+ in an action of contract, negligence or other tortious action,\r
+ arising out of or in connection with the use or performance of\r
+ this software.\r
+*/\r
+\r
+#ifndef _CLOCK_MANAGEMENT_H_\r
+#define _CLOCK_MANAGEMENT_H_\r
+\r
+ /* Includes: */\r
+ #include <avr32/io.h>\r
+ #include <stdbool.h>\r
+ #include <stdint.h>\r
+\r
+ #include <LUFA/Common/Common.h>\r
+\r
+ /* Macros: */\r
+ enum Extern_OSC_ClockStartup_t\r
+ {\r
+ EXOSC_START_0CLK = 0,\r
+ EXOSC_START_64CLK = 1,\r
+ EXOSC_START_128CLK = 2,\r
+ EXOSC_START_2048CLK = 3,\r
+ EXOSC_START_4096CLK = 4,\r
+ EXOSC_START_8192CLK = 5,\r
+ EXOSC_START_16384CLK = 6,\r
+ };\r
+ \r
+ enum Extern_OSC_ClockMode_t\r
+ {\r
+ EXOSC_MODE_CLOCK = 0,\r
+ EXOSC_MODE_900KHZ_MAX = 1,\r
+ EXOSC_MODE_3MHZ_MAX = 2,\r
+ EXOSC_MODE_8MHZ_MAX = 3,\r
+ EXOSC_MODE_8MHZ_OR_MORE = 4, \r
+ };\r
+ \r
+ enum\r
+ {\r
+ CLOCK_SRC_SLOW_CLK = 0,\r
+ CLOCK_SRC_OSC0 = 1,\r
+ CLOCK_SRC_OSC1 = 2,\r
+ CLOCK_SRC_PLL0 = 3,\r
+ CLOCK_SRC_PLL1 = 4,\r
+ };\r
+\r
+ /* Inline Functions: */\r
+ static inline bool AVR32CLK_StartExternalOscillator(const uint8_t Channel,\r
+ const uint8_t Type,\r
+ const uint8_t Startup)\r
+ {\r
+ AVR32_PM.OSCCTRL0.startup = Startup;\r
+ AVR32_PM.OSCCTRL0.mode = Type;\r
+ AVR32_PM.mcctrl |= (1 << (AVR32_PM_MCCTRL_OSC0EN_OFFSET + Channel));\r
+\r
+ while (!(AVR32_PM.poscsr & (1 << (AVR32_PM_POSCSR_OSC0RDY_OFFSET + Channel))));\r
+ return true;\r
+ }\r
+\r
+ static inline void AVR32CLK_StopExternalOscillator(const uint8_t Channel)\r
+ {\r
+ AVR32_PM.mcctrl &= ~(1 << (AVR32_PM_MCCTRL_OSC0EN_OFFSET + Channel));\r
+ }\r
+\r
+ static inline bool AVR32CLK_StartPLL(const uint8_t Channel,\r
+ const uint8_t Source,\r
+ const uint32_t SourceFreq,\r
+ const uint32_t Frequency)\r
+ {\r
+ switch (Source)\r
+ {\r
+ case CLOCK_SRC_OSC0:\r
+ AVR32_PM.PLL[Channel].pllosc = 0;\r
+ break;\r
+ case CLOCK_SRC_OSC1:\r
+ AVR32_PM.PLL[Channel].pllosc = 1;\r
+ break;\r
+ default:\r
+ return false;\r
+ }\r
+\r
+ AVR32_PM.PLL[Channel].pllmul = (Frequency / SourceFreq) ? (((Frequency / SourceFreq) - 1) / 2) : 0;\r
+ AVR32_PM.PLL[Channel].plldiv = 0;\r
+ AVR32_PM.PLL[Channel].pllen = true;\r
+\r
+ while (!(AVR32_PM.poscsr & (1 << (AVR32_PM_POSCSR_LOCK0_OFFSET + Channel))));\r
+ return true;\r
+ }\r
+\r
+ static inline void AVR32CLK_StopPLL(const uint8_t Channel)\r
+ {\r
+ AVR32_PM.PLL[Channel].pllen = false;\r
+ }\r
+ \r
+ static inline bool AVR32CLK_StartGenericClock(const uint8_t Channel,\r
+ const uint8_t Source,\r
+ const uint32_t SourceFreq,\r
+ const uint32_t Frequency)\r
+ {\r
+ switch (Source)\r
+ {\r
+ case CLOCK_SRC_OSC0:\r
+ AVR32_PM.GCCTRL[Channel].pllsel = 0;\r
+ AVR32_PM.GCCTRL[Channel].oscsel = 0;\r
+ break;\r
+ case CLOCK_SRC_OSC1:\r
+ AVR32_PM.GCCTRL[Channel].pllsel = 0;\r
+ AVR32_PM.GCCTRL[Channel].oscsel = 1;\r
+ break;\r
+ case CLOCK_SRC_PLL0:\r
+ AVR32_PM.GCCTRL[Channel].pllsel = 1;\r
+ AVR32_PM.GCCTRL[Channel].oscsel = 0;\r
+ break;\r
+ case CLOCK_SRC_PLL1:\r
+ AVR32_PM.GCCTRL[Channel].pllsel = 1;\r
+ AVR32_PM.GCCTRL[Channel].oscsel = 1;\r
+ break;\r
+ default:\r
+ return false;\r
+ }\r
+\r
+ AVR32_PM.GCCTRL[Channel].diven = (SourceFreq > Frequency) ? true : false;\r
+ AVR32_PM.GCCTRL[Channel].div = (((SourceFreq / Frequency) - 1) / 2);\r
+ AVR32_PM.GCCTRL[Channel].cen = true;\r
+ \r
+ return true;\r
+ }\r
+ \r
+ static inline void AVR32CLK_StopGenericClock(const uint8_t Channel)\r
+ {\r
+ AVR32_PM.GCCTRL[Channel].cen = false;\r
+ }\r
+\r
+ static inline bool AVR32CLK_SetCPUClockSource(const uint8_t Source,\r
+ const uint32_t SourceFreq)\r
+ {\r
+ AVR32_FLASHC.FCR.fws = (SourceFreq > 30000000) ? true : false;\r
+\r
+ switch (Source)\r
+ {\r
+ case CLOCK_SRC_SLOW_CLK:\r
+ AVR32_PM.MCCTRL.mcsel = 0;\r
+ break;\r
+ case CLOCK_SRC_OSC0:\r
+ AVR32_PM.MCCTRL.mcsel = 1;\r
+ break;\r
+ case CLOCK_SRC_PLL0:\r
+ AVR32_PM.MCCTRL.mcsel = 2;\r
+ break;\r
+ default:\r
+ return false;\r
+ }\r
+ \r
+ return true;\r
+ }\r
+\r
+#endif\r