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

Change Arduino auto-reset via RTS to allow direct RTS-reset connection #1309

Merged
merged 3 commits into from
Mar 21, 2023

Conversation

mariusgreuel
Copy link
Contributor

Closes #1262

@stefanrueger Here is something to look at. I have not grasped the timing logic in urclock, but assuming the magic starts with releasing the reset line in line 2247, I figured 120 minus 50 for the extra wait?

@stefanrueger
Copy link
Collaborator

stefanrueger commented Mar 5, 2023

assuming the magic starts with releasing the reset line in line 2247

Only when directly connected!! When connected via a cap, the magic starts less than a ms after Line 2245. See

reset

Typical C is 100 nF, typical R is 10k, so we are talking t = RC ~ 1 ms. Really no need to spend 50 ms each!

Why the sleep in Line 2249? Time will march on regardless. Can I suggest

  // This code assumes a negative-logic USB to TTL serial adapter
  // Set RTS/DTR high to discharge the series-capacitor, if present
  serial_set_dtr_rts(&pgm->fd, 0);
  usleep(20*1000);
  // Pull the RTS/DTR line low to reset AVR
  serial_set_dtr_rts(&pgm->fd, 1);
  usleep(20*1000);
  // Set the RTS/DTR line back to high
  serial_set_dtr_rts(&pgm->fd, 0);

  if((100+ur.delay) > 0)
    usleep((100+ur.delay)*1000); // Wait until board comes out of reset

Similar in stk500.c

If you insist on changing the original 250 ms wait in arduino.c in Line 91 to 50 ms, I predict there will be trouble when executing two uploads to optiboot in quick succession

$ avrdude -c arduino -qqp m328p -U blink.hex; avrdude -c arduino -qqp m328p -U blink.hex;

Leave that 250 ms be. (Urclock deals with this by sleeping at exit for 200 ms if it had served optiboot).

See my comment on this

@stefanrueger
Copy link
Collaborator

Similar in stk500.c

Actually, we know DTR/RTS has been set 0 (ie, high voltage) earlier, all we need to do is

  // Pull the RTS/DTR line low to reset AVR
  serial_set_dtr_rts(&pgm->fd, 1);
  usleep(20*1000);
  // Set the RTS/DTR line back to high
  serial_set_dtr_rts(&pgm->fd, 0);

The sleep after that won't be necessary as draining input itself sleeps and getsync spends time ...

And should we not also improve the other two serial programmers that reset the MCU board by plugging DTR/RTS?

wiring.c
xbee.c

@mcuee mcuee added the enhancement New feature or request label Mar 6, 2023
@mcuee
Copy link
Collaborator

mcuee commented Mar 6, 2023

And should we not also improve the other two serial programmers that reset the MCU board by plugging DTR/RTS?

wiring.c
xbee.c

I will say wiring.c (stk500v2 based bootloader) is good to have the change as well.

I am not so sure whether it is a good idea to change xbee.c or not. It seems that there is no such thing as direct connect to reset pin for the xbee programmer.
https://www.arduino.cc/en/uploads/Main/XbeeShieldSchematic.pdf

serial_set_dtr_rts is also used by updi_link.c but I guess the usage there is different.

@stefanrueger
Copy link
Collaborator

Last weekend I played with the Falstad simulator. It corroborates my sketch above: below graph pulls DTR low for 20 ms
on a reset circuit. After the first millisecond Reset is high enough to be deactivated. Crucially, it is low for at least ~ 2 µs that are required for reset to become active. The simulation shows that 20 ms is sufficient by a country mile.

reset-simulation-label

(The circuit misses external clamp diode protection to Vcc. The chip doesn't have internal protection on the reset pin to allow High Voltage programming. The graph above neatly shows the spike to 2*Vcc when switching the DTR signal to Vcc again. With Vcc > 5V one needs the diode to prevent accidentally entering the HV mode.)

@stefanrueger
Copy link
Collaborator

Anyway, I pushed my suggested changes onto your PR, @mariusgreuel, so we can try them out.

If things work out, I'd be keen to integrate this into avrdude because this is great to be able to use DRT/RTS directly on the reset pin! Your idea, @mariusgreuel, is a great improvement.

@mcuee
Copy link
Collaborator

mcuee commented Mar 15, 2023

I have tested some direct short cases using ATmega2560 and ATmega13A.

I will carry out more tests for this to see if there are regressions for STK500 v1 related programmers later.

@mcuee
Copy link
Collaborator

mcuee commented Mar 16, 2023

No regressions for Arduino optiboot bootloader.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1309 -c arduino -P COM13
-p m328p -D -U .\hex\Blink.ino.s
tandard.hex

avrdude_pr1309: AVR device initialized and ready to accept instructions
avrdude_pr1309: device signature = 0x1e950f (probably m328p)
avrdude_pr1309: 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_pr1309: writing 924 bytes flash ...

Writing | ################################################## | 100% 0.19 s

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

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

avrdude_pr1309: 924 bytes of flash verified

avrdude_pr1309 done.  Thank you.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1309 -c urclock -P COM13
-p m328p -U .\hex\Blink.ino.standard.hex

avrdude_pr1309: AVR device initialized and ready to accept instructions
avrdude_pr1309: device signature = 0x1e950f (probably m328p)
avrdude_pr1309: Note: flash memory has been specified, an erase cycle will be performed.
                To disable this feature, specify the -D option.
avrdude_pr1309: erasing chip
                delaying chip erase until first -U upload to flash
avrdude_pr1309: 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_pr1309: preparing flash input for device bootloader
avrdude_pr1309: writing 924 bytes flash ...

Writing | ################################################## | 100% 5.19 s

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

Reading | ################################################## | 100% 3.65 s

avrdude_pr1309: 924 bytes of flash verified

avrdude_pr1309 done.  Thank you.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1309 -c urclock -P COM13 -p m328p -xshowall

avrdude_pr1309: AVR device initialized and ready to accept instructions
0 2022-06-10 21.21 Blink.ino.standard.hex 924 store 31298 meta 34 boot 512 o4.4 --s-h-r-- vector 0 (RESET) ATmega328P

@mcuee
Copy link
Collaborator

mcuee commented Mar 16, 2023

No regression for "Arduino as ISP" either.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1309 -c avrisp -P COM12 -b 19200 -p t88 -U t88_blink.hex

avrdude_pr1309: AVR device initialized and ready to accept instructions
avrdude_pr1309: device signature = 0x1e9311 (probably t88)
avrdude_pr1309: Note: flash memory has been specified, an erase cycle will be performed.
                To disable this feature, specify the -D option.
avrdude_pr1309: erasing chip
avrdude_pr1309: reading input file t88_blink.hex for flash
                with 8070 bytes in 1 section within [0, 0x1f85]
                using 127 pages and 58 pad bytes
avrdude_pr1309: writing 8070 bytes flash ...

Writing | ################################################## | 100% 12.59 s

avrdude_pr1309: 8070 bytes of flash written
avrdude_pr1309: verifying flash memory against t88_blink.hex

Reading | ################################################## | 100% 6.59 s

avrdude_pr1309: 8070 bytes of flash verified

avrdude_pr1309 done.  Thank you.

@stefanrueger
Copy link
Collaborator

Made the change for wiring.c as well, so STK500 v2 bootloaders should work with direct reset, too.

Earmarked for the next mergefest (probably in the next days) so any objection speak up now

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

Successfully merging this pull request may close these issues.

Change Arduino auto-reset via DTR/RTS/CTS to allow direct reset connection
3 participants