-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakefile
210 lines (182 loc) · 6.18 KB
/
makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# This makefile should go in the root of a repository (except lib-common)
# Reference: http://www.atmel.com/webdoc/avrlibcreferencemanual/group__demo__project_1demo_project_compile.html
# Parameters that might need to be changed, depending on the repository
#-------------------------------------------------------------------------------
# Libraries from lib-common to link
# For some reason, conversions needs to come after dac or else it gives an error
# Need to put dac before conversions, uptime before timer, heartbeat before can,
# or else gives an error for undefined reference
LIB = -L./lib-common/lib -ladc -lheartbeat -lcan -ldac -lconversions -lpex -lqueue -lspi -luptime -ltimer -luart -lutilities -lwatchdog -lprintf_flt -lm
# Program name
PROG = eps
# Name of microcontroller ("32m1" or "64m1")
MCU = 64m1
#-------------------------------------------------------------------------------
# AVR-GCC compiler
CC = avr-gcc
# Compiler flags
CFLAGS = -Wall -Wl,-u,vfprintf -std=gnu99 -g -mmcu=atmega$(MCU) -Os -mcall-prologues
# Includes (header files)
INCLUDES = -I./lib-common/include/
# Programmer
PGMR = stk500
# avrdude device
DEVICE = m$(MCU)
# Build directory
BUILD = build
# Manual tests directory
MANUAL_TESTS = $(dir $(wildcard manual_tests/*/.))
# Harness testing folder
TEST = harness_tests
# HARNESS_ARGS - can specify from the command line when calling `make`
# Detect operating system - based on https://gist.github.com/sighingnow/deee806603ec9274fd47
# One of these flags will be set to true based on the operating system
WINDOWS := false
MAC_OS := false
LINUX := false
REMOTE := false
ifeq ($(OS),Windows_NT)
WINDOWS := true
else
# Unix - get the operating system
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Darwin)
MAC_OS := true
endif
ifeq ($(UNAME_S),Linux)
LINUX := true
endif
endif
# PORT - Computer port that the programmer is connected to
# Try to automatically detect the port
ifeq ($(WINDOWS), true)
# higher number
PORT = $(shell powershell "[System.IO.Ports.SerialPort]::getportnames() | sort | select -First 2 | select -Last 1")
UART = $(shell powershell "[System.IO.Ports.SerialPort]::getportnames() | sort | select -First 1")
endif
ifeq ($(MAC_OS), true)
# lower number
PORT = $(shell find /dev -name 'tty.usbmodem[0-9]*' | sort | head -n1)
UART = $(shell find /dev -name 'tty.usbmodem[0-9]*' | sort | sed -n 2p)
endif
ifeq ($(LINUX), true)
# lower number
PORT = $(shell pavr2cmd --prog-port)
UART = $(shell pavr2cmd --ttl-port)
ifeq ($(shell whoami),ss)
REMOTE := true
endif
endif
# Set the PYTHON variable - Python interpreter
# Windows uses `python` for either Python 2 or 3,
# while macOS/Linux use `python3` to explicitly use Python 3
ifeq ($(WINDOWS), true)
PYTHON := python
endif
ifeq ($(MAC_OS), true)
PYTHON := python3
endif
ifeq ($(LINUX), true)
PYTHON := python3
endif
# Special commands
.PHONY: all clean debug harness help lib-common manual_tests read-eeprom upload
# Get all .c files in src folder
SRC = $(wildcard ./src/*.c)
# All .c files in src get compiled to to .o files in build
OBJ = $(SRC:./src/%.c=./build/%.o)
DEP = $(OBJ:.o=.d)
all: $(PROG)
# Make main program
$(PROG): $(OBJ)
$(CC) $(CFLAGS) -o ./build/[email protected] $(OBJ) $(LIB)
avr-objcopy -j .text -j .data -O ihex ./build/[email protected] ./build/[email protected]
# .o files depend on .c files
./build/%.o: ./src/%.c
$(CC) $(CFLAGS) -o $@ -c $< $(INCLUDES)
-include $(DEP)
./build/%.d: ./src/%.c | $(BUILD)
@$(CC) $(CFLAGS) $< -MM -MT $(@:.d=.o) >$@
# Create the build directory if it doesn't exist
$(BUILD):
mkdir $(BUILD)
# Remove all files in the build directory
clean:
rm -f $(BUILD)/*
# Print debug information
debug:
@echo ------------
@echo "MCU:" $(MCU)
@echo ------------
@echo "DEVICE:" $(DEVICE)
@echo ------------
@echo "TEST:" $(TEST)
@echo ------------
@echo "HARNESS_ARGS:" $(HARNESS_ARGS)
@echo ------------
@echo "WINDOWS:" $(WINDOWS)
@echo ------------
@echo "MAC_OS:" $(MAC_OS)
@echo ------------
@echo "LINUX:" $(LINUX)
@echo ------------
@echo "PYTHON:" $(PYTHON)
@echo ------------
@echo "SRC:" $(SRC)
@echo ------------
@echo "OBJ:" $(OBJ)
@echo ------------
# Need to cd into lib-common and refer back up one directory to the harness_tests folder
# because harness.py has the `include` and `src` paths hardcoded
# If multi-board testing, PORT2 and UART2 must both be specified
harness:
cd lib-common && \
$(PYTHON) ./bin/harness.py -m $(MCU) -p $(PORT) $(PORT2) -u $(UART) $(UART2) -d ../$(TEST) $(HARNESS_ARGS)
# Help shows available commands
help:
@echo "usage: make [all | clean | debug | harness | help | lib-common | manual_tests | read-eeprom | upload]"
@echo "Running make without any arguments is equivalent to running make all."
@echo "all build the main program (src directory)"
@echo "clean clear the build directory and all subdirectories"
@echo "debug display debugging information"
@echo "harness run the test harness"
@echo "help display this help message"
@echo "lib-common fetch and build the latest version of lib-common"
@echo "manual_tests build all manual test programs (manual_tests directory)"
@echo "read-eeprom read and display the contents of the microcontroller's EEPROM"
@echo "upload upload the main program to a board"
lib-common:
@echo "Fetching latest version of lib-common..."
git submodule update --remote
@echo "Building lib-common..."
make -C lib-common clean MCU=$(MCU)
make -C lib-common MCU=$(MCU)
manual_tests:
@for dir in $(MANUAL_TESTS) ; do \
cd $$dir ; \
make clean ; \
make ; \
cd ../.. ; \
done
# Create a file called eeprom.bin, which contains a raw binary copy of the micro's EEPROM memory.
# View the contents of the binary file in hex
read-eeprom:
@echo "Reading EEPROM to binary file eeprom.bin..."
avrdude -p $(DEVICE) -c stk500 -P $(PORT) -U eeprom:r:eeprom.bin:r
@echo "Displaying eeprom.bin in hex..."
ifeq ($(WINDOWS), true)
powershell Format-Hex eeprom.bin
else
hexdump eeprom.bin
endif
# Upload program to board
upload: $(PROG)
ifeq ($(REMOTE),true)
gpio -g mode 14 IN
gpio -g mode 15 IN
endif
avrdude -c $(PGMR) -C ./lib-common/avrdude.conf -p $(DEVICE) -P $(PORT) -U flash:w:./build/$^.hex
ifeq ($(REMOTE),true)
gpio -g mode 14 ALT0
gpio -g mode 15 ALT0
endif