blob: 09cc5d84c373c53f681b7b5bd95c31bc4f1922ee [file] [log] [blame]
Christina Quastc8ae58b2014-11-28 11:02:16 +01001# ----------------------------------------------------------------------------
2# ATMEL Microcontroller Software Support
3# ----------------------------------------------------------------------------
4# Copyright (c) 2010, Atmel Corporation
5#
6# All rights reserved.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions are met:
10#
11# - Redistributions of source code must retain the above copyright notice,
12# this list of conditions and the disclaimer below.
13#
14# Atmel's name may not be used to endorse or promote products derived from
15# this software without specific prior written permission.
16#
17# DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
18# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
20# DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
21# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27# ----------------------------------------------------------------------------
28
29# Makefile for compiling the Getting Started with SAM3S Microcontrollers project
30
31#-------------------------------------------------------------------------------
32# User-modifiable options
33#-------------------------------------------------------------------------------
34
35# Chip & board used for compilation
36# (can be overriden by adding CHIP=chip and BOARD=board to the command-line)
Harald Welted8a003d2017-02-27 20:31:09 +010037CHIP ?= sam3s4
38BOARD ?= qmod
Christina Quastc8ae58b2014-11-28 11:02:16 +010039
40# Defines which are the available memory targets for the SAM3S-EK board.
Harald Welted8a003d2017-02-27 20:31:09 +010041MEMORIES ?= flash dfu
Christina Quastc8ae58b2014-11-28 11:02:16 +010042
Christina Quastc8ae58b2014-11-28 11:02:16 +010043# Output file basename
Harald Welted09829d2017-02-27 22:58:59 +010044APP ?= dfu
Christina Quastc8ae58b2014-11-28 11:02:16 +010045
46# Output directories
Harald Welted8a003d2017-02-27 20:31:09 +010047OUTPUT = $(BOARD)-$(APP)
Christina Quastc8ae58b2014-11-28 11:02:16 +010048BIN = bin
Harald Welted8a003d2017-02-27 20:31:09 +010049OBJ = obj/$(BOARD)
Christina Quastc8ae58b2014-11-28 11:02:16 +010050
51#-------------------------------------------------------------------------------
52# Tools
53#-------------------------------------------------------------------------------
54
Harald Welte3f5e3dd2017-02-27 13:53:17 +010055AT91LIB = ./atmel_softpack_libraries
56
57AT91LIB_USB_COMMON_CORE_PATH = $(AT91LIB)/usb/common/core
58AT91LIB_USB_CORE_PATH = $(AT91LIB)/usb/device/core
59AT91LIB_USB_DFU_PATH = $(AT91LIB)/usb/device/dfu
Christina Quast96493612015-01-03 22:22:36 +010060
Christina Quastc8ae58b2014-11-28 11:02:16 +010061# Tool suffix when cross-compiling
62CROSS_COMPILE = arm-none-eabi-
63
Harald Welted09829d2017-02-27 22:58:59 +010064LIBS = -Wl,--start-group -lgcc -Wl,--end-group -nostdlib
Christina Quastc8ae58b2014-11-28 11:02:16 +010065
66# Compilation tools
67CC = $(CROSS_COMPILE)gcc
68LD = $(CROSS_COMPILE)ld
69SIZE = $(CROSS_COMPILE)size
70STRIP = $(CROSS_COMPILE)strip
71OBJCOPY = $(CROSS_COMPILE)objcopy
72GDB = $(CROSS_COMPILE)gdb
73NM = $(CROSS_COMPILE)nm
74
Harald Welte2315e6b2016-03-19 21:37:55 +010075TOP=..
76GIT_VERSION=$(shell $(TOP)/git-version-gen $(TOP)/.tarvers)
77
Harald Welted09829d2017-02-27 22:58:59 +010078#-------------------------------------------------------------------------------
79# Files
80#-------------------------------------------------------------------------------
81
82# Directories where source files can be found
83
84USB_PATHS = $(AT91LIB_USB_CORE_PATH) $(AT91LIB_USB_DFU_PATH) $(AT91LIB_USB_COMMON_CORE_PATH)
85
86VPATH += $(USB_PATHS)
87VPATH += $(AT91LIB)/libchip_sam3s/source/ $(AT91LIB)/libchip_sam3s/cmsis
88VPATH += libboard/common/source libboard/$(BOARD)/source
89VPATH += libcommon/source
Harald Welte37b6e412017-02-27 23:20:38 +010090VPATH += libosmocore/source
Harald Welted09829d2017-02-27 22:58:59 +010091VPATH += apps/$(APP)
92
93# Objects built from C source files
94C_OSMOCORE = $(notdir $(wildcard libosmocore/source/*.c))
95C_LIBCHIP = $(notdir $(wildcard $(AT91LIB)/libchip_sam3s/source/*.c) $(wildcard $(AT91LIB)/libchip_sam3s/cmsis/*.c))
96
97C_LIBUSB = USBDescriptors.c USBRequests.c USBD.c USBDCallbacks.c USBDDriver.c USBDDriverCallbacks.c
98C_LIBUSB_RT = dfu.c dfu_desc.c dfu_runtime.c
99C_LIBUSB_DFU = dfu.c dfu_desc.c dfu_driver.c
100C_LIBCOMMON = string.c stdio.c fputs.c req_ctx.c ringbuffer.c
101
102C_BOARD = $(notdir $(wildcard libboard/common/source/*.c))
103C_BOARD += $(notdir $(wildcard libboard/$(BOARD)/source/*.c))
104
105C_APPLEVEL = $(notdir $(wildcard apps/$(APP)/*.c))
106
107C_FILES = $(C_OSMOCORE) $(C_LIBCHIP) $(C_LIBUSB) $(C_LIBCOMMON) $(C_BOARD) $(C_APPLEVEL)
108
109-include apps/$(APP)/Makefile
110
111C_OBJECTS = $(C_FILES:%.c=%.o)
112
113# Trace level used for compilation
114# (can be overriden by adding TRACE_LEVEL=#number to the command-line)
115# TRACE_LEVEL_DEBUG 5
116# TRACE_LEVEL_INFO 4
117# TRACE_LEVEL_WARNING 3
118# TRACE_LEVEL_ERROR 2
119# TRACE_LEVEL_FATAL 1
120# TRACE_LEVEL_NO_TRACE 0
121TRACE_LEVEL ?= 4
122
123DEBUG_PHONE_SNIFF?=0
124
125#CFLAGS+=-DUSB_NO_DEBUG=1
126
127# Optimization level, put in comment for debugging
Harald Welteedf9c9d2017-02-27 23:24:22 +0100128OPTIMIZATION ?= -Os
Harald Welted09829d2017-02-27 22:58:59 +0100129
130
131
Christina Quastc8ae58b2014-11-28 11:02:16 +0100132# Flags
Harald Welte3f5e3dd2017-02-27 13:53:17 +0100133INCLUDES_USB = -I$(AT91LIB)/usb/include -I$(AT91LIB)
Christina Quast96493612015-01-03 22:22:36 +0100134
Harald Welte3f5e3dd2017-02-27 13:53:17 +0100135INCLUDES = $(INCLUDES_USB)
136INCLUDES += -I$(AT91LIB)/libchip_sam3s -I$(AT91LIB)/libchip_sam3s/include
137INCLUDES += -I$(AT91LIB)/libchip_sam3s/cmsis
138INCLUDES += -Ilibboard/common/include -Ilibboard/$(BOARD)/include
139INCLUDES += -Ilibcommon/include
140INCLUDES += -Ilibosmocore/include
Harald Welte3f5e3dd2017-02-27 13:53:17 +0100141INCLUDES += -Isrc_simtrace -Iinclude
Christina Quastc8ae58b2014-11-28 11:02:16 +0100142
Christina Quast29c99b92015-04-07 20:13:44 +0200143CFLAGS += -Wall -Wchar-subscripts -Wcomment -Wimplicit-int #-Wformat=2
Christina Quastc8ae58b2014-11-28 11:02:16 +0100144CFLAGS += -Werror-implicit-function-declaration -Wmain -Wparentheses
Christina Quast96493612015-01-03 22:22:36 +0100145CFLAGS += -Wsequence-point -Wreturn-type -Wswitch -Wtrigraphs #-Wunused
Christina Quastc8ae58b2014-11-28 11:02:16 +0100146CFLAGS += -Wuninitialized -Wunknown-pragmas -Wfloat-equal -Wundef
147CFLAGS += -Wshadow -Wpointer-arith -Wbad-function-cast -Wwrite-strings
148CFLAGS += -Wsign-compare -Waggregate-return
Christina Quast076396f2015-04-06 19:57:53 +0200149CFLAGS += -Wformat=0
Christina Quast96493612015-01-03 22:22:36 +0100150CFLAGS += -Wmissing-format-attribute -Wno-deprecated-declarations
Christina Quast29c99b92015-04-07 20:13:44 +0200151CFLAGS += #-Wpacked
Christina Quast96493612015-01-03 22:22:36 +0100152CFLAGS += -Wredundant-decls -Wnested-externs -Winline #-Wlong-long
Christina Quastc8ae58b2014-11-28 11:02:16 +0100153CFLAGS += -Wunreachable-code
Harald Welte7abdb512016-03-03 17:48:32 +0100154#CFLAGS += -Wcast-align
Harald Weltef672e9d2016-02-29 14:08:12 +0100155#CFLAGS += -std=c11
Christina Quast87d141e2015-01-27 14:56:33 +0100156CFLAGS += -Wmissing-noreturn
157#CFLAGS += -Wconversion
Christina Quastd20f26d2015-01-27 14:40:31 +0100158CFLAGS += -Wno-unused-but-set-variable -Wno-unused-variable
Christina Quast87d141e2015-01-27 14:56:33 +0100159CFLAGS += -Wno-suggest-attribute=noreturn
Christina Quastc8ae58b2014-11-28 11:02:16 +0100160
Christina Quastc8ae58b2014-11-28 11:02:16 +0100161# -mlong-calls -Wall
162#CFLAGS += -save-temps -fverbose-asm
163#CFLAGS += -Wa,-a,-ad
Harald Welte9d3e3822015-11-09 00:50:54 +0100164CFLAGS += -D__ARM
Christina Quastc8ae58b2014-11-28 11:02:16 +0100165CFLAGS += --param max-inline-insns-single=500 -mcpu=cortex-m3 -mthumb # -mfix-cortex-m3-ldrd
Christina Quast5c6a2992015-04-11 20:03:14 +0200166CFLAGS += -ffunction-sections -g $(OPTIMIZATION) $(INCLUDES) -D$(CHIP) -DTRACE_LEVEL=$(TRACE_LEVEL) -DDEBUG_PHONE_SNIFF=$(DEBUG_PHONE_SNIFF)
Harald Welteadbe72a2017-03-02 23:16:01 +0100167CFLAGS += -DGIT_VERSION=\"$(GIT_VERSION)\"
168CFLAGS += -DBOARD=\"$(BOARD)\" -DBOARD_$(BOARD)
169CFLAGS += -DAPPLICATION=\"$(APP)\" -DAPPLICATION_$(APP)
Christina Quastc8ae58b2014-11-28 11:02:16 +0100170ASFLAGS = -mcpu=cortex-m3 -mthumb -Wall -g $(OPTIMIZATION) $(INCLUDES) -D$(CHIP) -D__ASSEMBLY__
Christina Quastf1192c62014-12-05 13:03:27 +0100171LDFLAGS = -mcpu=cortex-m3 -mthumb -Wl,--cref -Wl,--check-sections -Wl,--gc-sections -Wl,--entry=ResetException -Wl,--unresolved-symbols=report-all -Wl,--warn-common -Wl,--warn-section-align -Wl,--warn-unresolved-symbols $(LIB)
Christina Quastc8ae58b2014-11-28 11:02:16 +0100172#LD_OPTIONAL=-Wl,--print-gc-sections -Wl,--stats
173
Christina Quastc8ae58b2014-11-28 11:02:16 +0100174
175# Append OBJ and BIN directories to output filename
176OUTPUT := $(BIN)/$(OUTPUT)
177
178#-------------------------------------------------------------------------------
179# Rules
180#-------------------------------------------------------------------------------
181
182all: $(BIN) $(OBJ) $(MEMORIES)
183
184$(BIN) $(OBJ):
185 mkdir $@
186
187define RULES
188C_OBJECTS_$(1) = $(addprefix $(OBJ)/$(1)_, $(C_OBJECTS))
189ASM_OBJECTS_$(1) = $(addprefix $(OBJ)/$(1)_, $(ASM_OBJECTS))
190
191$(1): $$(ASM_OBJECTS_$(1)) $$(C_OBJECTS_$(1))
Harald Welted8a003d2017-02-27 20:31:09 +0100192 @$(CC) $(LDFLAGS) $(LD_OPTIONAL) -T"libboard/common/resources/$(CHIP)/$$@.ld" -Wl,-Map,$(OUTPUT)-$$@.map -o $(OUTPUT)-$$@.elf $$^ $(LIBS)
Christina Quastb6f77d32015-04-07 20:36:23 +0200193 @$(NM) $(OUTPUT)-$$@.elf >$(OUTPUT)-$$@.elf.txt
194 @$(OBJCOPY) -O binary $(OUTPUT)-$$@.elf $(OUTPUT)-$$@.bin
195 @$(SIZE) $$^ $(OUTPUT)-$$@.elf
Christina Quastc8ae58b2014-11-28 11:02:16 +0100196
197$$(C_OBJECTS_$(1)): $(OBJ)/$(1)_%.o: %.c Makefile $(OBJ) $(BIN)
Christina Quastb6f77d32015-04-07 20:36:23 +0200198 @echo [COMPILING $$<]
Harald Welteadbe72a2017-03-02 23:16:01 +0100199 @$(CC) $(CFLAGS) -DENVIRONMENT_$(1) -DENVIRONMENT=\"$(1)\" -Wa,-ahlms=$(BIN)/$$*.lst -c -o $$@ $$<
Christina Quastc8ae58b2014-11-28 11:02:16 +0100200
201$$(ASM_OBJECTS_$(1)): $(OBJ)/$(1)_%.o: %.S Makefile $(OBJ) $(BIN)
202 @echo [ASSEMBLING $$@]
Harald Welteadbe72a2017-03-02 23:16:01 +0100203 @$(CC) $(ASFLAGS) -DENVIRONMENT_$(1) -DENVIRONMENT=\"$(1)\" -c -o $$@ $$<
Christina Quastc8ae58b2014-11-28 11:02:16 +0100204
205debug_$(1): $(1)
206 $(GDB) -x "$(BOARD_LIB)/resources/gcc/$(BOARD)_$(1).gdb" -ex "reset" -readnow -se $(OUTPUT)-$(1).elf
207endef
208
209$(foreach MEMORY, $(MEMORIES), $(eval $(call RULES,$(MEMORY))))
210
211program:
Christina Quast58c1ae32015-04-07 18:19:13 +0200212 openocd -f openocd/openocd.cfg -c "init" -c "halt" -c "flash write_bank 0 ./bin/project-flash.bin 0" -c "reset" -c "shutdown"
Christina Quastc8ae58b2014-11-28 11:02:16 +0100213
Christina Quast55cd8732015-04-08 00:10:50 +0200214SERIAL ?= /dev/ttyUSB0
215log:
216 stty -F $(SERIAL) 115200
Christina Quastc63da3f2015-04-13 22:09:50 +0200217 lsof $(SERIAL) && echo "log is already opened" || ( sed -u "s/\r//" $(SERIAL) | ts )
Christina Quast55cd8732015-04-08 00:10:50 +0200218
Christina Quastc8ae58b2014-11-28 11:02:16 +0100219clean:
220 -rm -fR $(OBJ)/*.o $(BIN)/*.bin $(BIN)/*.elf $(BIN)/*.elf.txt $(BIN)/*.map $(BIN)/*.lst