CC				=avr-gcc
CC_FLAGS		=-Wall -Werror -g -fshort-enums
SRC				=main.c mcuPeripherals.c my_soft_uart.c
DEFINES			=CC_GCC
TARGET			=attiny13a
OUT_DIR			=./gnu-output/
OUT_FILE_NAME	=out
OUT_ELF			=$(OUT_FILE_NAME).elf
OUT_MAP			=$(OUT_FILE_NAME).map
OUT_HEX			=$(OUT_FILE_NAME).hex
OPTIMIZE		=s
SIZE			=avr-size					# Utility to print the memory usage
SIZE_FLAGS		=-C
OBJ_COPY		=avr-objcopy				# Utility to extract the HEX from the generated ELF file

all:
	$(CC) -O$(OPTIMIZE) -D $(DEFINES) $(SRC) -mmcu=$(TARGET) $(CC_FLAGS) -o $(OUT_DIR)$(OUT_ELF) -Wl,-Map,$(OUT_DIR)$(OUT_MAP)
	$(OBJ_COPY) -j .text -j .data -O ihex $(OUT_DIR)$(OUT_ELF) $(OUT_DIR)$(OUT_HEX)
	$(SIZE) $(SIZE_FLAGS) $(OUT_DIR)$(OUT_ELF) --mcu=$(TARGET)

# avr-gcc options:
# -c							Compile only, don't link the files
# -fshort-enums					Allocate only as many bytes as it needs for the declared range of possible values
# -g							Embed debug information in the generated files
# -mmcu=attiny13a				Specify the target to compile for
# -std=gnu99					Specify the C language standard used
# -Wall							Generate warnings on everything
# -Werror						Threat each warning as error
# -Wl,-Map,out.map				Generate map file

# avr-objdump options:
# -h 							Display the memory usage of the generated elf file
# -S							Display disassembly of the source code

# avr-objcopy options:
# -j 							Extract only this section from the elf file in to the ouput hex file
# -O 							Specify the output hex format

# Examples:
#avr-gcc -g -c main.c -mmcu=attiny13a -Wall -Werror					# Compile to generate the object file
#avr-gcc -g main.o -o main.elf -mmcu=attiny13a -Wl,-Map,main.map	# Link to generate the elf binary file and the map file
#avr-objdump -h main.elf											# Show the memory usage
#avr-objcopy -j .text -j .data -O ihex main.elf main.hex			# Generate the output hex file from the elf