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 Keyboard demo. This file contains the main tasks of 
  34  *  the demo and is responsible for the initial application hardware configuration. 
  39 /** Buffer to hold the previously generated Keyboard HID report, for comparison purposes inside the HID class driver. */ 
  40 static uint8_t PrevKeyboardHIDReportBuffer
[sizeof(USB_KeyboardReport_Data_t
)]; 
  42 /** LUFA HID Class driver interface configuration and state information. This structure is 
  43  *  passed to all HID Class driver functions, so that multiple instances of the same class 
  44  *  within a device can be differentiated from one another. 
  46 USB_ClassInfo_HID_Device_t Keyboard_HID_Interface 
= 
  50                                 .InterfaceNumber              
= INTERFACE_ID_Keyboard
, 
  53                                                 .Address              
= KEYBOARD_EPADDR
, 
  54                                                 .Size                 
= KEYBOARD_EPSIZE
, 
  57                                 .PrevReportINBuffer           
= PrevKeyboardHIDReportBuffer
, 
  58                                 .PrevReportINBufferSize       
= sizeof(PrevKeyboardHIDReportBuffer
), 
  63 /** Main program entry point. This routine contains the overall program flow, including initial 
  64  *  setup of all components and the main program loop. 
  70         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
  71         GlobalInterruptEnable(); 
  75                 HID_Device_USBTask(&Keyboard_HID_Interface
); 
  80 /** Configures the board hardware and chip peripherals for the demo's functionality. */ 
  83 #if (ARCH == ARCH_AVR8) 
  84         /* Disable watchdog if enabled by bootloader/fuses */ 
  85         MCUSR 
&= ~(1 << WDRF
); 
  88         /* Disable clock division */ 
  89         clock_prescale_set(clock_div_1
); 
  90 #elif (ARCH == ARCH_XMEGA) 
  91         /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */ 
  92         XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ
, 2000000, F_CPU
); 
  93         XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL
); 
  95         /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */ 
  96         XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ
); 
  97         XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ
, DFLL_REF_INT_USBSOF
, F_USB
); 
  99         PMIC
.CTRL 
= PMIC_LOLVLEN_bm 
| PMIC_MEDLVLEN_bm 
| PMIC_HILVLEN_bm
; 
 102         /* Hardware Initialization */ 
 109 /** Event handler for the library USB Connection event. */ 
 110 void EVENT_USB_Device_Connect(void) 
 112         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
 115 /** Event handler for the library USB Disconnection event. */ 
 116 void EVENT_USB_Device_Disconnect(void) 
 118         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
 121 /** Event handler for the library USB Configuration Changed event. */ 
 122 void EVENT_USB_Device_ConfigurationChanged(void) 
 124         bool ConfigSuccess 
= true; 
 126         ConfigSuccess 
&= HID_Device_ConfigureEndpoints(&Keyboard_HID_Interface
); 
 128         USB_Device_EnableSOFEvents(); 
 130         LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY 
: LEDMASK_USB_ERROR
); 
 133 /** Event handler for the library USB Control Request reception event. */ 
 134 void EVENT_USB_Device_ControlRequest(void) 
 136         HID_Device_ProcessControlRequest(&Keyboard_HID_Interface
); 
 139 /** Event handler for the USB device Start Of Frame event. */ 
 140 void EVENT_USB_Device_StartOfFrame(void) 
 142         HID_Device_MillisecondElapsed(&Keyboard_HID_Interface
); 
 145 /** HID class driver callback function for the creation of HID reports to the host. 
 147  *  \param[in]     HIDInterfaceInfo  Pointer to the HID class interface configuration structure being referenced 
 148  *  \param[in,out] ReportID    Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID 
 149  *  \param[in]     ReportType  Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature 
 150  *  \param[out]    ReportData  Pointer to a buffer where the created report should be stored 
 151  *  \param[out]    ReportSize  Number of bytes written in the report (or zero if no report is to be sent) 
 153  *  \return Boolean \c true to force the sending of the report, \c false to let the library determine if it needs to be sent 
 155 bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t
* const HIDInterfaceInfo
, 
 156                                          uint8_t* const ReportID
, 
 157                                          const uint8_t ReportType
, 
 159                                          uint16_t* const ReportSize
) 
 161         USB_KeyboardReport_Data_t
* KeyboardReport 
= (USB_KeyboardReport_Data_t
*)ReportData
; 
 163         uint8_t JoyStatus_LCL    
= Joystick_GetStatus(); 
 164         uint8_t ButtonStatus_LCL 
= Buttons_GetStatus(); 
 166         uint8_t UsedKeyCodes 
= 0; 
 168         if (JoyStatus_LCL 
& JOY_UP
) 
 169           KeyboardReport
->KeyCode
[UsedKeyCodes
++] = HID_KEYBOARD_SC_A
; 
 170         else if (JoyStatus_LCL 
& JOY_DOWN
) 
 171           KeyboardReport
->KeyCode
[UsedKeyCodes
++] = HID_KEYBOARD_SC_B
; 
 173         if (JoyStatus_LCL 
& JOY_LEFT
) 
 174           KeyboardReport
->KeyCode
[UsedKeyCodes
++] = HID_KEYBOARD_SC_C
; 
 175         else if (JoyStatus_LCL 
& JOY_RIGHT
) 
 176           KeyboardReport
->KeyCode
[UsedKeyCodes
++] = HID_KEYBOARD_SC_D
; 
 178         if (JoyStatus_LCL 
& JOY_PRESS
) 
 179           KeyboardReport
->KeyCode
[UsedKeyCodes
++] = HID_KEYBOARD_SC_E
; 
 181         if (ButtonStatus_LCL 
& BUTTONS_BUTTON1
) 
 182           KeyboardReport
->KeyCode
[UsedKeyCodes
++] = HID_KEYBOARD_SC_F
; 
 185           KeyboardReport
->Modifier 
= HID_KEYBOARD_MODIFIER_LEFTSHIFT
; 
 187         *ReportSize 
= sizeof(USB_KeyboardReport_Data_t
); 
 191 /** HID class driver callback function for the processing of HID reports from the host. 
 193  *  \param[in] HIDInterfaceInfo  Pointer to the HID class interface configuration structure being referenced 
 194  *  \param[in] ReportID    Report ID of the received report from the host 
 195  *  \param[in] ReportType  The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature 
 196  *  \param[in] ReportData  Pointer to a buffer where the received report has been stored 
 197  *  \param[in] ReportSize  Size in bytes of the received HID report 
 199 void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t
* const HIDInterfaceInfo
, 
 200                                           const uint8_t ReportID
, 
 201                                           const uint8_t ReportType
, 
 202                                           const void* ReportData
, 
 203                                           const uint16_t ReportSize
) 
 205         uint8_t  LEDMask   
= LEDS_NO_LEDS
; 
 206         uint8_t* LEDReport 
= (uint8_t*)ReportData
; 
 208         if (*LEDReport 
& HID_KEYBOARD_LED_NUMLOCK
) 
 209           LEDMask 
|= LEDS_LED1
; 
 211         if (*LEDReport 
& HID_KEYBOARD_LED_CAPSLOCK
) 
 212           LEDMask 
|= LEDS_LED3
; 
 214         if (*LEDReport 
& HID_KEYBOARD_LED_SCROLLLOCK
) 
 215           LEDMask 
|= LEDS_LED4
; 
 217         LEDs_SetAllLEDs(LEDMask
);