I want to use my first STM32 MCU in the project. The project involves a few peripherals (UART/SPI/I2C) and has a bit of math to do here and there, mainly bit shifting and/or integer computations, so I found myself an MCU with the peripherals I need, internal oscillator, enough flash program space and RAM for my application.

So I found this smol boi: STM32L051K8T6 in LQFP-32, Datasheet

I looked through the datasheet, everything seems clear, I have no problem picking up new stuff, IDE and all, however, I have a pair of questions I wasn't able to google up clear answer for, and the community can be more helpful anyway:

  1. I would like to be able to program it via SWD. It's a 2 wire communication, right? SWDIO and SWCLK. So, in theory, provided I supply power for the MCU, I only need 2 pins to hook myself up to MCU (or maybe also reset? Grounds connected, obviously). I mean, I don't need large 10-pin connector or anything? Pretty much like Atmegas via UART (which need RX, TX and DTR pulse to MCU reset). Googling ST-Link v2 pinout showed me only these two and an unknown to me "SWIM" pin. (which is never mentioned in datasheet; I actually have an ST-Link v2 and even a nucleo board with on board programmer, but, again, it never hurts to ask smart people).

  2. SWDIO and SWCLK are marked as "Alternate functions" of the pins. So, I assume, I won't be able to program it via SWDIO/SWCLK out of the box, because the pins are not configured as SWDIO/SWCLK by default. Is it something I need to use BOOT0 for? Does the bootloader from the internal memory activate SWD pins for me to program the flash? There is also no BOOT1 pin in this specific case (Ctrl+F "Boot1" yields no useful mentions in the entire datasheet).

  3. I want to have user-updatable software. Via USB. It's easy to do it with ATMega, I just tossed in a USB-UART CP2102N, and it works like a charm, can update software all day. I guess I could write my own UART bootloader for STM32, it's only a matter of determination and taking pills to stay sane in the process. But if there is an easier way, I don't want to reinvent the wheel (although that programming part on the nucleo looks a lot bigger than just CP2102N chip). Found this document on using UART: STM32 Bootloader UART seems promising, it doesn't seem too difficult to use CP2102N to program STM32, I guess?

Of course I have watched a few videos on the youtube about STM32 as well, but they are basically "blinky-blinky" with nucleo, not with standalone device, so not much talk about datasheets, alternate functions, controlling stuff via registers; and setting up I2C or SPI won't be a problem anyway. Basically, I have no problem controlling the registers and stuff, I just need to get it running first. It's like I can drive a car, but I don't have a key to start the engine - or maybe I do and I'm just not aware of it. In order not to explode the car on ignition, I prefer to ask questions first.

Thank you

  • 2
    \$\begingroup\$ You need to download the reference manual. All your questions will be answered. \$\endgroup\$
    – Kartman
    Commented Feb 21, 2021 at 13:30
  • 1
    \$\begingroup\$ Short answer: according to the reference manual, swd works out of the box. \$\endgroup\$
    – Kartman
    Commented Feb 21, 2021 at 13:41

1 Answer 1

  1. That is correct. You only need SWDIO & SWCLK. Most of the time NRST connection is not needed. AFAIK, debugger can send some kind of soft reset sequence using data and clock pins. Hardware reset pin may be needed if SWDIO & SWCLK pins are configured as GPIO pins (related with your 2nd question) just after the reset, thus preventing debugging. I have never needed it, but it's still wise to include it in your debugging header if you have space in your PCB. There is also SWO pin, which provides additional debug output if enabled. It's not essential, and Chinese ST-Link clones don't have the pin breaked out in their connector, even though their firmware supports it.
  2. After reset, all pins related with debugging are available unless you configure them for other purposes in your firmware. In some models, this includes the JTAG pins. If you configure them as GPIO pins (or for other purposes) just at the beginning of your firmware, the debugger may not have time to start a debugging session. In this case, usage of the hardware NRST pin may be necessary. BOOT pins are not related with debug pin configuration. They configure mapping of the address space. Most of the time you connect BOOT0 to GND to boot from flash. Booting from RAM or internal factory bootloader is also possible.
  3. As I stated on answer 2, STM32 devices have internal bootloaders, which can be activated using BOOT0 pin (with the correct OPTION bytes in flash). It's also possible to jump into this factory programmed bootloader from user code. Although it's a little bit complicated, it's possible to find examples on web. Some devices also provide USB DFU bootloaders (STM32F042 for example). When using these products, you don't even need to use a USB - Serial converter. Factory programmed bootloader may also support other interfaces, like SPI or CAN, but I'm not sure and I can't remember. You need to check the specific device documentation.

BTW, SWIM pin is used to program and debug STM8 parts (yes, ST-Link is capable of that). It's not related with STM32 parts.

  • \$\begingroup\$ So, in theory, I don't even need to boot into bootloader to program the thing, it is programmable via SWD even while it's running flash program. Is this statement correct? I'm not going to use SWD pins for anything else if I use SWD. And I still need to connect it to usb for enduser to update firmware. I will write a small program for it, no problem, so there is no way without USB-Serial, if it's actually possible to program flash memory via UART. Unless I put the program on the microSD card, which I have (with SPI then) \$\endgroup\$
    – Ilya
    Commented Feb 21, 2021 at 14:01
  • 1
    \$\begingroup\$ Correct, ST-Link can stop and program (or debug) an STM32 which is running from flash, given that SWD pins are left at default states and not configured for other purposes. Your STM32L051K8T6 doesn't seem to have USB, so the end user will probably need a USB to Serial converter to access the factory programmed bootloader. Note that some models, like STM32F103, have USB hardware but they lack factory programmed USB bootloader. \$\endgroup\$
    – Tagli
    Commented Feb 21, 2021 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.