Setting up avrdude and arduino

The end goal is to learn how to program in C using arduinos, but before that I want to start from a simpler base and build everything up instead of jumping into everything at once.  I always like to build up layers of complexity (the goal is always divide a large problem into a set of smaller problems). This is done on Ubuntu 14.04. So start with the initial things:

  1. See if the board works using preexisting arduino software
  2. Get avrdude to work

I am using ProMicro 3.3V. This is a very simple board and doesn’t have a lot of extra things going on. I am sure there are better boards for learning but this is what I had lying around. The board looks like this:

Pro Micro - 3.3V/8MHz

The first part using the blinking LEDs example I will toggle the leds near pins 16 and 8. I want to first make sure that I can even communicate device using pre-existing software. My learning technique is to build on complexity instead of all at once. I first downloaded the arduino software. After I download the software I then go to File>Preferences and then copy this link into Additional Boards Manager URLs: https://raw.githubusercontent.com/sparkfun/Arduino_Boards/master/IDE_Board_Manager/package_sparkfun_index.json. The point of this is to let the software know about the other boards that the software can connect to, otherwise it has the bare minimum of boards to use. Go to Tools>Boards>Board Manager and then look for ‘sparkfun’ and install SparkFun AVR Boards or follow directions listed here. This will allow me to upload code onto the promicro. Close the software and find the .arduino* folder and then look for the file preferences.txt and in there change “upload.verbose=false” to “upload.verbose=true” so that it can help us with avrdude.

I copied the blinking code from here. The code looks like this:

/* Pro Micro Test Code
   by: Nathan Seidle
   modified by: Jim Lindblom
   SparkFun Electronics
   date: September 16, 2013
   license: Public Domain - please use this code however you'd like.
   It's provided as a learning tool.

   This code is provided to show how to control the SparkFun
   ProMicro's TX and RX LEDs within a sketch. It also serves
   to explain the difference between Serial.print() and
   Serial1.print().
*/

int RXLED = 17;  // The RX LED has a defined Arduino pin
// The TX LED was not so lucky, we'll need to use pre-defined
// macros (TXLED1, TXLED0) to control that.
// (We could use the same macros for the RX LED too -- RXLED1,
//  and RXLED0.)

void setup()
{
 pinMode(RXLED, OUTPUT);  // Set RX LED as an output
 // TX LED is set as an output behind the scenes

 Serial.begin(9600); //This pipes to the serial monitor
 Serial1.begin(9600); //This is the UART, pipes to sensors attached to board
}

void loop()
{
 Serial.println("Hello world");  // Print "Hello World" to the Serial Monitor
 Serial1.println("Hello!");  // Print "Hello!" over hardware UART

 digitalWrite(RXLED, LOW);   // set the LED on
 TXLED0; //TX LED is not tied to a normally controlled pin
 delay(1000);              // wait for a second
 digitalWrite(RXLED, HIGH);    // set the LED off
 TXLED1;
 delay(1000);              // wait for a second
}

Once the code is copied over to the Arduino IDE then save the program and then Sketch>Verify/Compile.  To export the binary hex file which we will use in setting up avrdude go to Sketch>Export compiled Binary.  Once these are both done it is time to upload everything to the promicro which is done by doing Sketch>Upload and the resulting verbose output should look something like this:

Sketch uses 5078 bytes (17%) of program storage space. Maximum is 28672 bytes.
Global variables use 346 bytes (13%) of dynamic memory, leaving 2214 bytes for local variables. Maximum is 2560 bytes.
Forcing reset using 1200bps open/close on port /dev/ttyACM0
PORTS {/dev/ttyACM0, } / {} => {}
PORTS {} / {/dev/ttyACM0, } => {/dev/ttyACM0, }
Found upload port: /dev/ttyACM0
/home/ashwini/Downloads/arduino-1.8.3/hardware/tools/avr/bin/avrdude -C/home/ashwini/.arduino15/packages/SparkFun/hardware/avr/1.1.6/avrdude.conf -v -patmega32u4 -cavr109 -P/dev/ttyACM0 -b57600 -D -Uflash:w:/tmp/arduino_build_264931/blinky.ino.hex:i

avrdude: Version 6.3, compiled on Jan 17 2017 at 11:00:16
Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
Copyright (c) 2007-2014 Joerg Wunsch

System wide configuration file is “/home/ashwini/.arduino15/packages/SparkFun/hardware/avr/1.1.6/avrdude.conf”
User configuration file is “/home/ashwini/.avrduderc”
User configuration file does not exist or is not a regular file, skipping

Using Port : /dev/ttyACM0
Using Programmer : avr109
Overriding Baud Rate : 57600
AVR Part : ATmega32U4
Chip Erase delay : 9000 us
PAGEL : PD7
BS2 : PA0
RESET disposition : dedicated
RETRY pulse : SCK
serial program mode : yes
parallel program mode : yes
Timeout : 200
StabDelay : 100
CmdexeDelay : 25
SyncLoops : 32
ByteDelay : 0
PollIndex : 3
PollValue : 0x53
Memory Detail :

Block Poll Page Polled
Memory Type Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack
———– —- —– —– —- —— —— —- —— —– —– ———
eeprom 65 20 4 0 no 1024 4 0 9000 9000 0x00 0x00
flash 65 6 128 0 yes 32768 128 256 4500 4500 0x00 0x00
lfuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00
hfuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00
efuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00
lock 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00
calibration 0 0 0 0 no 1 0 0 0 0 0x00 0x00
signature 0 0 0 0 no 3 0 0 0 0 0x00 0x00

Programmer Type : butterfly
Description : Atmel AppNote AVR109 Boot Loader

Connecting to programmer: .
Found programmer: Id = “CATERIN”; type = S
Software Version = 1.0; No Hardware Version given.
Programmer supports auto addr increment.
Programmer supports buffered memory access with buffersize=128 bytes.

Programmer supports the following devices:
Device code: 0x44

avrdude: devcode selected: 0x44
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e9587 (probably m32u4)
avrdude: reading input file “/tmp/arduino_build_264931/blinky.ino.hex”
avrdude: writing flash (5078 bytes):

Writing | ################################################## | 100% 0.40s

avrdude: 5078 bytes of flash written
avrdude: verifying flash memory against /tmp/arduino_build_264931/blinky.ino.hex:
avrdude: load data flash data from input file /tmp/arduino_build_264931/blinky.ino.hex:
avrdude: input file /tmp/arduino_build_264931/blinky.ino.hex contains 5078 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.06s

avrdude: verifying …
avrdude: 5078 bytes of flash verified

avrdude done. Thank you.

You should 2 LEDs flashing in an alternating fashion.  If you don’t get his output then it means  you have done this right.  Also if you don’t get the long output it means that you have probably not set upload.verbose to true.

Loading the Hex directly from avrdude

The arduino software verbose output was pretty useful it gave us a pretty good understanding of what is happening under the hood for the upload process.

I used the output to figure out how to run avrdude from command line.  Take a look at the following output:

/home/ashwini/Downloads/arduino-1.8.3/hardware/tools/avr/bin/avrdude -C/home/ashwini/.arduino15/packages/SparkFun/hardware/avr/1.1.6/avrdude.conf -v -patmega32u4 -cavr109 -P/dev/ttyACM0 -b57600 -D -Uflash:w:/tmp/arduino_build_264931/blinky.ino.hex:i

I replaced “/home/ashwini/Downloads/arduino-1.8.3/hardware/tools/avr/bin/avrdude” with the my machine’s version of avrdude (I had to upgrade to avrdude 6.3 on Ubuntu 14.04, which is outlined here).

Then next part was to get my own version of avrdude.conf file.  This file is used to figure out how avrdude is going to talk to a certain chip type.  If you go in the file and look for “ATMega32U4” you should start seeing information about the chip and what kind of memory is available.  I copied mine from the sparkfun github repo then extracted the bz2 file and copied over the avrdude.conf file to my git repo.

The -p indicates the part number which is found in the avrdude.conf file, -c is the programmer file which is also in the conf file, -P is the port, -D is disable autoerase for flash memory, -U performs a memory operation, flash is the memory type to do the operation on and w indicates that we will read data from a specified file which is the hex file and write it to device memory, and the i indicates the type of hex file it which in our case is an Intel Hex.  This information can also be found when you type in “man avrdude”.

The other thing to note was that for Promicro you have to open the device at a baud rate of 1200 to reset the device and only after that can you load all the hex file. This is done by the following set of lines:

stty -F /dev/serial/by-id/usb-SparkFun_SparkFun_Pro_Micro-if00 1200;
sleep 1s;

and then you can do:

avrdude -C/home/ashwini/git-repos/arduino/avr-1.1.6/avrdude.conf -v -patmega32u4 -cavr109 -P/dev/ttyACM0 -b57600 -D -Uflash:w:/home/ashwini/git-repos/arduino/blinky/blinky.ino.promicro.hex:i -v

This is also in a shell file called: blinky_avr_script.sh . You can see i went through multiple iterations to slowly remove parts of the arduino stuff until I got to a point where I can run it seperately.

Once all this was completed my output looked something like this:

avrdude: Version 6.3
Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
Copyright (c) 2007-2014 Joerg Wunsch

System wide configuration file is “/home/ashwini/git-repos/arduino/avr-1.1.6/avrdude.conf”
User configuration file is “/home/ashwini/.avrduderc”
User configuration file does not exist or is not a regular file, skipping

Using Port : /dev/ttyACM0
Using Programmer : avr109
Overriding Baud Rate : 57600
AVR Part : ATmega32U4
Chip Erase delay : 9000 us
PAGEL : PD7
BS2 : PA0
RESET disposition : dedicated
RETRY pulse : SCK
serial program mode : yes
parallel program mode : yes
Timeout : 200
StabDelay : 100
CmdexeDelay : 25
SyncLoops : 32
ByteDelay : 0
PollIndex : 3
PollValue : 0x53
Memory Detail :

Block Poll Page Polled
Memory Type Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack
———– —- —– —– —- —— —— —- —— —– —– ———
eeprom 65 20 4 0 no 1024 4 0 9000 9000 0x00 0x00
flash 65 6 128 0 yes 32768 128 256 4500 4500 0x00 0x00
lfuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00
hfuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00
efuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00
lock 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00
calibration 0 0 0 0 no 1 0 0 0 0 0x00 0x00
signature 0 0 0 0 no 3 0 0 0 0 0x00 0x00

Programmer Type : butterfly
Description : Atmel AppNote AVR109 Boot Loader

Connecting to programmer: .
Found programmer: Id = “CATERIN”; type = S
Software Version = 1.0; No Hardware Version given.
Programmer supports auto addr increment.
Programmer supports buffered memory access with buffersize=128 bytes.

Programmer supports the following devices:
Device code: 0x44

avrdude: devcode selected: 0x44
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e9587 (probably m32u4)
avrdude: safemode: hfuse reads as D8
avrdude: safemode: efuse reads as FB
avrdude: reading input file “/home/ashwini/git-repos/arduino/blinky/blinky.ino.promicro.hex”
avrdude: writing flash (5078 bytes):

Writing | ################################################## | 100% 0.41s

avrdude: 5078 bytes of flash written
avrdude: verifying flash memory against /home/ashwini/git-repos/arduino/blinky/blinky.ino.promicro.hex:
avrdude: load data flash data from input file /home/ashwini/git-repos/arduino/blinky/blinky.ino.promicro.hex:
avrdude: input file /home/ashwini/git-repos/arduino/blinky/blinky.ino.promicro.hex contains 5078 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.06s

avrdude: verifying …
avrdude: 5078 bytes of flash verified

avrdude: safemode: hfuse reads as D8
avrdude: safemode: efuse reads as FB
avrdude: safemode: Fuses OK (E:FB, H:D8, L:DE)

avrdude done. Thank you.

Just as a side note the ProMicro is similar to arduino leonardo. I hope this helps! Let me know if you have any questions or suggestions on how to improve the information.

Side note: I ran into issues accessing the /dev/ttyACM0 and blacklisted the device from the modem manager as described here. I went into /etc/udev/rules.d/77-mm-usb-device-blacklist.rules and added:

ATTRS{idVendor}==”1b4f”, ATTRS{idProduct}==”9203″, ENV{ID_MM_DEVICE_IGNORE}=”1″
ATTRS{idVendor}==”1b4f”, ATTRS{idProduct}==”9204″, ENV{ID_MM_DEVICE_IGNORE}=”1″

Some people had to actually remove network-manager and/or modem-manager, and only then it would work for them. This is why it is important to get everything working before hand before you start going to the command line approach.

Here are a list of useful links:

https://balau82.wordpress.com/2011/03/29/programming-arduino-uno-in-pure-c/
https://www.sparkfun.com/products/12640https://learn.sparkfun.com/tutorials/pro-micro–fio-v3-hookup-guide
https://learn.sparkfun.com/tutorials/pro-micro–fio-v3-hookup-guide/troubleshooting-and-faq
http://forum.arduino.cc/index.php?topic=217910.0
http://forum.arduino.cc/index.php?topic=111213.0
http://cholla.mmto.org/computers/arduino/boards/promicro/
http://www.nongnu.org/avrdude/user-manual/avrdude_4.html#Option-Descriptions
http://ubuntuhandbook.org/index.php/2017/01/install-avrdude-6-4-ubuntu-16-04/
https://github.com/sparkfun/Arduino_Boards/blob/master/IDE_Board_Manager/sparkfunboards.1.1.6.tar.bz2
http://xcom.ee/project_files/beer_temp2/beertemp2_buutload_howto.txt
https://www.arduino.cc/en/Guide/ArduinoLeonardoMicrot

All the documentation and the relevant code can be found on my github