USB for embedded devices - designing a device driver/protocol stack

lordhog picture lordhog · Feb 5, 2009 · Viewed 8.5k times · Source

I have been tasked to write a device driver for an embedded device which will communicate with the micro controller via the SPI interface. Eventually, the USB interface will be used to download updated code externally and used during the verification phase.

My question is, does anyone know of a good reference design or documentation or online tutorial which covers the implementation/design of the USB protocol stack/device driver within an embedded system? I am just starting out and reading through the 650 page USB v2.0 spec is a little daunting at the moment.

Just as a FYI, the micro controller that I am using is a Freescale 9S12.

Mark

Based upon goldenmean's (-AD) comments I wanted to add the following info:

1) The embedded device uses a custom executive and makes no use of a COTS or RTOS.

2) The device will use interrupts to indicate data is ready to be retrieved from the device.

3) I have read through some of the docs regarding Linux, but since I am not at all familiar with Linux it isn't very helpful at the moment (though I am hoping it will be very quickly).

4) The design approach, for now at least, it to write a device driver for the USB device then a USB protocol layer (I/O) would reside on top of the device driver to interpret the data. I would assume this would be the best approach, though I could be wrong.

Edit - A year later

I just wanted to share a few items before they vanish from my mind in case I never work on a USB device again. I ran into a few obstacles when developing code and getting it up and running for the first.

The first problem I ran into was that when the USB device was connected to the Host (Windows in my case) was the host issues a Reset request. The USB device would reset and clear the interrupt enable flags. I didn't read the literature enough to know this was happening, thus I was never receiving the Set-Up Request Interrupt. It took me quite a while to figure this out.

The second problem I ran into was not handling the Set-Up Request for Set_Configuration properly. I was handling it, but I was not processing the request correctly in that the USB device was not sending an ACK when this Set-Up Request came in. I eventually found this out by using a hardware USB protocol analyzer.

There were other issues that I ran into, but these were the two biggest ones that took me quite a while to figure out. The other issue I had to worry about is big-endian and little-endian, Freescale 9S12 vs USB data format (Intel), respectively.

I ended up building the USB device driver similar to UART device drivers I had done in the past. I have posted the code to this at the following URL.

http://lordhog.wordpress.com/2010/12/13/usb-drive

I tend to use structures a lot, so people may not like them since they are not as portal as using #defines (e.g., MAX3420_SETUP_DATA_AVAIL_INT_REQR 0x20), but I like them since it makes the code more readable for me. If anyone has questions regarding it please feel free to e-mail and I can try to give some insight to it. The book "USB Complete: The Developer's Guide" was helpful, so long as you knew what areas to concentrate on. This was a simple application and only used low-speed USB.

Answer

goldenmean picture goldenmean · Feb 5, 2009

While writing a device driver for any interface (USB, Parallel port, etc...) the code needed to be developed would depend upon whether there is any Operating System(OS), RTOS running on that Processor/Micro controller. e.g. if thats going to run say WinCE - It will have its own Driver development Kit , and steps to be followed in the device driver development. Same for any other OS like Linux, symbian.

If its going to be a plain firmware code(No OS) which is going to control the processor/microcontroller, then it's a different situation altogether. So based on either of the above situation u are in, one needs to read & understand:-

1.) The Hardware Specification of the processor/micro controller development board - Register files, ports, memory layout, etc.

2.) USB spec

3.) Couple of pointers i found quickly. Google shud be ur friend! http://www.lrr.in.tum.de/Par/arch/usb/usbdoc/ - Linux USB device driver

http://www.microsoft.com/technet/archive/wce/support/usbce.mspx

-AD