Added new Pipe_IsFrozen() macro to determine if the currently selected pipe is frozen.

Added new USB_GetHIDReportSize() function to the HID report parser to retrieve the size of a given report by its ID.

More additions to the unfinished HID Host Class Driver.
pull/1469/head
Dean Camera 15 years ago
parent cd0adb7574
commit 51566d1a81

@ -524,7 +524,7 @@ void CDC_Task(void)
else if (Command == 'D')
{
/* Read the byte from the endpoint and write it to the EEPROM */
eeprom_write_byte((uint8_t*)(uint16_t)(CurrAddress >> 1), FetchNextCommandByte());
eeprom_write_byte((uint8_t*)((uint16_t)(CurrAddress >> 1)), FetchNextCommandByte());
/* Increment the address after use */
CurrAddress += 2;
@ -535,7 +535,7 @@ void CDC_Task(void)
else if (Command == 'd')
{
/* Read the EEPROM byte and write it to the endpoint */
WriteNextResponseByte(eeprom_read_byte((uint8_t*)(uint16_t)(CurrAddress >> 1)));
WriteNextResponseByte(eeprom_read_byte((uint8_t*)((uint16_t)(CurrAddress >> 1))));
/* Increment the address after use */
CurrAddress += 2;

@ -123,7 +123,7 @@ USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
.Class = 0x03,
.SubClass = 0x00,
.Protocol = 0x00,
.Protocol = HID_NON_BOOT_PROTOCOL,
.InterfaceStrIndex = NO_DESCRIPTOR
},

@ -133,7 +133,7 @@ USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
.Class = 0x03,
.SubClass = 0x00,
.Protocol = 0x00,
.Protocol = HID_NON_BOOT_PROTOCOL,
.InterfaceStrIndex = NO_DESCRIPTOR
},

@ -140,7 +140,7 @@ USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
.Class = 0x03,
.SubClass = 0x01,
.Protocol = 0x01,
.Protocol = HID_BOOT_KEYBOARD_PROTOCOL,
.InterfaceStrIndex = NO_DESCRIPTOR
},

@ -173,7 +173,7 @@ USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
.Class = 0x03,
.SubClass = 0x01,
.Protocol = 0x01,
.Protocol = HID_BOOT_KEYBOARD_PROTOCOL,
.InterfaceStrIndex = NO_DESCRIPTOR
},
@ -210,7 +210,7 @@ USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
.Class = 0x03,
.SubClass = 0x01,
.Protocol = 0x02,
.Protocol = HID_BOOT_MOUSE_PROTOCOL,
.InterfaceStrIndex = NO_DESCRIPTOR
},

@ -133,7 +133,7 @@ USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
.Class = 0x03,
.SubClass = 0x01,
.Protocol = 0x02,
.Protocol = HID_BOOT_MOUSE_PROTOCOL,
.InterfaceStrIndex = NO_DESCRIPTOR
},

@ -47,7 +47,7 @@ USB_ClassInfo_HID_Host_t Mouse_HID_Interface =
.DataINPipeNumber = 1,
.DataOUTPipeNumber = 2,
.HIDInterfaceProtocol = 0x02,
.HIDInterfaceProtocol = HID_BOOT_MOUSE_PROTOCOL,
},
};
@ -110,7 +110,38 @@ int main(void)
printf("Mouse Enumerated.\r\n");
USB_HostState = HOST_STATE_Configured;
break;
case HOST_STATE_Configured:
case HOST_STATE_Configured:
if (HID_Host_IsReportReceived(&Mouse_HID_Interface))
{
USB_MouseReport_Data_t MouseReport;
uint8_t ReportID = 0;
uint8_t LEDMask = LEDS_NO_LEDS;
HID_Host_ReceiveReport(&Mouse_HID_Interface, false, &ReportID, &MouseReport);
/* Alter status LEDs according to mouse X movement */
if (MouseReport.X > 0)
LEDMask |= LEDS_LED1;
else if (MouseReport.X < 0)
LEDMask |= LEDS_LED2;
/* Alter status LEDs according to mouse Y movement */
if (MouseReport.Y > 0)
LEDMask |= LEDS_LED3;
else if (MouseReport.Y < 0)
LEDMask |= LEDS_LED4;
/* Alter status LEDs according to mouse button position */
if (MouseReport.Button)
LEDMask = LEDS_ALL_LEDS;
LEDs_SetAllLEDs(LEDMask);
}
else
{
LEDs_SetAllLEDs(LEDS_NO_LEDS);
}
break;
}

@ -136,6 +136,7 @@ SRC = $(TARGET).c \
$(LUFA_PATH)/LUFA/Drivers/USB/HighLevel/ConfigDescriptor.c \
$(LUFA_PATH)/LUFA/Drivers/USB/Class/Device/HID.c \
$(LUFA_PATH)/LUFA/Drivers/USB/Class/Host/HID.c \
$(LUFA_PATH)/LUFA/Drivers/USB/Class/Host/HIDParser.c \
# List C++ source files here. (C dependencies are automatically generated.)

@ -48,29 +48,44 @@
/* Macros: */
/** HID Class Specific Request to get the current HID report from the device. */
#define REQ_GetReport 0x01
#define REQ_GetReport 0x01
/** HID Class Specific Request to get the current device idle count. */
#define REQ_GetIdle 0x02
#define REQ_GetIdle 0x02
/** HID Class Specific Request to set the current HID report to the device. */
#define REQ_SetReport 0x09
#define REQ_SetReport 0x09
/** HID Class Specific Request to set the device's idle count. */
#define REQ_SetIdle 0x0A
#define REQ_SetIdle 0x0A
/** HID Class Specific Request to get the current HID report protocol mode. */
#define REQ_GetProtocol 0x03
#define REQ_GetProtocol 0x03
/** HID Class Specific Request to set the current HID report protocol mode. */
#define REQ_SetProtocol 0x0B
#define REQ_SetProtocol 0x0B
/** Descriptor header type value, to indicate a HID class HID descriptor. */
#define DTYPE_HID 0x21
#define DTYPE_HID 0x21
/** Descriptor header type value, to indicate a HID class HID report descriptor. */
#define DTYPE_Report 0x22
#define DTYPE_Report 0x22
/** Constant for the protocol value of a HID interface descriptor, indicating that the interface does not support
* any HID class boot protocol (see HID Class Specification).
*/
#define HID_NON_BOOT_PROTOCOL 0x00
/** Constant for the protocol value of a HID interface descriptor, indicating that the interface supports the
* HID class Mouse boot protocol (see HID Class Specification).
*/
#define HID_BOOT_MOUSE_PROTOCOL 0x02
/** Constant for the protocol value of a HID interface descriptor, indicating that the interface supports the
* HID class Keyboard boot protocol (see HID Class Specification).
*/
#define HID_BOOT_KEYBOARD_PROTOCOL 0x01
/* Type Defines: */
/** 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.
@ -88,6 +103,22 @@
uint16_t HIDReportLength;
} USB_HID_Descriptor_t;
/** 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 */
} USB_MouseReport_Data_t;
/** Type define for a standard Boot Protocol Keyboard report */
typedef struct
{
uint8_t Modifier; /**< Keyboard modifier byte, indicating pressed modifier keys (such as Shift, Control, etc.) */
uint8_t Reserved; /**< Reserved for OEM use, always set to 0 */
uint8_t KeyCode; /**< Key code of the currently pressed key */
} 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;

@ -131,7 +131,7 @@
*/
void HID_Device_MillisecondElapsed(USB_ClassInfo_HID_Device_t* HIDInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
/** HID class driver callback for the user creation of a HID input report. This callback may fire in response to either
/** 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.
*
@ -148,7 +148,7 @@
bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo, uint8_t* const ReportID,
void* ReportData, uint16_t* ReportSize) ATTR_NON_NULL_PTR_ARG(1, 2, 3, 4);
/** HID class driver callback for the user processing of a received HID input report. This callback may fire in response to
/** 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.
*

@ -61,7 +61,7 @@ uint8_t HID_Host_ConfigurePipes(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo, uint
(CurrentHIDInterface->Protocol != HIDInterfaceInfo->Config.HIDInterfaceProtocol));
HIDInterfaceInfo->State.InterfaceNumber = CurrentHIDInterface->InterfaceNumber;
HIDInterfaceInfo->State.SupportsBootSubClass = (CurrentHIDInterface->SubClass != 0);
HIDInterfaceInfo->State.SupportsBootProtocol = (CurrentHIDInterface->SubClass != HID_NON_BOOT_PROTOCOL);
if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData, DComp_NextHID) != DESCRIPTOR_SEARCH_COMP_Found)
{
@ -97,6 +97,8 @@ uint8_t HID_Host_ConfigurePipes(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo, uint
EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE);
HIDInterfaceInfo->State.DataOUTPipeSize = EndpointData->EndpointSize;
HIDInterfaceInfo->State.DeviceUsesOUTPipe = true;
FoundEndpoints |= HID_FOUND_DATAPIPE_OUT;
}
}
@ -152,6 +154,97 @@ void HID_Host_USBTask(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo)
}
uint8_t HID_Host_ReceiveReport(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo, bool ControlRequest, uint8_t* ReportID, void* Buffer)
{
if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
return false;
if (ControlRequest)
{
USB_ControlRequest = (USB_Request_Header_t)
{
.bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
.bRequest = REQ_SetReport,
.wValue = *ReportID,
.wIndex = HIDInterfaceInfo->State.InterfaceNumber,
.wLength = USB_GetHIDReportSize(HIDInterfaceInfo->Config.HIDParserData, *ReportID, REPORT_ITEM_TYPE_In),
};
Pipe_SelectPipe(PIPE_CONTROLPIPE);
return USB_Host_SendControlRequest(Buffer);
}
else
{
uint8_t ErrorCode;
Pipe_SelectPipe(HIDInterfaceInfo->Config.DataINPipeNumber);
Pipe_Unfreeze();
uint16_t ReportSize;
if (HIDInterfaceInfo->State.UsingBootProtocol)
{
ReportSize = Pipe_BytesInPipe();
}
else
{
if (HIDInterfaceInfo->Config.HIDParserData->UsingReportIDs)
*ReportID = Pipe_Read_Byte();
else
*ReportID = 0;
ReportSize = USB_GetHIDReportSize(HIDInterfaceInfo->Config.HIDParserData, *ReportID, REPORT_ITEM_TYPE_In);
}
if ((ErrorCode = Pipe_Read_Stream_LE(Buffer, ReportSize, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
return ErrorCode;
Pipe_Freeze();
return PIPE_RWSTREAM_NoError;
}
}
uint8_t HID_Host_SendReport(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo, uint8_t ReportID, void* Buffer, uint16_t ReportSize)
{
if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
return false;
if (HIDInterfaceInfo->State.DeviceUsesOUTPipe)
{
USB_ControlRequest = (USB_Request_Header_t)
{
.bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
.bRequest = REQ_SetReport,
.wValue = ReportID,
.wIndex = HIDInterfaceInfo->State.InterfaceNumber,
.wLength = ReportSize,
};
Pipe_SelectPipe(PIPE_CONTROLPIPE);
return USB_Host_SendControlRequest(Buffer);
}
else
{
uint8_t ErrorCode;
Pipe_SelectPipe(HIDInterfaceInfo->Config.DataOUTPipeNumber);
Pipe_Unfreeze();
if (ReportID)
Pipe_Write_Stream_LE(&ReportID, sizeof(ReportID), NO_STREAM_CALLBACK);
if ((ErrorCode = Pipe_Write_Stream_LE(Buffer, ReportSize, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
return ErrorCode;
Pipe_Freeze();
return PIPE_RWSTREAM_NoError;
}
}
bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo)
{
if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
@ -165,7 +258,7 @@ bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo)
ReportReceived = Pipe_IsReadWriteAllowed();
Pipe_Freeze();
return ReportReceived;
}
@ -174,6 +267,8 @@ uint8_t USB_HID_Host_SetBootProtocol(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo)
if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
return false;
uint8_t ErrorCode;
USB_ControlRequest = (USB_Request_Header_t)
{
.bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
@ -188,7 +283,7 @@ uint8_t USB_HID_Host_SetBootProtocol(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo)
if (!(HIDInterfaceInfo->State.SupportsBootProtocol))
return HID_ERROR_LOGICAL;
if ((ErrorCode = USB_Host_SendControlRequest(HIDReportData)) != HOST_SENDCONTROL_Successful)
if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
return ErrorCode;
HIDInterfaceInfo->State.UsingBootProtocol = true;

@ -73,8 +73,9 @@
uint8_t DataOUTPipeNumber; /**< Pipe number of the HID interface's OUT data pipe */
uint8_t HIDInterfaceProtocol; /**< HID interface protocol value to match against if a specific
* boot subclass protocol is required (e.g. keyboard, mouse), or
* leave as 0 to match against the first HID interface found
* boot subclass protocol is required, either \ref HID_BOOT_MOUSE_PROTOCOL,
* \ref HID_BOOT_KEYBOARD_PROTOCOL or \ref HID_NON_BOOT_PROTOCOL if any
* HID device should be enumerated by the interface
*/
HID_ReportInfo_t* HIDParserData; /**< HID parser data to store the parsed HID report data, when boot protocol
* is not used */
@ -95,6 +96,9 @@
bool SupportsBootProtocol; /**< Indicates if the current interface instance supports the HID Boot
* Protocol when enabled via \ref USB_HID_Host_SetBootProtocol()
*/
bool DeviceUsesOUTPipe; /**< Indicates if the current interface instance uses a seperate OUT data pipe for
* OUT reports, or if OUT reports are sent via the control pipe instead.
*/
bool UsingBootProtocol; /**< Indicates that the interface is currently initialised in Boot Protocol mode */
uint16_t HIDReportSize; /**< Size in bytes of the HID report descriptor in the device */
} State; /**< State data for the USB class interface within the device. All elements in this section
@ -141,14 +145,43 @@
uint8_t HID_Host_ConfigurePipes(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo, uint16_t ConfigDescriptorLength,
uint8_t* DeviceConfigDescriptor) ATTR_NON_NULL_PTR_ARG(1, 3);
/** Determines if a report has been received on the HID interface's IN report pipe, when the device is initialized
* into Report Protocol mode.
/** Receives a HID IN report from the attached HID device, either the next report from the device's IN data pipe,
* or a given report (by Report ID) if a specific report is desired.
*
* \param[in,out] HIDInterfaceInfo Pointer to a structure containing a HID Class host configuration and state
* \param[in] ControlRequest Set to true if the report should be requested by a control request, false otherwise
* \param[in,out] ReportID Report ID of the received report if ControlRequest is false, set by the to the Report ID
* to fetch if ControlRequest is true
* \param[in] Buffer Buffer to store the received report into
*
* \return An error code from the \ref USB_Host_SendControlErrorCodes_t enum if the ControlRequest flag is set,
* a value from the \ref Pipe_Stream_RW_ErrorCodes_t enum otherwise
*/
uint8_t HID_Host_ReceiveReport(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo, bool ControlRequest, uint8_t* ReportID,
void* Buffer) ATTR_NON_NULL_PTR_ARG(1, 3);
/** Sends an OUT report to the currently attached HID device, using the device's OUT pipe if available or the device's
* Control pipe if not.
*
* \param[in,out] HIDInterfaceInfo Pointer to a structure containing a HID Class host configuration and state
* \param[in] ReportID Report ID of the report to send to the device, or 0 if the device does not use report IDs
* \param[in] Buffer Buffer containing the report to send to the attached device
* \param[in] ReportSize Report size in bytes to send to the attached device
*
* \return An error code from the \ref USB_Host_SendControlErrorCodes_t enum if the DeviceUsesOUTPipe flag is set in
* the interface's state structure, a value from the \ref Pipe_Stream_RW_ErrorCodes_t enum otherwise
*/
uint8_t HID_Host_SendReport(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo, uint8_t ReportID,
void* Buffer, uint16_t ReportSize) ATTR_NON_NULL_PTR_ARG(1, 3);
/** Determines if a HID IN report has been received from the attached device on the data IN pipe.
*
* \param[in,out] HIDInterfaceInfo Pointer to a structure containing a HID Class host configuration and state
*
* \return Boolean true if a report has been received, false otherwise
*/
bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
/** Switches the attached HID device's reporting protocol over to the Boot Report protocol mode, on supported devices.
*

@ -342,4 +342,25 @@ void USB_SetHIDReportItemInfo(uint8_t* ReportData, const HID_ReportItem_t* Repor
}
}
uint16_t USB_GetHIDReportSize(HID_ReportInfo_t* const ParserData, uint8_t ReportID, uint8_t ReportType)
{
for (uint8_t i = 0; i < HID_MAX_REPORT_IDS; i++)
{
if (ParserData->ReportIDSizes[i].ReportID == ReportID)
{
switch (ReportType)
{
case REPORT_ITEM_TYPE_In:
return ParserData->ReportIDSizes[i].BitsIn;
case REPORT_ITEM_TYPE_Out:
return ParserData->ReportIDSizes[i].BitsOut;
case REPORT_ITEM_TYPE_Feature:
return ParserData->ReportIDSizes[i].BitsFeature;
}
}
}
return 0;
}
#endif

@ -269,6 +269,18 @@
void USB_SetHIDReportItemInfo(uint8_t* ReportData, const HID_ReportItem_t* ReportItem)
ATTR_NON_NULL_PTR_ARG(1, 2);
/** Retrieves the size of a given HID report in bytes from it's Report ID.
*
* \param[in] ParserData Pointer to a \ref HID_ReportInfo_t instance containing the parser output
* \param[in] ReportID Report ID of the report whose size is to be retrieved
* \param[in] ReportType Type of the report whose size is to be determined, a valued from the
* \ref HID_ReportItemTypes_t enum
*
* \return Size of the report in bytes, or 0 if the report does not exist
*/
uint16_t USB_GetHIDReportSize(HID_ReportInfo_t* const ParserData, uint8_t ReportID,
uint8_t ReportType) ATTR_NON_NULL_PTR_ARG(1);
/** Callback routine for the HID Report Parser. This callback <b>must</b> be implemented by the user code when
* the parser is used, to determine what report IN, OUT and FEATURE item's information is stored into the user
* HID_ReportInfo_t structure. This can be used to filter only those items the application will be using, so that

@ -93,7 +93,7 @@ uint8_t Pipe_WaitUntilReady(void)
#else
uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
#endif
for (;;)
{
if (Pipe_GetPipeToken() == PIPE_TOKEN_IN)

@ -292,6 +292,12 @@
/** Freezes the selected pipe, preventing it from communicating with an attached device. */
static inline void Pipe_Freeze(void);
/** Determines if the currently selected pipe is frozen, and not able to accept data.
*
* \return Boolean true if the currently selected pipe is frozen, false otherwise
*/
static inline bool Pipe_IsFrozen(void);
/** Clears the master pipe error flag. */
static inline void Pipe_ClearError(void);
@ -445,6 +451,8 @@
#define Pipe_Unfreeze() MACROS{ UPCONX &= ~(1 << PFREEZE); }MACROE
#define Pipe_Freeze() MACROS{ UPCONX |= (1 << PFREEZE); }MACROE
#define Pipe_IsFrozen() ((UPCONX & (1 << PFREEZE)) ? true : false)
#define Pipe_ClearError() MACROS{ UPINTX &= ~(1 << PERRI); }MACROE

@ -20,7 +20,9 @@
* - Added extra masks to the SPI driver, changed SPI_Init() so that the clock polarity and sample modes can be set
* - Added new callback to the HID report parser, so that the user application can filter only the items it is interested
* in to be stored into the HIDReportInfo structure to save RAM
* - Added support for the officially recommended layout of the external peripherals connected to the BUMBLEB board
* - Added support for the officially recommended external peripheral layout for the BUMBLEB board (thanks to Dave Fletcher)
* - Added new Pipe_IsFrozen() macro to determine if the currently selected pipe is frozen
* - Added new USB_GetHIDReportSize() function to the HID report parser to retrieve the size of a given report by its ID
*
* <b>Changed:</b>
* - SetIdle requests to the HID device driver with a 0 idle period (send changes only) now only affect the requested

@ -29,7 +29,7 @@
* - ATAVRUSBRF01
*
* Currently supported third-party boards:
* - BUMBLEB (using recommended peripheral layout)
* - BUMBLEB (using officially recommended peripheral layout)
* - Any Other Custom User Boards (with Board Drivers, \see Page_WritingBoardDrivers)
*/
Loading…
Cancel
Save