Skip to main content
JR2963
Senior II
May 21, 2023
Solved

How to control STLink from cli, or python?

  • May 21, 2023
  • 9 replies
  • 9219 views

Hi, I need to control Stlink form python code.

1) On some PC I am not able to connect with STLink to my MCU - I tried STM32CubeProg and ST-Link utility. The Error is  UR connection mode is defined with the HWrst reset mode.

But why on the same PC the STM32CubeIde can without problem load the program to the MCU? What kinds of "API" STM32CubeIde use? I want these API - because always works!

I have not connected RESET pin - but it should not be problem hope

2) On the PC when I am able to connect to MCU VIA STM32CubeProgrammer - I also can use STM32CubeProg cli interface to conrol it via python. But is not there something different? Because STM32Cubeprogrammer is too large (about 700 MB on disk?) I also tried ST_Link utitliy - cli, but it was not much reliable.

Thank you very much.

This topic has been closed for replies.
Best answer by Pete Dietl

Sometimes an STLink is able to identify the chip it is connected to, and sometimes not. I think it only works for STLinks that are embedded into a dev board, like one of the Nucleo boards.

For instance, I have an NUG431KB Nucleo board. When I plug it into my computer via USB (and thus am using its embedded STLink), I can get the following:

pyocd pack install stm32g4
# Lots of output, errors and warnings are usually fine
pyocd list
# Probe/Board Unique ID Target 
# ------------------------------------------------------------------
# 0 STLINK-V3 002700044D4B500A20373831 ✔︎ 
# stm32g431kbtx 
# NUCLEO-G431KB 
 


_legacyfs_online_stmicro_images_0693W00000dJqyqQAC.png

9 replies

Pete Dietl
Associate II
May 22, 2023

What are you trying to do with the STLink from Python? Depending on your use case, pyOCD can be a great option. https://pyocd.io/

JR2963
JR2963Author
Senior II
May 23, 2023

Looks good, Is there any nice example (I didn't find much)? I just want to erase and load new .bin file to the flash memory of STM32

AScha.3
Super User
May 22, 2023

>I have not connected RESET pin - but it should not be problem hope

no problem, but you have to set "software system reset" .

>Error is  UR connection mode is defined with the HWrst reset mode

set software system reset !

If you feel a post has answered your question, please click on " Best Answer ".
JR2963
JR2963Author
Senior II
May 22, 2023

Yes I did it, but nothing changed

JR2963
JR2963Author
Senior II
May 27, 2023

Hi thank you, I want to use pyOCD but I really do not understand the doccumentaion:

1) How to check if any MCU is connected to the STlink? Would be great if it returns me specifi name of the MCU like STM32F303RBTx

  • I tried pyocd status, but it returns: pyocd: error: unrecognized arguments: --status

really do not why it does not know command status

2) I Want to erase and flash MCU

  • I used pyocd erase --chip -t STM32F303RBTx - it works
  • I used pyocd flash -t STM32F303RBTx tester_wallbox_power.bin

3) I want to read and write to the specific memory location

There is list of all commands, which I found: https://pyocd.io/docs/command_reference.html

Can you help with it please?

Pete Dietl
Pete DietlBest answer
Associate II
June 1, 2023

Sometimes an STLink is able to identify the chip it is connected to, and sometimes not. I think it only works for STLinks that are embedded into a dev board, like one of the Nucleo boards.

For instance, I have an NUG431KB Nucleo board. When I plug it into my computer via USB (and thus am using its embedded STLink), I can get the following:

pyocd pack install stm32g4
# Lots of output, errors and warnings are usually fine
pyocd list
# Probe/Board Unique ID Target 
# ------------------------------------------------------------------
# 0 STLINK-V3 002700044D4B500A20373831 ✔︎ 
# stm32g431kbtx 
# NUCLEO-G431KB 
 


_legacyfs_online_stmicro_images_0693W00000dJqyqQAC.png