Pages: [1]   Go Down
Author Topic: Arduino Reset Problems?  (Read 831 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,

I use eclipse as my IDE of choice. Everything works well except when uploading to an Arduino Mega 2560. For some reason, avrdude does not reset the mega like it does for the uno.

I have looked high and low for a solution, but every suggestion I found seemed to fail.

So I wrote a simple wrapper for avrdude.exe that will automatically reset the arduino before running the real avrdude.exe

Installation:

  • Create a folder called realavrdude (name is important!) in the directory where your current avrdude.exe resides.
  • Move the real avrdude.exe into the folder you just created.
  • Copy my avrdude.exe into the original folder.


How it works:

It is a simple wrapper which will take the COM port from the parameters and connect to the Arduino, it then toggles the DRT. This will reset your arduino.

It then calls realavrdude/avrdude.exe and passes the original parameters to it.
 
C# Code:

Code:
using System;
using System.Diagnostics;
using System.IO.Ports;
using System.Linq;
using System.Threading;

namespace avrdude
{
    internal class Program
    {
        private const string RealAvrDude = "realavrdude\\avrdude.exe";

        private static int Main(string[] args)
        {
            try
            {
                var reset = args.Any(x => x == "-pm2560" || x == "-patmega2560" || x == "-reset");

                var p = new Process
                            {
                                StartInfo =
                                    {
                                        FileName = RealAvrDude,
                                        Arguments = string.Join(" ", args.Where(x => x != "-wait" && x != "-reset")
                                                                         .Select(x => string.Format("\"{0}\"", x))),
                                        RedirectStandardOutput = true,
                                        UseShellExecute = false
                                    },
                                EnableRaisingEvents = true
                            };

                p.OutputDataReceived += POutputDataReceived;

                if (reset)
                {
                    var com = args.FirstOrDefault(x => x.StartsWith("-P"));

                    if (com == null)
                    {
                        Console.WriteLine("Fake avrdude: Cannot find -P parameter. Deferring to real avrdude.");
                        Console.WriteLine();
                    }
                    else
                    {
                        com = com
                            .Replace("-P", "")
                            .Replace("\\", "")
                            .Replace(".", "");

                        Console.Write("Fake avrdude: Resetting device...");

                        using (var serial = new SerialPort(com))
                        {
                            serial.Open();
                            serial.DtrEnable = false;
                            serial.RtsEnable = false;
                            Thread.Sleep(100);
                            serial.DtrEnable = true;
                            serial.RtsEnable = true;
                        }

                        Console.WriteLine(" Done.");
                        Console.WriteLine();
                    }
                }
                else
                {
                    Console.WriteLine("Fake avrdude: Deferring to real avrdude.");
                    Console.WriteLine();
                }

                Console.WriteLine(string.Format("Starting '{0}' {1}", RealAvrDude, p.StartInfo.Arguments));
                Console.WriteLine();

                p.Start();
                p.BeginOutputReadLine();
                p.WaitForExit();

                if (args.Any(x => x == "-wait"))
                {
                    Console.ReadLine();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return 1;
            }

            return 0;
        }

        private static void POutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            Console.Write(e.Data);
        }
    }
}

Hope this helps someone else as much as it has helped me!

-edit:
Modified code to only reset if -pm2560, -patmega2560 or -reset is found.
Edit RealAvrDude variable to point elsewhere if you like.
« Last Edit: February 14, 2012, 07:40:08 am by kizedek » Logged

Offline Offline
Full Member
***
Karma: 11
Posts: 168
IDE for Microsoft Visual Studio and Atmel Studio 6.1, optional USB Debugger
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Nice, believe it or not that's what the Arduino IDE and Visual Studio Plugin also have to do for the 2560!Arduino  should add a boards.txt property to signify a reset is required.

Your solution might help the Eclipse Plugin dev, it might be an idea to let Trump211 know. He might already know but it's not obvious in the arduino source.
Logged

Arduino for Microsoft Visual Studio Pro and Atmel Studio 6.1 http://www.visualmicro.com
Arduino Debugger http://www.visualmicro.com/post/2012/05/05/Debug-Arduino-Overview.aspx

Belgium
Offline Offline
Edison Member
*
Karma: 68
Posts: 1922
Arduino rocks; but with my plugin it can fly rocking the world ;-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The problem is related to the AVRdude configuration.
For nearly all boards boards.txt state to use arduino. It is not for the Mega2560.
I modified board.txt to have arduino also for Mega2560. That didn't work  smiley-mad
So I consider this to be a bug in AVRDude (not supporting arduino for Mega).
I've been looking on how to handle serial communication in eclipse. Didn't find anything yet.
If someone has a pointer on how to handle com ports in an eclipse plugin let me know.
Best regards
Jantje
Logged

Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@visualmicro - Yup, I looked through the Arduino IDE source to see how it resets the device. That's why I toggle RTS as well, not sure exactly what that is meant to do, but the IDE does it... smiley-confuse

@Jantje - Sorry I don't, I tried many things before this, none worked  smiley-sad
Logged

Offline Offline
Full Member
***
Karma: 11
Posts: 168
IDE for Microsoft Visual Studio and Atmel Studio 6.1, optional USB Debugger
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

@Jantje Yes agree, what i was suggesting was that a new boards property would be more elegant should this problem happen with other boards in the future. Currently our code has specifc handling for 2560 which seems a breakage point. The new boards.txt property might be called "ResetBeforeUpload"

@kizedek I think this is a way to describe it... RTS means "Ready To Send", in effect it is a wire in the serial/usb cable. All arduinos auto reset if they detect an RTS signal. Once reset the arduino will wait/listen for a short time for an upload. I have no idea why the 2560 is any different and believe that Jantje knows a lot more than me about avrdude smiley
Logged

Arduino for Microsoft Visual Studio Pro and Atmel Studio 6.1 http://www.visualmicro.com
Arduino Debugger http://www.visualmicro.com/post/2012/05/05/Debug-Arduino-Overview.aspx

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17301
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@Jantje Yes agree, what i was suggesting was that a new boards property would be more elegant should this problem happen with other boards in the future. Currently our code has specifc handling for 2560 which seems a breakage point. The new boards.txt property might be called "ResetBeforeUpload"

@kizedek I think this is a way to describe it... RTS means "Ready To Send", in effect it is a wire in the serial/usb cable. All arduinos auto reset if they detect an RTS signal. Once reset the arduino will wait/listen for a short time for an upload. I have no idea why the 2560 is any different and believe that Jantje knows a lot more than me about avrdude smiley

I think if you look into the history of the arduino auto-reset feature as used on arduino boards you will find the following. The first arduino board released that included the auto reset feature used the RTS signal from the FTDI board to activate the auto-reset. Later boards changed that to use the DTR signal instead for the auto-reset signal. A specific board will therefore use only one or the other of those two signals but never both. As the Arduino IDE can't really tell which signal a specific board may be using it activates both DTR and RTS to make sure the attached board performs the auto-reset. Never found out why the arduino started with using the RTS and then later changed to DTR on their hardware, but history is stranger then fiction sometimes. Most boards made in the last three years will work fine just pulsing the DTR signal.

Lefty
« Last Edit: February 09, 2012, 06:27:21 pm by retrolefty » Logged

Offline Offline
Full Member
***
Karma: 11
Posts: 168
IDE for Microsoft Visual Studio and Atmel Studio 6.1, optional USB Debugger
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks retrolefty, Very useful I didn't know all that and will make sure I do both when needed in normal serial comms.
Logged

Arduino for Microsoft Visual Studio Pro and Atmel Studio 6.1 http://www.visualmicro.com
Arduino Debugger http://www.visualmicro.com/post/2012/05/05/Debug-Arduino-Overview.aspx

Pages: [1]   Go Up
Jump to: