3 from __future__
import print_function
10 def openocd_telnet_await_prompt(s
):
17 if prev
== '>' and b
== ' ':
18 # Prompt for next command
23 def openocd_telnet_command(s
, cmd
):
25 openocd_telnet_await_prompt(s
)
27 def openocd_flash_telnet(port
, filename
):
29 s
= socket
.create_connection(('localhost', port
))
33 openocd_telnet_await_prompt(s
)
34 openocd_telnet_command(s
, 'halt')
35 openocd_telnet_command(s
, 'program {} verify reset\n'.format(filename
))
36 openocd_telnet_command(s
, 'exit')
40 def openocd_flash_cmd(openocd
, args
, filename
):
43 cmd
.extend(('-c', 'program {} verify reset exit'.format(filename
)))
44 status
= subprocess
.call(cmd
)
48 print('Usage: {} <openocd_args> <elf_file>'.format(sys
.argv
[0]))
49 print('Environment variables: OPENOCD_CMD = path to openocd')
55 # Default openocd telnet port
56 # TODO: Parse arguments and check if we
57 # should use a non-default port
59 openocd
= os
.environ
.get('OPENOCD_CMD') or 'openocd'
64 for arg
in sys
.argv
[1:]:
66 openocd_args
.append(arg
)
69 if arg
.startswith('-'):
70 openocd_args
.append(arg
)
77 if len(openocd_args
) == 0 or elf
is None:
80 if not openocd_flash_telnet(port
, elf
):
81 if not openocd_flash_cmd(openocd
, openocd_args
, elf
):
82 print('could not flash')
85 if __name__
== '__main__':