261261endif
262262
263263# include Common.mk now we know where it is
264- include $(ARDMK_DIR ) /Common.mk
264+ ifndef COMMON_INCLUDED
265+ include $(ARDMK_DIR ) /Common.mk
266+ endif
265267
266268# show_config_variable macro is available now. So let's print config details for ARDMK_DIR
267269ifndef ARDMK_DIR_MSG
@@ -317,18 +319,19 @@ else
317319 ifeq ($(ARCHITECTURE),avr)
318320 ARDUINO_ARCH_FLAG = -DARDUINO_ARCH_AVR
319321 else
320- ifeq ($(ARCHITECTURE),sam)
321- ARDUINO_ARCH_FLAG = -DARDUINO_ARCH_SAM
322- else
323- ARDUINO_ARCH_FLAG = -DARDUINO_ARCH_$(shell echo $(ARCHITECTURE ) | tr '[:lower:]' '[:upper:]')
324- endif
322+ ARDUINO_ARCH_FLAG = -DARDUINO_ARCH_$(shell echo $(ARCHITECTURE ) | tr '[:lower:]' '[:upper:]')
325323 endif
326324endif
327325
328326# #######################################################################
329327# 1.5.x vendor - defaults to arduino
330328ifndef ARDMK_VENDOR
331- ARDMK_VENDOR = arduino
329+ ARCH_LINUX := $(shell grep "Arch Linux" /etc/os-release 2>/dev/null)
330+ ifdef ARCH_LINUX
331+ ARDMK_VENDOR = archlinux-arduino
332+ else
333+ ARDMK_VENDOR = arduino
334+ endif
332335 $(call show_config_variable,ARDMK_VENDOR,[DEFAULT])
333336else
334337 $(call show_config_variable,ARDMK_VENDOR,[USER])
@@ -454,7 +457,14 @@ ifndef AVR_TOOLS_DIR
454457 AVR_TOOLS_DIR = $(SYSTEMPATH_AVR_TOOLS_DIR )
455458 $(call show_config_variable,AVR_TOOLS_DIR,[AUTODETECTED],(found in $$PATH))
456459 else
457- echo $(error No AVR tools directory found)
460+ # One last attempt using avr-gcc in case using arm
461+ SYSTEMPATH_AVR_TOOLS_DIR := $(call dir_if_exists,$(abspath $(dir $(shell which $(avr-gcc ) ) ) /..) )
462+ ifdef SYSTEMPATH_AVR_TOOLS_DIR
463+ AVR_TOOLS_DIR = $(SYSTEMPATH_AVR_TOOLS_DIR )
464+ $(call show_config_variable,AVR_TOOLS_DIR,[AUTODETECTED],(found in $$PATH))
465+ else
466+ echo $(error No AVR tools directory found)
467+ endif
458468 endif # SYSTEMPATH_AVR_TOOLS_DIR
459469
460470 endif # BUNDLED_AVR_TOOLS_DIR
@@ -644,6 +654,9 @@ ifeq ($(strip $(NO_CORE)),)
644654 USB_PID := $(call PARSE_BOARD,$(BOARD_TAG ) ,menu.(chip|cpu) .$(BOARD_SUB ) .build.pid)
645655 endif
646656 endif
657+
658+ # add caterina flag to ARD_RESET_OPTS
659+ ARD_RESET_OPTS += --caterina
647660 endif
648661
649662 # normal programming info
@@ -745,34 +758,21 @@ endif
745758# Reset
746759
747760ifndef RESET_CMD
748- ARD_RESET_ARDUINO := $(shell which ard-reset-arduino 2> /dev/null)
749- ifndef ARD_RESET_ARDUINO
750- # same level as *.mk in bin directory when checked out from git
751- # or in $PATH when packaged
752- ARD_RESET_ARDUINO = $(ARDMK_DIR)/bin/ard-reset-arduino
753- endif
754- ifneq ($(CATERINA),)
755- ifneq (,$(findstring CYGWIN,$(shell uname -s)))
756- # confirm user is using default cygwin unix Python (which uses ttySx) and not Windows Python (which uses COMx)
757- ifeq ($(shell which python),/usr/bin/python)
758- RESET_CMD = $(ARD_RESET_ARDUINO ) --caterina $(ARD_RESET_OPTS ) $(DEVICE_PATH )
759- else
760- RESET_CMD = $(ARD_RESET_ARDUINO ) --caterina $(ARD_RESET_OPTS ) $(call get_monitor_port)
761- endif
762- else
763- RESET_CMD = $(ARD_RESET_ARDUINO ) --caterina $(ARD_RESET_OPTS ) $(call get_monitor_port)
764- endif
761+ ARD_RESET_ARDUINO := $(shell which ard-reset-arduino 2> /dev/null)
762+ ifndef ARD_RESET_ARDUINO
763+ # same level as *.mk in bin directory when checked out from git
764+ # or in $PATH when packaged
765+ ARD_RESET_ARDUINO = $(ARDMK_DIR ) /bin/ard-reset-arduino
766+ endif
767+ ifneq (,$(findstring CYGWIN,$(shell uname -s)))
768+ # confirm user is using default cygwin unix Python (which uses ttySx) and not Windows Python (which uses COMx)
769+ ifeq ($(shell which python),/usr/bin/python)
770+ RESET_CMD = $(ARD_RESET_ARDUINO ) $(ARD_RESET_OPTS ) $(DEVICE_PATH )
771+ else
772+ RESET_CMD = $(ARD_RESET_ARDUINO ) $(ARD_RESET_OPTS ) $(call get_monitor_port)
773+ endif
765774 else
766- ifneq (,$(findstring CYGWIN,$(shell uname -s)))
767- # confirm user is using default cygwin unix Python (which uses ttySx) and not Windows Python (which uses COMx)
768- ifeq ($(shell which python),/usr/bin/python)
769- RESET_CMD = $(ARD_RESET_ARDUINO ) $(ARD_RESET_OPTS ) $(DEVICE_PATH )
770- else
771- RESET_CMD = $(ARD_RESET_ARDUINO ) $(ARD_RESET_OPTS ) $(call get_monitor_port)
772- endif
773- else
774- RESET_CMD = $(ARD_RESET_ARDUINO ) $(ARD_RESET_OPTS ) $(call get_monitor_port)
775- endif
775+ RESET_CMD = $(ARD_RESET_ARDUINO ) $(ARD_RESET_OPTS ) $(call get_monitor_port)
776776 endif
777777endif
778778
@@ -815,7 +815,7 @@ ifeq ($(strip $(CHK_SOURCES)),)
815815 $(call show_config_info,No .pde or .ino files found. If you are compiling .c or .cpp files then you need to explicitly include Arduino header files)
816816 else
817817 # TODO: Support more than one file. https://github.com/sudar/Arduino-Makefile/issues/49
818- $(error Need exactly one .pde or .ino file. This makefile doesn't support multiple .ino/.pde files yet)
818+ $(error Need exactly one .pde or .ino file. This makefile doesn\ 't support multiple .ino/.pde files yet)
819819 endif
820820 endif
821821
@@ -830,13 +830,26 @@ ifeq ($(strip $(NO_CORE)),)
830830 CORE_CPP_SRCS = $(wildcard $(ARDUINO_CORE_PATH ) /* .cpp)
831831 CORE_AS_SRCS = $(wildcard $(ARDUINO_CORE_PATH ) /* .S)
832832
833+ # USB Core if samd or sam
834+ ifeq ($(findstring sam, $(strip $(ARCHITECTURE))), sam)
835+ CORE_C_SRCS += $(wildcard $(ARDUINO_CORE_PATH ) /USB/* .c)
836+ CORE_CPP_SRCS += $(wildcard $(ARDUINO_CORE_PATH ) /USB/* .cpp)
837+ endif
838+
833839 ifneq ($(strip $(NO_CORE_MAIN_CPP)),)
834840 CORE_CPP_SRCS := $(filter-out % main.cpp, $(CORE_CPP_SRCS ) )
835841 $(call show_config_info,NO_CORE_MAIN_CPP set so core library will not include main.cpp,[MANUAL])
836842 endif
837843
844+ # Put alt core variant file for M0 devices in OTHER_OJBS
845+ ifdef ALT_CORE_CPP_SRCS
846+ ALT_CORE_OBJ_FILES = $(ALT_CORE_C_SRCS:.c=.c.o ) $(ALT_CORE_CPP_SRCS:.cpp=.cpp.o ) $(ALT_CORE_AS_SRCS:.S=.S.o )
847+ OTHER_OBJS := $(patsubst $(ALTERNATE_CORE_PATH ) /variants/$(VARIANT ) /% , \
848+ $(OBJDIR ) /core/% ,$(ALT_CORE_OBJ_FILES ) )
849+ endif
850+
838851 CORE_OBJ_FILES = $(CORE_C_SRCS:.c=.c.o ) $(CORE_CPP_SRCS:.cpp=.cpp.o ) $(CORE_AS_SRCS:.S=.S.o )
839- CORE_OBJS = $(patsubst $(ARDUINO_CORE_PATH ) /% , \
852+ CORE_OBJS + = $(patsubst $(ARDUINO_CORE_PATH ) /% , \
840853 $(OBJDIR ) /core/% ,$(CORE_OBJ_FILES ) )
841854 endif
842855else
@@ -908,6 +921,7 @@ endif
908921TARGET_HEX = $(OBJDIR ) /$(TARGET ) .hex
909922TARGET_ELF = $(OBJDIR ) /$(TARGET ) .elf
910923TARGET_EEP = $(OBJDIR ) /$(TARGET ) .eep
924+ TARGET_BIN = $(OBJDIR ) /$(TARGET ) .bin
911925CORE_LIB = $(OBJDIR ) /libcore.a
912926
913927# Names of executables - chipKIT needs to override all to set paths to PIC32
@@ -1023,6 +1037,7 @@ endif
10231037
10241038# SoftwareSerial requires -Os (some delays are tuned for this optimization level)
10251039% SoftwareSerial.cpp.o : OPTIMIZATION_FLAGS = -Os
1040+ % Uart.cpp.o : OPTIMIZATION_FLAGS = -Os
10261041
10271042ifndef MCU_FLAG_NAME
10281043 MCU_FLAG_NAME = mmcu
@@ -1100,7 +1115,7 @@ ifeq ($(shell expr $(CC_VERNUM) '>' 490), 1)
11001115endif
11011116LDFLAGS += -$(MCU_FLAG_NAME ) =$(MCU ) -Wl,--gc-sections -O$(OPTIMIZATION_LEVEL )
11021117ifeq ($(shell expr $(CC_VERNUM ) '>' 490) , 1)
1103- LDFLAGS += -flto -fuse-linker-plugin
1118+ LDFLAGS += -flto -fuse-linker-plugin
11041119endif
11051120SIZEFLAGS ?= --mcu=$(MCU ) -C
11061121
@@ -1308,7 +1323,24 @@ $(OBJDIR)/core/%.S.o: $(ARDUINO_CORE_PATH)/%.S $(COMMON_DEPS) | $(OBJDIR)
13081323 @$(MKDIR ) $(dir $@ )
13091324 $(CC ) -MMD -c $(CPPFLAGS ) $(ASFLAGS ) $< -o $@
13101325
1326+ # alt core files
1327+ $(OBJDIR ) /core/% .c.o : $(ALTERNATE_CORE_PATH ) /variants/$(VARIANT ) /% .c $(COMMON_DEPS ) | $(OBJDIR )
1328+ @$(MKDIR ) $(dir $@ )
1329+ $(CC ) -MMD -c $(CPPFLAGS ) $(CFLAGS ) $< -o $@
1330+
1331+ $(OBJDIR ) /core/% .cpp.o : $(ALTERNATE_CORE_PATH ) /variants/$(VARIANT ) /% .cpp $(COMMON_DEPS ) | $(OBJDIR )
1332+ @$(MKDIR ) $(dir $@ )
1333+ $(CXX ) -MMD -c $(CPPFLAGS ) $(CXXFLAGS ) $< -o $@
1334+
1335+ $(OBJDIR ) /core/% .S.o : $(ALTERNATE_CORE_PATH ) /variants/$(VARIANT ) /% .S $(COMMON_DEPS ) | $(OBJDIR )
1336+ @$(MKDIR ) $(dir $@ )
1337+ $(CC ) -MMD -c $(CPPFLAGS ) $(ASFLAGS ) $< -o $@
1338+
13111339# various object conversions
1340+ $(OBJDIR ) /% .bin : $(OBJDIR ) /% .elf $(COMMON_DEPS )
1341+ @$(MKDIR ) $(dir $@ )
1342+ -$(OBJCOPY ) -O binary $< $@
1343+
13121344$(OBJDIR ) /% .hex : $(OBJDIR ) /% .elf $(COMMON_DEPS )
13131345 @$(MKDIR ) $(dir $@ )
13141346 $(OBJCOPY ) -O ihex -R .eeprom $< $@
@@ -1448,12 +1480,21 @@ endif
14481480# so we do not set it by default.
14491481AVRDUDE_ISP_OPTS = -c $(ISP_PROG ) -b $(AVRDUDE_ISP_BAUDRATE )
14501482
1451- ifndef $( ISP_PORT)
1483+ ifndef ISP_PORT
14521484 ifneq ($(strip $(ISP_PROG)),$(filter $(ISP_PROG), atmelice_isp usbasp usbtiny gpio linuxgpio avrispmkii dragon_isp dragon_dw))
1453- AVRDUDE_ISP_OPTS += -P $(call get_isp_port)
1485+ # switch for sam devices as bootloader will be on usb serial if using stk500_v2
1486+ ifeq ($(findstring sam, $(strip $(ARCHITECTURE))), sam)
1487+ AVRDUDE_ISP_OPTS += -P $(call get_monitor_port)
1488+ else
1489+ AVRDUDE_ISP_OPTS += -P $(call get_isp_port)
1490+ endif
14541491 endif
14551492else
1456- AVRDUDE_ISP_OPTS += -P $(call get_isp_port)
1493+ ifeq ($(CURRENT_OS), WINDOWS)
1494+ AVRDUDE_ISP_OPT += -P ISP_PORT
1495+ else
1496+ AVRDUDE_ISP_OPTS += -P $(call get_isp_port)
1497+ endif
14571498endif
14581499
14591500ifndef ISP_EEPROM
@@ -1471,7 +1512,7 @@ endif
14711512# #######################################################################
14721513# Explicit targets start here
14731514
1474- all : $(TARGET_EEP ) $(TARGET_HEX )
1515+ all : $(TARGET_EEP ) $(TARGET_BIN ) $( TARGET_HEX )
14751516
14761517# Rule to create $(OBJDIR) automatically. All rules with recipes that
14771518# create a file within it, but do not already depend on a file within it
@@ -1485,30 +1526,63 @@ $(OBJDIR): pre-build
14851526pre-build :
14861527 $(call runscript_if_exists,$(PRE_BUILD_HOOK ) )
14871528
1529+ # copied from arduino with start-group, end-group
14881530$(TARGET_ELF ) : $(LOCAL_OBJS ) $(CORE_LIB ) $(OTHER_OBJS )
1531+ # sam devices need start and end group
1532+ ifeq ($(findstring sam, $(strip $(ARCHITECTURE ) ) ) , sam)
1533+ $(CC) $(LINKER_SCRIPTS) -Wl,-Map=$(OBJDIR)/$(TARGET).map -o $@ $(LOCAL_OBJS) $(OTHER_OBJS) $(OTHER_LIBS) $(LDFLAGS) $(CORE_LIB) -Wl,--end-group
1534+ # otherwise traditional
1535+ else
14891536 $(CC) $(LDFLAGS) -o $@ $(LOCAL_OBJS) $(CORE_LIB) $(OTHER_OBJS) $(OTHER_LIBS) -lc -lm $(LINKER_SCRIPTS)
1537+ endif
14901538
14911539$(CORE_LIB ) : $(CORE_OBJS ) $(LIB_OBJS ) $(PLATFORM_LIB_OBJS ) $(USER_LIB_OBJS )
14921540 $(AR ) rcs $@ $(CORE_OBJS ) $(LIB_OBJS ) $(PLATFORM_LIB_OBJS ) $(USER_LIB_OBJS )
14931541
14941542error_on_caterina :
14951543 $(ERROR_ON_CATERINA )
14961544
1497-
14981545# Use submake so we can guarantee the reset happens
14991546# before the upload, even with make -j
15001547upload : $(TARGET_HEX ) verify_size
1548+ ifeq ($(findstring sam, $(strip $(ARCHITECTURE ) ) ) , sam)
1549+ # do reset toggle at 1200 BAUD to enter bootloader if using avrdude or bossa
1550+ ifeq ($(strip $(UPLOAD_TOOL ) ) , avrdude)
1551+ $(MAKE) reset
1552+ else ifeq ($(findstring bossac, $(strip $(UPLOAD_TOOL))), bossac)
1553+ $(MAKE) reset
1554+ endif
1555+ $(MAKE) do_sam_upload
1556+ else
15011557 $(MAKE) reset
15021558 $(MAKE) do_upload
1559+ endif
15031560
15041561raw_upload : $(TARGET_HEX ) verify_size
1562+ ifeq ($(findstring sam, $(strip $(ARCHITECTURE ) ) ) , sam)
1563+ $(MAKE) do_sam_upload
1564+ else
15051565 $(MAKE) error_on_caterina
15061566 $(MAKE) do_upload
1567+ endif
15071568
15081569do_upload :
15091570 $(AVRDUDE ) $(AVRDUDE_COM_OPTS ) $(AVRDUDE_ARD_OPTS ) \
15101571 $(AVRDUDE_UPLOAD_HEX )
15111572
1573+ do_sam_upload : $(TARGET_BIN ) verify_size
1574+ ifeq ($(findstring openocd, $(strip $(UPLOAD_TOOL ) ) ) , openocd)
1575+ $(OPENOCD) $(OPENOCD_OPTS) -c "telnet_port disabled; program {{$(TARGET_BIN)}} verify reset $(BOOTLOADER_SIZE); shutdown"
1576+ else ifeq ($(findstring bossac, $(strip $(UPLOAD_TOOL))), bossac)
1577+ $(BOSSA) $(BOSSA_OPTS) $(TARGET_BIN)
1578+ else ifeq ($(findstring gdb, $(strip $(UPLOAD_TOOL))), gdb)
1579+ $(GDB) $(GDB_UPLOAD_OPTS)
1580+ else ifeq ($(strip $(UPLOAD_TOOL)), avrdude)
1581+ $(MAKE) ispload
1582+ else
1583+ @$(ECHO) "$(BOOTLOADER_UPLOAD_TOOL) not currently supported!\n\n"
1584+ endif
1585+
15121586do_eeprom : $(TARGET_EEP ) $(TARGET_HEX )
15131587 $(AVRDUDE ) $(AVRDUDE_COM_OPTS ) $(AVRDUDE_ARD_OPTS ) \
15141588 $(AVRDUDE_UPLOAD_EEP )
@@ -1541,14 +1615,22 @@ ispload: $(TARGET_EEP) $(TARGET_HEX) verify_size
15411615 $(AVRDUDE_ISPLOAD_OPTS )
15421616
15431617burn_bootloader :
1544- ifneq ($(strip $(AVRDUDE_ISP_FUSES_PRE ) ) ,)
1545- $(AVRDUDE) $(AVRDUDE_COM_OPTS) $(AVRDUDE_ISP_OPTS) -e $(AVRDUDE_ISP_FUSES_PRE)
1546- endif
1547- ifneq ($(strip $(AVRDUDE_ISP_BURN_BOOTLOADER ) ) ,)
1548- $(AVRDUDE) $(AVRDUDE_COM_OPTS) $(AVRDUDE_ISP_OPTS) $(AVRDUDE_ISP_BURN_BOOTLOADER)
1549- endif
1550- ifneq ($(strip $(AVRDUDE_ISP_FUSES_POST ) ) ,)
1551- $(AVRDUDE) $(AVRDUDE_COM_OPTS) $(AVRDUDE_ISP_OPTS) $(AVRDUDE_ISP_FUSES_POST)
1618+ ifeq ($(findstring sam, $(strip $(ARCHITECTURE ) ) ) , sam)
1619+ ifeq ($(strip $(BOOTLOADER_UPLOAD_TOOL)), openocd)
1620+ $(OPENOCD) $(OPENOCD_OPTS) -c "telnet_port disabled; init; halt; $(BOOTLOADER_UNPROTECT); program {{$(BOOTLOADER_PARENT)/$(BOOTLOADER_FILE)}} verify reset; shutdown"
1621+ else
1622+ @$(ECHO) "$(BOOTLOADER_UPLOAD_TOOL) not currently supported!\n\n"
1623+ endif
1624+ else
1625+ ifneq ($(strip $(AVRDUDE_ISP_FUSES_PRE)),)
1626+ $(AVRDUDE) $(AVRDUDE_COM_OPTS) $(AVRDUDE_ISP_OPTS) -e $(AVRDUDE_ISP_FUSES_PRE)
1627+ endif
1628+ ifneq ($(strip $(AVRDUDE_ISP_BURN_BOOTLOADER)),)
1629+ $(AVRDUDE) $(AVRDUDE_COM_OPTS) $(AVRDUDE_ISP_OPTS) $(AVRDUDE_ISP_BURN_BOOTLOADER)
1630+ endif
1631+ ifneq ($(strip $(AVRDUDE_ISP_FUSES_POST)),)
1632+ $(AVRDUDE) $(AVRDUDE_COM_OPTS) $(AVRDUDE_ISP_OPTS) $(AVRDUDE_ISP_FUSES_POST)
1633+ endif
15521634endif
15531635
15541636set_fuses :
@@ -1586,6 +1668,12 @@ else
15861668 $(MONITOR_CMD) $(call get_monitor_port) $(MONITOR_BAUDRATE)
15871669endif
15881670
1671+ debug_init :
1672+ $(OPENOCD )
1673+
1674+ debug :
1675+ $(GDB ) $(GDB_OPTS )
1676+
15891677disasm : $(OBJDIR ) /$(TARGET ) .lss
15901678 @$(ECHO ) " The compiled ELF file has been disassembled to $( OBJDIR) /$( TARGET) .lss\n\n"
15911679
@@ -1605,7 +1693,6 @@ generate_assembly: $(OBJDIR)/$(TARGET).s
16051693generated_assembly : generate_assembly
16061694 @$(ECHO ) " \" generated_assembly\" target is deprecated. Use \" generate_assembly\" target instead\n\n"
16071695
1608- .PHONY : tags
16091696tags :
16101697ifneq ($(words $(wildcard $(TAGS_FILE ) ) ) , 0)
16111698 rm -f $(TAGS_FILE)
@@ -1640,6 +1727,8 @@ help:
16401727 make show_boards - list all the boards defined in boards.txt\n\
16411728 make show_submenu - list all board submenus defined in boards.txt\n\
16421729 make monitor - connect to the Arduino's serial port\n\
1730+ make debug_init - start openocd gdb server\n\
1731+ make debug - connect to gdb target and begin debugging\n\
16431732 make size - show the size of the compiled output (relative to\n\
16441733 resources, if you have a patched avr-size).\n\
16451734 make verify_size - verify that the size of the final file is less than\n\
@@ -1661,7 +1750,7 @@ help:
16611750
16621751.PHONY : all upload raw_upload raw_eeprom error_on_caterina reset reset_stty ispload \
16631752 clean depends size show_boards monitor disasm symbol_sizes generated_assembly \
1664- generate_assembly verify_size burn_bootloader help pre-build
1753+ generate_assembly verify_size burn_bootloader help pre-build tags debug debug_init
16651754
16661755# added - in the beginning, so that we don't get an error if the file is not present
16671756-include $(DEPS )
0 commit comments