-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathflash
executable file
·165 lines (135 loc) · 4.13 KB
/
flash
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/python3
'''
---- UoS3 OBC Firmware Flash Script ----
This script will flash the given binary onto the TM4C.
Author: Duncan Hamill ([email protected]/[email protected])
Version: 0.1
Date: 2020-11-05
Copyright (c) UoS3 2020
'''
import sys
import argparse
import subprocess
from pathlib import Path
class TermCols:
'''Terminal colour codes'''
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def error(msg, code=1):
'''Print error message and exit with an error code'''
print(f'[{TermCols.FAIL}ERR{TermCols.ENDC}] {msg}')
sys.exit(code)
def info(msg):
'''Print an information message to the screen'''
print(f'[---] {msg}')
def ok(msg):
'''Print a success message to the screen'''
print(f'[{TermCols.OKGREEN} OK{TermCols.ENDC}] {msg}')
def main():
'''
Run the main flash script.
'''
# Get the command line args
args = parse_args()
# Get the path and check it exists
elf_path = Path(args.elf)
if not elf_path.exists():
error(f'{elf_path} does not exist.')
# Check it's got an .elf extension
if not elf_path.suffix == '.elf':
error(f'Expected a .elf file, found {elf_path}')
# Flash to the correct board using either GDB for the TOBC or lm4flash for
# the launchpad
flash_ok = False
if args.target == 'launchpad':
flash_ok = flash_launchpad(elf_path)
elif args.target == 'tobc':
flash_ok = flash_tobc(elf_path, args.debug)
if not flash_ok:
error('Could not flash binary to target')
return 1
ok('Binary successfully flashed to target')
return 0
def flash_launchpad(elf_path):
'''
Flash the launchpad using lm4flash with the given ELF file path.
'''
# Get the binary path by replacing .elf with .bin
bin_path = elf_path.with_suffix('.bin')
info('Converting ELF file to binary format')
# Convert the file from elf to binary format using the right objcopy.
objcopy = subprocess.run([
'arm-none-eabi-objcopy',
'-O',
'binary',
elf_path,
bin_path
])
if objcopy.returncode != 0:
error('Could not convert from ELF to binary format')
return False
else:
ok('ELF converted to binary format')
info(f'Flashing {bin_path} using lm4flash')
# Flash the binary
lm4flash = subprocess.run([
'lm4flash',
bin_path
])
return lm4flash.returncode == 0
def flash_tobc(elf_path, debug):
'''
Use gdb to flash the TOBC with the given elf file, which should be
connected to the host via the BlackMagic probe
'''
info('Launching arm-none-eabi-gdb and flashing')
# Use gdb to flash the file, in debug mode if needed
if debug:
info('Starting debug mode')
gdb = subprocess.run([
'arm-none-eabi-gdb',
'-nx',
'-ex', 'target extended-remote /dev/serial/by-id/usb-Black_Sphere_Technologies_Black_Magic_Probe_79A779DC-if00',
'-x', 'blackmagic_debug.gdb',
elf_path
])
else:
gdb = subprocess.run([
'arm-none-eabi-gdb',
'-nx',
'--batch',
'-ex', 'target extended-remote /dev/serial/by-id/usb-Black_Sphere_Technologies_Black_Magic_Probe_79A779DC-if00',
'-x', 'blackmagic.gdb',
elf_path
])
return gdb.returncode == 0
def parse_args():
'''
Parse command line arguments
'''
# Create argparser
parser = argparse.ArgumentParser(description='Build the OBC firmware.')
parser.add_argument(
'target',
help='The target board to flash to, either `launchpad` or `tobc`'
)
parser.add_argument(
'elf',
help='The path to the ELF image to flash'
)
parser.add_argument(
'--debug',
action='store_true',
help='If flashing the TOBC start a GDB debugging session'
)
# Parse the arguments
return parser.parse_args()
if __name__ == '__main__':
main()