OpenOCD 多适配器类型配置

问题描述

我们团队中的一些开发人员使用 J-Link 调试器,而其他开发人员使用 ST-Link 调试器。我们所有人都使用相同的固件在相同的硬件上工作,基本上其他一切都相同。当前设置需要为每个适配器使用不同的 cfg 文件启动 OpenOCD。我希望自动完成此操作。

有没有办法将 OpenOCD 配置为根据连接的适配器自动选择正确的 cfg 文件

解决方法

我不知道 OpenOCD 是否可以做到(我认为它不能),但是由于您使用的是 Windows,因此您可以使用 VBScript 编写的小包装器来检测哪个探针可用,并根据检测到的探针启动不同的命令:

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,false
End If

用法:

cscript.exe openocd.vbs

您必须根据需要调整 strStlinkCommandstrJlinkCommand 变量的内容。

您当然可以从批处理过程中调用它:

launch-openocd.cmd

@cscript.exe openocd.vbs

Windows 10 version 20H2 19042.746 上测试

,

使用 linux,您可以使用 USB-VendorID/ProductID 来选择配置文件。

你可以通过一个 shell 脚本启动

if lsusb | grep 1366:1015 > /dev/null; then 
  config=j-link.conf
else
  config=st-link.conf
fi
openocd -f $config