Simplify USB_DeviceCallback() and its surroundings.

This commit is contained in:
László Monda
2016-02-25 18:20:35 +01:00
parent 7e89132fd8
commit 9e52743ba0
3 changed files with 49 additions and 79 deletions

View File

@@ -19,28 +19,16 @@
#include "usb_keyboard_descriptors.h"
#include "usb_mouse_descriptors.h"
void BOARD_InitHardware(void);
static usb_status_t USB_DeviceCallback(usb_device_handle handle, uint32_t event, void *param);
static void USB_DeviceApplicationInit(void);
static usb_device_composite_struct_t UsbDeviceComposite;
static usb_device_composite_struct_t g_UsbDeviceComposite;
usb_device_class_config_struct_t g_CompositeClassConfig[USB_COMPOSITE_INTERFACE_COUNT] = {
{
USB_DeviceHidKeyboardCallback,
(class_handle_t)NULL,
&UsbKeyboardClass,
},
{
USB_DeviceHidMouseCallback,
(class_handle_t)NULL,
&UsbDeviceMouseClass,
}
usb_device_class_config_struct_t UsbDeviceCompositeClassConfig[USB_COMPOSITE_INTERFACE_COUNT] = {
{USB_DeviceHidKeyboardCallback, (class_handle_t)NULL, &UsbKeyboardClass},
{USB_DeviceHidMouseCallback, (class_handle_t)NULL, &UsbDeviceMouseClass}
};
usb_device_class_config_list_struct_t g_UsbDeviceCompositeConfigList = {
g_CompositeClassConfig,
UsbDeviceCompositeClassConfig,
USB_DeviceCallback,
USB_COMPOSITE_INTERFACE_COUNT,
};
@@ -51,83 +39,65 @@ static usb_status_t USB_DeviceCallback(usb_device_handle handle, uint32_t event,
uint16_t *temp16 = (uint16_t *)param;
uint8_t *temp8 = (uint8_t *)param;
if (!param && event != kUSB_DeviceEventBusReset && event != kUSB_DeviceEventSetInterface) {
return error;
}
switch (event) {
case kUSB_DeviceEventBusReset:
g_UsbDeviceComposite.attach = 0U;
UsbDeviceComposite.attach = 0U;
error = kStatus_USB_Success;
break;
case kUSB_DeviceEventSetConfiguration:
if (param) {
g_UsbDeviceComposite.attach = 1U;
g_UsbDeviceComposite.currentConfiguration = *temp8;
USB_DeviceHidMouseSetConfigure(g_UsbDeviceComposite.hidMouseHandle, *temp8);
USB_DeviceHidKeyboardSetConfigure(g_UsbDeviceComposite.hidKeyboardHandle, *temp8);
error = kStatus_USB_Success;
}
UsbDeviceComposite.attach = 1U;
UsbDeviceComposite.currentConfiguration = *temp8;
USB_DeviceHidMouseSetConfigure(UsbDeviceComposite.hidMouseHandle, *temp8);
USB_DeviceHidKeyboardSetConfigure(UsbDeviceComposite.hidKeyboardHandle, *temp8);
error = kStatus_USB_Success;
break;
case kUSB_DeviceEventGetConfiguration:
*temp8 = UsbDeviceComposite.currentConfiguration;
error = kStatus_USB_Success;
break;
case kUSB_DeviceEventSetInterface:
if (g_UsbDeviceComposite.attach) {
if (UsbDeviceComposite.attach) {
uint8_t interface = (uint8_t)((*temp16 & 0xFF00U) >> 0x08U);
uint8_t alternateSetting = (uint8_t)(*temp16 & 0x00FFU);
if (interface < USB_COMPOSITE_INTERFACE_COUNT) {
g_UsbDeviceComposite.currentInterfaceAlternateSetting[interface] = alternateSetting;
USB_DeviceHidMouseSetInterface(g_UsbDeviceComposite.hidMouseHandle, interface, alternateSetting);
USB_DeviceHidKeyboardSetInterface(g_UsbDeviceComposite.hidKeyboardHandle, interface,
UsbDeviceComposite.currentInterfaceAlternateSetting[interface] = alternateSetting;
USB_DeviceHidMouseSetInterface(UsbDeviceComposite.hidMouseHandle, interface, alternateSetting);
USB_DeviceHidKeyboardSetInterface(UsbDeviceComposite.hidKeyboardHandle, interface,
alternateSetting);
error = kStatus_USB_Success;
}
}
break;
case kUSB_DeviceEventGetConfiguration:
if (param) {
*temp8 = g_UsbDeviceComposite.currentConfiguration;
case kUSB_DeviceEventGetInterface: ;
uint8_t interface = (uint8_t)((*temp16 & 0xFF00U) >> 0x08U);
if (interface < USB_COMPOSITE_INTERFACE_COUNT) {
*temp16 = (*temp16 & 0xFF00U) | UsbDeviceComposite.currentInterfaceAlternateSetting[interface];
error = kStatus_USB_Success;
}
break;
case kUSB_DeviceEventGetInterface:
if (param) {
uint8_t interface = (uint8_t)((*temp16 & 0xFF00U) >> 0x08U);
if (interface < USB_COMPOSITE_INTERFACE_COUNT) {
*temp16 = (*temp16 & 0xFF00U) | g_UsbDeviceComposite.currentInterfaceAlternateSetting[interface];
error = kStatus_USB_Success;
} else {
error = kStatus_USB_InvalidRequest;
}
} else {
error = kStatus_USB_InvalidRequest;
}
break;
case kUSB_DeviceEventGetDeviceDescriptor:
if (param) {
error = USB_DeviceGetDeviceDescriptor(handle, (usb_device_get_device_descriptor_struct_t *)param);
}
error = USB_DeviceGetDeviceDescriptor(handle, (usb_device_get_device_descriptor_struct_t *)param);
break;
case kUSB_DeviceEventGetConfigurationDescriptor:
if (param) {
error = USB_DeviceGetConfigurationDescriptor(handle,
(usb_device_get_configuration_descriptor_struct_t *)param);
}
error = USB_DeviceGetConfigurationDescriptor(handle, (usb_device_get_configuration_descriptor_struct_t *)param);
break;
case kUSB_DeviceEventGetStringDescriptor:
if (param) {
error = USB_DeviceGetStringDescriptor(handle, (usb_device_get_string_descriptor_struct_t *)param);
}
error = USB_DeviceGetStringDescriptor(handle, (usb_device_get_string_descriptor_struct_t *)param);
break;
case kUSB_DeviceEventGetHidDescriptor:
if (param) {
error = USB_DeviceGetHidDescriptor(handle, (usb_device_get_hid_descriptor_struct_t *)param);
}
error = USB_DeviceGetHidDescriptor(handle, (usb_device_get_hid_descriptor_struct_t *)param);
break;
case kUSB_DeviceEventGetHidReportDescriptor:
if (param) {
error =
USB_DeviceGetHidReportDescriptor(handle, (usb_device_get_hid_report_descriptor_struct_t *)param);
}
error = USB_DeviceGetHidReportDescriptor(handle, (usb_device_get_hid_report_descriptor_struct_t *)param);
break;
case kUSB_DeviceEventGetHidPhysicalDescriptor:
if (param) {
error = USB_DeviceGetHidPhysicalDescriptor(handle, (usb_device_get_hid_physical_descriptor_struct_t *)param);
}
break;
default:
error = USB_DeviceGetHidPhysicalDescriptor(handle, (usb_device_get_hid_physical_descriptor_struct_t *)param);
break;
}
@@ -136,7 +106,7 @@ static usb_status_t USB_DeviceCallback(usb_device_handle handle, uint32_t event,
void USB0_IRQHandler(void)
{
USB_DeviceKhciIsrFunction(g_UsbDeviceComposite.deviceHandle);
USB_DeviceKhciIsrFunction(UsbDeviceComposite.deviceHandle);
}
static void USB_DeviceApplicationInit(void)
@@ -149,24 +119,24 @@ static void USB_DeviceApplicationInit(void)
CLOCK_EnableUsbfs0Clock(kCLOCK_UsbSrcIrc48M, 48000000U);
// Set composite device to default state.
g_UsbDeviceComposite.speed = USB_SPEED_FULL;
g_UsbDeviceComposite.attach = 0U;
g_UsbDeviceComposite.hidMouseHandle = (class_handle_t)NULL;
g_UsbDeviceComposite.hidKeyboardHandle = (class_handle_t)NULL;
g_UsbDeviceComposite.deviceHandle = NULL;
UsbDeviceComposite.speed = USB_SPEED_FULL;
UsbDeviceComposite.attach = 0U;
UsbDeviceComposite.hidMouseHandle = (class_handle_t)NULL;
UsbDeviceComposite.hidKeyboardHandle = (class_handle_t)NULL;
UsbDeviceComposite.deviceHandle = NULL;
usb_status_t deviceStatus = USB_DeviceClassInit(
CONTROLLER_ID, &g_UsbDeviceCompositeConfigList, &g_UsbDeviceComposite.deviceHandle);
CONTROLLER_ID, &g_UsbDeviceCompositeConfigList, &UsbDeviceComposite.deviceHandle);
if (kStatus_USB_Success != deviceStatus) {
usb_echo("USB device composite demo init failed\r\n");
return;
} else {
usb_echo("USB device composite demo\r\n");
g_UsbDeviceComposite.hidKeyboardHandle = g_UsbDeviceCompositeConfigList.config[0].classHandle;
g_UsbDeviceComposite.hidMouseHandle = g_UsbDeviceCompositeConfigList.config[1].classHandle;
USB_DeviceHidKeyboardInit(&g_UsbDeviceComposite);
USB_DeviceHidMouseInit(&g_UsbDeviceComposite);
UsbDeviceComposite.hidKeyboardHandle = g_UsbDeviceCompositeConfigList.config[0].classHandle;
UsbDeviceComposite.hidMouseHandle = g_UsbDeviceCompositeConfigList.config[1].classHandle;
USB_DeviceHidKeyboardInit(&UsbDeviceComposite);
USB_DeviceHidMouseInit(&UsbDeviceComposite);
}
// Install ISR, set priority, and enable IRQ.
@@ -174,7 +144,7 @@ static void USB_DeviceApplicationInit(void)
NVIC_EnableIRQ((IRQn_Type)irqNumber);
// Start the device function.
USB_DeviceRun(g_UsbDeviceComposite.deviceHandle);
USB_DeviceRun(UsbDeviceComposite.deviceHandle);
}
void main(void)

View File

@@ -165,7 +165,7 @@ uint8_t UsbManufacturerString[USB_MANUFACTURER_STRING_DESCRIPTOR_LENGTH] = {
'.', 0x00U,
};
uint8_t UsbProductString[USB_DESCRIPTOR_LENGTH_STRING2] = {
uint8_t UsbProductString[USB_PRODUCT_STRING_DESCRIPTOR_LENGTH] = {
sizeof(UsbProductString),
USB_DESCRIPTOR_TYPE_STRING,
'C', 0x00U,

View File

@@ -26,7 +26,7 @@
#define USB_DESCRIPTOR_LENGTH_HID (9U)
#define USB_DESCRIPTOR_LENGTH_STRING0 (4U)
#define USB_MANUFACTURER_STRING_DESCRIPTOR_LENGTH (58U)
#define USB_DESCRIPTOR_LENGTH_STRING2 (34U)
#define USB_PRODUCT_STRING_DESCRIPTOR_LENGTH (34U)
#define USB_DEVICE_CONFIGURATION_COUNT (1U)
#define USB_STRING_DESCRIPTOR_COUNT (5U)