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

Autobaud does not work on one ATmega328P board under Windows #5

Closed
mcuee opened this issue Nov 18, 2022 · 68 comments
Closed

Autobaud does not work on one ATmega328P board under Windows #5

mcuee opened this issue Nov 18, 2022 · 68 comments

Comments

@mcuee
Copy link

mcuee commented Nov 18, 2022

Interestingly I have a Arduino Nano clone with CH340 and Atmega328P (5V, 16MHz). It works well with 115200bps and even 1000000bps urboot hex file (tested with the variant (tested with ee_led+b5_fr_ce_ur_vbl) but not working with the autobaud firmware (no matter which baud rate I try). Kind of strange.

The autobaud FW works fine with my Uno Clone (ATmega328P, 5V, 16MHz) and Mega2560 Clone (ATmega2560, 5V, 16MHz).

Maybe this is not a real issue but rather board specific.

@mcuee
Copy link
Author

mcuee commented Nov 18, 2022

Run log:

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c usbasp
 -p m328p -U .\atmega328p_16mhz_1000000bps_ee_led+b5_fr_ce_ur_vbl.hex -qq && echo OK
OK
PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c urclock
 -P COM10 -p m328p -b 1000000 -xshowall

avrdude_pr1171v6: AVR device initialized and ready to accept instructions
0000ffffffff 0000-00-00 00.00  application 0 store 0 meta 0 boot 384 u7.7 weu-jpr-c vector 25 
(SPM_Ready) ATmega328P

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c usbasp
 -p m328p -U .\atmega328p_16mhz_115200bps_ee_led+b5_fr_ce_ur_vbl.hex -qq && echo OK
OK
PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c urclock
 -P COM10 -p m328p -b 115200 -xshowall

avrdude_pr1171v6: AVR device initialized and ready to accept instructions
0000ffffffff 0000-00-00 00.00  application 0 store 0 meta 0 boot 384 u7.7 weu-jpr-c vector 25 
(SPM_Ready) ATmega328P

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c usbasp
 -p m328p -U .\atmega328p_autobaud_ee_led+b5_fr_ce_ur_vbl.hex -qq && echo OK
OK
PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c urclock
 -P COM10 -p m328p -b 115200 -xshowall
avrdude_pr1171v6 warning: attempt 1 of 20: not in sync
avrdude_pr1171v6 warning: programmer is not responding; try, eg, -xdelay=200
avrdude_pr1171v6 warning: attempt 2 of 20: not in sync
...
avrdude_pr1171v6 warning: attempt 20 of 20: not in sync
avrdude_pr1171v6 error: unable to open programmer urclock on port COM10

avrdude_pr1171v6 done.  Thank you.

@mcuee
Copy link
Author

mcuee commented Nov 18, 2022

BTW, the autobaud FW seems to work pretty well for the ATmega2560, tested with some random baud rate.

Some do not work (300000, 600000, 800000, 900000; 120000, 160000, 170000, 190000, 210000, 310000, 350000).

The other baud rates (100000, 200000, 400000, 600000, 700000, 1000000; 115200, 125000, 144000, 150000, 250000, 327680, 330000; 110000, 130000, 140000, 150000, 180000) work well.

For lower baud rate it works even better -- 8k, 9k, 10k, 20k, 30k, 40k, 50k, 60k, 70k, 80k, 90k, 100k all work fine.

For even lower baud rate, it may have some issues. I can not get anything below 7700 to work.

@stefanrueger
Copy link
Owner

stefanrueger commented Nov 18, 2022

Arduino Nano clone with CH340 and Atmega328P

Could you try on your board

  • Pulling up rx (10k)
  • Pulling down tx (10k)
  • Doing both

I have been testing autobaud with FTDI FT232L (don't know whether the board has pullups/pulldowns) and with CH330N (the latter does not need external pullups/pulldowns - the chip does everything right). Both USB-TTL chips work well.

tested with some random baud rate not working with the autobaud firmware

Thanks. Yes, your tests gave the expected results b/c the poor little Atmel USARTs can only do certain baud rates: See my comment in the source code

urboot/src/urboot.c

Lines 530 to 580 in 77bec7c

* If AUTOBAUD is set then the generated bootloader will try to initialise the USART with the host
* communciation speed that the bootloader augurs from the first byte that is expected from the
* host at runtime; the code utilises that the lower 5 bit of that byte (0x30 = STK_GET_SYNC) are
* 0x10. AUTOBAUD is a bit of a misnomer, as urboot only offers 256 different baud rates. Moreover,
* these must be compatible with F_CPU, namely F_CPU/8, F_CPU/16, F_CPU/24, ..., F_CPU/2048. The
* set of compatible baud rates comes with a narrow tolerance band of +/-1.5%. As a consequence, a
* 16 MHz MCU can only safely communicate in the following 33 baud rate intervals (the last one is
* the union of 224 overlapping individual baud rate intervals):
*
* [1970000, 2030000] = [0.985*F_CPU/(8*1), 1.015*F_CPU/(8*1)], F_CPU = 16 MHz
* [ 985000, 1015000] = [0.985*F_CPU/(8*2), 1.015*F_CPU/(8*2)]
* [ 656667, 676667]
* [ 492500, 507500]
* [ 394000, 406000]
* [ 328333, 338333]
* [ 281429, 290000]
* [ 246250, 253750]
* [ 218889, 225556]
* [ 197000, 203000]
* [ 179091, 184545]
* [ 164167, 169167]
* [ 151538, 156154]
* [ 140714, 145000]
* [ 131333, 135333]
* [ 123125, 126875]
* [ 115882, 119412] <-- 115200 baud is out of specs for a 16 MHz MCU
* [ 109444, 112778]
* [ 103684, 106842]
* [ 98500, 101500]
* [ 93810, 96667]
* [ 89545, 92273]
* [ 85652, 88261]
* [ 82083, 84583]
* [ 78800, 81200]
* [ 75769, 78077]
* [ 72963, 75185]
* [ 70357, 72500]
* [ 67931, 70000]
* [ 65667, 67667]
* [ 63548, 65484]
* [ 61563, 63437]
* [ 7695, 61515] = [0.985*F_CPU/(8*256), 1.015*F_CPU/(8*33)]
*
* Notice that the ubiquituous 115,200 baud is actually out of specs for popular 16 MHz MCU builds.
* In practice, tolerances of up to 2.5% may actually work, though that is not recommended. Also
* signal noise and cable length plays a big role in whether communication is stable. The maximum
* permitted baud rate of F_CPU/8 may not always work. Above set of baud rates is generated by
* setting UART2X to 1, the default when AUTOBAUD has been set. Setting UART2X=0 saves 4 bytes code
* and has a higher tolerance of 2% but will restrict available baud rates to half of above baud
* rates: F_CPU/16, F_CPU/32, F_CPU/48, ..., F_CPU/4096. In order for the AUTOBAUD feature to work,
* urboot will need to know the rx line of the UART so it can measure the host's baud rate.

Also see the legend for a autobaud detection that details the set of baud rates the AVRs can do: f_cpu/8n using discrete divisors, n = 1, 2, ..., 256

legend-autobaud

@stefanrueger stefanrueger changed the title Autoboot does not work on one ATmega328P board Autobaud does not work on one ATmega328P board Nov 18, 2022
@stefanrueger
Copy link
Owner

@MCUdude - do you have similar experiences? Which USB-TTL chips have you used?

@MCUdude
Copy link

MCUdude commented Nov 18, 2022

@stefanrueger I've only tried it on this custom DIP-40 board, and it worked all the way from 9600 and up to 2M. This board uses a CH340C USB to serial chip.

Never experienced any issues

@stefanrueger
Copy link
Owner

OK, let's see what @mcuee's tests bring about. If it's pullups/pulldowns then this could be done by software at a cost of 2 bytes each, though my favourite build, the 510 byte weudhprac dual boot template bootloader, would then be 2 bytes over. I always thought that the serial hardware (ie, the USB-TTL chip) ought to ensure rx is pulled high at receiver side.

@MCUdude
Copy link

MCUdude commented Nov 18, 2022

I've tested the autobaud functionality using a bare-bone ATtiny2313 breakout board without any pull-ups or series resistors on the RX and TX lines. I can confirm that autobaud works perfectly using a CH340G USB to serial adapter, a CP2104 USB to serial adapter from Adafruit and an FT232RL based one from Sparkfun.

@stefanrueger
Copy link
Owner

Brilliant list of USB-TTL tests. Adding my CH330N, we have already 4 chips under our belt.

@mcuee
Copy link
Author

mcuee commented Nov 19, 2022

I have no issues with the CH340G on my Arduino Mega Clone (ATmega2560, 5V, 16MHz). The Arduino Nano Clone which has issues (ATmega328P, 5V, 16MHz) is also using the same CH340G chipset. I will try to test the pull-up/pull-down test later as I am not able to test now.

@mcuee
Copy link
Author

mcuee commented Nov 19, 2022

I can reproduce the issue on another Arduini Nano clone with the same CH340G design but using ATmega168P (5V, 16MHz). So the root cause should be the same. I will try to play with the pull-up/pull-down next week.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c usbasp
 -p m168p -U .\atmega168p_16mhz_115200bps_ee_lednop_fr_ce_ur_vbl.hex -qq && echo OK
OK
PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c urclock
 -P COM10 -b 115200 -p m168p -xshowall

avrdude_pr1171v6: AVR device initialized and ready to accept instructions
0000ffffffff 0000-00-00 00.00  application 0 store 0 meta 0 boot 384 u7.7 weu-jpr-c vector 25 
(SPM_Ready) ATmega168P

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c usbasp
 -p m168p -U .\atmega168p_autobaud_ee_lednop_fr_ce_ur_vbl.hex -qq && echo OK
OK

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c urclock
 -P COM10 -b 115200 -p m168p -xshowall
avrdude_pr1171v6 warning: attempt 1 of 20: not in sync
avrdude_pr1171v6 warning: programmer is not responding; try, eg, -xdelay=200
...
avrdude_pr1171v6 warning: attempt 19 of 20: not in sync
avrdude_pr1171v6 warning: programmer is not responding; try, eg, -xdelay=200
avrdude_pr1171v6 warning: attempt 20 of 20: not in sync
avrdude_pr1171v6 error: unable to open programmer urclock on port COM10

avrdude_pr1171v6 done.  Thank you.

@mcuee
Copy link
Author

mcuee commented Nov 19, 2022

BTW, the autobaud FW seems to work pretty well for the ATmega2560, tested with some random baud rate.
Some do not work (300000, 600000, 800000, 900000; 120000, 160000, 170000, 190000, 210000, 310000, 350000).
For even lower baud rate, it may have some issues. I can not get anything below 7700 to work.

Yes, your tests gave the expected results b/c the poor little Atmel USARTs can only do certain baud rates: See my comment in the source code

I tested the Arduino Uno Clone using ATmega328P and ATmega16U2 (USB to Serial FW, using USB CDC-ACM class), it can work with even more baud rates than the CH340G. It works well with the above baud rate (300000, 600000, 800000, 900000; 120000, 160000, 170000, 190000, 210000, 310000, 350000).

As expected, it does not work with anything <=7700 bps (7800bps works).

Official Arduino Uno and Arduino Mega 2560 use ATmega16U2 USB to Serial FW. So urboot autobaud FW should work for many users of such boards.

Official Arduino Nano uses FT232RL

Most of the Uno and Nano clones use CH340 serials.

Note: I am not exactly sure if the ATmega16U2 really talks to the ATmega328P using the baudrate specified though. But at least I can not get other baud rate to work when I use the 115200bps urboot firmware.

Edit: I think it does based on the ATmega16U2 FW source codes from Arduino.
https://github.com/arduino/ArduinoCore-avr/blob/master/firmwares/atmegaxxu2/arduino-usbserial/Arduino-usbserial.c#L209

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c urclock -P COM3
 -b 300000 -p m328p -xshowall

avrdude_pr1171v6: AVR device initialized and ready to accept instructions
0000ffffffff 0000-00-00 00.00  application 0 store 0 meta 0 boot 384 u7.7 weu-jprac vector 25 (SPM_Ready) ATmega328P

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c urclock -P COM3
 -b 600000 -p m328p -xshowall

avrdude_pr1171v6: AVR device initialized and ready to accept instructions
0000ffffffff 0000-00-00 00.00  application 0 store 0 meta 0 boot 384 u7.7 weu-jprac vector 25 (SPM_Ready) ATmega328P
PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c urclock -P COM3
 -b 800000 -p m328p -xshowall

avrdude_pr1171v6: AVR device initialized and ready to accept instructions
0000ffffffff 0000-00-00 00.00  application 0 store 0 meta 0 boot 384 u7.7 weu-jprac vector 25 (SPM_Ready) ATmega328P
PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c urclock -P COM3
 -b 900000 -p m328p -xshowall

avrdude_pr1171v6: AVR device initialized and ready to accept instructions
0000ffffffff 0000-00-00 00.00  application 0 store 0 meta 0 boot 384 u7.7 weu-jprac vector 25 (SPM_Ready) ATmega328P
PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c urclock -P COM3
 -b 120000 -p m328p -xshowall

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c urclock -P COM3
 -b 160000 -p m328p -xshowall

avrdude_pr1171v6: AVR device initialized and ready to accept instructions
0000ffffffff 0000-00-00 00.00  application 0 store 0 meta 0 boot 384 u7.7 weu-jprac vector 25 (SPM_Ready) ATmega328P

avrdude_pr1171v6: AVR device initialized and ready to accept instructions
0000ffffffff 0000-00-00 00.00  application 0 store 0 meta 0 boot 384 u7.7 weu-jprac vector 25 (SPM_Ready) ATmega328P
PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c urclock -P COM3
 -b 170000 -p m328p -xshowall

avrdude_pr1171v6: AVR device initialized and ready to accept instructions
0000ffffffff 0000-00-00 00.00  application 0 store 0 meta 0 boot 384 u7.7 weu-jprac vector 25 (SPM_Ready) ATmega328P
PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c urclock -P COM3
 -b 190000 -p m328p -xshowall

avrdude_pr1171v6: AVR device initialized and ready to accept instructions
0000ffffffff 0000-00-00 00.00  application 0 store 0 meta 0 boot 384 u7.7 weu-jprac vector 25 (SPM_Ready) ATmega328P
PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c urclock -P COM3
 -b 210000 -p m328p -xshowall

avrdude_pr1171v6: AVR device initialized and ready to accept instructions
0000ffffffff 0000-00-00 00.00  application 0 store 0 meta 0 boot 384 u7.7 weu-jprac vector 25 (SPM_Ready) ATmega328P
PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c urclock -P COM3
 -b 310000 -p m328p -xshowall

avrdude_pr1171v6: AVR device initialized and ready to accept instructions
0000ffffffff 0000-00-00 00.00  application 0 store 0 meta 0 boot 384 u7.7 weu-jprac vector 25 (SPM_Ready) ATmega328P
PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c urclock -P COM3
 -b 350000 -p m328p -xshowall

avrdude_pr1171v6: AVR device initialized and ready to accept instructions
0000ffffffff 0000-00-00 00.00  application 0 store 0 meta 0 boot 384 u7.7 weu-jprac vector 25 (SPM_Ready) ATmega328P

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c urclock -P COM3
 -b 7800 -p m328p -xshowall

avrdude_pr1171v6: AVR device initialized and ready to accept instructions
0000ffffffff 0000-00-00 00.00  application 0 store 0 meta 0 boot 384 u7.7 weu-jprac vector 25 (SPM_Ready) ATmega328P

@mcuee
Copy link
Author

mcuee commented Nov 19, 2022

BTW, for the two Nano clones, picoboot does not work either.

I tested another Uno clone with ATmega328P and CH340G, the test results are the same as the Mega2560 clone with CH340G. This time I pushed it up to 3,000,000 bps (12MHz clock for the CH340G) and the autobaud urboot FW works fine.

So there are some design differences on these Nano clones compared to the Uno clones (both use CH340G) which make autobaud not working.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c urclock -P COM5
 -b 3000000 -p m328p -xshowall

avrdude_pr1171v6: AVR device initialized and ready to accept instructions
0000ffffffff 0000-00-00 00.00  application 0 store 0 meta 0 boot 384 u7.7 weu-jprac vector 25 
(SPM_Ready) ATmega328P

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c urclock -P COM5
 -b 3000000 -p m328p -U .\hex\Blink.ino.standard.hex

avrdude_pr1171v6: AVR device initialized and ready to accept instructions
avrdude_pr1171v6: device signature = 0x1e950f (probably m328p)
avrdude_pr1171v6: Note: flash memory has been specified, an erase cycle will be performed.
                  To disable this feature, specify the -D option.
avrdude_pr1171v6: erasing chip
avrdude_pr1171v6: reading input file .\hex\Blink.ino.standard.hex for flash
                  with 924 bytes in 1 section within [0, 0x39b]
                  using 8 pages and 100 pad bytes
avrdude_pr1171v6: preparing flash input for device bootloader
avrdude_pr1171v6: writing 924 bytes flash ...

Writing | ################################################## | 100% 1.09 s

avrdude_pr1171v6: 924 bytes of flash written
avrdude_pr1171v6: verifying flash memory against .\hex\Blink.ino.standard.hex

Reading | ################################################## | 100% 1.03 s

avrdude_pr1171v6: 924 bytes of flash verified

avrdude_pr1171v6 done.  Thank you.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c urclock
 -P COM5 -b 3000000 -p m328p -xshowall

avrdude_pr1171v6: AVR device initialized and ready to accept instructions
0000ffffffff 2022-06-10 21.21 .\hex\Blink.ino.standard.hex 924 store 31420 meta 40 boot 384 u7.7 
weu-jprac vector 25 (SPM_Ready) ATmega328P

@mcuee
Copy link
Author

mcuee commented Nov 19, 2022

I look at the design of the Nano clones and the design may be quite similar to this one.
https://oshwlab.com/eeoj/arduino-nano-clone-ch340

The TX/RX LED driving circuitry may be the problem. --> but my clones have 1k Ohm and not 330 ohm for the LED driving.
Note: TX LED circuitry is not correct. RP1B (1k ohm) should be moved to the left of the TX LED.

arduino_nano_ch340

Just a comparison, official Arduino Nano has two FT232RL CBUS pins driving the TX/RX LEDs.
https://www.arduino.cc/en/uploads/Main/Arduino_Nano-Rev3.2-SCH.pdf

@mcuee
Copy link
Author

mcuee commented Nov 19, 2022

Official Arduino Uno Rev3 schematics: two GPIO pins of the ATmega16U2 (USB to Serial chip) are driving the TX/RX LEDs.
https://www.arduino.cc/en/uploads/Main/Arduino_Uno_Rev3-schematic.pdf

Typical Arduino Uno clone CH340G schematics: the TX/R LEDs driver circuitry is actually similar to my two Nano clones.
http://electronoobs.com/eng_arduino_tut31_sch3.php

Note: TX LED driving is not so correct, R8 (1k ohm) should be moved to the right of the TX LED.
arduino_uno_scheamtic_ch340

Typical Arduino Uno clone FT232RL schematics: using CBUS pins to drive TX/RX LEDs, similar to official Arduino Nano design.
https://oshwlab.com/nata.iiinx88/arduino-uno

@MCUdude
Copy link

MCUdude commented Nov 19, 2022

I've alto tested autobaud on a CH330N with the following schematic. It works perfectly fine. The only difference is that all resistors are 1k, except for R6 which is 10k. It's an older design, so I don't remember why I didn't use 10k for R6. Also, the power flags are either 5V or 3.3V, but for some reason, the text above the power symbols are gone

image

@mcuee
Copy link
Author

mcuee commented Nov 19, 2022

Interestingly I have another ATmega328P Nano clone (Type C, CH340G, 5V,16MHz) which works well with the autobaud urboot FW (up to 2,490,000bps). I should be able to compare the differences of the different Nano clones next week.

@mcuee
Copy link
Author

mcuee commented Nov 19, 2022

@MCUdude
I think your LED driving circuitry is correct (using 4 x 1k ohm resistor). The clone design may not be correct. Take note the difference in the RX and TX LED design.

It should be using the following sequence.
CH330N/340G TXD --> TX LED driving --> 1k ohm --> ATmega328P RXD
CH330N/340G RXD <-- 1k ohm <--RX LED driving <-- ATmega328P TXD

But the clone design makes a mistake for either the RX or TX LED driving.

@mcuee
Copy link
Author

mcuee commented Nov 19, 2022

Further debugging shows an interesting result.

I use another CH340 USB to TTL converter (COM13) to connect to the Nano 168P clone -- without the DTR RC reset add-on -- so I have to use manual reset (by pressing the button after the command). But now it works.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c usbasp -p m168p
 -U .\atmega168p_autobaud_ee_lednop_fr_ce_ur_vbl.hex -qq && echo OK
OK
PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c urclock
 -P COM13  -p m168p -xshowall

avrdude_pr1171v6: AVR device initialized and ready to accept instructions
0000ffffffff 0000-00-00 00.00  application 0 store 0 meta 0 boot 384 u7.7 weu-jprac vector 25 (SPM_Ready) ATmega168P

Then I find out I do not need the external USB to TTL converter either. Rather I can use manually reset and it will work with the on-board CH340G (COM10). This points out some subtle difference between the autobaud FW and the normal FW (no need the manual reset). It could be the reset times out too fast before the baud rate negotiation.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c urclock -P COM10
 -p m168p -xshowall

avrdude_pr1171v6: AVR device initialized and ready to accept instructions
0000ffffffff 0000-00-00 00.00  application 0 store 0 meta 0 boot 384 u7.7 weu-jprac vector 25 (SPM_Ready) ATmega168P

@mcuee
Copy link
Author

mcuee commented Nov 19, 2022

It could be the reset times out too fast before the baud rate negotiation

I am thinking that #7 can help in this situation but I am not so sure if it is in scope for urboot or not. I can see that optiboot_x and optiboot_dx has the 8s delay option for chips without proper auto reset.

@mcuee
Copy link
Author

mcuee commented Nov 19, 2022

I am also testing with a ATmega32A breakout board (5V, 8MHz crystal, CH340G) without the proper DTR RC reset circuitry. It does have a reset button. I can get the 115,200 bps and 1,000,000bps urboot FW working well by pressing the reset button but not the urboot autobaud FW.

Then I tested another ATmega8A breakout board (5V, 16MHz crystal) with an external CH340G USB to TTL converter, I am able to use the reset buttone to get autobaud urboot FW to work.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c usbasp -p m8a
 -U .\atmega8a_autobaud_ee_lednop_fr_ce_ur_vbl.hex -qq && echo OK
OK
PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c urclock
 -P COM13 -p m8a -xshowall

avrdude_pr1171v6: AVR device initialized and ready to accept instructions
0000ffffffff 0000-00-00 00.00  application 0 store 0 meta 0 boot 384 u7.7 weu-jprac vector 18 
(SPM_RDY) ATmega8A

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c urclock
 -P COM13 -b 500000 -p m8a -xshowall

avrdude_pr1171v6: AVR device initialized and ready to accept instructions
0000ffffffff 0000-00-00 00.00  application 0 store 0 meta 0 boot 384 u7.7 weu-jprac vector 18 (SPM_RDY) ATmega8A
PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v6 -C .\avrdude_pr1171v6.conf -c urclock
 -P COM13 -b 1000000 -p m8a -xshowall

avrdude_pr1171v6: AVR device initialized and ready to accept instructions
0000ffffffff 0000-00-00 00.00  application 0 store 0 meta 0 boot 384 u7.7 weu-jprac vector 18 (SPM_RDY) ATmega8A

@stefanrueger
Copy link
Owner

It could be the reset times out too fast before the baud rate negotiation.

Baud rate detection is fast. The following sequence is needed, tough:

  1. Board resets and gets out of reset; autoboot waits for a sign of life on the rx line
  2. Avrdude sends the first GET_SYNC after the board has come out of reset (see -xdelay=...)
  3. Bootloader is still active and has not timed out (-xdelay value must be less than the WDT timeout)

For manual reset on a board without reset-circuit compile with a WDTO=8S timeout. Upload as follows

  1. Reset the board manually; then
  2. Start avrdude (avrdude plucks the RTS/DTR lines, but since the board does not have a reset circuit no reset happens)

For automated reset it could be that the board takes a loooong time to get out of reset, by which time avrdude had already sent the GET_SYNC; investigate the SUT_CKSEL values in the fuses, or check whether there are particularly large caps in the reset circuit. In this case -xdelay=50 or similar might be your friend.

Are you in a position to compile the bootloaders? Try uncommenting the line

// UR_PORT(RX) |= UR_BV(RX); // Pullup rx (should be done by HW, really)

This creates a software pullup for rx.

Compile by either

make MCU=atmega328p F_CPU=16000000L WDTO=1S AUTOBAUD=1 LED=AtmelPB5 DUAL=0 EEPROM=1 URPROTOCOL=1 FRILLS=7 MOVETO=atmega328p_autobaud_ee_led+b5_fr_ce_ur_vbl

or (if you don't get urboot-gcc to work)

./avr-toolchain/5.4.0/bin/avr-gcc -DSTART=0x7e80UL -DRJMPWP=0xcfd6 -Wl,--section-start=.text=0x7e80 -Wl,--section-start=.version=0x7ffa -D_urboot_AVAILABLE=22 -g -Wundef -Wall -Os -fno-split-wide-types -mrelax -mmcu=atmega328p -DF_CPU=16000000L -Wno-clobbered -DWDTO=1S -DAUTOBAUD=1 -DLED=AtmelPB5 -DDUAL=0 -DEEPROM=1 -DURPROTOCOL=1 -DFRILLS=7 -DVBL=1 -Wl,--relax -nostartfiles -nostdlib -o atmega328p_16mhz_auto_115200bps_ev1f7_ledPB5_wdto1s_ur.elf urboot.c
./avr-toolchain/5.4.0/bin/avr-objcopy -j .text -j .data -j .version --set-section-flags .version=alloc,load -O ihex atmega328p_16mhz_auto_115200bps_ev1f7_ledPB5_wdto1s_ur.elf atmega328p_16mhz_auto_115200bps_ev1f7_ledPB5_wdto1s_ur.hex
./avr-toolchain/5.4.0/bin/avr-objdump -h -S atmega328p_16mhz_auto_115200bps_ev1f7_ledPB5_wdto1s_ur.elf > atmega328p_16mhz_auto_115200bps_ev1f7_ledPB5_wdto1s_ur.lst
mv atmega328p_16mhz_auto_115200bps_ev1f7_ledPB5_wdto1s_ur.hex atmega328p_autobaud_ee_led+b5_fr_ce_ur_vbl.hex
-- 376 384 u7.7 weu-jPrac atmega328p_autobaud_ee_led+b5_fr_ce_ur_vbl.hex

Must be on Linux, I am afraid.

@mcuee
Copy link
Author

mcuee commented Nov 20, 2022

I should be able to build any command line application with proper build instruction under Linux/macOS/Windows. :)
I like cross-platform open source projects (eg: libusb, hidapi, libftdi, pyusb, OpenOCD and avrdude). And I have physical machines to run all three OS since 2011. I also run FreeBSD/OpenBSD/NetBSD under VirtualBox but I am less familiar with the BSDs.

I will give it a try to build custom urboot FW.

Going back to the initial issues, I think autobaud FW is really working well. It is the RESET circuitry which can be the problem. The pull-up/pulll-down on the TX/RX lines does not seem to be the issue here.

@stefanrueger
Copy link
Owner

@mcuee Impressive array of Cross OS experience. Also would be interested which tips you have to better describe how to build the .hex files.

For the time being, here is a hex file with software rx pullup (sbi 0x0b, 0): atmega328p_autobaud_ee_led+b5_fr_ce_ur_vbl.hex

See if that does the trick.

@mcuee
Copy link
Author

mcuee commented Nov 20, 2022

For the time being, here is a hex file with software rx pullup (sbi 0x0b, 0): atmega328p_autobaud_ee_led+b5_fr_ce_ur_vbl.hex
See if that does the trick.

Tested and it does not work as expected -- reset is the problem and not rx/tx pin pull-up/pull-down. I still need to manually press the reset button after the avrdude command.

@mcuee
Copy link
Author

mcuee commented Nov 20, 2022

First hurdle to build the hex file is with the toolchain.

mcuee@UbuntuSwift3:~/build/avr/urboot/src$ make MCU=atmega328p F_CPU=16000000L WDTO=8S AUTOBAUD=1 LED=AtmelPB5 DUAL=0 EEPROM=1 URPROTOCOL=1 FRILLS=7 MOVETO=atmega328p_autobaud_ee_led+b5_fr_ce_ur_vbl
./avr-toolchain/5.4.0/bin/avr-gcc -DSTART=0x7c00UL -DRJMPWP=0xcfff -Wl,--section-start=.text=0x7c00 -Wl,--section-start=.version=0x7ffa -g -Wundef -Wall -Os -fno-split-wide-types -mrelax -mmcu=atmega328p -DF_CPU=16000000L -Wno-clobbered -DWDTO=8S -DAUTOBAUD=1 -DLED=AtmelPB5 -DDUAL=0 -DEEPROM=1 -DURPROTOCOL=1 -DFRILLS=7 -DVBL=0 -Wl,--relax -nostartfiles -nostdlib -o atmega328p_16mhz_auto_115200bps_ef7_ledPB5_wdto8s_ur.elf urboot.c
avr-gcc: fatal error: -fuse-linker-plugin, but liblto_plugin.so not found
compilation terminated.
./avr-toolchain/5.4.0/bin/avr-objcopy -j .text -j .data -j .version --set-section-flags .version=alloc,load -O ihex atmega328p_16mhz_auto_115200bps_ef7_ledPB5_wdto8s_ur.elf atmega328p_16mhz_auto_115200bps_ef7_ledPB5_wdto8s_ur.hex
./avr-toolchain/5.4.0/bin/avr-objcopy: 'atmega328p_16mhz_auto_115200bps_ef7_ledPB5_wdto8s_ur.elf': No such file
make: *** [Makefile:719: atmega328p_16mhz_auto_115200bps_ef7_ledPB5_wdto8s_ur.hex] Error 1

mcuee@UbuntuSwift3:~/build/avr/urboot/src$ ./avr-toolchain/5.4.0/bin/avr-gcc -DSTART=0x7e80UL -DRJMPWP=0xcfd6 -Wl,--section-start=.text=0x7e80 -Wl,--section-start=.version=0x7ffa -D_urboot_AVAILABLE=22 -g -Wundef -Wall -Os -fno-split-wide-types -mrelax -mmcu=atmega328p -DF_CPU=16000000L -Wno-clobbered -DWDTO=1S -DAUTOBAUD=1 -DLED=AtmelPB5 -DDUAL=0 -DEEPROM=1 -DURPROTOCOL=1 -DFRILLS=7 -DVBL=1 -Wl,--relax -nostartfiles -nostdlib -o atmega328p_16mhz_auto_115200bps_ev1f7_ledPB5_wdto1s_ur.elf urboot.c
urboot.c:1356:19: warning: call-clobbered register used for global register variable
 register uint16_t zaddress asm("r30");
                   ^
urboot.c:1356:19: warning: call-clobbered register used for global register variable
avr-gcc: fatal error: -fuse-linker-plugin, but liblto_plugin.so not found
compilation terminated.


@mcuee
Copy link
Author

mcuee commented Nov 20, 2022

But no problem for me -- I can use the gcc-avr and avr-libc provded by Ubuntu 20.04 to build the hex file.

mcuee@UbuntuSwift3:~/build/avr/urboot/src$ avr-gcc -DSTART=0x7e80UL -DRJMPWP=0xcfd6 -Wl,--section-start=.text=0x7e80 -Wl,--section-start=.version=0x7ffa -D_urboot_AVAILABLE=22 -g -Wundef -Wall -Os -fno-split-wide-types -mrelax -mmcu=atmega328p -DF_CPU=16000000L -Wno-clobbered -DWDTO=8S -DAUTOBAUD=1 -DLED=AtmelPB5 -DDUAL=0 -DEEPROM=1 -DURPROTOCOL=1 -DFRILLS=7 -DVBL=1 -Wl,--relax -nostartfiles -nostdlib -o atmega328p_16mhz_auto_115200bps_ev1f7_ledPB5_wdto1s_ur.elf urboot.c
urboot.c:1356:19: warning: call-clobbered register used for global register variable
 register uint16_t zaddress asm("r30");
                   ^
urboot.c:1356:19: warning: call-clobbered register used for global register variable
mcuee@UbuntuSwift3:~/build/avr/urboot/src$ avr-objcopy -j .text -j .data -j .version --set-section-flags .version=alloc,load -O ihex atmega328p_16mhz_auto_115200bps_ev1f7_ledPB5_wdto1s_ur.elf atmega328p_16mhz_auto_115200bps_ev1f7_ledPB5_wdto1s_ur.hex
mcuee@UbuntuSwift3:~/build/avr/urboot/src$ avr-objdump -h -S atmega328p_16mhz_auto_115200bps_ev1f7_ledPB5_wdto1s_ur.elf > atmega328p_16mhz_auto_115200bps_ev1f7_ledPB5_wdto1s_ur.lst
mcuee@UbuntuSwift3:~/build/avr/urboot/src$ mv atmega328p_16mhz_auto_115200bps_ev1f7_ledPB5_wdto1s_ur.hex atmega328p_autobaud_ee_led+b5_fr_ce_ur_vbl.hex

@mcuee
Copy link
Author

mcuee commented Nov 20, 2022

The result hex file works perfectly. I do not need to manually press the button any more.

Strange thing is that it seems to work with any baudrate >=400bps, even rediculously high numbers above the crystal frequency (16MHz). Maybe it is not really true. Probaly Linux just caps the max baud rate it uses.

mcuee@UbuntuSwift3:~/build/avr/urboot/src$ ls *.hex
atmega328p_autobaud_ee_led+b5_fr_ce_ur_vbl.hex
mcuee@UbuntuSwift3:~/build/avr/urboot/src$ avrdude -c usbasp -p m328p
 -U ./atmega328p_autobaud_ee_led+b5_fr_ce_ur_vbl.hex -qq && echo OK
OK

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ avrdude -c urclock -P /dev/ttyUSB0 -b 115200 -p m328p -xshowall

avrdude: AVR device initialized and ready to accept instructions
ffffffffffff 0000-00-00 00.00  application 0 store 0 meta 0 boot 384 u7.7 weu-jPrac vector 25 
(SPM_Ready) ATmega328P
mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ avrdude -c urclock -P /dev/ttyUSB0 -b 115200 -p m328p
 -U ./blink_m328p.hex 

avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e950f (probably m328p)
avrdude: Note: flash memory has been specified, an erase cycle will be performed.
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file ./blink_m328p.hex for flash
         with 1030 bytes in 1 section within [0, 0x405]
         using 9 pages and 122 pad bytes
avrdude: preparing flash input for device bootloader
avrdude: writing 1030 bytes flash ...

Writing | ################################################## | 100% 0.21 s 

avrdude: 1030 bytes of flash written
avrdude: verifying flash memory against ./blink_m328p.hex

Reading | ################################################## | 100% 0.13 s 

avrdude: 1030 bytes of flash verified

avrdude done.  Thank you.

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ avrdude -c urclock -P /dev/ttyUSB0 -b 115200 -p m328p -xshowall

avrdude: AVR device initialized and ready to accept instructions
ffffffffffff 2022-11-20 12.58 blink_m328p.hex 1030 store 31327 meta 27 boot 384 u7.7 weu-jPrac vector 25 
(SPM_Ready) ATmega328P

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ avrdude -c urclock -P /dev/ttyUSB0 -b 500000 -p m328p -xshowall

avrdude: AVR device initialized and ready to accept instructions
ffffffffffff 2022-11-20 12.58 blink_m328p.hex 1030 store 31327 meta 27 boot 384 u7.7 weu-jPrac vector 25 
(SPM_Ready) ATmega328P

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ avrdude -c urclock -P /dev/ttyUSB0 -b 16000000 -p m328p -xshowall

avrdude: AVR device initialized and ready to accept instructions
ffffffffffff 2022-11-20 12.58 blink_m328p.hex 1030 store 31327 meta 27 boot 384 u7.7 weu-jPrac vector 25 
(SPM_Ready) ATmega328P

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ avrdude -c urclock -P /dev/ttyUSB0 -b 20000000 -p m328p -xshowall

avrdude: AVR device initialized and ready to accept instructions
ffffffffffff 2022-11-20 12.58 blink_m328p.hex 1030 store 31327 meta 27 boot 384 u7.7 weu-jPrac vector 25 
(SPM_Ready) ATmega328P

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ avrdude -c urclock -P /dev/ttyUSB0 -b 400 -p m328p -xshowall

avrdude: AVR device initialized and ready to accept instructions
ffffffffffff 2022-11-20 12.58 blink_m328p.hex 1030 store 31327 meta 27 boot 384 u7.7 weu-jPrac vector 25 
(SPM_Ready) ATmega328P

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ avrdude -c urclock -P /dev/ttyUSB0 -b 200000000 -p m328p -xshowall

avrdude: AVR device initialized and ready to accept instructions
ffffffffffff 2022-11-20 12.58 blink_m328p.hex 1030 store 31327 meta 27 boot 384 u7.7 weu-jPrac vector 25
 (SPM_Ready) ATmega328P
mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ avrdude -c urclock -P /dev/ttyUSB0 -b 2000000000 -p m328p -xshowall

avrdude: AVR device initialized and ready to accept instructions
ffffffffffff 2022-11-20 12.58 blink_m328p.hex 1030 store 31327 meta 27 boot 384 u7.7 weu-jPrac vector 25 
(SPM_Ready) ATmega328P
mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ avrdude -c urclock -P /dev/ttyUSB0 -b 20000000000 -p m328p -xshowall

avrdude: AVR device initialized and ready to accept instructions
ffffffffffff 2022-11-20 12.58 blink_m328p.hex 1030 store 31327 meta 27 boot 384 u7.7 weu-jPrac vector 25 
(SPM_Ready) ATmega328P
mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ avrdude -c urclock -P /dev/ttyUSB0 -b 200000000000 -p m328p -xshowall

avrdude: AVR device initialized and ready to accept instructions
ffffffffffff 2022-11-20 12.58 blink_m328p.hex 1030 store 31327 meta 27 boot 384 u7.7 weu-jPrac vector 25 
(SPM_Ready) ATmega328P

@mcuee
Copy link
Author

mcuee commented Nov 20, 2022

It also works fine with Windows -- down to 7800 bps and up to 3,000,000bps.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v7 -C .\avrdude_pr1171v7.conf -c urclock
 -P COM10 -b 3000000 -p m328p -xshowall

avrdude_pr1171v7: AVR device initialized and ready to accept instructions
0000ffffffff 2022-11-20 12.58 blink_m328p.hex 1030 store 31327 meta 27 boot 384 u7.7 weu-jPrac vector 25 
(SPM_Ready) ATmega328P

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v7 -C .\avrdude_pr1171v7.conf -c urclock
 -P COM10 -b 7800 -p m328p -U .\zeros31KB.hex -F

avrdude_pr1171v7: AVR device initialized and ready to accept instructions
avrdude_pr1171v7: device signature = 0x1e950f (probably m328p)
avrdude_pr1171v7: Note: flash memory has been specified, an erase cycle will be performed.
                  To disable this feature, specify the -D option.
avrdude_pr1171v7: erasing chip
avrdude_pr1171v7: reading input file .\zeros31KB.hex for flash
                  with 31744 bytes in 1 section within [0, 0x7bff]
                  using 248 pages and 0 pad bytes
avrdude_pr1171v7 warning: not patching jmp to application as input does not start with a vector table
avrdude_pr1171v7: preparing flash input for device bootloader
avrdude_pr1171v7: writing 31744 bytes flash ...

Writing | ################################################## | 100% 46.32 s

avrdude_pr1171v7: 31744 bytes of flash written
avrdude_pr1171v7: verifying flash memory against .\zeros31KB.hex

Reading | ################################################## | 100% 44.34 s

avrdude_pr1171v7: 31744 bytes of flash verified

avrdude_pr1171v7 done.  Thank you.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v7 -C .\avrdude_pr1171v7.conf -c urclock
 -P COM10 -b 3000000 -p
 m328p -U .\zeros31KB.hex -F

avrdude_pr1171v7: AVR device initialized and ready to accept instructions
avrdude_pr1171v7: device signature = 0x1e950f (probably m328p)
avrdude_pr1171v7: Note: flash memory has been specified, an erase cycle will be performed.
                  To disable this feature, specify the -D option.
avrdude_pr1171v7: erasing chip
avrdude_pr1171v7: reading input file .\zeros31KB.hex for flash
                  with 31744 bytes in 1 section within [0, 0x7bff]
                  using 248 pages and 0 pad bytes
avrdude_pr1171v7 warning: not patching jmp to application as input does not start with a vector table
avrdude_pr1171v7: preparing flash input for device bootloader
avrdude_pr1171v7: writing 31744 bytes flash ...

Writing | ################################################## | 100% 32.20 s

avrdude_pr1171v7: 31744 bytes of flash written
avrdude_pr1171v7: verifying flash memory against .\zeros31KB.hex

Reading | ################################################## | 100% 30.42 s

avrdude_pr1171v7: 31744 bytes of flash verified

avrdude_pr1171v7 done.  Thank you.


@mcuee
Copy link
Author

mcuee commented Nov 20, 2022

mcuee@UbuntuSwift3:~/build/avr/urboot/src$ ./hexls *.hex
358 512 u7.7 weu-hprac atmega328p_autobaud_ee_led+b5_fr_ce_ur_vbl_1s.hex
358 512 u7.7 weu-hprac atmega328p_autobaud_ee_led+b5_fr_ce_ur_vbl.hex

mcuee@UbuntuSwift3:~/build/avr/urboot/src$ ./hexls ../bootloaders/atmega328p/autobaud/atmega328p_autobaud_ee_led+b5_fr_ce_ur_vbl.hex 
362 384 u7.7 weu-jprac ../bootloaders/atmega328p/autobaud/atmega328p_autobaud_ee_led+b5_fr_ce_ur_vbl.hex

@stefanrueger
Now that I am using the same toolchain under Linux, just wondering what is the difference here in terms of code size and used flash space.

How should I invoke the Makefile if I want to use the system provded toolchain (urboot-gcc -toolchain=system`)?

@stefanrueger
Copy link
Owner

How should I invoke the Makefile if I want to use the system provded toolchain (urboot-gcc -toolchain=system`)?

The Makefile selects a suitable toolchain for the bootloader, but if you want to force a particular version it is TOOLVER=system or, eg, TOOLVER=5.4.0 as argument to make

Here a few truly random examples how to invoke make

make MCU=atmega1284p F_CPU=20000000L BAUD_RATE=500000 VBL=0 EEPROM=1 TEMPLATE=1 URPROTOCOL=1 FRILLS=1 MOVETO=atmega1284p_20mhz_500000bps_ee_lednop_ur
make MCU=atmega325pa F_CPU=22118400L BAUD_RATE=38400 VBL=0 EEPROM=0 TEMPLATE=1 URPROTOCOL=1 FRILLS=1 MOVETO=atmega325pa_22mhz1184_38400bps_lednop_ur
make MCU=at90pwm2 F_CPU=14745600L BAUD_RATE=460800 VBL=0 EEPROM=1 TEMPLATE=1 URPROTOCOL=1 FRILLS=0 MOVETO=at90pwm2_14mhz7456_460800bps_ee_ur
make MCU=atmega328p F_CPU=11059200L BAUD_RATE=57600 VBL=0 EEPROM=0 LEDPOLARITY=-1 LED=AtmelPB1 URPROTOCOL=0 FRILLS=7 MOVETO=atmega328p_11mhz0592_57600bps_led-b1_fr_ce
make MCU=atmega8515 F_CPU=12000000L BAUD_RATE=9600 VBL=1 EEPROM=1 TEMPLATE=1 URPROTOCOL=1 FRILLS=7 MOVETO=atmega8515_12mhz_9600bps_ee_lednop_fr_ce_ur_vbl
make MCU=atmega649a F_CPU=14745600L BAUD_RATE=19200 VBL=0 EEPROM=1 TEMPLATE=1 URPROTOCOL=0 FRILLS=0 MOVETO=atmega649a_14mhz7456_19200bps_ee
make MCU=atmega64rfr2 F_CPU=4000000L BAUD_RATE=19200 VBL=0 EEPROM=1 TEMPLATE=1 URPROTOCOL=1 FRILLS=0 MOVETO=atmega64rfr2_4mhz_19200bps_ee_ur
make MCU=atmega128rfa1 F_CPU=7372800L BAUD_RATE=230400 VBL=1 EEPROM=1 TEMPLATE=1 URPROTOCOL=1 FRILLS=6 MOVETO=atmega128rfa1_7mhz3728_230400bps_ee_lednop_fr_ur_vbl
make MCU=ata6286 F_CPU=11059200L BAUD_RATE=9600 SWIO=1 RX=AtmelPB0 TX=AtmelPB1 VBL=0 EEPROM=0 TEMPLATE=1 URPROTOCOL=0 FRILLS=6 MOVETO=ata6286_11mhz0592_9600bps_rxb0_txb1_lednop_fr
make MCU=ata5831 F_CPU=2000000L BAUD_RATE=19200 SWIO=1 RX=AtmelPB0 TX=AtmelPB1 VBL=0 EEPROM=1 TEMPLATE=1 URPROTOCOL=1 FRILLS=0 MOVETO=ata5831_2mhz_19200bps_rxb0_txb1_ee_ur

Here all make-commands.zip that I used for the pre-compiled bootloaders. Note they are generated by mkallbootloaders. Afterwards the script deletes redundant bootloaders (eg, when a bootloader with more features occupies the same size as another one with less the later one is deleted).

@stefanrueger
Copy link
Owner

The fix is easy though -- need to add the soft link.

Thanks, I just pushed symbolic links, too.

@stefanrueger
Copy link
Owner

stefanrueger commented Nov 20, 2022

Only difference is -DWDTO=8S.

So timing? Well, I don't get this. The bootloaders were compiled with -DWDTO=1S. Plenty'o time for the host to send the first command (and can delay, too, with -xdelay=...). Baud rate "negotiation" takes as long as it takes to send one byte (< 1ms).

Are you sure you set the fuses correct? And deleted flash before burning the bootloader? Note, you cannot put a program and a vector bootloader onto the chip at the same time. The vector bootloader must be burned onto an erased chip first. Then you can upload with the vector bootloader (remember -c urclock patches the application - you miss this when you burn the unpatched application plus vector bootloader at the same time).

@stefanrueger
Copy link
Owner

It works well with the above baud rate (300000, 600000, 800000, 900000; 120000, 160000, 170000, 190000, 210000, 310000, 350000).

Cannot imagine. A 16 MHz AVR can only tolerate baud rates in the following intervals (UART2X=1) with the USART_BRL register and a 1.5% tolerance:

  [1970000, 2030000] = [0.985*F_CPU/(8*1), 1.015*F_CPU/(8*1)], F_CPU = 16 MHz
  [ 985000, 1015000] = [0.985*F_CPU/(8*2), 1.015*F_CPU/(8*2)]
  [ 656667,  676667]
  [ 492500,  507500]
  [ 394000,  406000]
  [ 328333,  338333]
  [ 281429,  290000]
  [ 246250,  253750]
  [ 218889,  225556]
  [ 197000,  203000]
  [ 179091,  184545]
  [ 164167,  169167]
  [ 151538,  156154]
  [ 140714,  145000]
  [ 131333,  135333]
  [ 123125,  126875]
  [ 115882,  119412] <-- 115200 baud is out of specs for a 16 MHz MCU
  [ 109444,  112778]
  [ 103684,  106842]
  [  98500,  101500]
  [  93810,   96667]
  [  89545,   92273]
  [  85652,   88261]
  [  82083,   84583]
  [  78800,   81200]
  [  75769,   78077]
  [  72963,   75185]
  [  70357,   72500]
  [  67931,   70000]
  [  65667,   67667]
  [  63548,   65484]
  [  61563,   63437]
  [  7695,    61515] = [0.985*F_CPU/(8*256), 1.015*F_CPU/(8*33)]

I think what is happening is that when Linux cannot generate the requested baud rate it uses a nearby standard one. The baud rates that the USB-TTL parts can generate Linux-side are much more fine grained than AVR USART baud rates, but still there is a limit in the driver software.

@stefanrueger
Copy link
Owner

@MCUdude and @mcuee: I have now implemented a further safety feature for urboot bootloaders: PROTECTRESET This ensures that the bootloder writes an rjmp from the reset vector to the bootloader whenever it writes Page 0. This - together with all the checks in -c urclock makes it all but impossible for a vector bootloader to be corrupted/disabled.

Whenever a vector bootloader is being compiled and there are enough bytes left, this protection is automatically built in. It is visible as a capital P in the features. So j/P is the same good protection as p/h. You need to update avrdude to the newest version in PR avrdudes/avrdude#1171

I think this is now the last addition I do for urboot after sorting out autobaud and security. Unless there are bugs you see in the next two weeks, I'll release v7.7 of urboot, the first public release!

@mcuee
Copy link
Author

mcuee commented Nov 20, 2022

I think what is happening is that when Linux cannot generate the requested baud rate it uses a nearby standard one.

That is what I believe as well.

@mcuee
Copy link
Author

mcuee commented Nov 20, 2022

Only difference is -DWDTO=8S.

So timing? Well, I don't get this. The bootloaders were compiled with -DWDTO=1S. Plenty'o time for the host to send the first command (and can delay, too, with -xdelay=...). Baud rate "negotiation" takes as long as it takes to send one byte (< 1ms).

Are you sure you set the fuses correct? And deleted flash before burning the bootloader? Note, you cannot put a program and a vector bootloader onto the chip at the same time. The vector bootloader must be burned onto an erased chip first. Then you can upload with the vector bootloader (remember -c urclock patches the application - you miss this when you burn the unpatched application plus vector bootloader at the same time).

If you look at my runlog, I use usbasp to burn the FW, so an erase will happen.
I tested again under Linux, it actually works under Linux using stock urboot FW.

So Windows is the issue here. It does not work with the stock urboot FW, no matter which -xdelay= value I use. It works with the -DWDTO=8S FW.

Windows runlog: you can see the 8s urboot FW works but stock urboot FW does not work.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v7 -C .\avrdude_pr1171v7.conf -c usbasp
 -p m328p -U .\atmega328p_autobaud_ee_led+b5_fr_ce_ur_vbl_wdt8s.hex -qq && echo OK
OK

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v7 -C .\avrdude_pr1171v7.conf -c urclock
 -P COM10 -b 115200 -p m328p -xshowall

avrdude_pr1171v7: AVR device initialized and ready to accept instructions
0000ffffffff 0000-00-00 00.00  application 0 store 0 meta 0 boot 384 u7.7 weu-jPrac vector 25 
(SPM_Ready) ATmega328P

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v7 -C .\avrdude_pr1171v7.conf -c usbasp
 -p m328p -U .\atmega328p_autobaud_ee_led+b5_fr_ce_ur_vbl.hex -qq && echo OK
OK
PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v7 -C .\avrdude_pr1171v7.conf -c urclock
 -P COM10 -b 115200 -p m328p -xshowall
avrdude_pr1171v7 warning: attempt 1 of 20: not in sync
avrdude_pr1171v7 warning: programmer is not responding; try, eg, -xdelay=200
avrdude_pr1171v7 warning: attempt 2 of 20: not in sync
avrdude_pr1171v7 warning: programmer is not responding; try, eg, -xdelay=200
avrdude_pr1171v7 warning: attempt 3 of 20: not in sync
avrdude_pr1171v7 warning: programmer is not responding; try, eg, -xdelay=200
avrdude_pr1171v7 warning: attempt 4 of 20: not in sync
avrdude_pr1171v7 warning: programmer is not responding; try, eg, -xdelay=200
avrdude_pr1171v7 warning: attempt 5 of 20: not in sync
avrdude_pr1171v7 warning: programmer is not responding; try, eg, -xdelay=200
avrdude_pr1171v7 warning: attempt 6 of 20: not in sync
avrdude_pr1171v7 warning: programmer is not responding; try, eg, -xdelay=200
avrdude_pr1171v7 warning: attempt 7 of 20: not in sync
avrdude_pr1171v7 warning: programmer is not responding; try, eg, -xdelay=200
avrdude_pr1171v7 warning: attempt 8 of 20: not in sync
avrdude_pr1171v7 warning: programmer is not responding; try, eg, -xdelay=200
avrdude_pr1171v7 warning: attempt 9 of 20: not in sync
avrdude_pr1171v7 warning: programmer is not responding; try, eg, -xdelay=200
avrdude_pr1171v7 warning: attempt 10 of 20: not in sync
avrdude_pr1171v7 warning: programmer is not responding; try, eg, -xdelay=200
avrdude_pr1171v7 warning: attempt 11 of 20: not in sync
avrdude_pr1171v7 warning: programmer is not responding; try, eg, -xdelay=200
avrdude_pr1171v7 warning: attempt 12 of 20: not in sync
avrdude_pr1171v7 warning: programmer is not responding; try, eg, -xdelay=200
avrdude_pr1171v7 warning: attempt 13 of 20: not in sync
avrdude_pr1171v7 warning: programmer is not responding; try, eg, -xdelay=200
avrdude_pr1171v7 warning: attempt 14 of 20: not in sync
avrdude_pr1171v7 warning: programmer is not responding; try, eg, -xdelay=200
avrdude_pr1171v7 warning: attempt 15 of 20: not in sync
avrdude_pr1171v7 warning: programmer is not responding; try, eg, -xdelay=200
avrdude_pr1171v7 warning: attempt 16 of 20: not in sync
avrdude_pr1171v7 warning: programmer is not responding; try, eg, -xdelay=200
avrdude_pr1171v7 warning: attempt 17 of 20: not in sync
avrdude_pr1171v7 warning: programmer is not responding; try, eg, -xdelay=200
avrdude_pr1171v7 warning: attempt 18 of 20: not in sync
avrdude_pr1171v7 warning: programmer is not responding; try, eg, -xdelay=200
avrdude_pr1171v7 warning: attempt 19 of 20: not in sync
avrdude_pr1171v7 warning: programmer is not responding; try, eg, -xdelay=200
avrdude_pr1171v7 warning: attempt 20 of 20: not in sync
avrdude_pr1171v7 error: unable to open programmer urclock on port COM10

avrdude_pr1171v7 done.  Thank you.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v7 -C .\avrdude_pr1171v7.conf -c usbasp
 -p m328p -U .\atmega328p_autobaud_ee_led+b5_fr_ce_ur_vbl_wdt8s.hex -qq && echo OK
OK

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v7 -C .\avrdude_pr1171v7.conf -c urclock
 -P COM10 -b 115200 -p m328p -U .\hex\Blink.ino.standard.hex

avrdude_pr1171v7: AVR device initialized and ready to accept instructions
avrdude_pr1171v7: device signature = 0x1e950f (probably m328p)
avrdude_pr1171v7: Note: flash memory has been specified, an erase cycle will be performed.
                  To disable this feature, specify the -D option.
avrdude_pr1171v7: erasing chip
avrdude_pr1171v7: reading input file .\hex\Blink.ino.standard.hex for flash
                  with 924 bytes in 1 section within [0, 0x39b]
                  using 8 pages and 100 pad bytes
avrdude_pr1171v7: preparing flash input for device bootloader
avrdude_pr1171v7: writing 924 bytes flash ...

Writing | ################################################## | 100% 0.23 s

avrdude_pr1171v7: 924 bytes of flash written
avrdude_pr1171v7: verifying flash memory against .\hex\Blink.ino.standard.hex

Reading | ################################################## | 100% 0.14 s

avrdude_pr1171v7: 924 bytes of flash verified

avrdude_pr1171v7 done.  Thank you.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v7 -C .\avrdude_pr1171v7.conf -c urclock
 -P COM10 -b 115200 -p m328p -xshowall

avrdude_pr1171v7: AVR device initialized and ready to accept instructions
0000ffffffff 2022-06-10 21.21 Blink.ino.standard.hex 924 store 31426 meta 34 boot 384 u7.7 
weu-jPrac vector 25 (SPM_Ready) ATmega328P

@stefanrueger
Copy link
Owner

So Windows is the issue here.

I wonder whether excluding Windows from reducing the drain timeout is to blame here: https://github.com/avrdudes/avrdude/blob/7f4474f0497099f9ad048948a53383b4cd729327/src/urclock.c#L2046-L2048
Maybe windows need slightly different values than the 250 ms default?

Other than that, it could be peculiarities on the rx line when opening the serial port. An oscilloscope might show what happens with the rx line in Windows and what in Linuz.

@mcuee
Copy link
Author

mcuee commented Nov 21, 2022

@stefanrueger
I will give the time out value a try later under Windows. As for using scope, that may not be feasible any time soon.

@mcuee
Copy link
Author

mcuee commented Nov 21, 2022

Re-open to continue the discussion.

@mcuee mcuee reopened this Nov 21, 2022
@mcuee mcuee changed the title Autobaud does not work on one ATmega328P board Autobaud does not work on one ATmega328P board under Windows Nov 21, 2022
@mcuee
Copy link
Author

mcuee commented Nov 21, 2022

@stefanrueger

I tried the following and it does not help. --> Sorry wrong one. Ignore this one.

$ git diff
diff --git a/src/urclock.c b/src/urclock.c
index c66c616..bfb1990 100644
--- a/src/urclock.c
+++ b/src/urclock.c
@@ -2043,9 +2043,9 @@ static int urclock_open(PROGRAMMER *pgm, const char *port) {
     usleep((80+ur.delay)*1000); // Wait until board comes out of reset

   // Drain any extraneous input
-#ifndef WIN32
-  serial_drain_timeout = 80;    // ms
-#endif
+//#ifndef WIN32
+//  serial_drain_timeout = 80;    // ms
+//#endif
   serial_drain(&pgm->fd, 0);

   if(urclock_getsync(pgm) < 0)

@mcuee
Copy link
Author

mcuee commented Nov 21, 2022

@stefanrueger

This works for me.

$ git diff
diff --git a/src/urclock.c b/src/urclock.c
index c66c616..28673f2 100644
--- a/src/urclock.c
+++ b/src/urclock.c
@@ -2045,6 +2045,8 @@ static int urclock_open(PROGRAMMER *pgm, const char *port) {
   // Drain any extraneous input
 #ifndef WIN32
   serial_drain_timeout = 80;    // ms
+#else
+  serial_drain_timeout = 150;    // ms
 #endif
   serial_drain(&pgm->fd, 0);

@mcuee
Copy link
Author

mcuee commented Nov 21, 2022

So Windows is the issue here.

I wonder whether excluding Windows from reducing the drain timeout is to blame here: https://github.com/avrdudes/avrdude/blob/7f4474f0497099f9ad048948a53383b4cd729327/src/urclock.c#L2046-L2048 Maybe windows need slightly different values than the 250 ms default?

You are absolutely right, 80ms works for Windows as well. So the following simple fix should work. I've tested 150ms and 200ms as well -- they work as well but 250ms failed for the Nano Clone.

$ git diff
diff --git a/src/urclock.c b/src/urclock.c
index c66c616..ba081d4 100644
--- a/src/urclock.c
+++ b/src/urclock.c
@@ -2043,9 +2043,7 @@ static int urclock_open(PROGRAMMER *pgm, const char *port) {
     usleep((80+ur.delay)*1000); // Wait until board comes out of reset

   // Drain any extraneous input
-#ifndef WIN32
   serial_drain_timeout = 80;    // ms
-#endif
   serial_drain(&pgm->fd, 0);

   if(urclock_getsync(pgm) < 0)

C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v8mod_80ms -C .\avrdude_pr1171v8.conf -c urclock
 -P COM6 -p m328p -xshowall

avrdude_pr1171v8mod_80ms: AVR device initialized and ready to accept instructions
0000ffffffff 0000-00-00 00.00  application 0 store 0 meta 0 boot 384 u7.7 weu-jPrac vector 25 (SPM_Ready) ATmega328P

C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v8mod_80ms -C .\avrdude_pr1171v8.conf -c urclock
 -P COM6 -b 7800 -p m328p -xshowall

avrdude_pr1171v8mod_80ms: AVR device initialized and ready to accept instructions
0000ffffffff 0000-00-00 00.00  application 0 store 0 meta 0 boot 384 u7.7 weu-jPrac vector 25 (SPM_Ready) ATmega328P
C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v8mod_80ms -C .\avrdude_pr1171v8.conf -c urclock
 -P COM6 -b 3000000 -p m328p -xshowall

avrdude_pr1171v8mod_80ms: AVR device initialized and ready to accept instructions
0000ffffffff 0000-00-00 00.00  application 0 store 0 meta 0 boot 384 u7.7 weu-jPrac vector 25 (SPM_Ready) ATmega328P

C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v8mod_80ms -C .\avrdude_pr1171v8.conf -c urclock
 -P COM6 -b 3000000 -p m328p -U .\Blink_Uno.ino.hex -qq && echo OK
OK

C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v8mod_80ms -C .\avrdude_pr1171v8.conf -c urclock
 -P COM6 -b 3000000 -p m328p -xshowall

avrdude_pr1171v8mod_80ms: AVR device initialized and ready to accept instructions
0000ffffffff 2022-10-17 09.23 Blink_Uno.ino.hex 924 store 31431 meta 29 boot 384 u7.7 weu-jPrac vector 25 
(SPM_Ready) ATmega328P

@mcuee
Copy link
Author

mcuee commented Nov 21, 2022

This change is also good for my Uno clone (ATmega16U2 USB to Serial FW). It is just that very high baud rate does not work well for sketch upload (only works up to 1,300,000bps) during the verification stage. -xshowall works up to 3,000,000bps.

That is probably the limitation of ATmega16U2 USB to Serial FW. I checked the default 250ms version and it has the same results.

C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v8mod_80ms -C .\avrdude_pr1171v8.conf -c urclock
 -P COM5 -b 1300000 -p m328p -U .\Blink_Uno.ino.hex

avrdude_pr1171v8mod_80ms: AVR device initialized and ready to accept instructions
avrdude_pr1171v8mod_80ms: device signature = 0x1e950f (probably m328p)
avrdude_pr1171v8mod_80ms: Note: flash memory has been specified, an erase cycle will be performed.
                          To disable this feature, specify the -D option.
avrdude_pr1171v8mod_80ms: erasing chip
                          delaying chip erase until first -U upload to flash
avrdude_pr1171v8mod_80ms: reading input file .\Blink_Uno.ino.hex for flash
                          with 924 bytes in 1 section within [0, 0x39b]
                          using 8 pages and 100 pad bytes
avrdude_pr1171v8mod_80ms: preparing flash input for device bootloader
avrdude_pr1171v8mod_80ms: writing 924 bytes flash ...

Writing | ################################################## | 100% 3.63 s

avrdude_pr1171v8mod_80ms: 924 bytes of flash written
avrdude_pr1171v8mod_80ms: verifying flash memory against .\Blink_Uno.ino.hex

Reading | ################################################## | 100% 1.45 s

avrdude_pr1171v8mod_80ms: 924 bytes of flash verified

avrdude_pr1171v8mod_80ms done.  Thank you.

C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v8mod_80ms -C .\avrdude_pr1171v8.conf -c urclock
 -P COM5 -b 1400000 -p m328p -U .\Blink_Uno.ino.hex

avrdude_pr1171v8mod_80ms: AVR device initialized and ready to accept instructions
avrdude_pr1171v8mod_80ms: device signature = 0x1e950f (probably m328p)
avrdude_pr1171v8mod_80ms: Note: flash memory has been specified, an erase cycle will be performed.
                          To disable this feature, specify the -D option.
avrdude_pr1171v8mod_80ms: erasing chip
                          delaying chip erase until first -U upload to flash
avrdude_pr1171v8mod_80ms: reading input file .\Blink_Uno.ino.hex for flash
                          with 924 bytes in 1 section within [0, 0x39b]
                          using 8 pages and 100 pad bytes
avrdude_pr1171v8mod_80ms: preparing flash input for device bootloader
avrdude_pr1171v8mod_80ms: writing 924 bytes flash ...

Writing | ################################################## | 100% 3.66 s

avrdude_pr1171v8mod_80ms: 924 bytes of flash written
avrdude_pr1171v8mod_80ms: verifying flash memory against .\Blink_Uno.ino.hex

Reading | -------------------------------------------------- | 0% 0.00 s

avrdude_pr1171v8mod_80ms error: protocol expects sync byte 0x20 but got 0x04 in urclock_paged_load()
avrdude_pr1171v8mod_80ms error: protocol expects sync byte 0x20 but got 0x78 in ur_readEF()
avrdude_pr1171v8mod_80ms error: unable to read byte at address 0x0000
avrdude_pr1171v8mod_80ms error: read operation not supported for memory flash
avrdude_pr1171v8mod_80ms error: unable to read all of flash memory, rc=-2
avrdude_pr1171v8mod_80ms error: protocol expects sync byte 0x20 but got 0xcf

avrdude_pr1171v8mod_80ms done.  Thank you.

C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v8mod_80ms -C .\avrdude_pr1171v8.conf -c urclock
 -P COM5 -b 1400000 -p m328p -xshowall

avrdude_pr1171v8mod_80ms: AVR device initialized and ready to accept instructions
0 2022-10-17 09.23 Blink_Uno.ino.hex 924 store 31559 meta 29 boot 256 u7.7 w-u-jpra- vector 25 
(SPM_Ready) ATmega328P

C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v8mod_80ms -C .\avrdude_pr1171v8.conf
 -c urclock -P COM5 -b 2000000 -p m328p -xshowall

avrdude_pr1171v8mod_80ms: AVR device initialized and ready to accept instructions
0 2022-10-17 09.23 Blink_Uno.ino.hex 924 store 31559 meta 29 boot 256 u7.7 w-u-jpra- vector 25 
(SPM_Ready) ATmega328P

C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1171v8mod_80ms -C .\avrdude_pr1171v8.conf -c urclock
 -P COM5 -b 3000000 -p m328p -xshowall

avrdude_pr1171v8mod_80ms: AVR device initialized and ready to accept instructions
0 2022-10-17 09.23 Blink_Uno.ino.hex 924 store 31559 meta 29 boot 256 u7.7 w-u-jpra- vector 25 
(SPM_Ready) ATmega328P

-c arduino has the same limitation anyway (tested using atmega328p_autobaud_ee_led+b5_fr_ce.hex). So this is okay.

C:\work\avr\avrdude_test\avrdude_bin> .\avrdude -c arduino -P COM5 -b 1400000 -p m328p
 -D -U .\Blink_Uno.ino.hex

avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e950f (probably m328p)
avrdude: reading input file .\Blink_Uno.ino.hex for flash
         with 924 bytes in 1 section within [0, 0x39b]
         using 8 pages and 100 pad bytes
avrdude: writing 924 bytes flash ...

Writing | ################################################## | 100% 0.14 s

avrdude: 924 bytes of flash written
avrdude: verifying flash memory against .\Blink_Uno.ino.hex

Reading | -------------------------------------------------- | 0% 0.01 s


avrdude error: protocol expects sync byte 0x14 but got 0x04
avrdude error: programmer is out of sync
avrdude error: unable to read byte at address 0x0000
avrdude error: read operation not supported for memory flash
avrdude error: unable to read all of flash memory, rc=-2
avrdude error: protocol expects sync byte 0x14 but got 0x94

avrdude done.  Thank you.

C:\work\avr\avrdude_test\avrdude_bin> .\avrdude -c arduino -P COM5 -b 1300000 -p m328p
 -D -U .\Blink_Uno.ino.hex

avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e950f (probably m328p)
avrdude: reading input file .\Blink_Uno.ino.hex for flash
         with 924 bytes in 1 section within [0, 0x39b]
         using 8 pages and 100 pad bytes
avrdude: writing 924 bytes flash ...

Writing | ################################################## | 100% 0.15 s

avrdude: 924 bytes of flash written
avrdude: verifying flash memory against .\Blink_Uno.ino.hex

Reading | ################################################## | 100% 0.09 s

avrdude: 924 bytes of flash verified

avrdude done.  Thank you.

@mcuee
Copy link
Author

mcuee commented Nov 21, 2022

Since this is avrdude -c urclock issue and not urboot issue now, I will close again.

@mcuee mcuee closed this as completed Nov 21, 2022
@stefanrueger
Copy link
Owner

but 250ms failed for the Nano Clone.

So, I am guessing that a 250 ms drain timeout setting in windows can translate to around 1000 ms wait and borderline a timeout for boards that come out of reset slower than others. I think (speculate!) that's because in windows the drain timeout is applied between characters as well as a final wait for characters after the last one was read; in Linux it's only the latter as far as I know.

So, with 80 ms for Windows, all is OK? autobaud? fixed baud? different boards?

BTW, I would be surprised if the LGT8F328P worked with the autobaud detection: The reason is that the loop that measures the baud rate must be 5 clock cycles to work, but according to this document the number of clock cycles is different for all three instructions in the loop at

urboot/src/urboot.c

Lines 2323 to 2325 in 0adff89

"2: adiw r26, 256/8\n" /* Add 0.125 to X, ie, 256/8 to XL, in 8-bit fixed point rep */ \
" sbis %[RXPin],%[RXBit]\n" /* Loop as long as rx bit is low */ \
" rjmp 2b\n" /* 5-cycle loop for 5 low bits (start bit + 4 lsb of 0x30) */
are shorter, so the loop consumes 3 cycles, not 5. This also means that SWIO (software I/O) does not work for the LGT8F328P with the bootloader.

@stefanrueger
Copy link
Owner

works up to 3,000,000bps.

If it's a 16 MHz board this cannot be. Max is F_CPU/8, ie 2 MHz baud rate. So really important to establish what baud rate is actually used. Ralph recommends studying the kernel source to figure out what the actual generated baud rate is: avrdudes/avrdude#1171 (comment)

@mcuee
Copy link
Author

mcuee commented Nov 21, 2022

If it's a 16 MHz board this cannot be. Max is F_CPU/8, ie 2 MHz baud rate. So really important to establish what baud rate is actually used. Ralph recommends studying the kernel source to figure out what the actual generated baud rate is: avrdudes/avrdude#1171 (comment)

Yes I believe Linux only selects the nearby baud rate. Anyway, this is not a real isse -- we can dig out the kernel source code later.

@mcuee
Copy link
Author

mcuee commented Nov 21, 2022

So, I am guessing that a 250 ms drain timeout setting in windows can translate to around 1000 ms wait and borderline a timeout for boards that come out of reset slower than others. I think (speculate!) that's because in windows the drain timeout is applied between characters as well as a final wait for characters after the last one was read; in Linux it's only the latter as far as I know.

So, with 80 ms for Windows, all is OK? autobaud? fixed baud? different boards?

Yes 80ms for Windows is good for my boards tested -- Uno ATmgea16U2 clone, Uno CH340 Clone, Nano CH340 Clone {all three using ATmega328P at 16MHz) and Mega2560 CH340 clone.

The Uno ATmgea16U2 clone, Uno CH340 Clone and Mega2560 CH340 clone work well with the default 250ms drain timeout value. Only the Nano Clone does not work.

But if you are really concerned about this parameter, you may want to get a new extension parameter like -xdrain= (default 80ms) for the user to specify in case they encounter issues.

BTW, I would be surprised if the LGT8F328P worked with the autobaud detection: The reason is that the loop that measures the baud rate must be 5 clock cycles to work, but according to this document the number of clock cycles is different for all three instructions in the loop at.

I do not have the programmer setting up for the LGT8F328P so I can not burn the bootloader . I was using the factory bootloader (-c arduino compatible, only 57600bps) for the testing.

@mcuee
Copy link
Author

mcuee commented Nov 21, 2022

If it's a 16 MHz board this cannot be. Max is F_CPU/8, ie 2 MHz baud rate. So really important to establish what baud rate is actually used. Ralph recommends studying the kernel source to figure out what the actual generated baud rate is: avrdudes/avrdude#1171 (comment)

I think the real test is to flash a hex file. -xshowall somehow works to rediculosly high baud rate due to Linux baud rate behaviors.

The Uno clone with ATmega16U2 USB to Serial FW is using kernel CDC-ACM driver. It only works to 1,000,000bps.

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ ./avrdude_pr1171v8 -C ./avrdude_pr1171v8.conf -c urclock
 -P /dev/ttyACM0 -b 1100000 -p m328p -U ./blink_m328p.hex 

avrdude_pr1171v8: AVR device initialized and ready to accept instructions
avrdude_pr1171v8: device signature = 0x1e950f (probably m328p)
avrdude_pr1171v8: Note: flash memory has been specified, an erase cycle will be performed.
                  To disable this feature, specify the -D option.
avrdude_pr1171v8: erasing chip
avrdude_pr1171v8: reading input file ./blink_m328p.hex for flash
                  with 1030 bytes in 1 section within [0, 0x405]
                  using 9 pages and 122 pad bytes
avrdude_pr1171v8: preparing flash input for device bootloader
avrdude_pr1171v8: writing 1030 bytes flash ...

Writing | ################################################## | 100% 0.11 s 

avrdude_pr1171v8: 1030 bytes of flash written
avrdude_pr1171v8: verifying flash memory against ./blink_m328p.hex

Reading | -------------------------------------------------- | 0% 0.00 s 

avrdude_pr1171v8 error: protocol expects sync byte 0xa0 but got 0x01 in urclock_paged_load()
avrdude_pr1171v8 error: protocol expects sync byte 0xa0 but got 0x77 in ur_readEF()
avrdude_pr1171v8 error: unable to read byte at address 0x0000
avrdude_pr1171v8 error: read operation not supported for memory flash
avrdude_pr1171v8 error: unable to read all of flash memory, rc=-2
avrdude_pr1171v8 error: protocol expects sync byte 0xa0 but got 0xcf

avrdude_pr1171v8 done.  Thank you.

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ ./avrdude_pr1171v8 -C ./avrdude_pr1171v8.conf -c urclock
 -P /dev/ttyACM0 -b 1000000 -p m328p -U ./blink_m328p.hex 

avrdude_pr1171v8: AVR device initialized and ready to accept instructions
avrdude_pr1171v8: device signature = 0x1e950f (probably m328p)
avrdude_pr1171v8: Note: flash memory has been specified, an erase cycle will be performed.
                  To disable this feature, specify the -D option.
avrdude_pr1171v8: erasing chip
avrdude_pr1171v8: reading input file ./blink_m328p.hex for flash
                  with 1030 bytes in 1 section within [0, 0x405]
                  using 9 pages and 122 pad bytes
avrdude_pr1171v8: preparing flash input for device bootloader
avrdude_pr1171v8: writing 1030 bytes flash ...

Writing | ################################################## | 100% 0.11 s 

avrdude_pr1171v8: 1030 bytes of flash written
avrdude_pr1171v8: verifying flash memory against ./blink_m328p.hex

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

avrdude_pr1171v8: 1030 bytes of flash verified

avrdude_pr1171v8 done.  Thank you.

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ ./avrdude_pr1171v8 -C ./avrdude_pr1171v8.conf -c urclock
 -P /dev/ttyACM0 -b 1000000 -p m328p -xshowall 

avrdude_pr1171v8: AVR device initialized and ready to accept instructions
ffffffffffff 2022-11-20 12.58 blink_m328p.hex 1030 store 31327 meta 27 boot 384 u7.7 
weu-jPrac vector 25 (SPM_Ready) ATmega328P

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ ./avrdude_pr1171v8 -C ./avrdude_pr1171v8.conf -c urclock
 -P /dev/ttyACM0 -b 100000000 -p m328p -xshowall 

avrdude_pr1171v8: AVR device initialized and ready to accept instructions
ffffffffffff 2022-11-20 12.58 blink_m328p.hex 1030 store 31327 meta 27 boot 384 u7.7 
weu-jPrac vector 25 (SPM_Ready) ATmega328P

@mcuee
Copy link
Author

mcuee commented Nov 21, 2022

I think the real test is to flash a hex file.

This is not valid for CH340. It claims to work to baud rate above the crystal frequency, which can not be true.

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ ./avrdude_pr1171v8 -C ./avrdude_pr1171v8.conf -c urclock
 -P /dev/ttyUSB0 -b 24000000 -p m328p -U blink_m328p.hex 

avrdude_pr1171v8: AVR device initialized and ready to accept instructions
avrdude_pr1171v8: device signature = 0x1e950f (probably m328p)
avrdude_pr1171v8: Note: flash memory has been specified, an erase cycle will be performed.
                  To disable this feature, specify the -D option.
avrdude_pr1171v8: erasing chip
avrdude_pr1171v8: reading input file blink_m328p.hex for flash
                  with 1030 bytes in 1 section within [0, 0x405]
                  using 9 pages and 122 pad bytes
avrdude_pr1171v8: preparing flash input for device bootloader
avrdude_pr1171v8: writing 1030 bytes flash ...

Writing | ################################################## | 100% 1.36 s 

avrdude_pr1171v8: 1030 bytes of flash written
avrdude_pr1171v8: verifying flash memory against blink_m328p.hex

Reading | ################################################## | 100% 1.30 s 

avrdude_pr1171v8: 1030 bytes of flash verified

avrdude_pr1171v8 done.  Thank you.

Kernel drivver source code is here.
https://github.com/torvalds/linux/blob/master/drivers/usb/serial/ch341.c#L168

 * The device line speed is given by the following equation:
 *
 *	baudrate = 48000000 / (2^(12 - 3 * ps - fact) * div), where
 *
 *		0 <= ps <= 3,
 *		0 <= fact <= 1,
 *		2 <= div <= 256 if fact = 0, or
 *		9 <= div <= 256 if fact = 1
 */

@mcuee
Copy link
Author

mcuee commented Nov 21, 2022

From https://superuser.com/questions/488449/is-there-any-way-to-check-which-baud-rates-are-supported-on-a-serial-device

The max baud rate for the CH340 under my Ubuntu 20.04 is 4,000,000 bps. Same for PL2303.

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ hfile=/usr/include/asm-generic/termbits.h ans=(yes no);column < <(
    while read -r bauds;do
        stty -F /dev/ttyUSB0 $bauds &>/dev/null
        printf '%8d %-3s\n' $bauds ${ans[$?]}
    done < <(
  sed -r 's/^#define\s+B([0-9]{2,})\s+.*/\1/p;d' <$hfile ))
      50 yes	     134 yes	     300 yes	    1800 yes	    9600 yes	   57600 yes	  460800 yes	  921600 yes	 1500000 yes	 3000000 yes
      75 yes	     150 yes	     600 yes	    2400 yes	   19200 yes	  115200 yes	  500000 yes	 1000000 yes	 2000000 yes	 3500000 yes
     110 yes	     200 yes	    1200 yes	    4800 yes	   38400 yes	  230400 yes	  576000 yes	 1152000 yes	 2500000 yes	 4000000 yes

Same results for the CDC-ACM ATmega16U2.

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ hfile=/usr/include/asm-generic/termbits.h ans=(yes no);column < <(
    while read -r bauds;do
        stty -F /dev/ttyACM0 $bauds &>/dev/null
        printf '%8d %-3s\n' $bauds ${ans[$?]}
    done < <(
  sed -r 's/^#define\s+B([0-9]{2,})\s+.*/\1/p;d' <$hfile ))
      50 yes	     134 yes	     300 yes	    1800 yes	    9600 yes	   57600 yes	  460800 yes	  921600 yes	 1500000 yes	 3000000 yes
      75 yes	     150 yes	     600 yes	    2400 yes	   19200 yes	  115200 yes	  500000 yes	 1000000 yes	 2000000 yes	 3500000 yes
     110 yes	     200 yes	    1200 yes	    4800 yes	   38400 yes	  230400 yes	  576000 yes	 1152000 yes	 2500000 yes	 4000000 yes

CP2102 has less number and also lower baud rate.

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ hfile=/usr/include/asm-generic/termbits.h ans=(yes no);column < <(
    while read -r bauds;do
        stty -F /dev/ttyUSB0 $bauds &>/dev/null
        printf '%8d %-3s\n' $bauds ${ans[$?]}
    done < <(
  sed -r 's/^#define\s+B([0-9]{2,})\s+.*/\1/p;d' <$hfile ))
      50 no 	     134 no 	     300 yes	    1800 yes	    9600 yes	   57600 yes	  460800 yes	  921600 yes	 1500000 no 	 3000000 no 
      75 no 	     150 no 	     600 yes	    2400 yes	   19200 yes	  115200 yes	  500000 yes	 1000000 yes	 2000000 no 	 3500000 no 
     110 no 	     200 no 	    1200 yes	    4800 yes	   38400 yes	  230400 yes	  576000 yes	 1152000 no 	 2500000 no 	 4000000 no 

I am not so sure if it is avrdude ser_posix.c is doing something or not to the baud rate.

@mcuee
Copy link
Author

mcuee commented Nov 21, 2022

A good info about USB to TTL converter is here. It only caters for UPDI parts though. But practically speaking anything above 500,000bps may not help much (avrdude speed is similar using 1,000,000 bps and 500,000 bps)
https://github.com/SpenceKonde/AVR-Guidance/blob/master/UPDI/jtag2updi.md

@mcuee
Copy link
Author

mcuee commented Nov 23, 2022

So, I am guessing that a 250 ms drain timeout setting in windows can translate to around 1000 ms wait and borderline a timeout for boards that come out of reset slower than others. I think (speculate!) that's because in windows the drain timeout is applied between characters as well as a final wait for characters after the last one was read; in Linux it's only the latter as far as I know.

So, with 80 ms for Windows, all is OK? autobaud? fixed baud? different boards?

Yes 80ms for Windows is good for my boards tested -- Uno ATmgea16U2 clone, Uno CH340 Clone, Nano CH340 Clone {all three using ATmega328P at 16MHz) and Mega2560 CH340 clone.

The Uno ATmgea16U2 clone, Uno CH340 Clone and Mega2560 CH340 clone work well with the default 250ms drain timeout value. Only the Nano Clone does not work.

But if you are really concerned about this parameter, you may want to get a new extension parameter like -xdrain= (default 80ms) for the user to specify in case they encounter issues.

Oops, I find another issue now. If I use the Optiboot bootloader (optiboot_atmega328.hex from Arduino), the Nano Clone does not work well with 80ms, rather I need to add -xdelay=at least 20 to get it work with -c urclock. I also checked that it has no issue if I change the drain timeout to 150ms, 200ms or 250ms.

So the urboot autobaud FW works with 80ms but not 250ms. Optiboot FW works with 250ms but not 80ms. It seems to me 150ms may be a good compromise for Windows.

Sorry for the late findings.

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

No branches or pull requests

3 participants