Page 1 of 12

example interfacing a STM32F4 with libusb

Posted: Sat Jul 07, 2012 2:25 pm
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.

Re: example interfacing a STM32F4 with libusb

Posted: Sat Jul 07, 2012 3:56 pm
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

Re: example interfacing a STM32F4 with libusb

Posted: Sat Jul 07, 2012 4:52 pm
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?

Re: example interfacing a STM32F4 with libusb

Posted: Sat Jul 07, 2012 5:05 pm
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

Re: example interfacing a STM32F4 with libusb

Posted: Sat Jul 07, 2012 5:34 pm
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"...

Re: example interfacing a STM32F4 with libusb

Posted: Sat Jul 07, 2012 6:46 pm
by Abhishek
Thanks for sharing Mathias.

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

Abhishek

Re: example interfacing a STM32F4 with libusb

Posted: Mon Jul 09, 2012 6:46 am
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,

Re: example interfacing a STM32F4 with libusb

Posted: Mon Jul 09, 2012 8:37 pm
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...

Re: example interfacing a STM32F4 with libusb

Posted: Tue Jul 10, 2012 11:35 am
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.

Re: example interfacing a STM32F4 with libusb

Posted: Tue Jul 10, 2012 11:40 am
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