Merge pull request #161 from enjoy-digital/litex_server_arguments

litex_server: refactor parameters and to allow setting bind address
This commit is contained in:
enjoy-digital 2019-04-15 08:24:28 +02:00 committed by GitHub
commit 66a74b1579
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 60 additions and 42 deletions

View File

@ -1,5 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import argparse
import sys import sys
import socket import socket
import time import time
@ -10,22 +12,19 @@ from litex.soc.tools.remote.etherbone import EtherboneIPC
class RemoteServer(EtherboneIPC): class RemoteServer(EtherboneIPC):
def __init__(self, comm, port=1234): def __init__(self, comm, bind_ip, bind_port=1234):
self.comm = comm self.comm = comm
self.port = port self.bind_ip = bind_ip
self.bind_port = bind_port
self.lock = False self.lock = False
def open(self): def open(self):
if hasattr(self, "socket"): if hasattr(self, "socket"):
return return
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
for i in range(32): self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
try: self.socket.bind((self.bind_ip, self.bind_port))
self.socket.bind(("localhost", self.port + i)) print("tcp port: {:d}".format(self.bind_port))
break
except:
pass
print("tcp port: {:d}".format(self.port + i))
self.socket.listen(1) self.socket.listen(1)
self.comm.open() self.comm.open()
@ -95,46 +94,65 @@ class RemoteServer(EtherboneIPC):
def main(): def main():
print("LiteX remote server") print("LiteX remote server")
if len(sys.argv) < 2 or len(sys.argv) > 4: parser = argparse.ArgumentParser()
print("usages:") # Common arguments
print("litex_server uart [port] [baudrate]") parser.add_argument("--bind-ip", default="localhost",
print("litex_server udp [server] [server_port]") help="Host bind address")
print("litex_server pcie [bar]") parser.add_argument("--bind-port", default=1234,
sys.exit() help="Host bind port")
comm = sys.argv[1]
if comm == "uart": # UART arguments
parser.add_argument("--uart", action="store_true",
help="Select UART interface")
parser.add_argument("--uart-port", default=None,
help="Set UART port")
parser.add_argument("--uart-baudrate", default=115200,
help="Set UART baudrate")
# UDP arguments
parser.add_argument("--udp", action="store_true",
help="Select UDP interface")
parser.add_argument("--udp-ip", default="192.168.1.50",
help="Set UDP remote IP address")
parser.add_argument("--udp-port", default=1234,
help="Set UDP remote port")
# PCIe arguments
parser.add_argument("--pcie", action="store_true",
help="Select PCIe interface")
parser.add_argument("--pcie-bar", default=None,
help="Set PCIe BAR")
args = parser.parse_args()
if args.uart:
from litex.soc.tools.remote import CommUART from litex.soc.tools.remote import CommUART
uart_port = None if args.uart_port is None:
uart_baudrate = 115200 print("Need to specify --uart-port, exiting.")
if len(sys.argv) > 2: exit()
uart_port = sys.argv[2] uart_port = args.uart_port
if len(sys.argv) > 3: uart_baudrate = int(float(args.uart_baudrate))
uart_baudrate = int(float(sys.argv[3]))
print("[CommUART] port: {} / baudrate: {} / ".format(uart_port, uart_baudrate), end="") print("[CommUART] port: {} / baudrate: {} / ".format(uart_port, uart_baudrate), end="")
comm = CommUART(uart_port, uart_baudrate) comm = CommUART(uart_port, uart_baudrate)
elif comm == "udp": elif args.udp:
from litex.soc.tools.remote import CommUDP from litex.soc.tools.remote import CommUDP
server = "192.168.1.50" udp_ip = args.udp_ip
server_port = 1234 udp_port = int(args.udp_port)
if len(sys.argv) > 2: print("[CommUDP] ip: {} / port: {} / ".format(udp_ip, udp_port), end="")
server = sys.argv[2] comm = CommUDP(udp_ip, udp_port)
if len(sys.argv) > 3: elif args.pcie:
server_port = int(sys.argv[3])
print("[CommUDP] server: {} / port: {} / ".format(server, server_port), end="")
comm = CommUDP(server, server_port)
elif comm == "pcie":
from litex.soc.tools.remote import CommPCIe from litex.soc.tools.remote import CommPCIe
bar = "" pcie_bar = args.pcie_bar
if len(sys.argv) > 2: if args.pcie_bar is None:
bar = sys.argv[2] print("Need to speficy --pcie-bar, exiting.")
if len(sys.argv) > 3: exit()
bar_size = int(sys.argv[3]) print("[CommPCIe] bar: {} / ".format(args.pcie_bar), end="")
print("[CommPCIe] bar: {} / ".format(bar), end="") comm = CommPCIe(args.pcie_bar)
comm = CommPCIe(bar)
else: else:
raise NotImplementedError parser.print_help()
exit()
server = RemoteServer(comm) server = RemoteServer(comm, args.bind_ip, int(args.bind_port))
server.open() server.open()
server.start(4) server.start(4)
try: try: