Arduino: How to compile and flash project
Contents
Compile
New project can be compiled 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. They are omitted 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 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 UBRR0H = UBRRH_VALUE; 272: 10 92 c5 00 sts 0x00C5, r1 UBRR0L = UBRRL_VALUE; 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