Whew, that was a thrash, but I finally found a fix for it.
It turned out that the Arduino application opens the serial port for some reason after it calls Avrdude, and it was leaving DTR high when it closed the port.
Avrdude works the first time because DTR is low when it opens the port, and so raising DTR generates a reset on the Boarduino. But every subsequent time I tried to upload code, DTR was still high, so trying to raise it from within Avrdude had no effect and the chip didn't get reset.
The solution I went with was to download source for the RXTXComm package that Arduino uses:
http://rxtx.qbang.org/pub/rxtx/rxtx-2.1-7r2.zip
I modded the file src/SerialImp.c, added the termios "HUPCL" flag in the configure_port() function, which will clear DTR (apparently the same as setting RTS???) when the port is closed:
- Code: Select all
--- src/SerialImp.c.orig 2006-01-29 17:19:04.000000000 -0500
+++ src/SerialImp.c 2007-12-28 17:45:11.000000000 -0500
@@ -380,7 +380,8 @@
ttyset.c_iflag = INPCK;
ttyset.c_lflag = 0;
ttyset.c_oflag = 0;
- ttyset.c_cflag = CREAD | CS8 | CLOCAL;
+// timv 122807 : clear DTR on close
+ ttyset.c_cflag = CREAD | CS8 | CLOCAL | HUPCL;
ttyset.c_cc[ VMIN ] = 0;
ttyset.c_cc[ VTIME ] = 0;
I ran "./configure" and "make" and then installed the modified native code library in Arduino's "lib" directory. I also copied the rebuilt .jar file just for superstition I guess:
- Code: Select all
cp RXTXcomm.jar ../arduino-0010/lib
cp i686-pc-linux-gnu/.libs/librxtxSerial.so ../arduino-0010/lib
I don't know where the serial port is getting re-opened in the Arduino app software. I couldn't find a downloadable copy of the source for it anywhere.
Is it just me or is that berlios.de site a total train wreck? There didn't seem to be any function to search the source, and Google didn't have any hits for
it either. SVN locked up dead halfway through an update operation when I tried to get the code that way. And I kept getting ominous messages from
my browser about attempts to access sites like "koder.com" with invalid certificates.
Has that site been kracked or what? And would it kill them to tar up a snapshot of the source for each release and post it along with the binaries, like decent folks do? </rant>
I also made a similar change to ser_posix.c in the avrdude-5.4-arduino-0010-src package:
- Code: Select all
--- box/avrdude-5.4-arduino-src/ser_posix.c 2007-07-18 16:24:11.000000000 -0400
+++ avrdude-5.4-arduino-src/ser_posix.c 2007-12-28 05:56:23.000000000 -0500
@@ -114,7 +114,9 @@
termios.c_iflag = IGNBRK;
termios.c_oflag = 0;
termios.c_lflag = 0;
- termios.c_cflag = (CS8 | CREAD | CLOCAL);
+// timv 12/28/07
+// termios.c_cflag = (CS8 | CREAD | CLOCAL );
+ termios.c_cflag = (CS8 | CREAD | CLOCAL | HUPCL );
termios.c_cc[VMIN] = 1;
termios.c_cc[VTIME] = 0;
I built the Avrdude binary and copied it to Arduino's hardware/tools directory.
I actually tried that change first, and it almost solved the problem. It would fail the first time after Arduino had closed the port with DTR set, but worked the next time--and kept working if I kept running Avrdude from the command line.
But from within the Arduino app, it worked
every other time, since Arduino apparently only messes with the port when Avrdude succeeds in programming the chip.
So that Avrdude change isn't really necessary as long as the Arduino app behaves itself. But I'm leaving it in because that seems like the right way for things to work.
One more thing: Somewhere towards the end of this adventure, I figured out that I could hook a test clip onto the serial header underneath the cord's connector, and use an LED to watch the status of that line. Here's a pic taken with my new junky old camera, which works a whole lot better than my old junky old camera:
The flash washed out the LED but it's actually on in this photo. Turns out that DTR low is logic high, but so is the AVR's Reset pin so that's how it should be when the serial port is closed. It would have save me a lot of swearing and head-scratching if I had thought to do this earlier in the process. But I fixed the problem and it's the tits now.