3      Copyright (C) Dean Camera, 2016. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2016  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 disclaims 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 
  33  *  Main source file for the Bulk Vendor demo. This file contains the main tasks of the demo and 
  34  *  is responsible for the initial application hardware configuration. 
  37 #define  INCLUDE_FROM_BULKVENDOR_C 
  38 #include "BulkVendor.h" 
  41 /** Main program entry point. This routine configures the hardware required by the application, then 
  42  *  enters a loop to run the application tasks in sequence. 
  48         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
  49         GlobalInterruptEnable(); 
  55                 uint8_t ReceivedData
[VENDOR_IO_EPSIZE
]; 
  56                 memset(ReceivedData
, 0x00, sizeof(ReceivedData
)); 
  58                 Endpoint_SelectEndpoint(VENDOR_OUT_EPADDR
); 
  59                 if (Endpoint_IsOUTReceived()) 
  61                         Endpoint_Read_Stream_LE(ReceivedData
, VENDOR_IO_EPSIZE
, NULL
); 
  64                         Endpoint_SelectEndpoint(VENDOR_IN_EPADDR
); 
  65                         Endpoint_Write_Stream_LE(ReceivedData
, VENDOR_IO_EPSIZE
, NULL
); 
  71 /** Configures the board hardware and chip peripherals for the demo's functionality. */ 
  72 void SetupHardware(void) 
  74 #if (ARCH == ARCH_AVR8) 
  75         /* Disable watchdog if enabled by bootloader/fuses */ 
  76         MCUSR 
&= ~(1 << WDRF
); 
  79         /* Disable clock division */ 
  80         clock_prescale_set(clock_div_1
); 
  81 #elif (ARCH == ARCH_XMEGA) 
  82         /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */ 
  83         XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ
, 2000000, F_CPU
); 
  84         XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL
); 
  86         /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */ 
  87         XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ
); 
  88         XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ
, DFLL_REF_INT_USBSOF
, F_USB
); 
  90         PMIC
.CTRL 
= PMIC_LOLVLEN_bm 
| PMIC_MEDLVLEN_bm 
| PMIC_HILVLEN_bm
; 
  93         /* Hardware Initialization */ 
  98 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs. */ 
  99 void EVENT_USB_Device_Connect(void) 
 101         /* Indicate USB enumerating */ 
 102         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
 105 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via 
 108 void EVENT_USB_Device_Disconnect(void) 
 110         /* Indicate USB not ready */ 
 111         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
 114 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration 
 115  *  of the USB device after enumeration - the device endpoints are configured. 
 117 void EVENT_USB_Device_ConfigurationChanged(void) 
 119         bool ConfigSuccess 
= true; 
 121         /* Setup Vendor Data Endpoints */ 
 122         ConfigSuccess 
&= Endpoint_ConfigureEndpoint(VENDOR_IN_EPADDR
,  EP_TYPE_BULK
, VENDOR_IO_EPSIZE
, 1); 
 123         ConfigSuccess 
&= Endpoint_ConfigureEndpoint(VENDOR_OUT_EPADDR
, EP_TYPE_BULK
, VENDOR_IO_EPSIZE
, 1); 
 125         /* Indicate endpoint configuration success or failure */ 
 126         LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY 
: LEDMASK_USB_ERROR
); 
 129 /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to 
 130  *  the device from the USB host before passing along unhandled control requests to the library for processing 
 133 void EVENT_USB_Device_ControlRequest(void) 
 135         // Process vendor specific control requests here