9 years, 9 months ago.

Simultaneous USB Host and USB device on separate ports

I have a project where I need to use one of the usb ports in Host mode to read data from the device, transform it and send off to computer through another usb port which is configured in device mode. I found EA LPC4088 board and it seems it has everything I need. However due to separate libraries for Host and Device usb modes, whatever I tried I can not make it work. Is it even possible to use two usb ports in the same time? USBD library for device mode on one port and LPCUSBLIB for host functionality on host port? I get HardFault error when I try to combine two example projects usbd_lib_hid_keyboard and lpcusblib_KeyboardHost.

First of all I added interrupt call from LPCUSBLIB to user space:

in file HAL_LPC17xx.c:

extern void extIrqHandler(void);

void USB_IRQHandler(void)
{
	if (USB_CurrentMode[0] == USB_MODE_Host) {
		#if defined(USB_CAN_BE_HOST)
		HcdIrqHandler(0);
		#endif
	}
	extIrqHandler(); //External call on USB interrupt
	if (USB_CurrentMode[0] == USB_MODE_Device) {
		#if defined(USB_CAN_BE_DEVICE)
		DcdIrqHandler(0);
		#endif
	}
}

In my main.c file:

void extIrqHandler(void)
{
	USBD_API->hw->ISR(g_hUsb);
}

And this is my main function. Initialization of the rom api in SetupHardware I select usb device port(2). ZXY100_HID_Interface.Config.PortNumber is set to 0 which is Host port. If I comment ZXY100_host() calls to test usb device my keyboard and mouse are detected by windows. If I only keep ZXY100_host() I get data on serial from the device connected to LPC4088. Give me some tips what is going wrong here.

Cheers.

/* Configures the board hardware and chip peripherals */
static void SetupHardware(void)
{
	SystemCoreClockUpdate();
	Board_Init();
	Chip_USB_Init();
	Board_USBD_Init(2);
	/* Hardware Initialization */
	Board_Debug_Init();
}
int main(void)
{
	USBD_API_INIT_PARAM_T usb_param;
	USB_CORE_DESCS_T desc;
	ErrorCode_t ret = LPC_OK;

	SetupHardware();

	/* initialize call back structures */
	memset((void *) &usb_param, 0, sizeof(USBD_API_INIT_PARAM_T));
	usb_param.usb_reg_base = LPC_USB_BASE + 0x200;
	usb_param.max_num_ep = 4;
	usb_param.mem_base = USB_STACK_MEM_BASE;
	usb_param.mem_size = USB_STACK_MEM_SIZE;

	/* Set the USB descriptors */
	desc.device_desc = (uint8_t *) USB_DeviceDescriptor;
	desc.string_desc = (uint8_t *) USB_StringDescriptor;

	desc.high_speed_desc = USB_FsConfigDescriptor;
	desc.full_speed_desc = USB_FsConfigDescriptor;
	desc.device_qualifier = 0;

	/* USB Initialization */
	ret = USBD_API->hw->Init(&g_hUsb, &desc, &usb_param);
	if (ret == LPC_OK) {

		ret = Mouse_Init(g_hUsb,
			(USB_INTERFACE_DESCRIPTOR *) find_IntfDesc(desc.high_speed_desc,
			0),
			&usb_param.mem_base, &usb_param.mem_size);

		if (ret == LPC_OK) {
			ret = Keyboard_init(g_hUsb,
					(USB_INTERFACE_DESCRIPTOR *) find_IntfDesc(desc.high_speed_desc,
					1),
				&usb_param.mem_base, &usb_param.mem_size);
			if (ret == LPC_OK) {
				/*  enable USB interrupts */
				/* now connect */
				NVIC_EnableIRQ(USB_IRQn);
				USBD_API->hw->Connect(g_hUsb, 1);
				ZXY100_host();
			}
		}
	}

	while (1) {
		if (ret == LPC_OK) {
			ZXY100_host();
		}
		/* Sleep until next IRQ happens */
		__WFI();
	}
}

int ZXY100_host(void)
{
	static int initialized = 0;
		/* Initialize host driver once */
		if (!initialized) {
			DEBUGOUT("ZXY100 Host Demo running.\r\n");
			USB_Init(ZXY100_HID_Interface.Config.PortNumber, USB_MODE_Host);
			initialized = 1;
		} else {

		ZXY100Host_Task();
		HID_Host_USBTask(&ZXY100_HID_Interface);
		USB_USBTask(ZXY100_HID_Interface.Config.PortNumber, USB_MODE_Host);
		}
}

Question relating to:

The mbed-enabled LPC4088 QuickStart Board from Embedded Artists is a easy to use ARM Cortex-M4 rapid prototyping board in a standard through hole DIP package (44-pin), targeted at high-performance as …

I haven't tested to use USB device and host simultaneously for the LPC4088 and have no deep knowledge about the LPCUSBLib. For another project where the LPC2478 was used we used device and host by looking at the USB Interrupt Status register when receiving a USB interrupt. If the USB_HOST_INT bit was set the USB host stack was called otherwise the USB device code.

I don't know if this is possible for LPCUSBLib. Since NXP is responsible for this library it is probably better if you ask the question at www.lpcware.com

posted by EmbeddedArtists AB 25 Jun 2014

Hello YAroslav Were you able to get your application working with both Host and Device mode interfaces on the :LPC4088? I am about to start a project and need to do exactly that also.. I would appreciate any info you may have if got that working ok? Thanks, Louis

posted by Louis Ashford 25 Oct 2014
Be the first to answer this question.