void CDC_Host_USBTask(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo) { if ((USB_HostState != HOST_STATE_Configured) || !(CDCInterfaceInfo->State.IsActive)) return; Pipe_SelectPipe(CDCInterfaceInfo->Config.NotificationPipeNumber); Pipe_Unfreeze(); if (Pipe_IsINReceived()) { USB_Request_Header_t Notification; Pipe_Read_Stream_LE(&Notification, sizeof(USB_Request_Header_t), NO_STREAM_CALLBACK); if ((Notification.bRequest == NOTIF_SerialState) && (Notification.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))) { Pipe_Read_Stream_LE(&CDCInterfaceInfo->State.ControlLineStates.DeviceToHost, sizeof(CDCInterfaceInfo->State.ControlLineStates.DeviceToHost), NO_STREAM_CALLBACK); Pipe_ClearIN(); EVENT_CDC_Host_ControLineStateChanged(CDCInterfaceInfo); } else { Pipe_ClearIN(); } } Pipe_Freeze(); CDC_Host_Flush(CDCInterfaceInfo); }
void CDC_Host_USBTask(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo) { uint8_t portnum = CDCInterfaceInfo->Config.PortNumber; if ((USB_HostState[portnum] != HOST_STATE_Configured) || !(CDCInterfaceInfo->State.IsActive)) return; Pipe_SelectPipe(portnum,CDCInterfaceInfo->Config.NotificationPipeNumber); Pipe_Unfreeze(); if (Pipe_IsINReceived(portnum)) { USB_Request_Header_t Notification; Pipe_Read_Stream_LE(portnum,&Notification, sizeof(USB_Request_Header_t), NULL); if ((Notification.bRequest == CDC_NOTIF_SerialState) && (Notification.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))) { Pipe_Read_Stream_LE(portnum, &CDCInterfaceInfo->State.ControlLineStates.DeviceToHost, sizeof(CDCInterfaceInfo->State.ControlLineStates.DeviceToHost), NULL); Pipe_ClearIN(portnum); EVENT_CDC_Host_ControLineStateChanged(CDCInterfaceInfo); } else { Pipe_ClearIN(portnum); } } Pipe_Freeze(); #if !defined(NO_CLASS_DRIVER_AUTOFLUSH) CDC_Host_Flush(CDCInterfaceInfo); #endif }
/** Main program entry point. This routine configures the hardware required by the application, then * enters a loop to run the application tasks in sequence. */ int main(void) { SetupHardware(); puts_P(PSTR(ESC_FG_CYAN "CDC Host Demo running.\r\n" ESC_FG_WHITE)); LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); for (;;) { switch (USB_HostState) { case HOST_STATE_Addressed: LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING); uint16_t ConfigDescriptorSize; uint8_t ConfigDescriptorData[512]; if (USB_Host_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 (CDC_Host_ConfigurePipes(&VirtualSerial_CDC_Interface, ConfigDescriptorSize, ConfigDescriptorData) != CDC_ENUMERROR_NoError) { printf("Attached Device Not a Valid CDC Class Device.\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; } printf("CDC Device Enumerated.\r\n"); LEDs_SetAllLEDs(LEDMASK_USB_READY); USB_HostState = HOST_STATE_Configured; break; case HOST_STATE_Configured: if (CDC_Host_BytesReceived(&VirtualSerial_CDC_Interface)) { /* Echo received bytes from the attached device through the USART */ while (CDC_Host_BytesReceived(&VirtualSerial_CDC_Interface)) putchar(CDC_Host_ReceiveByte(&VirtualSerial_CDC_Interface)); CDC_Host_Flush(&VirtualSerial_CDC_Interface); } break; } CDC_Host_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); } }