/** Task to manage an enumerated USB CDC device once connected, to print received data * from the device to the serial port. */ void CDCHost_Task(void) { if (USB_HostState != HOST_STATE_Configured) return; if (CDC_Host_BytesReceived(&VirtualSerial_CDC_Interface)) { /* Echo received bytes from the attached device through the USART */ int16_t ReceivedByte = CDC_Host_ReceiveByte(&VirtualSerial_CDC_Interface); if (!(ReceivedByte < 0)) putchar(ReceivedByte); } }
/* CDC application specific task */ static void SerialHost_Task(void) { uint16_t dataLen; uint16_t i, j; if (USB_HostState[Serial_Interface[0].Config.PortNumber] != HOST_STATE_Configured) { return; } /* For each CDC interface check if any data is received, if so read data, modify data and send it back to device. */ for (i = 0; i < serialInfCount; i++) { if((dataLen = CDC_Host_BytesReceived(&Serial_Interface[i])) > 0) { for(j = 0; j < dataLen; j++) { buffer[i][j] = CDC_Host_ReceiveByte(&Serial_Interface[i]); } DEBUGOUT("Received data from interface %d: %s", i, buffer[i]); /* Modify data and send it back to device */ buffer[i][15]++; if(buffer[i][15] >= 0x3A) { buffer[i][15] = 0x30; } CDC_Host_SendData(&Serial_Interface[i], (const uint8_t *)&buffer[i][0], 19); } } }
/** 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(); } }