元驭机器人元驭机器人Hall 传感器教学下载原文件
1# Flash Hall_Senser onto GD32E230G6 with OpenOCD (CMSIS-DAP / SWD).2# Prefer GigaDevice OpenOCD (`gd32e23x`). xPack/stock OpenOCD uses3# scripts/openocd-gd32e230-stm32f1x.cfg (32 KB, no mass_erase).4# Usage:5#   .\scripts\flash.ps16#   .\scripts\flash.ps1 -BuildBeforeFlash7#   .\scripts\flash.ps1 -Preset release8#   .\scripts\flash.ps1 -ElfFile 'D:\path\Hall_Senser.elf'910param(11    [ValidateSet('debug', 'release')]12    [string]$Preset = 'debug',1314    [string]$ElfFile = '',15    [string]$OpenOcdExe = '',16    [string]$OpenOcdScripts = '',17    [string]$ConfigFile = '',1819    [Alias('Build')]20    [switch]$BuildBeforeFlash,21    [switch]$NoResetRun22)2324$ErrorActionPreference = 'Stop'2526$Root = Split-Path -Parent $PSScriptRoot2728function Invoke-Native {29    param(30        [string]$Exe,31        [string[]]$Arguments32    )33    & $Exe @Arguments34    if ($LASTEXITCODE -ne 0) {35        exit $LASTEXITCODE36    }37}3839function Get-FirstExisting {40    param([string[]]$Paths)41    foreach ($p in $Paths) {42        if ([string]::IsNullOrWhiteSpace($p)) { continue }43        $hits = @(Get-Item -Path $p -ErrorAction SilentlyContinue)44        if ($hits.Count -gt 0) {45            return ($hits | Sort-Object FullName -Descending | Select-Object -First 1).FullName46        }47    }48    return $null49}5051function Find-OpenOcdScriptsDir {52    param([string]$HomeDir)53    foreach ($rel in @('openocd\scripts', 'share\openocd\scripts', 'scripts')) {54        $d = Join-Path $HomeDir $rel55        if (Test-Path -LiteralPath $d) { return $d }56    }57    return $null58}5960function Test-OpenOcdHasGd32E23x {61    param(62        [string]$Exe,63        [string]$ScriptsDir64    )65    if ($Exe -match 'com\.gd\.tools') { return $true }66    if (-not $ScriptsDir) { return $false }67    return (Test-Path -LiteralPath (Join-Path $ScriptsDir 'target\openocd_gdlink_gd32e23x.cfg'))68}6970function Find-OpenOcd {71    if ($env:HALL_OPENOCD_EXE -and (Test-Path -LiteralPath $env:HALL_OPENOCD_EXE)) {72        $homeDir = Split-Path -Parent (Split-Path -Parent $env:HALL_OPENOCD_EXE)73        $scripts = $env:HALL_OPENOCD_SCRIPTS74        if (-not $scripts) { $scripts = Find-OpenOcdScriptsDir $homeDir }75        return [pscustomobject]@{76            Exe     = $env:HALL_OPENOCD_EXE77            Scripts = $scripts78            HasGd32 = Test-OpenOcdHasGd32E23x $env:HALL_OPENOCD_EXE $scripts79        }80    }8182    $gdHome = Get-FirstExisting @(83        'D:\GD32EB_v1.5.11_Rel\GD32EB_v1.5.11_Rel\GD32EB\plugins\com.gd.tools.openocd_*\Tools\OpenOCD'84        'D:\GD32EB*\*\GD32EB\plugins\com.gd.tools.openocd_*\Tools\OpenOCD'85        'C:\GD32EB*\*\GD32EB\plugins\com.gd.tools.openocd_*\Tools\OpenOCD'86    )87    if ($gdHome -and (Test-Path -LiteralPath (Join-Path $gdHome 'bin\openocd.exe'))) {88        $scripts = Join-Path $gdHome 'openocd\scripts'89        $exe = Join-Path $gdHome 'bin\openocd.exe'90        return [pscustomobject]@{91            Exe     = $exe92            Scripts = $scripts93            HasGd32 = $true94        }95    }9697    $exe = Get-FirstExisting @(98        'D:\ClionProject\YFOCV3_LITE\tools\openocd\bin\openocd.exe'99        'D:\ClionProject\YFOCV3_LITE\tools\openocd\*\bin\openocd.exe'100    )101    if (-not $exe) {102        $cmd = Get-Command openocd -ErrorAction SilentlyContinue103        if ($cmd) { $exe = $cmd.Source }104    }105    if ($exe) {106        $homeDir = Split-Path -Parent (Split-Path -Parent $exe)107        $scripts = Find-OpenOcdScriptsDir $homeDir108        return [pscustomobject]@{109            Exe     = $exe110            Scripts = $scripts111            HasGd32 = Test-OpenOcdHasGd32E23x $exe $scripts112        }113    }114    return $null115}116117$ocd = Find-OpenOcd118if ($OpenOcdExe -eq '') {119    if ($ocd) { $OpenOcdExe = $ocd.Exe }120}121if ($OpenOcdScripts -eq '') {122    if ($ocd) { $OpenOcdScripts = $ocd.Scripts }123}124125$hasGd32 = $false126if ($ocd) { $hasGd32 = [bool]$ocd.HasGd32 }127if ($OpenOcdExe -or $OpenOcdScripts) {128    $hasGd32 = Test-OpenOcdHasGd32E23x $OpenOcdExe $OpenOcdScripts129}130131if ($ConfigFile -eq '') {132    if ($hasGd32) {133        $ConfigFile = Join-Path $PSScriptRoot 'openocd-gd32e230.cfg'134    }135    else {136        $ConfigFile = Join-Path $PSScriptRoot 'openocd-gd32e230-stm32f1x.cfg'137    }138}139140if ($ElfFile -eq '') {141    $ElfFile = Join-Path $Root "build\$Preset\Hall_Senser.elf"142}143144if ($BuildBeforeFlash) {145    $buildScript = Join-Path $PSScriptRoot 'build.ps1'146    if (-not (Test-Path -LiteralPath $buildScript)) {147        throw "Build script not found: $buildScript"148    }149    & $buildScript -Preset $Preset150    if ($LASTEXITCODE -ne 0) {151        exit $LASTEXITCODE152    }153}154155if (-not $OpenOcdExe -or -not (Test-Path -LiteralPath $OpenOcdExe)) {156    throw "OpenOCD executable not found. Install GD32EB OpenOCD or pass -OpenOcdExe."157}158if (-not $OpenOcdScripts -or -not (Test-Path -LiteralPath $OpenOcdScripts)) {159    throw "OpenOCD scripts directory not found. Pass -OpenOcdScripts."160}161if (-not (Test-Path -LiteralPath $ConfigFile)) {162    throw "OpenOCD config not found: $ConfigFile"163}164if (-not (Test-Path -LiteralPath $ElfFile)) {165    throw "ELF file not found: $ElfFile. Run .\scripts\build.ps1 -Preset $Preset first."166}167168$resolvedElf = (Resolve-Path -LiteralPath $ElfFile).Path169$elfForOpenOcd = $resolvedElf.Replace('\', '/')170171# Page-erase used pages, then verify. Do not stm32f1x mass_erase.172$commands = @(173    'tcl port disabled',174    'gdb port disabled'175)176if ($NoResetRun) {177    $commands += "program `"$elfForOpenOcd`" verify exit"178} else {179    $commands += "program `"$elfForOpenOcd`" verify reset exit"180}181182$ocdArgs = @(183    '-s', $OpenOcdScripts,184    '-f', $ConfigFile185)186foreach ($command in $commands) {187    $ocdArgs += @('-c', $command)188}189190Write-Host "Target: GD32E230G6U6TR"191Write-Host "Preset: $Preset"192Write-Host "ELF: $resolvedElf"193Write-Host "OpenOCD: $OpenOcdExe"194if ($hasGd32) {195    Write-Host "Flash: gd32e23x program+verify (no mass_erase)"196} else {197    Write-Host "Flash: stm32f1x fallback 32 KB program+verify (no mass_erase)"198}199200Invoke-Native -Exe $OpenOcdExe -Arguments $ocdArgs201Write-Host "OK: flashed $resolvedElf"