2026-11-25 14:43:34 +01:00
# One command for the overnight run on the SPARE board (no battery): optionally builds and flashes this checkout's firmware,
# runs the PC-side C tests in the background, runs the board tests, and keeps Windows awake meanwhile.
#
# powershell -ExecutionPolicy Bypass -File obu-firmware\test\bench\start_overnight.ps1 -Flash -Hours 8
# ... -Port COM6 name the port (default: the one Espressif native-USB board that is plugged in)
# ... -Quick about 8 minutes on the board, no host fuzz: does everything work?
# ... -NoTx the radio stays silent
# ... -NoHostTests skip the PC-side C tests
#
# Before it: git pull, close everything that holds the port (the phone app, station_status_watch.py, a serial monitor).
# Opening the port resets the board, and the run reconfigures it for hours and writes the demo ticket to its NVS: plug in
# ONLY the spare board (a board without a cell reads about 4000 mV from the USB charger, so battery_mv cannot tell them apart).
param (
[ double ] $Hours = 8 ,
[ string ] $Port = "auto" ,
[ switch ] $Flash ,
[ switch ] $Quick ,
[ switch ] $NoTx ,
[ switch ] $NoHostTests ,
[ string ] $IdfVersion = "v6.0.2" ,
[ string ] $IdfToolsPath = "C:\Espressif" ,
[ string ] $Msys = "C:\msys64"
)
$ErrorActionPreference = "Stop"
$bench = $PSScriptRoot
$fw = ( Resolve-Path ( Join-Path $bench "..\.." )). Path # obu-firmware
$py = if ( Get-Command py -ErrorAction SilentlyContinue ) { @ ( "py" , "-3.11" ) } else { @ ( Join-Path $env:LOCALAPPDATA "Programs\Python\Python311\python.exe" ) }
function Run-Python([string[]]$a ) { $rest = @ (); if ( $py . Count -gt 1 ) { $rest = $py [ 1 ..( $py . Count - 1 )] }; & $py [ 0 ] @rest @a }
# Keep Windows awake for as long as this PowerShell process lives.
Add-Type -Namespace Win -Name Power -MemberDefinition '[DllImport("kernel32.dll")] public static extern uint SetThreadExecutionState(uint f);'
2026-11-27 10:58:30 +01:00
[ void][Win.Power ]:: SetThreadExecutionState ([ uint32 ] 2147483649 ) # 0x80000001 = ES_CONTINUOUS | ES_SYSTEM_REQUIRED (PowerShell 5.1 reads that literal as a negative int)
2026-11-25 14:43:34 +01:00
# Which board: the one Espressif native-USB port, unless named.
if ( $Port -eq "auto" ) {
$found = @ ( Run-Python @ ( "-c" , "from serial.tools import list_ports; print(' '.join(p.device for p in list_ports.comports() if p.vid == 0x303A))" ) -split "\s+" | Where-Object { $_ })
if ( $found . Count -ne 1 ) { Write-Host "Found $( $found . Count ) Espressif USB boards ( $( $found -join ', ' ) ): pass -Port COMx." -ForegroundColor Red ; exit 2 }
$Port = $found [ 0 ]
}
Write-Host "Board port: $Port "
if ( $Flash ) {
$idf = Join-Path $IdfToolsPath "frameworks\esp-idf- $IdfVersion \export.ps1"
if ( -not ( Test-Path $idf )) { Write-Host "ESP-IDF $IdfVersion not found at ${idf} : run setup_workstation.ps1 first." -ForegroundColor Red ; exit 2 }
# A fresh PowerShell, so no other IDF's environment is in the way (two exports in one window fail).
$cmd = " `$ env:IDF_TOOLS_PATH=' $IdfToolsPath '; Remove-Item Env:IDF_PYTHON_ENV_PATH,Env:IDF_PATH -ErrorAction SilentlyContinue; " +
"& ' $idf ' *> `$ null; Set-Location ' $fw '; " +
"if (-not (Test-Path build\CMakeCache.txt)) { idf.py set-target esp32c5; if ( `$ LASTEXITCODE) { exit 1 } }; " +
"idf.py build; if ( `$ LASTEXITCODE) { exit 1 }; " +
"Set-Location build; python -m esptool --chip esp32c5 -p $Port -b 460800 --before default-reset --after hard-reset write-flash '@flash_args'; exit `$ LASTEXITCODE"
Write-Host "Building and flashing (the first build downloads components and takes ~20 minutes) ..."
& powershell -NoProfile -ExecutionPolicy Bypass -Command $cmd
if ( $LASTEXITCODE -ne 0 ) {
Write-Host ( "Build or flash failed. If the port was not found or no serial data arrived: unplug the board, hold BOOT, plug in, release, run again; " +
"if the stub flasher will not start, see obu-firmware/FLASHING.md ('--no-stub'). Unplug and replug WITHOUT BOOT afterwards." ) -ForegroundColor Red
exit 3
}
Start-Sleep -Seconds 5 # the board re-enumerates after the hard reset
}
# PC-side C tests, in the background (they need no board).
$hostJob = $null
if ( -not $NoHostTests -and -not $Quick ) {
if ( Test-Path ( Join-Path $Msys "ucrt64\bin\gcc.exe" )) {
$hostJob = Start-Process powershell -PassThru -WindowStyle Minimized -ArgumentList @ ( "-NoProfile" , "-ExecutionPolicy" , "Bypass" , "-File" , ( Join-Path $bench "run_host_tests_overnight.ps1" ), "-Hours" , [ string ]( $Hours - 0.2 ), "-Msys" , $Msys )
Write-Host "PC-side tests started in the background (pid $( $hostJob . Id ) ); results in obu-firmware\test\results\host-*"
} else { Write-Host "MSYS2 gcc not found: skipping the PC-side tests (setup_workstation.ps1 installs it)." -ForegroundColor Yellow }
}
# The board tests.
$runArgs = @ (( Join-Path $bench "run_overnight.py" ), $Port , "--provision-demo" )
if ( $Quick ) { $runArgs += "--quick" } else { $runArgs += @ ( "--hours" , [ string ] $Hours ) }
if ( $NoTx ) { $runArgs += "--no-tx" }
Run-Python $runArgs
$code = $LASTEXITCODE
if ( $hostJob ) {
Write-Host "Waiting for the PC-side tests ..."
$hostJob . WaitForExit ()
Write-Host "PC-side tests exit code: $( $hostJob . ExitCode ) "
if ( $hostJob . ExitCode -ne 0 -and $code -eq 0 ) { $code = 1 }
}
Write-Host "Board tests exit code: $code . Reports: obu-firmware\test\results\overnight-*\report.txt and host-*\summary.txt"
exit $code