3      Copyright (C) Dean Camera, 2018. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2018  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  11   Copyright 2010  Matthias Hullin (lufa [at] matthias [dot] hullin [dot] net) 
  13   Permission to use, copy, modify, distribute, and sell this 
  14   software and its documentation for any purpose is hereby granted 
  15   without fee, provided that the above copyright notice appear in 
  16   all copies and that both that the copyright notice and this 
  17   permission notice and warranty disclaimer appear in supporting 
  18   documentation, and that the name of the author not be used in 
  19   advertising or publicity pertaining to distribution of the 
  20   software without specific, written prior permission. 
  22   The author disclaims all warranties with regard to this 
  23   software, including all implied warranties of merchantability 
  24   and fitness.  In no event shall the author be liable for any 
  25   special, indirect or consequential damages or any damages 
  26   whatsoever resulting from loss of use, data or profits, whether 
  27   in an action of contract, negligence or other tortious action, 
  28   arising out of or in connection with the use or performance of 
  34  *  Main source file for the MassStorageKeyboard demo. This file contains the main tasks of 
  35  *  the demo and is responsible for the initial application hardware configuration. 
  38 #include "MassStorageKeyboard.h" 
  40 /** LUFA Mass Storage Class driver interface configuration and state information. This structure is 
  41  *  passed to all Mass Storage Class driver functions, so that multiple instances of the same class 
  42  *  within a device can be differentiated from one another. 
  44 USB_ClassInfo_MS_Device_t Disk_MS_Interface 
= 
  48                                 .InterfaceNumber           
= INTERFACE_ID_MassStorage
, 
  51                                                 .Address           
= MASS_STORAGE_IN_EPADDR
, 
  52                                                 .Size              
= MASS_STORAGE_IO_EPSIZE
, 
  57                                                 .Address           
= MASS_STORAGE_OUT_EPADDR
, 
  58                                                 .Size              
= MASS_STORAGE_IO_EPSIZE
, 
  61                                 .TotalLUNs                 
= TOTAL_LUNS
, 
  65 /** Buffer to hold the previously generated Keyboard HID report, for comparison purposes inside the HID class driver. */ 
  66 static uint8_t PrevKeyboardHIDReportBuffer
[sizeof(USB_KeyboardReport_Data_t
)]; 
  68 /** LUFA HID Class driver interface configuration and state information. This structure is 
  69  *  passed to all HID Class driver functions, so that multiple instances of the same class 
  70  *  within a device can be differentiated from one another. 
  72 USB_ClassInfo_HID_Device_t Keyboard_HID_Interface 
= 
  76                                 .InterfaceNumber              
= INTERFACE_ID_Keyboard
, 
  79                                                 .Address              
= KEYBOARD_EPADDR
, 
  80                                                 .Size                 
= KEYBOARD_EPSIZE
, 
  83                                 .PrevReportINBuffer           
= PrevKeyboardHIDReportBuffer
, 
  84                                 .PrevReportINBufferSize       
= sizeof(PrevKeyboardHIDReportBuffer
), 
  88 /** Main program entry point. This routine contains the overall program flow, including initial 
  89  *  setup of all components and the main program loop. 
  95         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
  96         GlobalInterruptEnable(); 
 100                 MS_Device_USBTask(&Disk_MS_Interface
); 
 101                 HID_Device_USBTask(&Keyboard_HID_Interface
); 
 106 /** Configures the board hardware and chip peripherals for the demo's functionality. */ 
 107 void SetupHardware(void) 
 109 #if (ARCH == ARCH_AVR8) 
 110         /* Disable watchdog if enabled by bootloader/fuses */ 
 111         MCUSR 
&= ~(1 << WDRF
); 
 114         /* Disable clock division */ 
 115         clock_prescale_set(clock_div_1
); 
 116 #elif (ARCH == ARCH_XMEGA) 
 117         /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */ 
 118         XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ
, 2000000, F_CPU
); 
 119         XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL
); 
 121         /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */ 
 122         XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ
); 
 123         XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ
, DFLL_REF_INT_USBSOF
, F_USB
); 
 125         PMIC
.CTRL 
= PMIC_LOLVLEN_bm 
| PMIC_MEDLVLEN_bm 
| PMIC_HILVLEN_bm
; 
 128         /* Hardware Initialization */ 
 135         /* Check if the Dataflash is working, abort if not */ 
 136         if (!(DataflashManager_CheckDataflashOperation())) 
 138                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 142         /* Clear Dataflash sector protections, if enabled */ 
 143         DataflashManager_ResetDataflashProtections(); 
 146 /** Event handler for the library USB Connection event. */ 
 147 void EVENT_USB_Device_Connect(void) 
 149         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
 152 /** Event handler for the library USB Disconnection event. */ 
 153 void EVENT_USB_Device_Disconnect(void) 
 155         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
 158 /** Event handler for the library USB Configuration Changed event. */ 
 159 void EVENT_USB_Device_ConfigurationChanged(void) 
 161         bool ConfigSuccess 
= true; 
 163         ConfigSuccess 
&= HID_Device_ConfigureEndpoints(&Keyboard_HID_Interface
); 
 164         ConfigSuccess 
&= MS_Device_ConfigureEndpoints(&Disk_MS_Interface
); 
 166         USB_Device_EnableSOFEvents(); 
 168         LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY 
: LEDMASK_USB_ERROR
); 
 171 /** Event handler for the library USB Control Request reception event. */ 
 172 void EVENT_USB_Device_ControlRequest(void) 
 174         MS_Device_ProcessControlRequest(&Disk_MS_Interface
); 
 175         HID_Device_ProcessControlRequest(&Keyboard_HID_Interface
); 
 178 /** Mass Storage class driver callback function the reception of SCSI commands from the host, which must be processed. 
 180  *  \param[in] MSInterfaceInfo  Pointer to the Mass Storage class interface configuration structure being referenced 
 182 bool CALLBACK_MS_Device_SCSICommandReceived(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
) 
 186         LEDs_SetAllLEDs(LEDMASK_USB_BUSY
); 
 187         CommandSuccess 
= SCSI_DecodeSCSICommand(MSInterfaceInfo
); 
 188         LEDs_SetAllLEDs(LEDMASK_USB_READY
); 
 190         return CommandSuccess
; 
 193 /** Event handler for the USB device Start Of Frame event. */ 
 194 void EVENT_USB_Device_StartOfFrame(void) 
 196     HID_Device_MillisecondElapsed(&Keyboard_HID_Interface
); 
 199 /** HID class driver callback function for the creation of HID reports to the host. 
 201  *  \param[in]     HIDInterfaceInfo  Pointer to the HID class interface configuration structure being referenced 
 202  *  \param[in,out] ReportID    Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID 
 203  *  \param[in]     ReportType  Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature 
 204  *  \param[out]    ReportData  Pointer to a buffer where the created report should be stored 
 205  *  \param[out]    ReportSize  Number of bytes written in the report (or zero if no report is to be sent) 
 207  *  \return Boolean \c true to force the sending of the report, \c false to let the library determine if it needs to be sent 
 209 bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t
* const HIDInterfaceInfo
, 
 210                                          uint8_t* const ReportID
, 
 211                                          const uint8_t ReportType
, 
 213                                          uint16_t* const ReportSize
) 
 215         USB_KeyboardReport_Data_t
* KeyboardReport 
= (USB_KeyboardReport_Data_t
*)ReportData
; 
 217         uint8_t JoyStatus_LCL    
= Joystick_GetStatus(); 
 218         uint8_t ButtonStatus_LCL 
= Buttons_GetStatus(); 
 220         KeyboardReport
->Modifier 
= HID_KEYBOARD_MODIFIER_LEFTSHIFT
; 
 222         if (JoyStatus_LCL 
& JOY_UP
) 
 223           KeyboardReport
->KeyCode
[0] = HID_KEYBOARD_SC_A
; 
 224         else if (JoyStatus_LCL 
& JOY_DOWN
) 
 225           KeyboardReport
->KeyCode
[0] = HID_KEYBOARD_SC_B
; 
 227         if (JoyStatus_LCL 
& JOY_LEFT
) 
 228           KeyboardReport
->KeyCode
[0] = HID_KEYBOARD_SC_C
; 
 229         else if (JoyStatus_LCL 
& JOY_RIGHT
) 
 230           KeyboardReport
->KeyCode
[0] = HID_KEYBOARD_SC_D
; 
 232         if (JoyStatus_LCL 
& JOY_PRESS
) 
 233           KeyboardReport
->KeyCode
[0] = HID_KEYBOARD_SC_E
; 
 235         if (ButtonStatus_LCL 
& BUTTONS_BUTTON1
) 
 236           KeyboardReport
->KeyCode
[0] = HID_KEYBOARD_SC_F
; 
 238         *ReportSize 
= sizeof(USB_KeyboardReport_Data_t
); 
 242 /** HID class driver callback function for the processing of HID reports from the host. 
 244  *  \param[in] HIDInterfaceInfo  Pointer to the HID class interface configuration structure being referenced 
 245  *  \param[in] ReportID    Report ID of the received report from the host 
 246  *  \param[in] ReportType  The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature 
 247  *  \param[in] ReportData  Pointer to a buffer where the received report has been stored 
 248  *  \param[in] ReportSize  Size in bytes of the received HID report 
 250 void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t
* const HIDInterfaceInfo
, 
 251                                           const uint8_t ReportID
, 
 252                                           const uint8_t ReportType
, 
 253                                           const void* ReportData
, 
 254                                           const uint16_t ReportSize
) 
 256         uint8_t  LEDMask   
= LEDS_NO_LEDS
; 
 257         uint8_t* LEDReport 
= (uint8_t*)ReportData
; 
 259         if (*LEDReport 
& HID_KEYBOARD_LED_NUMLOCK
) 
 260           LEDMask 
|= LEDS_LED1
; 
 262         if (*LEDReport 
& HID_KEYBOARD_LED_CAPSLOCK
) 
 263           LEDMask 
|= LEDS_LED3
; 
 265         if (*LEDReport 
& HID_KEYBOARD_LED_SCROLLLOCK
) 
 266           LEDMask 
|= LEDS_LED4
; 
 268         LEDs_SetAllLEDs(LEDMask
);