Skip to content

Commit 39f725d

Browse files
committed
Add infrastructure to compile arduino-based projects without IDE; Add makefile for Wire library; Add MIT license
0 parents  commit 39f725d

File tree

4 files changed

+366
-0
lines changed

4 files changed

+366
-0
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Sergei Danielian
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

+272
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
2+
#
3+
## Board Options
4+
5+
CPU=avr
6+
7+
# boards.txt :: <BRD_TYPE>.build.mcu
8+
MCU=atmega328p
9+
10+
# boards.txt :: <BRD_TYPE>.build.f_cpu
11+
F_CPU=16000000L
12+
13+
# boards.txt :: <BRD_TYPE>.build.variant
14+
BUILD_VARIANT := eightanaloginputs
15+
16+
17+
#
18+
## Variables
19+
20+
PORT ?= /dev/ttyS0
21+
BAUD ?= 57600
22+
23+
ValidBaudRates := 300 600 1200 2400 4800 9600 14400 19200 28800 38400 57600 115200
24+
25+
ifeq ($(filter $(ValidBaudRates), $(BAUD)),)
26+
$(error Invalid customer log level selected: $(BAUD). Valid are: $(ValidBaudRates))
27+
endif
28+
29+
30+
# boards.txt :: <BRD_TYPE>.build.board
31+
BRD ?= AVR_NANO
32+
33+
ValidBoards := \
34+
AVR_YUN AVR_UNO \
35+
AVR_DUEMILANOVE \
36+
AVR_NANO \
37+
AVR_MEGA2560 \
38+
AVR_ADK \
39+
AVR_LEONARDO \
40+
AVR_LEONARDO_ETH \
41+
AVR_MICRO \
42+
AVR_ESPLORA \
43+
AVR_MINI \
44+
AVR_ETHERNET \
45+
AVR_FIO \
46+
AVR_BT \
47+
AVR_LILYPAD_USB \
48+
AVR_LILYPAD \
49+
AVR_PRO \
50+
AVR_NG \
51+
AVR_ROBOT_CONTROL \
52+
AVR_ROBOT_MOTOR \
53+
AVR_GEMMA \
54+
AVR_CIRCUITPLAY \
55+
AVR_YUNMINI \
56+
AVR_INDUSTRIAL101 \
57+
AVR_LININO_ONE \
58+
AVR_UNO_WIFI_DEV_ED
59+
60+
ifeq ($(filter $(ValidBoards), $(BRD)),)
61+
$(error Invalid customer board selected: $(BRD). Valid are: $(ValidBoards))
62+
endif
63+
64+
# Pretty print
65+
ImplSpace :=
66+
ImplSpace +=
67+
PrettifyBaudRates:=$(subst $(ImplSpace),|,${ValidBaudRates})
68+
PrettifyBoards:=$(subst $(ImplSpace),|,${ValidBoards})
69+
70+
71+
#
72+
## Directories Setup
73+
74+
CurrentDir := $(shell pwd)
75+
76+
BRD_DIR=arduino
77+
78+
BUILD_DIR=$(CurrentDir)/build
79+
80+
ARDUINO_SYS_DIR=/usr/local/arduino
81+
82+
# /usr/local/arduino/hardware
83+
ARDUINO_HW_DIR=$(ARDUINO_SYS_DIR)/hardware
84+
85+
# /usr/local/arduino/hardware/arduino/avr
86+
ARDUINO_ROOT_DIR=$(ARDUINO_HW_DIR)/$(BRD_DIR)/$(CPU)
87+
88+
# /usr/local/arduino/hardware/tools/avr/bin
89+
ARDUINO_TOOLS_DIR=$(ARDUINO_HW_DIR)/tools/$(CPU)/bin
90+
91+
# /usr/local/arduino/hardware/tools/avr/etc/avrdude.conf
92+
ARDUINO_CONF_FILE=$(ARDUINO_HW_DIR)/tools/$(CPU)/etc/avrdude.conf
93+
94+
# /usr/local/arduino/hardware/arduino/avr/cores/arduino
95+
ARDUINO_CORES_PATH=$(ARDUINO_ROOT_DIR)/cores/$(BRD_DIR)
96+
97+
# /usr/local/arduino/hardware/arduino/avr/variants
98+
ARDUINO_VAR_PATH=$(ARDUINO_ROOT_DIR)/variants
99+
100+
# /usr/local/arduino/hardware/arduino/avr/libraries
101+
ARDUINO_LIBS=$(ARDUINO_ROOT_DIR)/libraries
102+
103+
104+
#
105+
## Toolchain
106+
107+
CC := $(ARDUINO_TOOLS_DIR)/avr-gcc
108+
109+
AR := $(ARDUINO_TOOLS_DIR)/avr-ar
110+
111+
CXX := $(ARDUINO_TOOLS_DIR)/avr-g++
112+
113+
OBJCOPY := $(ARDUINO_TOOLS_DIR)/avr-objcopy
114+
115+
#
116+
## Tools
117+
118+
# boards.txt :: <BRD_TYPE>.upload.tool
119+
AVRDUDE := avrdude.exe
120+
121+
122+
#
123+
## Compilation Flags
124+
125+
INCLUDE_DIRS=-I$(ARDUINO_CORES_PATH) -I$(ARDUINO_VAR_PATH)/$(BUILD_VARIANT)
126+
127+
LDFLAGS=-lm
128+
129+
CPPFLAGS=
130+
131+
# Main static library that holds all other
132+
DEPS = $(BUILD_DIR)/core.a
133+
134+
OBJECTS = $(CXXSRC:.cpp=.cpp.o) $(CSRC:.c=.c.o) $(ASRC:.S=.S.o)
135+
OBJS = $(addprefix $(BUILD_DIR)/,$(OBJECTS))
136+
137+
138+
#
139+
## Additional Arduino libraries
140+
141+
ifdef WITH_WIRE
142+
-include mkfiles/Wire.mk
143+
endif
144+
145+
#
146+
## Flags
147+
148+
GENERAL_FLAGS=-c -g -mmcu=$(MCU) -MMD -DF_CPU=$(F_CPU) -DARDUINO=10803 -DARDUINO_${BRD} -DARDUINO_ARCH_AVR
149+
150+
# -- [-x] option lets you override the default by specifying the language of
151+
# the source file, rather than inferring the language from the file suffix.
152+
153+
# -- [-x assembler-with-cpp] indicates that the assembly code contains
154+
# C directives and armclang must run the C preprocessor.
155+
156+
ASMFLAGS = $(INCLUDE_DIRS) -x assembler-with-cpp $(GENERAL_FLAGS)
157+
158+
# -- [-ffunction-sections] generates a separate ELF section for each function in
159+
# the source file. The unused section elimination feature of the linker can
160+
# then remove unused functions at link time.
161+
162+
# -- [-fdata-sections] enables or disables the generation of one ELF section for
163+
# each variable in the source file.
164+
165+
# -- [-fno-threadsafe-statics] do not emit the extra code to use the routines
166+
# specified in the C++ ABI for thread-safe initialization of local statics.
167+
168+
CFLAGS = \
169+
$(INCLUDE_DIRS) \
170+
$(CPPFLAGS) \
171+
-Os -Wall -Wextra -std=gnu11 -ffunction-sections -fdata-sections \
172+
$(GENERAL_FLAGS)
173+
174+
CXXFLAGS = \
175+
$(INCLUDE_DIRS) \
176+
$(CPPFLAGS) \
177+
-Os -Wall -Wextra -std=gnu++11 -ffunction-sections -fdata-sections \
178+
-fpermissive -fno-exceptions -fno-threadsafe-statics \
179+
$(GENERAL_FLAGS)
180+
181+
#
182+
## Sources
183+
184+
# ASM sources
185+
ASRC = $(notdir $(wildcard $(ARDUINO_CORES_PATH)/*.S))
186+
# C sources
187+
CSRC = $(notdir $(wildcard $(ARDUINO_CORES_PATH)/*.c))
188+
# C++ sources
189+
CXXSRC = $(notdir $(filter-out $(ARDUINO_CORES_PATH)/main.cpp, $(wildcard $(ARDUINO_CORES_PATH)/*.cpp)))
190+
191+
192+
#
193+
## Targets
194+
195+
196+
.DEFAULT_GOAL: all
197+
198+
all: fwimage
199+
200+
.PHONY: all clean build builddir fwimage
201+
202+
203+
204+
$(BUILD_DIR)/%.c.o: $(ARDUINO_CORES_PATH)/%.c
205+
$(CC) $< -o $@ $(CFLAGS)
206+
207+
$(BUILD_DIR)/%.cpp.o: $(ARDUINO_CORES_PATH)/%.cpp
208+
$(CXX) $< -o $@ $(CXXFLAGS)
209+
210+
$(BUILD_DIR)/%.S.o: $(ARDUINO_CORES_PATH)/%.S
211+
$(CC) $< -o $@ $(ASMFLAGS)
212+
213+
214+
archive: $(OBJS)
215+
@rm -f $(BUILD_DIR)/core.a
216+
$(foreach OBJ,$(OBJS), \
217+
$(AR) rcs $(BUILD_DIR)/core.a $(OBJ); \
218+
)
219+
220+
$(OBJS): | $(BUILD_DIR)
221+
222+
223+
fwimage: archive
224+
$(CXX) main.cpp -o $(BUILD_DIR)/main.cpp.o $(CXXFLAGS)
225+
$(CC) -Wall -Wextra -Os -g -Wl,--gc-sections -mmcu=$(MCU) -o $(BUILD_DIR)/$@.elf \
226+
$(BUILD_DIR)/main.cpp.o $(DEPS) $(LDFLAGS)
227+
$(OBJCOPY) -O ihex -R .eeprom $(BUILD_DIR)/$@.elf $@.hex
228+
229+
$(BUILD_DIR):
230+
mkdir -p $(BUILD_DIR)
231+
232+
#
233+
## Miscelaneous Targets
234+
235+
upload:
236+
$(AVRDUDE) -C $(ARDUINO_CONF_FILE) -p $(MCU) -c $(BRD_DIR) -P $(PORT) -b $(BAUD) -D -U flash:w:fwimage.hex:i
237+
238+
clean:
239+
rm -rf *.hex
240+
rm -rf $(BUILD_DIR)
241+
242+
#
243+
## Usage
244+
245+
help:
246+
@echo
247+
@echo "Usage: make <targets> [BRD=<board>] [PORT=<port>] [BAUD=<baud>] [WITH_WIRE]"
248+
@echo
249+
@echo " ENV:"
250+
@echo
251+
@echo " BRD - Type of Arduino board. For a list of possible values check the board.txt"
252+
@echo " or this Makefile's source"
253+
@echo " Default: AVR_NANO"
254+
@echo
255+
@echo " PORT - Communication port:"
256+
@echo " * COMx - for Windows [MinGW] if [0 < x < 9]"
257+
@echo " * \\\\.\\COMx - for Windows [MinGW] if x >= 10"
258+
@echo " * /dev/ttySx - for Linux (classic)"
259+
@echo " * /dev/ttyUSBx - for Linux (RS232 adapter, i.e. FTDI232)"
260+
@echo " Default: /dev/ttyS0"
261+
@echo
262+
@echo " BAUD - Serial port baudrate. Possible values:"
263+
@echo " ${PrettifyBaudRates}"
264+
@echo " Default: 57600"
265+
@echo
266+
@echo
267+
@echo " targets:"
268+
@echo
269+
@echo " help - Prints usage"
270+
@echo " fwimage - Builds firmware image (elf file)"
271+
@echo " upload - Uploads the HEX file to Arduino"
272+

main.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <Arduino.h>
2+
3+
#ifdef USE_LIBRARY_WIRE
4+
#include <Wire.h>
5+
#endif
6+
7+
// Declared weak in Arduino.h to allow user redefinitions.
8+
int atexit(void (* /*func*/ )()) { return 0; }
9+
10+
// Weak empty variant initialization function.
11+
// May be redefined by variant files.
12+
void initVariant() __attribute__((weak));
13+
void initVariant() { }
14+
15+
void setupUSB() __attribute__((weak));
16+
void setupUSB() { }
17+
18+
int main(void)
19+
{
20+
init();
21+
22+
initVariant();
23+
24+
#if defined(USBCON)
25+
USBDevice.attach();
26+
#endif
27+
28+
#if 0
29+
30+
//
31+
// Your code goes here
32+
//
33+
34+
#endif
35+
36+
if (serialEventRun) serialEventRun();
37+
38+
return 0;
39+
}

mkfiles/Wire.mk

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
ARDUINO_WIRE_LIBRARY=$(ARDUINO_LIBS)/Wire/src
3+
4+
# Relative to ARDUINO_<LIBNAME>LIBRARY
5+
WIRE_CSRC = utility/twi.c
6+
WIRE_CXXSRC = Wire.cpp
7+
8+
WIRE_BUILD_DIR=$(BUILD_DIR)/Wire
9+
10+
WIRE_OBJECTS = $(WIRE_CXXSRC:.cpp=.cpp.o) $(WIRE_CSRC:.c=.c.o)
11+
WIRE_OBJS = $(addprefix $(WIRE_BUILD_DIR)/,$(WIRE_OBJECTS))
12+
13+
#
14+
## Extend parent variables
15+
16+
OBJS+=$(WIRE_BUILD_DIR)/Wire.cpp.o $(WIRE_BUILD_DIR)/utility/twi.c.o
17+
18+
INCLUDE_DIRS+=-I$(ARDUINO_WIRE_LIBRARY)/ -I$(ARDUINO_WIRE_LIBRARY)/utility/
19+
20+
CPPFLAGS+=-DUSE_LIBRARY_WIRE
21+
22+
#
23+
## Local Targets
24+
25+
$(WIRE_OBJS): | $(WIRE_BUILD_DIR)/utility
26+
27+
$(WIRE_BUILD_DIR)/utility:
28+
mkdir -p $(WIRE_BUILD_DIR)/utility
29+
30+
$(WIRE_BUILD_DIR)/%.c.o: $(ARDUINO_WIRE_LIBRARY)/%.c
31+
$(CC) $< -o $@ $(CFLAGS)
32+
33+
$(WIRE_BUILD_DIR)/%.cpp.o: $(ARDUINO_WIRE_LIBRARY)/%.cpp
34+
$(CXX) $< -o $@ $(CXXFLAGS)

0 commit comments

Comments
 (0)