73 lines
2.7 KiB
PowerShell
73 lines
2.7 KiB
PowerShell
# Flash the BL616 on the Tang Primer 25K Dock.
|
|||
|
|
#
|
||
|
|
# .\scripts\flash.ps1 this firmware (Sipeed loader @0x0 + firmware @0x20000)
|
||
|
|
# .\scripts\flash.ps1 -Restore original Sipeed debugger firmware
|
||
|
|
#
|
||
|
|
# The BL616 has to be in its ROM USB bootloader ("Bouffalo CDC DEMO"). The
|
||
|
|
# script gets it there by itself when this firmware is already running
|
||
|
|
# (serial port opened at 1234 baud). Otherwise, unplug the board and plug it
|
||
|
|
# back while either
|
||
|
|
# - holding the S3 button (this firmware is installed), or
|
||
|
|
# - shorting the TDO test point to 3V3 (bottom side, always works).
|
||
|
|
param(
|
||
|
|
[switch]$Restore,
|
||
|
|
[string]$Port
|
||
|
|
)
|
||
|
|
$ErrorActionPreference = 'Stop'
|
||
|
|
. (Join-Path $PSScriptRoot 'common.ps1')
|
||
|
|
|
||
|
|
$root = Get-ProjectRoot
|
||
|
|
$sdk = Join-Path $root 'third_party\bouffalo_sdk'
|
||
|
|
$tool = Join-Path $sdk 'tools\bflb_tools\bouffalo_flash_cube\BLFlashCommand.exe'
|
||
|
|
$cfg = if ($Restore) { 'restore_prog_cfg.ini' } else { 'flash_prog_cfg.ini' }
|
||
|
|
|
||
|
|
if (-not $Restore -and -not (Test-Path (Join-Path $root 'build\build_out\tang25k_debugger_bl616.bin'))) {
|
||
|
|
throw 'firmware not built yet: run scripts\build.ps1'
|
||
|
|
}
|
||
|
|
|
||
|
|
function Wait-BootromPort([int]$Seconds) {
|
||
|
|
$deadline = (Get-Date).AddSeconds($Seconds)
|
||
|
|
do {
|
||
|
|
$p = @(Find-ComPort $BootromPortId)
|
||
|
|
if ($p.Count) { return $p[0] }
|
||
|
|
Start-Sleep -Milliseconds 300
|
||
|
|
} while ((Get-Date) -lt $deadline)
|
||
|
|
return $null
|
||
|
|
}
|
||
|
|
|
||
|
|
if (-not $Port) {
|
||
|
|
$Port = @(Find-ComPort $BootromPortId) | Select-Object -First 1
|
||
|
|
}
|
||
|
|
if (-not $Port) {
|
||
|
|
$fw = @(Find-ComPort $FirmwarePortId) | Select-Object -First 1
|
||
|
|
if ($fw) {
|
||
|
|
Write-Host "Firmware found on $fw, rebooting it into the ROM bootloader..."
|
||
|
|
try {
|
||
|
|
$sp = New-Object System.IO.Ports.SerialPort $fw, 1234
|
||
|
|
$sp.Open()
|
||
|
|
Start-Sleep -Milliseconds 200
|
||
|
|
$sp.Close()
|
||
|
|
} catch {
|
||
|
|
Write-Warning "could not open ${fw}: $_ (is a terminal using it?)"
|
||
|
|
}
|
||
|
|
$Port = Wait-BootromPort 15
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (-not $Port) {
|
||
|
|
Write-Host 'Waiting for the BL616 ROM bootloader (Bouffalo CDC DEMO)...' -ForegroundColor Yellow
|
||
|
|
Write-Host ' unplug the board, then plug it back while holding S3'
|
||
|
|
Write-Host ' (or while shorting the TDO test point to 3V3 on the bottom side)'
|
||
|
|
$Port = Wait-BootromPort 120
|
||
|
|
}
|
||
|
|
if (-not $Port) { throw 'BL616 bootloader port not found' }
|
||
|
|
|
||
|
|
Write-Host "Flashing $cfg via $Port" -ForegroundColor Cyan
|
||
|
|
Push-Location $root
|
||
|
|
try {
|
||
|
|
& $tool --interface=uart --baudrate=2000000 --port=$Port --chipname=bl616 --cpu_id= --config=$cfg
|
||
|
|
if ($LASTEXITCODE) { throw "BLFlashCommand failed ($LASTEXITCODE)" }
|
||
|
|
} finally {
|
||
|
|
Pop-Location
|
||
|
|
}
|
||
|
|
Write-Host 'Done. Unplug and plug the board back (power cycle) to start the new firmware.' -ForegroundColor Green
|