# Flash archived CYD BLE firmware (no rebuild needed) # Usage: # .\flash.ps1 # .\flash.ps1 -Port COM5 # .\flash.ps1 -Port COM5 -Baud 460800 param( [string]$Port = "", [int]$Baud = 921600 ) $ErrorActionPreference = "Stop" $here = Split-Path -Parent $MyInvocation.MyCommand.Path Set-Location $here function Find-Esptool { $pio = Get-Command pio -ErrorAction SilentlyContinue if (-not $pio) { $pioPath = Join-Path $env:USERPROFILE "AppData\Local\Programs\Python\Python312\Scripts\pio.exe" if (Test-Path $pioPath) { $env:Path = "$(Split-Path $pioPath);$env:Path" $pio = Get-Command pio -ErrorAction SilentlyContinue } } if ($pio) { $pkg = Join-Path $env:USERPROFILE ".platformio\packages\tool-esptoolpy\esptool.py" if (Test-Path $pkg) { return @{ Kind = "py"; Path = $pkg } } } $py = Get-Command python -ErrorAction SilentlyContinue if ($py) { try { & python -c "import esptool" 2>$null if ($LASTEXITCODE -eq 0) { return @{ Kind = "mod"; Path = "esptool" } } } catch {} } $esp = Get-Command esptool.py -ErrorAction SilentlyContinue if ($esp) { return @{ Kind = "py"; Path = $esp.Source } } return $null } if (-not $Port) { $ports = @() try { Add-Type -AssemblyName System.IO.Ports -ErrorAction SilentlyContinue | Out-Null $ports = [System.IO.Ports.SerialPort]::GetPortNames() | Sort-Object } catch {} if (-not $ports -or $ports.Count -eq 0) { $ports = Get-CimInstance Win32_SerialPort -ErrorAction SilentlyContinue | Select-Object -ExpandProperty DeviceID | Sort-Object } if (-not $ports -or $ports.Count -eq 0) { Write-Error "No serial ports found. Plug in the CYD USB cable, then retry with -Port COMx" } if ($ports.Count -eq 1) { $Port = $ports[0] } else { Write-Host "Available ports: $($ports -join ', ')" $Port = $ports[-1] Write-Host "Using last port: $Port (override with -Port)" } } $tool = Find-Esptool if (-not $tool) { Write-Error "esptool not found. Install PlatformIO (`pio`) or: pip install esptool" } $needed = @("bootloader.bin", "partitions.bin", "boot_app0.bin", "firmware.bin") foreach ($f in $needed) { if (-not (Test-Path (Join-Path $here $f))) { Write-Error "Missing $f — run ..\export_from_build.ps1 from repo root first" } } $argsCommon = @( "--chip", "esp32", "--port", $Port, "--baud", "$Baud", "--before", "default_reset", "--after", "hard_reset", "write_flash", "-z", "--flash_mode", "dio", "--flash_freq", "40m", "--flash_size", "4MB", "0x1000", "bootloader.bin", "0x8000", "partitions.bin", "0xe000", "boot_app0.bin", "0x10000", "firmware.bin" ) Write-Host "Flashing $here -> $Port @ $Baud" if ($tool.Kind -eq "mod") { & python -m esptool @argsCommon } else { & python $tool.Path @argsCommon } if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } Write-Host "Done. Open serial monitor at 460800 if needed."