8000 Fix r/w timeout for Windows `ser_send()` by stefanrueger · Pull Request #1987 · avrdudes/avrdude · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix r/w timeout for Windows ser_send() #1987

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

Merged
merged 2 commits into from
May 21, 2025

Conversation

stefanrueger
Copy link
Collaborator
@stefanrueger stefanrueger commented May 12, 2025

Fixes #1986.

First there is no bus for 20 years and then there are two buses within a couple of days (unrelated edge cases in low-level serial I/O: one for Posix in PR 1983, the other one for Windows...).

Should be thoroughly tested with serial I/O, best with large parts with large page sizes and low baud rates.

@stefanrueger stefanrueger added the bug Something isn't working label May 12, 2025
@mcuee
Copy link
Collaborator
mcuee commented May 14, 2025

@stefanrueger

Difficulty in testing -- how to get working programmer or bootloader to test this PR, eg, working bootloader FW at 100bps/200bps/300bps.

@mcuee
Copy link
Collaborator
mcuee commented May 14, 2025

Example: urboot autobaud bootloader, 16MHz crystal, ATmega2560, CH340.
It works at 9600 bps but not 4800 bps, using git main or this PR.

C:\work\avr\avrdude_bin> .\avrdude_git.exe -c urclock -P ch340 -b 4800 -p m2560 -v
Avrdude_git version 8.0-20250512 (127f40ee)
Copyright see https://github.com/avrdudes/avrdude/blob/main/AUTHORS

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

Using port            : COM4
Using programmer      : urclock
Setting baud rate     : 4800
Warning: attempt 1 of 10: not in sync
Warning: attempt 2 of 10: not in sync
Warning: attempt 3 of 10: not in sync
Warning: attempt 4 of 10: not in sync
Warning: attempt 5 of 10: not in sync
Warning: attempt 6 of 10: not in sync
Warning: attempt 7 of 10: not in sync
Warning: attempt 8 of 10: not in sync
Warning: attempt 9 of 10: not in sync
Warning: attempt 10 of 10: not in sync
Warning: programmer is not responding; try -x strict and/or vary -x delay=100
Error: unable to open port COM4 for programmer urclock
Possible candidate serial ports are:
  -P COM3
  -P COM4 or -P ch340
  -P COM6
  -P COM8
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_git done.  Thank you.

C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -c urclock -P ch340 -b 4800 -p m2560 -v
Avrdude_pr1987 version 8.0-20250513 (e9d58b5f)
Copyright see https://github.com/avrdudes/avrdude/blob/main/AUTHORS

System wide configuration file is C:\work\avr\avrdude_bin\avrdude.conf
Warning: system wide configuration file version (8.0-20250512 (127f40ee))
does not match Avrdude build version (8.0-20250513 (e9d58b5f))

Using port            : COM4
Using programmer      : urclock
Setting baud rate     : 4800
Warning: attempt 1 of 10: not in sync
Warning: attempt 2 of 10: not in sync
Warning: attempt 3 of 10: not in sync
Warning: attempt 4 of 10: not in sync
Warning: attempt 5 of 10: not in sync
Warning: attempt 6 of 10: not in sync
Warning: attempt 7 of 10: not in sync
Warning: attempt 8 of 10: not in sync
Warning: attempt 9 of 10: not in sync
Warning: attempt 10 of 10: not in sync
Warning: programmer is not responding; try -x strict and/or vary -x delay=100
Error: unable to open port COM4 for programmer urclock
Possible candidate serial ports are:
  -P COM3
  -P COM4 or -P ch340
  -P COM6
  -P COM8
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_pr1987 done.  Thank you.

@mcuee
Copy link
Collaborator
mcuee commented May 14, 2025

@stefanrueger

By accident, I was able to reproduce Issue #1986 with a Bluetooth COM port under my Dell Windows 11 Laptop. And I can confirm that this PR fixed the issue.

@mcuee
Copy link
Collaborator
mcuee commented May 14, 2025

@stefanrueger

Just wondering if you can provide working low baud rate bootloader hex files for Arduino Mega2560 or Arduino Uno, eg, 100bps, 200bps, 300bps and 600bps, if that is even possible.
https://github.com/stefanrueger/urboot.hex/tree/main/boards/mega-r3/atmega2560/watchdog_1_s/external_oscillator_x/16m000000_hz

@stefanrueger
Copy link
Collaborator Author
stefanrueger commented May 14, 2025

provide working low baud rate bootloader hex files for Arduino Mega2560

Easy: replace dryrun with your favourite programmer in the following line to get a 300 baud bootloader:

$ avrdude -c dryrun -p m2560 -U urboot:16mhz_300baud_ce_ee
Reading 420 bytes for flash from input file urboot:16mhz_300baud_ce_ee
Writing 420 bytes to flash
Writing | ################################################## | 100% 0.00 s 
Reading | ################################################## | 100% 0.00 s 
420 bytes of flash verified
Setting fuses for bootloader urboot:16mhz_300baud_ce_ee

Avrdude done.  Thank you.`

Fixed-rate bootloaders can have a theoretical baud rate range in [F_CPU/16/4096, F_CPU/8/1].

Using the internal 8 MHz clock, you can go down to 150 baud. However, the parser for 8000 urboot:... limits the baud rate arbitrarily to [300 baud, 800 8000 kbaud]. Couldn't imagine anyone wanting a lower baud rate 😮. Turns out a 16 MHz/300 baud bootloader is exactly the same as an 8 MHz/150 baud bootloader, so all you need to do is remember the current fuse sut_cksel setting and switch it to internal F_CPU for 150 baud:

$ avrdude -qqc dryrun -p m2560 -T "config sut_cksel"
config sut_cksel=[... remember me ...]

$ avrdude -qqc dryrun -p m2560 -T "config -v sut_cksel=intrcosc_6ck_65ms"
config sut_cksel=intrcosc_6ck_65ms # 34

... and later set the sut_cksel configuration back to what it was.

@stefanrueger
Copy link
Collaborator Author
stefanrueger commented May 14, 2025

Edit: factory F_CPU is 1 MHz, not 8 MHz, so, you'd need to burn a 1760 baud bootloader (= 110*16) for a 16 MHz F_CPU and then switch to internal F_CPU of 1 MHz to experience 110 baud.

Edit edit:

factory F_CPU is 1 MHz

But that needs the -T config ckdiv8=by_8 configuration set as well... otherwise switching to internal switches 8 MHz as previously stated.

@stefanrueger
Copy link
Collaborator Author

autobaud bootloader, 16MHz crystal, ATmega2560, CH340, works at 9600 bps but not 4800 bps

Autobaud is limited to [F_CPU/8/256, F_CPU/8] but even there has a few blindspots owing to the nature of the integer baud rate division in classic parts.

@stefanrueger
Copy link
Collaborator Author
stefanrueger commented May 14, 2025

Just to say, git main now parses an expanded baud interval of [10 baud, 8 Mbaud] irrespective of whether that's achievable with the given F_CPU. So can ask for 110 baud bootloaders at 1 MHz F_CPU using -U urboot:i1MHz_110baud. It's a matter of many hours reading the flash contents of an ATmega2560, though. Low baud rates are probably only ever useful for very low frequency low power applications, eg, when driven by an external clock quartz of 32768 Hz...

@mcuee
Copy link
Collaborator
mcuee commented May 15, 2025

@stefanrueger

Somehow it does not work at 300bps.

I tried 600bps and 1200 bps and both do not work. 2400bps works.

C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -c usbasp -p m2560 -U urboot:16mhz_300baud_ce_ee
Warning: system wide configuration file version (8.0-20250512 (127f40ee))
does not match Avrdude build version (8.0-20250513 (e9d58b5f))
Reading 420 bytes for flash from input file urboot:16mhz_300baud_ce_ee
Writing 420 bytes to flash
Writing | ################################################## | 100% 0.26 s
Reading | ################################################## | 100% 0.14 s
420 bytes of flash verified
Setting fuses for bootloader urboot:16mhz_300baud_ce_ee

Avrdude_pr1987 done.  Thank you.

C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c urclock -P ch340 -b 300 -p m2560 -v
Avrdude_pr1987 version 8.0-20250513 (e9d58b5f)
Copyright see https://github.com/avrdudes/avrdude/blob/main/AUTHORS

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

Using port            : COM4
Using programmer      : urclock
Setting baud rate     : 300
AVR part              : ATmega2560
Programming modes     : SPM, ISP, HVPP, JTAG
Programmer type       : Urclock
Description           : Urboot bootloaders using urprotocol
Protocol              : Urprotocol

AVR device initialized and ready to accept instructions
Error: protocol expects OK byte 0x8f but got 0x67 in ur_readEF()
Error: unable to read signature data for part ATmega2560 (rc = -1)
Error: unable to read signature data (rc = -1)
use -F to override this check
Error: protocol expects sync byte 0xa0 but got 0x40

Avrdude_pr1987 done.  Thank you.

@mcuee
Copy link
Collaborator
mcuee commented May 15, 2025

2400bps -- working with 16MHz crystal.

C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -c usbasp -p m2560 -U urboot:16mhz_2400baud_ce_ee
Warning: system wide configuration file version (8.0-20250512 (127f40ee))
does not match Avrdude build version (8.0-20250513 (e9d58b5f))
Reading 420 bytes for flash from input file urboot:16mhz_2400baud_ce_ee
Writing 420 bytes to flash
Writing | ################################################## | 100% 0.27 s
Reading | ################################################## | 100% 0.15 s
420 bytes of flash verified
Setting fuses for bootloader urboot:16mhz_2400baud_ce_ee

Avrdude_pr1987 done.  Thank you.

C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c urclock -P ch340 -b 2400 -p m2560 -v
Avrdude_pr1987 version 8.0-20250513 (e9d58b5f)
Copyright see https://github.com/avrdudes/avrdude/blob/main/AUTHORS

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

Using port            : COM4
Using programmer      : urclock
Setting baud rate     : 2400
AVR part              : ATmega2560
Programming modes     : SPM, ISP, HVPP, JTAG
Programmer type       : Urclock
Description           : Urboot bootloaders using urprotocol
Protocol              : Urprotocol

AVR device initialized and ready to accept instructions
Device signature = 1E 98 01 (ATmega2560)

Avrdude_pr1987 done.  Thank you.

Now we change the clock configuration.

C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -qq -c usbasp -p m2560 -T "config sut_cksel"
config sut_cksel=fsosc_16kck_65ms_xosc_slowpwr # 55

C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -qq -c usbasp -p m2560 -T "config -v sut_cksel=intrcosc_6ck_65ms"
config sut_cksel=intrcosc_6ck_65ms # 34

Now urboot is supposed to work with 1200 bps (from 16MHz CPU clock down to 8MHz internal clock). Unfortunately it does not work.

C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c urclock -P ch340 -b 1200 -p m2560 -v
Avrdude_pr1987 version 8.0-20250513 (e9d58b5f)
Copyright see https://github.com/avrdudes/avrdude/blob
8000
/main/AUTHORS

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

Using port            : COM4
Using programmer      : urclock
Setting baud rate     : 1200
AVR part              : ATmega2560
Programming modes     : SPM, ISP, HVPP, JTAG
Programmer type       : Urclock
Description           : Urboot bootloaders using urprotocol
Protocol              : Urprotocol

AVR device initialized and ready to accept instructions
Error: protocol expects OK byte 0x8f but got 0x67 in ur_readEF()
Error: unable to read signature data for part ATmega2560 (rc = -1)
Error: unable to read signature data (rc = -1)
use -F to override this check
Error: protocol expects sync byte 0xa0 but got 0x40

Avrdude_pr1987 done.  Thank you.

Same if I further reduce the internal clock to 1MHz and still it does not work with 150 bps.

C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -qq -c usbasp -p m2560 -T "config -v sut_cksel=intrcosc_6ck_65ms" -T "config -v ckdiv8=by_8"
config sut_cksel=intrcosc_6ck_65ms # 34
config ckdiv8=by_8 # 0

C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c urclock -P ch340 -b 150 -p m2560 -v
Avrdude_pr1987 version 8.0-20250513 (e9d58b5f)
Copyright see https://github.com/avrdudes/avrdude/blob/main/AUTHORS

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

Using port            : COM4
Using programmer      : urclock
Setting baud rate     : 150
AVR part              : ATmega2560
Programming modes     : SPM, ISP, HVPP, JTAG
Programmer type       : Urclock
Description           : Urboot bootloaders using urprotocol
Protocol              : Urprotocol
Error: protocol expects sync byte 0xa0 but got 0x8f in urclock_program_enable()
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
Warning: programmer is not responding

Avrdude_pr1987 done.  Thank you.

@mcuee
Copy link
Collaborator
mcuee commented May 15, 2025

External clock 16MHz --> 1200bps does not work. But no regression from this PR either, same behavior as avrdude 8.0 release.

C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c usbasp -p m2560 -U urboot:16mhz_4800baud_ce_ee -qq && echo OK
OK
C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c urclock -P ch340 -b 4800 -p m2560

Avrdude_pr1987 done.  Thank you.
C:\work\avr\avrdude_bin> .\avrdude80.exe -C .\avrdude80.conf -c urclock -P COM4 -b 4800 -p m2560

Avrdude80 done.  Thank you.
C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c usbasp -p m2560 -U urboot:16mhz_2400baud_ce_ee -qq && echo OK
OK
C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c urclock -P ch340 -b 2400 -p m2560

Avrdude_pr1987 done.  Thank you.
C:\work\avr\avrdude_bin> .\avrdude80.exe -C .\avrdude80.conf -c urclock -P COM4 -b 2400 -p m2560

Avrdude80 done.  Thank you.
C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c usbasp -p m2560 -U urboot:16mhz_1200baud_ce_ee -qq && echo OK
OK
C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c urclock -P ch340 -b 1200 -p m2560
Error: protocol expects OK byte 0x8f but got 0x67 in ur_readEF()
Error: unable to read signature data for part ATmega2560 (rc = -1)
Error: unable to read signature data (rc = -1)
use -F to override this check
Error: protocol expects sync byte 0xa0 but got 0x40

Avrdude_pr1987 done.  Thank you.
C:\work\avr\avrdude_bin> .\avrdude80.exe -C .\avrdude80.conf -c urclock -P COM4 -b 1200 -p m2560
Warning: programmer is not responding
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
Warning: programmer is not responding

Avrdude80 done.  Thank you.

@mcuee
Copy link
Collaborator
mcuee commented May 15, 2025

Internal clock 8MHz --> 1200bps does not work. But no regression from this PR either, same behavior as avrdude 8.0 release.

C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c usbasp -p m2560 -U urboot:8mhz_1200baud_ce_ee -qq && echo OK
OK
C:\work\avr\avrdude_bin> .\avrdude80.exe -C .\avrdude80.conf -c urclock -P COM4 -b 1200 -p m2560
Warning: programmer is not responding
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
Warning: programmer is not responding

Avrdude80 done.  Thank you.
C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c urclock -P ch340 -b 1200 -p m2560
Error: protocol expects OK byte 0x8f but got 0x67 in ur_readEF()
Error: unable to read signature data for part ATmega2560 (rc = -1)
Error: unable to read signature data (rc = -1)
use -F to override this check
Error: protocol expects sync byte 0xa0 but got 0x40

Avrdude_pr1987 done.  Thank you.

C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c usbasp -p m2560 -U urboot:8mhz_2400baud_ce_ee -qq && echo OK
OK
C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c urclock -P ch340 -b 2400 -p m2560

Avrdude_pr1987 done.  Thank you.
C:\work\avr\avrdude_bin> .\avrdude80.exe -C .\avrdude80.conf -c urclock -P COM4 -b 2400 -p m2560

Avrdude80 done.  Thank you.

@mcuee
Copy link
Collaborator
mcuee commented May 15, 2025

Internal clock 1MHz, no regressions.

When it failed, this PR also gives a bit more info.

C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c usbasp -p m2560 -U urboot:1mhz_2400baud_ce_ee -qq && echo OK
OK
C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c urclock -P ch340 -b 2400 -p m2560

Avrdude_pr1987 done.  Thank you.
C:\work\avr\avrdude_bin> .\avrdude_git.exe -C .\avrdude_git.conf -c urclock -P COM4 -b 2400 -p m2560

Avrdude_git done.  Thank you.
C:\work\avr\avrdude_bin> .\avrdude80.exe -C .\avrdude80.conf -c urclock -P COM4 -b 2400 -p m2560

Avrdude80 done.  Thank you.

C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c usbasp -p m2560 -U urboot:1mhz_1200baud_ce_ee -qq && echo OK
OK
C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c urclock -P ch340 -b 1200 -p m2560
Error: protocol expects OK byte 0x8f but got 0x67 in ur_readEF()
Error: unable to read signature data for part ATmega2560 (rc = -1)
Error: unable to read signature data (rc = -1)
use -F to override this check
Error: protocol expects sync byte 0xa0 but got 0x40

Avrdude_pr1987 done.  Thank you.
C:\work\avr\avrdude_bin> .\avrdude_git.exe -C .\avrdude_git.conf -c urclock -P COM4 -b 1200 -p m2560
Warning: programmer is not responding
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
Warning: programmer is not responding

Avrdude_git done.  Thank you.
C:\work\avr\avrdude_bin> .\avrdude80.exe -C .\avrdude80.conf -c urclock -P COM4 -b 1200 -p m2560
Warning: programmer is not responding
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
Warning: programmer is not responding

Avrdude80 done.  Thank you.

@mcuee
Copy link
Collaborator
mcuee commented May 15, 2025

@stefanrueger

I think this PR is good to go. It sorted out Issue #1986. And the good thing is that I do not see regressions either.

@mcuee
Copy link
Collaborator
mcuee commented May 15, 2025

Not so sure if the following is related to #1982, but git main and this PR works better than avrdude 8.0.

1MHz internal clock, 1800 bps.

C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c usbasp -p m2560 -U urboot:1mhz_1800baud_ce_ee -qq && echo OK
OK

C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c urclock -P ch340 -b 1800 -p m2560

Avrdude_pr1987 done.  Thank you.
C:\work\avr\avrdude_bin> .\avrdude_git.exe -C .\avrdude_git.conf -c urclock -P COM4 -b 1800 -p m2560

Avrdude_git done.  Thank you.

C:\work\avr\avrdude_bin> .\avrdude80.exe -C .\avrdude80.conf -c urclock -P COM4 -b 1800 -p m2560
Warning: programmer is not responding
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
Warning: programmer is not responding

Avrdude80 done.  Thank you.

@mcuee
Copy link
Collaborator
mcuee commented May 15, 2025

Same for ATmega328P, 16MHz crystal, 2400bps works, 1200bps do not work.

1800bps --> avrdude 8.0 release does not work. git main and this PR both work.

C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c usbasp -p m328p -U urboot:16mhz_1200baud_ce_ee -qq && echo OK
OK
C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c urclock -P COM4 -b 1200 -p m328p
Error: protocol expects OK byte 0x77 but got 0x67 in ur_readEF()
Error: unable to read signature data for part ATmega328P (rc = -1)
Error: unable to read signature data (rc = -1)
use -F to override this check
Error: protocol expects sync byte 0xa0 but got 0x40

Avrdude_pr1987 done.  Thank you.
C:\work\avr\avrdude_bin> .\avrdude_git.exe -C .\avrdude_git.conf -c urclock -P COM4 -b 1200 -p m328p
Error: protocol expects OK byte 0x77 but got 0x67 in ur_readEF()
Error: unable to read signature data for part ATmega328P (rc = -1)
Error: unable to read signature data (rc = -1)
use -F to override this check
Error: protocol expects sync byte 0xa0 but got 0x40

Avrdude_git done.  Thank you.
C:\work\avr\avrdude_bin> .\avrdude80.exe -C .\avrdude80.conf -c urclock -P COM4 -b 1200 -p m328p
Warning: programmer is not responding
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
Warning: programmer is not responding

Avrdude80 done.  Thank you.

C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c usbasp -p m328p -U urboot:16mhz_1800baud_ce_ee -qq && echo OK
OK
C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c urclock -P COM4 -b 1800 -p m328p

Avrdude_pr1987 done.  Thank you.
C:\work\avr\avrdude_bin> .\avrdude_git.exe -C .\avrdude_git.conf -c urclock -P COM4 -b 1800 -p m328p

Avrdude_git done.  Thank you.
C:\work\avr\avrdude_bin> .\avrdude80.exe -C .\avrdude80.conf -c urclock -P COM4 -b 1800 -p m328p
Warning: programmer is not responding
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
Warning: programmer is not responding

Avrdude80 done.  Thank you.

@stefanrueger
Copy link
Collaborator Author

Thanks for verifying that 2400 baud works. I have no idea why 300 baud doesn't. Here is the difference of the two bootloaders:

$ avrdude -qqc dryrun -p m2560 -U urboot:16mhz_2400baud_ce_ee -T "disasm flash -512 -1" >/tmp/d2400
$ avrdude -qqc dryrun -p m2560 -U urboot:16mhz_300baud_ce_ee -T "disasm flash -512 -1" | diff - /tmp/d2400
23c23
< L3fe16: bd e0        ldi     r27, 0x0d            ; 13
---
> L3fe16: b1 e0        ldi     r27, 0x01            ; 1
25c25
< L3fe1a: b4 e0        ldi     r27, 0x04            ; 4
---
> L3fe1a: b0 ea        ldi     r27, 0xa0            ; 160

That difference is down to the initialisation of the 12-bit UBBR0 register:

$ avrdude -qqc dryrun -p m2560 -U urboot:16mhz_300baud_ce_ee \
    -T "disasm flash 0x3fe12 12" -T "regfile -am ubrr0"

                     .equ    mem.ucsr0a, 0xc0
L3fe12: e0 ec        ldi     r30, lo8(mem.ucsr0a) ; 0xc0 = 192
L3fe14: f0 e0        ldi     r31, hi8(mem.ucsr0a) ; 0x00 = 0
L3fe16: bd e0        ldi     r27, 0x0d            ; 13
L3fe18: b5 83        std     Z+5, r27
L3fe1a: b4 e0        ldi     r27, 0x04            ; 4
L3fe1c: b4 83        std     Z+4, r27

mem 0xc4: usart0.ubrr0

The baud rate formula for the asynchronous normal mode (U2X = 0) is

$$b = \frac{f_{\rm CPU}}{16({\rm UBRR0} + 1)}.$$

So, in one case (UBRR0 = 0x0d04) it is 300.03 baud and in the other case (UBRR0 = 0x01a0) it is 2398.08 baud.

And, as is usual for 16-bit registers, the high byte is written before the low byte...

@stefanrueger
Copy link
Collaborator Author

Maybe for lower baud rates the WDT timeout needs increasing? As in -U urboot:16mhz_300baud_ce_ee_2s

6D40

@stefanrueger
Copy link
Collaborator Author

Not so sure if the following is related to #1982, but git main and this PR works better than avrdude 8.0.
1MHz internal clock, 1800 bps.

Hmm. I doubt #1982 has anything to do with this. Internal clocks of classic parts are notoriously inaccurate. F_CPU varies with temperature, too. So handling a board could well move F_CPU. Best to measure F_CPU with a second MCU (say, by timing the period of a blinking LED) and use that frequency for creating a bootloader.

@mcuee
Copy link
Collaborator
mcuee commented May 15, 2025

@stefanrueger

I understand that the internal clock is not accurate. So the 1MHz internal clock is just for refernce.

But I have also done the tests with external 16MHz crystal for both ATmega2560 and ATmega328. The accuracy at room temperature should be pretty decent. The conclusion from my tests are the same.

  1. 2400 bps works fine
  2. 1200 bps does not work, same for 600 bps and 300 bps. I will try to increase the WDT time to see if that helps.
  3. 1800 bps, git main and this PR work better than avrdude 8.0 release.

@stefanrueger
Copy link
Collaborator Author

tests with external 16MHz crystal for both ATmega2560 and ATmega328 [fail below 2400 baud]

Come to think of that, there is an awful amount of tinkering in urclock.c to ensure the response is quite zippy. This includes lowering timeout values, which I suspect might prevent low baud rates below 4800 baud to work reliably (doubling the observed working well speed to be on the safe side). I am not sure I can (or want to) spend energy on making vvv low baud rates work for -c urclock . Your observation is useful @mcuee in that I'll either document this limitation or fix it before c8.1 release.

@mcuee
Copy link
Collaborator
mcuee commented May 16, 2025

ATmega328P (Arduino Uno CH340 Clone). Internal 8MHz clock, working up to 2400 bps.

C> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c usbasp -p m328p -U urboot:8mhz_9600baud_ce_ee -qq && echo OK
OK
C> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c urclock -P COM4 -b 9600 -p m328p

Avrdude_pr1987 done.  Thank you.
C> .\avrdude_git.exe -C .\avrdude_git.conf -c urclock -P COM4 -b 9600 -p m328p

Avrdude_git done.  Thank you.
C> .\avrdude80.exe -C .\avrdude80.conf -c urclock -P COM4 -b 9600 -p m328p

Avrdude80 done.  Thank you.
C> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c usbasp -p m328p -U urboot:8mhz_4800baud_ce_ee -qq && echo OK
OK
C> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c urclock -P COM4 -b 4800 -p m328p

Avrdude_pr1987 done.  Thank you.
C> .\avrdude_git.exe -C .\avrdude_git.conf -c urclock -P COM4 -b 4800 -p m328p

Avrdude_git done.  Thank you.
C> .\avrdude80.exe -C .\avrdude80.conf -c urclock -P COM4 -b 4800 -p m328p

Avrdude80 done.  Thank you.
C> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c usbasp -p m328p -U urboot:8mhz_2400baud_ce_ee -qq && echo OK
OK
C> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c urclock -P COM4 -b 2400 -p m328p

Avrdude_pr1987 done.  Thank you.
C> .\avrdude_git.exe -C .\avrdude_git.conf -c urclock -P COM4 -b 2400 -p m328p

Avrdude_git done.  Thank you.
C> .\avrdude80.exe -C .\avrdude80.conf -c urclock -P COM4 -b 2400 -p m328p

Avrdude80 done.  Thank you.
C> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c usbasp -p m328p -U urboot:8mhz_1200baud_ce_ee -qq && echo OK
OK
C> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c urclock -P COM4 -b 1200 -p m328p
Error: protocol expects OK byte 0x77 but got 0x67 in ur_readEF()
Error: unable to read signature data for part ATmega328P (rc = -1)
Error: unable to read signature data (rc = -1)
use -F to override this check
Error: protocol expects sync byte 0xa0 but got 0x40

Avrdude_pr1987 done.  Thank you.
C> .\avrdude_git.exe -C .\avrdude_git.conf -c urclock -P COM4 -b 1200 -p m328p
Warning: programmer is not responding
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
Warning: programmer is not responding

Avrdude_git done.  Thank you.
C> .\avrdude80.exe -C .\avrdude80.conf -c urclock -P COM4 -b 1200 -p m328p
Warning: programmer is not responding
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
Warning: programmer is not responding

Avrdude80 done.  Thank you.

@mcuee
Copy link
Collaborator
mcuee commented May 16, 2025

Same for 1MHz internal clock.

C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c usbasp -p m328p -U urboot:1mhz_9600baud_ce_ee -qq && echo OK
OK
C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c urclock -P COM4 -b 9600 -p m328p

Avrdude_pr1987 done.  Thank you.
C:\work\avr\avrdude_bin> .\avrdude_git.exe -C .\avrdude_git.conf -c urclock -P COM4 -b 9600 -p m328p

Avrdude_git done.  Thank you.
C:\work\avr\avrdude_bin> .\avrdude80.exe -C .\avrdude80.conf -c urclock -P COM4 -b 9600 -p m328p

Avrdude80 done.  Thank you.
C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c usbasp -p m328p -U urboot:1mhz_4800baud_ce_ee -qq && echo OK
OK
C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c urclock -P COM4 -b 4800 -p m328p

Avrdude_pr1987 done.  Thank you.
C:\work\avr\avrdude_bin> .\avrdude_git.exe -C .\avrdude_git.conf -c urclock -P COM4 -b 4800 -p m328p

Avrdude_git done.  Thank you.
C:\work\avr\avrdude_bin> .\avrdude80.exe -C .\avrdude80.conf -c urclock -P COM4 -b 4800 -p m328p

Avrdude80 done.  Thank you.
C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c usbasp -p m328p -U urboot:1mhz_2400baud_ce_ee -qq && echo OK
OK
C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c urclock -P COM4 -b 2400 -p m328p

Avrdude_pr1987 done.  Thank you.
C:\work\avr\avrdude_bin> .\avrdude_git.exe -C .\avrdude_git.conf -c urclock -P COM4 -b 2400 -p m328p

Avrdude_git done.  Thank you.
C:\work\avr\avrdude_bin> .\avrdude80.exe -C .\avrdude80.conf -c urclock -P COM4 -b 2400 -p m328p

Avrdude80 done.  Thank you.
C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c usbasp -p m328p -U urboot:1mhz_1200baud_ce_ee -qq && echo OK
OK
C:\work\avr\avrdude_bin> .\avrdude_pr1987.exe -C .\avrdude_pr1987.conf -c urclock -P COM4 -b 1200 -p m328p
Error: protocol expects OK byte 0x77 but got 0x67 in ur_readEF()
Error: unable to read signature data for part ATmega328P (rc = -1)
Error: unable to read signature data (rc = -1)
use -F to override this check
Error: protocol expects sync byte 0xa0 but got 0x40

Avrdude_pr1987 done.  Thank you.
C:\work\avr\avrdude_bin> .\avrdude_git.exe -C .\avrdude_git.conf -c urclock -P COM4 -b 1200 -p m328p
Error: protocol expects OK byte 0x77 but got 0x67 in ur_readEF()
Error: unable to read signature data for part ATmega328P (rc = -1)
Error: unable to read signature data (rc = -1)
use -F to override this check
Error: protocol expects sync byte 0xa0 but got 0x40

Avrdude_git done.  Thank you.
C:\work\avr\avrdude_bin> .\avrdude80.exe -C .\avrdude80.conf -c urclock -P COM4 -b 1200 -p m328p
Warning: programmer is not responding
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
Warning: programmer is not responding

Avrdude80 done.  Thank you.

@stefanrueger
Copy link
Collaborator Author

@mcuee Very, very cool experiments. This makes clear that -c urclock communications don't scale down to low baud rates. It looks like baud rate is the critical thing, not F_CPU. The timeouts in those -c urclock routines are absolute numbers in milliseconds and they actually do not consider the baud rate. Sending just a couple of handshake bytes at 2400 baud takes 8.333 ms minimum (20 bits/2400 baud) and the absolute timeouts chosen in urclock.c are in the area of 20 ms, whilst there is intermittent draining to get in sync with the autobaud protocol. So, yes, it looks like -c urboot needs more work for it to be able to handle low baud rates.

@stefanrueger
Copy link
Collaborator Author

PS: I no longer think upping WDT timeout will help

@mcuee
Copy link
Collaborator
mcuee commented May 20, 2025

Low baud rate issue of urboot has been addressed in PR #1993.

@mcuee
Copy link
Collaborator
mcuee commented May 20, 2025

@stefanrueger

This PR should be good to go as it fixes Issue #1986.

@stefanrueger stefanrueger merged commit dcf0b48 into avrdudes:main May 21, 2025
15 checks passed
@stefanrueger stefanrueger deleted the win-write-timeout branch May 21, 2025 06:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

avrdude hangs if valid serial port without serial programmer attached is specified
2 participants
0