I237 Riistvaralähedane programmerimine 2016
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.5KB

  1. ###############################################################################
  2. #
  3. # Simple Makefile for Arduino MEGA 2560 C projects
  4. #
  5. ###############################################################################
  6. CC = avr-gcc
  7. OBJCOPY = avr-objcopy
  8. AVRDUDE = avrdude
  9. CODE_FORMATTER = tooling/format-code.sh
  10. AVRSIZE = avr-size
  11. BOARD = atmega2560
  12. # Use shell command export to define it
  13. # Example: export ARDUINO=/dev/ttyACM0
  14. DEVICE = $(ARDUINO)
  15. # Build artifacts
  16. BINDIR = bin
  17. TARGET = $(BINDIR)/$(BOARD)-user-code.ihx
  18. ELF = $(BINDIR)/$(BOARD)-user-code.elf
  19. # Source files. wildard "uses" all .c files in src directory
  20. SRCDIR = src
  21. BUILD_LIBS_DIR = lib
  22. SRC = $(wildcard $(SRCDIR)/*.c $(BUILD_LIBS_DIR)/*/*.c)
  23. # Define object files from .c files defined above
  24. OBJ=$(SRC:.c=.o)
  25. # Compiler flags
  26. # Note that those beginning with -D are acctually pre-processor macros
  27. # -Wall ... -Wfatal-errors All possible warning options
  28. # -Os Optimize code. The special option -Os is meant to turn on all -O2
  29. # optimizations that are not expected to increase code size.
  30. # -std=c11 use C11 standard
  31. CFLAGS = -Wall \
  32. -Wextra \
  33. -Wpedantic \
  34. -Wformat \
  35. -pedantic-errors \
  36. -Werror \
  37. -Wfatal-errors \
  38. -Os \
  39. -flto \
  40. -fdata-sections \
  41. -ffunction-sections \
  42. -mmcu=$(BOARD) \
  43. -DF_CPU=16000000UL \
  44. -DGIT_DESCR=\"$(shell git describe --abbrev=6 --dirty --always --tags --long)\" \
  45. -ffreestanding \
  46. -std=c11
  47. # Linker flags
  48. LDFLAGS = -mmcu=$(BOARD) \
  49. -flto \
  50. -Wl,-gc-sections
  51. OBJCOPYARGS = -O ihex \
  52. -R .eeprom
  53. # FIXME Find out why some Arduinos require -D to write code
  54. AVRDUDEARGS = -p $(BOARD) \
  55. -c wiring \
  56. -F \
  57. -P $(DEVICE) \
  58. -b 115200 \
  59. -V \
  60. -D
  61. AVRSIZEARGS = -C \
  62. --mcu=$(BOARD)
  63. all: $(ELF) $(TARGET)
  64. %.o : %.c
  65. $(CC) -c $(CFLAGS) -o $*.o $<
  66. $(ELF): $(OBJ)
  67. $(CC) $(LDFLAGS) $^ -o $@
  68. $(TARGET):
  69. $(OBJCOPY) $(OBJCOPYARGS) $(ELF) $(TARGET)
  70. clean:
  71. #Do not remove .placeholder in BINDIR
  72. find $(BINDIR) -type f -not -name '.placeholder' -print0 | xargs -0 rm -f --
  73. rm -f $(SRCDIR)/*.o
  74. rm -fr $(BUILD_LIBS_DIR)/*/*.o
  75. install:
  76. @if [ ! -c "$(ARDUINO)" ]; then \
  77. echo -e "\n\nEnvironment variable ARDUINO is \"$(ARDUINO)\" and that is invalid."\
  78. "\nDid you do \"export ARDUINO=/dev/ttyACM0\" before running make install?"\
  79. "\nAlso make sure that ARDUINO env var points to a valid tty device\n\n"; \
  80. exit 1;\
  81. fi
  82. $(AVRDUDE) $(AVRDUDEARGS) -U flash:w:$(TARGET)
  83. format:
  84. $(CODE_FORMATTER) $(SRCDIR)/*.c
  85. size:
  86. $(AVRSIZE) $(AVRSIZEARGS) $(ELF)
  87. .PHONY: clean install format size