Search code examples
debuggingarmgdbstm32openocd

How to use OpenOCD to talk to two STM32 boards at once?


Let's say I've got two STM32's, and I'm using this programmer here.

I want to connect to both of them and debug/reflash/itterate independently.

So, the setup I have is as follows:

HW

PC |-> USB1 -> ST-LINK-Programmer1 -> STM32_Board1 |-> USB2 -> ST-LINK-Programmer2 -> STM32_Board2

SW

They way I normally do this with one board is pretty simple.

openocd -f config.cfg

And here is the config file I'm calling:

source [find interface/stlink-v2.cfg] transport select hla_swd source [find target/stm32f4x.cfg] reset_config none

Then, in a different terminal, I call arm-gdb like so:

arm-none-eabi-gdb build/FW.elf

and in the ~/.gdbinit, I've got this single line:

target remote localhost:3333

What's not Working

This is pretty obvious...I'm using port 3333 for the first OpenOCD, but the second instance is trying to use that same port and fails with

Error: couldn't bind tcl to socket: Address already in use

What I've Tried

I've looked over the documentation here, but I'm failing to see how to call these options in my config.cfg file.

I've also tried adding these commands about tcl_port and gdb_port to the actual command line arguments, like openocd -f config.cfg -c tcl_port 4444, but this doesn't work either...The socket is still in use.

My Real Question

What's the right way to do this? And are there any gotchas with dealing with arm-none-eabi-gdb after getting openocd configured so that it connects to the right OpenOCD instance?


Solution

  • stlinkv2.cfg from openocd

    #
    # STMicroelectronics ST-LINK/V2 in-circuit debugger/programmer
    #
    
    interface hla
    hla_layout stlink
    hla_device_desc "ST-LINK/V2"
    hla_vid_pid 0x0483 0x3748
    
    # Optionally specify the serial number of ST-LINK/V2 usb device.  ST-LINK/V2
    # devices seem to have serial numbers with unreadable characters.  ST-LINK/V2
    # firmware version >= V2.J21.S4 recommended to avoid issues with adapter serial
    # number reset issues.
    # eg.
    #hla_serial "\xaa\xbc\x6e\x06\x50\x75\xff\x55\x17\x42\x19\x3f"
    

    your config is sourcing this, comment out the source to that file line and replace with this

    interface hla
    hla_layout stlink
    hla_device_desc "ST-LINK/V2"
    hla_vid_pid 0x0483 0x3748
    hla_serial 12345623498723497
    

    With whatever that specific stlinks serial number is (hopefully they vary)

    repeat with a new config for each, then change the tcp/gdb port numbers for each, I never use gdb so I

    telnet_port 4442
    gdb_port 0
    tcl_port 0
    

    lsusb -vvv

    Bus 002 Device 011: ID 0483:374b STMicroelectronics ST-LINK/V2.1 (Nucleo-F103RB)
    Device Descriptor:
      bLength                18
      bDescriptorType         1
      bcdUSB               2.00
      bDeviceClass          239 Miscellaneous Device
      bDeviceSubClass         2 ?
      bDeviceProtocol         1 Interface Association
      bMaxPacketSize0        64
      idVendor           0x0483 STMicroelectronics
      idProduct          0x374b ST-LINK/V2.1 (Nucleo-F103RB)
      bcdDevice            1.00
      iManufacturer           1 STMicroelectronics
      iProduct                2 STM32 STLink
      iSerial                 3 12345623498723497
      bNumConfigurations      1
    

    I made up that iSerial number not my real one, probably doesnt matter if everyone knows the real one for this board...

    I didnt try with two boards, if I add the hla_serial line and use the iSerial number then it does its thing. If I modify the serial number to not match it changes how openocd works it doesnt find the stlink. I would have to dig up more boards to fully test this, but you have these boards handy already so will let you see if this works for you.