3      Copyright (C) Dean Camera, 2013. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2013  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 
= 
  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 
= 
  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         /* Disable watchdog if enabled by bootloader/fuses */ 
 110         MCUSR 
&= ~(1 << WDRF
); 
 113         /* Disable clock division */ 
 114         clock_prescale_set(clock_div_1
); 
 116         /* Hardware Initialization */ 
 120         SPI_Init(SPI_SPEED_FCPU_DIV_2 
| SPI_ORDER_MSB_FIRST 
| SPI_SCK_LEAD_FALLING 
| SPI_SAMPLE_TRAILING 
| SPI_MODE_MASTER
); 
 124         /* Check if the Dataflash is working, abort if not */ 
 125         if (!(DataflashManager_CheckDataflashOperation())) 
 127                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 131         /* Clear Dataflash sector protections, if enabled */ 
 132         DataflashManager_ResetDataflashProtections(); 
 135 /** Event handler for the library USB Connection event. */ 
 136 void EVENT_USB_Device_Connect(void) 
 138         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
 141 /** Event handler for the library USB Disconnection event. */ 
 142 void EVENT_USB_Device_Disconnect(void) 
 144         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
 147 /** Event handler for the library USB Configuration Changed event. */ 
 148 void EVENT_USB_Device_ConfigurationChanged(void) 
 150         bool ConfigSuccess 
= true; 
 152         ConfigSuccess 
&= HID_Device_ConfigureEndpoints(&Keyboard_HID_Interface
); 
 153         ConfigSuccess 
&= MS_Device_ConfigureEndpoints(&Disk_MS_Interface
); 
 155         USB_Device_EnableSOFEvents(); 
 157         LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY 
: LEDMASK_USB_ERROR
); 
 160 /** Event handler for the library USB Control Request reception event. */ 
 161 void EVENT_USB_Device_ControlRequest(void) 
 163         MS_Device_ProcessControlRequest(&Disk_MS_Interface
); 
 164         HID_Device_ProcessControlRequest(&Keyboard_HID_Interface
); 
 167 /** Mass Storage class driver callback function the reception of SCSI commands from the host, which must be processed. 
 169  *  \param[in] MSInterfaceInfo  Pointer to the Mass Storage class interface configuration structure being referenced 
 171 bool CALLBACK_MS_Device_SCSICommandReceived(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
) 
 175         LEDs_SetAllLEDs(LEDMASK_USB_BUSY
); 
 176         CommandSuccess 
= SCSI_DecodeSCSICommand(MSInterfaceInfo
); 
 177         LEDs_SetAllLEDs(LEDMASK_USB_READY
); 
 179         return CommandSuccess
; 
 182 /** Event handler for the USB device Start Of Frame event. */ 
 183 void EVENT_USB_Device_StartOfFrame(void) 
 185     HID_Device_MillisecondElapsed(&Keyboard_HID_Interface
); 
 188 /** HID class driver callback function for the creation of HID reports to the host. 
 190  *  \param[in]     HIDInterfaceInfo  Pointer to the HID class interface configuration structure being referenced 
 191  *  \param[in,out] ReportID    Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID 
 192  *  \param[in]     ReportType  Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature 
 193  *  \param[out]    ReportData  Pointer to a buffer where the created report should be stored 
 194  *  \param[out]    ReportSize  Number of bytes written in the report (or zero if no report is to be sent) 
 196  *  \return Boolean \c true to force the sending of the report, \c false to let the library determine if it needs to be sent 
 198 bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t
* const HIDInterfaceInfo
, 
 199                                          uint8_t* const ReportID
, 
 200                                          const uint8_t ReportType
, 
 202                                          uint16_t* const ReportSize
) 
 204         USB_KeyboardReport_Data_t
* KeyboardReport 
= (USB_KeyboardReport_Data_t
*)ReportData
; 
 206         uint8_t JoyStatus_LCL    
= Joystick_GetStatus(); 
 207         uint8_t ButtonStatus_LCL 
= Buttons_GetStatus(); 
 209         KeyboardReport
->Modifier 
= HID_KEYBOARD_MODIFIER_LEFTSHIFT
; 
 211         if (JoyStatus_LCL 
& JOY_UP
) 
 212           KeyboardReport
->KeyCode
[0] = HID_KEYBOARD_SC_A
; 
 213         else if (JoyStatus_LCL 
& JOY_DOWN
) 
 214           KeyboardReport
->KeyCode
[0] = HID_KEYBOARD_SC_B
; 
 216         if (JoyStatus_LCL 
& JOY_LEFT
) 
 217           KeyboardReport
->KeyCode
[0] = HID_KEYBOARD_SC_C
; 
 218         else if (JoyStatus_LCL 
& JOY_RIGHT
) 
 219           KeyboardReport
->KeyCode
[0] = HID_KEYBOARD_SC_D
; 
 221         if (JoyStatus_LCL 
& JOY_PRESS
) 
 222           KeyboardReport
->KeyCode
[0] = HID_KEYBOARD_SC_E
; 
 224         if (ButtonStatus_LCL 
& BUTTONS_BUTTON1
) 
 225           KeyboardReport
->KeyCode
[0] = HID_KEYBOARD_SC_F
; 
 227         *ReportSize 
= sizeof(USB_KeyboardReport_Data_t
); 
 231 /** HID class driver callback function for the processing of HID reports from the host. 
 233  *  \param[in] HIDInterfaceInfo  Pointer to the HID class interface configuration structure being referenced 
 234  *  \param[in] ReportID    Report ID of the received report from the host 
 235  *  \param[in] ReportType  The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature 
 236  *  \param[in] ReportData  Pointer to a buffer where the received report has been stored 
 237  *  \param[in] ReportSize  Size in bytes of the received HID report 
 239 void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t
* const HIDInterfaceInfo
, 
 240                                           const uint8_t ReportID
, 
 241                                           const uint8_t ReportType
, 
 242                                           const void* ReportData
, 
 243                                           const uint16_t ReportSize
) 
 245         uint8_t  LEDMask   
= LEDS_NO_LEDS
; 
 246         uint8_t* LEDReport 
= (uint8_t*)ReportData
; 
 248         if (*LEDReport 
& HID_KEYBOARD_LED_NUMLOCK
) 
 249           LEDMask 
|= LEDS_LED1
; 
 251         if (*LEDReport 
& HID_KEYBOARD_LED_CAPSLOCK
) 
 252           LEDMask 
|= LEDS_LED3
; 
 254         if (*LEDReport 
& HID_KEYBOARD_LED_SCROLLLOCK
) 
 255           LEDMask 
|= LEDS_LED4
; 
 257         LEDs_SetAllLEDs(LEDMask
);