STM32: How to program it via USB

armprogrammingstm32

Until now I was always using Arduino Nano Boards, which had the proper bootloader installed so that programming via the ArduinoIDE was easy and fast. Just plug it in via USB, wait for Windows to install the driver, open ArduinoIDE and there you go.

Now I have a project which demands more powerful and versatile peripheral devices via SPI and CAN. Therefore I chose a STM32 as an ARM microprocessor.

Now what's confusing me:

  1. Somehow you don't Plug'n'Play with those, at least on self made custom PCBs. There are many applications I found that used a SWD connection via a "ST-Link" (is this the programmer/"bridge" establishing the connection between PC and actual microprocessor?).

Can I program the STM32 easily by pluging it to a ST-Link and program it via any ARM-compatible IDE like mBed for example? (Like I would with an Arduino and ArduinoIDE?)

  1. Most of the microcontroller do have a USB interface onboard, where you can directly connect USB_DM and USB_DP with a USB port. Can I also program a STM32 via the USB interface by connecting it directly to a PC?

Would it be recognised by a PC and IDE like a Arduino would or are there other steps I would have to do in order to have a ""Plug'n'Play"-like programming with the STM32 via USB?

Some explanations on how to programm those ARM microprocessors (like the STM32) would be really helpful to me, because I get too confused with all the programming methods.

Best Answer

via a "ST-Link" (is this the programmer/"bridge" establishing the connection between PC and actual microprocessor?)

Exactly; it's a debugging adapter. The debugging interface ("SWD", single wire debug, on the STM32) allows for writing the flash memory.

Can I program the STM32 easily by pluging it to a ST-Link and program it via any ARM-compatible IDE like mBed for example?

The tooling needs to know how to program your specific chip. But usually that just means your IDE uses a software tool (often, OpenOCD) to do the flashing; just as your IDE is not your compiler, but uses a compiler (e.g. clang, gcc) to convert source code to machine code.

Most IDEs will bring, or know how to bring in, such a flashing software.

Most of the microcontroller do have a USB interface onboard, where you can directly connect USB_DM and USB_DP with a USB port. Can I also program a STM32 via the USB interface by connecting it directly to a PC?

That very much depends on the individual STM32 model. Some come with a USB bootloader that allows for that. You'll have to read the product page of the STM32 in question!

Would it be recognised by a PC and IDE like a Arduino would or are there other steps I would have to do in order to have a ""Plug'n'Play"-like programming with the STM32 via USB?

Can't tell you - depends on the existence and type of said bootloader, and your IDE.

Notice that you might be a bit surprised by the useful things that you can actually do if you have a debugger attached - Arduino is quite bad in its debugging experience. So, for development, chances are that using an STLinkv2 (or clone thereof) will be your preferred way, anyways! (These things are cheap. Worst case, you buy a cheap STM32 Nucleo eval board and use the one that's integrated there – you can take of three jumpers and connect your own board via cables.)

The USB bootloader (and I²C or SPI or UART bootloaders some devices have) are really more meant for production or field updates, where you'd want to plug thousand boards in and load finished firmware, not for development. (again, the original Arduino is less comfortable than what most engineers are used working with: you can do things like halt your program where it is, modify code or data memory or the state of some peripheral, and continue running and so on. Debugging.)

Some explanations on how to programm those ARM microprocessors (like the STM32) would be really helpful to me, because I get too confused with all the programming methods.

Download ST's IDE then and read/watch a tutorial by ST then.