r/stm32 • • Jan 31 '21

Why is it so difficult to develop USB using HAL libraries

I'm a Python engineer. I wanted to develop hardware, and I wanted to make some creative computer peripherals. However, the library using STM32 is too complex

0 Upvotes

23 comments sorted by

7

u/hawhill Jan 31 '21

What exactly are you asking us for?

7

u/krgoodwin Jan 31 '21

They're not asking for anything. Seems to just be a statement on the complexity of the HAL.

8

u/orclev Jan 31 '21

I imagine he's probably looking for an experience more like arduino where they've wrapped up all the low level register access and bit twiddling behind a high level API that's designed around accomplishing specific limited abilities (I2C, SIO, toggling a GPIO pin, etc.).

What I can say from experience, is that acquiring and working with the STM32 SDK is an exercise in serial annoyances. While the HAL itself might be perfectly fine, the build system around it feels positively arcane and archaic by modern sensibilities. I was fortunate in that my target language was Rust so I could bypass essentially all of the C based SDK in favor of the excellent cortex-m crates, but my initial attempts that were aimed at getting the C SDK up and running were very annoying and confusing.

1

u/johhuang12138 Feb 01 '21

I imagine he's probably looking for an experience more like arduino where they've wrapped up all the low level register access and bit twiddling behind a high level API that's designed around accomplishing specific limited abilities (I2C, SIO, toggling a GPIO pin, etc.).

This expresses exactly what I think

1

u/orclev Feb 01 '21 edited Feb 01 '21

For better or worse Arduino is more the exception than the rule when it comes to that level of abstraction in the embedded space. Understand that that kind of a library can be built, but it exists at a level higher than a HAL. HAL is the bones something like that is built on top of.

I think some of that does exist in the STM32 C SDK, but it's all done with a very convoluted code generation system that creates it as a wrapper around the HAL.

It's important to understand the target audiences at play here as well. The entire goal of Arduino was to make a super approachable low cost microcontroller that a "average" person with even limited programming experience would be able to work with. As such a great deal of effort was put into making its tooling dead simple, and to provide a plethora of easy to follow tutorials. STM32 on the other hand is aimed primarily at commercial developers looking to integrate a simple but powerful microcontroller into their products. It's expected your average STM32 dev is going to be a professional with years of experience working in the embedded development space. That is changing somewhat with the emergence of devices like the bluepill and the new RP2040 (which incidentally might provide an experience closer to what you're looking for), but it's still early days for that and most of the tools out there are still the crufty arcane ones that were aimed at the professional audience and put ease of use somewhere around the bottom in terms of their priorities.

The good news is, while most of the C tooling is frankly appalling, there's a lot of work being done in other languages to provide a far more approachable STM32 dev experience. I personally am most familiar with the Rust libraries, but I believe there are also builds of embedded versions of Python, and I think Lua and possibly Javascript that are available and targeted at STM32 devices. You will likely find those libraries much closer to the sort of experience you're looking for.

Edit: thinking on it more, in the C space you've got a couple options.

  1. you can try to get the STM32 SDK to build, although honestly it's arcane enough that it might be best to skip that.

  2. assuming you do get the SDK working you could either build your own USB library on top of the HAL, or more sanely there are a couple C USB libraries out there that you could use without too much trouble.

  3. possibly the best idea would be to do what projects like QMK do, pull in a realtime OS like ChibiOS or FreeRTOS. They often have embedded copies of the necessary SDK files and they've done all the legwork of getting makefiles and such setup to produce a working bin ready to flash. Even better they provide the kind of high level API that abstracts over things like USB.

1

u/johhuang12138 Feb 01 '21

I totally agree with you that STM32 is for a different demographic than Arduion.To be honest, I also enjoy being directly in front of the low level hardware. It was cool, but complicated

1

u/johhuang12138 Feb 01 '21

possibly the best idea would be to do what projects like QMK do, pull in a realtime OS like ChibiOS or FreeRTOS. They often have embedded copies of the necessary SDK files and they've done all the legwork of getting makefiles and such setup to produce a working bin ready to flash. Even better they provide the kind of high level API that abstracts over things like USB.

Thanks for your advice. It seems that I need to learn some knowledge about RealTime OS first.I haven't learned this part yet.

4

u/[deleted] Jan 31 '21

[removed] — view removed comment

2

u/orclev Feb 01 '21

I haven't done anything significant, but it has been pretty good honestly. At least for cortex-m devices. In theory there's some support for AVR, but it's fairly primitive and I haven't personally tried any of it. The various HAL crates are really good and provide a very respectable if limited abstraction over various registers. There's some even higher level crates that provide even better abstractions closer to the level of arduino libraries, but not many of them yet, so depending on your exact ARM device you may or may not be in luck there. In terms of actually building and flashing rust code it couldn't be easier. Just need a cargo file with a couple plugins, so much like any other rust project, and the semi-hosting crate is very easy to work with.

2

u/johhuang12138 Jan 31 '21

The development board I use is Bluepill, and the chip is F103C8T6

3

u/thekakester Jan 31 '21

Start here: video #2 should help with the learning curve.

https://youtube.com/playlist?list=PLNyfXcjhOAwO5HNTKpZPsqBhelLF2rWQx

1

u/johhuang12138 Feb 03 '21

Guys, I've got it. The USB mouse program I wrote before can't be recognized when plugged into the computer.I found out yesterday that the reason was that I plugged in the USB without power off after download the program.

It turned out that after download the program, I had to power off and reconnect the USB to power on so that it could be recognized by the computer.

1

u/johhuang12138 Feb 03 '21

Thank you very much for your encouragement and suggestions, I have compiled the program according to some examples. Although I can't understand the USB protocol deeply, I can realize the basic mouse and keyboard program.

1

u/johhuang12138 Feb 03 '21

This is my work. It's kind of silly

STM32 Keyboard

1

u/krgoodwin Feb 03 '21

Cool, glad you got it working. What's the plan? MIDI stuff?

1

u/johhuang12138 Feb 04 '21

It's just the keyboard right now. I'll add MIDI functionality after I learn about the MIDI protocol.

1

u/Aggressive_Doughnut Feb 01 '21

"Why is it so difficult to develop USB using any real* SDK?"

There fixed it for you.

The answer is that USB can do so much. No, really, SO SO much. Much more than you think it can. So even the clearest API needs to be incredibly flexible to accommodate the insane variety of possibilities. The SDK creator doesn't make an API just to make it easy to send a key stroke. Unfortunate for you, but true.

I recommend looking through the examples that come with cube mx. I'm on the road and can't easily look through them to tell you which one to use, but I modified an ST example for my first USB HID device and it worked out very well.

Good luck, and be patient!

*In this context "real" means a (usually C-based) SDK from the platform developer. There are plenty of third party libraries/environments/etc that offer simplified APIs for beginners to accomplish the most common tasks. Keep in mind that there are reasons that these aren't commonly used in professional development.

1

u/johhuang12138 Feb 02 '21

1

u/johhuang12138 Feb 02 '21

Sorry, I don't know how to upload pictures on my phone through the Reddit app

1

u/Aggressive_Doughnut Feb 02 '21

That's where I would begin for building new projects for interfaces and peripherals I don't already know well. However when you are just starting out with something, it can be even easier to modify a working example. Here's a source for official ST USB examples on STM32F4 MCUs: https://github.com/STMicroelectronics/STM32CubeF4/tree/master/Projects/STM32446E_EVAL/Applications/USB_Device

You would have to modify this to work on your board by changing the pins for the buttons or LEDs or whatever. There are other boards and examples in that GitHub and on other places via the ST website. Spend some time looking through them, seeing how they work and which functions they call, reading the documentation for those fictions, etc. Example code can get you started quickly, it's one of the reasons they can sell those $200 demo/eval boards. It's much faster to get started on a product when you already have some working code to modify.

0

u/[deleted] Jan 31 '21

[deleted]

1

u/johhuang12138 Feb 01 '21

T_T, It's terrible. I want to cry .

1

u/johhuang12138 Feb 01 '21

I simply want it to send a keyboard message to my computer.