USB Host code for VS1010C

Designing hardware and software for systems that use the VS1010 MP3 Audio DSP Microcontroller.
Post Reply
User avatar
Panu
VLSI Staff
Posts: 2585
Joined: Tue 2010-06-22 13:43

USB Host code for VS1010C

Post by Panu » Mon 2018-06-25 18:54

Dear VS1010 users,

Here's a first step towards what many have been waiting for: USB host functionality for VS1010. Now, what I have for you today is far from being a complete and finished USB mass storage driver, but it's the first and most difficult step towards it: the capability to power up the USB Host hardware, issue a bus reset and send and receive USB data packets.

This program reads and prints out a Device Descriptor and String Descriptors from a USB device connected to the VS1010 USB port. For example:
VS1010>usbhrd
Relocked (3007)
Issue bus reset

Requesting device descriptor
18 bytes received: 1201000200000040e804231a100101020301

Requesting string descriptor 1
18 bytes received: Samsung

Requesting string descriptor 2
34 bytes received: Mighty Drive

Requesting string descriptor 3
26 bytes received: 076C1991C30E
This program can be used for detecting if there is anything present in the USB host port of the VS1010 and as such, it's quite useful in its own right. But my real reason for releasing it today is because it's such a clean and simple example of how the USB host hardware works. And USB software tends to be complex, for whatever reasons, so I feel that simple examples of working USB software are very valuable indeed.

For example, here is the code to receive a USB datagram (it can be several packets long):

Code: Select all

int UsbReceiveData(register u_int16 addr_ep, register s_int16 bytes, register __i0 u_int16 *dataptr) {
	u_int16 timeout = 128;
	u_int16 totalBytes = 0;
	while(1) {
		// Start USB transaction by sending out a token (OUT or SETUP for outgoing data, IN for incoming data)
		UsbSendToken(addr_ep, HOST_TX_PID(PID_IN)); //Send IN token
	
		// Then wait for a response (DATA, STALL, TIMEOUT, SOF etc)
		if ((UsbWaitFor(USB_ST_RX | USB_ST_SOF | USB_ST_TIME) == USB_ST_RX) // { //Response packet received
		&& ((PERIP(USB_ST) & 0x7) == 3)) { //DATA0 or DATA1 packet has been received
			u_int16 bytesReceived = (USEY(USB_RECV_MEM) & 0x03FF);
			u_int16 lengthWords = (bytesReceived + 1) >> 1;
			totalBytes += bytesReceived;
			bytes -= bytesReceived;
			memcpyYX(dataptr,(__y *)(USB_RECV_MEM+1),lengthWords);
			dataptr += lengthWords;
			if (bytesReceived < ep0size) return totalBytes;
		} else { //something other happened than the reception of a response packet. What exactly? We're not really interested.
			if (--timeout == 0) {
				return S_ERROR;
			}
		}
	}
}
and the main code that reads and prints descriptors:

Code: Select all

	printf("\nRequesting device descriptor\n");
	UsbSendData(USB_DEVICE_ENDPOINT(0,0), HOST_TX_PID(PID_SETUP), 8, "\p\x80\x06\x00\x01\x00\x00\x12\x00"); // Send SETUP query
	bytes = UsbReceiveData(USB_DEVICE_ENDPOINT(0,0), 18, pktBuf); // Get DATA reply
	UsbSendData(USB_DEVICE_ENDPOINT(0,0), HOST_TX_PID(PID_OUT), 0, NULL); // Send zero length packet
	PrintPacketBinary(bytes, pktBuf);
	
	printf("\nRequesting string descriptor 1\n");
	UsbSendData(USB_DEVICE_ENDPOINT(0,0), HOST_TX_PID(PID_SETUP), 8, "\p\x80\x06\x01\x03\x09\x04\xFF\x00"); // Send SETUP query
	bytes = UsbReceiveData(USB_DEVICE_ENDPOINT(0,0), 255, pktBuf); // Get DATA reply
	UsbSendData(USB_DEVICE_ENDPOINT(0,0), HOST_TX_PID(PID_OUT), 0, NULL); // Send zero length packet
	PrintPacketString(bytes, pktBuf);

There are a few oversimplifications in this code. For example, it only communincates with endpoint zero of the device. But these should be evident from the source code. Later revisions will elaborate this code and add the mass storage functionality.

To compile and run this code, you'll need the latest VSIDE with VS1010 libraries updated from this thread: viewtopic.php?f=13&t=680&start=90

Oh, and one final thing. As it turns out, there's a snag. The DP and DN pins are reversed on the USB host connector in the developer board, so you'll need an adapter to change the polarity. We're having some done this week, so you'll get one from VLSI, free of charge, for your developer board if you ask us. And we'll of course fix this when we make a new version of the developer board and until then include the adapter for any new developer board orders.

Let's keep in touch!
-Panu

[Edit] USB Flash Driver released 2018-09-06
Please see viewtopic.php?f=15&t=2309
Attachments
arch-USBHRD-2018-06-25-19-23-PrintSomeDescriptors.zip
VS1010 USB Host source code example, VSIDE Solution. Please use VSIDE from http://www.vsdsp-forum.com/phpbb/viewtopic.php?f=13&t=680&start=90 to compile.
(15.21 KiB) Downloaded 29 times
Info: Line In and Line Out, VS1000 User interface, Overlay howto, Latest VSIDE, MCU Howto, Youtube
Panu-Kristian Poiksalo, VLSI Solution Oy

Post Reply