Detailed steps
Before developing the host that supports multiple devices, the user needs to determine:
How many classes this host needs to support.
How many subclasses for every class. For example, the HID class may include HID mouse and HID keyboard.
The code change for the host that supports HID mouse and HID keyboard is similar to that of the host supporting CDC virtual com and HID mouse.
Host event handle function
The USB_HostEvent is a common handle function for attach, unsupported device, enumeration, and detach event. This function needs to call the class-specific event handle function. When the host only supports CDC devices, the USB_HostEvent function is the following:
usb_status_t USB_HostEvent(usb_device_handle deviceHandle,
usb_host_configuration_handle configurationHandle,
uint32_t event_code)
{
usb_status_t status;
status = kStatus_USB_Success;
switch (event_code)
{
case kUSB_HostEventAttach:
status = USB_HostCdcEvent(deviceHandle, configurationHandle, event_code);
/* here add the new device’s event handle function */
break;
case kUSB_HostEventNotSupported:
usb_echo("device not supported.\r\n");
break;
case kUSB_HostEventEnumerationDone:
status = USB_HostCdcEvent(deviceHandle, configurationHandle, event_code);
/* here add the new device’s event handle function */
break;
case kUSB_HostEventDetach:
status = USB_HostCdcEvent(deviceHandle, configurationHandle, event_code);
/* here add the new device’s event handle function */
break;
default:
break;
}
return status;
}
To support other devices, add the corresponding class-specific event handle function. Additionally, it is necessary to add the local variable to receive the return value of every event handle function. The return value of USB_HostEvent should be changed according to the following occasions:
kUSB_HostEventAttach: if the return values for all of the class-specific event handle functions are kUSB_HostEventNotSupported, the return value of USB_HostEvent is kUSB_HostEventNotSupported.
kUSB_HostEventNotSupported: no change.
kUSB_HostEventEnumerationDone: if the return values for all of the class-specific event handle functions are not kStatus_USB_Success, the return value of USB_HostEvent is kStatus_USB_Error.
kUSB_HostEventDetach: if the return values for all of the class-specific event handle functions are not kStatus_USB_Success, the return value of USB_HostEvent is kStatus_USB_Error.
Parent topic:Detailed steps
Class-specific device task
The main function needs to schedule every supported device’s task. If the host only supports CDC devices, the class-specific task in the main function is as follows:
int main(void)
{
BOARD_InitHardware();
APP_init();
while (1)
{
USB_HostTaskFn(g_hostHandle);
/* cdc class task */
USB_HosCdcTask(&g_cdc);
/* here add the new device’s task */
}
}
Parent topic:Detailed steps