avrdudes / avrdude

AVRDUDE is a utility to program AVR microcontrollers
GNU General Public License v2.0
696 stars 136 forks source link

PICKit 4, SNAP, Atmel ICE and Power Debugger only work with hidapi but not libusb (low priority) #1221

Open mcuee opened 1 year ago

mcuee commented 1 year ago

From here:

For this test, I build avrdude without hidapi support. And I make sure the hid drver is already detached and then libusb is used.

hidapi version works fine (with hidapi-libusb backend). So there is a bug here in git main.

mcuee@UbuntuSwift3:~/build/avr/avrdude_main$ cat build_nohidapi.sh 
#!/bin/sh
cmake -D CMAKE_BUILD_TYPE=RelWithDebInfo -D HAVE_LIBHIDAPI=OFF  -B build_linux
cmake --build build_linux

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ ldd ./avrdude_git_nohidapi 
    linux-vdso.so.1 (0x00007ffc52b2f000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fa6169f6000)
    libelf.so.1 => /lib/x86_64-linux-gnu/libelf.so.1 (0x00007fa6169da000)
    libusb-0.1.so.4 => /usr/local/lib/libusb-0.1.so.4 (0x00007fa6169d2000)
    libusb-1.0.so.0 => /usr/local/lib/libusb-1.0.so.0 (0x00007fa6169b2000)
    libftdi.so.1 => /lib/x86_64-linux-gnu/libftdi.so.1 (0x00007fa6169a8000)
    libftdi1.so.2 => /usr/local/lib/libftdi1.so.2 (0x00007fa616995000)
    libreadline.so.8 => /lib/x86_64-linux-gnu/libreadline.so.8 (0x00007fa616945000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fa616753000)
    /lib64/ld-linux-x86-64.so.2 (0x00007fa616c5f000)
    libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fa616737000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fa616714000)
    libudev.so.1 => /lib/x86_64-linux-gnu/libudev.so.1 (0x00007fa6166e7000)
    libtinfo.so.6 => /lib/x86_64-linux-gnu/libtinfo.so.6 (0x00007fa6166b5000)

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ ./avrdude_git_nohidapi -c pickit4_updi -p m4808 -v

avrdude_git_nohidapi: Version 7.0-20221214 (159d126)
                      Copyright the AVRDUDE authors;
                      see https://github.com/avrdudes/avrdude/blob/main/AUTHORS

                      System wide configuration file is /home/mcuee/build/avr/avrdude_bin/avrdude.conf
                      User configuration file is /home/mcuee/.avrduderc
                      User configuration file does not exist or is not a regular file, skipping

                      Using Port                    : usb
                      Using Programmer              : pickit4_updi
avrdude_git_nohidapi: usbdev_open(): found MPLAB PICkit 4 CMSIS-DAP, serno: BUR2009xxxxx
avrdude_git_nohidapi usbdev_open() warning: unable to set configuration 1: Device or resource busy
avrdude_git_nohidapi: found CMSIS-DAP compliant device, using EDBG protocol
avrdude_git_nohidapi usbdev_send() error: wrote -5 out of 912 bytes, err = Input/output error
avrdude_git_nohidapi jtag3_edbg_prepare() error: unable to send command to serial port
avrdude_git_nohidapi main() error: unable to open programmer pickit4_updi on port usb

avrdude_git_nohidapi done.  Thank you.
mcuee commented 1 year ago

Same result under Windows.

I built avrdude without hidapi and readline support in this test.

I also tried with the default Windows HID driver (libusb-1.0 under Windows supports HID driver), or with WinUSB driver. The results are the same.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_git_nohidapi -c pickit4_updi -p m4808 -v

avrdude_git_nohidapi: Version 7.0-20221214 (159d126)
                      Copyright the AVRDUDE authors;
                      see https://github.com/avrdudes/avrdude/blob/main/AUTHORS

                      System wide configuration file is C:\work\avr\avrdude_test\avrdude_bin\avrdude.conf

                      Using Port                    : usb
                      Using Programmer              : pickit4_updi
avrdude_git_nohidapi: usbdev_open(): found MPLAB PICkit 4 CMSIS-DAP, serno: BUR2009xxxxx
avrdude_git_nohidapi usbdev_open() error: SET_IDLE failed
avrdude_git_nohidapi: found CMSIS-DAP compliant device, using EDBG protocol
avrdude_git_nohidapi usbdev_send() error: wrote -2 out of 912 bytes, err = No such file or directory
avrdude_git_nohidapi jtag3_edbg_prepare() error: unable to send command to serial port
avrdude_git_nohidapi main() error: unable to open programmer pickit4_updi on port usb

avrdude_git_nohidapi done.  Thank you.
MCUdude commented 1 year ago

I'll assume the SNAP is also affected.

But how about other, "older" JTAG3 compatible programmers such as the Xplained Mini ATtiny817?

mcuee commented 1 year ago

I'll assume the SNAP is also affected.

But how about other, "older" JTAG3 compatible programmers such as the Xplained Mini ATtiny817?

It seems to work with WinUSB driver. I am using the Xplained Pro Attiny817 for this test.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_git_nohidapi -c xplainedpro_updi -p t817

                      Vtarget                      : 3.34 V
                      JTAG clock megaAVR/program   : 1000 kHz
                      JTAG clock megaAVR/debug     : 1000 kHz
                      JTAG clock Xmega             : 1000 kHz
                      PDI/UPDI clock Xmega/megaAVR : 1000 kHz
avrdude_git_nohidapi: AVR device initialized and ready to accept instructions
avrdude_git_nohidapi: device signature = 0x1e9320 (probably t817)

avrdude_git_nohidapi done.  Thank you.
mcuee commented 1 year ago

I'll assume the SNAP is also affected. But how about other, "older" JTAG3 compatible programmers such as the Xplained Mini ATtiny817?

It seems to work with WinUSB driver. I am using the Xplained Pro Attiny817 for this test.

It also works under Linux even though there is a warning.

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ ./avrdude_git -c xplainedpro_updi -p t817 -v

avrdude_git: Version 7.0-20221214 (159d126)
             Copyright the AVRDUDE authors;
             see https://github.com/avrdudes/avrdude/blob/main/AUTHORS

             System wide configuration file is /home/mcuee/build/avr/avrdude_bin/avrdude.conf
             User configuration file is /home/mcuee/.avrduderc
             User configuration file does not exist or is not a regular file, skipping

             Using Port                    : usb
             Using Programmer              : xplainedpro_updi
avrdude_git: found CMSIS-DAP compliant device, using EDBG protocol
             AVR Part                      : ATtiny817
             RESET disposition             : dedicated
             RETRY pulse                   : SCK
             Serial program mode           : yes
             Parallel program mode         : yes
             ....
             Programmer Type : JTAGICE3_UPDI
             Description     : Atmel AVR XplainedPro in UPDI mode
             ICE HW version  : 240
             ICE FW version  : 3.34 (rel. 435)
             Serial number   : ATMLxxxxxxxxxxxxxxxxxxx
             Vtarget         : 3.34 V
             JTAG clock megaAVR/program   : 1000 kHz
             JTAG clock megaAVR/debug     : 1000 kHz
             JTAG clock Xmega             : 1000 kHz
             PDI/UPDI clock Xmega/megaAVR : 1000 kHz
avrdude_git: partial Family_ID returned: "tiny"
avrdude_git: AVR device initialized and ready to accept instructions
avrdude_git: device signature = 0x1e9320 (probably t817)

avrdude_git done.  Thank you.

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ ./avrdude_git_nohidapi -c xplainedpro_updi -p t817 -qqt
avrdude_git_nohidapi warning: unable to set configuration 1: Device or resource busy
avrdude> dump fuses
0000  00 00 02 ff 00 c4 04 00  02                       |.........       |

avrdude> quit
avrdude> mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ 
MCUdude commented 1 year ago

It also works under Linux even though there is a warning.

Cool! I think we're on our own though. IIRC, no JTAG3 programmer will work with Microchip Studio if not using the hidapi driver.

mcuee commented 1 year ago

Cool! I think we're on our own though. IIRC, no JTAG3 programmer will work with Microchip Studio if not using the hidapi driver.

Yes we are on our own as Microchip Studio and MPLAB X use native Windows HID API under Windows and not hidapi/libusb. But I think we should be able to figure out the issue as hidapi-libusb works under Linux -- therefore libusb should also work.

MPLAB X under macOS and Linux may use HIDAPI (I need to double check) for HID device. They for sure use libusb-1.0 under macOS and Linux with generic USB devices (not HID devices).

And this is not an important issue either as HIDAPI works, so Windows/macOS/Linux all work. FreeBSD should also work.

mcuee commented 1 year ago

BTW, this is not only applicable to UPDI, but also ISP.

Tested under Windows with either WinUSB driver.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_git_nohidapi -c pickit4_isp -p m8a -v

avrdude_git_nohidapi: Version 7.0-20221214 (159d126)
                      Copyright the AVRDUDE authors;
                      see https://github.com/avrdudes/avrdude/blob/main/AUTHORS

                      System wide configuration file is C:\work\avr\avrdude_test\avrdude_bin\avrdude.conf

                      Using Port                    : usb
                      Using Programmer              : pickit4_isp
avrdude_git_nohidapi: usbdev_open(): found MPLAB PICkit 4 CMSIS-DAP, serno: BUR2009xxxxx
avrdude_git_nohidapi usbdev_open() error: SET_IDLE failed
avrdude_git_nohidapi: found CMSIS-DAP compliant device, using EDBG protocol
avrdude_git_nohidapi usbdev_send() error: wrote -2 out of 912 bytes, err = No such file or directory
avrdude_git_nohidapi jtag3_edbg_prepare() error: unable to send command to serial port
avrdude_git_nohidapi stk500v2_jtag3_open() error: unable to sync with the JTAGICE3 in ISP mode
avrdude_git_nohidapi main() error: unable to open programmer pickit4_isp on port usb

avrdude_git_nohidapi done.  Thank you.
mcuee commented 1 year ago

I'll assume the SNAP is also affected.

Yes it is also affected.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_git_nohidapi -c snap_isp -p m8a -v

avrdude_git_nohidapi: Version 7.0-20221214 (159d126)
                      Copyright the AVRDUDE authors;
                      see https://github.com/avrdudes/avrdude/blob/main/AUTHORS

                      System wide configuration file is C:\work\avr\avrdude_test\avrdude_bin\avrdude.conf

                      Using Port                    : usb
                      Using Programmer              : snap_isp
avrdude_git_nohidapi: usbdev_open(): found MPLAB Snap ICD CMSIS-DAP, serno: BURxxxxxxxxx
avrdude_git_nohidapi: found CMSIS-DAP compliant device, using EDBG protocol
avrdude_git_nohidapi usbdev_send() error: wrote -2 out of 912 bytes, err = No such file or directory
avrdude_git_nohidapi jtag3_edbg_prepare() error: unable to send command to serial port
avrdude_git_nohidapi stk500v2_jtag3_open() error: unable to sync with the JTAGICE3 in ISP mode
avrdude_git_nohidapi main() error: unable to open programmer snap_isp on port usb

avrdude_git_nohidapi done.  Thank you.
mcuee commented 1 year ago

More detailed debug log under Linux.

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ sudo ./avrdude_git_nohidapi -c pickit4_isp -p m8a -vvvv
executable_abspath = /home/mcuee/build/avr/avrdude_bin/avrdude_git_nohidapi
executable_abspath_len = 54
executable_dirpath = /home/mcuee/build/avr/avrdude_bin
executable_dirpath_len = 33
sys_config = /home/mcuee/build/avr/avrdude_bin/avrdude.conf
sys_config_found = true

avrdude_git_nohidapi: Version 7.0-20221214 (159d126)
                      Copyright the AVRDUDE authors;
                      see https://github.com/avrdudes/avrdude/blob/main/AUTHORS

                      System wide configuration file is /home/mcuee/build/avr/avrdude_bin/avrdude.conf
                      User configuration file is /root/.avrduderc
                      User configuration file does not exist or is not a regular file, skipping

                      Using Port                    : usb
                      Using Programmer              : pickit4_isp
avrdude_git_nohidapi: stk500v2_jtag3_open()
avrdude_git_nohidapi: usbdev_open(): found MPLAB PICkit 4 CMSIS-DAP, serno: BUR2009xxxxx
avrdude_git_nohidapi usbdev_open() [usb_libusb.c:197] warning: unable to set configuration 1: Device or resource busy
avrdude_git_nohidapi: found CMSIS-DAP compliant device, using EDBG protocol
avrdude_git_nohidapi: jtag3_getsync()

avrdude_git_nohidapi: jtag3_edbg_prepare()
avrdude_git_nohidapi usbdev_send() [usb_libusb.c:344] error: wrote -5 out of 912 bytes, err = Input/output error
avrdude_git_nohidapi jtag3_edbg_prepare() [jtag3.c:542] error: unable to send command to serial port
avrdude_git_nohidapi stk500v2_jtag3_open() [stk500v2.c:3510] error: unable to sync with the JTAGICE3 in ISP mode
avrdude_git_nohidapi main() [main.c:1123] error: unable to open programmer pickit4_isp on port usb

avrdude_git_nohidapi done.  Thank you.

Debug log for working version with hidapi-libusb.

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ ./avrdude_git -c pickit4_isp -p m8a -vvvv
executable_abspath = /home/mcuee/build/avr/avrdude_bin/avrdude_git
executable_abspath_len = 45
executable_dirpath = /home/mcuee/build/avr/avrdude_bin
executable_dirpath_len = 33
sys_config = /home/mcuee/build/avr/avrdude_bin/avrdude.conf
sys_config_found = true

avrdude_git: Version 7.0-20221214 (159d126)
             Copyright the AVRDUDE authors;
             see https://github.com/avrdudes/avrdude/blob/main/AUTHORS

             System wide configuration file is /home/mcuee/build/avr/avrdude_bin/avrdude.conf
             User configuration file is /home/mcuee/.avrduderc
             User configuration file does not exist or is not a regular file, skipping

             Using Port                    : usb
             Using Programmer              : pickit4_isp
avrdude_git: stk500v2_jtag3_open()
avrdude_git: usbhid_open(): probing for max packet size
avrdude_git: usbhid_open(): setting max_xfer from DAP_Info response to 64
avrdude_git: found CMSIS-DAP compliant device, using EDBG protocol
avrdude_git: jtag3_getsync()

avrdude_git: jtag3_edbg_prepare()
avrdude_git: jtag3_edbg_prepare(): connection status 0x01
avrdude_git: sending sign-on command: 
avrdude_git: jtag3_edbg_send(): sending 3 bytes
avrdude_git: jtag3_edbg_recv():
avrdude_git: jtag3_recv(): Got message seqno 0 (command_sequence == 0)

Raw message:
01  80  00  
[general] OK
             AVR Part                      : ATmega8A
             Chip Erase delay              : 10000 us
             PAGEL                         : PD7
             BS2                           : PC2
             RESET disposition             : possible i/o
             RETRY pulse                   : SCK
             Serial program mode           : yes
             Parallel program mode         : yes
             Timeout                       : 200
             StabDelay                     : 100
             CmdexeDelay                   : 25
             SyncLoops                     : 32
             PollIndex                     : 3
             PollValue                     : 0x53
             Memory Detail                 :

                                               Block Poll               Page                       Polled
               Memory Type Alias    Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
               ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
               eeprom                  4    20   128    0 no        512    4      0  9000  9000 0xff 0xff
                                               Block Poll               Page                       Polled
               Memory Type Alias    Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
               ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
               flash                  33    10    64    0 yes      8192   64    128  4500  4500 0xff 0x00
                                               Block Poll               Page                       Polled
               Memory Type Alias    Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
               ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
               lfuse                   0     0     0    0 no          1    1      0  2000  2000 0x00 0x00
                                               Block Poll               Page                       Polled
               Memory Type Alias    Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
               ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
               hfuse                   0     0     0    0 no          1    1      0  2000  2000 0x00 0x00
                                               Block Poll               Page                       Polled
               Memory Type Alias    Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
               ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
               lock                    0     0     0    0 no          1    1      0  2000  2000 0x00 0x00
                                               Block Poll               Page                       Polled
               Memory Type Alias    Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
               ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
               signature               0     0     0    0 no          3    1      0     0     0 0x00 0x00
                                               Block Poll               Page                       Polled
               Memory Type Alias    Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
               ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
               calibration             0     0     0    0 no          4    1      0     0     0 0x00 0x00

             Programmer Type : JTAG3_ISP
             Description     : MPLAB(R) PICkit 4 in ISP mode
avrdude_git: jtag3_getparm()
avrdude_git: sending get parameter (scope 0x01, section 1, parm 0) command: 
avrdude_git: jtag3_edbg_send(): sending 6 bytes
avrdude_git: jtag3_edbg_recv():
avrdude_git: jtag3_recv(): Got message seqno 1 (command_sequence == 1)

Raw message:
01  84  01  02  13  00  
[general] Data returned:
0x01 0x02 0x13 0x00 
             Vtarget         : 4.87 V
avrdude_git: jtag3_getparm()
avrdude_git: sending get parameter (scope 0x12, section 1, parm 32) command: 
avrdude_git: jtag3_edbg_send(): sending 6 bytes
avrdude_git: jtag3_edbg_recv():
avrdude_git: jtag3_recv(): Got message seqno 2 (command_sequence == 2)

Raw message:
12  84  00  e8  03  00  
[AVR] Data returned:
0x00 0xe8 0x03 0x00 
             JTAG clock megaAVR/program   : 1000 kHz
avrdude_git: jtag3_getparm()
avrdude_git: sending get parameter (scope 0x12, section 1, parm 33) command: 
avrdude_git: jtag3_edbg_send(): sending 6 bytes
avrdude_git: jtag3_edbg_recv():
avrdude_git: jtag3_recv(): Got message seqno 3 (command_sequence == 3)

Raw message:
12  84  00  64  00  00  
[AVR] Data returned:
0x00 0x64 0x00 0x00 
             JTAG clock megaAVR/debug     : 100 kHz
avrdude_git: jtag3_getparm()
avrdude_git: sending get parameter (scope 0x12, section 1, parm 48) command: 
avrdude_git: jtag3_edbg_send(): sending 6 bytes
avrdude_git: jtag3_edbg_recv():
avrdude_git: jtag3_recv(): Got message seqno 4 (command_sequence == 4)

Raw message:
12  84  00  00  00  00  
[AVR] Data returned:
0x00 0x00 0x00 0x00 
avrdude_git: jtag3_getparm()
avrdude_git: sending get parameter (scope 0x12, section 1, parm 49) command: 
avrdude_git: jtag3_edbg_send(): sending 6 bytes
avrdude_git: jtag3_edbg_recv():
avrdude_git: jtag3_recv(): Got message seqno 5 (command_sequence == 5)

Raw message:
12  84  00  64  00  00  
[AVR] Data returned:
0x00 0x64 0x00 0x00 
             PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude_git: jtag3_edbg_send(): sending 2 bytes
avrdude_git: jtag3_edbg_recv():
avrdude_git: jtag3_recv(): Got message seqno 6 (command_sequence == 6)
             SCK period                   : 8.00 us

avrdude_git: jtag3_setparm()
avrdude_git: sending set parameter (scope 0x12, section 0, parm 0) command: 
avrdude_git: jtag3_edbg_send(): sending 7 bytes
avrdude_git: jtag3_edbg_recv():
avrdude_git: jtag3_recv(): Got message seqno 7 (command_sequence == 7)

Raw message:
12  80  00  
[AVR] OK
avrdude_git: jtag3_setparm()
avrdude_git: sending set parameter (scope 0x12, section 0, parm 1) command: 
avrdude_git: jtag3_edbg_send(): sending 7 bytes
avrdude_git: jtag3_edbg_recv():
avrdude_git: jtag3_recv(): Got message seqno 8 (command_sequence == 8)

Raw message:
12  80  00  
[AVR] OK
avrdude_git: jtag3_setparm()
avrdude_git: sending set parameter (scope 0x12, section 1, parm 0) command: 
avrdude_git: jtag3_edbg_send(): sending 7 bytes
avrdude_git: jtag3_edbg_recv():
avrdude_git: jtag3_recv(): Got message seqno 9 (command_sequence == 9)

Raw message:
12  80  00  
[AVR] OK

avrdude_git: jtag3_edbg_send(): sending 2 bytes
avrdude_git: jtag3_edbg_recv():
avrdude_git: jtag3_recv(): Got message seqno 10 (command_sequence == 10)

avrdude_git: jtag3_edbg_send(): sending 13 bytes
avrdude_git: jtag3_edbg_recv():
avrdude_git: jtag3_recv(): Got message seqno 11 (command_sequence == 11)
avrdude_git: AVR device initialized and ready to accept instructions

Reading |                                                    | 0% 0.00 s avrdude_git: stk500isp_read_byte(.., signature, 0x0, ...)
avrdude_git: stk500isp_read_byte(): sending read memory command: 
avrdude_git: jtag3_edbg_send(): sending 7 bytes
avrdude_git: jtag3_edbg_recv():
avrdude_git: jtag3_recv(): Got message seqno 12 (command_sequence == 12)
avrdude_git: stk500isp_read_byte(.., signature, 0x1, ...)
avrdude_git: stk500isp_read_byte(): sending read memory command: 
avrdude_git: jtag3_edbg_send(): sending 7 bytes
avrdude_git: jtag3_edbg_recv():
avrdude_git: jtag3_recv(): Got message seqno 13 (command_sequence == 13)
Reading | #################                                  | 33% 0.01 s avrdude_git: stk500isp_read_byte(.., signature, 0x2, ...)
avrdude_git: stk500isp_read_byte(): sending read memory command: 
avrdude_git: jtag3_edbg_send(): sending 7 bytes
avrdude_git: jtag3_edbg_recv():
avrdude_git: jtag3_recv(): Got message seqno 14 (command_sequence == 14)
Reading | ################################################## | 100% 0.01 s 

avrdude_git: device signature = 0x1e9307 (probably m8a)

avrdude_git: jtag3_edbg_send(): sending 4 bytes
avrdude_git: jtag3_edbg_recv():
avrdude_git: jtag3_recv(): Got message seqno 15 (command_sequence == 15)
avrdude_git: stk500v2_jtag3_close()
avrdude_git: jtag3_close()
avrdude_git: sending AVR sign-off command: 
avrdude_git: jtag3_edbg_send(): sending 3 bytes
avrdude_git: jtag3_edbg_recv():
avrdude_git: jtag3_recv(): Got message seqno 16 (command_sequence == 16)

Raw message:
12  80  00  
[AVR] OK
avrdude_git: sending sign-off command: 
avrdude_git: jtag3_edbg_send(): sending 4 bytes
avrdude_git: jtag3_edbg_recv():
avrdude_git: jtag3_recv(): Got message seqno 17 (command_sequence == 17)

Raw message:
01  80  00  
[general] OK

avrdude_git: jtag3_edbg_signoff()

avrdude_git done.  Thank you.
mcuee commented 1 year ago

Simple troubleshooting patch (to bypass the first warning and error) does not help too much.

mcuee@UbuntuSwift3:~/build/avr/avrdude_main$ git diff
diff --git a/src/usb_libusb.c b/src/usb_libusb.c
index 858db21..e12046a 100644
--- a/src/usb_libusb.c
+++ b/src/usb_libusb.c
@@ -192,13 +192,13 @@ static int usbdev_open(const char *port, union pinfo pinfo, union filedescriptor
                      goto trynext;
                    }

-                 if (usb_set_configuration(udev, dev->config[0].bConfigurationValue))
-                   {
-                     pmsg_warning("unable to set configuration %d: %s\n",
-                        dev->config[0].bConfigurationValue, usb_strerror());
+                 //if (usb_set_configuration(udev, dev->config[0].bConfigurationValue))
+                 //  {
+                 //    pmsg_warning("unable to set configuration %d: %s\n",
+                   //     dev->config[0].bConfigurationValue, usb_strerror());
                      /* let's hope it has already been configured */
                      // goto trynext;
-                   }
+                 //  }

                  for (iface = 0; iface < dev->config[0].bNumInterfaces; iface++)
                    {
@@ -341,7 +341,7 @@ static int usbdev_send(const union filedescriptor *fd, const unsigned char *bp,
       rv = usb_bulk_write(udev, fd->usb.wep, (char *)bp, tx_size, 10000);
     if (rv != tx_size)
     {
-        pmsg_error("wrote %d out of %d bytes, err = %s\n", rv, tx_size, usb_strerror());
+        pmsg_warning("wrote %d out of %d bytes, err = %s\n", rv, tx_size, usb_strerror());
         return -1;
     }
     bp += tx_size;

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ sudo ./avrdude_issue1221 -c pickit4_isp -p m8a -vvvv
executable_abspath = /home/mcuee/build/avr/avrdude_bin/avrdude_issue1221
executable_abspath_len = 51
executable_dirpath = /home/mcuee/build/avr/avrdude_bin
executable_dirpath_len = 33
sys_config = /home/mcuee/build/avr/avrdude_bin/avrdude.conf
sys_config_found = true

avrdude_issue1221: Version 7.0-20221214 (159d126)
                   Copyright the AVRDUDE authors;
                   see https://github.com/avrdudes/avrdude/blob/main/AUTHORS

                   System wide configuration file is /home/mcuee/build/avr/avrdude_bin/avrdude.conf
                   User configuration file is /root/.avrduderc
                   User configuration file does not exist or is not a regular file, skipping

                   Using Port                    : usb
                   Using Programmer              : pickit4_isp
avrdude_issue1221: stk500v2_jtag3_open()
avrdude_issue1221: usbdev_open(): found MPLAB PICkit 4 CMSIS-DAP, serno: BUR200973052
avrdude_issue1221: found CMSIS-DAP compliant device, using EDBG protocol
avrdude_issue1221: jtag3_getsync()

avrdude_issue1221: jtag3_edbg_prepare()
avrdude_issue1221 usbdev_send() [usb_libusb.c:344] warning: wrote -5 out of 912 bytes, err = Input/output error
avrdude_issue1221 jtag3_edbg_prepare() [jtag3.c:542] error: unable to send command to serial port
avrdude_issue1221 stk500v2_jtag3_open() [stk500v2.c:3510] error: unable to sync with the JTAGICE3 in ISP mode
avrdude_issue1221 main() [main.c:1123] error: unable to open programmer pickit4_isp on port usb

avrdude_issue1221 done.  Thank you.
mcuee commented 1 year ago

Just revisit this issue again.

I tested ATmega328pb-xmini with both the defaut HID driver (using hidapi library) or WinUSB driver, both works fine, even though there is a warning for using WinUSB driver (libusb library). There is a warning when using WinUSB/libusb.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude -c xplainedmini -p m328pb -v

avrdude: Version 7.1-20230504 (e6a2f3c0)
         Copyright the AVRDUDE authors;
         see https://github.com/avrdudes/avrdude/blob/main/AUTHORS

         System wide configuration file is C:\work\avr\avrdude_test\avrdude_bin\avrdude.conf

         Using Port                    : usb
         Using Programmer              : xplainedmini
avrdude usbhid_open() warning: USB device with VID: 0x03eb and PID: 0x2145 not found
avrdude: usbdev_open(): found mEDBG CMSIS-DAP, serno: ATML2523052700008048
avrdude: max packet size expected 912, but found 64 due to EP 0x81's wMaxPacketSize
avrdude: found CMSIS-DAP compliant device, using EDBG protocol
         AVR Part                      : ATmega328PB
         Chip Erase delay              : 10500 us
         PAGEL                         : PD7
         BS2                           : PC2
         RESET disposition             : possible i/o
         RETRY pulse                   : SCK
         Serial program mode           : yes
         Parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                           Block Poll               Page                       Polled
           Memory Type Alias    Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom                 65    20     4    0 no       1024    4      0  3600  3600 0x00 0x00
           flash                  65    10   128    0 yes     32768  128    256  4500  4500 0x00 0x00
           lfuse                   0     0     0    0 no          1    1      0  4500  4500 0x00 0x00
           hfuse                   0     0     0    0 no          1    1      0  4500  4500 0x00 0x00
           efuse                   0     0     0    0 no          1    1      0  4500  4500 0x00 0x00
           lock                    0     0     0    0 no          1    1      0  4500  4500 0x00 0x00
           signature               0     0     0    0 no          3    1      0     0     0 0x00 0x00
           calibration             0     0     0    0 no          1    1      0     0     0 0x00 0x00

         Programmer Type : JTAG3_ISP
         Description     : Atmel AVR XplainedMini in ISP mode
         ICE HW version  : 0
         ICE FW version  : 1.22 (rel. 134)
         Serial number   : ATML2523052700008048
         SCK period      : 8.00 us
         Vtarget         : 5.00 V

avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9516 (probably m328pb)

avrdude done.  Thank you.

No warnings with the default HID driver.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude -c xplainedmini -p m328pb -v

avrdude: Version 7.1-20230504 (e6a2f3c0)
         Copyright the AVRDUDE authors;
         see https://github.com/avrdudes/avrdude/blob/main/AUTHORS

         System wide configuration file is C:\work\avr\avrdude_test\avrdude_bin\avrdude.conf

         Using Port                    : usb
         Using Programmer              : xplainedmini
avrdude: found CMSIS-DAP compliant device, using EDBG protocol
         AVR Part                      : ATmega328PB
         Chip Erase delay              : 10500 us
         PAGEL                         : PD7
         BS2                           : PC2
         RESET disposition             : possible i/o
         RETRY pulse                   : SCK
         Serial program mode           : yes
         Parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                           Block Poll               Page                       Polled
           Memory Type Alias    Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom                 65    20     4    0 no       1024    4      0  3600  3600 0x00 0x00
           flash                  65    10   128    0 yes     32768  128    256  4500  4500 0x00 0x00
           lfuse                   0     0     0    0 no          1    1      0  4500  4500 0x00 0x00
           hfuse                   0     0     0    0 no          1    1      0  4500  4500 0x00 0x00
           efuse                   0     0     0    0 no          1    1      0  4500  4500 0x00 0x00
           lock                    0     0     0    0 no          1    1      0  4500  4500 0x00 0x00
           signature               0     0     0    0 no          3    1      0     0     0 0x00 0x00
           calibration             0     0     0    0 no          1    1      0     0     0 0x00 0x00

         Programmer Type : JTAG3_ISP
         Description     : Atmel AVR XplainedMini in ISP mode
         ICE HW version  : 0
         ICE FW version  : 1.22 (rel. 134)
         Serial number   : ATML2523052700008048
         SCK period      : 8.00 us
         Vtarget         : 5.00 V

avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9516 (probably m328pb)

avrdude done.  Thank you.
mcuee commented 1 year ago

Somehow for PICKit 4, the warning becomes an error when using WinUSB driver/libusb. Need to dig further.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude -c pickit4_isp -p m8a -v

avrdude: Version 7.1-20230504 (e6a2f3c0)
         Copyright the AVRDUDE authors;
         see https://github.com/avrdudes/avrdude/blob/main/AUTHORS

         System wide configuration file is C:\work\avr\avrdude_test\avrdude_bin\avrdude.conf

         Using Port                    : usb
         Using Programmer              : pickit4_isp
avrdude usbhid_open() warning: USB device with VID: 0x03eb and PID: 0x2177 not found
avrdude usbhid_open() warning: USB device with VID: 0x03eb and PID: 0x2178 not found
avrdude usbhid_open() warning: USB device with VID: 0x03eb and PID: 0x2179 not found
avrdude: usbdev_open(): found MPLAB PICkit 4 CMSIS-DAP, serno: BUR200973052
avrdude: found CMSIS-DAP compliant device, using EDBG protocol
avrdude usbdev_send() error: wrote -2 out of 912 bytes, err = No such file or directory
avrdude jtag3_edbg_prepare() error: unable to send command to serial port
avrdude stk500v2_jtag3_open() error: unable to sync with the JTAGICE3 in ISP mode
avrdude main() error: unable to open programmer pickit4_isp on port usb

avrdude done.  Thank you.
mcuee commented 1 year ago

In any case, this is not of high priority since the interface is an HID interface and we should use HIDAPI anyway and not libusb.

mcuee commented 1 year ago

From hidapi hidtest utility (under Windows, which only generate simulated HID report, I will test under Linux later which will dump the real HID report.

Input report size should only be 64 bytes. No idea why 912 bytes was there in the libusb output.

Device Found
  type: 03eb 2177
  path: \\?\HID#VID_03EB&PID_2177&MI_00#8&36594c76&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
  serial_number: BUR200973052
  Manufacturer: Microchip Technology Incorporated
  Product:      MPLAB PICkit 4 CMSIS-DAP
  Release:      100
  Interface:    0
  Usage (page): 0x1 (0xff00)
  Bus type: 1 (USB)

  Report Descriptor: (47 bytes)
0x06, 0x00, 0xff, 0x09, 0x01, 0xa1, 0x01, 0x09, 0x01, 0x15,
0x00, 0x26, 0xff, 0x00, 0x75, 0x08, 0x95, 0x40, 0x81, 0x02,
0x09, 0x01, 0x15, 0x00, 0x26, 0xff, 0x00, 0x75, 0x08, 0x95,
0x40, 0x91, 0x02, 0x09, 0x01, 0x15, 0x00, 0x26, 0xff, 0x00,
0x75, 0x08, 0x95, 0x04, 0xb1, 0x02, 0xc0,

Feed the above through an online HID report parser https://eleccelerator.com/usbdescreqparser/

0x06, 0x00, 0xFF,  // Usage Page (Vendor Defined 0xFF00)
0x09, 0x01,        // Usage (0x01)
0xA1, 0x01,        // Collection (Application)
0x09, 0x01,        //   Usage (0x01)
0x15, 0x00,        //   Logical Minimum (0)
0x26, 0xFF, 0x00,  //   Logical Maximum (255)
0x75, 0x08,        //   Report Size (8)
0x95, 0x40,        //   Report Count (64)
0x81, 0x02,        //   Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x09, 0x01,        //   Usage (0x01)
0x15, 0x00,        //   Logical Minimum (0)
0x26, 0xFF, 0x00,  //   Logical Maximum (255)
0x75, 0x08,        //   Report Size (8)
0x95, 0x40,        //   Report Count (64)
0x91, 0x02,        //   Output (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
0x09, 0x01,        //   Usage (0x01)
0x15, 0x00,        //   Logical Minimum (0)
0x26, 0xFF, 0x00,  //   Logical Maximum (255)
0x75, 0x08,        //   Report Size (8)
0x95, 0x04,        //   Report Count (4)
0xB1, 0x02,        //   Feature (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
0xC0,              // End Collection

// 47 bytes
mcuee commented 1 year ago

Under Linux. The HID report will be the correct one.

Again input report size should only be 64 bytes. No idea why 912 bytes was there in the libusb output.

Device Found
  type: 03eb 2177
  path: /dev/hidraw4
  serial_number: BUR200973052
  Manufacturer: Microchip Technology Incorporated
  Product:      MPLAB PICkit 4 CMSIS-DAP
  Release:      100
  Interface:    0
  Usage (page): 0x1 (0xff00)
  Bus type: 1 (USB)

  Report Descriptor: (35 bytes)
0x06, 0x00, 0xff, 0x09, 0x01, 0xa1, 0x01, 0x15, 0x00, 0x26, 
0xff, 0x00, 0x75, 0x08, 0x96, 0x40, 0x00, 0x09, 0x01, 0x81, 
0x02, 0x96, 0x40, 0x00, 0x09, 0x01, 0x91, 0x02, 0x95, 0x04, 
0x09, 0x01, 0xb1, 0x02, 0xc0,

HID report patser output

0x06, 0x00, 0xFF,  // Usage Page (Vendor Defined 0xFF00)
0x09, 0x01,        // Usage (0x01)
0xA1, 0x01,        // Collection (Application)
0x15, 0x00,        //   Logical Minimum (0)
0x26, 0xFF, 0x00,  //   Logical Maximum (255)
0x75, 0x08,        //   Report Size (8)
0x96, 0x40, 0x00,  //   Report Count (64)
0x09, 0x01,        //   Usage (0x01)
0x81, 0x02,        //   Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x96, 0x40, 0x00,  //   Report Count (64)
0x09, 0x01,        //   Usage (0x01)
0x91, 0x02,        //   Output (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
0x95, 0x04,        //   Report Count (4)
0x09, 0x01,        //   Usage (0x01)
0xB1, 0x02,        //   Feature (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
0xC0,              // End Collection

// 35 bytes
mcuee commented 1 year ago

Full debug log under Linux using hidapi.

Correct run log with hidapi ``` mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ ./avrdude -c pickit4_isp -p m8a -vvvv avrdude: Version 7.1-20230504 (e6a2f3c0) Copyright the AVRDUDE authors; see https://github.com/avrdudes/avrdude/blob/main/AUTHORS System wide configuration file is /home/mcuee/build/avr/avrdude_bin/avrdude.conf User configuration file is /home/mcuee/.avrduderc User configuration file does not exist or is not a regular file, skipping Using Port : usb Using Programmer : pickit4_isp avrdude: stk500v2_jtag3_open() avrdude: usbhid_open(): probing for max packet size avrdude: usbhid_open(): setting max_xfer from DAP_Info response to 64 avrdude: found CMSIS-DAP compliant device, using EDBG protocol avrdude: jtag3_getsync() avrdude: jtag3_edbg_prepare() avrdude: jtag3_edbg_prepare(): connection status 0x01 avrdude: sending sign-on command: avrdude: jtag3_edbg_send(): sending 3 bytes avrdude: jtag3_edbg_recv(): avrdude: jtag3_recv(): Got message seqno 0 (command_sequence == 0) Raw message: 01 80 00 [general] OK AVR Part : ATmega8A Chip Erase delay : 10000 us PAGEL : PD7 BS2 : PC2 RESET disposition : possible i/o RETRY pulse : SCK Serial program mode : yes Parallel program mode : yes Timeout : 200 StabDelay : 100 CmdexeDelay : 25 SyncLoops : 32 PollIndex : 3 PollValue : 0x53 Memory Detail : Block Poll Page Polled Memory Type Alias Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- --------- eeprom 4 20 128 0 no 512 4 0 9000 9000 0xff 0xff Block Poll Page Polled Memory Type Alias Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- --------- flash 33 10 64 0 yes 8192 64 128 4500 4500 0xff 0x00 Block Poll Page Polled Memory Type Alias Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- --------- lfuse 0 0 0 0 no 1 1 0 2000 2000 0x00 0x00 Block Poll Page Polled Memory Type Alias Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- --------- hfuse 0 0 0 0 no 1 1 0 2000 2000 0x00 0x00 Block Poll Page Polled Memory Type Alias Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- --------- lock 0 0 0 0 no 1 1 0 2000 2000 0x00 0x00 Block Poll Page Polled Memory Type Alias Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- --------- signature 0 0 0 0 no 3 1 0 0 0 0x00 0x00 Block Poll Page Polled Memory Type Alias Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- --------- calibration 0 0 0 0 no 4 1 0 0 0 0x00 0x00 Programmer Type : JTAG3_ISP Description : MPLAB(R) PICkit 4 in ISP mode avrdude: jtag3_getparm() avrdude: sending get parameter (scope 0x01, section 0, parm 0) command: avrdude: jtag3_edbg_send(): sending 6 bytes avrdude: jtag3_edbg_recv(): avrdude: jtag3_recv(): Got message seqno 1 (command_sequence == 1) Raw message: 01 84 01 04 01 0e 0c 01 00 [general] Data returned: 0x01 0x04 0x01 0x0e 0x0c 0x01 0x00 ICE HW version : 4 ICE FW version : 1.14 (rel. 268) Serial number : BUR200973052 avrdude: jtag3_edbg_send(): sending 2 bytes avrdude: jtag3_edbg_recv(): avrdude: jtag3_recv(): Got message seqno 2 (command_sequence == 2) SCK period : 8.00 us avrdude: jtag3_getparm() avrdude: sending get parameter (scope 0x01, section 1, parm 0) command: avrdude: jtag3_edbg_send(): sending 6 bytes avrdude: jtag3_edbg_recv(): avrdude: jtag3_recv(): Got message seqno 3 (command_sequence == 3) Raw message: 01 84 01 4a 13 00 [general] Data returned: 0x01 0x4a 0x13 0x00 Vtarget : 4.94 V avrdude: jtag3_getparm() avrdude: sending get parameter (scope 0x12, section 1, parm 0) command: avrdude: jtag3_edbg_send(): sending 6 bytes avrdude: jtag3_edbg_recv(): avrdude: jtag3_recv(): Got message seqno 4 (command_sequence == 4) Raw message: 12 84 00 00 00 [AVR] Data returned: 0x00 0x00 0x00 avrdude: jtag3_getparm() avrdude: sending get parameter (scope 0x12, section 0, parm 0) command: avrdude: jtag3_edbg_send(): sending 6 bytes avrdude: jtag3_edbg_recv(): avrdude: jtag3_recv(): Got message seqno 5 (command_sequence == 5) Raw message: 12 84 00 ff 00 [AVR] Data returned: 0x00 0xff 0x00 avrdude: jtag3_setparm() avrdude: sending set parameter (scope 0x12, section 0, parm 0) command: avrdude: jtag3_edbg_send(): sending 7 bytes avrdude: jtag3_edbg_recv(): avrdude: jtag3_recv(): Got message seqno 6 (command_sequence == 6) Raw message: 12 80 00 [AVR] OK avrdude: jtag3_setparm() avrdude: sending set parameter (scope 0x12, section 0, parm 1) command: avrdude: jtag3_edbg_send(): sending 7 bytes avrdude: jtag3_edbg_recv(): avrdude: jtag3_recv(): Got message seqno 7 (command_sequence == 7) Raw message: 12 80 00 [AVR] OK avrdude: jtag3_setparm() avrdude: sending set parameter (scope 0x12, section 1, parm 0) command: avrdude: jtag3_edbg_send(): sending 7 bytes avrdude: jtag3_edbg_recv(): avrdude: jtag3_recv(): Got message seqno 8 (command_sequence == 8) Raw message: 12 80 00 [AVR] OK avrdude: jtag3_edbg_send(): sending 2 bytes avrdude: jtag3_edbg_recv(): avrdude: jtag3_recv(): Got message seqno 9 (command_sequence == 9) avrdude: jtag3_edbg_send(): sending 13 bytes avrdude: jtag3_edbg_recv(): avrdude: jtag3_recv(): Got message seqno 10 (command_sequence == 10) avrdude: AVR device initialized and ready to accept instructions Reading | | 0% 0.00 s avrdude: stk500isp_read_byte(.., signature, 0x0, ...) avrdude: stk500isp_read_byte(): sending read memory command: avrdude: jtag3_edbg_send(): sending 7 bytes avrdude: jtag3_edbg_recv(): avrdude: jtag3_recv(): Got message seqno 11 (command_sequence == 11) avrdude: stk500isp_read_byte(.., signature, 0x1, ...) avrdude: stk500isp_read_byte(): sending read memory command: avrdude: jtag3_edbg_send(): sending 7 bytes avrdude: jtag3_edbg_recv(): avrdude: jtag3_recv(): Got message seqno 12 (command_sequence == 12) Reading | ################# | 33% 0.01 s avrdude: stk500isp_read_byte(.., signature, 0x2, ...) avrdude: stk500isp_read_byte(): sending read memory command: avrdude: jtag3_edbg_send(): sending 7 bytes avrdude: jtag3_edbg_recv(): avrdude: jtag3_recv(): Got message seqno 13 (command_sequence == 13) Reading | ################################################## | 100% 0.01 s avrdude: device signature = 0x1e9307 (probably m8a) avrdude: jtag3_edbg_send(): sending 4 bytes avrdude: jtag3_edbg_recv(): avrdude: jtag3_recv(): Got message seqno 14 (command_sequence == 14) avrdude: stk500v2_jtag3_close() avrdude: jtag3_close() avrdude: sending AVR sign-off command: avrdude: jtag3_edbg_send(): sending 3 bytes avrdude: jtag3_edbg_recv(): avrdude: jtag3_recv(): Got message seqno 15 (command_sequence == 15) Raw message: 12 80 00 [AVR] OK avrdude: sending sign-off command: avrdude: jtag3_edbg_send(): sending 4 bytes avrdude: jtag3_edbg_recv(): avrdude: jtag3_recv(): Got message seqno 16 (command_sequence == 16) Raw message: 01 80 00 [general] OK avrdude: jtag3_edbg_signoff() avrdude done. Thank you. ```
mcuee commented 1 year ago

I will need to debug the issue under Linux a bit further. The error output is actually as expected since avrdude did not detach the kernel HID driver, that is why we have the warning like this.

usbdev_open() warning: unable to set configuration 1: Device or resource busy

I will try later to manually detach the kernel HID driver.

mcuee commented 1 year ago

The following is the step to manually unbind kernel usbhid driver.

Ref: Manual driver binding and unbinding https://lwn.net/Articles/143397/

mcuee@UbuntuSwift3:/sys/bus/usb/drivers/usbhid$ ls
3-2.1:1.0  3-3:1.0  3-3:1.1  3-3:1.2  bind  module  new_id  remove_id  uevent  unbind
mcuee@UbuntuSwift3:/sys/bus/usb/drivers/usbhid$ lsusb -t
/:  Bus 04.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/4p, 10000M
/:  Bus 03.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/12p, 480M
    |__ Port 2: Dev 2, If 0, Class=Hub, Driver=hub/4p, 480M
        |__ Port 1: Dev 10, If 0, Class=Human Interface Device, Driver=usbhid, 480M
        |__ Port 1: Dev 10, If 1, Class=Communications, Driver=cdc_acm, 480M
        |__ Port 1: Dev 10, If 2, Class=CDC Data, Driver=cdc_acm, 480M
    |__ Port 3: Dev 3, If 0, Class=Human Interface Device, Driver=usbhid, 12M
    |__ Port 3: Dev 3, If 1, Class=Human Interface Device, Driver=usbhid, 12M
    |__ Port 3: Dev 3, If 2, Class=Human Interface Device, Driver=usbhid, 12M
    |__ Port 5: Dev 5, If 0, Class=Video, Driver=uvcvideo, 480M
    |__ Port 5: Dev 5, If 1, Class=Video, Driver=uvcvideo, 480M
    |__ Port 7: Dev 6, If 0, Class=Vendor Specific Class, Driver=, 480M
    |__ Port 10: Dev 7, If 0, Class=Wireless, Driver=btusb, 12M
    |__ Port 10: Dev 7, If 1, Class=Wireless, Driver=btusb, 12M
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/4p, 10000M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/4p, 5000M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/1p, 480M
mcuee@UbuntuSwift3:/sys/bus/usb/drivers/usbhid$ cd 3-2.1\:1.0
mcuee@UbuntuSwift3:/sys/bus/usb/drivers/usbhid/3-2.1:1.0$ ls
0003:03EB:2177.0016  bAlternateSetting  bInterfaceNumber    bInterfaceSubClass  driver  ep_81     power      supports_autosuspend  usbmisc
authorized           bInterfaceClass    bInterfaceProtocol  bNumEndpoints       ep_02   modalias  subsystem  uevent

mcuee@UbuntuSwift3:/sys/bus/usb/drivers/usbhid$ tree /sys/bus/usb/drivers/usbhid/
/sys/bus/usb/drivers/usbhid/
├── 3-2.1:1.0 -> ../../../../devices/pci0000:00/0000:00:14.0/usb3/3-2/3-2.1/3-2.1:1.0
├── 3-3:1.0 -> ../../../../devices/pci0000:00/0000:00:14.0/usb3/3-3/3-3:1.0
├── 3-3:1.1 -> ../../../../devices/pci0000:00/0000:00:14.0/usb3/3-3/3-3:1.1
├── 3-3:1.2 -> ../../../../devices/pci0000:00/0000:00:14.0/usb3/3-3/3-3:1.2
├── bind
├── module -> ../../../../module/usbhid
├── new_id
├── remove_id
├── uevent
└── unbind

5 directories, 5 files
mcuee@UbuntuSwift3:/sys/bus/usb/drivers/usbhid$ sudo su
root@UbuntuSwift3:/sys/bus/usb/drivers/usbhid# echo -n "3-2.1:1.0" >/sys/bus/usb/drivers/usbhid/unbind 
root@UbuntuSwift3:/sys/bus/usb/drivers/usbhid# lsusb -t
/:  Bus 04.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/4p, 10000M
/:  Bus 03.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/12p, 480M
    |__ Port 2: Dev 2, If 0, Class=Hub, Driver=hub/4p, 480M
        |__ Port 1: Dev 10, If 0, Class=Human Interface Device, Driver=, 480M
        |__ Port 1: Dev 10, If 1, Class=Communications, Driver=cdc_acm, 480M
        |__ Port 1: Dev 10, If 2, Class=CDC Data, Driver=cdc_acm, 480M
    |__ Port 3: Dev 3, If 0, Class=Human Interface Device, Driver=usbhid, 12M
    |__ Port 3: Dev 3, If 1, Class=Human Interface Device, Driver=usbhid, 12M
    |__ Port 3: Dev 3, If 2, Class=Human Interface Device, Driver=usbhid, 12M
    |__ Port 5: Dev 5, If 0, Class=Video, Driver=uvcvideo, 480M
    |__ Port 5: Dev 5, If 1, Class=Video, Driver=uvcvideo, 480M
    |__ Port 7: Dev 6, If 0, Class=Vendor Specific Class, Driver=, 480M
    |__ Port 10: Dev 7, If 0, Class=Wireless, Driver=btusb, 12M
    |__ Port 10: Dev 7, If 1, Class=Wireless, Driver=btusb, 12M
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/4p, 10000M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/4p, 5000M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/1p, 480M
mcuee commented 1 year ago

After detaching the kernel usbhid driver, default avrdude build will still work as it is using hidapi-libusb (because of CMake library search order.

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ ./avrdude -c pickit4_isp -p m8a -v

avrdude: Version 7.1-20230504 (e6a2f3c0)
         Copyright the AVRDUDE authors;
         see https://github.com/avrdudes/avrdude/blob/main/AUTHORS

         System wide configuration file is /home/mcuee/build/avr/avrdude_bin/avrdude.conf
         User configuration file is /home/mcuee/.avrduderc
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : usb
         Using Programmer              : pickit4_isp
avrdude: found CMSIS-DAP compliant device, using EDBG protocol
         AVR Part                      : ATmega8A
         Chip Erase delay              : 10000 us
         PAGEL                         : PD7
         BS2                           : PC2
         RESET disposition             : possible i/o
         RETRY pulse                   : SCK
         Serial program mode           : yes
         Parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                           Block Poll               Page                       Polled
           Memory Type Alias    Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom                  4    20   128    0 no        512    4      0  9000  9000 0xff 0xff
           flash                  33    10    64    0 yes      8192   64    128  4500  4500 0xff 0x00
           lfuse                   0     0     0    0 no          1    1      0  2000  2000 0x00 0x00
           hfuse                   0     0     0    0 no          1    1      0  2000  2000 0x00 0x00
           lock                    0     0     0    0 no          1    1      0  2000  2000 0x00 0x00
           signature               0     0     0    0 no          3    1      0     0     0 0x00 0x00
           calibration             0     0     0    0 no          4    1      0     0     0 0x00 0x00

         Programmer Type : JTAG3_ISP
         Description     : MPLAB(R) PICkit 4 in ISP mode
         ICE HW version  : 4
         ICE FW version  : 1.14 (rel. 268)
         Serial number   : BUR200973052
         SCK period      : 8.00 us
         Vtarget         : 4.93 V

avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9307 (probably m8a)

avrdude done.  Thank you.

Buf if I change the sequence and use hidapi-hidraw, then it will not work with hidapi as the hidraw driver has been detached. Then it will fall back to libusb and it does not work.

mcuee@UbuntuSwift3:~/build/avr/avrdude_main$ git diff
diff --git a/CMakeLists.txt b/CMakeLists.txt
index a50207fd..d3f05e18 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -130,7 +130,7 @@ else()
     set(PREFERRED_LIBELF elf)
     set(PREFERRED_LIBUSB usb)
     set(PREFERRED_LIBUSB_1_0 usb-1.0)
-    set(PREFERRED_LIBHIDAPI hidapi hidapi-libusb hidapi-hidraw)
+    set(PREFERRED_LIBHIDAPI hidapi hidapi-hidraw hidapi-libusb)
     set(PREFERRED_LIBFTDI ftdi)
     set(PREFERRED_LIBFTDI1 ftdi1)
     set(PREFERRED_LIBREADLINE readline)

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ ./avrdude_git_hidraw -c pickit4_isp -p m8a -v

avrdude_git_hidraw: Version 7.1-20230504 (e6a2f3c0)
                    Copyright the AVRDUDE authors;
                    see https://github.com/avrdudes/avrdude/blob/main/AUTHORS

                    System wide configuration file is /home/mcuee/build/avr/avrdude_bin/avrdude.conf
                    User configuration file is /home/mcuee/.avrduderc
                    User configuration file does not exist or is not a regular file, skipping

                    Using Port                    : usb
                    Using Programmer              : pickit4_isp
avrdude_git_hidraw usbhid_open() warning: USB device with VID: 0x03eb and PID: 0x2177 not found
avrdude_git_hidraw usbhid_open() warning: USB device with VID: 0x03eb and PID: 0x2178 not found
avrdude_git_hidraw usbhid_open() warning: USB device with VID: 0x03eb and PID: 0x2179 not found
avrdude_git_hidraw: usbdev_open(): found MPLAB PICkit 4 CMSIS-DAP, serno: BUR200973052
avrdude_git_hidraw usbdev_open() warning: unable to set configuration 1: Device or resource busy
avrdude_git_hidraw: found CMSIS-DAP compliant device, using EDBG protocol
avrdude_git_hidraw usbdev_send() error: wrote -5 out of 912 bytes, err = Input/output error
avrdude_git_hidraw jtag3_edbg_prepare() error: unable to send command to serial port
avrdude_git_hidraw stk500v2_jtag3_open() error: unable to sync with the JTAGICE3 in ISP mode
avrdude_git_hidraw main() error: unable to open programmer pickit4_isp on port usb

avrdude_git_hidraw done.  Thank you.
mcuee commented 1 year ago

It is very strange that the code path for the fall-back libusb is different from the normal hidapi code path mentioned above

mcuee@UbuntuSwift3:~/build/avr/avrdude_main$ git diff
diff --git a/CMakeLists.txt b/CMakeLists.txt
index a50207fd..d3f05e18 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -130,7 +130,7 @@ else()
     set(PREFERRED_LIBELF elf)
     set(PREFERRED_LIBUSB usb)
     set(PREFERRED_LIBUSB_1_0 usb-1.0)
-    set(PREFERRED_LIBHIDAPI hidapi hidapi-libusb hidapi-hidraw)
+    set(PREFERRED_LIBHIDAPI hidapi hidapi-hidraw hidapi-libusb)
     set(PREFERRED_LIBFTDI ftdi)
     set(PREFERRED_LIBFTDI1 ftdi1)
     set(PREFERRED_LIBREADLINE readline)
diff --git a/src/usb_libusb.c b/src/usb_libusb.c
index 605828ee..94dba683 100644
--- a/src/usb_libusb.c
+++ b/src/usb_libusb.c
@@ -193,13 +193,13 @@ static int usbdev_open(const char *port, union pinfo pinfo, union filedescriptor
                      goto trynext;
                    }

-                 if (usb_set_configuration(udev, dev->config[0].bConfigurationValue))
-                   {
-                     pmsg_warning("unable to set configuration %d: %s\n",
-                        dev->config[0].bConfigurationValue, usb_strerror());
-                     /* let's hope it has already been configured */
-                     // goto trynext;
-                   }
+//               if (usb_set_configuration(udev, dev->config[0].bConfigurationValue))
+//                 {
+//                   pmsg_warning("unable to set configuration %d: %s\n",
+//                        dev->config[0].bConfigurationValue, usb_strerror());
+//                   /* let's hope it has already been configured */
+//                   // goto trynext;
+//                 }

                  for (iface = 0; iface < dev->config[0].bNumInterfaces; iface++)
                    {
@@ -343,7 +343,7 @@ static int usbdev_send(const union filedescriptor *fd, const unsigned char *bp,
     if (rv != tx_size)
     {
         pmsg_error("wrote %d out of %d bytes, err = %s\n", rv, tx_size, usb_strerror());
-        return -1;
+ //       return -1;
     }
     bp += tx_size;
     mlen -= tx_size;

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ ./avrdude_mod2_hidraw -c pickit4_isp -p m8a -vvvv

avrdude_mod2_hidraw: Version 7.1-20230504 (e6a2f3c0)
                     Copyright the AVRDUDE authors;
                     see https://github.com/avrdudes/avrdude/blob/main/AUTHORS

                     System wide configuration file is /home/mcuee/build/avr/avrdude_bin/avrdude.conf
                     User configuration file is /home/mcuee/.avrduderc
                     User configuration file does not exist or is not a regular file, skipping

                     Using Port                    : usb
                     Using Programmer              : pickit4_isp
avrdude_mod2_hidraw: stk500v2_jtag3_open()
avrdude_mod2_hidraw usbhid_open() [usb_hidapi.c:126] warning: USB device with VID: 0x03eb and PID: 0x2177 not found
avrdude_mod2_hidraw usbhid_open() [usb_hidapi.c:126] warning: USB device with VID: 0x03eb and PID: 0x2178 not found
avrdude_mod2_hidraw usbhid_open() [usb_hidapi.c:126] warning: USB device with VID: 0x03eb and PID: 0x2179 not found
avrdude_mod2_hidraw: usbdev_open(): found MPLAB PICkit 4 CMSIS-DAP, serno: BUR200973052
avrdude_mod2_hidraw: found CMSIS-DAP compliant device, using EDBG protocol
avrdude_mod2_hidraw: jtag3_getsync()

avrdude_mod2_hidraw: jtag3_edbg_prepare()
libusb: error [submit_bulk_transfer] submiturb failed, errno=2
avrdude_mod2_hidraw usbdev_send() [usb_libusb.c:345] error: wrote -5 out of 912 bytes, err = Input/output error
avrdude_mod2_hidraw: sent: . [02] . [01] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] . [00] 
libusb: error [submit_bulk_transfer] submiturb failed, errno=2
avrdude_mod2_hidraw: usbdev_recv_frame(): usb_interrupt_read(): Input/output error
avrdude_mod2_hidraw jtag3_edbg_prepare() [jtag3.c:567] error: unable to read from serial port (-1)
avrdude_mod2_hidraw stk500v2_jtag3_open() [stk500v2.c:4051] error: unable to sync with the JTAGICE3 in ISP mode
avrdude_mod2_hidraw main() [main.c:1174] error: unable to open programmer pickit4_isp on port usb

avrdude_mod2_hidraw done.  Thank you.
mcuee commented 1 year ago

@MCUdude and @stefanrueger It seems to me that there is a bug in the fall back code path which some initialization steps were missing. If you got the time, please take a look. Thanks.

mcuee commented 1 year ago

I am not so sure why we get the following error under libusb. It seems to me wrong packet size is used somehow.

usbdev_send() error: wrote -5 out of 912 bytes, err = Input/output error

912 comes from the following code in usbdevs.h. Apparently it does not work with PICKit 4 and SNAP..

/* 
 * The mk3 tools (type jtagice3) have a maxPayloadSize of 912. When
 * accessing paged memory the access should be limited to pageSize.
 */
#define USBDEV_MAX_XFER_3         912
#define USBDEV_MAX_XFER_3_UNPAGED 256

There is a code when using hidapi which seems to be missing when using libusb. Not so sure if this is the reason.

      /* No timely response, assume 512 byte size */
      hid_write(dev, usbbuf, (512 - 64) + 1);
      fd->usb.max_xfer = 512;
      fd->usb.max_xfer = usbbuf[2] + (usbbuf[3] << 8);
      pmsg_debug("usbhid_open(): setting max_xfer from DAP_Info response to %d\n",
        fd->usb.max_xfer);

At least for xplainedpro, I got the following warning.

avrdude: usbdev_open(): found EDBG CMSIS-DAP, serno: ATMLxxxxxxxxxxxxxxxx
avrdude: max packet size expected 912, but found 512 due to EP 0x01's wMaxPacketSize
mcuee commented 1 year ago

@MCUdude

I think we need to set PICKIt4 and SNAP max packet size to 64 to see if that helps.

mcuee commented 10 months ago

I think we need to set PICKIt4 and SNAP max packet size to 64 to see if that helps.

@MCUdude and @stefanrueger,

I believe I have found the root cause of the issue, however, I do not know how to fix the issue. Just wondering if you can look at my comments above to see if there is a simple fix or not. Thanks.

mcuee commented 7 months ago
mcuee commented 6 months ago

Close this one as not planned until someone is actibely working on this issue.

This issue is also linked here. https://github.com/avrdudes/avrdude/discussions/1710

mcuee commented 1 month ago

@askn37

Please take a look at this issue and my comments to see if you can come out with a patch. Thanks.

askn37 commented 1 month ago

Descriptor when ATMELICE3 is recognized at High-Speed ​​on macos/M1/sonoma14.5

High Speed device @ 1 (0x02300000): .............................................   Miscellaneous/Common Class device: "Atmel-ICE CMSIS-DAP"
    Port Information:   0x001a
           Not Captive
           Attached to Root Hub
           External Device
           Connected
           Enabled
    Number Of Endpoints (includes EP0):   
        Total Endpoints for Configuration 1 (current):   5
    Device Descriptor   
        Descriptor Version Number:   0x0200
        Device Class:   239   (Miscellaneous)
        Device Subclass:   2   (Common Class)
        Device Protocol:   1   (Interface Association)
        Device MaxPacketSize:   64
        Device VendorID/ProductID:   0x03EB/0x2141   (unknown vendor)
        Device Version Number:   0x0101
        Number of Configurations:   1
        Manufacturer String:   1 "Atmel Corp."
        Product String:   2 "Atmel-ICE CMSIS-DAP"
        Serial Number String:   3 "J42700015230"
    Configuration Descriptor (current config)   
        Length (and contents):   64
            Raw Descriptor (hex)    0000: 09 02 40 00 02 01 00 80  32 09 04 00 00 02 03 00  
            Raw Descriptor (hex)    0010: 00 04 09 21 11 01 00 01  22 23 00 07 05 01 03 00  
            Raw Descriptor (hex)    0020: 02 01 07 05 82 03 00 02  01 09 04 01 00 02 FF FF  
            Raw Descriptor (hex)    0030: FF 05 07 05 84 02 00 02  FF 07 05 03 02 00 02 FF  
            Unknown Descriptor   0040: 
        Number of Interfaces:   2
        Configuration Value:   1
        Attributes:   0x80 (bus-powered)
        MaxPower:   100 mA
        Interface #0 - HID ..............................................   "Atmel-ICE CMSIS-DAP"
            Alternate Setting   0
            Number of Endpoints   2
            Interface Class:   3   (HID)
            Interface Subclass;   0
            Interface Protocol:   0
            HID Descriptor   
                Descriptor Version Number:   0x0111
                Country Code:   0
                Descriptor Count:   1
                Descriptor 1   
                    Type:   0x22  (Report Descriptor)
                    Length (and contents):   35
                        Raw Descriptor (hex)    0000: 06 00 FF 09 01 A1 01 15  00 26 FF 00 75 08 96 00  
                        Raw Descriptor (hex)    0010: 02 09 01 81 02 96 00 02  09 01 91 02 95 04 09 01  
                        Raw Descriptor (hex)    0020: B1 02 C0 
                    Parsed Report Descriptor:   
                          Usage Page    (Vendor defined 0) 
                          Usage 1 (0x1)    
                              Collection (Application)    
                                Logical Minimum.........    (0)  
                                Logical Maximum.........    (255)  
                                Report Size.............    (8)  
                                Report Count............    (512)  
                                Usage 1 (0x1)    
                                Input...................   (Data, Variable, Absolute, No Wrap, Linear, Preferred State, No Null Position, Bitfield) 
                                Report Count............    (512)  
                                Usage 1 (0x1)    
                                Output..................   (Data, Variable, Absolute, No Wrap, Linear, Preferred State, No Null Position, Nonvolatile, Bitfield) 
                                Report Count............    (4)  
                                Usage 1 (0x1)    
                                Feature.................   (Data, Variable, Absolute, No Wrap, Linear, Preferred State, No Null Position, Nonvolatile, Bitfield) 
                              End Collection     
            Endpoint 0x01 - Interrupt Output   
                Address:   0x01  (OUT)
                Attributes:   0x03  (Interrupt)
                Max Packet Size:   0x0200  (512 x 1  transactions opportunities per microframe)
                Polling Interval:   1 (1 microframe (125 microsecs) )
            Endpoint 0x82 - Interrupt Input   
                Address:   0x82  (IN)
                Attributes:   0x03  (Interrupt)
                Max Packet Size:   0x0200  (512 x 1  transactions opportunities per microframe)
                Polling Interval:   1 (1 microframe (125 microsecs) )
        Interface #1 - Vendor-specific ..............................................   "Atmel-ICE Data Gateway"
            Alternate Setting   0
            Number of Endpoints   2
            Interface Class:   255   (Vendor-specific)
            Interface Subclass;   255   (Vendor-specific)
            Interface Protocol:   255
            Endpoint 0x84 - Bulk Input   
                Address:   0x84  (IN)
                Attributes:   0x02  (Bulk)
                Max Packet Size:   512
                Polling Interval:   255 ( At most 1 NAK every 255 microframe(s) )
            Endpoint 0x03 - Bulk Output   
                Address:   0x03  (OUT)
                Attributes:   0x02  (Bulk)
                Max Packet Size:   512
                Polling Interval:   255 ( At most 1 NAK every 255 microframe(s) )
    Device Qualifier Descriptor   
        Descriptor Version Number:   0x0200
        Device Class   239   (Miscellaneous)
        Device Subclass   2   (Common Class)
        Device Protocol   1   (Interface Association)
        Device MaxPacketSize:   64
        Number of Configurations:   1
        bReserved:   0
    Other Speed Configuration Descriptor   
        Length (and contents):   64
            Raw Descriptor (hex)    0000: 09 07 40 00 02 01 00 80  32 09 04 00 00 02 03 00  
            Raw Descriptor (hex)    0010: 00 04 09 21 11 01 00 01  22 23 00 07 05 01 03 40  
            Raw Descriptor (hex)    0020: 00 01 07 05 82 03 40 00  01 09 04 01 00 02 FF FF  
            Raw Descriptor (hex)    0030: FF 05 07 05 84 02 40 00  FF 07 05 03 02 40 00 FF  
            Unknown Descriptor   0040: 
        Number of Interfaces:   2
        Configuration Value:   1
        Attributes:   0x80 (bus-powered)
        MaxPower:   100 mA
        Interface #0 - HID ..............................................   "Atmel-ICE CMSIS-DAP"
            Alternate Setting   0
            Number of Endpoints   2
            Interface Class:   3   (HID)
            Interface Subclass;   0
            Interface Protocol:   0
            HID Descriptor   
                Descriptor Version Number:   0x0111
                Country Code:   0
                Descriptor Count:   1
                Descriptor 1   
                    Type:   0x22  (Report Descriptor)
                    Length (interface does not currently exist):   35
            Endpoint 0x01 - Interrupt Output   
                Address:   0x01  (OUT)
                Attributes:   0x03  (Interrupt)
                Max Packet Size:   64
                Polling Interval:   1 ms
            Endpoint 0x82 - Interrupt Input   
                Address:   0x82  (IN)
                Attributes:   0x03  (Interrupt)
                Max Packet Size:   64
                Polling Interval:   1 ms
        Interface #1 - Vendor-specific ..............................................   "Atmel-ICE Data Gateway"
            Alternate Setting   0
            Number of Endpoints   2
            Interface Class:   255   (Vendor-specific)
            Interface Subclass;   255   (Vendor-specific)
            Interface Protocol:   255
            Endpoint 0x84 - Bulk Input   
                Address:   0x84  (IN)
                Attributes:   0x02  (Bulk)
                Max Packet Size:   64
                Polling Interval:   255 ms
            Endpoint 0x03 - Bulk Output   
                Address:   0x03  (OUT)
                Attributes:   0x02  (Bulk)
                Max Packet Size:   64
                Polling Interval:   255 ms

We can see that the "HID Report Descriptor" allows a fixed payload of 512 bytes.

Report Count............ (512)

And endpoint 0x01/0x82 allows 512 bytes per communication.

Max Packet Size: 0x0200 (512 x 1 transactions opportunities per microframe)

On the other hand, the descriptor for Full-Speed ​​exists as "Other Speed ​​Configuration Descriptor".

Max Packet Size: 64

In this case, will HID communication be performed using 8 sets of 64 bytes? The "HID Report Descriptor" for Full-Speed ​​is not listed here.

You should be able to select/switch which one is active in the "Device Qualifier Descriptor". That means there should be an API somewhere to downgrade/upgrade between High-Speed ​​and Full-Speed, and you should be able to distinguish between them. The Windows API should be able to do it. But I don't know about HIDAPI or libusb.

This display is displayed by the usbdiagnose command on macos, but it can be used without privileges, so it's clear that there is an API that can be used with user privileges at the OS level. It's probably not built into HIDAPI for policy reasons. And libusb's descriptor-related API requires privileges, so we can see that there is a problem somewhere in the implementation.

mcuee commented 1 month ago

You should be able to select/switch which one is active in the "Device Qualifier Descriptor". That means there should be an API somewhere to downgrade/upgrade between High-Speed ​​and Full-Speed, and you should be able to distinguish between them. The Windows API should be able to do it. But I don't know about HIDAPI or libusb.

I do not think you can chage the speed using libusb or hidapi. And for Windows, I do not think it is possible to change the negotiated speed by the OS either.

@Youw Please share your thoughts as well. Thanks.

Youw commented 1 month ago

Please share your thoughts as well. Thanks.

I'm not aware if such method. Last time I needed to do something like that - I've sent a special command to my USB device and it rebooted/reinitialized at different speed. Although, I have no idea how - that was done by a different team, who made the FW for that device.

stefanrueger commented 1 month ago

@mcuee So best to close this issue again?

mcuee commented 1 month ago

@mcuee So best to close this issue again?

No, there is a bug here in avrdude abut packet size and we need to fix that.

Not able to detect Atmel ICE or Power Debugger when they are not connected to high speed USB is a minor issue which we can live with.

mcuee commented 1 month ago

This is still good to be fixed as hdiapi does not work under NetBSD/OpenBSD. Note: it is still troublesome for NetBSD/OpenBSD to work with libusb for HID device though.

BTW, there is an hidapi enhancement for NetBSD but I have not tested it myself. https://github.com/libusb/hidapi/tree/master/netbsd