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  Denver Gingerich (denver [at] ossguy [dot] com) 
  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 KeyboardMouse demo. This file contains the main tasks of the demo and 
  35  *  is responsible for the initial application hardware configuration. 
  38 #include "KeyboardMouse.h" 
  40 /** Global structure to hold the current keyboard interface HID report, for transmission to the host */ 
  41 static USB_KeyboardReport_Data_t KeyboardReportData
; 
  43 /** Global structure to hold the current mouse interface HID report, for transmission to the host */ 
  44 static USB_MouseReport_Data_t MouseReportData
; 
  47 /** Main program entry point. This routine configures the hardware required by the application, then 
  48  *  enters a loop to run the application tasks in sequence. 
  54         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
  55         GlobalInterruptEnable(); 
  65 /** Configures the board hardware and chip peripherals for the demo's functionality. */ 
  66 void SetupHardware(void) 
  68 #if (ARCH == ARCH_AVR8) 
  69         /* Disable watchdog if enabled by bootloader/fuses */ 
  70         MCUSR 
&= ~(1 << WDRF
); 
  73         /* Disable clock division */ 
  74         clock_prescale_set(clock_div_1
); 
  75 #elif (ARCH == ARCH_XMEGA) 
  76         /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */ 
  77         XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ
, 2000000, F_CPU
); 
  78         XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL
); 
  80         /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */ 
  81         XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ
); 
  82         XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ
, DFLL_REF_INT_USBSOF
, F_USB
); 
  84         PMIC
.CTRL 
= PMIC_LOLVLEN_bm 
| PMIC_MEDLVLEN_bm 
| PMIC_HILVLEN_bm
; 
  87         /* Hardware Initialization */ 
  93 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and 
  94  *  starts the library USB task to begin the enumeration and USB management process. 
  96 void EVENT_USB_Device_Connect(void) 
  98         /* Indicate USB enumerating */ 
  99         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
 102 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via 
 103  *  the status LEDs and stops the USB management task. 
 105 void EVENT_USB_Device_Disconnect(void) 
 107         /* Indicate USB not ready */ 
 108         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
 111 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration 
 112  *  of the USB device after enumeration, and configures the keyboard and mouse device endpoints. 
 114 void EVENT_USB_Device_ConfigurationChanged(void) 
 116         bool ConfigSuccess 
= true; 
 118         /* Setup Keyboard HID Report Endpoints */ 
 119         ConfigSuccess 
&= Endpoint_ConfigureEndpoint(KEYBOARD_IN_EPADDR
, EP_TYPE_INTERRUPT
, HID_EPSIZE
, 1); 
 120         ConfigSuccess 
&= Endpoint_ConfigureEndpoint(KEYBOARD_OUT_EPADDR
, EP_TYPE_INTERRUPT
, HID_EPSIZE
, 1); 
 122         /* Setup Mouse HID Report Endpoint */ 
 123         ConfigSuccess 
&= Endpoint_ConfigureEndpoint(MOUSE_IN_EPADDR
, EP_TYPE_INTERRUPT
, HID_EPSIZE
, 1); 
 125         /* Indicate endpoint configuration success or failure */ 
 126         LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY 
: LEDMASK_USB_ERROR
); 
 129 /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to 
 130  *  the device from the USB host before passing along unhandled control requests to the library for processing 
 133 void EVENT_USB_Device_ControlRequest(void) 
 138         /* Handle HID Class specific requests */ 
 139         switch (USB_ControlRequest
.bRequest
) 
 141                 case HID_REQ_GetReport
: 
 142                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 144                                 Endpoint_ClearSETUP(); 
 146                                 /* Determine if it is the mouse or the keyboard data that is being requested */ 
 147                                 if (!(USB_ControlRequest
.wIndex
)) 
 149                                         ReportData 
= (uint8_t*)&KeyboardReportData
; 
 150                                         ReportSize 
= sizeof(KeyboardReportData
); 
 154                                         ReportData 
= (uint8_t*)&MouseReportData
; 
 155                                         ReportSize 
= sizeof(MouseReportData
); 
 158                                 /* Write the report data to the control endpoint */ 
 159                                 Endpoint_Write_Control_Stream_LE(ReportData
, ReportSize
); 
 162                                 /* Clear the report data afterwards */ 
 163                                 memset(ReportData
, 0, ReportSize
); 
 167                 case HID_REQ_SetReport
: 
 168                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_HOSTTODEVICE 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 170                                 Endpoint_ClearSETUP(); 
 172                                 /* Wait until the LED report has been sent by the host */ 
 173                                 while (!(Endpoint_IsOUTReceived())) 
 175                                         if (USB_DeviceState 
== DEVICE_STATE_Unattached
) 
 179                                 /* Read in the LED report from the host */ 
 180                                 uint8_t LEDStatus 
= Endpoint_Read_8(); 
 183                                 Endpoint_ClearStatusStage(); 
 185                                 /* Process the incoming LED report */ 
 186                                 Keyboard_ProcessLEDReport(LEDStatus
); 
 193 /** Processes a given Keyboard LED report from the host, and sets the board LEDs to match. Since the Keyboard 
 194  *  LED report can be sent through either the control endpoint (via a HID SetReport request) or the HID OUT 
 195  *  endpoint, the processing code is placed here to avoid duplicating it and potentially having different 
 196  *  behavior depending on the method used to sent it. 
 198 void Keyboard_ProcessLEDReport(const uint8_t LEDStatus
) 
 200         uint8_t LEDMask 
= LEDS_LED2
; 
 202         if (LEDStatus 
& HID_KEYBOARD_LED_NUMLOCK
) 
 203           LEDMask 
|= LEDS_LED1
; 
 205         if (LEDStatus 
& HID_KEYBOARD_LED_CAPSLOCK
) 
 206           LEDMask 
|= LEDS_LED3
; 
 208         if (LEDStatus 
& HID_KEYBOARD_LED_SCROLLLOCK
) 
 209           LEDMask 
|= LEDS_LED4
; 
 211         /* Set the status LEDs to the current Keyboard LED status */ 
 212         LEDs_SetAllLEDs(LEDMask
); 
 215 /** Keyboard task. This generates the next keyboard HID report for the host, and transmits it via the 
 216  *  keyboard IN endpoint when the host is ready for more data. Additionally, it processes host LED status 
 217  *  reports sent to the device via the keyboard OUT reporting endpoint. 
 219 void Keyboard_HID_Task(void) 
 221         uint8_t JoyStatus_LCL 
= Joystick_GetStatus(); 
 223         /* Device must be connected and configured for the task to run */ 
 224         if (USB_DeviceState 
!= DEVICE_STATE_Configured
) 
 227         /* Check if board button is not pressed, if so mouse mode enabled */ 
 228         if (!(Buttons_GetStatus() & BUTTONS_BUTTON1
)) 
 230                 /* Make sent key uppercase by indicating that the left shift key is pressed */ 
 231                 KeyboardReportData
.Modifier 
= HID_KEYBOARD_MODIFIER_LEFTSHIFT
; 
 233                 if (JoyStatus_LCL 
& JOY_UP
) 
 234                   KeyboardReportData
.KeyCode
[0] = HID_KEYBOARD_SC_A
; 
 235                 else if (JoyStatus_LCL 
& JOY_DOWN
) 
 236                   KeyboardReportData
.KeyCode
[0] = HID_KEYBOARD_SC_B
; 
 238                 if (JoyStatus_LCL 
& JOY_LEFT
) 
 239                   KeyboardReportData
.KeyCode
[0] = HID_KEYBOARD_SC_C
; 
 240                 else if (JoyStatus_LCL 
& JOY_RIGHT
) 
 241                   KeyboardReportData
.KeyCode
[0] = HID_KEYBOARD_SC_D
; 
 243                 if (JoyStatus_LCL 
& JOY_PRESS
) 
 244                   KeyboardReportData
.KeyCode
[0] = HID_KEYBOARD_SC_E
; 
 247         /* Select the Keyboard Report Endpoint */ 
 248         Endpoint_SelectEndpoint(KEYBOARD_IN_EPADDR
); 
 250         /* Check if Keyboard Endpoint Ready for Read/Write */ 
 251         if (Endpoint_IsReadWriteAllowed()) 
 253                 /* Write Keyboard Report Data */ 
 254                 Endpoint_Write_Stream_LE(&KeyboardReportData
, sizeof(KeyboardReportData
), NULL
); 
 256                 /* Finalize the stream transfer to send the last packet */ 
 259                 /* Clear the report data afterwards */ 
 260                 memset(&KeyboardReportData
, 0, sizeof(KeyboardReportData
)); 
 263         /* Select the Keyboard LED Report Endpoint */ 
 264         Endpoint_SelectEndpoint(KEYBOARD_OUT_EPADDR
); 
 266         /* Check if Keyboard LED Endpoint Ready for Read/Write */ 
 267         if (Endpoint_IsReadWriteAllowed()) 
 269                 /* Read in and process the LED report from the host */ 
 270                 Keyboard_ProcessLEDReport(Endpoint_Read_8()); 
 272                 /* Handshake the OUT Endpoint - clear endpoint and ready for next report */ 
 277 /** Mouse task. This generates the next mouse HID report for the host, and transmits it via the 
 278  *  mouse IN endpoint when the host is ready for more data. 
 280 void Mouse_HID_Task(void) 
 282         uint8_t JoyStatus_LCL 
= Joystick_GetStatus(); 
 284         /* Device must be connected and configured for the task to run */ 
 285         if (USB_DeviceState 
!= DEVICE_STATE_Configured
) 
 288         /* Check if board button is pressed, if so mouse mode enabled */ 
 289         if (Buttons_GetStatus() & BUTTONS_BUTTON1
) 
 291                 if (JoyStatus_LCL 
& JOY_UP
) 
 292                   MouseReportData
.Y 
=  1; 
 293                 else if (JoyStatus_LCL 
& JOY_DOWN
) 
 294                   MouseReportData
.Y 
= -1; 
 296                 if (JoyStatus_LCL 
& JOY_RIGHT
) 
 297                   MouseReportData
.X 
=  1; 
 298                 else if (JoyStatus_LCL 
& JOY_LEFT
) 
 299                   MouseReportData
.X 
= -1; 
 301                 if (JoyStatus_LCL 
& JOY_PRESS
) 
 302                   MouseReportData
.Button 
|= (1 << 0); 
 305         /* Select the Mouse Report Endpoint */ 
 306         Endpoint_SelectEndpoint(MOUSE_IN_EPADDR
); 
 308         /* Check if Mouse Endpoint Ready for Read/Write */ 
 309         if (Endpoint_IsReadWriteAllowed()) 
 311                 /* Write Mouse Report Data */ 
 312                 Endpoint_Write_Stream_LE(&MouseReportData
, sizeof(MouseReportData
), NULL
); 
 314                 /* Finalize the stream transfer to send the last packet */ 
 317                 /* Clear the report data afterwards */ 
 318                 memset(&MouseReportData
, 0, sizeof(MouseReportData
));