-
Compilation en ligne de commande avec gcc.
- Oubliez le "concurrent" clang.
- Compilation classique avec avr-gcc :
-
uniquement trois paquetages à installer :
# apt install gcc-avr avr-libc avrdude
- compilation par ligne de commande :
$ avr-gcc -mmcu=atmega328p -DF_CPU=16000000UL -c -Wall -I. -Os timer.c -o timer.o
$ avr-gcc -mmcu=atmega328p -g -lm -Wl,--gc-sections -o timer.elf timer.o
- téléversement sur le microcontrôleur en ligne de commande :
$ avr-objcopy -j .text -j .data -O ihex timer.elf timer.hex
$ stty -F /dev/ttyACM0 hupcl
$ avrdude -F -v -p atmega328p -c stk500v1 -b 115200 -P /dev/ttyACM0 -U flash:w:timer.hex
- maitrise totale du code ;
- des programmes compacts et efficaces.
- Automatisation avec make (chargement série):
export CC = avr-gcc
export LD = avr-gcc
export MCU = atmega328p
export FCPU = 16000000
export TARGET_ARCH = -mmcu=$(MCU)
export CFLAGS = -Wall -I. -DF_CPU=$(FCPU) -Os
export LDFLAGS = -g $(TARGET_ARCH) -lm -Wl,--gc-sections
TERM = /dev/ttyACM0
PGMERISP = -c stk500v1 -b 115200 -P $(TERM)
export DUDE = /usr/bin/avrdude -F -v -p $(MCU)
TARGET = timer
C_SRC = $(wildcard *.c)
OBJS = $(C_SRC:.c=.o)
all: $(TARGET).hex
clean:
rm -f $(TARGET).o $(TARGET).hex $(TARGET).elf
$(TARGET).elf: $(OBJS)
$(LD) $(LDFLAGS) -o $@ $(OBJS)
$(TARGET).hex: $(TARGET).elf
avr-objcopy -j .text -j .data -O ihex $(TARGET).elf $(TARGET).hex
upload: $(TARGET).hex
stty -F $(TERM) hupcl # reset
$(DUDE) $(PGMERISP) -U flash:w:$(TARGET).hex
size: $(TARGET).elf
avr-size --format=avr --mcu=$(MCU) $(TARGET).elf
- Téléversement avec DFU/USB :
# apt install dfu-programmer
- Automatisation avec make (chargement USB) :
export CC = avr-gcc
export LD = avr-gcc
export MCU = atmega32u4
export FCPU = 16000000
export FLAGS = -mmcu=$(MCU)
export CFLAGS = -Wall $(FLAGS) -DF_CPU=$(FCPU) -Os
export LDFLAGS = $(FLAGS)
export PROGRAMMER = dfu-programmer
TARGET = pad
SOURCES = $(wildcard *.c)
OBJECTS = $(SOURCES:.c=.o)
all: $(TARGET).hex
clean:
rm -f *.o $(TARGET).hex $(TARGET)
$(TARGET): $(OBJECTS)
$(TARGET).hex: $(TARGET)
avr-objcopy -j .text -j .data -O ihex $(TARGET) $(TARGET).hex
upload: $(TARGET).hex
$(PROGRAMMER) $(MCU) erase
$(PROGRAMMER) $(MCU) flash $(TARGET).hex
$(PROGRAMMER) $(MCU) reset
size: $(TARGET)
avr-size --format=avr --mcu=$(MCU) $(TARGET)