Commit fa8d25ef authored by Dean Camera's avatar Dean Camera
Browse files

Remove old OTG demo directory with useless TestApp demo. Add new DualRole...

Remove old OTG demo directory with useless TestApp demo. Add new DualRole directory with new Class Driver powered Mouse dual role demonstration application showing a dual role device using the HID host/device Class drivers.
parent e57e08c6
......@@ -37,6 +37,42 @@
#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.
*/
USB_Descriptor_HIDReport_Datatype_t PROGMEM MouseReport[] =
{
0x05, 0x01, /* Usage Page (Generic Desktop) */
0x09, 0x02, /* Usage (Mouse) */
0xA1, 0x01, /* Collection (Application) */
0x09, 0x01, /* Usage (Pointer) */
0xA1, 0x00, /* Collection (Application) */
0x95, 0x03, /* Report Count (3) */
0x75, 0x01, /* Report Size (1) */
0x05, 0x09, /* Usage Page (Button) */
0x19, 0x01, /* Usage Minimum (Button 1) */
0x29, 0x03, /* Usage Maximum (Button 3) */
0x15, 0x00, /* Logical Minimum (0) */
0x25, 0x01, /* Logical Maximum (1) */
0x81, 0x02, /* Input (Data, Variable, Absolute) */
0x95, 0x01, /* Report Count (1) */
0x75, 0x05, /* Report Size (5) */
0x81, 0x01, /* Input (Constant) */
0x75, 0x08, /* Report Size (8) */
0x95, 0x02, /* Report Count (2) */
0x05, 0x01, /* Usage Page (Generic Desktop Control) */
0x09, 0x30, /* Usage X */
0x09, 0x31, /* Usage Y */
0x15, 0x81, /* Logical Minimum (-127) */
0x25, 0x7F, /* Logical Maximum (127) */
0x81, 0x06, /* Input (Data, Variable, Relative) */
0xC0, /* End Collection */
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
......@@ -50,18 +86,18 @@ USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
.Class = 0x00,
.SubClass = 0x00,
.Protocol = 0x00,
.Endpoint0Size = 8,
.Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
.VendorID = 0x03EB,
.ProductID = 0x2040,
.ProductID = 0x2041,
.ReleaseNumber = 0x0000,
.ManufacturerStrIndex = 0x01,
.ProductStrIndex = 0x02,
.SerialNumStrIndex = NO_DESCRIPTOR,
.NumberOfConfigurations = 1
.NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
};
/** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
......@@ -74,17 +110,15 @@ USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
.Config =
{
.Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
.TotalConfigurationSize = ( sizeof(USB_Descriptor_Configuration_Header_t)
+ sizeof(USB_Descriptor_Interface_t) ),
.TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
.TotalInterfaces = 1,
.ConfigurationNumber = 1,
.ConfigurationStrIndex = NO_DESCRIPTOR,
.ConfigAttributes = (USB_CONFIG_ATTR_BUSPOWERED | USB_CONFIG_ATTR_SELFPOWERED),
.MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
},
......@@ -92,23 +126,44 @@ USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
{
.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
.InterfaceNumber = 1,
.AlternateSetting = 0,
.TotalEndpoints = 0,
.InterfaceNumber = 0x00,
.AlternateSetting = 0x00,
.Class = 0xFF,
.SubClass = 0x00,
.Protocol = 0x00,
.TotalEndpoints = 1,
.Class = 0x03,
.SubClass = 0x01,
.Protocol = HID_BOOT_MOUSE_PROTOCOL,
.InterfaceStrIndex = NO_DESCRIPTOR
},
.MouseHID =
{
.Header = {.Size = sizeof(USB_HID_Descriptor_t), .Type = DTYPE_HID},
.HIDSpec = VERSION_BCD(01.11),
.CountryCode = 0x00,
.TotalReportDescriptors = 1,
.HIDReportType = DTYPE_Report,
.HIDReportLength = sizeof(MouseReport)
},
.MouseEndpoint =
{
.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
.EndpointAddress = (ENDPOINT_DESCRIPTOR_DIR_IN | MOUSE_EPNUM),
.Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
.EndpointSize = MOUSE_EPSIZE,
.PollingIntervalMS = 0x0A
}
};
/** 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.
*/
*/
USB_Descriptor_String_t PROGMEM LanguageString =
{
.Header = {.Size = USB_STRING_LEN(1), .Type = DTYPE_String},
......@@ -133,9 +188,9 @@ USB_Descriptor_String_t PROGMEM ManufacturerString =
*/
USB_Descriptor_String_t PROGMEM ProductString =
{
.Header = {.Size = USB_STRING_LEN(9), .Type = DTYPE_String},
.Header = {.Size = USB_STRING_LEN(15), .Type = DTYPE_String},
.UnicodeString = L"LUFA Demo"
.UnicodeString = L"LUFA Mouse Demo"
};
/** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
......@@ -158,30 +213,39 @@ uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, const uint8_t wIndex,
Address = (void*)&DeviceDescriptor;
Size = sizeof(USB_Descriptor_Device_t);
break;
case DTYPE_Configuration:
case DTYPE_Configuration:
Address = (void*)&ConfigurationDescriptor;
Size = sizeof(USB_Descriptor_Configuration_t);
break;
case DTYPE_String:
case DTYPE_String:
switch (DescriptorNumber)
{
case 0x00:
case 0x00:
Address = (void*)&LanguageString;
Size = pgm_read_byte(&LanguageString.Header.Size);
break;
case 0x01:
case 0x01:
Address = (void*)&ManufacturerString;
Size = pgm_read_byte(&ManufacturerString.Header.Size);
break;
case 0x02:
case 0x02:
Address = (void*)&ProductString;
Size = pgm_read_byte(&ProductString.Header.Size);
break;
}
break;
case DTYPE_HID:
Address = (void*)&ConfigurationDescriptor.MouseHID;
Size = sizeof(USB_HID_Descriptor_t);
break;
case DTYPE_Report:
Address = (void*)&MouseReport;
Size = sizeof(MouseReport);
break;
}
*DescriptorAddress = Address;
*DescriptorAddress = Address;
return Size;
}
......@@ -32,12 +32,13 @@
*
* Header file for Descriptors.c.
*/
#ifndef _DESCRIPTORS_H_
#define _DESCRIPTORS_H_
/* Includes: */
#include <LUFA/Drivers/USB/USB.h>
#include <LUFA/Drivers/USB/Class/HID.h>
#include <avr/pgmspace.h>
......@@ -48,9 +49,18 @@
*/
typedef struct
{
USB_Descriptor_Configuration_Header_t Config; /**< Configuration descriptor header structure */
USB_Descriptor_Interface_t Interface; /**< Interface descriptor, required for the device to enumerate */
USB_Descriptor_Configuration_Header_t Config;
USB_Descriptor_Interface_t Interface;
USB_HID_Descriptor_t MouseHID;
USB_Descriptor_Endpoint_t MouseEndpoint;
} USB_Descriptor_Configuration_t;
/* Macros: */
/** Endpoint number of the Mouse HID reporting IN endpoint. */
#define MOUSE_EPNUM 1
/** Size in bytes of the Mouse HID reporting IN endpoint. */
#define MOUSE_EPSIZE 8
/* Function Prototypes: */
uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, const uint8_t wIndex, void** const DescriptorAddress)
......
/*
LUFA Library
Copyright (C) Dean Camera, 2009.
dean [at] fourwalledcubicle [dot] com
www.fourwalledcubicle.com
*/
/*
Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, and distribute this software
and its documentation for any purpose and without fee is hereby
granted, 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 disclaim 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
*
* Device Mode USB Mouse functionality for the MouseHostDevice demo. This file contains the Device mode
* USB Mouse related code of the demo and is responsible for all the Device mode Mouse functionality.
*/
#include "DeviceFunctions.h"
/** Buffer to hold the previously generated Mouse Device HID report, for comparison purposes inside the HID class driver. */
uint8_t PrevMouseHIDReportBuffer[sizeof(USB_MouseReport_Data_t)];
/** 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.
*/
USB_ClassInfo_HID_Device_t Mouse_HID_Device_Interface =
{
.Config =
{
.InterfaceNumber = 0,
.ReportINEndpointNumber = MOUSE_EPNUM,
.ReportINEndpointSize = MOUSE_EPSIZE,
.PrevReportINBuffer = PrevMouseHIDReportBuffer,
.PrevReportINBufferSize = sizeof(PrevMouseHIDReportBuffer),
},
};
/** Event handler for the library USB WakeUp event. */
void EVENT_USB_Device_Connect(void)
{
LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
}
/** Event handler for the library USB Suspend event. */
void EVENT_USB_Device_Disconnect(void)
{
LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
}
/** Event handler for the library USB Configuration Changed event. */
void EVENT_USB_Device_ConfigurationChanged(void)
{
LEDs_SetAllLEDs(LEDMASK_USB_READY);
if (!(HID_Device_ConfigureEndpoints(&Mouse_HID_Device_Interface)))
LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
USB_Device_EnableSOFEvents();
}
/** Event handler for the library USB Unhandled Control Request event. */
void EVENT_USB_Device_UnhandledControlRequest(void)
{
HID_Device_ProcessControlRequest(&Mouse_HID_Device_Interface);
}
/** Event handler for the USB device Start Of Frame event. */
void EVENT_USB_Device_StartOfFrame(void)
{
HID_Device_MillisecondElapsed(&Mouse_HID_Device_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[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 true to force the sending of the report, 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,
void* ReportData, uint16_t* ReportSize)
{
USB_MouseReport_Data_t* MouseReport = (USB_MouseReport_Data_t*)ReportData;
uint8_t JoyStatus_LCL = Joystick_GetStatus();
uint8_t ButtonStatus_LCL = Buttons_GetStatus();
if (JoyStatus_LCL & JOY_UP)
MouseReport->Y = -1;
else if (JoyStatus_LCL & JOY_DOWN)
MouseReport->Y = 1;
if (JoyStatus_LCL & JOY_RIGHT)
MouseReport->X = 1;
else if (JoyStatus_LCL & JOY_LEFT)
MouseReport->X = -1;
if (JoyStatus_LCL & JOY_PRESS)
MouseReport->Button = (1 << 0);
if (ButtonStatus_LCL & BUTTONS_BUTTON1)
MouseReport->Button |= (1 << 1);
*ReportSize = sizeof(USB_MouseReport_Data_t);
return true;
}
/** 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] ReportData Pointer to a buffer where the created 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 void* ReportData, const uint16_t ReportSize)
{
// Unused (but mandatory for the HID class driver) in this demo, since there are no Host->Device reports
}
\ No newline at end of file
......@@ -30,33 +30,23 @@
/** \file
*
* Header file for TestApp.c.
* Header file for DeviceFunctions.c.
*/
#ifndef _TESTAPP_H_
#define _TESTAPP_H_
#ifndef _MOUSE_DEVICE_FUNCTIONS_H_
#define _MOUSE_DEVICE_FUNCTIONS_H_
/* Includes: */
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <avr/power.h>
#include <LUFA/Version.h>
#include <LUFA/Drivers/USB/USB.h>
#include <LUFA/Drivers/Misc/TerminalCodes.h>
#include <LUFA/Drivers/Peripheral/ADC.h>
#include <LUFA/Drivers/Peripheral/SerialStream.h>
#include <LUFA/Drivers/Board/Joystick.h>
#include <LUFA/Drivers/Board/LEDs.h>
#include <LUFA/Drivers/Board/Buttons.h>
#include <LUFA/Drivers/Board/Temperature.h>
#include "MouseHostDevice.h"
/* External Variables: */
extern USB_ClassInfo_HID_Device_t Mouse_HID_Device_Interface;
/* 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_UnhandledControlRequest(void);
void EVENT_USB_Device_StartOfFrame(void);
void CheckJoystick(void);
void CheckButton(void);
void CheckTemperature(void);
#endif
......@@ -25,7 +25,7 @@ DOXYFILE_ENCODING = UTF-8
# The PROJECT_NAME tag is a single word (or a sequence of words surrounded
# by quotes) that should identify the project.
PROJECT_NAME = "LUFA Library - Test Application Demo"
PROJECT_NAME = "LUFA Library - Mouse Host/Device Dual Role Demo"
# The PROJECT_NUMBER tag can be used to enter a project or revision number.
# This could be handy for archiving the generated documentation or
......
/*
LUFA Library
Copyright (C) Dean Camera, 2009.
dean [at] fourwalledcubicle [dot] com
www.fourwalledcubicle.com
*/
/*
Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, and distribute this software
and its documentation for any purpose and without fee is hereby
granted, 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 disclaim 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
*
* Host Mode USB Mouse functionality for the MouseHostDevice demo. This file contains the Host mode
* USB Mouse related code of the demo and is responsible for all the Host mode Mouse functionality.
*/
#include "HostFunctions.h"
/** 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.
*/
USB_ClassInfo_HID_Host_t Mouse_HID_Host_Interface =
{
.Config =
{
.DataINPipeNumber = 1,
.DataOUTPipeNumber = 2,
.HIDInterfaceProtocol = HID_BOOT_MOUSE_PROTOCOL,
},
};
/** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
* starts the library USB task to begin the enumeration and USB management process.
*/
void EVENT_USB_Host_DeviceAttached(void)
{
puts_P(PSTR("Device Attached.\r\n"));
LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
}
/** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
* stops the library USB task management process.
*/
void EVENT_USB_Host_DeviceUnattached(void)
{
puts_P(PSTR("\r\nDevice Unattached.\r\n"));
LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
}
/** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
* enumerated by the host and is now ready to be used by the application.
*/
void EVENT_USB_Host_DeviceEnumerationComplete(void)
{
LEDs_SetAllLEDs(LEDMASK_USB_READY);
}
/** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
{
USB_ShutDown();
printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"
" -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);
LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
for(;;);
}
/** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
* enumerating an attached USB device.
*/
void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode, const uint8_t SubErrorCode)
{
printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"
" -- Error Code %d\r\n"
" -- Sub Error Code %d\r\n"
" -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState);
LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
}
/** Host state machine task. This task handles the enumeration and control of USB Mice while in USB Host mode,
* setting up the appropriate data pipes and processing reports from the attached device.
*/
void MouseHostTask(void)
{
switch (USB_HostState)
{
case HOST_STATE_Addressed:
LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
uint16_t ConfigDescriptorSize;
uint8_t ConfigDescriptorData[512];
if (USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData,
sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful)
{
printf("Error Retrieving Configuration Descriptor.\r\n");
LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
USB_HostState = HOST_STATE_WaitForDeviceRemoval;
break;
}
if (HID_Host_ConfigurePipes(&Mouse_HID_Host_Interface,
ConfigDescriptorSize, ConfigDescriptorData) != HID_ENUMERROR_NoError)
{
printf("Attached Device Not a Valid Mouse.\r\n");
LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
USB_HostState = HOST_STATE_WaitForDeviceRemoval;
break;
}
if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful)
{
printf("Error Setting Device Configuration.\r\n");
LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
USB_HostState = HOST_STATE_WaitForDeviceRemoval;
break;
}
if (HID_Host_SetBootProtocol(&Mouse_HID_Host_Interface) != 0)
{
printf("Could not Set Boot Protocol Mode.\r\n");
LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
USB_HostState = HOST_STATE_WaitForDeviceRemoval;
break;
}
printf("Mouse Enumerated.\r\n");
USB_HostState = HOST_STATE_Configured;
break;
case HOST_STATE_Configured:
if (HID_Host_IsReportReceived(&Mouse_HID_Host_Interface))
{
uint8_t LEDMask = LEDS_NO_LEDS;
USB_MouseReport_Data_t MouseReport;
HID_Host_ReceiveReport(&Mouse_HID_Host_Interface, &MouseReport);
printf_P(PSTR("dX:%2d dY:%2d Button:%d\r\n"), MouseReport.X,
MouseReport.Y,
MouseReport.Button);
if (MouseReport.X > 0)
LEDMask |= LEDS_LED1;
else if (MouseReport.X < 0)
LEDMask |= LEDS_LED2;
if (MouseReport.Y > 0)