Auto-select Cortex-M4 on flash/capture so J-Link never prompts for device.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
yuquanjun 2026-08-30 20:11:17 +08:00
parent f3774b069b
commit 1d5fe8073d
4 changed files with 84 additions and 1 deletions

64
scripts/flash-app.ps1 Normal file
View File

@ -0,0 +1,64 @@
#Requires -Version 5.1
<#
.SYNOPSIS
Build (optional) and flash YNGJ-GT1-M app via IAR cspybat + J-Link.
Auto-selects Cortex-M4 and suppresses J-Link device-selection dialogs.
#>
param(
[switch]$NoBuild
)
$ErrorActionPreference = "Stop"
$Root = Split-Path -Parent $PSScriptRoot
$Ewp = Join-Path $Root "project\IAR_V7.4\YNGJ-GT1-M.ewp"
$ExeDir = Join-Path $Root "project\IAR_V7.4\YNGJ-GT1-M\Exe"
$Out = Join-Path $ExeDir "YNGJ-GT1-M.out"
$Bin = Join-Path $ExeDir "YNGJ-GT1-M.bin"
$ToolsDir = Join-Path $Root "tools"
$SettingsSrc = Join-Path $ToolsDir "JLinkSettings.ini"
$JlinkScript = Join-Path $ToolsDir "jlink_hidedevid.jlink"
$GeneralXcl = Join-Path $Root "project\IAR_V7.4\settings\YNGJ-GT1-M.YNGJ-GT1-M.general.xcl"
$DriverXcl = Join-Path $Root "project\IAR_V7.4\settings\YNGJ-GT1-M.YNGJ-GT1-M.driver.xcl"
$IarBuild = "C:\Program Files (x86)\IAR Systems\Embedded Workbench 7.3\common\bin\IarBuild.exe"
$CspyBat = "C:\Program Files (x86)\IAR Systems\Embedded Workbench 7.3\common\bin\cspybat.exe"
$TempFlash = "C:\Temp\k1flash"
# Suppress J-Link unknown-device popup (cwd / AppData / ASCII path for script).
New-Item -ItemType Directory -Force -Path $ExeDir, $TempFlash | Out-Null
Copy-Item -Force $SettingsSrc (Join-Path $ExeDir "JLinkSettings.ini")
Copy-Item -Force $SettingsSrc (Join-Path $TempFlash "JLinkSettings.ini")
Copy-Item -Force $JlinkScript (Join-Path $TempFlash "jlink_hidedevid.jlink")
$Roaming = Join-Path $env:APPDATA "SEGGER"
New-Item -ItemType Directory -Force -Path $Roaming | Out-Null
Copy-Item -Force $SettingsSrc (Join-Path $Roaming "JLinkSettings.ini")
# Stop leftover debug servers that hold the probe.
Get-Process -Name CSpyBat, JLinkGUIServer, JLink -ErrorAction SilentlyContinue |
Where-Object { $_.ProcessName -match 'CSpyBat|JLinkGUIServer' } |
Stop-Process -Force -ErrorAction SilentlyContinue
if (-not $NoBuild) {
Write-Host "Building..."
& $IarBuild $Ewp -make YNGJ-GT1-M
if ($LASTEXITCODE -ne 0) { throw "IarBuild failed: $LASTEXITCODE" }
}
if (-not (Test-Path $Out)) { throw "Missing $Out" }
if (Test-Path $Bin) {
Copy-Item -Force $Bin (Join-Path $TempFlash "YNGJ-GT1-M.bin")
}
Write-Host "Flashing (Cortex-M4, no device dialog)..."
Push-Location $ExeDir
try {
# HideDeviceSelection comes from JLinkSettings.ini + driver.xcl jlink script.
& $CspyBat -f $GeneralXcl --download_only --silent --backend -f $DriverXcl
$code = $LASTEXITCODE
} finally {
Pop-Location
}
if ($code -ne 0) { throw "Flash failed: $code" }
Write-Host "Flash OK"
exit 0

8
tools/JLinkSettings.ini Normal file
View File

@ -0,0 +1,8 @@
[GENERAL]
HideDeviceSelection = 1
[CPU]
Device="Cortex-M4"
[FLASH]
Device="Cortex-M4"

View File

@ -0,0 +1,2 @@
HideDeviceSelection 1
Device Cortex-M4

View File

@ -12,7 +12,7 @@ import time
MAGIC = b"SCRN" MAGIC = b"SCRN"
HEADER_FMT = "<4sHHHH" HEADER_FMT = "<4sHHHH"
HEADER_SIZE = struct.calcsize(HEADER_FMT) HEADER_SIZE = struct.calcsize(HEADER_FMT)
DEFAULT_DEVICES = ("AT32F403AC", "AT32F403A", "Cortex-M4") DEFAULT_DEVICES = ("Cortex-M4", "AT32F403AC", "AT32F403A")
def rgb565_bytes_to_rgb(hi: int, lo: int) -> tuple[int, int, int]: def rgb565_bytes_to_rgb(hi: int, lo: int) -> tuple[int, int, int]:
@ -39,11 +39,20 @@ def connect_jlink(device: str):
jlink = pylink.JLink() jlink = pylink.JLink()
jlink.open() jlink.open()
try:
# Avoid interactive "select device" popup for unknown AT32 ID.
jlink.exec_command("HideDeviceSelection = 1")
except Exception:
pass
jlink.set_tif(pylink.enums.JLinkInterfaces.SWD) jlink.set_tif(pylink.enums.JLinkInterfaces.SWD)
last_error = None last_error = None
for candidate in (device, *DEFAULT_DEVICES): for candidate in (device, *DEFAULT_DEVICES):
try: try:
try:
jlink.exec_command(f"Device = {candidate}")
except Exception:
pass
jlink.connect(candidate) jlink.connect(candidate)
print(f"Connected as {candidate}") print(f"Connected as {candidate}")
return jlink return jlink