Choosing STLink V2 programmer

Not possible out of the box.

You’ll have to modify the OpenOCD script to e.g. check for the existence of an environment variable like “SERIAL”, and then the script can use that serial to identify the correct STLinkv2. That’s how I’ve implemented a “flash firmware to a specific STLink” some time ago.

The code for the modified stlink.cfg can be found at Programming with multiple ST-Link programmers (the new scripts all redirect to stlink.cfg).

#
# STMicroelectronics ST-LINK/V2-1 in-circuit debugger/programmer
#

interface hla
hla_layout stlink
if { [info exists ::env(SERIAL) ] } {
        hla_serial  $::env(SERIAL)
}
hla_device_desc "ST-LINK/V2-1"
hla_vid_pid 0x0483 0x374b

This file is at <home folder>\.platformio\packages\tool-openocd\scripts\interface\stlink.cfg.

You can then find out the serial number of your STLinks with e.g. the ST-Link utility, but it’s also printed by OpenOCD

Once you have that you can write a python script for PIO (see docs) to invoke OpenOCD in a special environment (or modify the global environment vars) which has this SERIAL variable set correctly according to the name of the environment or some other configuration paramter in the platformio.ini. Normal python functions can be used for that.

1 Like