4d3f4c1bab3abf0cc9ee8cd8e3277b1a06f46de4
   3      Copyright (C) Dean Camera, 2010. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2010  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 
  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" 
  39 /** Static buffer to hold the last received report from the host, so that it can be echoed back in the next sent report */ 
  40 static uint8_t LastReceived
[GENERIC_REPORT_SIZE
]; 
  43 /** Main program entry point. This routine configures the hardware required by the application, then 
  44  *  enters a loop to run the application tasks in sequence. 
  50         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
  59 /** Configures the board hardware and chip peripherals for the demo's functionality. */ 
  60 void SetupHardware(void) 
  62         /* Disable watchdog if enabled by bootloader/fuses */ 
  63         MCUSR 
&= ~(1 << WDRF
); 
  66         /* Disable clock division */ 
  67         clock_prescale_set(clock_div_1
); 
  69         /* Hardware Initialization */ 
  74 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and 
  75  *  starts the library USB task to begin the enumeration and USB management process. 
  77 void EVENT_USB_Device_Connect(void) 
  79         /* Indicate USB enumerating */ 
  80         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
  83 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via 
  84  *  the status LEDs and stops the USB management task. 
  86 void EVENT_USB_Device_Disconnect(void) 
  88         /* Indicate USB not ready */ 
  89         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
  92 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration 
  93  *  of the USB device after enumeration, and configures the generic HID device endpoints. 
  95 void EVENT_USB_Device_ConfigurationChanged(void) 
  97         /* Indicate USB connected and ready */ 
  98         LEDs_SetAllLEDs(LEDMASK_USB_READY
); 
 100         /* Setup Generic IN Report Endpoint */ 
 101         if (!(Endpoint_ConfigureEndpoint(GENERIC_IN_EPNUM
, EP_TYPE_INTERRUPT
, 
 102                                              ENDPOINT_DIR_IN
, GENERIC_EPSIZE
, 
 103                                          ENDPOINT_BANK_SINGLE
))) 
 105                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 108         /* Setup Generic OUT Report Endpoint */ 
 109         if (!(Endpoint_ConfigureEndpoint(GENERIC_OUT_EPNUM
, EP_TYPE_INTERRUPT
, 
 110                                              ENDPOINT_DIR_OUT
, GENERIC_EPSIZE
, 
 111                                          ENDPOINT_BANK_SINGLE
))) 
 113                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 117 /** Event handler for the USB_UnhandledControlRequest event. This is used to catch standard and class specific 
 118  *  control requests that are not handled internally by the USB library (including the HID commands, which are 
 119  *  all issued via the control endpoint), so that they can be handled appropriately for the application. 
 121 void EVENT_USB_Device_UnhandledControlRequest(void) 
 123         /* Handle HID Class specific requests */ 
 124         switch (USB_ControlRequest
.bRequest
) 
 127                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 129                                 uint8_t GenericData
[GENERIC_REPORT_SIZE
]; 
 131                                 Endpoint_ClearSETUP(); 
 133                                 CreateGenericHIDReport(GenericData
); 
 135                                 /* Write the report data to the control endpoint */ 
 136                                 Endpoint_Write_Control_Stream_LE(&GenericData
, sizeof(GenericData
)); 
 138                                 /* Finalize the stream transfer to send the last packet or clear the host abort */ 
 144                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_HOSTTODEVICE 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 146                                 uint8_t GenericData
[GENERIC_REPORT_SIZE
]; 
 148                                 Endpoint_ClearSETUP(); 
 150                                 /* Wait until the generic report has been sent by the host */ 
 151                                 while (!(Endpoint_IsOUTReceived())) 
 153                                         if (USB_DeviceState 
== DEVICE_STATE_Unattached
) 
 157                                 Endpoint_Read_Control_Stream_LE(&GenericData
, sizeof(GenericData
)); 
 159                                 ProcessGenericHIDReport(GenericData
); 
 161                                 /* Clear the endpoint data */ 
 164                                 /* Wait until the host is ready to receive the request confirmation */ 
 165                                 while (!(Endpoint_IsINReady())) 
 167                                         if (USB_DeviceState 
== DEVICE_STATE_Unattached
) 
 171                                 /* Handshake the request by sending an empty IN packet */ 
 179 /** Function to process the lest received report from the host. 
 181  *  \param[in] DataArray  Pointer to a buffer where the last report data is stored 
 183 void ProcessGenericHIDReport(uint8_t* DataArray
) 
 186                 This is where you need to process the reports being sent from the host to the device. 
 187                 DataArray is an array holding the last report from the host. This function is called 
 188                 each time the host has sent a report to the device. 
 191         for (uint8_t i 
= 0; i 
< GENERIC_REPORT_SIZE
; i
++) 
 192           LastReceived
[i
] = DataArray
[i
]; 
 195 /** Function to create the next report to send back to the host at the next reporting interval. 
 197  *  \param[out] DataArray  Pointer to a buffer where the next report data should be stored 
 199 void CreateGenericHIDReport(uint8_t* DataArray
) 
 202                 This is where you need to create reports to be sent to the host from the device. This 
 203                 function is called each time the host is ready to accept a new report. DataArray is  
 204                 an array to hold the report to the host. 
 207         for (uint8_t i 
= 0; i 
< GENERIC_REPORT_SIZE
; i
++) 
 208           DataArray
[i
] = LastReceived
[i
]; 
 213         /* Device must be connected and configured for the task to run */ 
 214         if (USB_DeviceState 
!= DEVICE_STATE_Configured
) 
 217         Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM
); 
 219         /* Check to see if a packet has been sent from the host */ 
 220         if (Endpoint_IsOUTReceived()) 
 222                 /* Check to see if the packet contains data */ 
 223                 if (Endpoint_IsReadWriteAllowed()) 
 225                         /* Create a temporary buffer to hold the read in report from the host */ 
 226                         uint8_t GenericData
[GENERIC_REPORT_SIZE
]; 
 228                         /* Read Generic Report Data */ 
 229                         Endpoint_Read_Stream_LE(&GenericData
, sizeof(GenericData
)); 
 231                         /* Process Generic Report Data */ 
 232                         ProcessGenericHIDReport(GenericData
); 
 235                 /* Finalize the stream transfer to send the last packet */ 
 239         Endpoint_SelectEndpoint(GENERIC_IN_EPNUM
); 
 241         /* Check to see if the host is ready to accept another packet */ 
 242         if (Endpoint_IsINReady()) 
 244                 /* Create a temporary buffer to hold the report to send to the host */ 
 245                 uint8_t GenericData
[GENERIC_REPORT_SIZE
]; 
 247                 /* Create Generic Report Data */ 
 248                 CreateGenericHIDReport(GenericData
); 
 250                 /* Write Generic Report Data */ 
 251                 Endpoint_Write_Stream_LE(&GenericData
, sizeof(GenericData
)); 
 253                 /* Finalize the stream transfer to send the last packet */