Commit cfa952b0 authored by Florent Kermarrec's avatar Florent Kermarrec

tools/litex_term: exit on 2 consecutive CTRL-C

When running OS with LiteX and when LiteXTerm is use, we want to be able to
send CTRl-C to the OS. Ensure a specific sequence is sent to close the terminal.
parent 1c34b4a0
#!/usr/bin/env python3 #!/usr/bin/env python3
import sys import sys
import signal
import os import os
import time import time
import serial import serial
...@@ -40,7 +41,6 @@ else: ...@@ -40,7 +41,6 @@ else:
def getkey(self): def getkey(self):
return os.read(self.fd, 1) return os.read(self.fd, 1)
sfl_prompt_req = b"F7: boot from serial\n" sfl_prompt_req = b"F7: boot from serial\n"
sfl_prompt_ack = b"\x06" sfl_prompt_ack = b"\x06"
...@@ -140,6 +140,9 @@ class LiteXTerm: ...@@ -140,6 +140,9 @@ class LiteXTerm:
self.console = Console() self.console = Console()
signal.signal(signal.SIGINT, self.sigint)
self.sigint_time_last = 0
def open(self, port, baudrate): def open(self, port, baudrate):
if hasattr(self, "port"): if hasattr(self, "port"):
return return
...@@ -151,6 +154,17 @@ class LiteXTerm: ...@@ -151,6 +154,17 @@ class LiteXTerm:
self.port.close() self.port.close()
del self.port del self.port
def sigint(self, sig, frame):
self.port.write(b"\x03")
sigint_time_current = time.time()
# Exit term if 2 CTRL-C pressed in less than 0.5s.
if (sigint_time_current - self.sigint_time_last < 0.5):
self.console.unconfigure()
self.close()
sys.exit()
else:
self.sigint_time_last = sigint_time_current
def send_frame(self, frame): def send_frame(self, frame):
retry = 1 retry = 1
while retry: while retry:
...@@ -316,15 +330,9 @@ def main(): ...@@ -316,15 +330,9 @@ def main():
args = _get_args() args = _get_args()
term = LiteXTerm(args.serial_boot, args.kernel, args.kernel_adr, args.images) term = LiteXTerm(args.serial_boot, args.kernel, args.kernel_adr, args.images)
term.console.configure() term.console.configure()
try: term.open(args.port, int(float(args.speed)))
term.open(args.port, int(float(args.speed))) term.start()
term.start() term.join(True)
term.join(True)
except KeyboardInterrupt:
term.console.unconfigure()
finally:
term.console.unconfigure()
term.close()
if __name__ == "__main__": if __name__ == "__main__":
main() main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment