RAFlasher.py: signature request info
[renesas-ra-flasher] / src / RAConnect.py
1 import sys
2 import time
3 import usb.core
4 import usb.util
5
6 MAX_TRANSFER_SIZE = 64
7
8 class RAConnect:
9     def __init__(self, vendor_id, product_id):
10         self.vendor_id = vendor_id
11         self.product_id = product_id
12         self.ep_in = 0x81
13         self.ep_out = 0x02
14         self.max_tries = 20
15         self.timeout_ms = 100
16         self.dev = None
17         self.rx_ep = None
18         self.tx_ep = None
19         self.find_device()
20
21     def find_device(self):
22         self.dev = usb.core.find(idVendor=self.vendor_id, idProduct=self.product_id)
23         if self.dev is None:
24             raise ValueError(f"Device {self.vendor_id}:{self.product_id} not found\nAre you sure it is connected?")
25
26         for config in self.dev:
27             #print(config)
28             intf = config[(1,0)]
29             #for intf in config:
30                 
31                 #if usb.util.find_descriptor(config, custom_match=lambda d: (d.bInterfaceClass == 0xa or d.bInterfaceClass == 0xBB)):
32             print("Found serial device with 0x0a | 0xFF")
33             if self.dev.is_kernel_driver_active(intf.bInterfaceNumber):
34                 print("Found kernel driver, detaching ... ")
35                 self.dev.detach_kernel_driver(intf.bInterfaceNumber)
36             for ep in intf:
37                 #print("=========")
38                 #print(ep)
39                 if (ep.bmAttributes == 0x02):
40                     if ep.bEndpointAddress == self.ep_in:
41                         self.rx_ep = ep
42                         print(ep)
43                     elif ep.bEndpointAddress == self.ep_out:
44                         self.tx_ep = ep
45                         print(ep)
46             return True
47
48         raise ValueError("Device does not have a serial interface")
49
50     def establish_connection(self):
51         for i in range(self.max_tries):
52             try:
53                 self.tx_ep.write(bytes([0x00]), self.timeout_ms)
54                 ret = self.rx_ep.read(1, self.timeout_ms)
55                 if ret[0] == 0x00:
56                     print("Reply ACK received (0x00)")
57                     return True
58             except usb.core.USBError as e:
59                 print(f"Timeout: retry #{i}", e)
60         return False
61
62     def confirm_connection(self):
63         for i in range(self.max_tries):
64             try:
65                 self.tx_ep.write(bytes([0x55]), self.timeout_ms)
66                 ret = self.rx_ep.read(1, self.timeout_ms)
67                 if ret[0] == 0xC3:
68                     print("Reply received (0xC3)")
69                     return True
70             except usb.core.USBError as e:
71                 print(f"Timeout: retry #{i}", e)
72         return False
73
74     def authenticate_connection(self):
75         raise Exception("Not implemented")
76
77     def send_data(self, packed_data):
78         if (self.tx_ep == None):
79             return False
80         try:
81             self.tx_ep.write(packed_data, self.timeout_ms)
82         except usb.core.USBError as e:
83             print(f"Timeout: error", e)
84             return False
85         return True
86
87     # packets are length 7, except for a read package
88     def recv_data(self, exp_len):
89         msg = bytearray(b'')
90         if (exp_len > MAX_TRANSFER_SIZE):
91             raise ValueError(f"length package {exp_len} over max transfer size")
92         if (self.rx_ep == None):
93             return False
94         try:
95             received = 0
96             while received != exp_len:
97                 buf = self.rx_ep.read(exp_len, self.timeout_ms)
98                 msg += buf
99                 print(buf, len(buf))
100                 received += len(buf)
101                 if received == exp_len:
102                     return msg
103         except usb.core.USBError as e:
104             print(f"Timeout: error", e)
105             #return False
106         return msg
107
108
109 #communicator = RAConnect(vendor_id=0x1a86, product_id=0x7523)
110
111 #communicator.send_data(b'\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA')
112 #communicator.recv_data(7)
113
114 #if not communicator.establish_connection():
115 #    print("Cannot connect")
116 #    sys.exit(0)
117 #
118 #if not communicator.confirm_connection():
119 #    print("Failed to confirm boot code")
120 #    sys.exit(0)
121