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

Support for serial port "touch" functionality using libserialport #1507

Merged
merged 19 commits into from
Oct 15, 2023

Conversation

MCUdude
Copy link
Collaborator

@MCUdude MCUdude commented Oct 1, 2023

This draft PR makes it possible to upload to boards like the Arduino Leonardo and Arduino Nano Every without using an external tool to enter "upload mode".

This PR is a result of some test code I hacked together that turned out to work quite well. I'm open to suggestions on how this can be further improved.

Tested on an Arduino Pro Micro (running the Arduino Leonardo bootloader) and the Arduino Nano Every.
The Pro Micro has a different USB PID when in bootloader mode, while the Nano Every stays constant.

$ ./avrdude -cjtag2updi -patmega4809 -P /dev/cu.usbmodem14201 -r 1200
avrdude: touching serial port /dev/cu.usbmodem14201 at 1200 baud
avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9651 (probably m4809)

avrdude done.  Thank you.


$ ./avrdude -cavr109 -patmega32u4 -P/dev/cu.usbmodem14201 -b57600 -r1200
avrdude: touching serial port /dev/cu.usbmodem14201 at 1200 baud
avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9587 (probably m32u4)

avrdude done.  Thank you.

Resolves #1443

This makes it possible to upload to boards like the Arduino Leonardo and Arduino Nano Every wwithout using an external tool to enter bootloader mode
@MCUdude
Copy link
Collaborator Author

MCUdude commented Oct 1, 2023

It seems like the CI is complaining for some reason. It builds just file using both make and cmake on my computer...

@mcuee mcuee added the enhancement New feature or request label Oct 2, 2023
@mcuee
Copy link
Collaborator

mcuee commented Oct 2, 2023

@MCUdude

I think github action failed because libserial port is not installed. This was left not done in the previous PR.

For now, I think we can update github action script to install libserialport for Linux, macOS and MinGW, and we have to disable libserialport for MSVC for now.

@mcuee
Copy link
Collaborator

mcuee commented Oct 2, 2023

I can build under MSYS2 mingw64 since I have libserialport installed. But there is a warning.

MINGW64 /c/work/avr/avrdude_test/avrdude_hans
$ ./build.sh
-- Building for: Ninja
-- The C compiler identification is GNU 13.1.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/msys64/mingw64/bin/cc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Found Git: C:/msys64/usr/bin/git.exe (found version "2.40.1")
-- Found FLEX: C:/msys64/usr/bin/flex.exe (found version "2.6.4")
-- Found BISON: C:/msys64/usr/bin/bison.exe (found version "3.8.2")
-- Looking for libelf.h
-- Looking for libelf.h - found
-- Looking for libelf/libelf.h
-- Looking for libelf/libelf.h - found
-- Looking for usb.h
-- Looking for usb.h - not found
-- Looking for lusb0_usb.h
-- Looking for lusb0_usb.h - found
-- Looking for libusb.h
-- Looking for libusb.h - not found
-- Looking for libusb-1.0/libusb.h
-- Looking for libusb-1.0/libusb.h - found
-- Looking for hidapi/hidapi.h
-- Looking for hidapi/hidapi.h - found
-- Looking for ftdi_tcioflush
-- Looking for ftdi_tcioflush - found
-- Configuration summary:
-- ----------------------
-- DO HAVE    libelf
-- DO HAVE    libusb
-- DO HAVE    libusb_1_0
-- DO HAVE    libhidapi
-- DON'T HAVE libftdi
-- DO HAVE    libftdi1
-- DO HAVE    libreadline
-- DO HAVE    libserialport
-- DISABLED   doc
-- DISABLED   parport
-- DISABLED   linuxgpio
-- DISABLED   linuxspi
-- ----------------------
-- Configuring done (8.0s)
-- Generating done (0.0s)
-- Build files have been written to: C:/work/avr/avrdude_test/avrdude_hans/build_mingw64_nt-10.0-22621
[66/71] Building C object src/CMakeFiles/avrdude.dir/main.c.obj
C:/work/avr/avrdude_test/avrdude_hans/src/main.c: In function 'main':
C:/work/avr/avrdude_test/avrdude_hans/src/main.c:1239:5: warning: 'touch_baudrate' may be used uninitialized [-Wmaybe-uninitialized]
 1239 |   if(touch_baudrate)
      |     ^
C:/work/avr/avrdude_test/avrdude_hans/src/main.c:529:11: note: 'touch_baudrate' was declared here
  529 |   int     touch_baudrate; /* baudrate to use when "touching" a serial port */
      |           ^~~~~~~~~~~~~~
[71/71] Linking C executable src\avrdude.exe

Build succeeded.

Run

sudo cmake --build build_mingw64_nt-10.0-22621 --target install

to install.

@mcuee
Copy link
Collaborator

mcuee commented Oct 2, 2023

Build log under Visual Studio 2022 (no libserialport installed) -- building step will fail.

Configuration step:

1> CMake generation started for configuration: 'x64-Debug'.
1> Found and using vcpkg toolchain file (C:/src/vcpkg/vcpkg/scripts/buildsystems/vcpkg.cmake).
1> Command line: "C:\WINDOWS\system32\cmd.exe" /c "%SYSTEMROOT%\System32\chcp.com 65001 >NUL && "C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\2022\COMMUNITY\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\CMake\bin\cmake.exe"  -G "Ninja"  -DCMAKE_BUILD_TYPE:STRING="Debug" -DCMAKE_INSTALL_PREFIX:PATH="C:\work\avr\avrdude_test\avrdude_hans\out\install\x64-Debug" -DUSE_EXTERNAL_LIBS:BOOL="true" -DCMAKE_C_COMPILER:FILEPATH="C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.37.32822/bin/Hostx64/x64/cl.exe"   -DCMAKE_MAKE_PROGRAM="C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\2022\COMMUNITY\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\Ninja\ninja.exe" -DCMAKE_TOOLCHAIN_FILE="C:/src/vcpkg/vcpkg/scripts/buildsystems/vcpkg.cmake" "C:\work\avr\avrdude_test\avrdude_hans" 2>&1"
1> Working directory: C:\work\avr\avrdude_test\avrdude_hans\out\build\x64-Debug
1> [CMake] -- The C compiler identification is MSVC 19.37.32824.0
1> [CMake] -- Detecting C compiler ABI info
1> [CMake] -- Detecting C compiler ABI info - done
1> [CMake] -- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.37.32822/bin/Hostx64/x64/cl.exe - skipped
1> [CMake] -- Detecting C compile features
1> [CMake] -- Detecting C compile features - done
1> [CMake] -- Found Git: C:/Program Files/Git/cmd/git.exe (found version "2.39.1.windows.1") 
1> [CMake] -- Found FLEX: C:/ProgramData/chocolatey/bin/win_flex.exe (found version "2.6.3") 
1> [CMake] -- Found BISON: C:/ProgramData/chocolatey/bin/win_bison.exe (found version "2.7")
1> [CMake] -- Looking for hidapi/hidapi.h
1> [CMake] -- Looking for hidapi/hidapi.h - not found
1> [CMake] -- Looking for ftdi_tcioflush
1> [CMake] -- Looking for ftdi_tcioflush - not found
1> [CMake] -- Fetching external libraries, please wait...
1> [CMake] -- The CXX compiler identification is MSVC 19.37.32824.0
1> [CMake] -- Detecting CXX compiler ABI info
1> [CMake] -- Detecting CXX compiler ABI info - done
1> [CMake] -- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.37.32822/bin/Hostx64/x64/cl.exe - skipped
1> [CMake] -- Detecting CXX compile features
1> [CMake] -- Detecting CXX compile features - done
1> [CMake] -- Using external library 'libelf'
1> [CMake] -- Using external library 'libusb'
1> [CMake] -- Using external library 'libhidapi'
1> [CMake] -- Using external library 'libftdi'
1> [CMake] -- Configuration summary:
1> [CMake] -- ----------------------
1> [CMake] -- DO HAVE    libelf
1> [CMake] -- DO HAVE    libusb
1> [CMake] -- DON'T HAVE libusb_1_0
1> [CMake] -- DO HAVE    libhidapi
1> [CMake] -- DO HAVE    libftdi (but prefer to use libftdi1)
1> [CMake] -- DO HAVE    libftdi1
1> [CMake] -- DO HAVE    libreadline
1> [CMake] -- DON'T HAVE libserialport
1> [CMake] -- DISABLED   doc
1> [CMake] -- DISABLED   parport
1> [CMake] -- DISABLED   linuxgpio
1> [CMake] -- DISABLED   linuxspi
1> [CMake] -- ----------------------
1> [CMake] -- Configuring done (19.0s)
1> [CMake] -- Generating done (0.0s)
1> [CMake] -- Build files have been written to: C:/work/avr/avrdude_test/avrdude_hans/out/build/x64-Debug
1> Extracted CMake variables.
1> Extracted source files and headers.
1> Extracted code model.
1> Extracted toolchain configurations.
1> Extracted includes paths.
1> CMake generation finished.

Building step:

>------ Build All started: Project: avrdude_hans, Configuration: x64-Debug ------
  [1/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\32.newehdr.c.obj
  [2/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\32.newphdr.c.obj
  [3/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\32.getphdr.c.obj
  [4/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\begin.c.obj
  [5/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\64.xlatetof.c.obj
  [6/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\32.xlatetof.c.obj
  [7/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\assert.c.obj
  [8/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\32.getehdr.c.obj
  [9/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\32.getshdr.c.obj
  [10/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\32.fsize.c.obj
  [11/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\cook.c.obj
  [12/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\errmsg.c.obj
  [13/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\cntl.c.obj
  [14/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\errno.c.obj
  [15/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\end.c.obj
  [16/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\data.c.obj
  [17/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\checksum.c.obj
  [18/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\fill.c.obj
  [19/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\gelfehdr.c.obj
  [20/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\flag.c.obj
  [21/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\gelfphdr.c.obj
  [22/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\gelfshdr.c.obj
  [23/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\gelftrans.c.obj
  [24/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\getaroff.c.obj
  [25/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\getarhdr.c.obj
  [26/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\getbase.c.obj
  [27/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\getdata.c.obj
  [28/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\getarsym.c.obj
  [29/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\getident.c.obj
  [30/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\hash.c.obj
  [31/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\getscn.c.obj
  [32/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\input.c.obj
  [33/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\memset.c.obj
  [34/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\kind.c.obj
  [35/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\newdata.c.obj
  [36/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\ndxscn.c.obj
  [37/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\newscn.c.obj
  [38/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\next.c.obj
  [39/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\nlist.c.obj
  [40/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\opt.delscn.c.obj
  [41/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\nextscn.c.obj
  [42/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\rand.c.obj
  [43/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\rawdata.c.obj
  [44/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\rawfile.c.obj
  [45/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\strptr.c.obj
  [46/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\swap64.c.obj
  [47/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\update.c.obj
  [48/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\verdef_32_tof.c.obj
  [49/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\verdef_64_tom.c.obj
  [50/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\verdef_32_tom.c.obj
  [51/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\verdef_64_tof.c.obj
  [52/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\version.c.obj
  [53/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\x.elfext.c.obj
  [54/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\x.movscn.c.obj
  [55/141] Building C object _deps\libelf-build\CMakeFiles\libelf.dir\src\x.remscn.c.obj
  [56/141] Building C object _deps\libusb-build\CMakeFiles\libusb.dir\src\descriptors.c.obj
  [57/141] Building C object _deps\libusb-build\CMakeFiles\libusb.dir\src\usb.c.obj
  [58/141] Linking C static library _deps\libelf-build\libelf.lib
  [59/141] Generating avrdude.conf
  [60/141] Building C object _deps\libusb-build\CMakeFiles\libusb.dir\src\error.c.obj
  [61/141] Building C object _deps\libhidapi-build\CMakeFiles\libhidapi.dir\src\hid.c.obj
  [62/141] Building C object _deps\libusb-build\CMakeFiles\libusb.dir\src\registry.c.obj
  [63/141] Linking C static library _deps\libhidapi-build\libhidapi.lib
  [64/141] Building CXX object _deps\libftdi-build\CMakeFiles\libftdi.dir\src\ftdi.cpp.obj
  [65/141] Linking CXX static library _deps\libftdi-build\libftdi.lib
  [66/141] Building CXX object _deps\libusb-build\CMakeFiles\libusb.dir\src\windows.cpp.obj
  [67/141] Linking CXX static library _deps\libusb-build\libusb.lib
  [68/141] [FLEX][Parser] Building scanner with win_flex 2.6.3
  [69/141] [BISON][Parser] Building parser with bison 2.7
  [70/141] Building C object src\CMakeFiles\libavrdude.dir\avrftdi_tpi.c.obj
  [71/141] Building C object src\CMakeFiles\libavrdude.dir\avr910.c.obj
  [72/141] Building C object src\CMakeFiles\libavrdude.dir\avrcache.c.obj
  [73/141] Building C object src\CMakeFiles\libavrdude.dir\arduino.c.obj
  [74/141] Building C object src\CMakeFiles\libavrdude.dir\avrftdi.c.obj
  [75/141] Building C object src\CMakeFiles\libavrdude.dir\avrpart.c.obj
  [76/141] Building C object src\CMakeFiles\libavrdude.dir\avr.c.obj
  [77/141] Building C object src\CMakeFiles\libavrdude.dir\buspirate.c.obj
  [78/141] Building C object src\CMakeFiles\libavrdude.dir\avrintel.c.obj
  [79/141] Building C object src\CMakeFiles\libavrdude.dir\crc16.c.obj
  [80/141] Building C object src\CMakeFiles\libavrdude.dir\ch341a.c.obj
  [81/141] Building C object src\CMakeFiles\libavrdude.dir\config.c.obj
  [82/141] Building C object src\CMakeFiles\libavrdude.dir\butterfly.c.obj
  [83/141] Building C object src\CMakeFiles\libavrdude.dir\fileio.c.obj
  [84/141] Building C object src\CMakeFiles\libavrdude.dir\dfu.c.obj
  [85/141] Building C object src\CMakeFiles\libavrdude.dir\dryrun.c.obj
  [86/141] Building C object src\CMakeFiles\libavrdude.dir\flip1.c.obj
  [87/141] Building C object src\CMakeFiles\libavrdude.dir\flip2.c.obj
  [88/141] Building C object src\CMakeFiles\libavrdude.dir\ft245r.c.obj
  [89/141] Building C object src\CMakeFiles\libavrdude.dir\jtagmkI.c.obj
  [90/141] Building C object src\CMakeFiles\libavrdude.dir\bitbang.c.obj
  [91/141] Building C object src\CMakeFiles\libavrdude.dir\linuxgpio.c.obj
  [92/141] Building C object src\CMakeFiles\libavrdude.dir\linuxspi.c.obj
  [93/141] Building C object src\CMakeFiles\libavrdude.dir\jtagmkII.c.obj
  [94/141] Building C object src\CMakeFiles\libavrdude.dir\lists.c.obj
  [95/141] Building C object src\CMakeFiles\libavrdude.dir\jtag3.c.obj
  [96/141] Building C object src\CMakeFiles\libavrdude.dir\ppi.c.obj
  [97/141] Building C object src\CMakeFiles\libavrdude.dir\serbb_posix.c.obj
  [98/141] Building C object src\CMakeFiles\libavrdude.dir\confwin.c.obj
  [99/141] Building C object src\CMakeFiles\libavrdude.dir\par.c.obj
  [100/141] Building C object src\CMakeFiles\libavrdude.dir\micronucleus.c.obj
  [101/141] Building C object src\CMakeFiles\libavrdude.dir\pgm.c.obj
  [102/141] Building C object src\CMakeFiles\libavrdude.dir\pindefs.c.obj
  [103/141] Building C object src\CMakeFiles\libavrdude.dir\ppiwin.c.obj
  [104/141] Building C object src\CMakeFiles\libavrdude.dir\ser_posix.c.obj
  [105/141] Building C object src\CMakeFiles\libavrdude.dir\pgm_type.c.obj
  [106/141] Building C object src\CMakeFiles\libavrdude.dir\ser_avrdoper.c.obj
  [107/141] Building C object src\CMakeFiles\libavrdude.dir\serialadapter.c.obj
  [108/141] Building C object src\CMakeFiles\libavrdude.dir\serialupdi.c.obj
  [109/141] Building C object src\CMakeFiles\libavrdude.dir\stk500.c.obj
  [110/141] Building C object src\CMakeFiles\libavrdude.dir\stk500v2.c.obj
  [111/141] Building C object src\CMakeFiles\libavrdude.dir\stk500generic.c.obj
  [112/141] Building C object src\CMakeFiles\libavrdude.dir\strutil.c.obj
  [113/141] Building C object src\CMakeFiles\libavrdude.dir\updi_link.c.obj
  [114/141] Building C object src\CMakeFiles\libavrdude.dir\updi_nvm.c.obj
  [115/141] Building C object src\CMakeFiles\libavrdude.dir\teensy.c.obj
  [116/141] Building C object src\CMakeFiles\libavrdude.dir\updi_readwrite.c.obj
  [117/141] Building C object src\CMakeFiles\libavrdude.dir\updi_state.c.obj
  [118/141] Building C object src\CMakeFiles\libavrdude.dir\pickit2.c.obj
  [119/141] Building C object src\CMakeFiles\libavrdude.dir\serbb_win32.c.obj
  [120/141] Building C object src\CMakeFiles\libavrdude.dir\urclock.c.obj
  [121/141] Building C object src\CMakeFiles\libavrdude.dir\ser_win32.c.obj
  [122/141] Building C object src\CMakeFiles\libavrdude.dir\usb_libusb.c.obj
  [123/141] Building C object src\CMakeFiles\libavrdude.dir\usbasp.c.obj
  [124/141] Building C object src\CMakeFiles\libavrdude.dir\usb_hidapi.c.obj
  [125/141] Building C object src\CMakeFiles\libavrdude.dir\update.c.obj
  [126/141] Building C object src\CMakeFiles\libavrdude.dir\wiring.c.obj
  [127/141] Building C object src\CMakeFiles\libavrdude.dir\usbtiny.c.obj
  [128/141] Building C object src\CMakeFiles\libavrdude.dir\xbee.c.obj
  [129/141] Building C object src\CMakeFiles\libavrdude.dir\__\config_gram.c.obj
  [130/141] Building C object src\CMakeFiles\libavrdude.dir\__\lexer.c.obj
  [131/141] Building C object src\CMakeFiles\avrdude.dir\main.c.obj
  [132/141] Building C object src\CMakeFiles\avrdude.dir\developer_opts.c.obj
  [133/141] Building RC object src\CMakeFiles\avrdude.dir\windows.rc.res
  [134/141] Building C object src\CMakeFiles\libavrdude.dir\msvc\gettimeofday.c.obj
  [135/141] Building C object src\CMakeFiles\libavrdude.dir\msvc\getopt.c.obj
  [136/141] Building C object src\CMakeFiles\libavrdude.dir\term.c.obj
  [137/141] Building CXX object src\CMakeFiles\libavrdude.dir\msvc\usleep.cpp.obj
  [138/141] Building C object src\CMakeFiles\avrdude.dir\whereami.c.obj
  [139/141] Building CXX object src\CMakeFiles\libavrdude.dir\msvc\readline.cpp.obj
  [140/141] Linking CXX static library src\libavrdude.lib
  [141/141] Linking CXX executable src\avrdude.exe
  FAILED: src/avrdude.exe 
  cmd.exe /C "cd . && "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -E vs_link_exe --intdir=src\CMakeFiles\avrdude.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100226~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100226~1.0\x64\mt.exe --manifests  -- C:\PROGRA~1\MIB055~1\2022\COMMUN~1\VC\Tools\MSVC\1437~1.328\bin\Hostx64\x64\link.exe /nologo src\CMakeFiles\avrdude.dir\main.c.obj src\CMakeFiles\avrdude.dir\developer_opts.c.obj src\CMakeFiles\avrdude.dir\whereami.c.obj src\CMakeFiles\avrdude.dir\windows.rc.res  /out:src\avrdude.exe /implib:src\avrdude.lib /pdb:src\avrdude.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console  src\libavrdude.lib  _deps\libelf-build\libelf.lib  _deps\libusb-build\libusb.lib  _deps\libhidapi-build\libhidapi.lib  _deps\libftdi-build\libftdi.lib  C:\src\vcpkg\vcpkg\installed\x64-windows\debug\lib\ftdi1.lib  setupapi.lib  hid.lib  ws2_32.lib  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cmd.exe /C "cd /D C:\work\avr\avrdude_test\avrdude_hans\out\build\x64-Debug\src && "C:\Program Files\PowerShell\7\pwsh.exe" -noprofile -executionpolicy Bypass -file C:/src/vcpkg/vcpkg/scripts/buildsystems/msbuild/applocal.ps1 -targetBinary C:/work/avr/avrdude_test/avrdude_hans/out/build/x64-Debug/src/avrdude.exe -installedDir C:/src/vcpkg/vcpkg/installed/x64-windows/debug/bin -OutVariable out""
  LINK Pass 1: command "C:\PROGRA~1\MIB055~1\2022\COMMUN~1\VC\Tools\MSVC\1437~1.328\bin\Hostx64\x64\link.exe /nologo src\CMakeFiles\avrdude.dir\main.c.obj src\CMakeFiles\avrdude.dir\developer_opts.c.obj src\CMakeFiles\avrdude.dir\whereami.c.obj src\CMakeFiles\avrdude.dir\windows.rc.res /out:src\avrdude.exe /implib:src\avrdude.lib /pdb:src\avrdude.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console src\libavrdude.lib _deps\libelf-build\libelf.lib _deps\libusb-build\libusb.lib _deps\libhidapi-build\libhidapi.lib _deps\libftdi-build\libftdi.lib C:\src\vcpkg\vcpkg\installed\x64-windows\debug\lib\ftdi1.lib setupapi.lib hid.lib ws2_32.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:src\CMakeFiles\avrdude.dir/intermediate.manifest src\CMakeFiles\avrdude.dir/manifest.res" failed (exit code 1120) with the following output:
     Creating library src\avrdude.lib and object src\avrdude.exp
  
C:\work\avr\avrdude_test\avrdude_hans\out\build\x64-Debug\main.c.obj : error LNK2019: unresolved external symbol touch_serialport referenced in function main
  
C:\work\avr\avrdude_test\avrdude_hans\out\build\x64-Debug\src\avrdude.exe : fatal error LNK1120: 1 unresolved externals
  
  ninja: build stopped: subcommand failed.

Build All failed.

@mcuee
Copy link
Collaborator

mcuee commented Oct 2, 2023

@MCUdude

Probably one simple fix is to guard touch_serialport with ifdefs since you wanted to build avrdude without libserialport by default in your answer to my comment about github action changes in PR #1498.

@MCUdude
Copy link
Collaborator Author

MCUdude commented Oct 2, 2023

Thanks for the heads up @mcuee!
I've pushed a few commits that resolve the issues you pointed out.
Can you try it with some hardware to see if the functionality works as expected for you?

And since @kristofmulier has requested this functionality earlier, maybe he wants to try it as well?
All you need to do is install libserialport before building Avrdude from source.

@mcuee
Copy link
Collaborator

mcuee commented Oct 2, 2023

Thanks for the heads up @mcuee! I've pushed a few commits that resolve the issues you pointed out. Can you try it with some hardware to see if the functionality works as expected for you?

Great. This seems to work fine under Windows (COM port changes from COM8 to COM9 during the test.

C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1507 -C .\avrdude_pr1507.conf -p m32u4 -c avr109 -P usb:2341:8036 -r 1200
avrdude_pr1507: touching serial port COM8 at 1200 baud
avrdude_pr1507: AVR device initialized and ready to accept instructions
avrdude_pr1507: device signature = 0x1e9587 (probably m32u4)

avrdude_pr1507 done.  Thank you.

C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1507 -C .\avrdude_pr1507.conf -p m32u4 -c avr109 -r 1200 -P usb:2341:8036
avrdude_pr1507: touching serial port COM8 at 1200 baud
avrdude_pr1507: AVR device initialized and ready to accept instructions
avrdude_pr1507: device signature = 0x1e9587 (probably m32u4)

avrdude_pr1507 done.  Thank you.

C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1507 -C .\avrdude_pr1507.conf -p m32u4 -c avr109 -r 1200 -P usb:2341:8036 -U .\hex\blink.leonardo.ino.hex
avrdude_pr1507: touching serial port COM8 at 1200 baud
avrdude_pr1507: AVR device initialized and ready to accept instructions
avrdude_pr1507: device signature = 0x1e9587 (probably m32u4)
avrdude_pr1507: Note: flash memory has been specified, an erase cycle will be performed.
                To disable this feature, specify the -D option.
avrdude_pr1507: erasing chip

avrdude_pr1507: processing -U flash:w:.\hex\blink.leonardo.ino.hex:i
avrdude_pr1507: reading input file .\hex\blink.leonardo.ino.hex for flash
                with 3956 bytes in 1 section within [0, 0xf73]
                using 31 pages and 12 pad bytes
avrdude_pr1507: writing 3956 bytes flash ...
Writing | ################################################## | 100% 0.50 s
avrdude_pr1507: 3956 bytes of flash written
avrdude_pr1507: verifying flash memory against .\hex\blink.leonardo.ino.hex
Reading | ################################################## | 100% 0.16 s
avrdude_pr1507: 3956 bytes of flash verified

avrdude_pr1507 done.  Thank you.

Arduino 1.89 with avrdude 6.3 output for reference as well.

Forcing reset using 1200bps open/close on port COM8
PORTS {COM3, COM8, } / {COM3, } => {}
PORTS {COM3, } / {COM3, COM9, } => {COM9, }
Found upload port: COM9
C:\Users\xfchen\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/bin/avrdude -CC:\Users\xfchen\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf -v -patmega32u4 -cavr109 -PCOM9 -b57600 -D -Uflash:w:C:\Users\xfchen\AppData\Local\Temp\arduino_build_631734/Blink.ino.hex:i 

avrdude: Version 6.3-20190619
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "C:\Users\xfchen\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf"

         Using Port                    : COM9
         Using Programmer              : avr109
         Overriding Baud Rate          : 57600
         AVR Part                      : ATmega32U4
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PA0
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

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

         Programmer Type : butterfly
         Description     : Atmel AppNote AVR109 Boot Loader

Connecting to programmer: .
Found programmer: Id = "CATERIN"; type = S
    Software Version = 1.0; No Hardware Version given.
Programmer supports auto addr increment.
Programmer supports buffered memory access with buffersize=128 bytes.

Programmer supports the following devices:
    Device code: 0x44

avrdude: devcode selected: 0x44
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e9587 (probably m32u4)
avrdude: reading input file "C:\Users\xfchen\AppData\Local\Temp\arduino_build_631734/Blink.ino.hex"
avrdude: writing flash (3956 bytes):

Writing | ################################################## | 100% 0.30s

avrdude: 3956 bytes of flash written
avrdude: verifying flash memory against C:\Users\xfchen\AppData\Local\Temp\arduino_build_631734/Blink.ino.hex:
avrdude: load data flash data from input file C:\Users\xfchen\AppData\Local\Temp\arduino_build_631734/Blink.ino.hex:
avrdude: input file C:\Users\xfchen\AppData\Local\Temp\arduino_build_631734/Blink.ino.hex contains 3956 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.04s

avrdude: verifying ...
avrdude: 3956 bytes of flash verified

avrdude done.  Thank you.

@MCUdude
Copy link
Collaborator Author

MCUdude commented Oct 2, 2023

Great. This seems to work fine under Windows (COM port changes from COM8 to COM9 during the test.

Excellent test! Confirming that this (worst-case scenario) works fine on Windows makes me think that this might be a reliable solution to the problem.

Forcing reset using 1200bps open/close on port COM8
PORTS {COM3, COM8, } / {COM3, } => {}
PORTS {COM3, } / {COM3, COM9, } => {COM9, }
Found upload port: COM9

Do you know where I can find the source code for the program Arduino uses to deal with 1200bps touch? Would be interesting to see how they do it. I've currently only looked at the PlatformIO implementation.

EDIT: Found it: SerialUploader.java. I'll go through it to see if there are any quirks I need to adopt to make sure the functionality in this PR works reliably on all platforms.

@MCUdude
Copy link
Collaborator Author

MCUdude commented Oct 2, 2023

@mcuee @stefanrueger I've been thinking about removing the user-specifiable baud rate and only supporting -r as a fixed "1200 baud touch" option. Yes, that would remove the ability to provide a user-specified baud rate, but on the other side, 1200 baud is the baud rate for entering programming mode across a wide variety of boards and architectures, even ARM-based Arduino boards.

At the moment, neither Arduino IDE nor PlatformIO supports other baud rates than 1200 for doing a "port touch".

@kristofmulier
Copy link
Contributor

Great! It's awesome to see that you guys are integrating this functionality in the official AVRDUDE. Any idea when it will be in the new release?

AFAIK, all boards/bootloaders that are using this approach to enter programming mode are using 1200 baud 8N1 anyways
@MCUdude
Copy link
Collaborator Author

MCUdude commented Oct 2, 2023

I've removed the user-specifiable touch baud rate in the last commit, as pretty much every application would be using -r 1200 anyway. If some of you disagree, I can always roll back to the previous commit.

Great! It's awesome to see that you guys are integrating this functionality in the official AVRDUDE. Any idea when it will be in the new release?

Avrdude 7.2 is quite new, so I'd say at least 4 or 5 months before a new version gets released.

@MCUdude MCUdude marked this pull request as ready for review October 2, 2023 21:43
@mcuee
Copy link
Collaborator

mcuee commented Oct 3, 2023

I've removed the user-specifiable touch baud rate in the last commit, as pretty much every application would be using -r 1200 anyway. If some of you disagree, I can always roll back to the previous commit.

I agree.

The latest commit is still good under Windows.

C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1507v1 -C .\avrdude_pr1507v1.conf -p m32u4 -c avr109 -r -P usb:2341:8036 -U .\hex\blink.leonardo.ino.hex
avrdude_pr1507v1: touching serial port COM8 at 1200 baud
avrdude_pr1507v1: AVR device initialized and ready to accept instructions
avrdude_pr1507v1: device signature = 0x1e9587 (probably m32u4)
avrdude_pr1507v1: Note: flash memory has been specified, an erase cycle will be performed.
                  To disable this feature, specify the -D option.
avrdude_pr1507v1: erasing chip

avrdude_pr1507v1: processing -U flash:w:.\hex\blink.leonardo.ino.hex:i
avrdude_pr1507v1: reading input file .\hex\blink.leonardo.ino.hex for flash
                  with 3956 bytes in 1 section within [0, 0xf73]
                  using 31 pages and 12 pad bytes
avrdude_pr1507v1: writing 3956 bytes flash ...
Writing | ################################################## | 100% 0.50 s
avrdude_pr1507v1: 3956 bytes of flash written
avrdude_pr1507v1: verifying flash memory against .\hex\blink.leonardo.ino.hex
Reading | ################################################## | 100% 0.16 s
avrdude_pr1507v1: 3956 bytes of flash verified

avrdude_pr1507v1 done.  Thank you.

@mcuee
Copy link
Collaborator

mcuee commented Oct 3, 2023

Great! It's awesome to see that you guys are integrating this functionality in the official AVRDUDE. Any idea when it will be in the new release?

As mentioned by Hans, we will not have avrdude 7.3 release so soon. You can always give this PR and git main a try.

@mcuee
Copy link
Collaborator

mcuee commented Oct 3, 2023

Somehow it does not work under Linux (Ubuntu 20.04).

mcuee@UbuntuSwift3 ~/build/avr/avrdude_bin $ pkg-config --version libserialport
0.29.1
mcuee@UbuntuSwift3 ~/build/avr/avrdude_bin $ pkg-config --libs libserialport
-lserialport

(mypy39venv) mcuee@UbuntuSwift3 ~/build/avr/avrdude_bin $ ./avrdude_pr1507v1 -C ./avrdude_pr1507v1.conf  -patmega32u4 -cavr109 -P/dev/ttyACM0 -b57600 -r
avrdude_pr1507v1: touching serial port /dev/ttyACM0 at 1200 baud
avrdude_pr1507v1 error: programmer is not responding
avrdude_pr1507v1 error: programmer is not responding
avrdude_pr1507v1 error: programmer is not responding
avrdude_pr1507v1 error: programmer is not responding
avrdude_pr1507v1 error: programmer is not responding
avrdude_pr1507v1 error: programmer is not responding
avrdude_pr1507v1 error: programmer is not responding
avrdude_pr1507v1 error: programmer is not responding
avrdude_pr1507v1 error: initialization failed, rc=-1
                 - double check the connections and try again
                 - use -B to set lower the bit clock frequency, e.g. -B 125kHz
                 - use -F to override this check
avrdude_pr1507v1 error: programmer is not responding
avrdude_pr1507v1 error: programmer did not respond to command: leave prog mode
avrdude_pr1507v1 error: programmer is not responding
avrdude_pr1507v1 error: programmer did not respond to command: exit bootloader
^C

Arduino 1.89 with avrdude 6.3 is okay.

/home/mcuee/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17/bin/avrdude -C/home/mcuee/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17/etc/avrdude.conf -v -patmega32u4 -cavr109 -P/dev/ttyACM0 -b57600 -D -Uflash:w:/tmp/arduino_build_818731/Blink.ino.hex:i 

avrdude: Version 6.3-20190619
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "/home/mcuee/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17/etc/avrdude.conf"
         User configuration file is "/home/mcuee/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : /dev/ttyACM0
         Using Programmer              : avr109
         Overriding Baud Rate          : 57600
         AVR Part                      : ATmega32U4
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PA0
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

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

         Programmer Type : butterfly
         Description     : Atmel AppNote AVR109 Boot Loader

Connecting to programmer: .
Found programmer: Id = "CATERIN"; type = S
    Software Version = 1.0; No Hardware Version given.
Programmer supports auto addr increment.
Programmer supports buffered memory access with buffersize=128 bytes.

Programmer supports the following devices:
    Device code: 0x44

avrdude: devcode selected: 0x44
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e9587 (probably m32u4)
avrdude: reading input file "/tmp/arduino_build_818731/Blink.ino.hex"
avrdude: writing flash (3956 bytes):

Writing | ################################################## | 100% 0.32s

avrdude: 3956 bytes of flash written
avrdude: verifying flash memory against /tmp/arduino_build_818731/Blink.ino.hex:
avrdude: load data flash data from input file /tmp/arduino_build_818731/Blink.ino.hex:
avrdude: input file /tmp/arduino_build_818731/Blink.ino.hex contains 3956 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.04s

avrdude: verifying ...
avrdude: 3956 bytes of flash verified

avrdude done.  Thank you.

@mcuee
Copy link
Collaborator

mcuee commented Oct 3, 2023

Maybe a bit of delay is necessary under Linux.

(mypy39venv) mcuee@UbuntuSwift3 ~/build/avr/avrdude_bin $ python3 1200bps.py /dev/ttyACM0 && sleep 1 && ./avrdude_git  -patmega32u4 -cavr109 -P/dev/ttyACM0
avrdude_git: AVR device initialized and ready to accept instructions
avrdude_git: device signature = 0x1e9587 (probably m32u4)

avrdude_git done.  Thank you.

(mypy39venv) mcuee@UbuntuSwift3 ~/build/avr/avrdude_bin $ python3 1200bps.py /dev/ttyACM0 && sleep 1 && ./avrdude_pr1507v1 -C ./avrdude_pr1507v1.conf  -patmega32u4 -cavr109 -P/dev/ttyACM0
avrdude_pr1507v1: AVR device initialized and ready to accept instructions
avrdude_pr1507v1: device signature = 0x1e9587 (probably m32u4)

avrdude_pr1507v1 done.  Thank you.

(mypy39venv) mcuee@UbuntuSwift3 ~/build/avr/avrdude_bin $ cat 1200bps.py 
#! /usr/bin/env python3

import sys
import serial

com = serial.Serial(sys.argv[1], 1200)
com.dtr=True
com.dtr=False
com.close()

# python ./reset.py "/dev/ttyACM0"

@MCUdude
Copy link
Collaborator Author

MCUdude commented Oct 3, 2023

Maybe a bit of delay is necessary under Linux.

That's weird. If you look at the source code, you can see that there's already a 1-second sleep (usleep(1000000)) in there before trying to figure out if the port number did change or not. Can you try changing usleep(500000) to usleep(250000) to see if it makes a difference? It may be that the bootloader actually times out before trying to connect.

int touch_serialport(char **portp, int baudrate) {
  int n1, n2;
  SERPORT *sp1, *sp2, **diff;
  sp1 = get_libserialport_data(&n1);
  if(!sp1 || n1 <= 0)
    return -1;

  pmsg_info("touching serial port %s at %d baud\n", *portp, baudrate);
  struct sp_port *prt;
  struct sp_port_config *cfg;
  sp_get_port_by_name(*portp, &prt);
  sp_open(prt, SP_MODE_READ);
  sp_set_baudrate(prt, baudrate);
  sp_set_bits(prt, 8);
  sp_set_parity(prt, SP_PARITY_NONE);
  sp_set_stopbits(prt, 1);
  sp_set_flowcontrol(prt, SP_FLOWCONTROL_NONE);
  sp_new_config(&cfg);
  sp_get_config(prt, cfg);
  sp_set_config_dtr(cfg, SP_DTR_OFF);
  sp_close(prt);
  sp_free_port(prt);
  usleep(1000000);

  for(int i = 0; i < 5; i++) {
    sp2 = get_libserialport_data(&n2);
    diff = sa_spa_not_spb(sp2, n2, sp1, n1);
    if(*diff && (*diff)->port) {
      pmsg_notice("new port %s discovered\n", (*diff)->port);
      if(portp) {
        if(*portp)
          free(*portp);
        *portp = cfg_strdup(__func__, (*diff)->port);
      }
      free(diff);
      break;
    }
    free(diff);
    usleep(500000);
  }

  return 0;
}

EDIT: if we should do it exactly like Arduino IDE does it, then the first sleep should be reduced from 1s to 400ms, and the second sleep (inside the for loop) from 500 to 250ms. Can you try this @mcuee?

@mcuee
Copy link
Collaborator

mcuee commented Oct 3, 2023

Still it does not work. Strange.

mcuee@UbuntuSwift3 ~/build/avr/avrdude_hans (baud-touch)$ git diff
diff --git a/src/serialadapter.c b/src/serialadapter.c
index 7c2f49ef..70043a65 100644
--- a/src/serialadapter.c
+++ b/src/serialadapter.c
@@ -325,7 +325,7 @@ int touch_serialport(char **portp, int baudrate) {
   sp_set_config_dtr(cfg, SP_DTR_OFF);
   sp_close(prt);
        sp_free_port(prt);
-  usleep(1000000);
+  usleep(400000);
 
   for(int i = 0; i < 5; i++) {
     sp2 = get_libserialport_data(&n2);
@@ -341,7 +341,7 @@ int touch_serialport(char **portp, int baudrate) {
       break;
     }
     free(diff);
-    usleep(500000);
+    usleep(250000);
   }
 
   return 0;

mcuee@UbuntuSwift3 ~/build/avr/avrdude_bin $ ./avrdude_pr1507v1mod -C ./avrdude_pr1507v1mod.conf  -patmega32u4 -cavr109 -P/dev/ttyACM0 -r
avrdude_pr1507v1mod: touching serial port /dev/ttyACM0 at 1200 baud
avrdude_pr1507v1mod error: programmer is not responding
avrdude_pr1507v1mod error: programmer is not responding
avrdude_pr1507v1mod error: programmer is not responding
avrdude_pr1507v1mod error: programmer is not responding
avrdude_pr1507v1mod error: programmer is not responding
avrdude_pr1507v1mod error: programmer is not responding
avrdude_pr1507v1mod error: programmer is not responding
avrdude_pr1507v1mod error: programmer is not responding
avrdude_pr1507v1mod error: initialization failed, rc=-1
                    - double check the connections and try again
                    - use -B to set lower the bit clock frequency, e.g. -B 125kHz
                    - use -F to override this check
avrdude_pr1507v1mod error: programmer is not responding
avrdude_pr1507v1mod error: programmer did not respond to command: leave prog mode
avrdude_pr1507v1mod error: programmer is not responding
avrdude_pr1507v1mod error: programmer did not respond to command: exit bootloader
^C

@mcuee
Copy link
Collaborator

mcuee commented Oct 3, 2023

I played a bit with other delay combinations and they do not work either.

I will try another Linux machine tomorrow to see if that makes a difference.

Or maybe @stefanrueger can take a look at the codes to see if there is something to take note.

@stefanrueger
Copy link
Collaborator

stefanrueger commented Oct 3, 2023

I had a quick look; well-programmed (but will do a proper review later).

Re timings. I realise this is modelled after the Arduino IDE, but we should figure out cross-platform timings ourselves.

second sleep (inside the for loop)

The loop assumes that the port changes by touching with 1200 baud. But does the port change in all OSes and all circs? If it doesn't the we attract 1000 ms + 5 · 500 ms sleep, 3.5 s. Maybe try running the loop only once (but not more twice) and tweak both sleep times so that they work across all platforms. Or have platform dependent sleeps. How long does it take for the port to spring up? I would have naively thought if a new port is not there by 500 ms then it never will.

@mcuee
Copy link
Collaborator

mcuee commented Oct 7, 2023

This works fine under Ubuntu Linux 20.04 x86_64 (my Windows11/Ubuntu 20.04 dual boot Acer Swift 3 year 2021 laptop). The good thing is that consecutive runs of avrdude (keep running the same command) are also fine.

mcuee@UbuntuSwift3 ~/build/avr/avrdude_bin $ ./avrdude_pr1507v5 -C ./avrdude_pr1507v5.conf  -patmega32u4 -cavr109 -P/dev/ttyACM0 -r -U flash:r:lenonardo_readback.hex
avrdude_pr1507v5: touching serial port /dev/ttyACM0 at 1200 baud
avrdude_pr1507v5: waiting for new port... 650 ms: using new port /dev/ttyACM0
avrdude_pr1507v5: AVR device initialized and ready to accept instructions
avrdude_pr1507v5: device signature = 0x1e9587 (probably m32u4)

avrdude_pr1507v5: processing -U flash:r:lenonardo_readback.hex:r
avrdude_pr1507v5: reading flash memory ...
Reading | ################################################## | 100% 0.25 s 
avrdude_pr1507v5: writing output file lenonardo_readback.hex

avrdude_pr1507v5 done.  Thank you.

@mcuee
Copy link
Collaborator

mcuee commented Oct 7, 2023

It is also find on Windows 11 on the same Acer laptop.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1507v5 -C .\avrdude_pr1507v5.conf -c avr109 -P COM47 -p m32u4 -r -U
 flash:r:leonardo_readback.hex:i
avrdude_pr1507v5: touching serial port COM47 at 1200 baud
avrdude_pr1507v5: waiting for new port... 550 ms: using new port COM40
avrdude_pr1507v5: AVR device initialized and ready to accept instructions
avrdude_pr1507v5: device signature = 0x1e9587 (probably m32u4)

avrdude_pr1507v5: processing -U flash:r:leonardo_readback.hex:i
avrdude_pr1507v5: reading flash memory ...
Reading | ################################################## | 100% 0.54 s
avrdude_pr1507v5: writing output file leonardo_readback.hex

avrdude_pr1507v5 done.  Thank you.

@mcuee
Copy link
Collaborator

mcuee commented Oct 7, 2023

@stefanrueger

Somehow __arm__ was not caught by Ubuntu 64bit Linux for my Raspberry Pi 400. I need to use -rr.

mcuee@ubunturpi400:~/build/avrdude_bin$ ./avrdude_pr1507v5 -C ./avrdude_pr1507v5.conf -p m32u4 -c avr109 -P /dev/ttyACM0 -r -U flash:r:leonardo_readback.hex:i
avrdude_pr1507v5: touching serial port /dev/ttyACM0 at 1200 baud
avrdude_pr1507v5: waiting for new port... 550 ms: using new port /dev/ttyACM0
avrdude_pr1507v5 OS error: cannot open port /dev/ttyACM0: Permission denied
avrdude_pr1507v5 error: unable to open port /dev/ttyACM0 for programmer avr109
Possible candidate serial ports are:
  -P /dev/ttyACM0 or -P usb:2341:0036
  -P /dev/ttyAMA0
Note that above ports might not be connected to a target board or an AVR programmer.
Also note there may be other direct serial ports not listed above.
avrdude_pr1507v5: alternatively, try -rr or -rrr for longer delays

avrdude_pr1507v5 done.  Thank you.

mcuee@ubunturpi400:~/build/avrdude_bin$ ./avrdude_pr1507v5 -C ./avrdude_pr1507v5.conf -p m32u4 -c avr109 -P /dev/ttyACM0 -rr -U flash:r:leonardo_readback.hex:i
avrdude_pr1507v5: touching serial port /dev/ttyACM0 at 1200 baud
avrdude_pr1507v5: waiting for new port... 850 ms: using new port /dev/ttyACM0
avrdude_pr1507v5: AVR device initialized and ready to accept instructions
avrdude_pr1507v5: device signature = 0x1e9587 (probably m32u4)

avrdude_pr1507v5: processing -U flash:r:leonardo_readback.hex:i
avrdude_pr1507v5: reading flash memory ...
Reading | ################################################## | 100% 0.27 s
avrdude_pr1507v5: writing output file leonardo_readback.hex

avrdude_pr1507v5 done.  Thank you.

mcuee@ubunturpi400:~/build/avrdude_bin$ ./avrdude_pr1507v5 -C ./avrdude_pr1507v5.conf -p m32u4 -c avr109 -P /dev/ttyACM0
 -rrr -U flash:r:leonardo_readback.hex:i
avrdude_pr1507v5: touching serial port /dev/ttyACM0 at 1200 baud
avrdude_pr1507v5: waiting for new port... 1250 ms: using new port /dev/ttyACM0
avrdude_pr1507v5: AVR device initialized and ready to accept instructions
avrdude_pr1507v5: device signature = 0x1e9587 (probably m32u4)

avrdude_pr1507v5: processing -U flash:r:leonardo_readback.hex:i
avrdude_pr1507v5: reading flash memory ...
Reading | ################################################## | 100% 0.28 s
avrdude_pr1507v5: writing output file leonardo_readback.hex

avrdude_pr1507v5 done.  Thank you.

@mcuee
Copy link
Collaborator

mcuee commented Oct 7, 2023

@stefanrueger
The fix is simple. ARM64 needs to use __aarch64__.

But probably need to exclude Apple Silicon which is also ARM64.

mcuee@ubunturpi400:~/build/avrdude_hans$ git diff
diff --git a/src/serialadapter.c b/src/serialadapter.c
index 21a350a9..007af317 100644
--- a/src/serialadapter.c
+++ b/src/serialadapter.c
@@ -326,7 +326,7 @@ int touch_serialport(char **portp, int baudrate, int nwaits) {
   serial_rawclose(&fd);

   int nloops = 32, nap = 50;
-#if defined(__arm__)
+#if defined(__arm__) || defined(__aarch64__)
   nwaits += 2;
 #endif
   pmsg_info("waiting for new port...");

mcuee@ubunturpi400:~/build/avrdude_bin$ ./avrdude_pr1507v5mod -C ./avrdude_pr1507v5.conf -p m32u4 -c avr109 -P /dev/ttyA
CM0 -r -U flash:r:leonardo_readback.hex:i
avrdude_pr1507v5mod: touching serial port /dev/ttyACM0 at 1200 baud
avrdude_pr1507v5mod: waiting for new port... 1250 ms: using new port /dev/ttyACM0
avrdude_pr1507v5mod: AVR device initialized and ready to accept instructions
avrdude_pr1507v5mod: device signature = 0x1e9587 (probably m32u4)

avrdude_pr1507v5mod: processing -U flash:r:leonardo_readback.hex:i
avrdude_pr1507v5mod: reading flash memory ...
Reading | ################################################## | 100% 0.28 s
avrdude_pr1507v5mod: writing output file leonardo_readback.hex

avrdude_pr1507v5mod done.  Thank you.

Edit to add: indeed it affects my Mac Mini M1 to make it too long (1250ms). The following patch is better which reduce the delay to 750ms for the Mac Mini M1.

mcuee@mcuees-Mac-mini avrdude_hans % git diff
diff --git a/src/serialadapter.c b/src/serialadapter.c
index 21a350a9..31ffe243 100644
--- a/src/serialadapter.c
+++ b/src/serialadapter.c
@@ -326,7 +326,7 @@ int touch_serialport(char **portp, int baudrate, int nwaits) {
   serial_rawclose(&fd);
 
   int nloops = 32, nap = 50;
-#if defined(__arm__)
+#if defined(__arm__) || defined(__aarch64__) && (!defined(__APPLE__))
   nwaits += 2;
 #endif
   pmsg_info("waiting for new port...");

@mcuee
Copy link
Collaborator

mcuee commented Oct 7, 2023

Somehow this PR does not work with official Arduino Nano Every board, under Windows. The older method works.

PS C:\work\avr\avrdude_test\avrdude_bin> mode COM12 baud=12 dtr=on > nul  && .\avrdude_git -c jtag2updi -p m4809 -P COM12
avrdude_git: AVR device initialized and ready to accept instructions
avrdude_git: device signature = 0x1e9651 (probably m4809)

avrdude_git done.  Thank you.
PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1507v5 -C .\avrdude_pr1507v5.conf -c jtag2updi -p m4809 -P COM12 -r

avrdude_pr1507v5: touching serial port COM12 at 1200 baud
avrdude_pr1507v5: waiting for new port... using same port COM12
avrdude_pr1507v5 warning: attempt 1 of 10: sign-on command: status -1
avrdude_pr1507v5 warning: attempt 2 of 10: sign-on command: status -1
avrdude_pr1507v5 warning: attempt 3 of 10: sign-on command: status -1
avrdude_pr1507v5 warning: attempt 4 of 10: sign-on command: status -1
avrdude_pr1507v5 warning: attempt 5 of 10: sign-on command: status -1
avrdude_pr1507v5 warning: attempt 6 of 10: sign-on command: status -1
avrdude_pr1507v5 warning: attempt 7 of 10: sign-on command: status -1
avrdude_pr1507v5 warning: attempt 8 of 10: sign-on command: status -1
avrdude_pr1507v5 warning: attempt 9 of 10: sign-on command: status -1
avrdude_pr1507v5 warning: attempt 10 of 10: sign-on command: status -1
avrdude_pr1507v5 error: timeout/error communicating with programmer (status -1)
avrdude_pr1507v5 error: unable to open port COM12 for programmer jtag2updi
A possible candidate serial port is:
  -P COM12 or -P usb:2341:0058
Note that above port might not be connected to a target board or an AVR programmer.
Also note there may be other direct serial ports not listed above.
avrdude_pr1507v5: alternatively, try -rr or -rrr for longer delays

avrdude_pr1507v5 done.  Thank you.

The following two experiments do not work either.

  1. no delay, not looking for new ports
$ git diff
diff --git a/src/serialadapter.c b/src/serialadapter.c
index 21a350a9..8fb92048 100644
--- a/src/serialadapter.c
+++ b/src/serialadapter.c
@@ -324,7 +324,7 @@ int touch_serialport(char **portp, int baudrate, int nwaits) {
   }
   serial_set_dtr_rts(&fd, 0);
   serial_rawclose(&fd);
-
+/*
   int nloops = 32, nap = 50;
 #if defined(__arm__)
   nwaits += 2;
@@ -349,7 +349,7 @@ int touch_serialport(char **portp, int baudrate, int nwaits) {
   }
   free_libserialport_data(sp1, n1);
   msg_info(" using %s port %s\n", i<0? "new": "same", *portp);
-
+*/
   return 0;
 }
  1. no 400ms initial delay, still looking for new ports
$ git diff
diff --git a/src/serialadapter.c b/src/serialadapter.c
index 21a350a9..41ebc8d0 100644
--- a/src/serialadapter.c
+++ b/src/serialadapter.c
@@ -330,7 +330,7 @@ int touch_serialport(char **portp, int baudrate, int nwaits) {
   nwaits += 2;
 #endif
   pmsg_info("waiting for new port...");
-  usleep(400*nwaits*1000);
+  //usleep(400*nwaits*1000);
   for(i = nloops; i > 0; i--) {
     usleep(nap*1000);
     if((sp2 = get_libserialport_data(&n2))) {

@mcuee
Copy link
Collaborator

mcuee commented Oct 7, 2023

Somehow this PR does not work with official Arduino Nano Every board, under Windows. The older method works.

But it works perfectly under Linux, including my Acer laptop and Raspberry Pi 400.

mcuee@UbuntuSwift3 ~/build/avr/avrdude_bin $ ./avrdude_pr1507v5 -C ./avrdude_pr1507v5.conf -c jtag2updi -p m4809 -P /dev/ttyACM0 -r -U flash:r:nano_every_readback.hex:i
avrdude_pr1507v5: touching serial port /dev/ttyACM0 at 1200 baud
avrdude_pr1507v5: waiting for new port... using same port /dev/ttyACM0
avrdude_pr1507v5: AVR device initialized and ready to accept instructions
avrdude_pr1507v5: device signature = 0x1e9651 (probably m4809)

avrdude_pr1507v5: processing -U flash:r:nano_every_readback.hex:i
avrdude_pr1507v5: reading flash memory ...
Reading | ################################################## | 100% 5.44 s 
avrdude_pr1507v5: writing output file nano_every_readback.hex

avrdude_pr1507v5 done.  Thank you.

Edit: it also works with my Mac Mini M1.

mcuee@mcuees-Mac-mini avrdude_bin % ./avrdude_pr1507v5mod1 -C ./avrdude_pr1507v5mod1.conf -p m4809 -c jtag2updi -P /dev/tty.usbmodem224301 -r -U flash:r:nano_every_readback.hex:i 
avrdude_pr1507v5mod1: touching serial port /dev/tty.usbmodem224301 at 1200 baud
avrdude_pr1507v5mod1: waiting for new port... using same port /dev/tty.usbmodem224301
avrdude_pr1507v5mod1: AVR device initialized and ready to accept instructions
avrdude_pr1507v5mod1: device signature = 0x1e9651 (probably m4809)

avrdude_pr1507v5mod1: processing -U flash:r:nano_every_readback.hex:i
avrdude_pr1507v5mod1: reading flash memory ...
Reading | ################################################## | 100% 5.69 s 
avrdude_pr1507v5mod1: writing output file nano_every_readback.hex

avrdude_pr1507v5mod1 done.  Thank you.

@stefanrueger
Copy link
Collaborator

[This] PR does not work with official Arduino Nano Every board, under Windows. The older method works.

Thanks for testing @mcuee! I have lost track: which older method do you refer to? So we can identify the difference.

The fix is simple. ARM64 needs to use __aarch64__. But probably need to exclude Apple Silicon which is also ARM64.

OK. I have now changed to

if (defined(__arm__) || defined(__aarch64__)) && !defined(__APPLE__)
  nwaits += 2;
#endif

Handing back to @MCUdude and @mcuee for the final round of checks and adaptations.

@mcuee
Copy link
Collaborator

mcuee commented Oct 8, 2023

[This] PR does not work with official Arduino Nano Every board, under Windows. The older method works.

Thanks for testing @mcuee! I have lost track: which older method do you refer to? So we can identify the difference.

The old method is mentioned in the above post. It is very strange that this PR does not work with Arduino Nano Every on Windows, yet no issues under Linux and macOS.
#1507 (comment)

PS C:\work\avr\avrdude_test\avrdude_bin> mode COM12 baud=12 dtr=on > nul  && .\avrdude_git -c jtag2updi -p m4809 -P COM12
avrdude_git: AVR device initialized and ready to accept instructions
avrdude_git: device signature = 0x1e9651 (probably m4809)

avrdude_git done.  Thank you.

The old method seems pretty reliable under Windows.

PS C:\work\avr\avrdude_test\avrdude_bin> mode COM12 baud=12 dtr=on > nul  && .\avrdude -c jtag2updi -p m4809 -P COM12 -U flash:r:nano_every_readback.hex:i
avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9651 (probably m4809)

avrdude: processing -U flash:r:nano_every_readback.hex:i
avrdude: reading flash memory ...
Reading | ################################################## | 100% 8.14 s
avrdude: writing output file nano_every_readback.hex

avrdude done.  Thank you.

For this PR, maybe it is again some timing issues. But I have not been able to figure out a work-around. Take note there is no PORT change and I do not hear Windows beep sound (no USB renumeration), either with the old working method, or this PR.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1507v5 -C .\avrdude_pr1507v5.conf -c jtag2updi -p m4809 -P COM12 -r

avrdude_pr1507v5: touching serial port COM12 at 1200 baud
avrdude_pr1507v5: waiting for new port... using same port COM12
avrdude_pr1507v5 warning: attempt 1 of 10: sign-on command: status -1
avrdude_pr1507v5 warning: attempt 2 of 10: sign-on command: status -1
avrdude_pr1507v5 warning: attempt 3 of 10: sign-on command: status -1
avrdude_pr1507v5 warning: attempt 4 of 10: sign-on command: status -1
avrdude_pr1507v5 warning: attempt 5 of 10: sign-on command: status -1
avrdude_pr1507v5 warning: attempt 6 of 10: sign-on command: status -1
avrdude_pr1507v5 warning: attempt 7 of 10: sign-on command: status -1
avrdude_pr1507v5 warning: attempt 8 of 10: sign-on command: status -1
avrdude_pr1507v5 warning: attempt 9 of 10: sign-on command: status -1
avrdude_pr1507v5 warning: attempt 10 of 10: sign-on command: status -1
avrdude_pr1507v5 error: timeout/error communicating with programmer (status -1)
avrdude_pr1507v5 error: unable to open port COM12 for programmer jtag2updi
A possible candidate serial port is:
  -P COM12 or -P usb:2341:0058
Note that above port might not be connected to a target board or an AVR programmer.
Also note there may be other direct serial ports not listed above.
avrdude_pr1507v5: alternatively, try -rr or -rrr for longer delays

avrdude_pr1507v5 done.  Thank you.

@MCUdude
Copy link
Collaborator Author

MCUdude commented Oct 9, 2023

My really, really slow Raspberry Pi 1 running Raspberry Pi OS works with both the Nano Every and the Pro Micro.

When connecting to the Nano Every the port stays the same so it connects to the same port, which does work.
When connecting to the Pro Micro the ports re-enumerates, and I'm getting a delay of 1250ms us with a single -r, and 1650ms with -rr:

pi@raspberrypi:~/avrdude $ build_linux/src/./avrdude -cavr109 -patmega32u4 -P /dev/ttyACM0 -r
avrdude: touching serial port /dev/ttyACM0 at 1200 baud
avrdude: waiting for new port... 1250 ms: using new port /dev/ttyACM0
avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9587 (probably m32u4)

avrdude done.  Thank you.

pi@raspberrypi:~/avrdude $ build_linux/src/./avrdude -cavr109 -patmega32u4 -P /dev/ttyACM0 -rr
avrdude: touching serial port /dev/ttyACM0 at 1200 baud
avrdude: waiting for new port... 1650 ms: using new port /dev/ttyACM0
avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9587 (probably m32u4)

avrdude done.  Thank you.

@mcuee
Copy link
Collaborator

mcuee commented Oct 9, 2023

@MCUdude

Please help to check if you can reproduce the issue of Arduino Nano Every board under Windows. Thanks. That is the only remaining issues I have with this PR.

@mcuee
Copy link
Collaborator

mcuee commented Oct 12, 2023

It seems to me that somehow the current touch method disturb the jtag2updi programmer.

Working method:

PS>mode COM6 baud=12 dtr=on > nul  && .\avrdude_pr1507v5 -C .\avrdude_pr1507v5.conf -c jtag2updi -p m4809 -P COM6 -vvvv

avrdude_pr1507v5: Version 7.2-20230720
                  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_pr1507v5.conf

                  Using Port                    : COM6
                  Using Programmer              : jtag2updi
avrdude_pr1507v5: jtagmkII_open_pdi()
avrdude_pr1507v5: jtagmkII_getsync()
avrdude_pr1507v5: jtagmkII_getsync() attempt 1 of 10: sending sign-on command:
avrdude_pr1507v5: jtagmkII_send(): sending 1 bytes
avrdude_pr1507v5: send: . [1b] . [00] . [00] . [01] . [00] . [00] . [00] . [0e] . [01] . [f3] . [97]
avrdude_pr1507v5: jtagmkII_recv():
avrdude_pr1507v5: recv: . [1b]
avrdude_pr1507v5: recv: . [00]
avrdude_pr1507v5: recv: . [00]
avrdude_pr1507v5: recv: . [1d]
avrdude_pr1507v5: recv: . [00]
avrdude_pr1507v5: recv: . [00]
avrdude_pr1507v5: recv: . [00]
avrdude_pr1507v5: recv: . [0e]
avrdude_pr1507v5: recv: . [86] . [01] . [01] . [07] . [01] . [01] . [01] . [07] . [06] . [01] . [00] . [00] . [00] . [00] . [00] . [00] J [4a] T [54] A [41] G [47] I [49] C [43] E [45]   [20] m [6d] k [6b] I [49] I [49] . [00]
avrdude_pr1507v5: recv: o [6f]
avrdude_pr1507v5: recv: V [56]

avrdude_pr1507v5: jtagmkII_recv(): got message seqno 0 (command_sequence == 0)
avrdude_pr1507v5: recv: . [86] . [01] . [01] . [07] . [01] . [01] . [01] . [07] . [06] . [01] . [00] . [00] . [00] . [00] . [00] . [00] J [4a] T [54] A [41] G [47] I [49] C [43] E [45]   [20] m [6d] k [6b] I [49] I [49] . [00]

Raw message:
0x86 0x01 0x01 0x07 0x01 0x01 0x01 0x07 0x06 0x01 0x00 0x00 0x00 0x00 0x00 0x00
0x4a 0x54 0x41 0x47 0x49 0x43 0x45 0x20 0x6d 0x6b 0x49 0x49 0x00
Sign-on succeeded
...

This PR: here I am trying the reduced time of 100ms; same result as decault 400ms. I have also tried to have longer delay.

PS>.\avrdude_pr1507v5_100ms_delay.exe -C .\avrdude_pr1507v5.conf -cjtag2updi -PCOM6 -pm4809 -r -vvvv

avrdude_pr1507v5_100ms_delay: Version 7.2-20230720
                              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_pr1507v5.conf

avrdude_pr1507v5_100ms_delay: touching serial port COM6 at 1200 baud
avrdude_pr1507v5_100ms_delay: waiting for new port... using same port COM6
                              Using Port                    : COM6
                              Using Programmer              : jtag2updi
avrdude_pr1507v5_100ms_delay: jtagmkII_open_pdi()
avrdude_pr1507v5_100ms_delay: jtagmkII_getsync()
avrdude_pr1507v5_100ms_delay: jtagmkII_getsync() attempt 1 of 10: sending sign-on command:
avrdude_pr1507v5_100ms_delay: jtagmkII_send(): sending 1 bytes
avrdude_pr1507v5_100ms_delay: send: . [1b] . [00] . [00] . [01] . [00] . [00] . [00] . [0e] . [01] . [f3] . [97]
avrdude_pr1507v5_100ms_delay: jtagmkII_recv():
avrdude_pr1507v5_100ms_delay: ser_recv(): programmer is not responding
avrdude_pr1507v5_100ms_delay: jtagmkII_recv(): timeout receiving packet
avrdude_pr1507v5_100ms_delay jtagmkII_getsync() [jtagmkII.c:685] warning: attempt 1 of 10: sign-on command: status -1
avrdude_pr1507v5_100ms_delay: jtagmkII_getsync() attempt 2 of 10: sending sign-on command:
avrdude_pr1507v5_100ms_delay: jtagmkII_send(): sending 1 bytes
avrdude_pr1507v5_100ms_delay: send: . [1b] . [00] . [00] . [01] . [00] . [00] . [00] . [0e] . [01] . [f3] . [97]
avrdude_pr1507v5_100ms_delay: jtagmkII_recv():
avrdude_pr1507v5_100ms_delay: ser_recv(): programmer is not responding
avrdude_pr1507v5_100ms_delay: jtagmkII_recv(): timeout receiving packet
avrdude_pr1507v5_100ms_delay jtagmkII_getsync() [jtagmkII.c:685] warning: attempt 2 of 10: sign-on command: status -1
avrdude_pr1507v5_100ms_delay: jtagmkII_getsync() attempt 3 of 10: sending sign-on command:
avrdude_pr1507v5_100ms_delay: jtagmkII_send(): sending 1 bytes
avrdude_pr1507v5_100ms_delay: send: . [1b] . [00] . [00] . [01] . [00] . [00] . [00] . [0e] . [01] . [f3] . [97]
avrdude_pr1507v5_100ms_delay: jtagmkII_recv():
avrdude_pr1507v5_100ms_delay: ser_recv(): programmer is not responding
avrdude_pr1507v5_100ms_delay: jtagmkII_recv(): timeout receiving packet
avrdude_pr1507v5_100ms_delay jtagmkII_getsync() [jtagmkII.c:685] warning: attempt 3 of 10: sign-on command: status -1
avrdude_pr1507v5_100ms_delay: jtagmkII_getsync() attempt 4 of 10: sending sign-on command:
avrdude_pr1507v5_100ms_delay: jtagmkII_send(): sending 1 bytes
avrdude_pr1507v5_100ms_delay: send: . [1b] . [00] . [00] . [01] . [00] . [00] . [00] . [0e] . [01] . [f3] . [97]
avrdude_pr1507v5_100ms_delay: jtagmkII_recv():
avrdude_pr1507v5_100ms_delay: ser_recv(): programmer is not responding
avrdude_pr1507v5_100ms_delay: jtagmkII_recv(): timeout receiving packet
avrdude_pr1507v5_100ms_delay jtagmkII_getsync() [jtagmkII.c:685] warning: attempt 4 of 10: sign-on command: status -1
avrdude_pr1507v5_100ms_delay: jtagmkII_getsync() attempt 5 of 10: sending sign-on command:
avrdude_pr1507v5_100ms_delay: jtagmkII_send(): sending 1 bytes
avrdude_pr1507v5_100ms_delay: send: . [1b] . [00] . [00] . [01] . [00] . [00] . [00] . [0e] . [01] . [f3] . [97]
avrdude_pr1507v5_100ms_delay: jtagmkII_recv():
avrdude_pr1507v5_100ms_delay: ser_recv(): programmer is not responding
avrdude_pr1507v5_100ms_delay: jtagmkII_recv(): timeout receiving packet
avrdude_pr1507v5_100ms_delay jtagmkII_getsync() [jtagmkII.c:685] warning: attempt 5 of 10: sign-on command: status -1
avrdude_pr1507v5_100ms_delay: jtagmkII_getsync() attempt 6 of 10: sending sign-on command:
avrdude_pr1507v5_100ms_delay: jtagmkII_send(): sending 1 bytes
avrdude_pr1507v5_100ms_delay: send: . [1b] . [00] . [00] . [01] . [00] . [00] . [00] . [0e] . [01] . [f3] . [97]
avrdude_pr1507v5_100ms_delay: jtagmkII_recv():
avrdude_pr1507v5_100ms_delay: ser_recv(): programmer is not responding
avrdude_pr1507v5_100ms_delay: jtagmkII_recv(): timeout receiving packet
avrdude_pr1507v5_100ms_delay jtagmkII_getsync() [jtagmkII.c:685] warning: attempt 6 of 10: sign-on command: status -1
avrdude_pr1507v5_100ms_delay: jtagmkII_getsync() attempt 7 of 10: sending sign-on command:
avrdude_pr1507v5_100ms_delay: jtagmkII_send(): sending 1 bytes
avrdude_pr1507v5_100ms_delay: send: . [1b] . [00] . [00] . [01] . [00] . [00] . [00] . [0e] . [01] . [f3] . [97]
avrdude_pr1507v5_100ms_delay: jtagmkII_recv():
avrdude_pr1507v5_100ms_delay: ser_recv(): programmer is not responding
avrdude_pr1507v5_100ms_delay: jtagmkII_recv(): timeout receiving packet
avrdude_pr1507v5_100ms_delay jtagmkII_getsync() [jtagmkII.c:685] warning: attempt 7 of 10: sign-on command: status -1
avrdude_pr1507v5_100ms_delay: jtagmkII_getsync() attempt 8 of 10: sending sign-on command:
avrdude_pr1507v5_100ms_delay: jtagmkII_send(): sending 1 bytes
avrdude_pr1507v5_100ms_delay: send: . [1b] . [00] . [00] . [01] . [00] . [00] . [00] . [0e] . [01] . [f3] . [97]
avrdude_pr1507v5_100ms_delay: jtagmkII_recv():
avrdude_pr1507v5_100ms_delay: ser_recv(): programmer is not responding
avrdude_pr1507v5_100ms_delay: jtagmkII_recv(): timeout receiving packet
avrdude_pr1507v5_100ms_delay jtagmkII_getsync() [jtagmkII.c:685] warning: attempt 8 of 10: sign-on command: status -1
avrdude_pr1507v5_100ms_delay: jtagmkII_getsync() attempt 9 of 10: sending sign-on command:
avrdude_pr1507v5_100ms_delay: jtagmkII_send(): sending 1 bytes
avrdude_pr1507v5_100ms_delay: send: . [1b] . [00] . [00] . [01] . [00] . [00] . [00] . [0e] . [01] . [f3] . [97]
avrdude_pr1507v5_100ms_delay: jtagmkII_recv():
avrdude_pr1507v5_100ms_delay: ser_recv(): programmer is not responding
avrdude_pr1507v5_100ms_delay: jtagmkII_recv(): timeout receiving packet
avrdude_pr1507v5_100ms_delay jtagmkII_getsync() [jtagmkII.c:685] warning: attempt 9 of 10: sign-on command: status -1
avrdude_pr1507v5_100ms_delay: jtagmkII_getsync() attempt 10 of 10: sending sign-on command:
avrdude_pr1507v5_100ms_delay: jtagmkII_send(): sending 1 bytes
avrdude_pr1507v5_100ms_delay: send: . [1b] . [00] . [00] . [01] . [00] . [00] . [00] . [0e] . [01] . [f3] . [97]
avrdude_pr1507v5_100ms_delay: jtagmkII_recv():
avrdude_pr1507v5_100ms_delay: ser_recv(): programmer is not responding
avrdude_pr1507v5_100ms_delay: jtagmkII_recv(): timeout receiving packet
avrdude_pr1507v5_100ms_delay jtagmkII_getsync() [jtagmkII.c:685] warning: attempt 10 of 10: sign-on command: status -1
avrdude_pr1507v5_100ms_delay jtagmkII_getsync() [jtagmkII.c:732] error: timeout/error communicating with programmer (status -1)
avrdude_pr1507v5_100ms_delay main() [main.c:1278] error: unable to open port COM6 for programmer jtag2updi
Possible candidate serial ports are:
  -P COM3
  -P COM6 or -P usb:2341:0058
Note that above ports might not be connected to a target board or an AVR programmer.
Also note there may be other direct serial ports not listed above.
avrdude_pr1507v5_100ms_delay: alternatively, try -rr or -rrr for longer delays

avrdude_pr1507v5_100ms_delay done.  Thank you.

@MCUdude
Copy link
Collaborator Author

MCUdude commented Oct 12, 2023

Please help to check if you can reproduce the issue of Arduino Nano Every board under Windows. Thanks. That is the only remaining issues I have with this PR.

Sorry, for the delay. I'll see if I can give it a try this evening.

It seems to me that somehow the current touch method disturbs the jtag2updi programmer.

Strange... And you're using an Arduino Nano Every, not a DIY jtag2updi?

@mcuee
Copy link
Collaborator

mcuee commented Oct 12, 2023

It seems to me that somehow the current touch method disturbs the jtag2updi programmer.

Strange... And you're using an Arduino Nano Every, not a DIY jtag2updi?

Correct. I am using the official Arduino Nano Every board, not a DIY jtag2updi. And the issue only happens under Windows. It is okay under Linux and macOS.

I will try with a DIY jtag2updi as well later.

@mcuee
Copy link
Collaborator

mcuee commented Oct 12, 2023

It seems to me that somehow the current touch method disturbs the jtag2updi programmer.

Strange... And you're using an Arduino Nano Every, not a DIY jtag2updi?

Correct. I am using the official Arduino Nano Every board, not a DIY jtag2updi. And the issue only happens under Windows. It is okay under Linux and macOS.

I will try with a DIY jtag2updi as well later.

@MCUdude

Thanks for the tip. No issues with a jtag2updi using a CH340 Arduino Uno clone.

PS>.\avrdude_pr1507v5 -C .\avrdude_pr1507v5.conf -cjtag2updi -PCOM15 -pm4808
avrdude_pr1507v5: AVR device initialized and ready to accept instructions
avrdude_pr1507v5: device signature = 0x1e9650 (probably m4808)

avrdude_pr1507v5 done.  Thank you.

@mcuee
Copy link
Collaborator

mcuee commented Oct 12, 2023

@MCUdude and @stefanrueger

I am good with the PR now.

I will attribute the issue with official Arduino Nano Every board under Windows to the on-board jtag2updi implementation by Arduino. If there is an easy fix, that would be good. If not, I can live with that problem under Windows.

Reference: Arduino jtag2updi implementation for Arduino Nano Every
https://github.com/arduino/ArduinoCore-megaavr/tree/master/firmwares/MuxTO

There are multiple issues from Arduino github repo:
https://github.com/search?q=repo%3Aarduino%2FArduinoCore-megaavr+MuxTO&type=issues

@mcuee
Copy link
Collaborator

mcuee commented Oct 13, 2023

@MCUdude

I have found the fix for the Arduino Nano Every problem under Windows.

I believe the fix is safe to be added. Please check as well. Thanks.

$ git diff
diff --git a/src/serialadapter.c b/src/serialadapter.c
index 6a5f9d2e..448424a0 100644
--- a/src/serialadapter.c
+++ b/src/serialadapter.c
@@ -322,6 +322,7 @@ int touch_serialport(char **portp, int baudrate, int nwaits) {
     pmsg_error("%s() failed to open port %s at %d baud\n", __func__, *portp, baudrate);
     return -1;
   }
+  serial_set_dtr_rts(&fd, 1);
   serial_set_dtr_rts(&fd, 0);
   serial_rawclose(&fd);

PS> .\avrdude_pr1507v5mod -C .\avrdude_pr1507v5mod.conf -c jtag2updi -p m4809 -P COM12 -r
avrdude_pr1507v5mod: touching serial port COM12 at 1200 baud
avrdude_pr1507v5mod: waiting for new port... using same port COM12
avrdude_pr1507v5mod: AVR device initialized and ready to accept instructions
avrdude_pr1507v5mod: device signature = 0x1e9651 (probably m4809)

avrdude_pr1507v5mod done.  Thank you.

@stefanrueger
Copy link
Collaborator

@mcuee Good find! I suggest a very short delay when plucking RTS/DTR (see discussion in PR #1505)

   serial_set_dtr_rts(&fd, 1);
   usleep(100);
   serial_set_dtr_rts(&fd, 0);

I pushed a relevant commit. Please re-test this version for all combos (as it is a fundamental change in the core serial touch routine).

@mcuee
Copy link
Collaborator

mcuee commented Oct 13, 2023

@mcuee Good find! I suggest a very short delay when plucking RTS/DTR (see discussion in PR #1505)

   serial_set_dtr_rts(&fd, 1);
   usleep(100);
   serial_set_dtr_rts(&fd, 0);

I pushed a relevant commit. Please re-test this version for all combos (as it is a fundamental change in the core serial touch routine).

Yes this is good for all three setup I have. Now I am fine with this PR.

  1. Official Arduino Leonardo
  2. Official Arduino Nano Every
  3. jtag2updi programmer based on Arduino Uno Clone using CH340, target is Atmega4808 Nano Clone.
PS> .\avrdude_pr1507v6 -C .\avrdude_pr1507v6.conf -c avr109 -p m32u4 -P COM5 -r
avrdude_pr1507v6: touching serial port COM5 at 1200 baud
avrdude_pr1507v6: waiting for new port... 500 ms: using new port COM6
avrdude_pr1507v6: AVR device initialized and ready to accept instructions
avrdude_pr1507v6: device signature = 0x1e9587 (probably m32u4)

PS> .\avrdude_pr1507v6 -C .\avrdude_pr1507v6.conf -c jtag2updi -p m4809 -P COM12 -r
avrdude_pr1507v6: touching serial port COM12 at 1200 baud
avrdude_pr1507v6: waiting for new port... using same port COM12
avrdude_pr1507v6: AVR device initialized and ready to accept instructions
avrdude_pr1507v6: device signature = 0x1e9651 (probably m4809)

avrdude_pr1507v6 done.  Thank you.

PS> .\avrdude_pr1507v6 -C .\avrdude_pr1507v6.conf -c jtag2updi -p m4808 -P COM7 -r
avrdude_pr1507v6: touching serial port COM7 at 1200 baud
avrdude_pr1507v6: waiting for new port... using same port COM7
avrdude_pr1507v6: AVR device initialized and ready to accept instructions
avrdude_pr1507v6: device signature = 0x1e9650 (probably m4808)

avrdude_pr1507v6 done.  Thank you.

@MCUdude
Copy link
Collaborator Author

MCUdude commented Oct 13, 2023

Great find @mcuee! The Nano Every still works on Mac OS.

BTW, avrdude doesn't suggest a serial port if the user specified is not present when using -r:

$ ./avrdude -cjtag2updi -patmega4809 -P dunno
avrdude OS error: cannot open port dunno: No such file or directory
avrdude error: unable to open port dunno for programmer jtag2updi
Possible candidate serial ports are:
  -P /dev/cu.Bluetooth-Incoming-Port
  -P /dev/cu.usbmodem14101 or -P usb:2341:0058
Note that above ports might not be connected to a target board or an AVR programmer.
Also note there may be other direct serial ports not listed above.

avrdude done.  Thank you.

$ ./avrdude -cjtag2updi -patmega4809 -P dunno -r
avrdude: touching serial port dunno at 1200 baud
avrdude OS error: cannot open port dunno: No such file or directory
avrdude error: touch_serialport() failed to open port dunno at 1200 baud

@stefanrueger
Copy link
Collaborator

avrdude doesn't suggest a serial port if the user specified is not present when using -r

Good catch. I pushed a commit; it should now.

@MCUdude
Copy link
Collaborator Author

MCUdude commented Oct 15, 2023

Excellent @stefanrueger! I think this PR safely can be merged in the next merge fest.

$ ./avrdude -cjtag2updi -patmega4809 -P dunno -r
avrdude: touching serial port dunno at 1200 baud
avrdude OS error: cannot open port dunno: No such file or directory
avrdude error: touch_serialport() failed to open port dunno at 1200 baud
Possible candidate serial ports are:
  -P /dev/cu.Bluetooth-Incoming-Port
  -P /dev/cu.usbmodem14201 or -P usb:2341:0058
Note that above ports might not be connected to a target board or an AVR programmer.
Also note there may be other direct serial ports not listed above.
avrdude: alternatively, try -rr or -rrr for longer delays

avrdude done.  Thank you.

@stefanrueger stefanrueger linked an issue Oct 15, 2023 that may be closed by this pull request
@stefanrueger stefanrueger merged commit 99a9a60 into avrdudes:main Oct 15, 2023
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for cross-platform "1200bps touch" using libserialport
4 participants