example interfacing a STM32F4 with libusb

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome. This forum is NOT for support.
Post Reply
Mathias
Posts: 43
Joined: Thu Jun 28, 2012 6:19 pm

example interfacing a STM32F4 with libusb

Post by Mathias »

I finished my libusb example.
It creates an USB device with two bulk endpoints, one IN and one OUT. (Remember: endpoint direction is always viewed from the host, so an IN endpoint sends data from the device to the host.)
The host programm uses libusb-1.0 to interface the device and alternates between sending and reading a packet.

With libusb you don't need special Drivers to interface your device, it is basically a driver in userspace. Of course you need the permission to access the device, I added a Linux udev rule to give everyone write permission to the device.

I didn't test this on windows, but am very interested on feedback. You may want to read the libusb FAQ. Maybe I will evaluate this (much) later.

git:
https://github.com/Mathias-L/STM32F4-libusb-example
tarball:
https://github.com/Mathias-L/STM32F4-li ... all/master

*edit* Now there is an example using the asynchronous as well as using the synchronous libUSB Api. Also I added an example that reads an ADC and transfers the data to the host PC.
Last edited by Mathias on Sat Aug 18, 2012 11:18 am, edited 1 time in total.
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: example interfacing a STM32F4 with libusb

Post by Giovanni »

Hi Mathias,

Thanks for the example, it is very interesting, I never used libusb myself. Note that currently isochronous endpoints are not yet supported.

Giovanni
Mathias
Posts: 43
Joined: Thu Jun 28, 2012 6:19 pm

Re: example interfacing a STM32F4 with libusb

Post by Mathias »

Ok, no isochronous, I'll keep that in mind.
I have to thank you for this nice OS. Also I was very glad to have the source to ChibiOS. I usually always only understand so much when reading documentation. The source often tells you so much more.
By the way, does ChibiOS support high speed mode?
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: example interfacing a STM32F4 with libusb

Post by Giovanni »

It is the STM32 that usually does not support it, it would require an external PHY, the USB driver itself does not care. Probably some extra settings would be required in the low level driver in order to initialize the PHY.

Giovanni
Mathias
Posts: 43
Joined: Thu Jun 28, 2012 6:19 pm

Re: example interfacing a STM32F4 with libusb

Post by Mathias »

Oh right... and I did consult the datasheet but misread it: "USB2.0 high-speed [...] controller with dedicated DMA", but only "on chip full-speed PHY"...
Abhishek
Posts: 266
Joined: Wed May 23, 2012 3:15 pm

Re: example interfacing a STM32F4 with libusb

Post by Abhishek »

Thanks for sharing Mathias.

Looking forward to trying it out on Windows (LibUsbDotNet) next week.

Abhishek
kjellhar
Posts: 39
Joined: Sun Jul 08, 2012 10:44 pm

Re: example interfacing a STM32F4 with libusb

Post by kjellhar »

Hi Mathias, and thanks for sharing.

I am new at Chibios and STM32F4, but I have done exactly the same with USB before using libusb, AVR32,FreeRTOS and Atmels not too good framework. Not too difficult once you figure out what to do.

I have tried your example on my Mac, and I can't get it to work. The USB device shows up in the system view as not configured, but when I try to connect, it complains that it can't get the device descriptor. When I try to debug it, it seems that it goes into an unhandled exception infinite loop during execution of the line "usbPrepareReceive(usbp, EP_OUT, receiveBuf, 64);"

I'll try and get a Linux VM up and running to see if there is something I'm missing on my Mac, but it really looks like something goes wrong before I even try to connect.

I'll continue to look into this, but any hints or tips is appreciated.

Regards,
Mathias
Posts: 43
Joined: Thu Jun 28, 2012 6:19 pm

Re: example interfacing a STM32F4 with libusb

Post by Mathias »

For some weired reason, I monitor a get a Get Descriptor request for string 0xee (on my Linux box), which I now anser with a "string not found" response. Maybe your mac also asks for String 0xEE (probably due to a still not found bug) and failed on the malformed packet that was send earlier.
Not too difficult once you figure out what to do.

once you figure out what to do ...

*edit*
http://technet.microsoft.com/en-us/query/ff537430
Maybe I should send a stall packet...
Mathias
Posts: 43
Joined: Thu Jun 28, 2012 6:19 pm

Re: example interfacing a STM32F4 with libusb

Post by Mathias »

So I started my windows computer and gave it a try. And surprisingly, it worked without much trouble. I used zadig to install the libusb driver for the device and compiled it against libusbx using mingw. If anyone is interested, I can also upload the exe.

One thing though: It seems that the device doesn't always show up automatically. I think that has something to do, when you connect the microUSB cable after the device powered up. I don't really check if something is connected during initialization, so that probably still has to be fixed. However, what awlays worked was connecting both cables and then pushing the reset button.
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: example interfacing a STM32F4 with libusb

Post by Tectu »

Hey Mathias,

I don't have any experience with USB, but from what I can say, your work is very nice!
Since I never used any USB stuff, I am not sure here: After compiling and flashing the program to the F4 discovery board, plugging it into my linux machine, how can I test / use it?


~ Tectu
Post Reply