cancel
Showing results for 
Search instead for 
Did you mean: 

How to control STLink from cli, or python?

JR2963
Senior II

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.

1 ACCEPTED SOLUTION

Accepted Solutions

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

View solution in original post

9 REPLIES 9
Pete Dietl
Associate II

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/

AScha.3
Chief III

>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 "Accept as Solution".

Yes I did it, but nothing changed

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

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?

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

If you give pyocd an object file, like an Intel `.hex` file or an ELF file, it can figure out at which addresses to write automatically. When using a binary file, you need to supply a base address manually. Take a look at the help output of `pyocd flash --help`. The relevant argument is:

  -a ADDR, --base-address ADDR
                        Base address used for the address where to write a binary. Defaults to
                        start of flash. Only allowed if a single binary file is being loaded.

So you will need to use the `-a` argument if you want to write the binary somewhere other than address `0`.

Thank you. I see my STlink is not able to recognize connected mcu.

The flashing seems to work!

Intersting:

I have Jlink and STlink, I loaded hex file to the STM32 like that: pyocd load -t STM32h743zGTx ADC7771.hex        

1) thorough Jlink - it erase, flash and finally run the code

2) thorought STlink - it erase, flash but finally the code is not running - why?

I still do not understand the documentation well. E.G. I want to find out status of target:

So i tried pyocd --target st - but it does not work, so how should the command looks like?

like is writte here: https://pyocd.io/docs/command_reference.html#status