Skip to main content
Graduate II
May 21, 2023
Solved

How to control STLink from cli, or python?

  • May 21, 2023
  • 2 replies
  • 9072 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

    2 replies

    Visitor 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/

    JR2963Author
    Graduate 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

    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 !

    JR2963Author
    Graduate II
    May 22, 2023

    Yes I did it, but nothing changed

    Visitor 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

    Visitor II
    June 1, 2023

    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`.

    JR2963Author
    Graduate II
    June 2, 2023

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

    The flashing seems to work!

    JR2963Author
    Graduate II
    June 9, 2023

    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