# Arduino Nano with ATmega328P programming script # detailed configuration on http://arduino.cc/en/uploads/Main/boards.txt # required packages: avrdude gcc-avr avr-libc # the output firmware name TARGET=scale # to compile CC=avr-gcc OBJDUMP=avr-objdump OBJCOPY=avr-objcopy SIZE=avr-size CFLAGS=-g -Wall -Werror -O3 -std=c99 # for this project we need float capable printf LDFLAGS=-lm -Wl,-u,vfprintf -lprintf_flt # the target (arduino nano) DEVICE=atmega328p F_CPU=16000000UL # the flasher PROGRAMMER=arduino PORT=/dev/ttyUSB0 # to flash # use the aduino bootlaoder, with a baudrate of 57600 # reset the device to start bootlaoder FLASHER=avrdude -p $(DEVICE) -c $(PROGRAMMER) -P $(PORT) -b 57600 -D # source files to compile SRC = $(wildcard *.c) # header files. HEADER = $(SRC:.c=.h) # object files. OBJ = $(SRC:.c=.o) # listing files. LST = $(SRC:.c=.lst) all: compile $(TARGET).lst flash echo "EEPROM has to be programmed separately" debug: CFLAGS += -DDEBUG debug: all # reset board by setting DTR # the capacitor on DTR with create a pulse on RESET # after reset the bootloader is start # the bootloader can be used to reflash the device reset: stty 57600 raw ignbrk hup < $(PORT) # flash the device using the internal bootloader flash: $(TARGET).hex reset $(FLASHER) -U flash:w:$(TARGET).hex:i # write EEPROM on the device eeprom: $(TARGET)_eeprom.hex reset $(FLASHER) -U eeprom:w:$(TARGET)_eeprom.hex:i # write bootloader and fuses bootloader: wget http://code.google.com/p/arduino/source/browse/trunk/hardware/arduino/bootloaders/atmega/ATmegaBOOT_168_atmega328.hex avrdude -p $(DEVICE) -c usbtiny -U lfuse:w:0xff:m -U hfuse:w:0xda:m -U efuse:w:0x05:m -U flash:w:ATmegaBOOT_168_atmega328.hex:i rm ATmegaBOOT_168_atmega328.hex # create main target firmware compile: $(TARGET).elf $(SIZE) --format=avr --mcu=$(DEVICE) $(TARGET).elf # compile source files %.o: %.c %.h $(CC) $(CFLAGS) -I. -mmcu=$(DEVICE) -DF_CPU=$(F_CPU) -c -o $@ $< # link compiled files %.elf: $(OBJ) $(CC) $(LDFLAGS) -I. -mmcu=$(DEVICE) -DF_CPU=$(F_CPU) -Wl,-Map=$(TARGET).map,--cref -o $@ $(OBJ) # create extended listing for additional information %.lst: %.elf $(OBJDUMP) --section-headers --source $< > $@ # create flashable ihex from firmware %.hex: %.elf $(OBJCOPY) --only-section .text --only-section .data --output-target ihex $< $@ %_eeprom.hex: %.elf $(OBJCOPY) --only-section .eeprom --change-section-lma .eeprom=0 --output-target ihex $< $@ clean: rm -f $(TARGET) $(TARGET).hex $(TARGET).elf $(TARGET).lst $(TARGET).map $(LST) $(OBJ)