X-Git-Url: https://robinkrens.nl/gitweb/?a=blobdiff_plain;f=src%2FRAConnect.py;h=2241275615876e8431ef9ca2d6a28bb3f947813c;hb=ea527f1f036f5f313d0c395a9e24ae365c22f451;hp=15a54a475620c76691e89d3f237286ed943c4770;hpb=f969a6fb10665bcaf1432b56234e60197c26a54a;p=renesas-ra-flasher diff --git a/src/RAConnect.py b/src/RAConnect.py index 15a54a4..2241275 100644 --- a/src/RAConnect.py +++ b/src/RAConnect.py @@ -3,11 +3,13 @@ import time import usb.core import usb.util +MAX_TRANSFER_SIZE = 64 + class RAConnect: def __init__(self, vendor_id, product_id): self.vendor_id = vendor_id self.product_id = product_id - self.ep_in = 0x82 + self.ep_in = 0x81 self.ep_out = 0x02 self.max_tries = 20 self.timeout_ms = 100 @@ -22,21 +24,26 @@ class RAConnect: raise ValueError(f"Device {self.vendor_id}:{self.product_id} not found\nAre you sure it is connected?") for config in self.dev: - for intf in config: - if usb.util.find_descriptor(config, custom_match=lambda d: (d.bInterfaceClass == 0x02 or d.bInterfaceClass == 0xFF)): - print("Found serial device with 0x02 | 0xFF") - if self.dev.is_kernel_driver_active(intf.bInterfaceNumber): - print("Found kernel driver, detaching ... ") - self.dev.detach_kernel_driver(intf.bInterfaceNumber) - for ep in intf: - if (ep.bmAttributes == 0x02): - if ep.bEndpointAddress == self.ep_in: - self.rx_ep = ep - print(ep) - elif ep.bEndpointAddress == self.ep_out: - self.tx_ep = ep - print(ep) - return True + #print(config) + intf = config[(1,0)] + #for intf in config: + + #if usb.util.find_descriptor(config, custom_match=lambda d: (d.bInterfaceClass == 0xa or d.bInterfaceClass == 0xBB)): + print("Found serial device with 0x0a | 0xFF") + if self.dev.is_kernel_driver_active(intf.bInterfaceNumber): + print("Found kernel driver, detaching ... ") + self.dev.detach_kernel_driver(intf.bInterfaceNumber) + for ep in intf: + #print("=========") + #print(ep) + if (ep.bmAttributes == 0x02): + if ep.bEndpointAddress == self.ep_in: + self.rx_ep = ep + print(ep) + elif ep.bEndpointAddress == self.ep_out: + self.tx_ep = ep + print(ep) + return True raise ValueError("Device does not have a serial interface") @@ -46,7 +53,7 @@ class RAConnect: self.tx_ep.write(bytes([0x00]), self.timeout_ms) ret = self.rx_ep.read(1, self.timeout_ms) if ret[0] == 0x00: - print("ACK received") + print("Reply ACK received (0x00)") return True except usb.core.USBError as e: print(f"Timeout: retry #{i}", e) @@ -58,20 +65,57 @@ class RAConnect: self.tx_ep.write(bytes([0x55]), self.timeout_ms) ret = self.rx_ep.read(1, self.timeout_ms) if ret[0] == 0xC3: - print("ACK received") + print("Reply received (0xC3)") return True except usb.core.USBError as e: print(f"Timeout: retry #{i}", e) return False -# Usage -communicator = RAConnect(vendor_id=0x1a86, product_id=0x7523) + def authenticate_connection(self): + raise Exception("Not implemented") + + def send_data(self, packed_data): + if (self.tx_ep == None): + return False + try: + self.tx_ep.write(packed_data, self.timeout_ms) + except usb.core.USBError as e: + print(f"Timeout: error", e) + return False + return True + + # packets are length 7, except for a read package + def recv_data(self, exp_len): + msg = bytearray(b'') + if (exp_len > MAX_TRANSFER_SIZE): + raise ValueError(f"length package {exp_len} over max transfer size") + if (self.rx_ep == None): + return False + try: + received = 0 + while received != exp_len: + buf = self.rx_ep.read(exp_len, self.timeout_ms) + msg += buf + print(buf, len(buf)) + received += len(buf) + if received == exp_len: + return msg + except usb.core.USBError as e: + print(f"Timeout: error", e) + #return False + return msg + + +#communicator = RAConnect(vendor_id=0x1a86, product_id=0x7523) -if not communicator.establish_connection(): - print("Cannot connect") - sys.exit(0) +#communicator.send_data(b'\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA') +#communicator.recv_data(7) -if not communicator.confirm_connection(): - print("Failed to confirm boot code") - sys.exit(0) +#if not communicator.establish_connection(): +# print("Cannot connect") +# sys.exit(0) +# +#if not communicator.confirm_connection(): +# print("Failed to confirm boot code") +# sys.exit(0)