STM32 USB - Page 1
Forum section: electronics / general

author: shangaoren
author_rank: Regular Contributor
timestamp: December 03, 2020, 09:21:52 am
content: Hello,I'm trying to use for the first time the usb device of stm32 (stm32l432) , i'm not familiar with this yet so i give a first try with STM HAL but i'm not a huge fan of this, i would like to know if there is a more proper way to get usb working (is there some libs or i should rebuild all by myself with registers and USB specification ?Best Regards

author: Tagli
author_rank: Contributor
timestamp: December 03, 2020, 03:52:58 pm
content: I prefer avoid using HAL too. I managed to get OTG_FS of STM32F407 working in device mode by pure register level programming. I'm not sure if I have done it properly, but it seems to work so far. I didn't bother with device classes, I configured it as a "Vendor Specific Class" and used it for raw data transfer.Unless you dive into device classes, I think USB 2.0 spec is not so hard do follow and implement. The hard part is the OTG_FS hardware, which feels like a black box. Its FIFO access and chain of interrupts are also difficult to code.Fortunately, if I'm not mistaken, your device has the "other" USB hardware (not the OTG one). I haven't tried this one, but I have read its documentation. I think it's easier to program compared to OTG_FS hardware.BTW, my fist attempt was a C program. Then I rewrote it using C++ & FreeRTOS.

author: pigrew
author_rank: Frequent Contributor
timestamp: December 03, 2020, 04:19:19 pm
content: For STM32, the proper/manufacturer-approved library is HAL and STM32CubeMX.I actually like STM32CubeMX, but do get frustrated with parts of the HAL.The stm32l4 uses the ST USB peripheral, and isn't _that_ complicated.There are a few other USB driver libraries that I know of:TinyUSB, which can be used with HAL. I wrote a USBTMC class driver and the device driverfor it (so I'm biased).libopencm3. This is a library that aims to support many different micros, and does have USB support.Keil MDK is a commercial library.stm32-usbd, if you like Rust

author: mark03
author_rank: Frequent Contributor
timestamp: December 03, 2020, 05:16:37 pm
content: Also check out dmitrystu's USB library on github. One of the best IMO.

author: Tagli
author_rank: Contributor
timestamp: December 03, 2020, 05:36:13 pm
content: dmitrystu's lib was my starting point. But at some point, it got too complicated for me to understand, so I ended up writing my own. Its structure inspired me a lot.

author: GromBeestje
author_rank: Frequent Contributor
timestamp: December 03, 2020, 09:44:50 pm
content: I'm working, on and off, on a level usb implementation, that can run with the STM32Cube HAL ( as-in the git repos ST publishes )As the ST high level USB driver is under a non-free license (SLA044), while the HAL is under BSD license. So, I've been writing some high level code, doing the protocol.

author: Doctorandus_P
author_rank: Super Contributor
timestamp: December 05, 2020, 02:07:48 am
content: I'm with Grombeesje.I don't like the license agreement of that ST stuff much. Don't even want to read that fine print.I have tinkered a bit with USB on STM32, but nothing spectacular.I found the serial_sathosimn project on github, which uses all three serial ports of a "Blue Pill" and makes them available over USB. That's 3 CDC devices on one chip. This project uses libopencm3 for the USB stuff.I have also burned one of the "GRBL" ports for the STM32 into a Blue Pill, and use that for my CNC router. It also worked out of the box for me. Just compiled via the supplied makefile, burned it into a "Blue Pill" and connected the rest. I have not looked much into the software side, I don't even know which library it uses.There are also lots of other USB projects for STM32 on sites like github and gitlab. My advise is to start with an example project that is near to what you want to do with USB, then compile it and try if it works. If it does not work, trow it away and try another project. If you've found a working project, then have a look if the software seems well written. If it's badly written, throw it away and go to the next project. After a few iterations you have a decently written example project that works and upon which you can build your own application.

author: shangaoren
author_rank: Regular Contributor
timestamp: December 07, 2020, 02:36:25 pm
content: Ok, thanks you all for your advices, i will take a look at thoses repos =)

author: grantb5
author_rank: Regular Contributor
timestamp: August 23, 2021, 08:51:46 pm
content: Hi gang,Anything new to report? I looked at the "dmitrystu" libusb_stm32 and was able to get a basic USB device going. This tends to be a LOT smaller code size than the STM HAL which I have played with a little (just made a project in CubeMX, compiled and tested). Both libusb_stm32 and STM32 HAL pass USB chapter 9 and HID tests, so that is good. I think I will try tinyusb and see of I can make that go.Unlike everyone else it seems, I prefer an IDE like Keil or STM/Eclipse. For the two projects above, I had them build-able under both. It's interesting to compare the sizes and for sure Keil is much smaller than what STM32CubeIDE outputs.

author: mck1117
author_rank: Contributor
timestamp: August 24, 2021, 10:43:19 pm
content: ChibiOS also has a pretty top shelf USB driver for the whole STM32 family.

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 25, 2021, 12:29:56 am
content: HAL might use a lot of flash, but time is a very expensive resource for a company.I can get a usb drive working in 2 minutes, the only real work goes in writing 10 lines for FatFS and your're ready for f_open, etc...

author: grantb5
author_rank: Regular Contributor
timestamp: August 25, 2021, 12:57:01 am
content: I had forgotten about the ChibiOS option and will look at that, although it might be a bit more than I can chew at the moment.I don't think the HAL size is just down to the provided USB library. I have found that a lot of code is 25% to 50% smaller if compiled on Keil. I have been working through examples that I can dual-compile with Keil or STM32CubeIDE. Of course optimization plays a part and I haven't been paying close enough attention to say beyond what I have done so far.The HAL does takes a beating on the forums for allegedly doing a lot of work at the ISR level, but again I have no first hand knowledge of that.I am playing with TinyUSB at the moment and I have not ruled out the STM32 HAL either. These two have a wide number of classes supported.Libusb_stm32 requires more work I would say, but once you've beaten the framework into submission it looks not too bad. You are kind of on your own a bit though.