blob: d7c64b9060f9e679b62647989d852e8e644f0e72 [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
36# Chip & board used for compilation
37# (can be overriden by adding CHIP=chip and BOARD=board to the command-line)
Harald Welted8a003d2017-02-27 20:31:09 +010038CHIP ?= sam3s4
39BOARD ?= qmod
Harald Welte87940f12019-12-06 20:07:06 +010040APP ?= dfu
Christina Quastc8ae58b2014-11-28 11:02:16 +010041
42# Defines which are the available memory targets for the SAM3S-EK board.
Harald Welte87940f12019-12-06 20:07:06 +010043ifeq ($(APP), dfu)
Kévin Redonb6e2f0f2019-12-11 17:04:21 +010044MEMORIES ?= flash dfu
Harald Welte87940f12019-12-06 20:07:06 +010045else
46MEMORIES ?= dfu
47endif
Christina Quastc8ae58b2014-11-28 11:02:16 +010048
Kévin Redond14970f2019-08-01 17:17:18 +020049# Output directories and filename
50OUTPUT = $(BOARD)-$(APP)
Christina Quastc8ae58b2014-11-28 11:02:16 +010051BIN = bin
Harald Welted8a003d2017-02-27 20:31:09 +010052OBJ = obj/$(BOARD)
Christina Quastc8ae58b2014-11-28 11:02:16 +010053
54#-------------------------------------------------------------------------------
55# Tools
56#-------------------------------------------------------------------------------
57
Harald Welte3f5e3dd2017-02-27 13:53:17 +010058AT91LIB = ./atmel_softpack_libraries
59
60AT91LIB_USB_COMMON_CORE_PATH = $(AT91LIB)/usb/common/core
61AT91LIB_USB_CORE_PATH = $(AT91LIB)/usb/device/core
62AT91LIB_USB_DFU_PATH = $(AT91LIB)/usb/device/dfu
Christina Quast96493612015-01-03 22:22:36 +010063
Christina Quastc8ae58b2014-11-28 11:02:16 +010064# Tool suffix when cross-compiling
65CROSS_COMPILE = arm-none-eabi-
66
Harald Welted09829d2017-02-27 22:58:59 +010067LIBS = -Wl,--start-group -lgcc -Wl,--end-group -nostdlib
Christina Quastc8ae58b2014-11-28 11:02:16 +010068
69# Compilation tools
70CC = $(CROSS_COMPILE)gcc
71LD = $(CROSS_COMPILE)ld
72SIZE = $(CROSS_COMPILE)size
73STRIP = $(CROSS_COMPILE)strip
74OBJCOPY = $(CROSS_COMPILE)objcopy
75GDB = $(CROSS_COMPILE)gdb
76NM = $(CROSS_COMPILE)nm
77
Harald Welte2315e6b2016-03-19 21:37:55 +010078TOP=..
Harald Welte2315e6b2016-03-19 21:37:55 +010079
Harald Welted09829d2017-02-27 22:58:59 +010080#-------------------------------------------------------------------------------
81# Files
82#-------------------------------------------------------------------------------
83
84# Directories where source files can be found
85
86USB_PATHS = $(AT91LIB_USB_CORE_PATH) $(AT91LIB_USB_DFU_PATH) $(AT91LIB_USB_COMMON_CORE_PATH)
87
88VPATH += $(USB_PATHS)
89VPATH += $(AT91LIB)/libchip_sam3s/source/ $(AT91LIB)/libchip_sam3s/cmsis
90VPATH += libboard/common/source libboard/$(BOARD)/source
91VPATH += libcommon/source
Harald Welte37b6e412017-02-27 23:20:38 +010092VPATH += libosmocore/source
Harald Welted09829d2017-02-27 22:58:59 +010093VPATH += apps/$(APP)
94
95# Objects built from C source files
96C_OSMOCORE = $(notdir $(wildcard libosmocore/source/*.c))
97C_LIBCHIP = $(notdir $(wildcard $(AT91LIB)/libchip_sam3s/source/*.c) $(wildcard $(AT91LIB)/libchip_sam3s/cmsis/*.c))
98
99C_LIBUSB = USBDescriptors.c USBRequests.c USBD.c USBDCallbacks.c USBDDriver.c USBDDriverCallbacks.c
Harald Welte912b1832017-03-06 09:28:13 +0100100C_LIBUSB_RT = dfu.c dfu_runtime.c
Harald Welted09829d2017-02-27 22:58:59 +0100101C_LIBUSB_DFU = dfu.c dfu_desc.c dfu_driver.c
Harald Weltee3b2de42020-01-11 12:31:51 +0100102C_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 +0200103 main_common.c stack_check.c
Harald Welted09829d2017-02-27 22:58:59 +0100104
105C_BOARD = $(notdir $(wildcard libboard/common/source/*.c))
Kévin Redon72a62cf2019-02-07 17:42:29 +0100106C_BOARD += $(notdir $(wildcard libboard/$(BOARD)/source/*.c))
Harald Welted09829d2017-02-27 22:58:59 +0100107
108C_APPLEVEL = $(notdir $(wildcard apps/$(APP)/*.c))
109
110C_FILES = $(C_OSMOCORE) $(C_LIBCHIP) $(C_LIBUSB) $(C_LIBCOMMON) $(C_BOARD) $(C_APPLEVEL)
111
112-include apps/$(APP)/Makefile
113
114C_OBJECTS = $(C_FILES:%.c=%.o)
115
116# Trace level used for compilation
117# (can be overriden by adding TRACE_LEVEL=#number to the command-line)
118# TRACE_LEVEL_DEBUG 5
119# TRACE_LEVEL_INFO 4
120# TRACE_LEVEL_WARNING 3
121# TRACE_LEVEL_ERROR 2
122# TRACE_LEVEL_FATAL 1
123# TRACE_LEVEL_NO_TRACE 0
124TRACE_LEVEL ?= 4
125
Kévin Redon6228d182019-05-23 17:33:45 +0200126# allow asserting the peer SAM3S ERASE signal to completely erase the flash
127# only applicable for qmod board
128ALLOW_PEER_ERASE?=0
129
Harald Welted09829d2017-02-27 22:58:59 +0100130#CFLAGS+=-DUSB_NO_DEBUG=1
131
132# Optimization level, put in comment for debugging
Harald Welteedf9c9d2017-02-27 23:24:22 +0100133OPTIMIZATION ?= -Os
Harald Welted09829d2017-02-27 22:58:59 +0100134
Christina Quastc8ae58b2014-11-28 11:02:16 +0100135# Flags
Harald Welte3f5e3dd2017-02-27 13:53:17 +0100136INCLUDES_USB = -I$(AT91LIB)/usb/include -I$(AT91LIB)
Christina Quast96493612015-01-03 22:22:36 +0100137
Harald Welte3f5e3dd2017-02-27 13:53:17 +0100138INCLUDES = $(INCLUDES_USB)
139INCLUDES += -I$(AT91LIB)/libchip_sam3s -I$(AT91LIB)/libchip_sam3s/include
140INCLUDES += -I$(AT91LIB)/libchip_sam3s/cmsis
141INCLUDES += -Ilibboard/common/include -Ilibboard/$(BOARD)/include
142INCLUDES += -Ilibcommon/include
143INCLUDES += -Ilibosmocore/include
Harald Welte3f5e3dd2017-02-27 13:53:17 +0100144INCLUDES += -Isrc_simtrace -Iinclude
Harald Welte2363fa02017-03-05 10:16:25 +0100145INCLUDES += -Iapps/$(APP)
Christina Quastc8ae58b2014-11-28 11:02:16 +0100146
Harald Weltec3941092018-08-26 09:53:13 +0200147CFLAGS += -Wall -Wchar-subscripts -Wcomment -Wimplicit-int -Wformat=2
Christina Quastc8ae58b2014-11-28 11:02:16 +0100148CFLAGS += -Werror-implicit-function-declaration -Wmain -Wparentheses
Christina Quast96493612015-01-03 22:22:36 +0100149CFLAGS += -Wsequence-point -Wreturn-type -Wswitch -Wtrigraphs #-Wunused
Harald Weltec35998e2017-11-28 20:47:23 +0100150CFLAGS += -Wuninitialized -Wunknown-pragmas -Wfloat-equal #-Wundef
Christina Quastc8ae58b2014-11-28 11:02:16 +0100151CFLAGS += -Wshadow -Wpointer-arith -Wbad-function-cast -Wwrite-strings
Harald Weltec35998e2017-11-28 20:47:23 +0100152CFLAGS += -Waggregate-return #-Wsign-compare
Christina Quast96493612015-01-03 22:22:36 +0100153CFLAGS += -Wmissing-format-attribute -Wno-deprecated-declarations
Christina Quast29c99b92015-04-07 20:13:44 +0200154CFLAGS += #-Wpacked
Harald Welte9457bf72017-05-07 11:20:24 +0200155CFLAGS += -Wredundant-decls -Wnested-externs #-Winline -Wlong-long
Christina Quastc8ae58b2014-11-28 11:02:16 +0100156CFLAGS += -Wunreachable-code
Harald Welte7abdb512016-03-03 17:48:32 +0100157#CFLAGS += -Wcast-align
Harald Weltef672e9d2016-02-29 14:08:12 +0100158#CFLAGS += -std=c11
Christina Quast87d141e2015-01-27 14:56:33 +0100159CFLAGS += -Wmissing-noreturn
160#CFLAGS += -Wconversion
Christina Quastd20f26d2015-01-27 14:40:31 +0100161CFLAGS += -Wno-unused-but-set-variable -Wno-unused-variable
Christina Quast87d141e2015-01-27 14:56:33 +0100162CFLAGS += -Wno-suggest-attribute=noreturn
Christina Quastc8ae58b2014-11-28 11:02:16 +0100163
Christina Quastc8ae58b2014-11-28 11:02:16 +0100164# -mlong-calls -Wall
165#CFLAGS += -save-temps -fverbose-asm
166#CFLAGS += -Wa,-a,-ad
Harald Welte9d3e3822015-11-09 00:50:54 +0100167CFLAGS += -D__ARM
Christina Quastc8ae58b2014-11-28 11:02:16 +0100168CFLAGS += --param max-inline-insns-single=500 -mcpu=cortex-m3 -mthumb # -mfix-cortex-m3-ldrd
Kévin Redon63490362019-05-23 17:37:18 +0200169CFLAGS += -ffunction-sections -g $(OPTIMIZATION) $(INCLUDES) -D$(CHIP) -DTRACE_LEVEL=$(TRACE_LEVEL) -DALLOW_PEER_ERASE=$(ALLOW_PEER_ERASE)
Harald Welteadbe72a2017-03-02 23:16:01 +0100170CFLAGS += -DGIT_VERSION=\"$(GIT_VERSION)\"
171CFLAGS += -DBOARD=\"$(BOARD)\" -DBOARD_$(BOARD)
172CFLAGS += -DAPPLICATION=\"$(APP)\" -DAPPLICATION_$(APP)
Christina Quastc8ae58b2014-11-28 11:02:16 +0100173ASFLAGS = -mcpu=cortex-m3 -mthumb -Wall -g $(OPTIMIZATION) $(INCLUDES) -D$(CHIP) -D__ASSEMBLY__
Kévin Redon98cf47a2019-12-11 16:13:27 +0100174LDFLAGS = -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 -Wl,--print-memory-usage $(LIB)
Christina Quastc8ae58b2014-11-28 11:02:16 +0100175#LD_OPTIONAL=-Wl,--print-gc-sections -Wl,--stats
176
Kévin Redond14970f2019-08-01 17:17:18 +0200177# Append BIN directories to output filename
Christina Quastc8ae58b2014-11-28 11:02:16 +0100178OUTPUT := $(BIN)/$(OUTPUT)
179
180#-------------------------------------------------------------------------------
181# Rules
182#-------------------------------------------------------------------------------
183
Harald Welte2363fa02017-03-05 10:16:25 +0100184all: apps/$(APP)/usb_strings_generated.h $(BIN) $(OBJ) $(MEMORIES)
Christina Quastc8ae58b2014-11-28 11:02:16 +0100185
Harald Weltecaca0b12017-05-05 22:29:15 +0200186combined: $(OUTPUT)-combined.bin
187
188$(BIN)/$(BOARD)-dfu-flash-padded.bin: $(BIN)/$(BOARD)-dfu-flash.bin
189 dd if=/dev/zero bs=16384 count=1 of=$@
190 dd if=$< conv=notrunc of=$@
191
192$(OUTPUT)-combined.bin: $(BIN)/$(BOARD)-dfu-flash-padded.bin $(OUTPUT)-dfu.bin
193 cat $^ > $@
194
Christina Quastc8ae58b2014-11-28 11:02:16 +0100195$(BIN) $(OBJ):
Harald Welteebe8b202018-06-29 21:43:42 +0200196 mkdir -p $@
Christina Quastc8ae58b2014-11-28 11:02:16 +0100197
Harald Welte2363fa02017-03-05 10:16:25 +0100198usbstring/usbstring: usbstring/usbstring.c
199 gcc $^ -o $@
200
Harald Welte36f888f2019-12-06 19:17:53 +0100201.PHONY: apps/$(APP)/usb_strings.txt.patched
202apps/$(APP)/usb_strings.txt.patched: apps/$(APP)/usb_strings.txt
203 sed "s/PRODUCT_STRING/$(shell cat libboard/$(BOARD)/product_string.txt)/" $< > $@
204
205apps/$(APP)/usb_strings_generated.h: apps/$(APP)/usb_strings.txt.patched usbstring/usbstring
Harald Welte2363fa02017-03-05 10:16:25 +0100206 cat $< | usbstring/usbstring > $@
207
Christina Quastc8ae58b2014-11-28 11:02:16 +0100208define RULES
209C_OBJECTS_$(1) = $(addprefix $(OBJ)/$(1)_, $(C_OBJECTS))
210ASM_OBJECTS_$(1) = $(addprefix $(OBJ)/$(1)_, $(ASM_OBJECTS))
211
212$(1): $$(ASM_OBJECTS_$(1)) $$(C_OBJECTS_$(1))
Harald Welted8a003d2017-02-27 20:31:09 +0100213 @$(CC) $(LDFLAGS) $(LD_OPTIONAL) -T"libboard/common/resources/$(CHIP)/$$@.ld" -Wl,-Map,$(OUTPUT)-$$@.map -o $(OUTPUT)-$$@.elf $$^ $(LIBS)
Kévin Redond14970f2019-08-01 17:17:18 +0200214 cp $(OUTPUT)-$$@.elf $(OUTPUT)-$$@-$(GIT_VERSION).elf
215 cp $(OUTPUT)-$$@.elf $(OUTPUT)-$$@-latest.elf
Christina Quastb6f77d32015-04-07 20:36:23 +0200216 @$(NM) $(OUTPUT)-$$@.elf >$(OUTPUT)-$$@.elf.txt
217 @$(OBJCOPY) -O binary $(OUTPUT)-$$@.elf $(OUTPUT)-$$@.bin
Kévin Redond14970f2019-08-01 17:17:18 +0200218 cp $(OUTPUT)-$$@.bin $(OUTPUT)-$$@-$(GIT_VERSION).bin
219 cp $(OUTPUT)-$$@.bin $(OUTPUT)-$$@-latest.bin
Christina Quastb6f77d32015-04-07 20:36:23 +0200220 @$(SIZE) $$^ $(OUTPUT)-$$@.elf
Christina Quastc8ae58b2014-11-28 11:02:16 +0100221
222$$(C_OBJECTS_$(1)): $(OBJ)/$(1)_%.o: %.c Makefile $(OBJ) $(BIN)
Christina Quastb6f77d32015-04-07 20:36:23 +0200223 @echo [COMPILING $$<]
Harald Welteadbe72a2017-03-02 23:16:01 +0100224 @$(CC) $(CFLAGS) -DENVIRONMENT_$(1) -DENVIRONMENT=\"$(1)\" -Wa,-ahlms=$(BIN)/$$*.lst -c -o $$@ $$<
Christina Quastc8ae58b2014-11-28 11:02:16 +0100225
226$$(ASM_OBJECTS_$(1)): $(OBJ)/$(1)_%.o: %.S Makefile $(OBJ) $(BIN)
227 @echo [ASSEMBLING $$@]
Harald Welteadbe72a2017-03-02 23:16:01 +0100228 @$(CC) $(ASFLAGS) -DENVIRONMENT_$(1) -DENVIRONMENT=\"$(1)\" -c -o $$@ $$<
Christina Quastc8ae58b2014-11-28 11:02:16 +0100229
230debug_$(1): $(1)
231 $(GDB) -x "$(BOARD_LIB)/resources/gcc/$(BOARD)_$(1).gdb" -ex "reset" -readnow -se $(OUTPUT)-$(1).elf
232endef
233
234$(foreach MEMORY, $(MEMORIES), $(eval $(call RULES,$(MEMORY))))
235
236program:
Christina Quast58c1ae32015-04-07 18:19:13 +0200237 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 +0100238
Christina Quast55cd8732015-04-08 00:10:50 +0200239SERIAL ?= /dev/ttyUSB0
240log:
Harald Welte0b7e5f32019-12-02 19:25:55 +0100241 stty -F $(SERIAL) 921600
Christina Quastc63da3f2015-04-13 22:09:50 +0200242 lsof $(SERIAL) && echo "log is already opened" || ( sed -u "s/\r//" $(SERIAL) | ts )
Christina Quast55cd8732015-04-08 00:10:50 +0200243
Christina Quastc8ae58b2014-11-28 11:02:16 +0100244clean:
Harald Welte36f888f2019-12-06 19:17:53 +0100245 -rm -f apps/$(APP)/usb_strings.txt.patched
Harald Welte0380d742017-05-07 11:20:40 +0200246 -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 +0200247
248install:
249 mkdir -p $(DESTDIR)/usr/share/simtrace2
250 cp $(BIN)/*.bin $(BIN)/*.elf $(BIN)/*.elf.txt $(BIN)/*.map $(DESTDIR)/usr/share/simtrace2