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