1 | # Driver settings |
2 | CXX = g++ --std=c++17 |
3 | CXXFLAGS = -Wall -Wextra -pedantic -Os |
4 | SOURCES = $(shell find src/rpi -name '*.cpp') |
5 | OBJECTS = $(SOURCES:%.cpp=%.o) |
6 | OUT = autoplow |
7 |
|
8 | .PHONY: all ard1-build clean |
9 |
|
10 | all: $(OUT) |
11 |
|
12 | $(OUT): $(OBJECTS) |
13 | $(CXX) $(CXXFLAGS) $^ -o $@ |
14 |
|
15 | %.o: %.cpp |
16 | $(CXX) -c $(CXXFLAGS) $^ -o $@ |
17 |
|
18 |
|
19 | # Arduino settings |
20 | F_CPU = 16000000L |
21 | MCU = atmega328p |
22 |
|
23 | ARDUINO_CC = avr-gcc |
24 | ARDUINO_CXX = avr-g++ |
25 | ARDUINO_AVRDUDE = avrdude |
26 | ARDUINO_OBJCOPY = avr-objcopy |
27 |
|
28 | ARDUINO_FLAGS = -c -w -Os -MMD -ffunction-sections -fdata-sections -DF_CPU=$(F_CPU) -DUSB_VID=null -DUSB_PID=null -DARDUINO=106 -mmcu=$(MCU) |
29 | ARDUINO_CFLAGS = $(ARDUINO_FLAGS) |
30 | ARDUINO_CXXFLAGS = $(ARDUINO_FLAGS) -fno-exceptions |
31 |
|
32 | ARDUINO_AVRDUDE_CONF = "include/arduino/avrdude.conf" |
33 | ARDUINO_INCLUDE = "include/arduino/" |
34 |
|
35 | ARDUINO_CORE_CXX_SRC = $(shell find include/arduino -name '*.cpp') |
36 | ARDUINO_CORE_C_SRC = $(shell find include/arduino -name '*.c') |
37 | ARDUINO_CORE_ASM_SRC = $(shell find include/arduino -name '*.S') |
38 |
|
39 | ARD1_SOURCES = $(shell find src/ard1 -name '*.cpp') |
40 |
|
41 | ARDUINO_CORE_C = $(ARDUINO_CORE_C_SRC:%.c=%.ard) |
42 | ARDUINO_CORE_CXX = $(ARDUINO_CORE_CXX_SRC:%.cpp=%.ard) |
43 | ARDUINO_CORE_ASM = $(ARDUINO_CORE_ASM_SRC:%.S=%.ard) |
44 |
|
45 | ARD1_OBJECTS = $(ARD1_SOURCES:%.cpp=%.ard) |
46 |
|
47 | ARD1_OUT = bin/ard1.hex |
48 |
|
49 |
|
50 | ard1-build: $(ARD1_OUT) |
51 |
|
52 | %.ard: %.c |
53 | @echo |
54 | @echo Compiling Arduino C file: $< to $@ |
55 | $(ARDUINO_CC) $(ARDUINO_CFLAGS) -I $(ARDUINO_INCLUDE) $< -o $@ |
56 |
|
57 | %.ard: %.cpp |
58 | @echo |
59 | @echo Compiling Arduino C++ file: $< to $@ |
60 | $(ARDUINO_CXX) $(ARDUINO_CXXFLAGS) -I $(ARDUINO_INCLUDE) $< -o $@ |
61 |
|
62 | %.ard: %.S |
63 | @echo |
64 | @echo Compiling Arduino Assembly file: $< to $@ |
65 | $(ARDUINO_CC) $(ARDUINO_CFLAGS) -I $(ARDUINO_INCLUDE) $< -o $@ |
66 |
|
67 |
|
68 | $(ARD1_OUT): $(ARD1_OBJECTS) $(ARDUINO_CORE_CXX) $(ARDUINO_CORE_C) $(ARDUINO_CORE_ASM) |
69 | @echo |
70 | @echo |
71 | @echo |
72 | @echo |
73 | @echo |
74 | @echo Compiling Arduino objects to $(MCU) hex |
75 | mkdir -p bin/ |
76 | $(ARDUINO_CC) -Os -Wl,-Map,bin/main.map -mmcu=$(MCU) $(ARD1_OBJECTS) $(ARDUINO_CORE_CXX) $(ARDUINO_CORE_C) $(ARDUINO_CORE_ASM) -o bin/main.elf |
77 | $(ARDUINO_OBJCOPY) -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 bin/main.elf bin/main.eep |
78 | $(ARDUINO_OBJCOPY) -O ihex -R .eeprom bin/main.elf $@ |
79 | @echo |
80 | @echo |
81 | @echo |
82 | @echo |
83 | @echo |
84 | @echo Uploading $(ARD1_OUT) to /dev/ttyACM0 |
85 | $(ARDUINO_AVRDUDE) -C$(ARDUINO_AVRDUDE_CONF) -p$(MCU) -carduino -P/dev/ttyACM0 -b115200 -D -Uflash:w:$(ARD1_OUT):i |
86 |
|
87 | clean: |
88 | rm -rf include/*.o |
89 | rm -rf include/*.d |
90 | rm -rf src/rpi/*.o |
91 | rm -rf src/rpi/*.d |
92 | rm -rf src/ard1/*.ard |
93 | rm -rf src/ard1/*.d |
94 | rm -rf bin/*.hex |
95 | rm -rf bin/*.eep |
96 | rm -rf bin/*.elf |
97 | rm -rf bin/*.map |
98 | rm -f autoplow |
99 | rm -rf bin/ |
100 |
|