AVR - How To Program An AVR Chip in Linux
AVR - How To Program An AVR Chip in Linux
I don't have the time for a full explanation, but I can give you cookbook-style
the commands I use on my Linux box to program AVRs:
Preparations
•On Ubuntu, make sure several required packages are installed:
•For each project I create a dedicated subfolder (and I don't mind long
names):
mkdir waveShare4digit8segmentDisplay; cd
waveShare4digit8segmentDisplay
Create source
•Edit the source file with your favorite text editor: vi project.cpp
Settings
The variables below depend on the exact controller you want to program:
Compile
•Third step is to create an Intel Hex file, this is the file that is actually sent
to the programmer: avr-objcopy -j .text -j .data -O ihex ${src).elf $
{src).flash.hex
Programming
Makefile
baud=19200
src=project
avrType=attiny2313
avrFreq=4000000 # 4MHz for accurate baudrate timing
programmerDev=/dev/ttyUSB003
programmerType=arduino
.PHONY: backup clean disassemble dumpelf edit eeprom elf flash fuses help
hex makefile object program
help:
@echo 'backup Read all known memory types from controller and write
it into a file. Available memory types: $(memoryTypes)'
@echo 'clean Delete automatically created files.'
@echo 'disassemble Compile source code, then disassemble object file to
mnemonics.'
@echo 'dumpelf Dump the contents of the .elf file. Useful for information
purposes only.'
@echo 'edit Edit the .cpp source file.'
@echo 'eeprom Extract EEPROM data from .elf file and program the
device with it.'
@echo 'elf Create $(src).elf'
@echo 'flash Program $(src).hex to controller flash memory.'
@echo 'fuses Extract FUSES data from .elf file and program the device
with it.'
@echo 'help Show this text.'
@echo 'hex Create all hex files for flash, eeprom and fuses.'
@echo 'object Create $(src).o'
@echo 'program Do all programming to controller.'
edit:
vi $(src).cpp
makefile:
vi Makefile
object:
avr-gcc $(cflags) -mmcu=$(avrType) -Wa,-ahlmns=$(src).lst -c -o $(src).o
$(src).cpp
elf: object
avr-gcc $(cflags) -mmcu=$(avrType) -o $(src).elf $(src).o
chmod a-x $(src).elf 2>&1
hex: elf
avr-objcopy -j .text -j .data -O ihex $(src).elf $(src).flash.hex
avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-
section-lma .eeprom=0 -O ihex $(src).elf $(src).eeprom.hex
avr-objcopy -j .fuse -O ihex $(src).elf $(src).fuses.hex --change-section-lma
.fuse=0
srec_cat $(src).fuses.hex -Intel -crop 0x00 0x01 -offset 0x00 -O $
(src).lfuse.hex -Intel
srec_cat $(src).fuses.hex -Intel -crop 0x01 0x02 -offset -0x01 -O $
(src).hfuse.hex -Intel
srec_cat $(src).fuses.hex -Intel -crop 0x02 0x03 -offset -0x02 -O $
(src).efuse.hex -Intel
disassemble: elf
avr-objdump -s -j .fuse $(src).elf
avr-objdump -C -d $(src).elf 2>&1
eeprom: hex
#avrdude -p$(avrType) -c$(programmerType) -P$(programmerDev) -b$
(baud) -v -U eeprom:w:$(src).eeprom.hex
date
fuses: hex
avrdude -p$(avrType) -c$(programmerType) -P$(programmerDev) -b$
(baud) -v -U lfuse:w:$(src).lfuse.hex
#avrdude -p$(avrType) -c$(programmerType) -P$(programmerDev) -b$
(baud) -v -U hfuse:w:$(src).hfuse.hex
#avrdude -p$(avrType) -c$(programmerType) -P$(programmerDev) -b$
(baud) -v -U efuse:w:$(src).efuse.hex
date
dumpelf: elf
avr-objdump -s -h $(src).elf
backup:
@for memory in $(memoryTypes); do \
avrdude -p $(avrType) -c$(programmerType) -P$(programmerDev) -b$
(baud) -v -U $$memory:r:./$(avrType).$$memory.hex:i; \
done
Hello World
Let me throw in an 'Hello World' that makes a controller pin 2 (PB3) (eg.
ATtiny13, ATtiny45, ATtiny85) toggle at 1Hz. Attach an LED and series resistor
to the pin and the LED should start to blink.
•make edit
i
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRB = 0x08;
while (1) {
PORTB = 0x00; _delay_ms(500);
PORTB = 0x08; _delay_ms(500);
}
}
<ESC>:wq
•make flash
Done!