send/receive jtagremote protocol
[soc.git] / src / soc / debug / test / jtagremote.py
1 #The server code
2 import socket
3 from socket import close, AF_INET, SOCK_STREAM
4 import sys
5 import select
6 import time
7
8
9 def server():
10 HOST = ''
11 PORT = 44853
12 s = socket.socket(AF_INET, SOCK_STREAM)
13 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
14 s.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)
15 s.bind((HOST, PORT))
16 s.listen(1) #only needs to receive one connection (the client)
17 return s
18
19 def get_connection(s):
20 r, w, e = select.select( [s], [], [], 0)
21 for sock in r:
22 #incoming message from remote server
23 if sock == s:
24 conn, addr = s.accept() #accepts the connection
25 print("Connected by: ", addr) #prints the connection
26 conn.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)
27 return conn
28 return None
29
30
31 def get_data(s, length=1024, timeout=None):
32 r, w, e = select.select( [s], [], [], timeout)
33
34 for sock in r:
35 #incoming message from remote server
36 if sock == s:
37 return sock.recv(length)
38 return None
39
40 def client():
41 HOST = 'localhost'
42 PORT = 44853
43 s = socket.socket(AF_INET, SOCK_STREAM)
44 s.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)
45 s.connect((HOST, PORT))
46 return s
47
48
49 def jtagremote_client_send(c, to_send):
50 # encode tck, tms and tdi as number from 0-7
51 tck, tms, tdi = to_send
52 data = 0
53 if tdi: data |= 1
54 if tms: data |= 2
55 if tck: data |= 4
56 data = chr(ord('0') + data)
57 c.sendall(str.encode(data))
58 print ("jtagremote_client_send", data)
59 # now read tdo
60 c.sendall(str.encode('R'))
61
62
63 def jtagremote_client_recv(c):
64 data = get_data(c, 1) # read 1 byte, blocking
65 print ("client recv", data)
66 data = bytes.decode(data)
67 return ord(data) - ord('0') # subtract ASCII for "0" to give value 0 or 1
68
69
70 def jtagremote_server_recv(s, tdo):
71 data = get_data(s, 1, 0) # read 1 byte, non-blocking
72 if data is None:
73 return None # no data read
74 data = bytes.decode(data)
75 print ("jtagremote_server_recv", data)
76 # request to read TDO
77 if data == 'R':
78 s.sendall(str.encode(chr(ord('0') + tdo)))
79 return [] # no data
80 # decode tck, tms, tdi
81 data = ord(data) - ord('0')
82 # encode tck, tms and tdi as number from 0-7
83 tdi = 1 if (data & 1) else 0
84 tms = 1 if (data & 2) else 0
85 tck = 1 if (data & 3) else 0
86
87 return (tck, tms, tdi)
88
89
90 def test_clientserver_jtagremote():
91 s = server()
92 c = client()
93 sc = get_connection(s)
94
95 jtagremote_client_send(c, (1, 0, 1))
96 while True:
97 resp = jtagremote_server_recv(sc, 1)
98 if resp is not None:
99 print ("response", resp)
100 break
101
102 while True:
103 resp = jtagremote_server_recv(sc, 1)
104 if resp is not None:
105 print ("response", resp)
106 break
107
108 tdo = jtagremote_client_recv(c)
109 print ("client recv", tdo)
110
111 s.close()
112 sc.close()
113 c.close()
114
115
116 def test_clientserver():
117 s = server()
118 c = client()
119 sc = get_connection(s)
120
121 c.sendall(str.encode("h"))
122 while True:
123 resp = get_data(sc)
124 if resp is not None:
125 print ("response", resp)
126 break
127 s.close()
128 sc.close()
129 c.close()
130
131
132 if __name__ == '__main__':
133 #test_clientserver()
134 test_clientserver_jtagremote()
135