Arduino: How to compile and flash project

From Luky-Wiki
Jump to: navigation, search

Compile Project

New project can be compiled by following command:

avr-gcc -g -O2 -mmcu=atmega328p -o project.elf project.c

It is important to select proper CPU using -mmcu= option. Arduino UNO board is equipped with Atmel ATmega328p. Option -g add debug information to elf file. Debug information are not included in hex file and are useful so I recommend to keep them. In Linux world I use optimization on level 2 (-O2). Maybe it will be better to use -Os (optimization for size) due to limited size of flash memory. I will perform some test first and then decide which one will be better for my projects.

Examine content of elf file

Elf file is binary but elf-objdump can show content of it. With debug information it show valuable data. Command to generate human readable data:

 avr-objdump -x -S project.elf > project.lst

Here is part of output:

void main (void) {
        // setup serial port
 272:   10 92 c5 00     sts     0x00C5, r1
 276:   87 e6           ldi     r24, 0x67       ; 103
 278:   80 93 c4 00     sts     0x00C4, r24

Convert elf file to Intel hex file

Most of the utilities for microprocessors don't accept elf files so it is necessary to create file in Intel hex format. This will produce file in format usable for example with avrdude.

avr-objcopy -j .text -j .data -O ihex project.elf project.hex

If there is EEPROM section then following command generate data for EEPROM memory:

avr-objcopy -j .eeprom --change-section-lma .eeprom=0 -O ihex project.elf project_eeprom.hex

Flash Intel Hex to Atmel mCPU (via ISP)

I am using AVR Dragon programmer in ISP mode. Following command flash data to microprocessor:

avrdude -p m328p -c dragon_isp -P usb -e -U flash:w:project.hex
  • -p m328p - this option select AVR device (Atmel ATMega328p in example)
  • -c dragon_isp - mode of programmer
  • -P usb - Port(interface) on computer side
  • -e - flash memory is normally erased before flash cycle. This option is redundant but it suppress warning message.
  • -U flash:w:project.hex - mode of operation in format <memtype>:r|w|v:<filename>[:format]. It can be selected more that once (for example to write flash memory and EEPROM at once).

Flash Intel Hex to Atmel mCPU (with help of bootloader)

If mCPU contain "Arduino" boot loader or Optiboot then it is possible to flash data using serial line. Check for correct serial line (-P option) and then use command like this:

avrdude -p m328p -c arduino -P /dev/ttyACM<number> -b 115200 -D -U flash:w:project.hex

Note 1: Options are similar to previous section. Consult man page for more details.

Note 2: This will work stable only if reset is properly wired to serial interface.

Note 3: Chip erase is not performed using this method. Therefore there may remain residue from previous flash commands under certain conditions. If this is not acceptable (risk in project) then use ISP method to burn firmware.