
# makefile for play-v6
# created 13 Apr 2015 by Enrico Colombini

# project name
# (should be play-v6, the underscore is for compatibility with the Arduino IDE)
NAME = play_v6

# USB serial port for native Arduino Uno connection
USB_SER_PORT = /dev/ttyUSB0
# serial port for virtual machine development, needs USB driver on host
# and mapping from host serial port (e.g. COM3:) to client /dev/ttyS0 (COM1:)
VM_SER_PORT = /dev/ttyS0
#serial connection baud rate
SER_SPEED = 115200

# microcontroller chip model
MCU = atmega328p
# same for avrdude
AD_MCU = m328p

# C source files
SRC = \
	cmain/main.c \
	play_v6.c \
	interface.c \
	play.c \
	pwm.c \
	userial.c \
	tables.c \
	instrum.c \
	music.c
# include directories
INCLUDE = .

# tools
CC = avr-gcc
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
SIZE = avr-size
RM = rm -f
AVRDUDE = avrdude

# compiler flags, debug flag is needed to extract info from .elf file later
# (the last line generates .d header dependency files)
CFLAGS = \
	-Os \
	-funsigned-char \
	-funsigned-bitfields \
	-fshort-enums \
	-fpack-struct \
	-ffunction-sections \
	-fdata-sections \
	-mmcu=$(MCU) \
	-std=gnu99 \
	-g2 \
	-Wall \
	-MD -MP -MF $(@:%.o=%.d) -MT $(@:%.o=%.d) -MT $(@:%.o=%.o)

# linker flags
LDFLAGS = -Wl,-Map,$(NAME).map

# object .o files corresponding to C source files
OBJ = $(SRC:.c=.o)
# dependency .d files corresponding to C source files
DEP = $(SRC:.c=.d)

# compile, link, generate .hex and .lss files, show sizes
all: $(NAME).elf $(NAME).hex $(NAME).lss size

# compile sources, link to .elf file, generate .map file
$(NAME).elf: ${OBJ}
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJ)

# convert to .hex file for programming
$(NAME).hex: $(NAME).elf
	$(OBJCOPY) -j .text -O ihex  -R .eeprom -R .fuse -R .lock -R .signature $(NAME).elf $(NAME).hex

# generate .lss file with source-commented asm code
$(NAME).lss: $(NAME).elf
	$(OBJDUMP) -d -h -S $(NAME).elf > $(NAME).lss

# show section sizes
size: $(NAME).elf
	@$(SIZE) $(NAME).elf

# send program to target MCU via USB-serial port
prog:
	avrdude -v -p $(AD_MCU) -c arduino -P $(USB_SER_PORT) -b $(SER_SPEED) -e -U flash:w:$(NAME).hex

# for vm development: send program to target MCU via vm-emulated serial port
vprog:
	avrdude -v -p $(AD_MCU) -c arduino -P $(VM_SER_PORT) -b $(SER_SPEED) -e -U flash:w:$(NAME).hex

# cleanup
clean:
	$(RM) $(OBJ) $(DEP) $(NAME).elf $(NAME).map $(NAME).hex $(NAME).lss

# include auto-generated dependencies
-include $(DEP)

