3      Copyright (C) Dean Camera, 2014. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2014  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 GenericHID demo. This file contains the main tasks of the demo and 
  34  *  is responsible for the initial application hardware configuration. 
  37 #include "GenericHID.h" 
  40 /** Main program entry point. This routine configures the hardware required by the application, then 
  41  *  enters a loop to run the application tasks in sequence. 
  47         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
  48         GlobalInterruptEnable(); 
  57 /** Configures the board hardware and chip peripherals for the demo's functionality. */ 
  58 void SetupHardware(void) 
  60 #if (ARCH == ARCH_AVR8) 
  61         /* Disable watchdog if enabled by bootloader/fuses */ 
  62         MCUSR 
&= ~(1 << WDRF
); 
  65         /* Disable clock division */ 
  66         clock_prescale_set(clock_div_1
); 
  67 #elif (ARCH == ARCH_XMEGA) 
  68         /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */ 
  69         XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ
, 2000000, F_CPU
); 
  70         XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL
); 
  72         /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */ 
  73         XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ
); 
  74         XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ
, DFLL_REF_INT_USBSOF
, F_USB
); 
  76         PMIC
.CTRL 
= PMIC_LOLVLEN_bm 
| PMIC_MEDLVLEN_bm 
| PMIC_HILVLEN_bm
; 
  79         /* Hardware Initialization */ 
  84 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and 
  85  *  starts the library USB task to begin the enumeration and USB management process. 
  87 void EVENT_USB_Device_Connect(void) 
  89         /* Indicate USB enumerating */ 
  90         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
  93 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via 
  94  *  the status LEDs and stops the USB management task. 
  96 void EVENT_USB_Device_Disconnect(void) 
  98         /* Indicate USB not ready */ 
  99         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
 102 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration 
 103  *  of the USB device after enumeration, and configures the generic HID device endpoints. 
 105 void EVENT_USB_Device_ConfigurationChanged(void) 
 107         bool ConfigSuccess 
= true; 
 109         /* Setup HID Report Endpoints */ 
 110         ConfigSuccess 
&= Endpoint_ConfigureEndpoint(GENERIC_IN_EPADDR
, EP_TYPE_INTERRUPT
, GENERIC_EPSIZE
, 1); 
 111         ConfigSuccess 
&= Endpoint_ConfigureEndpoint(GENERIC_OUT_EPADDR
, EP_TYPE_INTERRUPT
, GENERIC_EPSIZE
, 1); 
 113         /* Indicate endpoint configuration success or failure */ 
 114         LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY 
: LEDMASK_USB_ERROR
); 
 117 /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to 
 118  *  the device from the USB host before passing along unhandled control requests to the library for processing 
 121 void EVENT_USB_Device_ControlRequest(void) 
 123         /* Handle HID Class specific requests */ 
 124         switch (USB_ControlRequest
.bRequest
) 
 126                 case HID_REQ_GetReport
: 
 127                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 129                                 uint8_t GenericData
[GENERIC_REPORT_SIZE
]; 
 130                                 CreateGenericHIDReport(GenericData
); 
 132                                 Endpoint_ClearSETUP(); 
 134                                 /* Write the report data to the control endpoint */ 
 135                                 Endpoint_Write_Control_Stream_LE(&GenericData
, sizeof(GenericData
)); 
 140                 case HID_REQ_SetReport
: 
 141                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_HOSTTODEVICE 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 143                                 uint8_t GenericData
[GENERIC_REPORT_SIZE
]; 
 145                                 Endpoint_ClearSETUP(); 
 147                                 /* Read the report data from the control endpoint */ 
 148                                 Endpoint_Read_Control_Stream_LE(&GenericData
, sizeof(GenericData
)); 
 151                                 ProcessGenericHIDReport(GenericData
); 
 158 /** Function to process the last received report from the host. 
 160  *  \param[in] DataArray  Pointer to a buffer where the last received report has been stored 
 162 void ProcessGenericHIDReport(uint8_t* DataArray
) 
 165                 This is where you need to process reports sent from the host to the device. This 
 166                 function is called each time the host has sent a new report. DataArray is an array 
 167                 holding the report sent from the host. 
 170         uint8_t NewLEDMask 
= LEDS_NO_LEDS
; 
 173           NewLEDMask 
|= LEDS_LED1
; 
 176           NewLEDMask 
|= LEDS_LED2
; 
 179           NewLEDMask 
|= LEDS_LED3
; 
 182           NewLEDMask 
|= LEDS_LED4
; 
 184         LEDs_SetAllLEDs(NewLEDMask
); 
 187 /** Function to create the next report to send back to the host at the next reporting interval. 
 189  *  \param[out] DataArray  Pointer to a buffer where the next report data should be stored 
 191 void CreateGenericHIDReport(uint8_t* DataArray
) 
 194                 This is where you need to create reports to be sent to the host from the device. This 
 195                 function is called each time the host is ready to accept a new report. DataArray is 
 196                 an array to hold the report to the host. 
 199         uint8_t CurrLEDMask 
= LEDs_GetLEDs(); 
 201         DataArray
[0] = ((CurrLEDMask 
& LEDS_LED1
) ? 
1 : 0); 
 202         DataArray
[1] = ((CurrLEDMask 
& LEDS_LED2
) ? 
1 : 0); 
 203         DataArray
[2] = ((CurrLEDMask 
& LEDS_LED3
) ? 
1 : 0); 
 204         DataArray
[3] = ((CurrLEDMask 
& LEDS_LED4
) ? 
1 : 0); 
 209         /* Device must be connected and configured for the task to run */ 
 210         if (USB_DeviceState 
!= DEVICE_STATE_Configured
) 
 213         Endpoint_SelectEndpoint(GENERIC_OUT_EPADDR
); 
 215         /* Check to see if a packet has been sent from the host */ 
 216         if (Endpoint_IsOUTReceived()) 
 218                 /* Check to see if the packet contains data */ 
 219                 if (Endpoint_IsReadWriteAllowed()) 
 221                         /* Create a temporary buffer to hold the read in report from the host */ 
 222                         uint8_t GenericData
[GENERIC_REPORT_SIZE
]; 
 224                         /* Read Generic Report Data */ 
 225                         Endpoint_Read_Stream_LE(&GenericData
, sizeof(GenericData
), NULL
); 
 227                         /* Process Generic Report Data */ 
 228                         ProcessGenericHIDReport(GenericData
); 
 231                 /* Finalize the stream transfer to send the last packet */ 
 235         Endpoint_SelectEndpoint(GENERIC_IN_EPADDR
); 
 237         /* Check to see if the host is ready to accept another packet */ 
 238         if (Endpoint_IsINReady()) 
 240                 /* Create a temporary buffer to hold the report to send to the host */ 
 241                 uint8_t GenericData
[GENERIC_REPORT_SIZE
]; 
 243                 /* Create Generic Report Data */ 
 244                 CreateGenericHIDReport(GenericData
); 
 246                 /* Write Generic Report Data */ 
 247                 Endpoint_Write_Stream_LE(&GenericData
, sizeof(GenericData
), NULL
); 
 249                 /* Finalize the stream transfer to send the last packet */