From 1d5fe8073d23fcbf7e1275a91330b50b26e74060 Mon Sep 17 00:00:00 2001 From: yuquanjun Date: Sun, 30 Aug 2026 20:11:17 +0800 Subject: [PATCH] Auto-select Cortex-M4 on flash/capture so J-Link never prompts for device. Co-authored-by: Cursor --- scripts/flash-app.ps1 | 64 +++++++++++++++++++++++++++++++++++++ tools/JLinkSettings.ini | 8 +++++ tools/jlink_hidedevid.jlink | 2 ++ tools/rtt_lcd_capture.py | 11 ++++++- 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 scripts/flash-app.ps1 create mode 100644 tools/JLinkSettings.ini create mode 100644 tools/jlink_hidedevid.jlink diff --git a/scripts/flash-app.ps1 b/scripts/flash-app.ps1 new file mode 100644 index 0000000..64be173 --- /dev/null +++ b/scripts/flash-app.ps1 @@ -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 diff --git a/tools/JLinkSettings.ini b/tools/JLinkSettings.ini new file mode 100644 index 0000000..a03c98d --- /dev/null +++ b/tools/JLinkSettings.ini @@ -0,0 +1,8 @@ +[GENERAL] +HideDeviceSelection = 1 + +[CPU] +Device="Cortex-M4" + +[FLASH] +Device="Cortex-M4" diff --git a/tools/jlink_hidedevid.jlink b/tools/jlink_hidedevid.jlink new file mode 100644 index 0000000..66bbce9 --- /dev/null +++ b/tools/jlink_hidedevid.jlink @@ -0,0 +1,2 @@ +HideDeviceSelection 1 +Device Cortex-M4 diff --git a/tools/rtt_lcd_capture.py b/tools/rtt_lcd_capture.py index 5e3b927..db4d339 100644 --- a/tools/rtt_lcd_capture.py +++ b/tools/rtt_lcd_capture.py @@ -12,7 +12,7 @@ import time MAGIC = b"SCRN" HEADER_FMT = "<4sHHHH" 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]: @@ -39,11 +39,20 @@ def connect_jlink(device: str): jlink = pylink.JLink() 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) last_error = None for candidate in (device, *DEFAULT_DEVICES): try: + try: + jlink.exec_command(f"Device = {candidate}") + except Exception: + pass jlink.connect(candidate) print(f"Connected as {candidate}") return jlink