Skip to content

Instantly share code, notes, and snippets.

@eltang
Last active April 30, 2017 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eltang/499e1bd4a65227591a28f973bb1be818 to your computer and use it in GitHub Desktop.
Save eltang/499e1bd4a65227591a28f973bb1be818 to your computer and use it in GitHub Desktop.
/*
LUFA Library
Copyright (C) Dean Camera, 2016.
dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/
/*
Copyright 2016 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in
all copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
The author disclaims all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/
/** \file
*
* USB Device Descriptors, for library use when in USB device mode. Descriptors are special
* computer-readable structures which the host requests upon device enumeration, to determine
* the device's capabilities and functions.
*/
#include "descriptors.h"
/** HID class report descriptor. This is a special descriptor constructed with values from the
* USBIF HID class specification to describe the reports and capabilities of the HID device. This
* descriptor is parsed by the host and its contents used to determine what data (and in what encoding)
* the device will send, and what it may be sent back from the host. Refer to the HID specification for
* more details on HID report descriptors.
*
* This descriptor describes the mouse HID interface's report structure.
*/
const USB_Descriptor_HIDReport_Datatype_t PROGMEM MouseReport[] =
{
/* Use the HID class driver's standard Mouse report.
* Min X/Y Axis values: -1
* Max X/Y Axis values: 1
* Min physical X/Y Axis values (used to determine resolution): -1
* Max physical X/Y Axis values (used to determine resolution): 1
* Buttons: 3
* Absolute screen coordinates: false
*/
HID_DESCRIPTOR_MOUSE(-1, 1, -1, 1, 3, false)
};
/** Same as the MouseReport structure, but defines the keyboard HID interface's report structure. */
const USB_Descriptor_HIDReport_Datatype_t PROGMEM KeyboardReport[] =
{
/* Use the HID class driver's standard Keyboard report.
* Max simultaneous keys: 6
*/
HID_DESCRIPTOR_KEYBOARD(6)
};
const USB_Descriptor_HIDReport_Datatype_t PROGMEM EnhancedKeyboardReport[] =
{
0x05, 0x0C, // Usage Page (Consumer)
0x09, 0x01, // Usage (Consumer Controls)
0xA1, 0x01, // Collection (Application)
0x75, 0x10, // Report Size (16)
0x95, 0x01, // Report Count (1)
0x19, 0x00, // Usage Minimum (0)
0x2A, 0x9C, 0x02, // Usage Maximum (0x29C)
0x15, 0x00, // Logical Minimum (0)
0x26, 0x9C, 0x02, // Logical Maximum (0x29C)
0x81, 0x00, // Input (Data, Array)
0x05, 0x01, // Usage Page (Generic Desktop)
0x75, 0x08, // Report Size (8)
0x95, 0x01, // Report Count (1)
0x19, 0x00, // Usage Minimum (0)
0x29, 0xB7, // Usage Maximum (0xB7)
0x15, 0x00, // Logical Minimum (0)
0x26, 0xB7, 0x00, // Logical Maximum (0xB7)
0x81, 0x00, // Input (Data, Array)
0xC0 // End Collection
};
/** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
* device characteristics, including the supported USB version, control endpoint size and the
* number of device configurations. The descriptor is read out by the USB host when the enumeration
* process begins.
*/
const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
{
.Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
.USBSpecification = VERSION_BCD(1,1,0),
#ifdef VIRTUAL_SERIAL_ENABLE
.Class = USB_CSCP_IADDeviceClass,
.SubClass = USB_CSCP_IADDeviceSubclass,
.Protocol = USB_CSCP_IADDeviceProtocol,
#else
.Class = USB_CSCP_NoDeviceClass,
.SubClass = USB_CSCP_NoDeviceSubclass,
.Protocol = USB_CSCP_NoDeviceProtocol,
#endif
.Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
.VendorID = 0x03EB,
.ProductID = 0x2062,
.ReleaseNumber = VERSION_BCD(0,0,1),
.ManufacturerStrIndex = STRING_ID_Manufacturer,
.ProductStrIndex = STRING_ID_Product,
#ifdef VIRTUAL_SERIAL_ENABLE
.SerialNumStrIndex = USE_INTERNAL_SERIAL,
#else
.SerialNumStrIndex = NO_DESCRIPTOR,
#endif
.NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
};
/** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
* of the device in one of its supported configurations, including information about any device interfaces
* and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
* a configuration so that the host may correctly communicate with the USB device.
*/
const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
{
.Config =
{
.Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
.TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
#ifdef VIRTUAL_SERIAL_ENABLE
.TotalInterfaces = 5,
#else
.TotalInterfaces = 3,
#endif
.ConfigurationNumber = 1,
.ConfigurationStrIndex = NO_DESCRIPTOR,
.ConfigAttributes = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_REMOTEWAKEUP),
.MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
},
.HID1_KeyboardInterface =
{
.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
.InterfaceNumber = INTERFACE_ID_Keyboard,
.AlternateSetting = 0x00,
.TotalEndpoints = 1,
.Class = HID_CSCP_HIDClass,
.SubClass = HID_CSCP_BootSubclass,
.Protocol = HID_CSCP_KeyboardBootProtocol,
.InterfaceStrIndex = NO_DESCRIPTOR
},
.HID1_KeyboardHID =
{
.Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},
.HIDSpec = VERSION_BCD(1,1,1),
.CountryCode = 0x00,
.TotalReportDescriptors = 1,
.HIDReportType = HID_DTYPE_Report,
.HIDReportLength = sizeof(KeyboardReport)
},
.HID1_ReportINEndpoint =
{
.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
.EndpointAddress = KEYBOARD_EPADDR,
.Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
.EndpointSize = HID_EPSIZE,
.PollingIntervalMS = 0x01
},
.HID2_MouseInterface =
{
.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
.InterfaceNumber = INTERFACE_ID_Mouse,
.AlternateSetting = 0x00,
.TotalEndpoints = 1,
.Class = HID_CSCP_HIDClass,
.SubClass = HID_CSCP_BootSubclass,
.Protocol = HID_CSCP_MouseBootProtocol,
.InterfaceStrIndex = NO_DESCRIPTOR
},
.HID2_MouseHID =
{
.Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},
.HIDSpec = VERSION_BCD(1,1,1),
.CountryCode = 0x00,
.TotalReportDescriptors = 1,
.HIDReportType = HID_DTYPE_Report,
.HIDReportLength = sizeof(MouseReport)
},
.HID2_ReportINEndpoint =
{
.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
.EndpointAddress = MOUSE_EPADDR,
.Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
.EndpointSize = HID_EPSIZE,
.PollingIntervalMS = 0x01
},
.HID3_EnhancedKeyboardInterface =
{
.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
.InterfaceNumber = INTERFACE_ID_EnhancedKeyboard,
.AlternateSetting = 0x00,
.TotalEndpoints = 1,
.Class = HID_CSCP_HIDClass,
.SubClass = HID_CSCP_NonBootSubclass,
.Protocol = HID_CSCP_NonBootProtocol,
.InterfaceStrIndex = NO_DESCRIPTOR
},
.HID3_EnhancedKeyboardHID =
{
.Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},
.HIDSpec = VERSION_BCD(1,1,1),
.CountryCode = 0x00,
.TotalReportDescriptors = 1,
.HIDReportType = HID_DTYPE_Report,
.HIDReportLength = sizeof(EnhancedKeyboardReport)
},
.HID3_ReportINEndpoint =
{
.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
.EndpointAddress = ENHANCEDKEYBOARD_EPADDR,
.Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
.EndpointSize = HID_EPSIZE,
.PollingIntervalMS = 0x01
},
#ifdef VIRTUAL_SERIAL_ENABLE
.CDC_IAD =
{
.Header = {.Size = sizeof(USB_Descriptor_Interface_Association_t), .Type = DTYPE_InterfaceAssociation},
.FirstInterfaceIndex = INTERFACE_ID_CDC_CCI,
.TotalInterfaces = 2,
.Class = CDC_CSCP_CDCClass,
.SubClass = CDC_CSCP_ACMSubclass,
.Protocol = CDC_CSCP_ATCommandProtocol,
.IADStrIndex = NO_DESCRIPTOR
},
.CDC_CCI_Interface =
{
.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
.InterfaceNumber = INTERFACE_ID_CDC_CCI,
.AlternateSetting = 0,
.TotalEndpoints = 1,
.Class = CDC_CSCP_CDCClass,
.SubClass = CDC_CSCP_ACMSubclass,
.Protocol = CDC_CSCP_ATCommandProtocol,
.InterfaceStrIndex = NO_DESCRIPTOR
},
.CDC_Functional_Header =
{
.Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalHeader_t), .Type = DTYPE_CSInterface},
.Subtype = CDC_DSUBTYPE_CSInterface_Header,
.CDCSpecification = VERSION_BCD(1,1,0),
},
.CDC_Functional_ACM =
{
.Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalACM_t), .Type = DTYPE_CSInterface},
.Subtype = CDC_DSUBTYPE_CSInterface_ACM,
.Capabilities = 0x06,
},
.CDC_Functional_Union =
{
.Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalUnion_t), .Type = DTYPE_CSInterface},
.Subtype = CDC_DSUBTYPE_CSInterface_Union,
.MasterInterfaceNumber = INTERFACE_ID_CDC_CCI,
.SlaveInterfaceNumber = INTERFACE_ID_CDC_DCI,
},
.CDC_NotificationEndpoint =
{
.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
.EndpointAddress = CDC_NOTIFICATION_EPADDR,
.Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
.EndpointSize = CDC_NOTIFICATION_EPSIZE,
.PollingIntervalMS = 0xFF
},
.CDC_DCI_Interface =
{
.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
.InterfaceNumber = INTERFACE_ID_CDC_DCI,
.AlternateSetting = 0,
.TotalEndpoints = 2,
.Class = CDC_CSCP_CDCDataClass,
.SubClass = CDC_CSCP_NoDataSubclass,
.Protocol = CDC_CSCP_NoDataProtocol,
.InterfaceStrIndex = NO_DESCRIPTOR
},
.CDC_DataOutEndpoint =
{
.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
.EndpointAddress = CDC_RX_EPADDR,
.Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
.EndpointSize = CDC_TXRX_EPSIZE,
.PollingIntervalMS = 0x05
},
.CDC_DataInEndpoint =
{
.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
.EndpointAddress = CDC_TX_EPADDR,
.Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
.EndpointSize = CDC_TXRX_EPSIZE,
.PollingIntervalMS = 0x05
}
#endif
};
/** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
* the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
* via the language ID table available at USB.org what languages the device supports for its string descriptors.
*/
const USB_Descriptor_String_t PROGMEM LanguageString = USB_STRING_DESCRIPTOR_ARRAY(LANGUAGE_ID_ENG);
/** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
* form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
* Descriptor.
*/
const USB_Descriptor_String_t PROGMEM ManufacturerString = USB_STRING_DESCRIPTOR(L"Eric Tang");
/** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
* and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
* Descriptor.
*/
const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"Keystrokes");
/** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
* documentation) by the application code so that the address and size of a requested descriptor can be given
* to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
* is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
* USB host.
*/
uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
const uint16_t wIndex,
const void** const DescriptorAddress)
{
const uint8_t DescriptorType = (wValue >> 8);
const uint8_t DescriptorNumber = (wValue & 0xFF);
const void* Address = NULL;
uint16_t Size = NO_DESCRIPTOR;
switch (DescriptorType)
{
case DTYPE_Device:
Address = &DeviceDescriptor;
Size = sizeof(USB_Descriptor_Device_t);
break;
case DTYPE_Configuration:
Address = &ConfigurationDescriptor;
Size = sizeof(USB_Descriptor_Configuration_t);
break;
case DTYPE_String:
switch (DescriptorNumber)
{
case STRING_ID_Language:
Address = &LanguageString;
Size = pgm_read_byte(&LanguageString.Header.Size);
break;
case STRING_ID_Manufacturer:
Address = &ManufacturerString;
Size = pgm_read_byte(&ManufacturerString.Header.Size);
break;
case STRING_ID_Product:
Address = &ProductString;
Size = pgm_read_byte(&ProductString.Header.Size);
break;
}
break;
case HID_DTYPE_HID:
switch (wIndex)
{
case INTERFACE_ID_Keyboard:
Address = &ConfigurationDescriptor.HID1_KeyboardHID;
Size = sizeof(USB_HID_Descriptor_HID_t);
break;
case INTERFACE_ID_Mouse:
Address = &ConfigurationDescriptor.HID2_MouseHID;
Size = sizeof(USB_HID_Descriptor_HID_t);
break;
case INTERFACE_ID_EnhancedKeyboard:
Address = &ConfigurationDescriptor.HID3_EnhancedKeyboardHID;
Size = sizeof(USB_HID_Descriptor_HID_t);
break;
}
break;
case HID_DTYPE_Report:
switch (wIndex)
{
case INTERFACE_ID_Keyboard:
Address = &KeyboardReport;
Size = sizeof(KeyboardReport);
break;
case INTERFACE_ID_Mouse:
Address = &MouseReport;
Size = sizeof(MouseReport);
break;
case INTERFACE_ID_EnhancedKeyboard:
Address = &EnhancedKeyboardReport;
Size = sizeof(EnhancedKeyboardReport);
break;
}
break;
}
*DescriptorAddress = Address;
return Size;
}
/*
LUFA Library
Copyright (C) Dean Camera, 2016.
dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/
/*
Copyright 2016 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in
all copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
The author disclaims all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/
/** \file
*
* Header file for Descriptors.c.
*/
#ifndef _DESCRIPTORS_H_
#define _DESCRIPTORS_H_
/* Includes: */
#include <avr/pgmspace.h>
#include <LUFA/Drivers/USB/USB.h>
/* Macros: */
/** Endpoint address of the Keyboard HID reporting IN endpoint. */
#define KEYBOARD_EPADDR (ENDPOINT_DIR_IN | 1)
/** Endpoint address of the Mouse HID reporting IN endpoint. */
#define MOUSE_EPADDR (ENDPOINT_DIR_IN | 2)
/** Endpoint address of the Enhanced Keyboard HID reporting IN endpoint. */
#define ENHANCEDKEYBOARD_EPADDR (ENDPOINT_DIR_IN | 3)
/** Size in bytes of the HID reporting IN endpoint. */
#define HID_EPSIZE 8
/** Endpoint address of the CDC device-to-host notification IN endpoint. */
#define CDC_NOTIFICATION_EPADDR (ENDPOINT_DIR_IN | 4)
/** Endpoint address of the CDC device-to-host data IN endpoint. */
#define CDC_TX_EPADDR (ENDPOINT_DIR_IN | 5)
/** Endpoint address of the CDC host-to-device data OUT endpoint. */
#define CDC_RX_EPADDR (ENDPOINT_DIR_OUT | 6)
/** Size in bytes of the CDC device-to-host notification IN endpoint. */
#define CDC_NOTIFICATION_EPSIZE 8
/** Size in bytes of the CDC data IN and OUT endpoints. */
#define CDC_TXRX_EPSIZE 16
/* Type Defines: */
/** Type define for the device configuration descriptor structure. This must be defined in the
* application code, as the configuration descriptor contains several sub-descriptors which
* vary between devices, and which describe the device's usage to the host.
*/
typedef struct
{
USB_Descriptor_Configuration_Header_t Config;
// Keyboard HID Interface
USB_Descriptor_Interface_t HID1_KeyboardInterface;
USB_HID_Descriptor_HID_t HID1_KeyboardHID;
USB_Descriptor_Endpoint_t HID1_ReportINEndpoint;
// Mouse HID Interface
USB_Descriptor_Interface_t HID2_MouseInterface;
USB_HID_Descriptor_HID_t HID2_MouseHID;
USB_Descriptor_Endpoint_t HID2_ReportINEndpoint;
// Enhanced Keyboard HID Interface
USB_Descriptor_Interface_t HID3_EnhancedKeyboardInterface;
USB_HID_Descriptor_HID_t HID3_EnhancedKeyboardHID;
USB_Descriptor_Endpoint_t HID3_ReportINEndpoint;
#ifdef VIRTUAL_SERIAL_ENABLE
// CDC Control Interface
USB_Descriptor_Interface_Association_t CDC_IAD;
USB_Descriptor_Interface_t CDC_CCI_Interface;
USB_CDC_Descriptor_FunctionalHeader_t CDC_Functional_Header;
USB_CDC_Descriptor_FunctionalACM_t CDC_Functional_ACM;
USB_CDC_Descriptor_FunctionalUnion_t CDC_Functional_Union;
USB_Descriptor_Endpoint_t CDC_NotificationEndpoint;
// CDC Data Interface
USB_Descriptor_Interface_t CDC_DCI_Interface;
USB_Descriptor_Endpoint_t CDC_DataOutEndpoint;
USB_Descriptor_Endpoint_t CDC_DataInEndpoint;
#endif
} USB_Descriptor_Configuration_t;
typedef struct {
uint16_t ConsumerControlCode;
uint8_t SystemControlCode;
} USB_EnhancedKeyboardReport_Data_t;
/** Enum for the device interface descriptor IDs within the device. Each interface descriptor
* should have a unique ID index associated with it, which can be used to refer to the
* interface from other descriptors.
*/
enum InterfaceDescriptors_t
{
INTERFACE_ID_Keyboard = 0, /**< Keyboard interface descriptor ID */
INTERFACE_ID_Mouse = 1, /**< Mouse interface descriptor ID */
INTERFACE_ID_EnhancedKeyboard = 2, /**< Enhanced Keyboard interface descriptor ID */
INTERFACE_ID_CDC_CCI = 3, /**< CDC CCI interface descriptor ID */
INTERFACE_ID_CDC_DCI = 4, /**< CDC DCI interface descriptor ID */
};
/** Enum for the device string descriptor IDs within the device. Each string descriptor should
* have a unique ID index associated with it, which can be used to refer to the string from
* other descriptors.
*/
enum StringDescriptors_t
{
STRING_ID_Language = 0, /**< Supported Languages string descriptor ID (must be zero) */
STRING_ID_Manufacturer = 1, /**< Manufacturer string ID */
STRING_ID_Product = 2, /**< Product string ID */
};
/* Function Prototypes: */
uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
const uint16_t wIndex,
const void** const DescriptorAddress)
ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(3);
#endif
/*
LUFA Library
Copyright (C) Dean Camera, 2017.
dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/
/*
Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in
all copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
The author disclaims all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/
/** \file
* \brief Common definitions and declarations for the library USB HID Class driver.
*
* Common definitions and declarations for the library USB HID Class driver.
*
* \note This file should not be included directly. It is automatically included as needed by the USB module driver
* dispatch header located in LUFA/Drivers/USB.h.
*/
/** \ingroup Group_USBClassHID
* \defgroup Group_USBClassHIDCommon Common Class Definitions
*
* \section Sec_USBClassHIDCommon_ModDescription Module Description
* Constants, Types and Enum definitions that are common to both Device and Host modes for the USB
* HID Class.
*
* @{
*/
#ifndef _HID_CLASS_COMMON_H_
#define _HID_CLASS_COMMON_H_
/* Includes: */
#include "../../Core/StdDescriptors.h"
#include "HIDParser.h"
/* Enable C linkage for C++ Compilers: */
#if defined(__cplusplus)
extern "C" {
#endif
/* Preprocessor Checks: */
#if !defined(__INCLUDE_FROM_HID_DRIVER)
#error Do not include this file directly. Include LUFA/Drivers/USB.h instead.
#endif
/* Macros: */
/** \name Keyboard Standard Report Modifier Masks */
//@{
/** Constant for a keyboard report modifier byte, indicating that the keyboard's left control key is currently pressed. */
#define HID_KEYBOARD_MODIFIER_LEFTCTRL (1 << 0)
/** Constant for a keyboard report modifier byte, indicating that the keyboard's left shift key is currently pressed. */
#define HID_KEYBOARD_MODIFIER_LEFTSHIFT (1 << 1)
/** Constant for a keyboard report modifier byte, indicating that the keyboard's left alt key is currently pressed. */
#define HID_KEYBOARD_MODIFIER_LEFTALT (1 << 2)
/** Constant for a keyboard report modifier byte, indicating that the keyboard's left GUI key is currently pressed. */
#define HID_KEYBOARD_MODIFIER_LEFTGUI (1 << 3)
/** Constant for a keyboard report modifier byte, indicating that the keyboard's right control key is currently pressed. */
#define HID_KEYBOARD_MODIFIER_RIGHTCTRL (1 << 4)
/** Constant for a keyboard report modifier byte, indicating that the keyboard's right shift key is currently pressed. */
#define HID_KEYBOARD_MODIFIER_RIGHTSHIFT (1 << 5)
/** Constant for a keyboard report modifier byte, indicating that the keyboard's right alt key is currently pressed. */
#define HID_KEYBOARD_MODIFIER_RIGHTALT (1 << 6)
/** Constant for a keyboard report modifier byte, indicating that the keyboard's right GUI key is currently pressed. */
#define HID_KEYBOARD_MODIFIER_RIGHTGUI (1 << 7)
//@}
/** \name Keyboard Standard Report LED Masks */
//@{
/** Constant for a keyboard output report LED byte, indicating that the host's NUM LOCK mode is currently set. */
#define HID_KEYBOARD_LED_NUMLOCK (1 << 0)
/** Constant for a keyboard output report LED byte, indicating that the host's CAPS LOCK mode is currently set. */
#define HID_KEYBOARD_LED_CAPSLOCK (1 << 1)
/** Constant for a keyboard output report LED byte, indicating that the host's SCROLL LOCK mode is currently set. */
#define HID_KEYBOARD_LED_SCROLLLOCK (1 << 2)
/** Constant for a keyboard output report LED byte, indicating that the host's COMPOSE mode is currently set. */
#define HID_KEYBOARD_LED_COMPOSE (1 << 3)
/** Constant for a keyboard output report LED byte, indicating that the host's KANA mode is currently set. */
#define HID_KEYBOARD_LED_KANA (1 << 4)
//@}
/** \name Keyboard Standard Report Key Scan-codes */
//@{
#define HID_KEYBOARD_SC_ERROR_ROLLOVER 0x01
#define HID_KEYBOARD_SC_POST_FAIL 0x02
#define HID_KEYBOARD_SC_ERROR_UNDEFINED 0x03
#define HID_KEYBOARD_SC_A 0x04
#define HID_KEYBOARD_SC_B 0x05
#define HID_KEYBOARD_SC_C 0x06
#define HID_KEYBOARD_SC_D 0x07
#define HID_KEYBOARD_SC_E 0x08
#define HID_KEYBOARD_SC_F 0x09
#define HID_KEYBOARD_SC_G 0x0A
#define HID_KEYBOARD_SC_H 0x0B
#define HID_KEYBOARD_SC_I 0x0C
#define HID_KEYBOARD_SC_J 0x0D
#define HID_KEYBOARD_SC_K 0x0E
#define HID_KEYBOARD_SC_L 0x0F
#define HID_KEYBOARD_SC_M 0x10
#define HID_KEYBOARD_SC_N 0x11
#define HID_KEYBOARD_SC_O 0x12
#define HID_KEYBOARD_SC_P 0x13
#define HID_KEYBOARD_SC_Q 0x14
#define HID_KEYBOARD_SC_R 0x15
#define HID_KEYBOARD_SC_S 0x16
#define HID_KEYBOARD_SC_T 0x17
#define HID_KEYBOARD_SC_U 0x18
#define HID_KEYBOARD_SC_V 0x19
#define HID_KEYBOARD_SC_W 0x1A
#define HID_KEYBOARD_SC_X 0x1B
#define HID_KEYBOARD_SC_Y 0x1C
#define HID_KEYBOARD_SC_Z 0x1D
#define HID_KEYBOARD_SC_1_AND_EXCLAMATION 0x1E
#define HID_KEYBOARD_SC_2_AND_AT 0x1F
#define HID_KEYBOARD_SC_3_AND_HASHMARK 0x20
#define HID_KEYBOARD_SC_4_AND_DOLLAR 0x21
#define HID_KEYBOARD_SC_5_AND_PERCENTAGE 0x22
#define HID_KEYBOARD_SC_6_AND_CARET 0x23
#define HID_KEYBOARD_SC_7_AND_AMPERSAND 0x24
#define HID_KEYBOARD_SC_8_AND_ASTERISK 0x25
#define HID_KEYBOARD_SC_9_AND_OPENING_PARENTHESIS 0x26
#define HID_KEYBOARD_SC_0_AND_CLOSING_PARENTHESIS 0x27
#define HID_KEYBOARD_SC_ENTER 0x28
#define HID_KEYBOARD_SC_ESCAPE 0x29
#define HID_KEYBOARD_SC_BACKSPACE 0x2A
#define HID_KEYBOARD_SC_TAB 0x2B
#define HID_KEYBOARD_SC_SPACE 0x2C
#define HID_KEYBOARD_SC_MINUS_AND_UNDERSCORE 0x2D
#define HID_KEYBOARD_SC_EQUAL_AND_PLUS 0x2E
#define HID_KEYBOARD_SC_OPENING_BRACKET_AND_OPENING_BRACE 0x2F
#define HID_KEYBOARD_SC_CLOSING_BRACKET_AND_CLOSING_BRACE 0x30
#define HID_KEYBOARD_SC_BACKSLASH_AND_PIPE 0x31
#define HID_KEYBOARD_SC_NON_US_HASHMARK_AND_TILDE 0x32
#define HID_KEYBOARD_SC_SEMICOLON_AND_COLON 0x33
#define HID_KEYBOARD_SC_APOSTROPHE_AND_QUOTE 0x34
#define HID_KEYBOARD_SC_GRAVE_ACCENT_AND_TILDE 0x35
#define HID_KEYBOARD_SC_COMMA_AND_LESS_THAN_SIGN 0x36
#define HID_KEYBOARD_SC_DOT_AND_GREATER_THAN_SIGN 0x37
#define HID_KEYBOARD_SC_SLASH_AND_QUESTION_MARK 0x38
#define HID_KEYBOARD_SC_CAPS_LOCK 0x39
#define HID_KEYBOARD_SC_F1 0x3A
#define HID_KEYBOARD_SC_F2 0x3B
#define HID_KEYBOARD_SC_F3 0x3C
#define HID_KEYBOARD_SC_F4 0x3D
#define HID_KEYBOARD_SC_F5 0x3E
#define HID_KEYBOARD_SC_F6 0x3F
#define HID_KEYBOARD_SC_F7 0x40
#define HID_KEYBOARD_SC_F8 0x41
#define HID_KEYBOARD_SC_F9 0x42
#define HID_KEYBOARD_SC_F10 0x43
#define HID_KEYBOARD_SC_F11 0x44
#define HID_KEYBOARD_SC_F12 0x45
#define HID_KEYBOARD_SC_PRINT_SCREEN 0x46
#define HID_KEYBOARD_SC_SCROLL_LOCK 0x47
#define HID_KEYBOARD_SC_PAUSE 0x48
#define HID_KEYBOARD_SC_INSERT 0x49
#define HID_KEYBOARD_SC_HOME 0x4A
#define HID_KEYBOARD_SC_PAGE_UP 0x4B
#define HID_KEYBOARD_SC_DELETE 0x4C
#define HID_KEYBOARD_SC_END 0x4D
#define HID_KEYBOARD_SC_PAGE_DOWN 0x4E
#define HID_KEYBOARD_SC_RIGHT_ARROW 0x4F
#define HID_KEYBOARD_SC_LEFT_ARROW 0x50
#define HID_KEYBOARD_SC_DOWN_ARROW 0x51
#define HID_KEYBOARD_SC_UP_ARROW 0x52
#define HID_KEYBOARD_SC_NUM_LOCK 0x53
#define HID_KEYBOARD_SC_KEYPAD_SLASH 0x54
#define HID_KEYBOARD_SC_KEYPAD_ASTERISK 0x55
#define HID_KEYBOARD_SC_KEYPAD_MINUS 0x56
#define HID_KEYBOARD_SC_KEYPAD_PLUS 0x57
#define HID_KEYBOARD_SC_KEYPAD_ENTER 0x58
#define HID_KEYBOARD_SC_KEYPAD_1_AND_END 0x59
#define HID_KEYBOARD_SC_KEYPAD_2_AND_DOWN_ARROW 0x5A
#define HID_KEYBOARD_SC_KEYPAD_3_AND_PAGE_DOWN 0x5B
#define HID_KEYBOARD_SC_KEYPAD_4_AND_LEFT_ARROW 0x5C
#define HID_KEYBOARD_SC_KEYPAD_5 0x5D
#define HID_KEYBOARD_SC_KEYPAD_6_AND_RIGHT_ARROW 0x5E
#define HID_KEYBOARD_SC_KEYPAD_7_AND_HOME 0x5F
#define HID_KEYBOARD_SC_KEYPAD_8_AND_UP_ARROW 0x60
#define HID_KEYBOARD_SC_KEYPAD_9_AND_PAGE_UP 0x61
#define HID_KEYBOARD_SC_KEYPAD_0_AND_INSERT 0x62
#define HID_KEYBOARD_SC_KEYPAD_DOT_AND_DELETE 0x63
#define HID_KEYBOARD_SC_NON_US_BACKSLASH_AND_PIPE 0x64
#define HID_KEYBOARD_SC_APPLICATION 0x65
#define HID_KEYBOARD_SC_POWER 0x66
#define HID_KEYBOARD_SC_KEYPAD_EQUAL_SIGN 0x67
#define HID_KEYBOARD_SC_F13 0x68
#define HID_KEYBOARD_SC_F14 0x69
#define HID_KEYBOARD_SC_F15 0x6A
#define HID_KEYBOARD_SC_F16 0x6B
#define HID_KEYBOARD_SC_F17 0x6C
#define HID_KEYBOARD_SC_F18 0x6D
#define HID_KEYBOARD_SC_F19 0x6E
#define HID_KEYBOARD_SC_F20 0x6F
#define HID_KEYBOARD_SC_F21 0x70
#define HID_KEYBOARD_SC_F22 0x71
#define HID_KEYBOARD_SC_F23 0x72
#define HID_KEYBOARD_SC_F24 0x73
#define HID_KEYBOARD_SC_EXECUTE 0x74
#define HID_KEYBOARD_SC_HELP 0x75
#define HID_KEYBOARD_SC_MENU 0x76
#define HID_KEYBOARD_SC_SELECT 0x77
#define HID_KEYBOARD_SC_STOP 0x78
#define HID_KEYBOARD_SC_AGAIN 0x79
#define HID_KEYBOARD_SC_UNDO 0x7A
#define HID_KEYBOARD_SC_CUT 0x7B
#define HID_KEYBOARD_SC_COPY 0x7C
#define HID_KEYBOARD_SC_PASTE 0x7D
#define HID_KEYBOARD_SC_FIND 0x7E
#define HID_KEYBOARD_SC_MUTE 0x7F
#define HID_KEYBOARD_SC_VOLUME_UP 0x80
#define HID_KEYBOARD_SC_VOLUME_DOWN 0x81
#define HID_KEYBOARD_SC_LOCKING_CAPS_LOCK 0x82
#define HID_KEYBOARD_SC_LOCKING_NUM_LOCK 0x83
#define HID_KEYBOARD_SC_LOCKING_SCROLL_LOCK 0x84
#define HID_KEYBOARD_SC_KEYPAD_COMMA 0x85
#define HID_KEYBOARD_SC_KEYPAD_EQUAL_SIGN_AS400 0x86
#define HID_KEYBOARD_SC_INTERNATIONAL1 0x87
#define HID_KEYBOARD_SC_INTERNATIONAL2 0x88
#define HID_KEYBOARD_SC_INTERNATIONAL3 0x89
#define HID_KEYBOARD_SC_INTERNATIONAL4 0x8A
#define HID_KEYBOARD_SC_INTERNATIONAL5 0x8B
#define HID_KEYBOARD_SC_INTERNATIONAL6 0x8C
#define HID_KEYBOARD_SC_INTERNATIONAL7 0x8D
#define HID_KEYBOARD_SC_INTERNATIONAL8 0x8E
#define HID_KEYBOARD_SC_INTERNATIONAL9 0x8F
#define HID_KEYBOARD_SC_LANG1 0x90
#define HID_KEYBOARD_SC_LANG2 0x91
#define HID_KEYBOARD_SC_LANG3 0x92
#define HID_KEYBOARD_SC_LANG4 0x93
#define HID_KEYBOARD_SC_LANG5 0x94
#define HID_KEYBOARD_SC_LANG6 0x95
#define HID_KEYBOARD_SC_LANG7 0x96
#define HID_KEYBOARD_SC_LANG8 0x97
#define HID_KEYBOARD_SC_LANG9 0x98
#define HID_KEYBOARD_SC_ALTERNATE_ERASE 0x99
#define HID_KEYBOARD_SC_SYSREQ 0x9A
#define HID_KEYBOARD_SC_CANCEL 0x9B
#define HID_KEYBOARD_SC_CLEAR 0x9C
#define HID_KEYBOARD_SC_PRIOR 0x9D
#define HID_KEYBOARD_SC_RETURN 0x9E
#define HID_KEYBOARD_SC_SEPARATOR 0x9F
#define HID_KEYBOARD_SC_OUT 0xA0
#define HID_KEYBOARD_SC_OPER 0xA1
#define HID_KEYBOARD_SC_CLEAR_AND_AGAIN 0xA2
#define HID_KEYBOARD_SC_CRSEL_AND_PROPS 0xA3
#define HID_KEYBOARD_SC_EXSEL 0xA4
#define HID_KEYBOARD_SC_KEYPAD_00 0xB0
#define HID_KEYBOARD_SC_KEYPAD_000 0xB1
#define HID_KEYBOARD_SC_THOUSANDS_SEPARATOR 0xB2
#define HID_KEYBOARD_SC_DECIMAL_SEPARATOR 0xB3
#define HID_KEYBOARD_SC_CURRENCY_UNIT 0xB4
#define HID_KEYBOARD_SC_CURRENCY_SUB_UNIT 0xB5
#define HID_KEYBOARD_SC_KEYPAD_OPENING_PARENTHESIS 0xB6
#define HID_KEYBOARD_SC_KEYPAD_CLOSING_PARENTHESIS 0xB7
#define HID_KEYBOARD_SC_KEYPAD_OPENING_BRACE 0xB8
#define HID_KEYBOARD_SC_KEYPAD_CLOSING_BRACE 0xB9
#define HID_KEYBOARD_SC_KEYPAD_TAB 0xBA
#define HID_KEYBOARD_SC_KEYPAD_BACKSPACE 0xBB
#define HID_KEYBOARD_SC_KEYPAD_A 0xBC
#define HID_KEYBOARD_SC_KEYPAD_B 0xBD
#define HID_KEYBOARD_SC_KEYPAD_C 0xBE
#define HID_KEYBOARD_SC_KEYPAD_D 0xBF
#define HID_KEYBOARD_SC_KEYPAD_E 0xC0
#define HID_KEYBOARD_SC_KEYPAD_F 0xC1
#define HID_KEYBOARD_SC_KEYPAD_XOR 0xC2
#define HID_KEYBOARD_SC_KEYPAD_CARET 0xC3
#define HID_KEYBOARD_SC_KEYPAD_PERCENTAGE 0xC4
#define HID_KEYBOARD_SC_KEYPAD_LESS_THAN_SIGN 0xC5
#define HID_KEYBOARD_SC_KEYPAD_GREATER_THAN_SIGN 0xC6
#define HID_KEYBOARD_SC_KEYPAD_AMP 0xC7
#define HID_KEYBOARD_SC_KEYPAD_AMP_AMP 0xC8
#define HID_KEYBOARD_SC_KEYPAD_PIPE 0xC9
#define HID_KEYBOARD_SC_KEYPAD_PIPE_PIPE 0xCA
#define HID_KEYBOARD_SC_KEYPAD_COLON 0xCB
#define HID_KEYBOARD_SC_KEYPAD_HASHMARK 0xCC
#define HID_KEYBOARD_SC_KEYPAD_SPACE 0xCD
#define HID_KEYBOARD_SC_KEYPAD_AT 0xCE
#define HID_KEYBOARD_SC_KEYPAD_EXCLAMATION_SIGN 0xCF
#define HID_KEYBOARD_SC_KEYPAD_MEMORY_STORE 0xD0
#define HID_KEYBOARD_SC_KEYPAD_MEMORY_RECALL 0xD1
#define HID_KEYBOARD_SC_KEYPAD_MEMORY_CLEAR 0xD2
#define HID_KEYBOARD_SC_KEYPAD_MEMORY_ADD 0xD3
#define HID_KEYBOARD_SC_KEYPAD_MEMORY_SUBTRACT 0xD4
#define HID_KEYBOARD_SC_KEYPAD_MEMORY_MULTIPLY 0xD5
#define HID_KEYBOARD_SC_KEYPAD_MEMORY_DIVIDE 0xD6
#define HID_KEYBOARD_SC_KEYPAD_PLUS_AND_MINUS 0xD7
#define HID_KEYBOARD_SC_KEYPAD_CLEAR 0xD8
#define HID_KEYBOARD_SC_KEYPAD_CLEAR_ENTRY 0xD9
#define HID_KEYBOARD_SC_KEYPAD_BINARY 0xDA
#define HID_KEYBOARD_SC_KEYPAD_OCTAL 0xDB
#define HID_KEYBOARD_SC_KEYPAD_DECIMAL 0xDC
#define HID_KEYBOARD_SC_KEYPAD_HEXADECIMAL 0xDD
#define HID_KEYBOARD_SC_LEFT_CONTROL 0xE0
#define HID_KEYBOARD_SC_LEFT_SHIFT 0xE1
#define HID_KEYBOARD_SC_LEFT_ALT 0xE2
#define HID_KEYBOARD_SC_LEFT_GUI 0xE3
#define HID_KEYBOARD_SC_RIGHT_CONTROL 0xE4
#define HID_KEYBOARD_SC_RIGHT_SHIFT 0xE5
#define HID_KEYBOARD_SC_RIGHT_ALT 0xE6
#define HID_KEYBOARD_SC_RIGHT_GUI 0xE7
#define HID_KEYBOARD_SC_MEDIA_PLAY 0xE8
#define HID_KEYBOARD_SC_MEDIA_STOP 0xE9
#define HID_KEYBOARD_SC_MEDIA_PREVIOUS_TRACK 0xEA
#define HID_KEYBOARD_SC_MEDIA_NEXT_TRACK 0xEB
#define HID_KEYBOARD_SC_MEDIA_EJECT 0xEC
#define HID_KEYBOARD_SC_MEDIA_VOLUME_UP 0xED
#define HID_KEYBOARD_SC_MEDIA_VOLUME_DOWN 0xEE
#define HID_KEYBOARD_SC_MEDIA_MUTE 0xEF
#define HID_KEYBOARD_SC_MEDIA_WWW 0xF0
#define HID_KEYBOARD_SC_MEDIA_BACKWARD 0xF1
#define HID_KEYBOARD_SC_MEDIA_FORWARD 0xF2
#define HID_KEYBOARD_SC_MEDIA_CANCEL 0xF3
#define HID_KEYBOARD_SC_MEDIA_SEARCH 0xF4
#define HID_KEYBOARD_SC_MEDIA_SLEEP 0xF8
#define HID_KEYBOARD_SC_MEDIA_LOCK 0xF9
#define HID_KEYBOARD_SC_MEDIA_RELOAD 0xFA
#define HID_KEYBOARD_SC_MEDIA_CALCULATOR 0xFB
//@}
/** \name Common HID Device Report Descriptors */
//@{
/** \hideinitializer
* A list of HID report item array elements that describe a typical HID USB Joystick. The resulting report
* descriptor is structured according to the following layout:
*
* \code
* struct
* {
* intA_t X; // Signed X axis value
* intA_t Y; // Signed Y axis value
* intA_t Z; // Signed Z axis value
* uintB_t Buttons; // Pressed buttons bitmask
* } Joystick_Report;
* \endcode
*
* Where \c uintA_t is a type large enough to hold the ranges of the signed \c MinAxisVal and \c MaxAxisVal values,
* and \c intB_t is a type large enough to hold one bit per button.
*
* \param[in] MinAxisVal Minimum logical axis value (16-bit).
* \param[in] MaxAxisVal Maximum logical axis value (16-bit).
* \param[in] MinPhysicalVal Minimum physical axis value, for movement resolution calculations (16-bit).
* \param[in] MaxPhysicalVal Maximum physical axis value, for movement resolution calculations (16-bit).
* \param[in] Buttons Total number of buttons in the device (8-bit).
*/
#define HID_DESCRIPTOR_JOYSTICK(MinAxisVal, MaxAxisVal, MinPhysicalVal, MaxPhysicalVal, Buttons) \
HID_RI_USAGE_PAGE(8, 0x01), \
HID_RI_USAGE(8, 0x04), \
HID_RI_COLLECTION(8, 0x01), \
HID_RI_USAGE(8, 0x01), \
HID_RI_COLLECTION(8, 0x00), \
HID_RI_USAGE(8, 0x30), \
HID_RI_USAGE(8, 0x31), \
HID_RI_USAGE(8, 0x32), \
HID_RI_LOGICAL_MINIMUM(16, MinAxisVal), \
HID_RI_LOGICAL_MAXIMUM(16, MaxAxisVal), \
HID_RI_PHYSICAL_MINIMUM(16, MinPhysicalVal), \
HID_RI_PHYSICAL_MAXIMUM(16, MaxPhysicalVal), \
HID_RI_REPORT_COUNT(8, 3), \
HID_RI_REPORT_SIZE(8, (((MinAxisVal >= -128) && (MaxAxisVal <= 127)) ? 8 : 16)), \
HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE), \
HID_RI_END_COLLECTION(0), \
HID_RI_USAGE_PAGE(8, 0x09), \
HID_RI_USAGE_MINIMUM(8, 0x01), \
HID_RI_USAGE_MAXIMUM(8, Buttons), \
HID_RI_LOGICAL_MINIMUM(8, 0x00), \
HID_RI_LOGICAL_MAXIMUM(8, 0x01), \
HID_RI_REPORT_SIZE(8, 0x01), \
HID_RI_REPORT_COUNT(8, Buttons), \
HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE), \
HID_RI_REPORT_SIZE(8, (Buttons % 8) ? (8 - (Buttons % 8)) : 0), \
HID_RI_REPORT_COUNT(8, 0x01), \
HID_RI_INPUT(8, HID_IOF_CONSTANT), \
HID_RI_END_COLLECTION(0)
/** \hideinitializer
* A list of HID report item array elements that describe a typical HID USB keyboard. The resulting report descriptor
* is compatible with \ref USB_KeyboardReport_Data_t when \c MaxKeys is equal to 6. For other values, the report will
* be structured according to the following layout:
*
* \code
* struct
* {
* uint8_t Modifier; // Keyboard modifier byte indicating pressed modifier keys (\c HID_KEYBOARD_MODIFER_* masks)
* uint8_t Reserved; // Reserved for OEM use, always set to 0.
* uint8_t KeyCode[MaxKeys]; // Length determined by the number of keys that can be reported
* } Keyboard_Report;
* \endcode
*
* \param[in] MaxKeys Number of simultaneous keys that can be reported at the one time (8-bit).
*/
#define HID_DESCRIPTOR_KEYBOARD(MaxKeys) \
HID_RI_USAGE_PAGE(8, 0x01), \
HID_RI_USAGE(8, 0x06), \
HID_RI_COLLECTION(8, 0x01), \
HID_RI_USAGE_PAGE(8, 0x07), \
HID_RI_USAGE_MINIMUM(8, 0xE0), \
HID_RI_USAGE_MAXIMUM(8, 0xE7), \
HID_RI_LOGICAL_MINIMUM(8, 0x00), \
HID_RI_LOGICAL_MAXIMUM(8, 0x01), \
HID_RI_REPORT_SIZE(8, 0x01), \
HID_RI_REPORT_COUNT(8, 0x08), \
HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE), \
HID_RI_REPORT_COUNT(8, 0x01), \
HID_RI_REPORT_SIZE(8, 0x08), \
HID_RI_INPUT(8, HID_IOF_CONSTANT), \
HID_RI_USAGE_PAGE(8, 0x08), \
HID_RI_USAGE_MINIMUM(8, 0x01), \
HID_RI_USAGE_MAXIMUM(8, 0x05), \
HID_RI_REPORT_COUNT(8, 0x05), \
HID_RI_REPORT_SIZE(8, 0x01), \
HID_RI_OUTPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE | HID_IOF_NON_VOLATILE), \
HID_RI_REPORT_COUNT(8, 0x01), \
HID_RI_REPORT_SIZE(8, 0x03), \
HID_RI_OUTPUT(8, HID_IOF_CONSTANT), \
HID_RI_LOGICAL_MINIMUM(8, 0x00), \
HID_RI_LOGICAL_MAXIMUM(16, 0xFF), \
HID_RI_USAGE_PAGE(8, 0x07), \
HID_RI_USAGE_MINIMUM(8, 0x00), \
HID_RI_USAGE_MAXIMUM(8, 0xFF), \
HID_RI_REPORT_COUNT(8, MaxKeys), \
HID_RI_REPORT_SIZE(8, 0x08), \
HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_ARRAY | HID_IOF_ABSOLUTE), \
HID_RI_END_COLLECTION(0)
/** \hideinitializer
* A list of HID report item array elements that describe a typical HID USB mouse. The resulting report descriptor
* is compatible with \ref USB_MouseReport_Data_t if the \c MinAxisVal and \c MaxAxisVal values fit within a \c int8_t range
* and the number of Buttons is less than 8. For other values, the report is structured according to the following layout:
*
* \code
* struct
* {
* uintA_t Buttons; // Pressed buttons bitmask
* intB_t X; // X axis value
* intB_t Y; // Y axis value
* } Mouse_Report;
* \endcode
*
* Where \c intA_t is a type large enough to hold one bit per button, and \c intB_t is a type large enough to hold the
* ranges of the signed \c MinAxisVal and \c MaxAxisVal values.
*
* \param[in] MinAxisVal Minimum X/Y logical axis value (16-bit).
* \param[in] MaxAxisVal Maximum X/Y logical axis value (16-bit).
* \param[in] MinPhysicalVal Minimum X/Y physical axis value, for movement resolution calculations (16-bit).
* \param[in] MaxPhysicalVal Maximum X/Y physical axis value, for movement resolution calculations (16-bit).
* \param[in] Buttons Total number of buttons in the device (8-bit).
* \param[in] AbsoluteCoords Boolean \c true to use absolute X/Y coordinates (e.g. touchscreen).
*/
#define HID_DESCRIPTOR_MOUSE(MinAxisVal, MaxAxisVal, MinPhysicalVal, MaxPhysicalVal, Buttons, AbsoluteCoords) \
HID_RI_USAGE_PAGE(8, 0x01), \
HID_RI_USAGE(8, 0x02), \
HID_RI_COLLECTION(8, 0x01), \
HID_RI_USAGE(8, 0x01), \
HID_RI_COLLECTION(8, 0x00), \
HID_RI_USAGE_PAGE(8, 0x09), \
HID_RI_USAGE_MINIMUM(8, 0x01), \
HID_RI_USAGE_MAXIMUM(8, Buttons), \
HID_RI_LOGICAL_MINIMUM(8, 0x00), \
HID_RI_LOGICAL_MAXIMUM(8, 0x01), \
HID_RI_REPORT_COUNT(8, Buttons), \
HID_RI_REPORT_SIZE(8, 0x01), \
HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE), \
HID_RI_REPORT_COUNT(8, 0x01), \
HID_RI_REPORT_SIZE(8, (Buttons % 8) ? (8 - (Buttons % 8)) : 0), \
HID_RI_INPUT(8, HID_IOF_CONSTANT), \
HID_RI_USAGE_PAGE(8, 0x01), \
HID_RI_USAGE(8, 0x30), \
HID_RI_USAGE(8, 0x31), \
HID_RI_LOGICAL_MINIMUM(16, MinAxisVal), \
HID_RI_LOGICAL_MAXIMUM(16, MaxAxisVal), \
HID_RI_PHYSICAL_MINIMUM(16, MinPhysicalVal), \
HID_RI_PHYSICAL_MAXIMUM(16, MaxPhysicalVal), \
HID_RI_REPORT_COUNT(8, 0x02), \
HID_RI_REPORT_SIZE(8, (((MinAxisVal >= -128) && (MaxAxisVal <= 127)) ? 8 : 16)), \
HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | (AbsoluteCoords ? HID_IOF_ABSOLUTE : HID_IOF_RELATIVE)), \
HID_RI_END_COLLECTION(0), \
HID_RI_END_COLLECTION(0)
/** \hideinitializer
* A list of HID report item array elements that describe a typical Vendor Defined byte array HID report descriptor,
* used for transporting arbitrary data between the USB host and device via HID reports. The resulting report should be
* a \c uint8_t byte array of the specified length in both Device to Host (IN) and Host to Device (OUT) directions.
*
* \param[in] VendorPageNum Vendor Defined HID Usage Page index, ranging from 0x00 to 0xFF.
* \param[in] CollectionUsage Vendor Usage for the encompassing report IN and OUT collection, ranging from 0x00 to 0xFF.
* \param[in] DataINUsage Vendor Usage for the IN report data, ranging from 0x00 to 0xFF.
* \param[in] DataOUTUsage Vendor Usage for the OUT report data, ranging from 0x00 to 0xFF.
* \param[in] NumBytes Length of the data IN and OUT reports.
*/
#define HID_DESCRIPTOR_VENDOR(VendorPageNum, CollectionUsage, DataINUsage, DataOUTUsage, NumBytes) \
HID_RI_USAGE_PAGE(16, (0xFF00 | VendorPageNum)), \
HID_RI_USAGE(8, CollectionUsage), \
HID_RI_COLLECTION(8, 0x01), \
HID_RI_USAGE(8, DataINUsage), \
HID_RI_LOGICAL_MINIMUM(8, 0x00), \
HID_RI_LOGICAL_MAXIMUM(8, 0xFF), \
HID_RI_REPORT_SIZE(8, 0x08), \
HID_RI_REPORT_COUNT(8, NumBytes), \
HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE), \
HID_RI_USAGE(8, DataOUTUsage), \
HID_RI_LOGICAL_MINIMUM(8, 0x00), \
HID_RI_LOGICAL_MAXIMUM(8, 0xFF), \
HID_RI_REPORT_SIZE(8, 0x08), \
HID_RI_REPORT_COUNT(8, NumBytes), \
HID_RI_OUTPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE | HID_IOF_NON_VOLATILE), \
HID_RI_END_COLLECTION(0)
//@}
/* Type Defines: */
/** Enum for possible Class, Subclass and Protocol values of device and interface descriptors relating to the HID
* device class.
*/
enum HID_Descriptor_ClassSubclassProtocol_t
{
HID_CSCP_HIDClass = 0x03, /**< Descriptor Class value indicating that the device or interface
* belongs to the HID class.
*/
HID_CSCP_NonBootSubclass = 0x00, /**< Descriptor Subclass value indicating that the device or interface
* does not implement a HID boot protocol.
*/
HID_CSCP_BootSubclass = 0x01, /**< Descriptor Subclass value indicating that the device or interface
* implements a HID boot protocol.
*/
HID_CSCP_NonBootProtocol = 0x00, /**< Descriptor Protocol value indicating that the device or interface
* does not belong to a HID boot protocol.
*/
HID_CSCP_KeyboardBootProtocol = 0x01, /**< Descriptor Protocol value indicating that the device or interface
* belongs to the Keyboard HID boot protocol.
*/
HID_CSCP_MouseBootProtocol = 0x02, /**< Descriptor Protocol value indicating that the device or interface
* belongs to the Mouse HID boot protocol.
*/
};
/** Enum for the HID class specific control requests that can be issued by the USB bus host. */
enum HID_ClassRequests_t
{
HID_REQ_GetReport = 0x01, /**< HID class-specific Request to get the current HID report from the device. */
HID_REQ_GetIdle = 0x02, /**< HID class-specific Request to get the current device idle count. */
HID_REQ_GetProtocol = 0x03, /**< HID class-specific Request to get the current HID report protocol mode. */
HID_REQ_SetReport = 0x09, /**< HID class-specific Request to set the current HID report to the device. */
HID_REQ_SetIdle = 0x0A, /**< HID class-specific Request to set the device's idle count. */
HID_REQ_SetProtocol = 0x0B, /**< HID class-specific Request to set the current HID report protocol mode. */
};
/** Enum for the HID class specific descriptor types. */
enum HID_DescriptorTypes_t
{
HID_DTYPE_HID = 0x21, /**< Descriptor header type value, to indicate a HID class HID descriptor. */
HID_DTYPE_Report = 0x22, /**< Descriptor header type value, to indicate a HID class HID report descriptor. */
};
/** Enum for the different types of HID reports. */
enum HID_ReportItemTypes_t
{
HID_REPORT_ITEM_In = 0, /**< Indicates that the item is an IN report type. */
HID_REPORT_ITEM_Out = 1, /**< Indicates that the item is an OUT report type. */
HID_REPORT_ITEM_Feature = 2, /**< Indicates that the item is a FEATURE report type. */
};
/** \brief HID class-specific HID Descriptor (LUFA naming conventions).
*
* Type define for the HID class-specific HID descriptor, to describe the HID device's specifications. Refer to the HID
* specification for details on the structure elements.
*
* \see \ref USB_HID_StdDescriptor_HID_t for the version of this type with standard element names.
*
* \note Regardless of CPU architecture, these values should be stored as little endian.
*/
typedef struct
{
USB_Descriptor_Header_t Header; /**< Regular descriptor header containing the descriptor's type and length. */
uint16_t HIDSpec; /**< BCD encoded version that the HID descriptor and device complies to.
*
* \see \ref VERSION_BCD() utility macro.
*/
uint8_t CountryCode; /**< Country code of the localized device, or zero if universal. */
uint8_t TotalReportDescriptors; /**< Total number of HID report descriptors for the interface. */
uint8_t HIDReportType; /**< Type of HID report, set to \ref HID_DTYPE_Report. */
uint16_t HIDReportLength; /**< Length of the associated HID report descriptor, in bytes. */
} ATTR_PACKED USB_HID_Descriptor_HID_t;
/** \brief HID class-specific HID Descriptor (USB-IF naming conventions).
*
* Type define for the HID class-specific HID descriptor, to describe the HID device's specifications. Refer to the HID
* specification for details on the structure elements.
*
* \see \ref USB_HID_Descriptor_HID_t for the version of this type with non-standard LUFA specific
* element names.
*
* \note Regardless of CPU architecture, these values should be stored as little endian.
*/
typedef struct
{
uint8_t bLength; /**< Size of the descriptor, in bytes. */
uint8_t bDescriptorType; /**< Type of the descriptor, either a value in \ref USB_DescriptorTypes_t or a value
* given by the specific class.
*/
uint16_t bcdHID; /**< BCD encoded version that the HID descriptor and device complies to.
*
* \see \ref VERSION_BCD() utility macro.
*/
uint8_t bCountryCode; /**< Country code of the localized device, or zero if universal. */
uint8_t bNumDescriptors; /**< Total number of HID report descriptors for the interface. */
uint8_t bDescriptorType2; /**< Type of HID report, set to \ref HID_DTYPE_Report. */
uint16_t wDescriptorLength; /**< Length of the associated HID report descriptor, in bytes. */
} ATTR_PACKED USB_HID_StdDescriptor_HID_t;
/** \brief Standard HID Boot Protocol Mouse Report.
*
* Type define for a standard Boot Protocol Mouse report
*/
typedef struct
{
uint8_t Button; /**< Button mask for currently pressed buttons in the mouse. */
int8_t X; /**< Current delta X movement of the mouse. */
int8_t Y; /**< Current delta Y movement on the mouse. */
} ATTR_PACKED USB_MouseReport_Data_t;
/** \brief Standard HID Boot Protocol Keyboard Report.
*
* Type define for a standard Boot Protocol Keyboard report
*/
typedef struct
{
uint8_t Modifier; /**< Keyboard modifier byte, indicating pressed modifier keys (a combination of
* \c HID_KEYBOARD_MODIFER_* masks).
*/
uint8_t Reserved; /**< Reserved for OEM use, always set to 0. */
uint8_t KeyCode[6]; /**< Key codes of the currently pressed keys. */
} ATTR_PACKED USB_KeyboardReport_Data_t;
/** Type define for the data type used to store HID report descriptor elements. */
typedef uint8_t USB_Descriptor_HIDReport_Datatype_t;
/* Disable C linkage for C++ Compilers: */
#if defined(__cplusplus)
}
#endif
#endif
/** @} */
/*
LUFA Library
Copyright (C) Dean Camera, 2017.
dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/
/*
Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in
all copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
The author disclaims all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/
#define __INCLUDE_FROM_USB_DRIVER
#include "../../Core/USBMode.h"
#if defined(USB_CAN_BE_DEVICE)
#define __INCLUDE_FROM_HID_DRIVER
#define __INCLUDE_FROM_HID_DEVICE_C
#include "HIDClassDevice.h"
void HID_Device_ProcessControlRequest(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo)
{
if (!(Endpoint_IsSETUPReceived()))
return;
if (USB_ControlRequest.wIndex != HIDInterfaceInfo->Config.InterfaceNumber)
return;
switch (USB_ControlRequest.bRequest)
{
case HID_REQ_GetReport:
if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
{
uint16_t ReportSize = 0;
uint8_t ReportID = (USB_ControlRequest.wValue & 0xFF);
uint8_t ReportType = (USB_ControlRequest.wValue >> 8) - 1;
uint8_t ReportData[HIDInterfaceInfo->Config.PrevReportINBufferSize];
memset(ReportData, 0, sizeof(ReportData));
CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo, &ReportID, ReportType, ReportData, &ReportSize);
if (HIDInterfaceInfo->Config.PrevReportINBuffer != NULL)
{
memcpy(HIDInterfaceInfo->Config.PrevReportINBuffer, ReportData,
HIDInterfaceInfo->Config.PrevReportINBufferSize);
}
Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);
Endpoint_ClearSETUP();
if (ReportID)
Endpoint_Write_8(ReportID);
Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
Endpoint_ClearOUT();
}
break;
case HID_REQ_SetReport:
if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
{
uint16_t ReportSize = USB_ControlRequest.wLength;
uint8_t ReportID = (USB_ControlRequest.wValue & 0xFF);
uint8_t ReportType = (USB_ControlRequest.wValue >> 8) - 1;
uint8_t ReportData[ReportSize];
Endpoint_ClearSETUP();
Endpoint_Read_Control_Stream_LE(ReportData, ReportSize);
Endpoint_ClearIN();
CALLBACK_HID_Device_ProcessHIDReport(HIDInterfaceInfo, ReportID, ReportType,
&ReportData[ReportID ? 1 : 0], ReportSize - (ReportID ? 1 : 0));
}
break;
case HID_REQ_GetProtocol:
if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
{
Endpoint_ClearSETUP();
while (!(Endpoint_IsINReady()));
Endpoint_Write_8(HIDInterfaceInfo->State.UsingReportProtocol);
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
}
break;
case HID_REQ_SetProtocol:
if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
{
Endpoint_ClearSETUP();
Endpoint_ClearStatusStage();
HIDInterfaceInfo->State.UsingReportProtocol = ((USB_ControlRequest.wValue & 0xFF) != 0x00);
}
break;
case HID_REQ_SetIdle:
if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
{
Endpoint_ClearSETUP();
Endpoint_ClearStatusStage();
HIDInterfaceInfo->State.IdleCount = ((USB_ControlRequest.wValue & 0xFF00) >> 6);
}
break;
case HID_REQ_GetIdle:
if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
{
Endpoint_ClearSETUP();
while (!(Endpoint_IsINReady()));
Endpoint_Write_8(HIDInterfaceInfo->State.IdleCount >> 2);
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
}
break;
}
}
bool HID_Device_ConfigureEndpoints(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo)
{
memset(&HIDInterfaceInfo->State, 0x00, sizeof(HIDInterfaceInfo->State));
HIDInterfaceInfo->State.UsingReportProtocol = true;
HIDInterfaceInfo->State.IdleCount = 500;
HIDInterfaceInfo->Config.ReportINEndpoint.Type = EP_TYPE_INTERRUPT;
if (!(Endpoint_ConfigureEndpointTable(&HIDInterfaceInfo->Config.ReportINEndpoint, 1)))
return false;
return true;
}
void HID_Device_USBTask(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo)
{
if (USB_DeviceState != DEVICE_STATE_Configured)
return;
if (HIDInterfaceInfo->State.PrevFrameNum == USB_Device_GetFrameNumber())
{
#if defined(USB_DEVICE_OPT_LOWSPEED)
if (!(USB_Options & USB_DEVICE_OPT_LOWSPEED))
return;
#else
return;
#endif
}
Endpoint_SelectEndpoint(HIDInterfaceInfo->Config.ReportINEndpoint.Address);
if (Endpoint_IsReadWriteAllowed())
{
uint8_t ReportINData[HIDInterfaceInfo->Config.PrevReportINBufferSize];
uint8_t ReportID = 0;
uint16_t ReportINSize = 0;
memset(ReportINData, 0, sizeof(ReportINData));
bool ForceSend = CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo, &ReportID, HID_REPORT_ITEM_In,
ReportINData, &ReportINSize);
bool StatesChanged = false;
bool IdlePeriodElapsed = (HIDInterfaceInfo->State.IdleCount && !(HIDInterfaceInfo->State.IdleMSRemaining));
if (HIDInterfaceInfo->Config.PrevReportINBuffer != NULL)
{
StatesChanged = (memcmp(ReportINData, HIDInterfaceInfo->Config.PrevReportINBuffer, ReportINSize) != 0);
memcpy(HIDInterfaceInfo->Config.PrevReportINBuffer, ReportINData, HIDInterfaceInfo->Config.PrevReportINBufferSize);
}
if (ReportINSize && (ForceSend || StatesChanged || IdlePeriodElapsed))
{
HIDInterfaceInfo->State.IdleMSRemaining = HIDInterfaceInfo->State.IdleCount;
Endpoint_SelectEndpoint(HIDInterfaceInfo->Config.ReportINEndpoint.Address);
if (ReportID)
Endpoint_Write_8(ReportID);
Endpoint_Write_Stream_LE(ReportINData, ReportINSize, NULL);
Endpoint_ClearIN();
}
HIDInterfaceInfo->State.PrevFrameNum = USB_Device_GetFrameNumber();
}
}
#endif
/*
LUFA Library
Copyright (C) Dean Camera, 2017.
dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/
/*
Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in
all copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
The author disclaims all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/
/** \file
* \brief Device mode driver for the library USB HID Class driver.
*
* Device mode driver for the library USB HID Class driver.
*
* \note This file should not be included directly. It is automatically included as needed by the USB module driver
* dispatch header located in LUFA/Drivers/USB.h.
*/
/** \ingroup Group_USBClassHID
* \defgroup Group_USBClassHIDDevice HID Class Device Mode Driver
*
* \section Sec_USBClassHIDDevice_Dependencies Module Source Dependencies
* The following files must be built with any user project that uses this module:
* - LUFA/Drivers/USB/Class/Device/HIDClassDevice.c <i>(Makefile source module name: LUFA_SRC_USBCLASS)</i>
*
* \section Sec_USBClassHIDDevice_ModDescription Module Description
* Device Mode USB Class driver framework interface, for the HID USB Class driver.
*
* @{
*/
#ifndef _HID_CLASS_DEVICE_H_
#define _HID_CLASS_DEVICE_H_
/* Includes: */
#include "../../USB.h"
#include "../Common/HIDClassCommon.h"
/* Enable C linkage for C++ Compilers: */
#if defined(__cplusplus)
extern "C" {
#endif
/* Preprocessor Checks: */
#if !defined(__INCLUDE_FROM_HID_DRIVER)
#error Do not include this file directly. Include LUFA/Drivers/USB.h instead.
#endif
/* Public Interface - May be used in end-application: */
/* Type Defines: */
/** \brief HID Class Device Mode Configuration and State Structure.
*
* Class state structure. An instance of this structure should be made for each HID interface
* within the user application, and passed to each of the HID class driver functions as the
* \c HIDInterfaceInfo parameter. This stores each HID interface's configuration and state information.
*
* \note Due to technical limitations, the HID device class driver does not utilize a separate OUT
* endpoint for host->device communications. Instead, the host->device data (if any) is sent to
* the device via the control endpoint.
*/
typedef struct
{
struct
{
uint8_t InterfaceNumber; /**< Interface number of the HID interface within the device. */
USB_Endpoint_Table_t ReportINEndpoint; /**< Data IN HID report endpoint configuration table. */
void* PrevReportINBuffer; /**< Pointer to a buffer where the previously created HID input report can be
* stored by the driver, for comparison purposes to detect report changes that
* must be sent immediately to the host. This should point to a buffer big enough
* to hold the largest HID input report sent from the HID interface. If this is set
* to \c NULL, it is up to the user to force transfers when needed in the
* \ref CALLBACK_HID_Device_CreateHIDReport() callback function.
*
* \note Due to the single buffer, the internal driver can only correctly compare
* subsequent reports with identical report IDs. In multiple report devices,
* this buffer should be set to \c NULL and the decision to send reports made
* by the user application instead.
*/
uint8_t PrevReportINBufferSize; /**< Size in bytes of the given input report buffer. This is used to create a
* second buffer of the same size within the driver so that subsequent reports
* can be compared. If the user app is to determine when reports are to be sent
* exclusively (i.e. \c PrevReportINBuffer is \c NULL) this value must still be
* set to the size of the largest report the device can issue to the host.
*/
} Config; /**< Config data for the USB class interface within the device. All elements in this section
* <b>must</b> be set or the interface will fail to enumerate and operate correctly.
*/
struct
{
bool UsingReportProtocol; /**< Indicates if the HID interface is set to Boot or Report protocol mode. */
uint16_t PrevFrameNum; /**< Frame number of the previous HID report packet opportunity. */
uint16_t IdleCount; /**< Report idle period, in milliseconds, set by the host. */
uint16_t IdleMSRemaining; /**< Total number of milliseconds remaining before the idle period elapsed - this
* should be decremented by the user application if non-zero each millisecond. */
} State; /**< State data for the USB class interface within the device. All elements in this section
* are reset to their defaults when the interface is enumerated.
*/
} USB_ClassInfo_HID_Device_t;
/* Function Prototypes: */
/** Configures the endpoints of a given HID interface, ready for use. This should be linked to the library
* \ref EVENT_USB_Device_ConfigurationChanged() event so that the endpoints are configured when the configuration
* containing the given HID interface is selected.
*
* \param[in,out] HIDInterfaceInfo Pointer to a structure containing a HID Class configuration and state.
*
* \return Boolean \c true if the endpoints were successfully configured, \c false otherwise.
*/
bool HID_Device_ConfigureEndpoints(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
/** Processes incoming control requests from the host, that are directed to the given HID class interface. This should be
* linked to the library \ref EVENT_USB_Device_ControlRequest() event.
*
* \param[in,out] HIDInterfaceInfo Pointer to a structure containing a HID Class configuration and state.
*/
void HID_Device_ProcessControlRequest(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
/** General management task for a given HID class interface, required for the correct operation of the interface. This should
* be called frequently in the main program loop, before the master USB management task \ref USB_USBTask().
*
* \param[in,out] HIDInterfaceInfo Pointer to a structure containing a HID Class configuration and state.
*/
void HID_Device_USBTask(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
/** HID class driver callback for the user creation of a HID IN report. This callback may fire in response to either
* HID class control requests from the host, or by the normal HID endpoint polling procedure. Inside this callback the
* user is responsible for the creation of the next HID input report to be sent to the host.
*
* \param[in,out] HIDInterfaceInfo Pointer to a structure containing a HID Class configuration and state.
* \param[in,out] ReportID If preset to a non-zero value, this is the report ID being requested by the host. If zero,
* this should be set to the report ID of the generated HID input report (if any). If multiple
* reports are not sent via the given HID interface, this parameter should be ignored.
* \param[in] ReportType Type of HID report to generate, either \ref HID_REPORT_ITEM_In or \ref HID_REPORT_ITEM_Feature.
* \param[out] ReportData Pointer to a buffer where the generated HID report should be stored.
* \param[out] ReportSize Number of bytes in the generated input report, or zero if no report is to be sent.
*
* \return Boolean \c true to force the sending of the report even if it is identical to the previous report and still within
* the idle period (useful for devices which report relative movement), \c false otherwise.
*/
bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
uint8_t* const ReportID,
const uint8_t ReportType,
void* ReportData,
uint16_t* const ReportSize) ATTR_NON_NULL_PTR_ARG(1)
ATTR_NON_NULL_PTR_ARG(2) ATTR_NON_NULL_PTR_ARG(4) ATTR_NON_NULL_PTR_ARG(5);
/** HID class driver callback for the user processing of a received HID OUT report. This callback may fire in response to
* either HID class control requests from the host, or by the normal HID endpoint polling procedure. Inside this callback
* the user is responsible for the processing of the received HID output report from the host.
*
* \param[in,out] HIDInterfaceInfo Pointer to a structure containing a HID Class configuration and state.
* \param[in] ReportID Report ID of the received output report. If multiple reports are not received via the given HID
* interface, this parameter should be ignored.
* \param[in] ReportType Type of received HID report, either \ref HID_REPORT_ITEM_Out or \ref HID_REPORT_ITEM_Feature.
* \param[in] ReportData Pointer to a buffer where the received HID report is stored.
* \param[in] ReportSize Size in bytes of the received report from the host.
*/
void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
const uint8_t ReportID,
const uint8_t ReportType,
const void* ReportData,
const uint16_t ReportSize) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(4);
/* Inline Functions: */
/** Indicates that a millisecond of idle time has elapsed on the given HID interface, and the interface's idle count should be
* decremented. This should be called once per millisecond so that hardware key-repeats function correctly. It is recommended
* that this be called by the \ref EVENT_USB_Device_StartOfFrame() event, once SOF events have been enabled via
* \ref USB_Device_EnableSOFEvents().
*
* \param[in,out] HIDInterfaceInfo Pointer to a structure containing a HID Class configuration and state.
*/
static inline void HID_Device_MillisecondElapsed(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo) ATTR_ALWAYS_INLINE ATTR_NON_NULL_PTR_ARG(1);
static inline void HID_Device_MillisecondElapsed(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo)
{
if (HIDInterfaceInfo->State.IdleMSRemaining)
HIDInterfaceInfo->State.IdleMSRemaining--;
}
/* Disable C linkage for C++ Compilers: */
#if defined(__cplusplus)
}
#endif
#endif
/** @} */
/*
LUFA Library
Copyright (C) Dean Camera, 2016.
dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/
/*
Copyright 2016 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in
all copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
The author disclaims all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/
/** \file
*
* Main source file for Keystrokes. This file contains the main tasks of the
* application and is responsible for the initial application hardware configuration.
*/
#include "main.h"
#include "keys.h"
#include "modifiers.h"
#include "matrix.h"
#include "i2cmaster/i2cmaster.h"
#include "timer.h"
#include "keystrokes.h"
#include "power.h"
#ifdef VIRTUAL_SERIAL_ENABLE
/** LUFA CDC Class driver interface configuration and state information. This structure is
* passed to all CDC Class driver functions, so that multiple instances of the same class
* within a device can be differentiated from one another.
*/
USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
{
.Config =
{
.ControlInterfaceNumber = INTERFACE_ID_CDC_CCI,
.DataINEndpoint =
{
.Address = CDC_TX_EPADDR,
.Size = CDC_TXRX_EPSIZE,
.Banks = 1,
},
.DataOUTEndpoint =
{
.Address = CDC_RX_EPADDR,
.Size = CDC_TXRX_EPSIZE,
.Banks = 1,
},
.NotificationEndpoint =
{
.Address = CDC_NOTIFICATION_EPADDR,
.Size = CDC_NOTIFICATION_EPSIZE,
.Banks = 1,
},
},
};
#endif
/** Buffer to hold the previously generated Keyboard HID report, for comparison purposes inside the HID class driver. */
static uint8_t PrevKeyboardHIDReportBuffer[sizeof(USB_KeyboardReport_Data_t)];
/** Buffer to hold the previously generated Mouse HID report, for comparison purposes inside the HID class driver. */
static uint8_t PrevMouseHIDReportBuffer[sizeof(USB_MouseReport_Data_t)];
static uint8_t PrevEnhancedKeyboardHIDReportBuffer[sizeof(USB_EnhancedKeyboardReport_Data_t)];
static uint8_t KeyboardReportCounter, MouseReportCounter, EnhancedKeyboardReportCounter;
/** LUFA HID Class driver interface configuration and state information. This structure is
* passed to all HID Class driver functions, so that multiple instances of the same class
* within a device can be differentiated from one another. This is for the keyboard HID
* interface within the device.
*/
USB_ClassInfo_HID_Device_t Keyboard_HID_Interface =
{
.Config =
{
.InterfaceNumber = INTERFACE_ID_Keyboard,
.ReportINEndpoint =
{
.Address = KEYBOARD_EPADDR,
.Size = HID_EPSIZE,
.Banks = 1,
},
.PrevReportINBuffer = PrevKeyboardHIDReportBuffer,
.PrevReportINBufferSize = sizeof(PrevKeyboardHIDReportBuffer),
},
};
/** LUFA HID Class driver interface configuration and state information. This structure is
* passed to all HID Class driver functions, so that multiple instances of the same class
* within a device can be differentiated from one another. This is for the mouse HID
* interface within the device.
*/
USB_ClassInfo_HID_Device_t Mouse_HID_Interface =
{
.Config =
{
.InterfaceNumber = INTERFACE_ID_Mouse,
.ReportINEndpoint =
{
.Address = MOUSE_EPADDR,
.Size = HID_EPSIZE,
.Banks = 1,
},
.PrevReportINBuffer = PrevMouseHIDReportBuffer,
.PrevReportINBufferSize = sizeof(PrevMouseHIDReportBuffer),
},
};
USB_ClassInfo_HID_Device_t EnhancedKeyboard_HID_Interface =
{
.Config =
{
.InterfaceNumber = INTERFACE_ID_EnhancedKeyboard,
.ReportINEndpoint =
{
.Address = ENHANCEDKEYBOARD_EPADDR,
.Size = HID_EPSIZE,
.Banks = 1,
},
.PrevReportINBuffer = PrevEnhancedKeyboardHIDReportBuffer,
.PrevReportINBufferSize = sizeof(PrevEnhancedKeyboardHIDReportBuffer),
},
};
/** Main program entry point. This routine contains the overall program flow, including initial
* setup of all components and the main program loop.
*/
int main(void)
{
SetupHardware();
GlobalInterruptEnable();
for (;;)
{
#ifdef VIRTUAL_SERIAL_ENABLE
/* Must throw away unused bytes from the host, or it will lock up while waiting for the device */
CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
#endif
keystrokes_task();
}
}
/** Configures the board hardware and chip peripherals for the demo's functionality. */
void SetupHardware()
{
#if (ARCH == ARCH_AVR8)
/* Disable clock division */
clock_prescale_set(clock_div_1);
MCUCR |= 1 << JTD;
MCUCR |= 1 << JTD;
#elif (ARCH == ARCH_XMEGA)
/* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */
XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);
/* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */
XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);
PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
#endif
/* Hardware Initialization */
power_init();
timer_init();
#ifdef USING_TWI
i2c_init();
#endif
matrix_init();
USB_Init();
}
/** Event handler for the library USB Connection event. */
void EVENT_USB_Device_Connect(void)
{
}
/** Event handler for the library USB Disconnection event. */
void EVENT_USB_Device_Disconnect(void)
{
}
/** Event handler for the library USB Configuration Changed event. */
void EVENT_USB_Device_ConfigurationChanged(void)
{
bool ConfigSuccess = true;
ConfigSuccess &= HID_Device_ConfigureEndpoints(&Keyboard_HID_Interface);
ConfigSuccess &= HID_Device_ConfigureEndpoints(&Mouse_HID_Interface);
ConfigSuccess &= HID_Device_ConfigureEndpoints(&EnhancedKeyboard_HID_Interface);
#ifdef VIRTUAL_SERIAL_ENABLE
ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
#endif
USB_Device_EnableSOFEvents();
}
/** Event handler for the library USB Control Request reception event. */
void EVENT_USB_Device_ControlRequest(void)
{
#ifdef VIRTUAL_SERIAL_ENABLE
CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
#endif
HID_Device_ProcessControlRequest(&Keyboard_HID_Interface);
HID_Device_ProcessControlRequest(&Mouse_HID_Interface);
HID_Device_ProcessControlRequest(&EnhancedKeyboard_HID_Interface);
}
/** Event handler for the USB device Start Of Frame event. */
void EVENT_USB_Device_StartOfFrame(void)
{
HID_Device_MillisecondElapsed(&Keyboard_HID_Interface);
HID_Device_MillisecondElapsed(&Mouse_HID_Interface);
HID_Device_MillisecondElapsed(&EnhancedKeyboard_HID_Interface);
}
/** HID class driver callback function for the creation of HID reports to the host.
*
* \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
* \param[in,out] ReportID Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID
* \param[in] ReportType Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature
* \param[out] ReportData Pointer to a buffer where the created report should be stored
* \param[out] ReportSize Number of bytes written in the report (or zero if no report is to be sent)
*
* \return Boolean \c true to force the sending of the report, \c false to let the library determine if it needs to be sent
*/
bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
uint8_t* const ReportID,
const uint8_t ReportType,
void* ReportData,
uint16_t* const ReportSize)
{
/* Determine which interface must have its report generated */
if (HIDInterfaceInfo == &Keyboard_HID_Interface)
{
USB_KeyboardReport_Data_t* KeyboardReport = (USB_KeyboardReport_Data_t*)ReportData;
KeyboardReport->Modifier = modifiers_get_disguised_modifiers();
memcpy(KeyboardReport->KeyCode, keys_get_scancode(), 6);
*ReportSize = sizeof(USB_KeyboardReport_Data_t);
++KeyboardReportCounter;
}
else if (HIDInterfaceInfo == &Mouse_HID_Interface)
{
USB_MouseReport_Data_t* MouseReport = (USB_MouseReport_Data_t*)ReportData;
*ReportSize = sizeof(USB_MouseReport_Data_t);
++MouseReportCounter;
}
else
{
USB_EnhancedKeyboardReport_Data_t* EnhancedKeyboardReport = (USB_EnhancedKeyboardReport_Data_t*)ReportData;
EnhancedKeyboardReport->ConsumerControlCode = keys_get_multimedia();
EnhancedKeyboardReport->SystemControlCode = keys_get_power_management();
*ReportSize = sizeof(USB_EnhancedKeyboardReport_Data_t);
++EnhancedKeyboardReportCounter;
}
return false;
}
/** HID class driver callback function for the processing of HID reports from the host.
*
* \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
* \param[in] ReportID Report ID of the received report from the host
* \param[in] ReportType The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature
* \param[in] ReportData Pointer to a buffer where the received report has been stored
* \param[in] ReportSize Size in bytes of the received HID report
*/
void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
const uint8_t ReportID,
const uint8_t ReportType,
const void* ReportData,
const uint16_t ReportSize)
{
if (HIDInterfaceInfo == &Keyboard_HID_Interface)
{
uint8_t* LEDReport = (uint8_t*)ReportData;
}
}
/** CDC class driver callback function the processing of changes to the virtual
* control lines sent from the host..
*
* \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced
*/
void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t *const CDCInterfaceInfo)
{
/* You can get changes to the virtual CDC lines in this callback; a common
use-case is to use the Data Terminal Ready (DTR) flag to enable and
disable CDC communications in your application when set to avoid the
application blocking while waiting for a host to become ready and read
in the pending data from the USB endpoints.
*/
bool HostReady = (CDCInterfaceInfo->State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR) != 0;
}
void SendKeyboardReport(void)
{
uint8_t Temp = KeyboardReportCounter;
do
HID_Device_USBTask(&Keyboard_HID_Interface);
while (Temp == KeyboardReportCounter);
}
void SendMouseReport(void)
{
uint8_t Temp = MouseReportCounter;
do
HID_Device_USBTask(&Mouse_HID_Interface);
while (Temp == MouseReportCounter);
}
void SendEnhancedKeyboardReport(void)
{
uint8_t Temp = EnhancedKeyboardReportCounter;
do
HID_Device_USBTask(&EnhancedKeyboard_HID_Interface);
while (Temp == EnhancedKeyboardReportCounter);
}
/*
LUFA Library
Copyright (C) Dean Camera, 2016.
dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/
/*
Copyright 2016 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in
all copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
The author disclaims all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/
/** \file
*
* Header file for main.c.
*/
#ifndef MAIN_H
#define MAIN_H
/* Includes: */
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/power.h>
#include <avr/interrupt.h>
#include <string.h>
#include "descriptors.h"
#include <LUFA/Drivers/USB/USB.h>
#include <LUFA/Platform/Platform.h>
/* Function Prototypes: */
void SetupHardware(void);
void EVENT_USB_Device_Connect(void);
void EVENT_USB_Device_Disconnect(void);
void EVENT_USB_Device_ConfigurationChanged(void);
void EVENT_USB_Device_ControlRequest(void);
void EVENT_USB_Device_StartOfFrame(void);
bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
uint8_t* const ReportID,
const uint8_t ReportType,
void* ReportData,
uint16_t* const ReportSize);
void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
const uint8_t ReportID,
const uint8_t ReportType,
const void* ReportData,
const uint16_t ReportSize);
void SendKeyboardReport(void);
void SendMouseReport(void);
void SendEnhancedKeyboardReport(void);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment