Arduino Micro (ATmega32U4) and automatic upload w/ Eclipse

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
o6k7
 
Posts: 9
Joined: Thu May 27, 2010 9:31 pm

Arduino Micro (ATmega32U4) and automatic upload w/ Eclipse

Post by o6k7 »

Hi All,

Because of the way Arduino Micro (ATmega32U4) is designed to handle serial communication, special software reset (bootloader initiation) is required in order for Micro to accept any new upload. Arduino IDE does it by connecting to the Micro's serial interface with 1200 bodrate and gracefully disconnecting. This action signals to the bootloader that it has to reboot and wait for 8 seconds for new upload. Then Arduino IDE starts avrdude uploading new image.

Since I work with Eclipse, I am trying to find a workaround for annoying requirement to push reset button every time I upload new code revision. Of course, a simple Python script could do the trick of connecting disconnecting on 1200. However, it seems there is no such thing as avrdude "pre-upload" script to run software reset prior to flashing.

Does anyone know how to solve this Arduino Micro/Leonardo software reset problem on Eclipse Juno with Jantje's Arduino Eclipse Plugin (http://www.baeyens.it/eclipse/)?

User avatar
o6k7
 
Posts: 9
Joined: Thu May 27, 2010 9:31 pm

Re: Arduino Micro (ATmega32U4) and automatic upload w/ Eclipse

Post by o6k7 »

After researching the problem I resorted to the following workaround.

I wrote a Python wrapper for avrdude that detects if "-P PORT" option is present in the arguments and performs a soft reset as expected by Arduino Micro/Leonardo. Then the wrapper calls the real avrdude passing all the arguments.

Feel free to post updates here if you find a way to improve the script. The initial version probably misses some possible avrdude usage scenarios and might not handle all errors. For example, one strange thing happens when Eclipse AVR plugin runs avrdude with "-c?" option to get the list of supported programmers. When executed (even from the command line) avrdude returns the list but for some reason exits with a non-zero exit code. So error handling of subprocess.check_call had to be commented out.

Solution has been tested on Ubuntu 11.10 running Eclipse Juno, Arduino IDE 1.0.2 (Eclipse AVR plugin uses its tools), avrdude version 5.11 and Arduino Micro board. Just rename the real avrdude as avrdude.orig and copy paste the code to a new avrdude file. No more hardware resets before uploading the code!

Code: Select all

#!/usr/bin/env python
import sys
import subprocess
import serial
import argparse
import time

# path to the real avrdude
AVRDUDE_PATH="/opt/arduino-1.0.2/hardware/tools"

# real name of avrdude
AVRDUDE_NAME="avrdude.orig"

# prepare argument parser to understand -P PORT
parser = argparse.ArgumentParser()
parser.add_argument("-P", dest="port")
# parse only known parameters
args = parser.parse_known_args()

# if port argument is present perform soft reset
if args[0].port:
  try: # try to initiate serial port connection on PORT with 1200 baudrate
    ser = serial.Serial(
      port=args[0].port,
      baudrate=1200,
      parity=serial.PARITY_NONE,
      stopbits=serial.STOPBITS_ONE,
      bytesize=serial.EIGHTBITS
    )
  except serial.SerialException, e:
    print "pySerial error: " + str(e) + "\n"
    sys.exit(1) 

  try: # try to open PORT
    ser.isOpen()
  except serial.SerialException:
    print "pySerial error: " + str(e) + "\n"
    sys.exit(1) 

  # and close it immediately signaling to bootloader that flashing is imminent
  ser.close()

  # wait 2 second to ensure Arduino is ready
  time.sleep(2)

  # args is a tuple with parsed -P PORT and the rest of the arguments, so it needs to be join back
  avrdude_args=" ".join(str(x) for x in args[1]) + " -P" + args[0].port

else: # if no port argument is present
  # join all other arguments to be ready for avrdude invokation
  avrdude_args=" ".join(str(x) for x in args[1])

try: # try to invoke avrdude passing all the options
  subprocess.check_call(AVRDUDE_PATH + "/" + AVRDUDE_NAME + " " + avrdude_args, shell=True)
except subprocess.CalledProcessError, e: pass
#  print "avrdude error:\n", e.output
#  sys.exit(2)

sys.exit()

User avatar
mike31416
 
Posts: 126
Joined: Wed Aug 26, 2009 12:06 pm

Re: Arduino Micro (ATmega32U4) and automatic upload w/ Eclipse

Post by mike31416 »

Hi,

What value are you using for the programmer hardware specified by the avrdude -c option?

I am also using Eclipse. This is what I am currently using, but it does not work:

C:\WinAVR-20100110\bin\avrdude -pm32u4 -cavr109 -PCOM12 -Uflash:w:DS1307.hex:a

This is the response:

Connecting to programmer: .
Found programmer: Id = "╧ #"; type = BANNED
Software Version = . ; Hardware Version = .

Almost looks like a baud rate issue. I have also tried adding -b9600 (and other rates) and get the same results. I press the reset button and wait for the com port to show before starting avrdude.

Mike

User avatar
o6k7
 
Posts: 9
Joined: Thu May 27, 2010 9:31 pm

Re: Arduino Micro (ATmega32U4) and automatic upload w/ Eclipse

Post by o6k7 »

Hi Mike,

Here is the whole command issued by my Eclipse (it runs on Ubuntu, so some adjustment is required for Windows).

Code: Select all

/opt/arduino-1.0.2/hardware/tools/avrdude -patmega32u4 -cavr109 -P/dev/ttyMicro -b57600 -Uflash:w:Blink.hex:a -C/opt/arduino-1.0.2/hardware/tools/avrdude.conf
I think that when you specify MCU (atmega32u4) and programmer protocol (avr109) as well as the full path to the avrdude config file from Arduino IDE, it should work.

User avatar
mike31416
 
Posts: 126
Joined: Wed Aug 26, 2009 12:06 pm

Re: Arduino Micro (ATmega32U4) and automatic upload w/ Eclipse

Post by mike31416 »

I don't have avrdude.exe or avrdude.conf in arduino-1.0.2/hardware/tools. Instead avrdude.exe is in arduino-1.0.2/hardware/tools/avr/bin and there is no avrdude.conf. Must be something different in the linux vs windows install. I'll keep looking, just wanted to make sure I had the correct parameter for the -c option.

Thanks for the response,
Mike

User avatar
mike31416
 
Posts: 126
Joined: Wed Aug 26, 2009 12:06 pm

Re: Arduino Micro (ATmega32U4) and automatic upload w/ Eclipse

Post by mike31416 »

Found the avrdude.conf file in arduino-1.0.2/hardware/tools/avr/etc but still no go. Got to be something stupid ;)

User avatar
mike31416
 
Posts: 126
Joined: Wed Aug 26, 2009 12:06 pm

Re: Arduino Micro (ATmega32U4) and automatic upload w/ Eclipse

Post by mike31416 »

Found out what I was doing wrong. After reading more about the micro boot loader I saw that it creates a port for the boot loader and then after 8 seconds it closes it and creates a new one for serial I/O. I was using the serial I/O port. Once I switched to the boot loader port it worked.

Mike

User avatar
o6k7
 
Posts: 9
Joined: Thu May 27, 2010 9:31 pm

Re: Arduino Micro (ATmega32U4) and automatic upload w/ Eclipse

Post by o6k7 »

Good to know that it worked out for you at the end of the day. So, are you using the wrapper script or resetting the board manually each time you invoke avrdude?

User avatar
mike31416
 
Posts: 126
Joined: Wed Aug 26, 2009 12:06 pm

Re: Arduino Micro (ATmega32U4) and automatic upload w/ Eclipse

Post by mike31416 »

Manually right now. I want to see if there is a better way and if not I will use your method. If I do come up with another solution I will post it here.

Mike

uniudra
 
Posts: 1
Joined: Thu Jan 31, 2013 11:49 pm

Re: Arduino Micro (ATmega32U4) and automatic upload w/ Eclipse

Post by uniudra »

@paranoid.rat, thanks for your code!
I have Leonardo board and I am tired manually press reset button. :)

I am trying to use your code on Ubuntu 12.04 from command line:
  • I moved avrdude to avrdude.orig
  • Created new avrdude file
  • Made new avrdude file executable for all
  • Pasted your code, changed AVRDUDE_PATH and AVRDUDE_NAME
After running next command I getting errors:

Code: Select all

/usr/share/arduino/hardware/tools/avrdude -P /dev/ttyACM0 -C /usr/share/arduino/hardware/tools/avrdude.conf -DV -p atmega32u4 -c avr109 -b 57600 -U flash:w:test.hex:i
File "/usr/share/arduino/hardware/tools/avrdude", line 22
SyntaxError: Non-ASCII character '\xc2' in file /usr/share/arduino/hardware/tools/avrdude on line 22, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
Line 22:

Code: Select all

  try: # try to initiate serial port connection on PORT with 1200 baudrate
My python version is Python 2.7.3 (default, Aug 1 2012, 05:14:39)

Then I tried to set encoding with next code:

Code: Select all

# coding=utf-8
But got next error:
File "/usr/share/arduino/hardware/tools/avrdude", line 24
    ser = serial.Serial(
^
IndentationError: expected an indented block
What is my fault?
Thanks!

User avatar
o6k7
 
Posts: 9
Joined: Thu May 27, 2010 9:31 pm

Re: Arduino Micro (ATmega32U4) and automatic upload w/ Eclipse

Post by o6k7 »

Hi uniudra!

It seems to be the problem similar to the one described here

It is not the code problem, but the problem of the Adafruit's forum software. When you copy paste the code from the forum, the text includes a character 0xC2 (hex, or in python format \xC2) for some (or all?) tabs/spaces used for indentation in the code. try to copy paste it into a regular editor like gedit and then see with the hex editor if you have "bad" characters. Or alternatively, run a script from here to substitute these chars for the good ones.

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Arduino products from Adafruit”