Example #1
0
/** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
 *  enumerated by the host and is now ready to be used by the application.
 */
void EVENT_USB_Host_DeviceEnumerationComplete(const uint8_t corenum)
{
	uint16_t ConfigDescriptorSize;
	uint8_t  ConfigDescriptorData[512];
	uint8_t *pCfgDesc;
	uint8_t i;
	uint8_t	err_code;

	/* Read Configuration descriptor */
	if (USB_Host_GetDeviceConfigDescriptor(corenum, 1, &ConfigDescriptorSize, ConfigDescriptorData,
										   sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful) {
		DEBUGOUT("Error Retrieving Configuration Descriptor.\r\n");
		return;
	}
	
	serialInfCount = 0;
	pCfgDesc = ConfigDescriptorData;
	/* Parse configuration descriptor to find CDC interfaces, note that the pointer to the configuration data is changed each time the function is called */
	while((err_code = CDC_Host_FindConfigInterface(&Serial_Interface[serialInfCount], &ConfigDescriptorSize, (void **)&pCfgDesc)) == CDC_ENUMERROR_NoError) {
		serialInfCount++;
		if(serialInfCount >= MAX_SERIAL_INTERFACES) {
			DEBUGOUT("Reached Max Serial Interface of %d\r\n", MAX_SERIAL_INTERFACES);
			break;
		}
	}
	
	if(err_code == CDC_ENUMERROR_PipeConfigurationFailed) {
		DEBUGOUT("Error in Configuring Pipe.\r\n");
		return;
	}
	/* No CDC interface was found */
	if(serialInfCount == 0) {
		DEBUGOUT("Attached Device Not a Valid CDC Device.\r\n");
		return;
	}

	if (USB_Host_SetDeviceConfiguration(Serial_Interface[0].Config.PortNumber, 1) != HOST_SENDCONTROL_Successful) {
		DEBUGOUT("Error Setting Device Configuration.\r\n");
		return;
	}
	
	/* Set line coding for each interface */
	for (i = 0; i < serialInfCount; i++) {
		Serial_Interface[i].State.LineEncoding.BaudRateBPS = 115200;
		Serial_Interface[i].State.LineEncoding.CharFormat = 0;
		Serial_Interface[i].State.LineEncoding.ParityType = 0;
		Serial_Interface[i].State.LineEncoding.DataBits = 8;
		CDC_Host_SetLineEncoding(&Serial_Interface[i]);
	}

	DEBUGOUT("Serial Device Enumerated.\r\n");
}
Example #2
0
/** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
 *  enumerated by the host and is now ready to be used by the application.
 */
void EVENT_USB_Host_DeviceEnumerationComplete(void)
{
	LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);

	uint16_t ConfigDescriptorSize;
	uint8_t  ConfigDescriptorData[512];

	if (USB_Host_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData,
	                                       sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful)
	{
		puts_P(PSTR("Error Retrieving Configuration Descriptor.\r\n"));
		LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
		return;
	}

	if (CDC_Host_ConfigurePipes(&VirtualSerial_CDC_Interface,
	                            ConfigDescriptorSize, ConfigDescriptorData) != CDC_ENUMERROR_NoError)
	{
		puts_P(PSTR("Attached Device Not a Valid CDC Class Device.\r\n"));
		LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
		return;
	}

	if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful)
	{
		puts_P(PSTR("Error Setting Device Configuration.\r\n"));
		LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
		return;
	}

	VirtualSerial_CDC_Interface.State.LineEncoding.BaudRateBPS = 9600;
	VirtualSerial_CDC_Interface.State.LineEncoding.CharFormat  = CDC_LINEENCODING_OneStopBit;
	VirtualSerial_CDC_Interface.State.LineEncoding.ParityType  = CDC_PARITY_None;
	VirtualSerial_CDC_Interface.State.LineEncoding.DataBits    = 8;

	if (CDC_Host_SetLineEncoding(&VirtualSerial_CDC_Interface))
	{
		puts_P(PSTR("Error Setting Device Line Encoding.\r\n"));
		LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
		return;
	}

	puts_P(PSTR("CDC Device Enumerated.\r\n"));
	LEDs_SetAllLEDs(LEDMASK_USB_READY);
}