blob: 684f223afc4b4cfb27832ed00d7e8b4b11c79acf [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
Oliver Smith09c3d452021-03-05 11:32:45 +010031GIT_VERSION=$(shell $(TOP)/git-version-gen $(TOP)/.tarball-version)
Christina Quastc8ae58b2014-11-28 11:02:16 +010032#-------------------------------------------------------------------------------
33# User-modifiable options
34#-------------------------------------------------------------------------------
35
Eric Wild93168902020-04-03 18:28:31 +020036# verbosity
37V ?= 0
38ifneq ("$(V)","0")
39SILENT :=
40else
41SILENT := @
42endif
43
Christina Quastc8ae58b2014-11-28 11:02:16 +010044# Chip & board used for compilation
45# (can be overriden by adding CHIP=chip and BOARD=board to the command-line)
Harald Welted8a003d2017-02-27 20:31:09 +010046CHIP ?= sam3s4
47BOARD ?= qmod
Harald Welte87940f12019-12-06 20:07:06 +010048APP ?= dfu
Christina Quastc8ae58b2014-11-28 11:02:16 +010049
50# Defines which are the available memory targets for the SAM3S-EK board.
Harald Welte87940f12019-12-06 20:07:06 +010051ifeq ($(APP), dfu)
Kévin Redonb6e2f0f2019-12-11 17:04:21 +010052MEMORIES ?= flash dfu
Harald Welte87940f12019-12-06 20:07:06 +010053else
54MEMORIES ?= dfu
55endif
Christina Quastc8ae58b2014-11-28 11:02:16 +010056
Kévin Redond14970f2019-08-01 17:17:18 +020057# Output directories and filename
58OUTPUT = $(BOARD)-$(APP)
Christina Quastc8ae58b2014-11-28 11:02:16 +010059BIN = bin
Harald Welted8a003d2017-02-27 20:31:09 +010060OBJ = obj/$(BOARD)
Christina Quastc8ae58b2014-11-28 11:02:16 +010061
62#-------------------------------------------------------------------------------
63# Tools
64#-------------------------------------------------------------------------------
65
Harald Welte3f5e3dd2017-02-27 13:53:17 +010066AT91LIB = ./atmel_softpack_libraries
67
68AT91LIB_USB_COMMON_CORE_PATH = $(AT91LIB)/usb/common/core
69AT91LIB_USB_CORE_PATH = $(AT91LIB)/usb/device/core
70AT91LIB_USB_DFU_PATH = $(AT91LIB)/usb/device/dfu
Christina Quast96493612015-01-03 22:22:36 +010071
Christina Quastc8ae58b2014-11-28 11:02:16 +010072# Tool suffix when cross-compiling
73CROSS_COMPILE = arm-none-eabi-
74
Harald Welted09829d2017-02-27 22:58:59 +010075LIBS = -Wl,--start-group -lgcc -Wl,--end-group -nostdlib
Christina Quastc8ae58b2014-11-28 11:02:16 +010076
77# Compilation tools
78CC = $(CROSS_COMPILE)gcc
79LD = $(CROSS_COMPILE)ld
80SIZE = $(CROSS_COMPILE)size
81STRIP = $(CROSS_COMPILE)strip
82OBJCOPY = $(CROSS_COMPILE)objcopy
83GDB = $(CROSS_COMPILE)gdb
84NM = $(CROSS_COMPILE)nm
85
Harald Welte2315e6b2016-03-19 21:37:55 +010086TOP=..
Harald Welte2315e6b2016-03-19 21:37:55 +010087
Harald Welted09829d2017-02-27 22:58:59 +010088#-------------------------------------------------------------------------------
89# Files
90#-------------------------------------------------------------------------------
91
92# Directories where source files can be found
93
94USB_PATHS = $(AT91LIB_USB_CORE_PATH) $(AT91LIB_USB_DFU_PATH) $(AT91LIB_USB_COMMON_CORE_PATH)
95
96VPATH += $(USB_PATHS)
97VPATH += $(AT91LIB)/libchip_sam3s/source/ $(AT91LIB)/libchip_sam3s/cmsis
98VPATH += libboard/common/source libboard/$(BOARD)/source
99VPATH += libcommon/source
Harald Welte37b6e412017-02-27 23:20:38 +0100100VPATH += libosmocore/source
Harald Welted09829d2017-02-27 22:58:59 +0100101VPATH += apps/$(APP)
102
103# Objects built from C source files
104C_OSMOCORE = $(notdir $(wildcard libosmocore/source/*.c))
105C_LIBCHIP = $(notdir $(wildcard $(AT91LIB)/libchip_sam3s/source/*.c) $(wildcard $(AT91LIB)/libchip_sam3s/cmsis/*.c))
106
107C_LIBUSB = USBDescriptors.c USBRequests.c USBD.c USBDCallbacks.c USBDDriver.c USBDDriverCallbacks.c
Harald Welte912b1832017-03-06 09:28:13 +0100108C_LIBUSB_RT = dfu.c dfu_runtime.c
Harald Welted09829d2017-02-27 22:58:59 +0100109C_LIBUSB_DFU = dfu.c dfu_desc.c dfu_driver.c
Harald Weltee3b2de42020-01-11 12:31:51 +0100110C_LIBCOMMON = string.c stdio.c fputs.c usb_buf.c ringbuffer.c pseudo_talloc.c host_communication.c \
Harald Welte1afb70a2020-08-05 11:59:26 +0200111 main_common.c stack_check.c
Harald Welted09829d2017-02-27 22:58:59 +0100112
113C_BOARD = $(notdir $(wildcard libboard/common/source/*.c))
Kévin Redon72a62cf2019-02-07 17:42:29 +0100114C_BOARD += $(notdir $(wildcard libboard/$(BOARD)/source/*.c))
Harald Welted09829d2017-02-27 22:58:59 +0100115
116C_APPLEVEL = $(notdir $(wildcard apps/$(APP)/*.c))
117
118C_FILES = $(C_OSMOCORE) $(C_LIBCHIP) $(C_LIBUSB) $(C_LIBCOMMON) $(C_BOARD) $(C_APPLEVEL)
119
120-include apps/$(APP)/Makefile
121
122C_OBJECTS = $(C_FILES:%.c=%.o)
123
124# Trace level used for compilation
125# (can be overriden by adding TRACE_LEVEL=#number to the command-line)
126# TRACE_LEVEL_DEBUG 5
127# TRACE_LEVEL_INFO 4
128# TRACE_LEVEL_WARNING 3
129# TRACE_LEVEL_ERROR 2
130# TRACE_LEVEL_FATAL 1
131# TRACE_LEVEL_NO_TRACE 0
132TRACE_LEVEL ?= 4
133
Kévin Redon6228d182019-05-23 17:33:45 +0200134# allow asserting the peer SAM3S ERASE signal to completely erase the flash
135# only applicable for qmod board
136ALLOW_PEER_ERASE?=0
137
Harald Welted09829d2017-02-27 22:58:59 +0100138#CFLAGS+=-DUSB_NO_DEBUG=1
139
140# Optimization level, put in comment for debugging
Harald Welteedf9c9d2017-02-27 23:24:22 +0100141OPTIMIZATION ?= -Os
Harald Welted09829d2017-02-27 22:58:59 +0100142
Christina Quastc8ae58b2014-11-28 11:02:16 +0100143# Flags
Harald Welte3f5e3dd2017-02-27 13:53:17 +0100144INCLUDES_USB = -I$(AT91LIB)/usb/include -I$(AT91LIB)
Christina Quast96493612015-01-03 22:22:36 +0100145
Harald Welte3f5e3dd2017-02-27 13:53:17 +0100146INCLUDES = $(INCLUDES_USB)
147INCLUDES += -I$(AT91LIB)/libchip_sam3s -I$(AT91LIB)/libchip_sam3s/include
148INCLUDES += -I$(AT91LIB)/libchip_sam3s/cmsis
149INCLUDES += -Ilibboard/common/include -Ilibboard/$(BOARD)/include
150INCLUDES += -Ilibcommon/include
151INCLUDES += -Ilibosmocore/include
Harald Welte3f5e3dd2017-02-27 13:53:17 +0100152INCLUDES += -Isrc_simtrace -Iinclude
Harald Welte2363fa02017-03-05 10:16:25 +0100153INCLUDES += -Iapps/$(APP)
Christina Quastc8ae58b2014-11-28 11:02:16 +0100154
Harald Weltec3941092018-08-26 09:53:13 +0200155CFLAGS += -Wall -Wchar-subscripts -Wcomment -Wimplicit-int -Wformat=2
Christina Quastc8ae58b2014-11-28 11:02:16 +0100156CFLAGS += -Werror-implicit-function-declaration -Wmain -Wparentheses
Christina Quast96493612015-01-03 22:22:36 +0100157CFLAGS += -Wsequence-point -Wreturn-type -Wswitch -Wtrigraphs #-Wunused
Harald Weltec35998e2017-11-28 20:47:23 +0100158CFLAGS += -Wuninitialized -Wunknown-pragmas -Wfloat-equal #-Wundef
Christina Quastc8ae58b2014-11-28 11:02:16 +0100159CFLAGS += -Wshadow -Wpointer-arith -Wbad-function-cast -Wwrite-strings
Harald Weltec35998e2017-11-28 20:47:23 +0100160CFLAGS += -Waggregate-return #-Wsign-compare
Christina Quast96493612015-01-03 22:22:36 +0100161CFLAGS += -Wmissing-format-attribute -Wno-deprecated-declarations
Christina Quast29c99b92015-04-07 20:13:44 +0200162CFLAGS += #-Wpacked
Harald Welte9457bf72017-05-07 11:20:24 +0200163CFLAGS += -Wredundant-decls -Wnested-externs #-Winline -Wlong-long
Christina Quastc8ae58b2014-11-28 11:02:16 +0100164CFLAGS += -Wunreachable-code
Harald Welte7abdb512016-03-03 17:48:32 +0100165#CFLAGS += -Wcast-align
Harald Weltef672e9d2016-02-29 14:08:12 +0100166#CFLAGS += -std=c11
Christina Quast87d141e2015-01-27 14:56:33 +0100167CFLAGS += -Wmissing-noreturn
168#CFLAGS += -Wconversion
Christina Quastd20f26d2015-01-27 14:40:31 +0100169CFLAGS += -Wno-unused-but-set-variable -Wno-unused-variable
Christina Quast87d141e2015-01-27 14:56:33 +0100170CFLAGS += -Wno-suggest-attribute=noreturn
Christina Quastc8ae58b2014-11-28 11:02:16 +0100171
Christina Quastc8ae58b2014-11-28 11:02:16 +0100172# -mlong-calls -Wall
173#CFLAGS += -save-temps -fverbose-asm
174#CFLAGS += -Wa,-a,-ad
Eric Wild19cd3b02020-04-03 21:33:30 +0200175CFLAGS += -D__ARM -fno-builtin
Christina Quastc8ae58b2014-11-28 11:02:16 +0100176CFLAGS += --param max-inline-insns-single=500 -mcpu=cortex-m3 -mthumb # -mfix-cortex-m3-ldrd
Kévin Redon63490362019-05-23 17:37:18 +0200177CFLAGS += -ffunction-sections -g $(OPTIMIZATION) $(INCLUDES) -D$(CHIP) -DTRACE_LEVEL=$(TRACE_LEVEL) -DALLOW_PEER_ERASE=$(ALLOW_PEER_ERASE)
Harald Welteadbe72a2017-03-02 23:16:01 +0100178CFLAGS += -DGIT_VERSION=\"$(GIT_VERSION)\"
179CFLAGS += -DBOARD=\"$(BOARD)\" -DBOARD_$(BOARD)
180CFLAGS += -DAPPLICATION=\"$(APP)\" -DAPPLICATION_$(APP)
Oliver Smith878fadd2021-04-08 11:58:59 +0200181
182# Disable stack protector by default (OS#5081)
183ifeq ($(STACK_PROTECTOR), 1)
184CFLAGS += -fstack-protector
185else
186CFLAGS += -fno-stack-protector
187endif
188
Christina Quastc8ae58b2014-11-28 11:02:16 +0100189ASFLAGS = -mcpu=cortex-m3 -mthumb -Wall -g $(OPTIMIZATION) $(INCLUDES) -D$(CHIP) -D__ASSEMBLY__
Eric Wild19cd3b02020-04-03 21:33:30 +0200190LDFLAGS = -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,--print-memory-usage -Wl,--no-undefined $(LIB)
Christina Quastc8ae58b2014-11-28 11:02:16 +0100191#LD_OPTIONAL=-Wl,--print-gc-sections -Wl,--stats
192
Kévin Redond14970f2019-08-01 17:17:18 +0200193# Append BIN directories to output filename
Christina Quastc8ae58b2014-11-28 11:02:16 +0100194OUTPUT := $(BIN)/$(OUTPUT)
195
196#-------------------------------------------------------------------------------
197# Rules
198#-------------------------------------------------------------------------------
199
Harald Welte2363fa02017-03-05 10:16:25 +0100200all: apps/$(APP)/usb_strings_generated.h $(BIN) $(OBJ) $(MEMORIES)
Christina Quastc8ae58b2014-11-28 11:02:16 +0100201
Harald Weltecaca0b12017-05-05 22:29:15 +0200202combined: $(OUTPUT)-combined.bin
203
204$(BIN)/$(BOARD)-dfu-flash-padded.bin: $(BIN)/$(BOARD)-dfu-flash.bin
205 dd if=/dev/zero bs=16384 count=1 of=$@
206 dd if=$< conv=notrunc of=$@
207
208$(OUTPUT)-combined.bin: $(BIN)/$(BOARD)-dfu-flash-padded.bin $(OUTPUT)-dfu.bin
209 cat $^ > $@
210
Christina Quastc8ae58b2014-11-28 11:02:16 +0100211$(BIN) $(OBJ):
Harald Welteebe8b202018-06-29 21:43:42 +0200212 mkdir -p $@
Christina Quastc8ae58b2014-11-28 11:02:16 +0100213
Harald Welte2363fa02017-03-05 10:16:25 +0100214usbstring/usbstring: usbstring/usbstring.c
215 gcc $^ -o $@
216
Harald Welte36f888f2019-12-06 19:17:53 +0100217.PHONY: apps/$(APP)/usb_strings.txt.patched
218apps/$(APP)/usb_strings.txt.patched: apps/$(APP)/usb_strings.txt
219 sed "s/PRODUCT_STRING/$(shell cat libboard/$(BOARD)/product_string.txt)/" $< > $@
220
221apps/$(APP)/usb_strings_generated.h: apps/$(APP)/usb_strings.txt.patched usbstring/usbstring
Harald Welte2363fa02017-03-05 10:16:25 +0100222 cat $< | usbstring/usbstring > $@
223
Christina Quastc8ae58b2014-11-28 11:02:16 +0100224define RULES
225C_OBJECTS_$(1) = $(addprefix $(OBJ)/$(1)_, $(C_OBJECTS))
226ASM_OBJECTS_$(1) = $(addprefix $(OBJ)/$(1)_, $(ASM_OBJECTS))
227
228$(1): $$(ASM_OBJECTS_$(1)) $$(C_OBJECTS_$(1))
Eric Wild93168902020-04-03 18:28:31 +0200229 $(SILENT)$(CC) $(LDFLAGS) $(LD_OPTIONAL) -T"libboard/common/resources/$(CHIP)/$$@.ld" -Wl,-Map,$(OUTPUT)-$$@.map -o $(OUTPUT)-$$@.elf $$^ $(LIBS)
230 $(SILENT)$(NM) $(OUTPUT)-$$@.elf >$(OUTPUT)-$$@.elf.txt
231 $(SILENT)$(OBJCOPY) -O binary $(OUTPUT)-$$@.elf $(OUTPUT)-$$@.bin
232 $(SILENT)$(SIZE) $$^ $(OUTPUT)-$$@.elf
Christina Quastc8ae58b2014-11-28 11:02:16 +0100233
234$$(C_OBJECTS_$(1)): $(OBJ)/$(1)_%.o: %.c Makefile $(OBJ) $(BIN)
Christina Quastb6f77d32015-04-07 20:36:23 +0200235 @echo [COMPILING $$<]
Eric Wild93168902020-04-03 18:28:31 +0200236 $(SILENT)$(CC) $(CFLAGS) -DENVIRONMENT_$(1) -DENVIRONMENT=\"$(1)\" -Wa,-ahlms=$(BIN)/$$*.lst -c -o $$@ $$<
Christina Quastc8ae58b2014-11-28 11:02:16 +0100237
238$$(ASM_OBJECTS_$(1)): $(OBJ)/$(1)_%.o: %.S Makefile $(OBJ) $(BIN)
239 @echo [ASSEMBLING $$@]
Eric Wild93168902020-04-03 18:28:31 +0200240 $(SILENT)@$(CC) $(ASFLAGS) -DENVIRONMENT_$(1) -DENVIRONMENT=\"$(1)\" -c -o $$@ $$<
Christina Quastc8ae58b2014-11-28 11:02:16 +0100241
242debug_$(1): $(1)
243 $(GDB) -x "$(BOARD_LIB)/resources/gcc/$(BOARD)_$(1).gdb" -ex "reset" -readnow -se $(OUTPUT)-$(1).elf
244endef
245
246$(foreach MEMORY, $(MEMORIES), $(eval $(call RULES,$(MEMORY))))
247
248program:
Christina Quast58c1ae32015-04-07 18:19:13 +0200249 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 +0100250
Christina Quast55cd8732015-04-08 00:10:50 +0200251SERIAL ?= /dev/ttyUSB0
252log:
Harald Welte0b7e5f32019-12-02 19:25:55 +0100253 stty -F $(SERIAL) 921600
Christina Quastc63da3f2015-04-13 22:09:50 +0200254 lsof $(SERIAL) && echo "log is already opened" || ( sed -u "s/\r//" $(SERIAL) | ts )
Christina Quast55cd8732015-04-08 00:10:50 +0200255
Christina Quastc8ae58b2014-11-28 11:02:16 +0100256clean:
Harald Welte36f888f2019-12-06 19:17:53 +0100257 -rm -f apps/$(APP)/usb_strings.txt.patched
Harald Welte0380d742017-05-07 11:20:40 +0200258 -rm -fR $(OBJ)/*.o $(BIN)/*.bin $(BIN)/*.elf $(BIN)/*.elf.txt $(BIN)/*.map $(BIN)/*.lst `find . -name \*.p`
Harald Weltef7f61cd2018-08-26 09:23:54 +0200259
260install:
261 mkdir -p $(DESTDIR)/usr/share/simtrace2
262 cp $(BIN)/*.bin $(BIN)/*.elf $(BIN)/*.elf.txt $(BIN)/*.map $(DESTDIR)/usr/share/simtrace2