forked from iNavFlight/inav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenocd_flash.py
executable file
·86 lines (72 loc) · 2.08 KB
/
openocd_flash.py
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
#!/usr/bin/env python
from __future__ import print_function
import os
import socket
import subprocess
import sys
def openocd_telnet_await_prompt(s):
prev = None
while True:
b = s.recv(1)
if b == '':
# Closed
return False
if prev == '>' and b == ' ':
# Prompt for next command
return True
prev = b
print(b, end='')
def openocd_telnet_command(s, cmd):
s.send(cmd + '\n')
openocd_telnet_await_prompt(s)
def openocd_flash_telnet(port, filename):
try:
s = socket.create_connection(('localhost', port))
except socket.error:
return False
openocd_telnet_await_prompt(s)
openocd_telnet_command(s, 'halt')
openocd_telnet_command(s, 'program {} verify reset\n'.format(filename))
openocd_telnet_command(s, 'exit')
s.close()
return True
def openocd_flash_cmd(openocd, args, filename):
cmd = [openocd]
cmd.extend(args)
cmd.extend(('-c', 'program {} verify reset exit'.format(filename)))
status = subprocess.call(cmd)
return status == 0
def usage():
print('Usage: {} <openocd_args> <elf_file>'.format(sys.argv[0]))
print('Environment variables: OPENOCD_CMD = path to openocd')
sys.exit(1)
def main():
import sys
# Default openocd telnet port
# TODO: Parse arguments and check if we
# should use a non-default port
port = 4444
openocd = os.environ.get('OPENOCD_CMD') or 'openocd'
openocd_args = []
flag = None
elf = None
for arg in sys.argv[1:]:
if flag:
openocd_args.append(arg)
flag = None
else:
if arg.startswith('-'):
openocd_args.append(arg)
flag = arg
elif elf is None:
elf = arg
else:
usage()
if len(openocd_args) == 0 or elf is None:
usage()
if not openocd_flash_telnet(port, elf):
if not openocd_flash_cmd(openocd, openocd_args, elf):
print('could not flash')
sys.exit(1)
if __name__ == '__main__':
main()