-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflash.ps1
116 lines (89 loc) · 3.26 KB
/
flash.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
$start = Get-Date
Write-Host "MLNDEV Flasher v1.0.0" -ForegroundColor Magenta
Write-Host "-----------------------"
if ($args.Length -lt 2) {
Write-Host "Usage: flash.ps1 <file> <jlink|jlink-rtt|stlink>" -ForegroundColor Yellow
exit 1
}
# take first argument as file name
$fileName = $args[0]
# take second argument as programming method, if empty, use jlink
$method = $args[1]
# Install powershell-yaml if not installed
if (-not (Get-Module -ListAvailable -Name powershell-yaml)) {
Write-Host "Installing powershell-yaml module..." -ForegroundColor Yellow
Install-Module -Name powershell-yaml -Scope CurrentUser -Force -Confirm
Write-Host "powershell-yaml module installed" -ForegroundColor Green
}
# Get settings from flash-settings.yaml
$settings = Get-Content .vscode\flash-settings.yaml | ConvertFrom-Yaml
Write-Host "Device: " -NoNewline
Write-Host $settings.device -ForegroundColor Blue
Write-Host "Interface: " -NoNewline
Write-Host $settings.interface -ForegroundColor Blue
Write-Host "Speed: " -NoNewline
Write-Host $settings.speed -ForegroundColor Blue
# echo flashing method in blue
Write-Host "Programming method: " -NoNewline
Write-Host $method -ForegroundColor Blue
$fileName = (Get-Item $fileName).DirectoryName + "\" + (Get-Item $fileName).BaseName + ".bin"
# echo file name in blue
Write-Host "Flashing file: " -NoNewline
Write-Host $fileName -ForegroundColor Blue
Write-Host
# if programming method is jlink
if ($method -eq "jlink") {
# Create JLink script
@"
loadfile $fileName 0x08000000
r
g
q
"@ | Out-File flash.jlink -Encoding ASCII
# Permanently change terminal color to dark gray
$prevForegroundColor = $Host.UI.RawUI.ForegroundColor
$Host.UI.RawUI.ForegroundColor = "DarkGray"
# Run JLink
& "C:\Program Files\SEGGER\JLink\JLink.exe" -NoGui 1 -ExitOnError 1 -device $settings.device -if $settings.interface -speed $settings.speed -autoconnect 1 -CommanderScript flash.jlink
# Change terminal color to default
$Host.UI.RawUI.ForegroundColor = $prevForegroundColor
# Remove JLink script
Remove-Item flash.jlink
}
elseif ($method -eq "jlink-rtt") {
# Replace .bin with .elf
$fileName = $fileName -replace ".bin", ".elf"
# Run JLink
& "C:\Program Files\SEGGER\JLink\JRun.exe" -device $settings.device -if $settings.interface -speed $settings.speed -s --rtt $fileName
# Remove JLink script
Remove-Item flash.jlink
}
elseif ($method -eq "stlink") {
# Run STLink
& st-flash write $fileName 0x8000000
}
elseif ($method -eq "cmsis-dap") {
# Run OpenOCD
$openocdTarget = $settings.openocd
@"
source [find interface/cmsis-dap.cfg]
adapter speed 8000
transport select swd
source [find target/$openocdTarget]
"@ | Out-File flash.cfg -Encoding ASCII
# Replace backslashes with forward slashes
$fileName = $fileName -replace "\\", "/"
# Replace .bin with .elf
$fileName = $fileName -replace ".bin", ".elf"
& openocd -f flash.cfg -c "program $fileName verify reset exit"
# Remove OpenOCD script
Remove-Item flash.cfg
}
else {
Write-Host "Unknown programming method: $method" -ForegroundColor Red
exit 1
}
Write-Host
Write-Host "Done after " -NoNewline
Write-Host ((Get-Date) - $start).TotalSeconds.ToString("N2") -ForegroundColor Green -NoNewline
Write-Host " seconds"