blob: fa72bd34a96a3c96ec4e9a7622c2c4083af6ef29 [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
Harald Welte912b1832017-03-06 09:28:13 +010098C_LIBUSB_RT = dfu.c dfu_runtime.c
Harald Welted09829d2017-02-27 22:58:59 +010099C_LIBUSB_DFU = dfu.c dfu_desc.c dfu_driver.c
Harald Welte8e7fca32017-05-07 16:14:33 +0200100C_LIBCOMMON = string.c stdio.c fputs.c usb_buf.c ringbuffer.c pseudo_talloc.c host_communication.c
Harald Welted09829d2017-02-27 22:58:59 +0100101
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
Kévin Redon6228d182019-05-23 17:33:45 +0200123# allow asserting the peer SAM3S ERASE signal to completely erase the flash
124# only applicable for qmod board
125ALLOW_PEER_ERASE?=0
126
Harald Welted09829d2017-02-27 22:58:59 +0100127DEBUG_PHONE_SNIFF?=0
128
129#CFLAGS+=-DUSB_NO_DEBUG=1
130
131# Optimization level, put in comment for debugging
Harald Welteedf9c9d2017-02-27 23:24:22 +0100132OPTIMIZATION ?= -Os
Harald Welted09829d2017-02-27 22:58:59 +0100133
134
135
Christina Quastc8ae58b2014-11-28 11:02:16 +0100136# Flags
Harald Welte3f5e3dd2017-02-27 13:53:17 +0100137INCLUDES_USB = -I$(AT91LIB)/usb/include -I$(AT91LIB)
Christina Quast96493612015-01-03 22:22:36 +0100138
Harald Welte3f5e3dd2017-02-27 13:53:17 +0100139INCLUDES = $(INCLUDES_USB)
140INCLUDES += -I$(AT91LIB)/libchip_sam3s -I$(AT91LIB)/libchip_sam3s/include
141INCLUDES += -I$(AT91LIB)/libchip_sam3s/cmsis
142INCLUDES += -Ilibboard/common/include -Ilibboard/$(BOARD)/include
143INCLUDES += -Ilibcommon/include
144INCLUDES += -Ilibosmocore/include
Harald Welte3f5e3dd2017-02-27 13:53:17 +0100145INCLUDES += -Isrc_simtrace -Iinclude
Harald Welte2363fa02017-03-05 10:16:25 +0100146INCLUDES += -Iapps/$(APP)
Christina Quastc8ae58b2014-11-28 11:02:16 +0100147
Harald Weltec3941092018-08-26 09:53:13 +0200148CFLAGS += -Wall -Wchar-subscripts -Wcomment -Wimplicit-int -Wformat=2
Christina Quastc8ae58b2014-11-28 11:02:16 +0100149CFLAGS += -Werror-implicit-function-declaration -Wmain -Wparentheses
Christina Quast96493612015-01-03 22:22:36 +0100150CFLAGS += -Wsequence-point -Wreturn-type -Wswitch -Wtrigraphs #-Wunused
Harald Weltec35998e2017-11-28 20:47:23 +0100151CFLAGS += -Wuninitialized -Wunknown-pragmas -Wfloat-equal #-Wundef
Christina Quastc8ae58b2014-11-28 11:02:16 +0100152CFLAGS += -Wshadow -Wpointer-arith -Wbad-function-cast -Wwrite-strings
Harald Weltec35998e2017-11-28 20:47:23 +0100153CFLAGS += -Waggregate-return #-Wsign-compare
Christina Quast96493612015-01-03 22:22:36 +0100154CFLAGS += -Wmissing-format-attribute -Wno-deprecated-declarations
Christina Quast29c99b92015-04-07 20:13:44 +0200155CFLAGS += #-Wpacked
Harald Welte9457bf72017-05-07 11:20:24 +0200156CFLAGS += -Wredundant-decls -Wnested-externs #-Winline -Wlong-long
Christina Quastc8ae58b2014-11-28 11:02:16 +0100157CFLAGS += -Wunreachable-code
Harald Welte7abdb512016-03-03 17:48:32 +0100158#CFLAGS += -Wcast-align
Harald Weltef672e9d2016-02-29 14:08:12 +0100159#CFLAGS += -std=c11
Christina Quast87d141e2015-01-27 14:56:33 +0100160CFLAGS += -Wmissing-noreturn
161#CFLAGS += -Wconversion
Christina Quastd20f26d2015-01-27 14:40:31 +0100162CFLAGS += -Wno-unused-but-set-variable -Wno-unused-variable
Christina Quast87d141e2015-01-27 14:56:33 +0100163CFLAGS += -Wno-suggest-attribute=noreturn
Christina Quastc8ae58b2014-11-28 11:02:16 +0100164
Christina Quastc8ae58b2014-11-28 11:02:16 +0100165# -mlong-calls -Wall
166#CFLAGS += -save-temps -fverbose-asm
167#CFLAGS += -Wa,-a,-ad
Harald Welte9d3e3822015-11-09 00:50:54 +0100168CFLAGS += -D__ARM
Christina Quastc8ae58b2014-11-28 11:02:16 +0100169CFLAGS += --param max-inline-insns-single=500 -mcpu=cortex-m3 -mthumb # -mfix-cortex-m3-ldrd
Kévin Redon6228d182019-05-23 17:33:45 +0200170CFLAGS += -ffunction-sections -g $(OPTIMIZATION) $(INCLUDES) -D$(CHIP) -DTRACE_LEVEL=$(TRACE_LEVEL) -DDEBUG_PHONE_SNIFF=$(DEBUG_PHONE_SNIFF) -DALLOW_PEER_ERASE=$(ALLOW_PEER_ERASE)
Harald Welteadbe72a2017-03-02 23:16:01 +0100171CFLAGS += -DGIT_VERSION=\"$(GIT_VERSION)\"
172CFLAGS += -DBOARD=\"$(BOARD)\" -DBOARD_$(BOARD)
173CFLAGS += -DAPPLICATION=\"$(APP)\" -DAPPLICATION_$(APP)
Christina Quastc8ae58b2014-11-28 11:02:16 +0100174ASFLAGS = -mcpu=cortex-m3 -mthumb -Wall -g $(OPTIMIZATION) $(INCLUDES) -D$(CHIP) -D__ASSEMBLY__
Christina Quastf1192c62014-12-05 13:03:27 +0100175LDFLAGS = -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 +0100176#LD_OPTIONAL=-Wl,--print-gc-sections -Wl,--stats
177
Christina Quastc8ae58b2014-11-28 11:02:16 +0100178
179# Append OBJ and BIN directories to output filename
180OUTPUT := $(BIN)/$(OUTPUT)
181
182#-------------------------------------------------------------------------------
183# Rules
184#-------------------------------------------------------------------------------
185
Harald Welte2363fa02017-03-05 10:16:25 +0100186all: apps/$(APP)/usb_strings_generated.h $(BIN) $(OBJ) $(MEMORIES)
Christina Quastc8ae58b2014-11-28 11:02:16 +0100187
Harald Weltecaca0b12017-05-05 22:29:15 +0200188combined: $(OUTPUT)-combined.bin
189
190$(BIN)/$(BOARD)-dfu-flash-padded.bin: $(BIN)/$(BOARD)-dfu-flash.bin
191 dd if=/dev/zero bs=16384 count=1 of=$@
192 dd if=$< conv=notrunc of=$@
193
194$(OUTPUT)-combined.bin: $(BIN)/$(BOARD)-dfu-flash-padded.bin $(OUTPUT)-dfu.bin
195 cat $^ > $@
196
Christina Quastc8ae58b2014-11-28 11:02:16 +0100197$(BIN) $(OBJ):
Harald Welteebe8b202018-06-29 21:43:42 +0200198 mkdir -p $@
Christina Quastc8ae58b2014-11-28 11:02:16 +0100199
Harald Welte2363fa02017-03-05 10:16:25 +0100200usbstring/usbstring: usbstring/usbstring.c
201 gcc $^ -o $@
202
203apps/$(APP)/usb_strings_generated.h: apps/$(APP)/usb_strings.txt usbstring/usbstring
204 cat $< | usbstring/usbstring > $@
205
Christina Quastc8ae58b2014-11-28 11:02:16 +0100206define RULES
207C_OBJECTS_$(1) = $(addprefix $(OBJ)/$(1)_, $(C_OBJECTS))
208ASM_OBJECTS_$(1) = $(addprefix $(OBJ)/$(1)_, $(ASM_OBJECTS))
209
210$(1): $$(ASM_OBJECTS_$(1)) $$(C_OBJECTS_$(1))
Harald Welted8a003d2017-02-27 20:31:09 +0100211 @$(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 +0200212 @$(NM) $(OUTPUT)-$$@.elf >$(OUTPUT)-$$@.elf.txt
213 @$(OBJCOPY) -O binary $(OUTPUT)-$$@.elf $(OUTPUT)-$$@.bin
214 @$(SIZE) $$^ $(OUTPUT)-$$@.elf
Christina Quastc8ae58b2014-11-28 11:02:16 +0100215
216$$(C_OBJECTS_$(1)): $(OBJ)/$(1)_%.o: %.c Makefile $(OBJ) $(BIN)
Christina Quastb6f77d32015-04-07 20:36:23 +0200217 @echo [COMPILING $$<]
Harald Welteadbe72a2017-03-02 23:16:01 +0100218 @$(CC) $(CFLAGS) -DENVIRONMENT_$(1) -DENVIRONMENT=\"$(1)\" -Wa,-ahlms=$(BIN)/$$*.lst -c -o $$@ $$<
Christina Quastc8ae58b2014-11-28 11:02:16 +0100219
220$$(ASM_OBJECTS_$(1)): $(OBJ)/$(1)_%.o: %.S Makefile $(OBJ) $(BIN)
221 @echo [ASSEMBLING $$@]
Harald Welteadbe72a2017-03-02 23:16:01 +0100222 @$(CC) $(ASFLAGS) -DENVIRONMENT_$(1) -DENVIRONMENT=\"$(1)\" -c -o $$@ $$<
Christina Quastc8ae58b2014-11-28 11:02:16 +0100223
224debug_$(1): $(1)
225 $(GDB) -x "$(BOARD_LIB)/resources/gcc/$(BOARD)_$(1).gdb" -ex "reset" -readnow -se $(OUTPUT)-$(1).elf
226endef
227
228$(foreach MEMORY, $(MEMORIES), $(eval $(call RULES,$(MEMORY))))
229
230program:
Christina Quast58c1ae32015-04-07 18:19:13 +0200231 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 +0100232
Christina Quast55cd8732015-04-08 00:10:50 +0200233SERIAL ?= /dev/ttyUSB0
234log:
235 stty -F $(SERIAL) 115200
Christina Quastc63da3f2015-04-13 22:09:50 +0200236 lsof $(SERIAL) && echo "log is already opened" || ( sed -u "s/\r//" $(SERIAL) | ts )
Christina Quast55cd8732015-04-08 00:10:50 +0200237
Christina Quastc8ae58b2014-11-28 11:02:16 +0100238clean:
Harald Welte0380d742017-05-07 11:20:40 +0200239 -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 +0200240
241install:
242 mkdir -p $(DESTDIR)/usr/share/simtrace2
243 cp $(BIN)/*.bin $(BIN)/*.elf $(BIN)/*.elf.txt $(BIN)/*.map $(DESTDIR)/usr/share/simtrace2