Add script reading bootloader flags
[pub/lufa.git] / LUFA / CodeTemplates / DeviceTemplate / DeviceApplication.c
index 893c733..0f90e55 100644 (file)
@@ -1,13 +1,13 @@
 /*
              LUFA Library
-     Copyright (C) Dean Camera, 2013.
+     Copyright (C) Dean Camera, 2021.
 
   dean [at] fourwalledcubicle [dot] com
            www.lufa-lib.org
 */
 
 /*
-  Copyright 2013  Dean Camera (dean [at] fourwalledcubicle [dot] com)
+  Copyright 2021  Dean Camera (dean [at] fourwalledcubicle [dot] com)
 
   Permission to use, copy, modify, distribute, and sell this
   software and its documentation for any purpose is hereby granted
@@ -55,15 +55,30 @@ int main(void)
 /** Configures the board hardware and chip peripherals for the demo's functionality. */
 void SetupHardware(void)
 {
-       /* Disable watchdog if enabled by bootloader/fuses */
-       MCUSR &= ~(1 << WDRF);
-       wdt_disable();
-
-       /* Disable clock division */
-       clock_prescale_set(clock_div_1);
-
-       /* Hardware Initialization */
-       USB_Init(USB_MODE_Device, USB_DEVICE_OPT_FULLSPEED | USB_OPT_AUTO_PLL);
+       #if (ARCH == ARCH_AVR8)
+               /* Disable watchdog if enabled by bootloader/fuses */
+               MCUSR &= ~(1 << WDRF);
+               wdt_disable();
+
+               /* Disable clock division */
+               clock_prescale_set(clock_div_1);
+
+               /* Hardware Initialization */
+               USB_Init(USB_MODE_Device, USB_DEVICE_OPT_FULLSPEED | USB_OPT_AUTO_PLL);
+       #elif (ARCH == ARCH_XMEGA)
+               /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */
+               XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
+               XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);
+
+               /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */
+               XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
+               XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);
+
+               PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
+
+               /* Hardware Initialization */
+               USB_Init(USB_OPT_RC32MCLKSRC | USB_OPT_BUSEVENT_PRIHIGH);
+       #endif
 }
 
 /** Event handler for the library USB Connection event. */