Commit 48400df1 authored by Dean Camera's avatar Dean Camera
Browse files

Combined Keyboard, KeyboardViaInt and KeyboardFullInt demos into a single unified demo.

parent 9e9fc97f
......@@ -194,9 +194,9 @@ USB_Descriptor_String_t PROGMEM LanguageString =
*/
USB_Descriptor_String_t PROGMEM ManufacturerString =
{
Header: {Size: USB_STRING_LEN(11), Type: DTYPE_String},
Header: {Size: USB_STRING_LEN(16), Type: DTYPE_String},
UnicodeString: L"Dean Camera"
UnicodeString: L"Denver Gingerich"
};
/** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
......
......@@ -31,7 +31,7 @@
/** \file
*
* Main source file for the Keyboard demo. This file contains the main tasks of the demo and
* Main source file for the KeyboardFullInt demo. This file contains the main tasks of the demo and
* is responsible for the initial application hardware configuration.
*/
......@@ -46,8 +46,13 @@ BUTTLOADTAG(LUFAVersion, "LUFA V" LUFA_VERSION_STRING);
/* Scheduler Task List */
TASK_LIST
{
#if !defined(INTERRUPT_CONTROL_ENDPOINT)
{ Task: USB_USBTask , TaskStatus: TASK_STOP },
#endif
#if !defined(INTERRUPT_DATA_ENDPOINT)
{ Task: USB_Keyboard_Report , TaskStatus: TASK_STOP },
#endif
};
/* Global Variables */
......@@ -69,7 +74,7 @@ uint16_t IdleMSRemaining = 0;
/** Main program entry point. This routine configures the hardware required by the application, then
* starts the scheduler to run the application tasks.
* starts the scheduler to run the USB management task.
*/
int main(void)
{
......@@ -92,7 +97,7 @@ int main(void)
/* Indicate USB not ready */
UpdateStatus(Status_USBNotReady);
/* Initialize Scheduler so that it can be used */
Scheduler_Init();
......@@ -108,29 +113,40 @@ int main(void)
*/
EVENT_HANDLER(USB_Connect)
{
/* Start USB management task */
Scheduler_SetTaskMode(USB_USBTask, TASK_RUN);
/* Indicate USB enumerating */
UpdateStatus(Status_USBEnumerating);
/* Default to report protocol on connect */
UsingReportProtocol = true;
}
/** Event handler for the USB_Reset event. This fires when the USB interface is reset by the USB host, before the
* enumeration process begins, and enables the control endpoint interrupt so that control requests can be handled
* asynchronously when they arrive rather than when the control endpoint is polled manually.
*/
EVENT_HANDLER(USB_Reset)
{
#if defined(INTERRUPT_CONTROL_ENDPOINT)
/* Select the control endpoint */
Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);
/* Enable the endpoint SETUP interrupt ISR for the control endpoint */
USB_INT_Enable(ENDPOINT_INT_SETUP);
#endif
}
/** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
* the status LEDs and stops the USB management and Keyboard reporting tasks.
* the status LEDs.
*/
EVENT_HANDLER(USB_Disconnect)
{
/* Stop running keyboard reporting and USB management tasks */
Scheduler_SetTaskMode(USB_Keyboard_Report, TASK_STOP);
Scheduler_SetTaskMode(USB_USBTask, TASK_STOP);
/* Indicate USB not ready */
UpdateStatus(Status_USBNotReady);
}
/** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
* of the USB device after enumeration - the device endpoints are configured and the keyboard reporting task started.
*/
/** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
* of the USB device after enumeration, and configures the keyboard device endpoints.
*/
EVENT_HANDLER(USB_ConfigurationChanged)
{
/* Setup Keyboard Keycode Report Endpoint */
......@@ -138,19 +154,23 @@ EVENT_HANDLER(USB_ConfigurationChanged)
ENDPOINT_DIR_IN, KEYBOARD_EPSIZE,
ENDPOINT_BANK_SINGLE);
#if defined(INTERRUPT_DATA_ENDPOINT)
/* Enable the endpoint IN interrupt ISR for the report endpoint */
USB_INT_Enable(ENDPOINT_INT_IN);
#endif
/* Setup Keyboard LED Report Endpoint */
Endpoint_ConfigureEndpoint(KEYBOARD_LEDS_EPNUM, EP_TYPE_INTERRUPT,
ENDPOINT_DIR_OUT, KEYBOARD_EPSIZE,
ENDPOINT_BANK_SINGLE);
#if defined(INTERRUPT_DATA_ENDPOINT)
/* Enable the endpoint OUT interrupt ISR for the LED report endpoint */
USB_INT_Enable(ENDPOINT_INT_OUT);
#endif
/* Indicate USB connected and ready */
UpdateStatus(Status_USBReady);
/* Default to report protocol on connect */
UsingReportProtocol = true;
/* Start Keyboard reporting task */
Scheduler_SetTaskMode(USB_Keyboard_Report, TASK_RUN);
}
/** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
......@@ -168,7 +188,7 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
USB_KeyboardReport_Data_t KeyboardReportData;
/* Create the next keyboard report for transmission to the host */
GetNextReport(&KeyboardReportData);
CreateKeyboardReport(&KeyboardReportData);
/* Ignore report type and ID number value */
Endpoint_Discard_Word();
......@@ -226,7 +246,7 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
/* Send the flag to the host */
Endpoint_ClearSetupIN();
/* Acknowledge status stage */
while (!(Endpoint_IsSetupOUTReceived()));
Endpoint_ClearSetupOUT();
......@@ -238,12 +258,12 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
{
/* Read in the wValue parameter containing the new protocol mode */
uint16_t wValue = Endpoint_Read_Word_LE();
Endpoint_ClearSetupReceived();
/* Set or clear the flag depending on what the host indicates that the current Protocol should be */
UsingReportProtocol = (wValue != 0x0000);
/* Acknowledge status stage */
while (!(Endpoint_IsSetupINReady()));
Endpoint_ClearSetupIN();
......@@ -303,7 +323,7 @@ ISR(TIMER0_COMPA_vect, ISR_BLOCK)
*
* \return Boolean true if the new report differs from the last report, false otherwise
*/
bool GetNextReport(USB_KeyboardReport_Data_t* ReportData)
bool CreateKeyboardReport(USB_KeyboardReport_Data_t* ReportData)
{
static uint8_t PrevJoyStatus = 0;
uint8_t JoyStatus_LCL = Joystick_GetStatus();
......@@ -335,13 +355,13 @@ bool GetNextReport(USB_KeyboardReport_Data_t* ReportData)
return InputChanged;
}
/** Processes a given LED report mask from the host and sets the board LEDs to match.
/** Processes a received LED report, and updates the board LEDs states to match.
*
* \param LEDReport LED mask from the host, containing a mask of what LEDs are set
* \param LEDReport LED status report from the host
*/
void ProcessLEDReport(uint8_t LEDReport)
{
uint8_t LEDMask = LEDS_LED2;
uint8_t LEDMask = LEDS_LED2;
if (LEDReport & 0x01) // NUM Lock
LEDMask |= LEDS_LED1;
......@@ -356,10 +376,63 @@ void ProcessLEDReport(uint8_t LEDReport)
LEDs_SetAllLEDs(LEDMask);
}
/** Sends the next HID report to the host, via the keyboard data endpoint. */
static inline void SendNextReport(void)
{
USB_KeyboardReport_Data_t KeyboardReportData;
bool SendReport;
/* Create the next keyboard report for transmission to the host */
SendReport = CreateKeyboardReport(&KeyboardReportData);
/* Check if the idle period is set and has elapsed */
if (IdleCount && !(IdleMSRemaining))
{
/* Idle period elapsed, indicate that a report must be sent */
SendReport = true;
/* Reset the idle time remaining counter, must multiply by 4 to get the duration in milliseconds */
IdleMSRemaining = (IdleCount << 2);
}
/* Select the Keyboard Report Endpoint */
Endpoint_SelectEndpoint(KEYBOARD_EPNUM);
/* Check if Keyboard Endpoint Ready for Read/Write, and if we should send a report */
if (Endpoint_ReadWriteAllowed() && SendReport)
{
/* Write Keyboard Report Data */
Endpoint_Write_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData));
/* Finalize the stream transfer to send the last packet */
Endpoint_ClearCurrentBank();
}
}
/** Reads the next LED status report from the host from the LED data endpoint, if one has been sent. */
static inline void ReceiveNextReport(void)
{
/* Select the Keyboard LED Report Endpoint */
Endpoint_SelectEndpoint(KEYBOARD_LEDS_EPNUM);
/* Check if Keyboard LED Endpoint Ready for Read/Write */
if (!(Endpoint_ReadWriteAllowed()))
return;
/* Read in the LED report from the host */
uint8_t LEDReport = Endpoint_Read_Byte();
/* Handshake the OUT Endpoint - clear endpoint and ready for next report */
Endpoint_ClearCurrentBank();
/* Process the read LED report from the host */
ProcessLEDReport(LEDReport);
}
/** Function to manage status updates to the user. This is done via LEDs on the given board, if available, but may be changed to
* log to a serial port, or anything else that is suitable for status updates.
*
* \param CurrentStatus Current status of the system, from the Keyboard_StatusCodes_t enum
* \param CurrentStatus Current status of the system, from the KeyboardFullInt_StatusCodes_t enum
*/
void UpdateStatus(uint8_t CurrentStatus)
{
......@@ -383,55 +456,77 @@ void UpdateStatus(uint8_t CurrentStatus)
LEDs_SetAllLEDs(LEDMask);
}
#if !defined(INTERRUPT_DATA_ENDPOINT)
/** Function to manage HID report generation and transmission to the host, when in report mode. */
TASK(USB_Keyboard_Report)
{
USB_KeyboardReport_Data_t KeyboardReportData;
bool SendReport;
/* Create the next keyboard report for transmission to the host */
SendReport = GetNextReport(&KeyboardReportData);
/* Check if the idle period is set and has elapsed */
if (IdleCount && !(IdleMSRemaining))
/* Check if the USB system is connected to a host */
if (USB_IsConnected)
{
/* Idle period elapsed, indicate that a report must be sent */
SendReport = true;
/* Send the next keypress report to the host */
SendNextReport();
/* Reset the idle time remaining counter, must multiply by 4 to get the duration in milliseconds */
IdleMSRemaining = (IdleCount << 2);
/* Process the LED report sent from the host */
ReceiveNextReport();
}
/* Check if the USB system is connected to a host */
if (USB_IsConnected)
}
#endif
/** ISR for the general Pipe/Endpoint interrupt vector. This ISR fires when an endpoint's status changes (such as
* a packet has been received) on an endpoint with its corresponding ISR enabling bits set. This is used to send
* HID packets to the host each time the HID interrupt endpoints polling period elapses, as managed by the USB
* controller. It is also used to respond to standard and class specific requests send to the device on the control
* endpoint, by handing them off to the LUFA library when they are received.
*/
ISR(ENDPOINT_PIPE_vect, ISR_BLOCK)
{
#if defined(INTERRUPT_CONTROL_ENDPOINT)
/* Check if the control endpoint has received a request */
if (Endpoint_HasEndpointInterrupted(ENDPOINT_CONTROLEP))
{
/* Clear the endpoint interrupt */
Endpoint_ClearEndpointInterrupt(ENDPOINT_CONTROLEP);
/* Process the control request */
USB_USBTask();
/* Handshake the endpoint setup interrupt - must be after the call to USB_USBTask() */
USB_INT_Clear(ENDPOINT_INT_SETUP);
}
#endif
#if defined(INTERRUPT_DATA_ENDPOINT)
/* Check if keyboard endpoint has interrupted */
if (Endpoint_HasEndpointInterrupted(KEYBOARD_EPNUM))
{
/* Select the Keyboard Report Endpoint */
Endpoint_SelectEndpoint(KEYBOARD_EPNUM);
/* Check if Keyboard Endpoint Ready for Read/Write, and if we should send a report */
if (Endpoint_ReadWriteAllowed() && SendReport)
{
/* Write Keyboard Report Data */
Endpoint_Write_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData));
/* Clear the endpoint IN interrupt flag */
USB_INT_Clear(ENDPOINT_INT_IN);
/* Clear the Keyboard Report endpoint interrupt */
Endpoint_ClearEndpointInterrupt(KEYBOARD_EPNUM);
/* Finalize the stream transfer to send the last packet */
Endpoint_ClearCurrentBank();
}
/* Send the next keypress report to the host */
SendNextReport();
}
/* Check if Keyboard LED status Endpoint has interrupted */
if (Endpoint_HasEndpointInterrupted(KEYBOARD_LEDS_EPNUM))
{
/* Select the Keyboard LED Report Endpoint */
Endpoint_SelectEndpoint(KEYBOARD_LEDS_EPNUM);
/* Check if Keyboard LED Endpoint Ready for Read/Write */
if (Endpoint_ReadWriteAllowed())
{
/* Read in the LED report from the host */
uint8_t LEDStatus = Endpoint_Read_Byte();
/* Clear the endpoint OUT interrupt flag */
USB_INT_Clear(ENDPOINT_INT_OUT);
/* Process the incomming LED report */
ProcessLEDReport(LEDStatus);
/* Clear the Keyboard LED Report endpoint interrupt */
Endpoint_ClearEndpointInterrupt(KEYBOARD_LEDS_EPNUM);
/* Handshake the OUT Endpoint - clear endpoint and ready for next report */
Endpoint_ClearCurrentBank();
}
/* Process the LED report sent from the host */
ReceiveNextReport();
}
#endif
}
......@@ -9,7 +9,7 @@
/*
Copyright 2009 Denver Gingerich (denver [at] ossguy [dot] com)
Based on code by 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
......@@ -33,7 +33,7 @@
*
* Header file for Keyboard.c.
*/
#ifndef _KEYBOARD_H_
#define _KEYBOARD_H_
......@@ -48,14 +48,11 @@
#include "Descriptors.h"
#include <LUFA/Version.h> // Library Version Information
#include <LUFA/Scheduler/Scheduler.h> // Simple scheduler for task management
#include <LUFA/Common/ButtLoadTag.h> // PROGMEM tags readable by the ButtLoad project
#include <LUFA/Drivers/USB/USB.h> // USB Functionality
#include <LUFA/Drivers/Board/Joystick.h> // Joystick driver
#include <LUFA/Drivers/Board/LEDs.h> // LEDs driver
#include <LUFA/Scheduler/Scheduler.h> // Simple scheduler for task management
/* Task Definitions: */
TASK(USB_Keyboard_Report);
/* Macros: */
/** HID Class specific request to get the next HID report from the device. */
......@@ -76,6 +73,9 @@
/** HID Class specific request to set the current HID protocol in use, either report or boot. */
#define REQ_SetProtocol 0x0B
/* Task Definitions: */
TASK(USB_Keyboard_Report);
/* Type Defines: */
/** Type define for the keyboard HID report structure, for creating and sending HID reports to the host PC.
* This mirrors the layout described to the host in the HID report descriptor, in Descriptors.c.
......@@ -103,6 +103,9 @@
/** Indicates that this module will catch the USB_Disconnect event when thrown by the library. */
HANDLES_EVENT(USB_Disconnect);
/** Indicates that this module will catch the USB_Reset event when thrown by the library. */
HANDLES_EVENT(USB_Reset);
/** Indicates that this module will catch the USB_ConfigurationChanged event when thrown by the library. */
HANDLES_EVENT(USB_ConfigurationChanged);
......@@ -110,8 +113,10 @@
HANDLES_EVENT(USB_UnhandledControlPacket);
/* Function Prototypes: */
bool GetNextReport(USB_KeyboardReport_Data_t* ReportData);
bool CreateKeyboardReport(USB_KeyboardReport_Data_t* ReportData);
void ProcessLEDReport(uint8_t LEDReport);
static inline void SendNextReport(void);
static inline void ReceiveNextReport(void);
void UpdateStatus(uint8_t CurrentStatus);
#endif
......@@ -5,18 +5,10 @@
*/
/** \mainpage Keyboard Device Demo
*
* Keyboard demonstration application. This gives a simple reference
* application for implementing a USB Keyboard using the basic USB HID
* drivers in all modern OSes (i.e. no special drivers required). It is
* boot protocol compatible, and thus works under compatible BIOS as if
* it was a native keyboard (e.g. PS/2).
*
* On startup the system will automatically enumerate and function
* as a keyboard when the USB connection to a host is present. To use
* the keyboard example, manipulate the joystick to send the letters
* a, b, c, d and e. See the USB HID documentation for more information
* on sending keyboard event and keypresses.
*
* \section SSec_Info USB Information:
*
* The following table gives a rundown of the USB utilization of this demo.
*
* <table>
* <tr>
......@@ -40,4 +32,42 @@
* <td>Low Speed Mode, Full Speed Mode</td>
* </tr>
* </table>
*
* \section SSec_Description Project Description:
* Keyboard demonstration application. This gives a simple reference application
* for implementing a USB Keyboard using the basic USB HID drivers in all modern
* OSes (i.e. no special drivers required). It is boot protocol compatible, and thus
* works under compatible BIOS as if it was a native keyboard (e.g. PS/2).
*
* On startup the system will automatically enumerate and function
* as a keyboard when the USB connection to a host is present. To use
* the keyboard example, manipulate the joystick to send the letters
* a, b, c, d and e. See the USB HID documentation for more information
* on sending keyboard event and keypresses.
*
* \section SSec_Options Project Options
*
* The following defines can be found in this demo, which can control the demo behaviour when defined, or changed in value.
*
* <table>
* <tr>
* <td><b>Define Name:</b></td>
* <td><b>Location:</b></td>
* <td><b>Description:</b></td>
* </tr>
* <tr>
* <td>INTERRUPT_CONTROL_ENDPOINT</td>
* <td>Makefile CDEFS</td>
* <td>When defined, this causes the demo to enable interrupts for the control endpoint,
* which services control requests from the host. If not defined, the control endpoint
* is serviced via polling using the task scheduler.</td>
* </tr>
* <tr>
* <td>INTERRUPT_DATA_ENDPOINT</td>
* <td>Makefile CDEFS</td>
* <td>When defined, this causes the demo to enable interrupts for the data endpoints,
* which services incomming LED reports and outgoing key status reports to and from the host.
* If not defined, the data endpoints are serviced via polling using the task scheduler.</td>
* </tr>
* </table>
*/
\ No newline at end of file
/*
LUFA Library
Copyright (C) Dean Camera, 2009.
dean [at] fourwalledcubicle [dot] com
www.fourwalledcubicle.com
*/
/*
Copyright 2009 Denver Gingerich (denver [at] ossguy [dot] com)
Based on code by 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
*
* 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.
*/
USB_Descriptor_HIDReport_Datatype_t PROGMEM KeyboardReport[] =
{
0x05, 0x01, /* Usage Page (Generic Desktop) */
0x09, 0x06, /* Usage (Keyboard) */
0xa1, 0x01, /* Collection (Application) */
0x75, 0x01, /* Report Size (1) */
0x95, 0x08, /* Report Count (8) */
0x05, 0x07, /* Usage Page (Key Codes) */
0x19, 0xe0, /* Usage Minimum (Keyboard LeftControl) */
0x29, 0xe7, /* Usage Maximum (Keyboard Right GUI) */
0x15, 0x00, /* Logical Minimum (0) */
0x25, 0x01, /* Logical Maximum (1) */
0x81, 0x02, /* Input (Data, Variable, Absolute) */
0x95, 0x01, /* Report Count (1) */
0x75, 0x08, /* Report Size (8) */
0x81, 0x03, /* Input (Const, Variable, Absolute) */
0x95, 0x05, /* Report Count (5) */
0x75, 0x01, /* Report Size (1) */
0x05, 0x08, /* Usage Page (LEDs) */
0x19, 0x01, /* Usage Minimum (Num Lock) */
0x29, 0x05, /* Usage Maximum (Kana) */
0x91, 0x02, /* Output (Data, Variable, Absolute) */
0x95, 0x01, /* Report Count (1) */
0x75, 0x03, /* Report Size (3) */
0x91, 0x03, /* Output (Const, Variable, Absolute) */
0x95, 0x06, /* Report Count (6) */
0x75, 0x08, /* Report Size (8) */
0x15, 0x00, /* Logical Minimum (0) */
0x25, 0x65, /* Logical Maximum (101) */
0x05, 0x07, /* Usage Page (Keyboard) */
0x19, 0x00, /* Usage Minimum (Reserved (no event indicated)) */
0x29, 0x65, /* Usage Maximum (Keyboard Application) */
0x81, 0x00, /* Input (Data, Array, Absolute) */
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.
*/
USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
{
Header: {Size: sizeof(USB_Descriptor_Device_t), Type: DTYPE_Device},
USBSpecification: VERSION_BCD(01.10),
Class: 0x00,
SubClass: 0x00,
Protocol: 0x00,
Endpoint0Size: 8,
VendorID: 0x03EB,
ProductID: 0x2042,
ReleaseNumber: 0x0000,
ManufacturerStrIndex: 0x01,
ProductStrIndex: 0x02,
SerialNumStrIndex: NO_DESCRIPTOR,
NumberOfConfigurations: 1
};
/** 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.
*/
USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
{
Config:
{
Header: {Size: sizeof(USB_Descriptor_Configuration_Header_t), Type: DTYPE_Configuration},
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)
},
Interface:
{
Header: {Size: sizeof(USB_Descriptor_Interface_t), Type: DTYPE_Interface},
InterfaceNumber: 0x00,
AlternateSetting: 0x00,
TotalEndpoints: 2,
Class: 0x03,
SubClass: 0x01,