embeddedopenocdsegger-jlink

OpenOCD multiadapter type configuration


Some devs on our team have J-Link debuggers while others are working with ST-Link debuggers. All of us work on the same hardware with the same firmware, basically everything else is the same. The current setup requires to start OpenOCD with a different cfg file for each adapter. I would like to have this done automatically.

Is there a way to configure OpenOCD to automatically choose the correct cfg file based on the connected adapter?


Solution

  • I don't know if OpenOCD can do that (I don't think it can), but since you are using Windows, you could just use a small wrapper written in VBScript detecting which probe is available, and launch a different command depending on which probe was detected:

    openocd.vbs:

    ' STLink: 
    ' idVendor:                        0x0483 = STMicroelectronics
    ' idProduct:                       0x3748
    Dim strStlink
    Dim stlinkPresent
    Dim strStlinkCommand
    strStlink = "VID_0483&PID_3748"
    stlinkPresent=0
    strStlinkCommand="cmd.exe /c D:\opt\openocd\0.10.0-14\stm32f103c8_blue_pill_stlink.cmd"
    
    ' Segger JLink'
    ' idVendor:                        0x1366 = SEGGER Microcontroller Systems GmbH
    ' idProduct:                       0x0101
    Dim strJlink
    Dim jlinkPresent
    Dim strJlinkCommand
    strJlink="VID_1366&PID_0101"
    jlinkPresent=0
    strJlinkCommand="cmd.exe /c D:\opt\openocd\0.10.0-14\stm32f103c8_blue_pill_jlink.cmd"
    
    ' Credits:
    ' https://stackoverflow.com/questions/3331043/get-list-of-connected-usb-devices
    strComputer = "." 
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
    Set colItems = objWMIService.ExecQuery( _
        "SELECT * FROM Win32_PnPEntity",,48) 
    For Each objItem in colItems 
     '   Wscript.Echo "-----------------------------------"
     '   Wscript.Echo "Win32_PnPEntity instance"
     '   Wscript.Echo "-----------------------------------"
    '    Wscript.Echo "DeviceID: " & objItem.DeviceID
        If InStr(objItem.DeviceID, strStlink) Then
          WScript.Echo("Found STLink Device")
          stlinkPresent=1
        End If
    
        If InStr(objItem.DeviceID, strJlink) Then
          WScript.Echo("Found JLink Device")
          jlinkPresent=1
        End If
    Next
    
    If (jlinkPresent=1 And stlinkPresent=1) Then
        WScript.Echo("Found both JLink and STLink devices - terminating.")
        WScript.Quit(1)
    End If
    
    If (jlinkPresent=0 And stlinkPresent=0) Then
        WScript.Echo("No JLink/STLink devices were found - terminating.")
        WScript.Quit(2)
    End If
    
    Set WshShell = WScript.CreateObject("WScript.Shell")
    
    If (stlinkPresent=1) Then
        WshShell.Run strStlinkCommand, 1, false
    End If
    
    If (jlinkPresent=1) Then
        WshShell.Run strJlinkCommand, 1, false
    End If
    

    Usage:

    cscript.exe openocd.vbs

    You will have to adapt the content of the strStlinkCommand and strJlinkCommand variables to your needs.

    You can of course call it from a batch procedure:

    launch-openocd.cmd:

    @cscript.exe openocd.vbs
    

    Tested on Windows 10 version 20H2 19042.746