From 611c36fe00979fc8ba17d02587592b15465d6676 Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Wed, 2 May 2018 00:13:52 +0200 Subject: [PATCH] Added Makefiles for compiling the two projects --- .gitignore | 1 + .travis.yml | 33 ++-- Brewfile | 2 - gcc-arm-embedded.rb | 35 ---- left/Makefile | 54 ++++++ right/Makefile | 73 ++++++++ right/link/MK22FN512xxx12_flash.ld | 276 +++++++++++++++++++++++++++++ scripts/Makedefs.mk | 211 ++++++++++++++++++++++ 8 files changed, 636 insertions(+), 49 deletions(-) delete mode 100644 Brewfile delete mode 100644 gcc-arm-embedded.rb create mode 100644 left/Makefile create mode 100644 right/Makefile create mode 100644 right/link/MK22FN512xxx12_flash.ld create mode 100644 scripts/Makedefs.mk diff --git a/.gitignore b/.gitignore index 7a7b33a..d963037 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ package-lock.json node_modules +build_make diff --git a/.travis.yml b/.travis.yml index f43ba66..d3735ee 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,25 +1,34 @@ language: generic + cache: directories: - $HOME/Library/Caches/Homebrew/Cask -install: eclipse -noSplash - -application org.eclipse.equinox.p2.director - -repository http://gnuarmeclipse.sourceforge.net/updates - -installIUs ilg.gnuarmeclipse.managedbuild.cross.feature.group + +install: eclipse -noSplash + -application org.eclipse.equinox.p2.director + -repository http://gnuarmeclipse.sourceforge.net/updates + -installIUs ilg.gnuarmeclipse.managedbuild.cross.feature.group -profileProperties org.eclipse.update.install.features=true -script: eclipse -noSplash - -application org.eclipse.cdt.managedbuilder.core.headlessbuild - -import $TRAVIS_BUILD_DIR/left/build - -import $TRAVIS_BUILD_DIR/right/build - -build all + +before_script: + - tar -xf gcc-arm-none-eabi-7-2017-q4-major-*.tar.bz2 + - export PATH=$PATH:$PWD/gcc-arm-none-eabi-7-2017-q4-major/bin + +script: + - eclipse -noSplash -application org.eclipse.cdt.managedbuilder.core.headlessbuild -import $TRAVIS_BUILD_DIR/left/build -import $TRAVIS_BUILD_DIR/right/build -build all + - make -j2 -C $TRAVIS_BUILD_DIR/left + - make -j2 -C $TRAVIS_BUILD_DIR/right + matrix: include: - os: linux sudo: required - before_install: + before_install: - curl https://nixos.org/nix/install | sh && . ~/.nix-profile/etc/profile.d/nix.sh - - nix-env -i gcc-arm-embedded-4.8-2014q1-20140314 - nix-env -i eclipse-cpp + - wget "https://developer.arm.com/-/media/Files/downloads/gnu-rm/7-2017q4/gcc-arm-none-eabi-7-2017-q4-major-linux.tar.bz2" - os: osx env: PATH="$PATH:/Applications/Eclipse CPP.app/Contents/MacOS" - before_install: brew update && brew bundle -v + before_install: + - brew cask install eclipse-cpp + - wget "https://developer.arm.com/-/media/Files/downloads/gnu-rm/7-2017q4/gcc-arm-none-eabi-7-2017-q4-major-mac.tar.bz2" diff --git a/Brewfile b/Brewfile deleted file mode 100644 index dc60eee..0000000 --- a/Brewfile +++ /dev/null @@ -1,2 +0,0 @@ -cask "eclipse-cpp" -cask "./gcc-arm-embedded.rb" diff --git a/gcc-arm-embedded.rb b/gcc-arm-embedded.rb deleted file mode 100644 index 9712513..0000000 --- a/gcc-arm-embedded.rb +++ /dev/null @@ -1,35 +0,0 @@ -cask 'gcc-arm-embedded' do - version '4_8-2014q3' - sha256 '6b30901738b09a8d22fdfff99e991217444b80ac492a6163af5c06a3baaa3487' - - # launchpad.net/gcc-arm-embedded/ was verified as official when first introduced to the cask - url "https://launchpad.net/gcc-arm-embedded/4.8/4.8-2014-q3-update/+download/gcc-arm-none-eabi-#{version}-20140805-mac.tar.bz2" - name 'GCC ARM Embedded' - homepage 'https://developer.arm.com/open-source/gnu-toolchain/gnu-rm' - - binary "gcc-arm-none-eabi-#{version}/bin/arm-none-eabi-addr2line" - binary "gcc-arm-none-eabi-#{version}/bin/arm-none-eabi-ar" - binary "gcc-arm-none-eabi-#{version}/bin/arm-none-eabi-as" - binary "gcc-arm-none-eabi-#{version}/bin/arm-none-eabi-c++" - binary "gcc-arm-none-eabi-#{version}/bin/arm-none-eabi-c++filt" - binary "gcc-arm-none-eabi-#{version}/bin/arm-none-eabi-cpp" - binary "gcc-arm-none-eabi-#{version}/bin/arm-none-eabi-elfedit" - binary "gcc-arm-none-eabi-#{version}/bin/arm-none-eabi-g++" - binary "gcc-arm-none-eabi-#{version}/bin/arm-none-eabi-gcc" - binary "gcc-arm-none-eabi-#{version}/bin/arm-none-eabi-gcc-ar" - binary "gcc-arm-none-eabi-#{version}/bin/arm-none-eabi-gcc-nm" - binary "gcc-arm-none-eabi-#{version}/bin/arm-none-eabi-gcc-ranlib" - binary "gcc-arm-none-eabi-#{version}/bin/arm-none-eabi-gcov" - binary "gcc-arm-none-eabi-#{version}/bin/arm-none-eabi-gdb" - binary "gcc-arm-none-eabi-#{version}/bin/arm-none-eabi-gprof" - binary "gcc-arm-none-eabi-#{version}/bin/arm-none-eabi-ld" - binary "gcc-arm-none-eabi-#{version}/bin/arm-none-eabi-ld.bfd" - binary "gcc-arm-none-eabi-#{version}/bin/arm-none-eabi-nm" - binary "gcc-arm-none-eabi-#{version}/bin/arm-none-eabi-objcopy" - binary "gcc-arm-none-eabi-#{version}/bin/arm-none-eabi-objdump" - binary "gcc-arm-none-eabi-#{version}/bin/arm-none-eabi-ranlib" - binary "gcc-arm-none-eabi-#{version}/bin/arm-none-eabi-readelf" - binary "gcc-arm-none-eabi-#{version}/bin/arm-none-eabi-size" - binary "gcc-arm-none-eabi-#{version}/bin/arm-none-eabi-strings" - binary "gcc-arm-none-eabi-#{version}/bin/arm-none-eabi-strip" -end diff --git a/left/Makefile b/left/Makefile new file mode 100644 index 0000000..2e4b524 --- /dev/null +++ b/left/Makefile @@ -0,0 +1,54 @@ +# Copyright (C) 2018 Kristian Lauszus. All rights reserved. +# +# This software may be distributed and modified under the terms of the GNU +# General Public License version 2 (GPL2) as published by the Free Software +# Foundation and appearing in the file GPL2.TXT included in the packaging of +# this file. Please note that GPL2 Section 2[b] requires that all works based +# on this software must also be made publicly available under the terms of +# the GPL2 ("Copyleft"). +# +# Contact information +# ------------------- +# Kristian Lauszus +# Web : http://www.lauszus.com +# e-mail : lauszus@gmail.com + +# Set project name. +PROJECT_NAME = uhk_left + +# Defines the part type that this project uses. +PART = MKL03Z32VFK4 + +# Defines the linker script to use for the application. +LDSCRIPT = ../lib/KSDK_2.0_MKL03Z8xxx4/devices/MKL03Z4/gcc/MKL03Z32xxx4_flash.ld + +# Size of the heap and stack. +HEAP_SIZE = 0 +STACK_SIZE = 0x0050 + +# Set the compiler CPU and FPU options. +CPU = -mcpu=cortex-m0plus +FPU = -mfloat-abi=soft + +# Source files. +SOURCE = $(wildcard src/*.c) \ + ../lib/KSDK_2.0_MKL03Z8xxx4/devices/MKL03Z4/system_MKL03Z4.c \ + ../lib/KSDK_2.0_MKL03Z8xxx4/devices/MKL03Z4/gcc/startup_MKL03Z4.S \ + ../lib/KSDK_2.0_MKL03Z8xxx4/devices/MKL03Z4/drivers/fsl_clock.c \ + ../lib/KSDK_2.0_MKL03Z8xxx4/devices/MKL03Z4/drivers/fsl_gpio.c \ + ../lib/KSDK_2.0_MKL03Z8xxx4/devices/MKL03Z4/drivers/fsl_i2c.c \ + ../lib/KSDK_2.0_MKL03Z8xxx4/devices/MKL03Z4/drivers/fsl_lptmr.c \ + ../lib/KSDK_2.0_MKL03Z8xxx4/devices/MKL03Z4/drivers/fsl_smc.c \ + ../lib/KSDK_2.0_MKL03Z8xxx4/devices/MKL03Z4/drivers/fsl_spi.c \ + ../lib/KSDK_2.0_MKL03Z8xxx4/devices/MKL03Z4/drivers/fsl_tpm.c \ + $(wildcard ../shared/*.c) + +# Header files. +IPATH = src \ + ../lib/KSDK_2.0_MKL03Z8xxx4/CMSIS/Include \ + ../lib/KSDK_2.0_MKL03Z8xxx4/devices/MKL03Z4 \ + ../lib/KSDK_2.0_MKL03Z8xxx4/devices/MKL03Z4/drivers \ + ../shared + +# Include main Makefile. +include ../scripts/Makedefs.mk diff --git a/right/Makefile b/right/Makefile new file mode 100644 index 0000000..f6ef801 --- /dev/null +++ b/right/Makefile @@ -0,0 +1,73 @@ +# Copyright (C) 2018 Kristian Lauszus. All rights reserved. +# +# This software may be distributed and modified under the terms of the GNU +# General Public License version 2 (GPL2) as published by the Free Software +# Foundation and appearing in the file GPL2.TXT included in the packaging of +# this file. Please note that GPL2 Section 2[b] requires that all works based +# on this software must also be made publicly available under the terms of +# the GPL2 ("Copyleft"). +# +# Contact information +# ------------------- +# Kristian Lauszus +# Web : http://www.lauszus.com +# e-mail : lauszus@gmail.com + +# Set project name. +PROJECT_NAME = uhk_right + +# Defines the part type that this project uses. +PART = MK22FN512VLH12 + +# Defines the linker script to use for the application. +LDSCRIPT = link/MK22FN512xxx12_flash.ld + +# Address of the app vector table. The bootloader will take up the flash before this address. +BL_APP_VECTOR_TABLE_ADDRESS = 0xc000 + +# Size of the heap and stack. +HEAP_SIZE = 0x2000 +STACK_SIZE = 0x0400 + + # Preprocessor directives. +BUILD_FLAGS = -DCPU_$(PART)_cm4 -DBL_APP_VECTOR_TABLE_ADDRESS=$(BL_APP_VECTOR_TABLE_ADDRESS) -DUSB_STACK_BM -DBL_HAS_BOOTLOADER_CONFIG=1 + +# Set the compiler CPU and FPU options. +CPU = -mcpu=cortex-m4 +FPU = -mfpu=fpv4-sp-d16 -mfloat-abi=hard + +# Source files. +SOURCE = $(wildcard src/*.c) \ + $(wildcard src/*/*.c) \ + $(wildcard src/*/*/*.c) \ + ../lib/bootloader/src/bootloader/src/wormhole.c \ + $(wildcard ../lib/KSDK_2.0_MK22FN512xxx12/middleware/usb_1.0.0/device/*.c) \ + $(wildcard ../lib/KSDK_2.0_MK22FN512xxx12/middleware/usb_1.0.0/osa/*.c) \ + ../lib/KSDK_2.0_MK22FN512xxx12/devices/MK22F51212/system_MK22F51212.c \ + ../lib/KSDK_2.0_MK22FN512xxx12/devices/MK22F51212/gcc/startup_MK22F51212.S \ + ../lib/KSDK_2.0_MK22FN512xxx12/devices/MK22F51212/drivers/fsl_adc16.c \ + ../lib/KSDK_2.0_MK22FN512xxx12/devices/MK22F51212/drivers/fsl_clock.c \ + ../lib/KSDK_2.0_MK22FN512xxx12/devices/MK22F51212/drivers/fsl_ftm.c \ + ../lib/KSDK_2.0_MK22FN512xxx12/devices/MK22F51212/drivers/fsl_gpio.c \ + ../lib/KSDK_2.0_MK22FN512xxx12/devices/MK22F51212/drivers/fsl_i2c.c \ + ../lib/KSDK_2.0_MK22FN512xxx12/devices/MK22F51212/drivers/fsl_pit.c \ + ../lib/KSDK_2.0_MK22FN512xxx12/devices/MK22F51212/drivers/fsl_sim.c \ + ../lib/KSDK_2.0_MK22FN512xxx12/devices/MK22F51212/drivers/fsl_smc.c \ + $(wildcard ../shared/*.c) + +# Header files. +IPATH = src \ + src/ksdk_usb \ + src/buspal \ + src/buspal/bm_usb \ + ../lib/bootloader/src \ + ../lib/KSDK_2.0_MK22FN512xxx12/middleware/usb_1.0.0/device \ + ../lib/KSDK_2.0_MK22FN512xxx12/middleware/usb_1.0.0/include \ + ../lib/KSDK_2.0_MK22FN512xxx12/middleware/usb_1.0.0/osa \ + ../lib/KSDK_2.0_MK22FN512xxx12/CMSIS/Include \ + ../lib/KSDK_2.0_MK22FN512xxx12/devices/MK22F51212 \ + ../lib/KSDK_2.0_MK22FN512xxx12/devices/MK22F51212/drivers \ + ../shared + +# Include main Makefile. +include ../scripts/Makedefs.mk diff --git a/right/link/MK22FN512xxx12_flash.ld b/right/link/MK22FN512xxx12_flash.ld new file mode 100644 index 0000000..dd1e065 --- /dev/null +++ b/right/link/MK22FN512xxx12_flash.ld @@ -0,0 +1,276 @@ +/* +** ################################################################### +** Processors: MK22FN512CAP12 +** MK22FN512VDC12 +** MK22FN512VLH12 +** MK22FN512VLL12 +** MK22FN512VMP12 +** +** Compiler: GNU C Compiler +** Reference manual: K22P121M120SF7RM, Rev. 1, March 24, 2014 +** Version: rev. 2.8, 2015-02-19 +** Build: b151217 +** +** Abstract: +** Linker file for the GNU C Compiler +** +** Copyright (c) 2015 Freescale Semiconductor, Inc. +** All rights reserved. +** +** Redistribution and use in source and binary forms, with or without modification, +** are permitted provided that the following conditions are met: +** +** o Redistributions of source code must retain the above copyright notice, this list +** of conditions and the following disclaimer. +** +** o Redistributions in binary form must reproduce the above copyright notice, this +** list of conditions and the following disclaimer in the documentation and/or +** other materials provided with the distribution. +** +** o Neither the name of Freescale Semiconductor, Inc. nor the names of its +** contributors may be used to endorse or promote products derived from this +** software without specific prior written permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +** DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +** ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +** ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +** +** http: www.freescale.com +** mail: support@freescale.com +** +** ################################################################### +*/ + +/* Entry Point */ +ENTRY(Reset_Handler) + +HEAP_SIZE = DEFINED(__heap_size__) ? __heap_size__ : 0x2000; +STACK_SIZE = DEFINED(__stack_size__) ? __stack_size__ : 0x0400; +M_VECTOR_RAM_SIZE = DEFINED(__ram_vector_table__) ? 0x0400 : 0x0; + +/* This should match the value in bootloader_config.h if KBOOT is used */ +BL_APP_VECTOR_TABLE_ADDRESS = DEFINED(__bl_app_vector_table_address__) ? __bl_app_vector_table_address__ : 0; + +/* Specify the memory areas */ +MEMORY +{ + m_interrupts (RX) : ORIGIN = 0x0 + BL_APP_VECTOR_TABLE_ADDRESS, LENGTH = 0x00000400 + m_text (RX) : ORIGIN = 0x00000400 + BL_APP_VECTOR_TABLE_ADDRESS, LENGTH = 512K - 0x400 - BL_APP_VECTOR_TABLE_ADDRESS + m_data (RW) : ORIGIN = 0x1FFF0000, LENGTH = 0x00010000 + m_data_2 (RW) : ORIGIN = 0x20000000, LENGTH = 0x0000FF00 + m_noinit (RW) : ORIGIN = 0x2000FF00, LENGTH = 0x000000FF +} + +/* Define output sections */ +SECTIONS +{ + /* The startup code goes first into internal flash */ + .interrupts : + { + __VECTOR_TABLE = .; + . = ALIGN(4); + KEEP(*(.isr_vector)) /* Startup code */ + . = ALIGN(4); + } > m_interrupts + + /* The program code and other data goes into internal flash */ + .text : + { + . = ALIGN(4); + *(.text) /* .text sections (code) */ + *(.text*) /* .text* sections (code) */ + *(.rodata) /* .rodata sections (constants, strings, etc.) */ + *(.rodata*) /* .rodata* sections (constants, strings, etc.) */ + *(.glue_7) /* glue arm to thumb code */ + *(.glue_7t) /* glue thumb to arm code */ + *(.eh_frame) + KEEP (*(.init)) + KEEP (*(.fini)) + . = ALIGN(4); + } > m_text + + .ARM.extab : + { + *(.ARM.extab* .gnu.linkonce.armextab.*) + } > m_text + + .ARM : + { + __exidx_start = .; + *(.ARM.exidx*) + __exidx_end = .; + } > m_text + + .ctors : + { + __CTOR_LIST__ = .; + /* gcc uses crtbegin.o to find the start of + the constructors, so we make sure it is + first. Because this is a wildcard, it + doesn't matter if the user does not + actually link against crtbegin.o; the + linker won't look for a file to match a + wildcard. The wildcard also means that it + doesn't matter which directory crtbegin.o + is in. */ + KEEP (*crtbegin.o(.ctors)) + KEEP (*crtbegin?.o(.ctors)) + /* We don't want to include the .ctor section from + from the crtend.o file until after the sorted ctors. + The .ctor section from the crtend file contains the + end of ctors marker and it must be last */ + KEEP (*(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors)) + KEEP (*(SORT(.ctors.*))) + KEEP (*(.ctors)) + __CTOR_END__ = .; + } > m_text + + .dtors : + { + __DTOR_LIST__ = .; + KEEP (*crtbegin.o(.dtors)) + KEEP (*crtbegin?.o(.dtors)) + KEEP (*(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors)) + KEEP (*(SORT(.dtors.*))) + KEEP (*(.dtors)) + __DTOR_END__ = .; + } > m_text + + .preinit_array : + { + PROVIDE_HIDDEN (__preinit_array_start = .); + KEEP (*(.preinit_array*)) + PROVIDE_HIDDEN (__preinit_array_end = .); + } > m_text + + .init_array : + { + PROVIDE_HIDDEN (__init_array_start = .); + KEEP (*(SORT(.init_array.*))) + KEEP (*(.init_array*)) + PROVIDE_HIDDEN (__init_array_end = .); + } > m_text + + .fini_array : + { + PROVIDE_HIDDEN (__fini_array_start = .); + KEEP (*(SORT(.fini_array.*))) + KEEP (*(.fini_array*)) + PROVIDE_HIDDEN (__fini_array_end = .); + } > m_text + + __etext = .; /* define a global symbol at end of code */ + __DATA_ROM = .; /* Symbol is used by startup for data initialization */ + + .interrupts_ram : + { + . = ALIGN(4); + __VECTOR_RAM__ = .; + __interrupts_ram_start__ = .; /* Create a global symbol at data start */ + *(.m_interrupts_ram) /* This is a user defined section */ + . += M_VECTOR_RAM_SIZE; + . = ALIGN(4); + __interrupts_ram_end__ = .; /* Define a global symbol at data end */ + } > m_data + + __VECTOR_RAM = DEFINED(__ram_vector_table__) ? __VECTOR_RAM__ : ORIGIN(m_interrupts); + __RAM_VECTOR_TABLE_SIZE_BYTES = DEFINED(__ram_vector_table__) ? (__interrupts_ram_end__ - __interrupts_ram_start__) : 0x0; + + .data : AT(__DATA_ROM) + { + . = ALIGN(4); + __DATA_RAM = .; + __data_start__ = .; /* create a global symbol at data start */ + *(.data) /* .data sections */ + *(.data*) /* .data* sections */ + KEEP(*(.jcr*)) + . = ALIGN(4); + __data_end__ = .; /* define a global symbol at data end */ + } > m_data + + __DATA_END = __DATA_ROM + (__data_end__ - __data_start__); + text_end = ORIGIN(m_text) + LENGTH(m_text); + ASSERT(__DATA_END <= text_end, "region m_text overflowed with text and data") + + /* Uninitialized data section */ + .bss : ALIGN(4) + { + /* This is used by the startup in order to initialize the .bss section */ + __START_BSS = .; + __bss_start__ = .; + *(.bss) + *(.bss*) + *(COMMON) + . = ALIGN(4); + } > m_data + + .m_usb_bdt (NOLOAD) : + { + . = ALIGN(512); + *(m_usb_bdt) + } > m_data + + .m_usb_global (NOLOAD) : + { + *(m_usb_global) + __bss_end__ = .; + __END_BSS = .; + } > m_data + + .m_data_2 : + { + . = ALIGN(4); + *(.m_data_2) /* This is an User defined section */ + . = ALIGN(4); + } > m_data_2 + + .heap : + { + . = ALIGN(8); + __end__ = .; + PROVIDE(end = .); + __HeapBase = .; + . += HEAP_SIZE; + __HeapLimit = .; + __heap_limit = .; /* Add for _sbrk */ + } > m_data_2 + + .stack : + { + . = ALIGN(8); + . += STACK_SIZE; + } > m_data_2 + + /* Initializes stack on the end of block */ + __StackTop = ORIGIN(m_data_2) + LENGTH(m_data_2); + __StackLimit = __StackTop - STACK_SIZE; + PROVIDE(__stack = __StackTop); + + .ARM.attributes 0 : { *(.ARM.attributes) } + + ASSERT(__StackLimit >= __HeapLimit, "region m_data_2 overflowed with stack and heap") + + .noinit (NOLOAD): + { + . = ALIGN(4); + _noinit = .; + + *(.noinit .noinit.*) + + . = ALIGN(4) ; + _end_noinit = .; + } > m_noinit + + /* Mandatory to be word aligned, _sbrk assumes this */ + PROVIDE ( end = _end_noinit ); /* was _ebss */ + PROVIDE ( _end = _end_noinit ); + PROVIDE ( __end = _end_noinit ); + PROVIDE ( __end__ = _end_noinit ); +} diff --git a/scripts/Makedefs.mk b/scripts/Makedefs.mk new file mode 100644 index 0000000..98fc69f --- /dev/null +++ b/scripts/Makedefs.mk @@ -0,0 +1,211 @@ +# Copyright (C) 2018 Kristian Lauszus. All rights reserved. +# +# This software may be distributed and modified under the terms of the GNU +# General Public License version 2 (GPL2) as published by the Free Software +# Foundation and appearing in the file GPL2.TXT included in the packaging of +# this file. Please note that GPL2 Section 2[b] requires that all works based +# on this software must also be made publicly available under the terms of +# the GPL2 ("Copyleft"). +# +# Contact information +# ------------------- +# Kristian Lauszus +# Web : http://www.lauszus.com +# e-mail : lauszus@gmail.com + +# Debug using Semihosting. +SEMIHOSTING ?= 0 + +# Build directory. +BUILD_DIR ?= build_make + + # Preprocessor directives. +BUILD_FLAGS += -D__NEWLIB__ -D__USE_CMSIS -D__MCUXPRESSO -DCPU_$(PART) -D__STARTUP_CLEAR_BSS + +# Path to project object file. +PROJECT_OBJ = $(BUILD_DIR)/$(PROJECT_NAME).axf + +# Set the prefix for the tools to use. +PREFIX ?= arm-none-eabi + +# Determine if we are on a Windows machine and set the .exe suffix. +# We can not use the suffix command, as the PREFIX might contain spaces. +UNAME_S := $(shell uname -s) +ifeq ($(OS),Windows_NT) # Native Windows. + SUFFIX := .exe +endif + +# The command for calling the compilers. +CC = $(PREFIX)-gcc$(SUFFIX) +CXX = $(PREFIX)-g++$(SUFFIX) + +# The command for calling the linker. +LD = $(PREFIX)-g++$(SUFFIX) + +# The command for extracting images from the linked executables. +OBJCOPY = $(PREFIX)-objcopy$(SUFFIX) + +# The command for the size tool. +SIZE = $(PREFIX)-size$(SUFFIX) + +# Auto-dependency generation flags. +DEPS = -MMD -MP + +# The flags passed to the assembler. +AFLAGS = -mthumb \ + $(CPU) \ + $(FPU) \ + $(DEPS) \ + $(BUILD_FLAGS) \ + -x assembler-with-cpp + +# The flags passed to the compiler. +CFLAGS = -mthumb \ + $(CPU) \ + $(FPU) \ + $(DEPS) \ + -fno-builtin \ + -ffunction-sections \ + -fdata-sections \ + -fno-common \ + -Wdouble-promotion \ + -Woverflow \ + -Wall \ + -Wshadow \ + $(BUILD_FLAGS) + +# Compiler options for C++ only. +CXXFLAGS = -felide-constructors -fno-exceptions -fno-rtti + +# Set the C/C++ standard to use. +CSTD = -std=gnu11 +CXXSTD = -std=gnu++14 + +# Make all warnings into errors when building using Travis CI. +ifdef TRAVIS + CFLAGS += -Werror +endif + +# Set default value for the bootloader vector table address. +BL_APP_VECTOR_TABLE_ADDRESS ?= 0 + +# The flags passed to the linker. +LDFLAGS = --specs=nano.specs -mthumb $(CPU) $(FPU) -T $(LDSCRIPT) -Wl,-Map=$(PROJECT_OBJ:.axf=.map),--gc-sections,-print-memory-usage,-no-wchar-size-warning,--defsym=__heap_size__=$(HEAP_SIZE),--defsym=__stack_size__=$(STACK_SIZE),--defsym=__bl_app_vector_table_address__=$(BL_APP_VECTOR_TABLE_ADDRESS) + +# Include the following archives. +LDARCHIVES = -Wl,--start-group -lg -lgcc -lm -Wl,--end-group + +# Add flags to the build flags and linker depending on the build settings. +ifeq ($(SEMIHOSTING),1) + # Include Semihost library. + LDARCHIVES += -lcr_newlib_semihost + + # Enable printf floating numbers. + LDFLAGS += -u _printf_float +else + # Include libnosys.a if Semihosting is disabled. + LDARCHIVES += -lnosys +endif + +# Check if the DEBUG environment variable is set. +DEBUG ?= 1 +ifeq ($(DEBUG),1) + CFLAGS += -O3 -g3 -DDEBUG +else + CFLAGS += -O3 -DNDEBUG +endif + +# Add the include file paths to AFLAGS and CFLAGS. +AFLAGS += $(patsubst %,-I%,$(subst :, ,$(IPATH))) +CFLAGS += $(patsubst %,-I%,$(subst :, ,$(IPATH))) + +# Create lists of C, C++ and assembly objects. +C_OBJS := $(addsuffix .o,$(addprefix $(BUILD_DIR)/,$(basename $(filter %.c,$(abspath $(SOURCE)))))) +CPP_OBJS := $(addsuffix .o,$(addprefix $(BUILD_DIR)/,$(basename $(filter %.cpp,$(abspath $(SOURCE)))))) +S_OBJS := $(addsuffix .o,$(addprefix $(BUILD_DIR)/,$(basename $(filter %.S,$(abspath $(SOURCE)))))) + +# Create a list of all objects. +OBJS := $(C_OBJS) $(CPP_OBJS) $(S_OBJS) + +# Define the commands used for compiling the project. +LD_CMD = $(LD) $(LDFLAGS) -o $(@) $(filter %.o %.a, $(^)) $(LDARCHIVES) +CC_CMD = $(CC) $(CFLAGS) $(CSTD) -c $(<) -o $(@) +CXX_CMD = $(CXX) $(CFLAGS) $(CXXFLAGS) $(CXXSTD) -c $(<) -o $(@) +AS_CMD = $(CC) $(AFLAGS) $(CSTD) -c $(<) -o $(@) + +# Everyone need colors in their life! +INTERACTIVE := $(shell [ -t 0 ] && echo 1) +ifeq ($(INTERACTIVE),1) + color_default = \033[0m + color_bold = \033[01m + color_red = \033[31m + color_green = \033[32m + color_yellow = \033[33m + color_blue = \033[34m + color_magenta = \033[35m + color_cyan = \033[36m + color_orange = \033[38;5;172m + color_light_blue = \033[38;5;039m + color_gray = \033[38;5;008m + color_purple = \033[38;5;097m +endif + +.PHONY: all clean + +# The default rule, which causes the project to be built. +all: $(PROJECT_OBJ) + +# The rule to clean out all the build products. +clean: + @rm -rf $(BUILD_DIR) $(wildcard *~) + @echo "$(color_red)Done cleaning!$(color_default)" + +# Rebuild all objects when the Makefile changes. +$(OBJS): Makefile + +# The rule for linking the application. +$(PROJECT_OBJ): $(OBJS) $(LDSCRIPT) + @if [ '$(VERBOSE)' = 1 ]; then \ + echo $(LD_CMD); \ + else \ + echo " $(color_purple)LD$(color_default) $(notdir $(@))"; \ + fi + @echo + @$(LD_CMD) + @echo + @if [ '$(VERBOSE)' = 1 ]; then \ + $(SIZE) -Ax $(@); \ + fi + @$(OBJCOPY) -O binary $(@) $(@:.axf=.bin) + @$(OBJCOPY) -O ihex $(@) $(@:.axf=.hex) + +# The rule for building the object files from each source file. +$(C_OBJS): $(BUILD_DIR)/%.o : %.c + @mkdir -p $(@D) + @if [ '$(VERBOSE)' = 1 ]; then \ + echo $(CC_CMD); \ + else \ + echo " $(color_green)CC$(color_default) $(notdir $(<))"; \ + fi + @$(CC_CMD) + +$(CPP_OBJS): $(BUILD_DIR)/%.o : %.cpp + @mkdir -p $(@D) + @if [ '$(VERBOSE)' = 1 ]; then \ + echo $(CXX_CMD); \ + else \ + echo " $(color_cyan)CXX$(color_default) $(notdir $(<))"; \ + fi + @$(CXX_CMD) + +$(S_OBJS): $(BUILD_DIR)/%.o: %.S + @mkdir -p $(@D) + @if [ '$(VERBOSE)' = 1 ]; then \ + echo $(AS_CMD); \ + else \ + echo " $(color_magenta)AS$(color_default) $(notdir $(<))"; \ + fi + @$(AS_CMD) + +# Include the automatically generated dependency files. +-include $(OBJS:.o=.d)