From f969a6fb10665bcaf1432b56234e60197c26a54a Mon Sep 17 00:00:00 2001 From: Robin Krens Date: Sat, 10 Feb 2024 18:37:45 +0100 Subject: [PATCH] dir: some restructering and refactoring - naming: connect.py > RAConnect.py flasher.py > Packer.py - .gitignore to mostly ignore cache - setup.py project info --- .gitignore | 6 ++ flasher/connect.py | 97 ----------------------------- setup.py | 6 +- flasher/flasher.py => src/Packer.py | 0 src/RAConnect.py | 77 +++++++++++++++++++++++ tests/test_parse.py | 2 +- 6 files changed, 87 insertions(+), 101 deletions(-) create mode 100644 .gitignore delete mode 100644 flasher/connect.py rename flasher/flasher.py => src/Packer.py (100%) create mode 100644 src/RAConnect.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2925ae6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +flasher.egg-info/ +ra_flasher.egg-info/ +src/__pycache__/ +tests/__pycache__/ +.venv/ + diff --git a/flasher/connect.py b/flasher/connect.py deleted file mode 100644 index 91f21d5..0000000 --- a/flasher/connect.py +++ /dev/null @@ -1,97 +0,0 @@ -# Copyright (C) - Robin Krens - 2024 -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -# - -import sys -import serial -import serial.threaded -import time -import usb.core -import usb.util - -LOW_PULSE = 0x00 -ACK = 0x00 -GENERIC_CODE = 0x55 -BOOT_CODE = 0xC3 - -VENDOR_ID = 0x045B -PRODUCT_ID = 0x0261 - -EP_IN = 0x82 -EP_OUT = 0x02 - -MAX_TRIES = 20 - -def find_device(vendor_id, product_id): - dev = usb.core.find(idVendor=vendor_id, idProduct=product_id) - if dev is None: - raise ValueError(f"Device {vendor_id}:{product_id} not found\n Are you sure it is connected?") - - for config in dev: - for intf in config: - if usb.util.find_descriptor(config, custom_match=lambda d: (d.bInterfaceClass == 0x02 or d.bInterfaceClass == 0xFF)): - print(f"Found serial device with 0x02 | 0xFF") - TxD, RxD = None, None - if dev.is_kernel_driver_active(intf.bInterfaceNumber): - print(f"Found kernel driver, detaching ... ") - dev.detach_kernel_driver(intf.bInterfaceNumber) - for ep in intf: - if (ep.bmAttributes == 0x02): - if ep.bEndpointAddress == EP_IN: - RxD = ep - print(ep) - elif ep.bEndpointAddress == EP_OUT: - TxD = ep - print(ep) - return (dev, RxD, TxD) - - raise ValueError("Device does not have a serial interface") - -def establish_connection(): - dev, rx_ep, tx_ep = find_device(0x1a86, 0x7523) - ret = [] - for i in range(MAX_TRIES): - try: - tx_ep.write(b'\x00', 100) - ret = rx_ep.read(1, 100) - if a[0] == ACK: - print("ACK received") - return True - except: - print(f"Timeout: retry #", i) - return False - -def confirm_connection(): - dev, rx_ep, tx_ep = find_device(0x1a86, 0x7523) - ret = [] - for i in range(MAX_TRIES): - try: - tx_ep.write(b'\x55', 100) - ret = rx_ep.read(1, 100) - if a[0] == BOOT_CODE: - print("ACK received") - return True - except: - print(f"Timeout: retry #", i) - return False - -if not establish_connection(): - print("Cannot connect") - sys.exit(0) - -if not confirm_connection(): - print("Failed to confirm boot code") - sys.exit(0) diff --git a/setup.py b/setup.py index 2b2880f..7ef5070 100644 --- a/setup.py +++ b/setup.py @@ -10,13 +10,13 @@ def read(fname): return open(os.path.join(os.path.dirname(__file__), fname)).read() setup( - name = "flasher", + name = "ra-flasher", version = "0.0.1", author = "Robin Krens", description = ("An example of how to set up pytest"), license = "GNU", - keywords = "example pytest", - packages=['flasher', 'tests'], + keywords = "Renesas RA chipset flasher", + packages=['src', 'tests'], #long_description=read('README.md'), classifiers=[ "Development Status :: 1", diff --git a/flasher/flasher.py b/src/Packer.py similarity index 100% rename from flasher/flasher.py rename to src/Packer.py diff --git a/src/RAConnect.py b/src/RAConnect.py new file mode 100644 index 0000000..15a54a4 --- /dev/null +++ b/src/RAConnect.py @@ -0,0 +1,77 @@ +import sys +import time +import usb.core +import usb.util + +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_out = 0x02 + self.max_tries = 20 + self.timeout_ms = 100 + self.dev = None + self.rx_ep = None + self.tx_ep = None + self.find_device() + + def find_device(self): + self.dev = usb.core.find(idVendor=self.vendor_id, idProduct=self.product_id) + if self.dev is None: + 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 + + raise ValueError("Device does not have a serial interface") + + def establish_connection(self): + for i in range(self.max_tries): + try: + 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") + return True + except usb.core.USBError as e: + print(f"Timeout: retry #{i}", e) + return False + + def confirm_connection(self): + for i in range(self.max_tries): + try: + 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") + 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) + +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) + diff --git a/tests/test_parse.py b/tests/test_parse.py index 81697d9..c498c28 100644 --- a/tests/test_parse.py +++ b/tests/test_parse.py @@ -1,4 +1,4 @@ -from flasher.flasher import calc_sum, unpack_pkt, pack_pkt +from src.Packer import calc_sum, unpack_pkt, pack_pkt import pytest def test_calc_sum(): -- 2.43.0