Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to get device for CH559 #16

Open
maxgerhardt opened this issue Apr 2, 2021 · 9 comments
Open

Failed to get device for CH559 #16

maxgerhardt opened this issue Apr 2, 2021 · 9 comments

Comments

@maxgerhardt
Copy link

maxgerhardt commented Apr 2, 2021

I've put my device into USB bootloader mode and I've previously installed libusb-win32 drivers via Zadig.

When I try to upload the firmware I get

>python  -m ch55xtool  -f .pio\build\ch559\firmware.bin -r
Failed to get device, please check your libusb installation.

When I call into the tool by https://github.com/LoveMHz/vnproch55x, directly after that, with no USB device reconnected or anything

>C:\Users\Max\Desktop\tools\win\vnproch55x.exe .pio\build\ch559\firmware.bin
------------------------------------------------------------------
CH55x Programmer by VNPro
------------------------------------------------------------------
Found Device CH559
Bootloader: 2.3.1
ID: 1B AC 4B 41
XOR Mask: 53 53 53 53 53 53 53 AC
Write 194 bytes from bin file.
..................................................
Write complete!!!
Verify chip
..................................................
Verify complete!!!
------------------------------------------------------------------

It can do it, as well as when I use the Python version of

>python C:\Users\Max\chflasher\chflasher.py .pio\build\ch559\firmware.bin
Could not access USB Device
Traceback (most recent call last):
  File "C:\Users\Max\chflasher\chflasher.py", line 36, in <module>
    dev.set_configuration()
  File "C:\Users\Max\AppData\Local\Programs\Python\Python38\lib\site-packages\usb\core.py", line 905, in set_configuration
    self._ctx.managed_set_configuration(self, configuration)
  File "C:\Users\Max\AppData\Local\Programs\Python\Python38\lib\site-packages\usb\core.py", line 113, in wrapper
    return f(self, *args, **kwargs)
  File "C:\Users\Max\AppData\Local\Programs\Python\Python38\lib\site-packages\usb\core.py", line 159, in managed_set_configuration
    self.backend.set_configuration(self.handle, cfg.bConfigurationValue)
  File "C:\Users\Max\AppData\Local\Programs\Python\Python38\lib\site-packages\usb\backend\libusb1.py", line 812, in set_configuration
    _check(self.lib.libusb_set_configuration(dev_handle.handle, config_value))
  File "C:\Users\Max\AppData\Local\Programs\Python\Python38\lib\site-packages\usb\backend\libusb1.py", line 604, in _check
    raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno 5] Input/Output Error

but when I use the precompile .exe version

>C:\Users\Max\chflasher\chflasher.exe .pio\build\ch559\firmware.bin
Found CH559
Bootloader version: 2.31
Flash Erased
Filesize: 194 bytes
Error: Something wrong with the bin file

(a related bug for that is opened at atc1441/chflasher#4)

In any case, with the tool in this repository I can't get my device recognized.

It is interesting that the chflasher tool is also written in Python and the script also doesn't see my device..

Any ideas?

Running on Windows 10, Python 3.8.6, pyUSB 1.1.1, Zadig libusb-win32 drivers were installed for the device.

@MarsTechHAN
Copy link
Owner

Thanks for the issue. I don't currently have CH559 by my hand. I will try to get a device and take a try.

@MarsTechHAN
Copy link
Owner

As you mentioned, as for thie tool.

Failed to get device, please check your libusb installation.

Can you add some print here to know where it is exited? ( I will fix this in next version)

def __get_dfu_device(idVendor=DFU_ID_VENDOR, idProduct=DFU_ID_PRODUCT):

Also, is that possibile for you to check the USB ID, to make sure the device's vendor id and product id is correct. I dont see a real differecnt between this and the vnproch55x you mentioned. ummmm...

@maxgerhardt
Copy link
Author

maxgerhardt commented Apr 6, 2021

If I add a

        print('Ret[1] = %s' % ret[1]) 

in the error printf I get output

C:\Users\Max\temp\ch552tool>python -m ch55xtool
Failed to get device, please check your libusb installation.
Ret[1] = USB_ERROR_CANNOT_SET_CONFIG

If I additionally do a print(dev) in __get_dfu_device I get

C:\Users\Max\temp\ch552tool>python -m ch55xtool
DEVICE ID 4348:55e0 on Bus 001 Address 007 =================
 bLength                :   0x12 (18 bytes)
 bDescriptorType        :    0x1 Device
 bcdUSB                 :  0x110 USB 1.1
 bDeviceClass           :   0xff Vendor-specific
 bDeviceSubClass        :   0x80
 bDeviceProtocol        :   0x55
 bMaxPacketSize0        :    0x8 (8 bytes)
 idVendor               : 0x4348
 idProduct              : 0x55e0
 bcdDevice              :  0x100 Device 1.0
 iManufacturer          :    0x0
 iProduct               :    0x0
 iSerialNumber          :    0x0
 bNumConfigurations     :    0x1
  CONFIGURATION 1: 100 mA ==================================
   bLength              :    0x9 (9 bytes)
   bDescriptorType      :    0x2 Configuration
   wTotalLength         :   0x20 (32 bytes)
   bNumInterfaces       :    0x1
   bConfigurationValue  :    0x1
   iConfiguration       :    0x0
   bmAttributes         :   0x80 Bus Powered
   bMaxPower            :   0x32 (100 mA)
    INTERFACE 0: Vendor Specific ===========================
     bLength            :    0x9 (9 bytes)
     bDescriptorType    :    0x4 Interface
     bInterfaceNumber   :    0x0
     bAlternateSetting  :    0x0
     bNumEndpoints      :    0x2
     bInterfaceClass    :   0xff Vendor Specific
     bInterfaceSubClass :   0x80
     bInterfaceProtocol :   0x55
     iInterface         :    0x0
      ENDPOINT 0x82: Bulk IN ===============================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :   0x82 IN
       bmAttributes     :    0x2 Bulk
       wMaxPacketSize   :   0x40 (64 bytes)
       bInterval        :    0x0
      ENDPOINT 0x2: Bulk OUT ===============================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :    0x2 OUT
       bmAttributes     :    0x2 Bulk
       wMaxPacketSize   :   0x40 (64 bytes)
       bInterval        :    0x0
Failed to get device, please check your libusb installation.
Ret[1] = USB_ERROR_CANNOT_SET_CONFIG

And I print the the exception traceback in dev.set_configuration() I get

Traceback (most recent call last):
  File "C:\Users\Max\temp\ch552tool\ch55xtool\ch55xtool.py", line 99, in __get_dfu_device
    dev.set_configuration()
  File "C:\Users\Max\AppData\Local\Programs\Python\Python38\lib\site-packages\usb\core.py", line 905, in set_configuration
    self._ctx.managed_set_configuration(self, configuration)
  File "C:\Users\Max\AppData\Local\Programs\Python\Python38\lib\site-packages\usb\core.py", line 113, in wrapper
    return f(self, *args, **kwargs)
  File "C:\Users\Max\AppData\Local\Programs\Python\Python38\lib\site-packages\usb\core.py", line 159, in managed_set_configuration
    self.backend.set_configuration(self.handle, cfg.bConfigurationValue)
  File "C:\Users\Max\AppData\Local\Programs\Python\Python38\lib\site-packages\usb\backend\libusb1.py", line 812, in set_configuration
    _check(self.lib.libusb_set_configuration(dev_handle.handle, config_value))
  File "C:\Users\Max\AppData\Local\Programs\Python\Python38\lib\site-packages\usb\backend\libusb1.py", line 604, in _check
    raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno 5] Input/Output Error

Device has libUSB drivers loaded

grafik

(it says libusb0 but that's the result I get after I replace it with libusb-win32 on the right).

Device manager shows the device.

grafik

@maxgerhardt
Copy link
Author

VID and PID of device in device manager is

grafik

@MarsTechHAN
Copy link
Owner

Hi, thanks for your additional information!

Can you try just remove the

dev.set_configuration()

line, and see will it work?

If that still make it error, can you try run the program with PYUSB_DEBUG=debug enviroment virable.

PYUSB_DEBUG=debug python -m ch55xtool # Under linux, we can do this, not sure how windows add env...

I see a similar issue here:
pyusb/pyusb#76

I think that's a pyusb or libusb issue, but we can try to workaround at here. :p

@maxgerhardt
Copy link
Author

If I comment out the dev.set_configuration, interestingly there is no output at all. Adding more print statements shows that after __get_dfu_device(), the dev is correctly there, it then calls __detect_ch55x_v2, which does dev.write(EP_OUT_ADDR, DETECT_CHIP_CMD_V2), from which it never returns -- the next line is never executed, the program just exits inside there. As if it got lost inside a libusb call.

I added

import os
os.environ['PYUSB_DEBUG'] = 'debug'

at the top of the script and got debug output and program return value

C:\Users\Max\temp\ch552tool>python -m ch55xtool
2021-04-08 10:38:55,338 DEBUG:usb.backend.libusb1:_LibUSB.__init__(<WinDLL 'C:\Users\Max\AppData\Local\Programs\Python\Python38\libusb-1.0.dll', handle 7fff52190000 at 0x1ca07149880>)
2021-04-08 10:38:55,341 INFO:usb.core:find(): using backend "usb.backend.libusb1"
2021-04-08 10:38:55,341 DEBUG:usb.backend.libusb1:_LibUSB.enumerate_devices()
2021-04-08 10:38:55,358 DEBUG:usb.backend.libusb1:_LibUSB.get_device_descriptor(<usb.backend.libusb1._Device object at 0x000001CA0898B490>)
2021-04-08 10:38:55,358 DEBUG:usb.backend.libusb1:_LibUSB.get_device_descriptor(<usb.backend.libusb1._Device object at 0x000001CA0898B610>)
2021-04-08 10:38:55,358 DEBUG:usb.backend.libusb1:_LibUSB.get_device_descriptor(<usb.backend.libusb1._Device object at 0x000001CA0898B4F0>)
2021-04-08 10:38:55,358 DEBUG:usb.backend.libusb1:_LibUSB.get_device_descriptor(<usb.backend.libusb1._Device object at 0x000001CA0898B670>)
2021-04-08 10:38:55,358 DEBUG:usb.backend.libusb1:_LibUSB.get_device_descriptor(<usb.backend.libusb1._Device object at 0x000001CA0898B580>)
2021-04-08 10:38:55,359 DEBUG:usb.backend.libusb1:_LibUSB.get_device_descriptor(<usb.backend.libusb1._Device object at 0x000001CA0898B6D0>)
2021-04-08 10:38:55,359 DEBUG:usb.backend.libusb1:_LibUSB.get_device_descriptor(<usb.backend.libusb1._Device object at 0x000001CA0898B4C0>)
2021-04-08 10:38:55,359 DEBUG:usb.backend.libusb1:_LibUSB.get_device_descriptor(<usb.backend.libusb1._Device object at 0x000001CA0898B640>)
2021-04-08 10:38:55,359 DEBUG:usb.backend.libusb1:_LibUSB.get_device_descriptor(<usb.backend.libusb1._Device object at 0x000001CA0898B550>)
2021-04-08 10:38:55,359 DEBUG:usb.backend.libusb1:_LibUSB.get_device_descriptor(<usb.backend.libusb1._Device object at 0x000001CA0898B6A0>)
2021-04-08 10:38:55,359 DEBUG:usb.backend.libusb1:_LibUSB.get_device_descriptor(<usb.backend.libusb1._Device object at 0x000001CA0898B490>)
2021-04-08 10:38:55,359 DEBUG:usb.backend.libusb1:_LibUSB.get_device_descriptor(<usb.backend.libusb1._Device object at 0x000001CA0898B610>)
2021-04-08 10:38:55,359 DEBUG:usb.backend.libusb1:_LibUSB.open_device(<usb.backend.libusb1._Device object at 0x000001CA0898B610>)
2021-04-08 10:38:55,360 DEBUG:usb.backend.libusb1:_LibUSB.is_kernel_driver_active(<usb.backend.libusb1._DeviceHandle object at 0x000001CA071295B0>, 0)
2021-04-08 10:38:55,360 DEBUG:usb.backend.libusb1:_LibUSB.claim_interface(<usb.backend.libusb1._DeviceHandle object at 0x000001CA071295B0>, 0)
After __get_dfu_device, ret is (<DEVICE ID 4348:55e0 on Bus 001 Address 007>, '')
Before __detect_ch55x_v2(), dev = <DEVICE ID 4348:55e0 on Bus 001 Address 007>
Before dev.write(EP_OUT_ADDR, DETECT_CHIP_CMD_V2)
2021-04-08 10:38:55,360 DEBUG:usb.backend.libusb1:_LibUSB.get_configuration(<usb.backend.libusb1._DeviceHandle object at 0x000001CA071295B0>)
2021-04-08 10:38:55,360 DEBUG:usb.backend.libusb1:_LibUSB.get_configuration_descriptor(<usb.backend.libusb1._Device object at 0x000001CA0898B610>, 0)
2021-04-08 10:38:55,360 DEBUG:usb.backend.libusb1:_LibUSB.get_interface_descriptor(<usb.backend.libusb1._Device object at 0x000001CA0898B610>, 0, 0, 0)
2021-04-08 10:38:55,360 DEBUG:usb.backend.libusb1:_LibUSB.get_configuration_descriptor(<usb.backend.libusb1._Device object at 0x000001CA0898B610>, 0)
2021-04-08 10:38:55,360 DEBUG:usb.backend.libusb1:_LibUSB.get_endpoint_descriptor(<usb.backend.libusb1._Device object at 0x000001CA0898B610>, 0, 0, 0, 0)
2021-04-08 10:38:55,361 DEBUG:usb.backend.libusb1:_LibUSB.get_interface_descriptor(<usb.backend.libusb1._Device object at 0x000001CA0898B610>, 0, 0, 0)
2021-04-08 10:38:55,361 DEBUG:usb.backend.libusb1:_LibUSB.get_configuration_descriptor(<usb.backend.libusb1._Device object at 0x000001CA0898B610>, 0)
2021-04-08 10:38:55,361 DEBUG:usb.backend.libusb1:_LibUSB.get_endpoint_descriptor(<usb.backend.libusb1._Device object at 0x000001CA0898B610>, 1, 0, 0, 0)
2021-04-08 10:38:55,361 DEBUG:usb.backend.libusb1:_LibUSB.get_interface_descriptor(<usb.backend.libusb1._Device object at 0x000001CA0898B610>, 0, 0, 0)
2021-04-08 10:38:55,361 DEBUG:usb.backend.libusb1:_LibUSB.get_configuration_descriptor(<usb.backend.libusb1._Device object at 0x000001CA0898B610>, 0)
2021-04-08 10:38:55,361 DEBUG:usb.backend.libusb1:_LibUSB.bulk_write(<usb.backend.libusb1._DeviceHandle object at 0x000001CA071295B0>, 2, 0, array('B', [161, 18, 0, 82, 17, 77, 67, 85, 32, 73, 83, 80, 32, 38, 32, 87, 67, 72, 46, 67, 78]), 1000)

C:\Users\Max\temp\ch552tool>echo %ERRORLEVEL%
-1073741819

The libusb-1.0.dll file is the latest one from https://github.com/libusb/libusb/releases as recommended in https://github.com/pyusb/pyusb#installing.

I will forward the device to a Linux virtual machine to see if the error is Windows-specific..

@maxgerhardt
Copy link
Author

Very interesting: If I just rename the

C:\Users\Max\AppData\Local\Programs\Python\Python38\libusb-1.0.dll

file to something else, it falls back to the libusb0 backend, but throws an exception

usb.core.USBError: [Errno None] Configuration not set

later. If I re-comment in the setting of the usb device configuration, it works!

C:\Users\Max\temp\ch552tool>python -m ch55xtool -f ..\vnproch55x\blink.bin
2021-04-08 10:46:44,460 ERROR:usb.libloader:'Libusb 1' could not be found
2021-04-08 10:46:44,461 ERROR:usb.backend.libusb1:Error loading libusb 1.0 backend
2021-04-08 10:46:44,462 ERROR:usb.libloader:'OpenUSB library' could not be found
2021-04-08 10:46:44,462 ERROR:usb.backend.openusb:Error loading OpenUSB backend
2021-04-08 10:46:44,466 INFO:usb.core:find(): using backend "usb.backend.libusb0"
2021-04-08 10:46:44,466 DEBUG:usb.backend.libusb0:_LibUSB.enumerate_devices()
2021-04-08 10:46:44,467 DEBUG:usb.backend.libusb0:_LibUSB.get_device_descriptor(<usb.backend.libusb0._usb_device object at 0x00000175F9D0CD40>)
2021-04-08 10:46:44,467 DEBUG:usb.backend.libusb0:_LibUSB.open_device(<usb.backend.libusb0._usb_device object at 0x00000175F9D0CD40>)
2021-04-08 10:46:44,467 DEBUG:usb.backend.libusb0:_LibUSB.is_kernel_driver_active(1606179102016, 0)
2021-04-08 10:46:44,467 DEBUG:usb.backend.libusb0:_LibUSB.get_configuration_descriptor(<usb.backend.libusb0._usb_device object at 0x00000175F9D0CD40>, 0)
2021-04-08 10:46:44,467 DEBUG:usb.backend.libusb0:_LibUSB.set_configuration(1606179102016, 1)
2021-04-08 10:46:44,467 DEBUG:usb.backend.libusb0:_LibUSB.claim_interface(1606179102016, 0)
After __get_dfu_device, ret is (<DEVICE ID 4348:55e0 on Bus 000 Address 001>, '')
Before __detect_ch55x_v2(), dev = <DEVICE ID 4348:55e0 on Bus 000 Address 001>
Before dev.write(EP_OUT_ADDR, DETECT_CHIP_CMD_V2)
2021-04-08 10:46:44,468 DEBUG:usb.backend.libusb0:_LibUSB.get_configuration_descriptor(<usb.backend.libusb0._usb_device object at 0x00000175F9D0CD40>, 0)
2021-04-08 10:46:44,468 DEBUG:usb.backend.libusb0:_LibUSB.get_interface_descriptor(<usb.backend.libusb0._usb_device object at 0x00000175F9D0CD40>, 0, 0, 0)
2021-04-08 10:46:44,468 DEBUG:usb.backend.libusb0:_LibUSB.get_configuration_descriptor(<usb.backend.libusb0._usb_device object at 0x00000175F9D0CD40>, 0)
2021-04-08 10:46:44,468 DEBUG:usb.backend.libusb0:_LibUSB.get_endpoint_descriptor(<usb.backend.libusb0._usb_device object at 0x00000175F9D0CD40>, 0, 0, 0, 0)
2021-04-08 10:46:44,468 DEBUG:usb.backend.libusb0:_LibUSB.get_interface_descriptor(<usb.backend.libusb0._usb_device object at 0x00000175F9D0CD40>, 0, 0, 0)
2021-04-08 10:46:44,468 DEBUG:usb.backend.libusb0:_LibUSB.get_configuration_descriptor(<usb.backend.libusb0._usb_device object at 0x00000175F9D0CD40>, 0)
2021-04-08 10:46:44,469 DEBUG:usb.backend.libusb0:_LibUSB.get_endpoint_descriptor(<usb.backend.libusb0._usb_device object at 0x00000175F9D0CD40>, 1, 0, 0, 0)
2021-04-08 10:46:44,469 DEBUG:usb.backend.libusb0:_LibUSB.get_interface_descriptor(<usb.backend.libusb0._usb_device object at 0x00000175F9D0CD40>, 0, 0, 0)
2021-04-08 10:46:44,469 DEBUG:usb.backend.libusb0:_LibUSB.get_configuration_descriptor(<usb.backend.libusb0._usb_device object at 0x00000175F9D0CD40>, 0)
2021-04-08 10:46:44,469 DEBUG:usb.backend.libusb0:_LibUSB.bulk_write(1606179102016, 2, 0, array('B', [161, 18, 0, 82, 17, 77, 67, 85, 32, 73, 83, 80, 32, 38, 32, 87, 67, 72, 46, 67, 78]), 1000)
After dev.write(EP_OUT_ADDR, DETECT_CHIP_CMD_V2)
2021-04-08 10:46:44,469 DEBUG:usb.backend.libusb0:_LibUSB.get_configuration_descriptor(<usb.backend.libusb0._usb_device object at 0x00000175F9D0CD40>, 0)
2021-04-08 10:46:44,470 DEBUG:usb.backend.libusb0:_LibUSB.get_interface_descriptor(<usb.backend.libusb0._usb_device object at 0x00000175F9D0CD40>, 0, 0, 0)
2021-04-08 10:46:44,470 DEBUG:usb.backend.libusb0:_LibUSB.get_configuration_descriptor(<usb.backend.libusb0._usb_device object at 0x00000175F9D0CD40>, 0)
2021-04-08 10:46:44,470 DEBUG:usb.backend.libusb0:_LibUSB.get_endpoint_descriptor(<usb.backend.libusb0._usb_device object at 0x00000175F9D0CD40>, 0, 0, 0, 0)
2021-04-08 10:46:44,470 DEBUG:usb.backend.libusb0:_LibUSB.get_interface_descriptor(<usb.backend.libusb0._usb_device object at 0x00000175F9D0CD40>, 0, 0, 0)
2021-04-08 10:46:44,470 DEBUG:usb.backend.libusb0:_LibUSB.get_configuration_descriptor(<usb.backend.libusb0._usb_device object at 0x00000175F9D0CD40>, 0)
2021-04-08 10:46:44,470 DEBUG:usb.backend.libusb0:_LibUSB.bulk_read(1606179102016, 130, 0, array('B', [0, 0, 0, 0, 0, 0]), 2000)
After __detect_ch55x_v2(), ret = {'device_name': 'CH559', 'device_flash_size': 61440, 'device_dataflash_size': 128, 'chip_id': 89}
Found CH559.
2021-04-08 10:46:44,471 DEBUG:usb.backend.libusb0:_LibUSB.bulk_write(1606179102016, 2, 0, array('B', [167, 2, 0, 31, 0]), 1000)
2021-04-08 10:46:44,471 DEBUG:usb.backend.libusb0:_LibUSB.bulk_read(1606179102016, 130, 0, array('B', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), 2000)
BTVER: V2.31.
2021-04-08 10:46:44,478 DEBUG:usb.backend.libusb0:_LibUSB.bulk_write(1606179102016, 2, 0, array('B', [163, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), 1000)
2021-04-08 10:46:44,479 DEBUG:usb.backend.libusb0:_LibUSB.bulk_read(1606179102016, 130, 0, array('B', [0, 0, 0, 0, 0, 0]), 2000)
2021-04-08 10:46:44,479 DEBUG:usb.backend.libusb0:_LibUSB.bulk_write(1606179102016, 2, 0, array('B', [164, 1, 0, 8]), 1000)
2021-04-08 10:46:44,479 DEBUG:usb.backend.libusb0:_LibUSB.bulk_read(1606179102016, 130, 0, array('B', [0, 0, 0, 0, 0, 0]), 2000)
2021-04-08 10:46:44,494 DEBUG:usb.backend.libusb0:_LibUSB.bulk_write(1606179102016, 2, 0, array('B', [165, 61, 0, 0, 0, 0, 0, 24, 81, 83, 85, 81, 83, 216, 38, 45, 84, 65, 83, 178, 182, 209, 51, 175, 81, 83, 80, 42, 83, 186, 23, 172, 51, 72, 41, 83, 195, 83, 182, 212, 82, 38, 243, 83, 183, 192, 161, 15, 91, 235, 83, 81, 86, 243, 138, 88, 137, 161, 38, 243, 172, 183, 43, 83]), 1000)
2021-04-08 10:46:44,494 DEBUG:usb.backend.libusb0:_LibUSB.bulk_read(1606179102016, 130, 0, array('B', [0, 0, 0, 0, 0, 0]), 2000)
2021-04-08 10:46:44,495 DEBUG:usb.backend.libusb0:_LibUSB.bulk_write(1606179102016, 2, 0, array('B', [165, 61, 0, 56, 0, 0, 0, 224, 165, 139, 174, 43, 83, 187, 23, 172, 51, 89, 42, 82, 38, 243, 83, 72, 160, 90, 139, 175, 43, 83, 187, 232, 83, 51, 95, 42, 83, 195, 83, 173, 183, 163, 240, 139, 175, 138, 169, 174, 83, 80, 38, 218, 82, 113, 253, 46, 252, 208, 221, 87, 220, 86, 77, 18]), 1000)
2021-04-08 10:46:44,496 DEBUG:usb.backend.libusb0:_LibUSB.bulk_read(1606179102016, 130, 0, array('B', [0, 0, 0, 0, 0, 0]), 2000)
2021-04-08 10:46:44,497 DEBUG:usb.backend.libusb0:_LibUSB.bulk_write(1606179102016, 2, 0, array('B', [165, 61, 0, 112, 0, 0, 0, 168, 172, 82, 76, 191, 30, 51, 64, 217, 223, 171, 38, 217, 99, 145, 222, 126, 223, 99, 222, 174, 145, 223, 145, 33, 211, 179, 113, 38, 242, 6, 38, 13, 249, 38, 234, 213, 38, 242, 83, 217, 242, 83, 38, 242, 83, 38, 242, 172, 38, 242, 83, 38, 242, 83, 38, 13]), 1000)
2021-04-08 10:46:44,498 DEBUG:usb.backend.libusb0:_LibUSB.bulk_read(1606179102016, 130, 0, array('B', [0, 0, 0, 0, 0, 0]), 2000)
2021-04-08 10:46:44,499 DEBUG:usb.backend.libusb0:_LibUSB.bulk_write(1606179102016, 2, 0, array('B', [165, 61, 0, 168, 0, 0, 0, 112, 83, 38, 242, 83, 38, 242, 83, 217, 242, 83, 38, 242, 83, 38, 242, 172, 38, 242, 83, 38, 242, 83, 38, 13, 83, 38, 242, 83, 0, 197, 173, 239, 196, 82, 65, 83, 49, 16, 227, 173, 195, 83, 55, 65, 83, 53, 0, 28, 173, 195, 83, 55, 65, 83, 53, 44]), 1000)
2021-04-08 10:46:44,499 DEBUG:usb.backend.libusb0:_LibUSB.bulk_read(1606179102016, 130, 0, array('B', [0, 0, 0, 0, 0, 0]), 2000)
2021-04-08 10:46:44,500 DEBUG:usb.backend.libusb0:_LibUSB.bulk_write(1606179102016, 2, 0, array('B', [165, 61, 0, 224, 0, 0, 0, 56, 191, 38, 209, 83, 113, 172, 172, 83, 172, 172, 172, 172, 172, 172, 172, 83, 172, 172, 172, 172, 172, 172, 172, 83, 172, 172, 172, 172, 172, 172, 172, 83, 172, 172, 172, 172, 172, 172, 172, 83, 172, 172, 172, 172, 172, 172, 172, 83, 172, 172, 172, 172, 172, 172, 172, 83]), 1000)
2021-04-08 10:46:44,500 DEBUG:usb.backend.libusb0:_LibUSB.bulk_read(1606179102016, 130, 0, array('B', [0, 0, 0, 0, 0, 0]), 2000)
2021-04-08 10:46:44,502 DEBUG:usb.backend.libusb0:_LibUSB.bulk_write(1606179102016, 2, 0, array('B', [166, 61, 0, 0, 0, 0, 0, 24, 81, 83, 85, 81, 83, 216, 38, 45, 84, 65, 83, 178, 182, 209, 51, 175, 81, 83, 80, 42, 83, 186, 23, 172, 51, 72, 41, 83, 195, 83, 182, 212, 82, 38, 243, 83, 183, 192, 161, 15, 91, 235, 83, 81, 86, 243, 138, 88, 137, 161, 38, 243, 172, 183, 43, 83]), 1000)
2021-04-08 10:46:44,502 DEBUG:usb.backend.libusb0:_LibUSB.bulk_read(1606179102016, 130, 0, array('B', [0, 0, 0, 0, 0, 0]), 2000)
2021-04-08 10:46:44,503 DEBUG:usb.backend.libusb0:_LibUSB.bulk_write(1606179102016, 2, 0, array('B', [166, 61, 0, 56, 0, 0, 0, 224, 165, 139, 174, 43, 83, 187, 23, 172, 51, 89, 42, 82, 38, 243, 83, 72, 160, 90, 139, 175, 43, 83, 187, 232, 83, 51, 95, 42, 83, 195, 83, 173, 183, 163, 240, 139, 175, 138, 169, 174, 83, 80, 38, 218, 82, 113, 253, 46, 252, 208, 221, 87, 220, 86, 77, 18]), 1000)
2021-04-08 10:46:44,503 DEBUG:usb.backend.libusb0:_LibUSB.bulk_read(1606179102016, 130, 0, array('B', [0, 0, 0, 0, 0, 0]), 2000)
2021-04-08 10:46:44,504 DEBUG:usb.backend.libusb0:_LibUSB.bulk_write(1606179102016, 2, 0, array('B', [166, 61, 0, 112, 0, 0, 0, 168, 172, 82, 76, 191, 30, 51, 64, 217, 223, 171, 38, 217, 99, 145, 222, 126, 223, 99, 222, 174, 145, 223, 145, 33, 211, 179, 113, 38, 242, 6, 38, 13, 249, 38, 234, 213, 38, 242, 83, 217, 242, 83, 38, 242, 83, 38, 242, 172, 38, 242, 83, 38, 242, 83, 38, 13]), 1000)
2021-04-08 10:46:44,504 DEBUG:usb.backend.libusb0:_LibUSB.bulk_read(1606179102016, 130, 0, array('B', [0, 0, 0, 0, 0, 0]), 2000)
2021-04-08 10:46:44,505 DEBUG:usb.backend.libusb0:_LibUSB.bulk_write(1606179102016, 2, 0, array('B', [166, 61, 0, 168, 0, 0, 0, 112, 83, 38, 242, 83, 38, 242, 83, 217, 242, 83, 38, 242, 83, 38, 242, 172, 38, 242, 83, 38, 242, 83, 38, 13, 83, 38, 242, 83, 0, 197, 173, 239, 196, 82, 65, 83, 49, 16, 227, 173, 195, 83, 55, 65, 83, 53, 0, 28, 173, 195, 83, 55, 65, 83, 53, 44]), 1000)
2021-04-08 10:46:44,505 DEBUG:usb.backend.libusb0:_LibUSB.bulk_read(1606179102016, 130, 0, array('B', [0, 0, 0, 0, 0, 0]), 2000)
2021-04-08 10:46:44,506 DEBUG:usb.backend.libusb0:_LibUSB.bulk_write(1606179102016, 2, 0, array('B', [166, 61, 0, 224, 0, 0, 0, 56, 191, 38, 209, 83, 113, 172, 172, 83, 172, 172, 172, 172, 172, 172, 172, 83, 172, 172, 172, 172, 172, 172, 172, 83, 172, 172, 172, 172, 172, 172, 172, 83, 172, 172, 172, 172, 172, 172, 172, 83, 172, 172, 172, 172, 172, 172, 172, 83, 172, 172, 172, 172, 172, 172, 172, 83]), 1000)
2021-04-08 10:46:44,506 DEBUG:usb.backend.libusb0:_LibUSB.bulk_read(1606179102016, 130, 0, array('B', [0, 0, 0, 0, 0, 0]), 2000)
2021-04-08 10:46:44,507 DEBUG:usb.backend.libusb0:_LibUSB.bulk_write(1606179102016, 2, 0, array('B', [162, 1, 0, 0]), 1000)
2021-04-08 10:46:44,507 DEBUG:usb.backend.libusb0:_LibUSB.bulk_read(1606179102016, 130, 0, array('B', [0, 0, 0, 0, 0, 0]), 2000)
Flash done.
2021-04-08 10:46:44,508 DEBUG:usb.backend.libusb0:_LibUSB.release_interface(1606179102016, 0)
2021-04-08 10:46:44,508 DEBUG:usb.backend.libusb0:_LibUSB.close_device(1606179102016)

or with less debug output

>python -m ch55xtool -f ..\vnproch55x\blink.bin
After __get_dfu_device, ret is (<DEVICE ID 4348:55e0 on Bus 000 Address 001>, '')
Before __detect_ch55x_v2(), dev = <DEVICE ID 4348:55e0 on Bus 000 Address 001>
Before dev.write(EP_OUT_ADDR, DETECT_CHIP_CMD_V2)
After dev.write(EP_OUT_ADDR, DETECT_CHIP_CMD_V2)
After __detect_ch55x_v2(), ret = {'device_name': 'CH559', 'device_flash_size': 61440, 'device_dataflash_size': 128, 'chip_id': 89}
Found CH559.
BTVER: V2.31.
Flash done.

So this has something to do with the used backend or libusb1.0 dll version. I'll try out linux + different libusb1 versions.

@Emiluren
Copy link

Emiluren commented Dec 28, 2022

I think I have a similar problem on linux. It seems like the bootloader doesn't respond properly to multiple set_configuration requests and the default usb driver sets a configuration automatically. I found a workaround here. I put this code at the start of __get_dfu_device.

	import fcntl, array
	USBDEVFS_CLAIM_PORT = -2147199720 # printf("0x%d\n", USBDEVFS_CLAIM_PORT);
	port = array.array('i', [1]) # For port 1 which i identified using udevadm info -t
	fcntl.ioctl(os.open("/dev/bus/usb/002/001", os.O_RDWR), USBDEVFS_CLAIM_PORT, port)
	input('Connect the device and press ENTER to continue')

I haven't tried a lot so far but it seems promising.

$ sudo python ch55xtool.py 
Connect the device and press ENTER to continue
Found CH559 with SubId:17
BTVER:02.40
UID:0E-57-48-BC-00-00-00-00
Finalize communication. Done.

I did a test in C first which did not require root. I don't know why I need to run python with sudo.

This requires me to use the same usb port every time and start the tool before plugging the device in which is a bit of a hassle. I'll see if I can prevent the automatic configuration using device/authorized in a udev rule.

EDIT: I've tried setting OWNER and ATTRS{authorized}:=0 in udev but it doesn't help

@Emiluren
Copy link

Oh, I just realized it has nothing to do with what I just wrote. If I ran the tool immediately (without any modifications) it worked. I also noticed that running it multiple times in a row sometimes helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants