# Copyright (c) 2014-2015 Arduino LLC.  All right reserved.
# Copyright (c) 2016 Sandeep Mistry All right reserved.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

name=DFRobot nRF5 Boards
version=0.4.0

# Compile variables
# -----------------

compiler.warning_flags=-w
compiler.warning_flags.none=-w
compiler.warning_flags.default=
compiler.warning_flags.more=-Wall
compiler.warning_flags.all=-Wall -Wextra

compiler.path={runtime.tools.gcc-arm-none-eabi.path}/bin/
compiler.c.cmd=arm-none-eabi-gcc
compiler.c.flags=-mcpu={build.mcu} -mthumb -c -g -Os {compiler.warning_flags} {build.float_flags} -std=gnu11 -ffunction-sections -fdata-sections -nostdlib --param max-inline-insns-single=500 -MMD
compiler.c.elf.cmd=arm-none-eabi-g++
compiler.c.elf.flags= -Os -Wl,--gc-sections -save-temps
compiler.S.cmd=arm-none-eabi-gcc
compiler.S.flags=-c -g -x assembler-with-cpp
compiler.cpp.cmd=arm-none-eabi-g++
compiler.cpp.flags=-mcpu={build.mcu} -mthumb -c -g -Os {compiler.warning_flags} {build.float_flags} -std=gnu++11 -ffunction-sections -fdata-sections -fno-threadsafe-statics -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -MMD
compiler.ar.cmd=arm-none-eabi-ar
compiler.ar.flags=rcs
compiler.objcopy.cmd=arm-none-eabi-objcopy
compiler.objcopy.eep.flags=-O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0
compiler.elf2bin.flags=-O binary
compiler.elf2bin.cmd=arm-none-eabi-objcopy
compiler.elf2hex.flags=-O ihex
compiler.elf2hex.cmd=arm-none-eabi-objcopy
compiler.ldflags=-mcpu={build.mcu} -mthumb {build.float_flags} -Wl,--cref -Wl,--check-sections -Wl,--gc-sections -Wl,--unresolved-symbols=report-all -Wl,--warn-common -Wl,--warn-section-align
compiler.size.cmd=arm-none-eabi-size
compiler.define=-DARDUINO=

# this can be overriden in boards.txt
build.extra_flags=
build.lfclk_flags=

compiler.nrf.flags=-DNRF5 "-I{build.core.path}/avr" "-I{build.core.path}/nrf51-sdk/source/nordic_sdk/components/ble/ble_radio_notification" "-I{build.core.path}/nrf51-sdk/source/nordic_sdk/components/ble/ble_services/ble_dfu" "-I{build.core.path}/nrf51-sdk/source/nordic_sdk/components/ble/common" "-I{build.core.path}/nrf51-sdk/source/nordic_sdk/components/ble/device_manager" "-I{build.core.path}/nrf51-sdk/source/nordic_sdk/components/ble/device_manager/config" "-I{build.core.path}/nrf51-sdk/source/nordic_sdk/components/ble/peer_manager" "-I{build.core.path}/nrf51-sdk/source/nordic_sdk/components/device" "-I{build.core.path}/nrf51-sdk/source/nordic_sdk/components/drivers_nrf/ble_flash" "-I{build.core.path}/nrf51-sdk/source/nordic_sdk/components/drivers_nrf/delay" "-I{build.core.path}/nrf51-sdk/source/nordic_sdk/components/drivers_nrf/hal" "-I{build.core.path}/nrf51-sdk/source/nordic_sdk/components/drivers_nrf/pstorage" "-I{build.core.path}/nrf51-sdk/source/nordic_sdk/components/drivers_nrf/pstorage/config" "-I{build.core.path}/nrf51-sdk/source/nordic_sdk/components/libraries/bootloader_dfu" "-I{build.core.path}/nrf51-sdk/source/nordic_sdk/components/libraries/bootloader_dfu/hci_transport" "-I{build.core.path}/nrf51-sdk/source/nordic_sdk/components/libraries/crc16" "-I{build.core.path}/nrf51-sdk/source/nordic_sdk/components/libraries/hci" "-I{build.core.path}/nrf51-sdk/source/nordic_sdk/components/libraries/scheduler" "-I{build.core.path}/nrf51-sdk/source/nordic_sdk/components/libraries/timer" "-I{build.core.path}/nrf51-sdk/source/nordic_sdk/components/libraries/util" "-I{build.core.path}/nrf51-sdk/source/nordic_sdk/components/libraries/fds" "-I{build.core.path}/nrf51-sdk/source/nordic_sdk/components/libraries/fstorage" "-I{build.core.path}/nrf51-sdk/source/nordic_sdk/components/libraries/experimental_section_vars" "-I{build.core.path}/nrf51-sdk/source/nordic_sdk/components/softdevice/common/softdevice_handler" "-I{build.core.path}/nrf51-sdk/source/nordic_sdk/components/softdevice/s130/headers" "-I{build.core.path}/nrf51-sdk/source/nordic_sdk/components/toolchain" "-I{build.core.path}/mbed-classic/targets" "-I{build.core.path}/mbed-classic/targets/cmsis/TARGET_NORDIC" "-I{build.core.path}/mbed-classic/targets/cmsis/TARGET_NORDIC/TARGET_MCU_NRF51822" "-I{build.core.path}/mbed-classic/targets/cmsis/TARGET_NORDIC/TARGET_MCU_NRF51822/TOOLCHAIN_GCC_ARM" "-I{build.core.path}/mbed-classic/targets/cmsis/TARGET_NORDIC/TARGET_MCU_NRF51822/TOOLCHAIN_GCC_ARM/TARGET_MCU_NRF51_16K_S110" "-I{build.core.path}/mbed-classic/targets/hal/TARGET_NORDIC" "-I{build.core.path}/mbed-classic/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822" "-I{build.core.path}/mbed-classic/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/Lib" "-I{build.core.path}/mbed-classic/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/Lib/nordic_sdk" "-I{build.core.path}/mbed-classic/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/Lib/nordic_sdk/components" "-I{build.core.path}/mbed-classic/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/Lib/nordic_sdk/components/libraries" "-I{build.core.path}/mbed-classic/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/Lib/nordic_sdk/components/libraries/crc16" "-I{build.core.path}/mbed-classic/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/Lib/nordic_sdk/components/libraries/scheduler" "-I{build.core.path}/mbed-classic/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/Lib/nordic_sdk/components/libraries/util" "-I{build.core.path}/mbed-classic/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/Lib/s110_nrf51822_8_0_0" "-I{build.core.path}/mbed-classic/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/Lib/s130_nrf51822_1_0_0" "-I{build.core.path}/mbed-classic/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/TARGET_NRF51_MICROBIT" "-I{build.core.path}/DFmicrobit" "-I{build.core.path}/microbit-dal" "-I{build.core.path}/mbed-classic" "-I{build.core.path}/ble" "-I{build.core.path}/ble-nrf51822" "-I{build.core.path}/nrf51-sdk" "-I{build.core.path}/DFmicrobit/inc" "-I{build.core.path}/microbit-dal/inc/core" "-I{build.core.path}/microbit-dal/inc/types" "-I{build.core.path}/microbit-dal/inc/drivers" "-I{build.core.path}/microbit-dal/inc/bluetooth" "-I{build.core.path}/microbit-dal/inc/platform" "-I{build.core.path}/mbed-classic/api" "-I{build.core.path}/mbed-classic/hal" "-I{build.core.path}/mbed-classic/targets/hal" "-I{build.core.path}/mbed-classic/targets/cmsis" "-I{build.core.path}/ble-nrf51822/source/btle" "-I{build.core.path}/ble-nrf51822/source/btle/custom" "-I{build.core.path}/ble-nrf51822/source/common"

# These can be overridden in platform.local.txt
compiler.c.extra_flags=
compiler.c.elf.extra_flags=
compiler.cpp.extra_flags=
compiler.S.extra_flags=
compiler.ar.extra_flags=
compiler.elf2bin.extra_flags=
compiler.elf2hex.extra_flags=


# Compile patterns
# ----------------

## Compile c files
recipe.c.o.pattern="{compiler.path}{compiler.c.cmd}" {compiler.c.flags} -DF_CPU={build.f_cpu} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {compiler.c.extra_flags} {build.extra_flags} {compiler.nrf.flags} {build.lfclk_flags} {includes} "{source_file}" -o "{object_file}"

## Compile c++ files
recipe.cpp.o.pattern="{compiler.path}{compiler.cpp.cmd}" {compiler.cpp.flags} -DF_CPU={build.f_cpu} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {compiler.cpp.extra_flags} {build.extra_flags} {compiler.nrf.flags} {build.lfclk_flags} {includes} "{source_file}" -o "{object_file}"

## Compile S files
recipe.S.o.pattern="{compiler.path}{compiler.S.cmd}" {compiler.S.flags} -DF_CPU={build.f_cpu} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {compiler.S.extra_flags} {build.extra_flags} {build.lfclk_flags} {includes} "{source_file}" -o "{object_file}"

## Create archives
recipe.ar.pattern="{compiler.path}{compiler.ar.cmd}" {compiler.ar.flags} {compiler.ar.extra_flags} "{archive_file_path}" "{object_file}"

## Combine gc-sections, archives, and objects
recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" "-L{build.path}" {compiler.c.elf.flags} {compiler.c.elf.extra_flags} "-L{build.core.path}/mbed-classic/targets/cmsis/TARGET_NORDIC/TARGET_MCU_NRF51822/TOOLCHAIN_GCC_ARM" "-T{build.ldscript}" "-Wl,-Map,{build.path}/{build.project_name}.map" --specs=nano.specs --specs=nosys.specs {compiler.ldflags} -o "{build.path}/{build.project_name}.elf" {object_files} -Wl,--start-group -lm "{build.path}/{archive_file}" -Wl,--end-group

## Create output (bin file)
recipe.objcopy.bin.pattern="{compiler.path}{compiler.elf2bin.cmd}" {compiler.elf2bin.flags} {compiler.elf2bin.extra_flags} "{build.path}/{build.project_name}.elf" "{build.path}/{build.project_name}.bin"

## Create output (hex file)
recipe.objcopy.hex.pattern="{compiler.path}{compiler.elf2hex.cmd}" {compiler.elf2hex.flags} {compiler.elf2hex.extra_flags} "{build.path}/{build.project_name}.elf" "{build.path}/{build.project_name}.hex"

## Save bin
recipe.output.tmp_file_bin={build.project_name}.bin
recipe.output.save_file_bin={build.project_name}.save.bin

## Save hex
recipe.output.tmp_file_hex={build.project_name}.hex
recipe.output.save_file_hex={build.project_name}.save.hex

## Compute size
recipe.size.pattern="{compiler.path}{compiler.size.cmd}" -A "{build.path}/{build.project_name}.elf"
recipe.size.regex=\.text\s+([0-9]+).*

## Export Compiled Binary
recipe.output.tmp_file={build.project_name}.hex
recipe.output.save_file={build.project_name}.{build.variant}.hex

#
# OpenOCD sketch upload
#

tools.openocd.path={runtime.tools.openocd.path}
tools.openocd.cmd=bin/openocd
tools.openocd.cmd.windows=bin/openocd.exe

tools.openocd.upload.params.verbose=-d2
tools.openocd.upload.params.quiet=-d0
tools.openocd.upload.pattern="{path}/{cmd}" {upload.verbose} -f interface/{upload.protocol}.cfg -c "{upload.setup_command}" -f target/{upload.target}.cfg -c "program {{{build.path}/{build.project_name}.hex}} verify reset; shutdown;"

tools.openocd.program.params.verbose=-d2
tools.openocd.program.params.quiet=-d0
tools.openocd.program.pattern="{path}/{cmd}" {program.verbose} -f interface/{program.protocol}.cfg -c "{program.setup_command}" -f target/{upload.target}.cfg -c "program {{{build.path}/{build.project_name}.hex}} verify reset; shutdown;"

tools.openocd.erase.params.verbose=-d3
tools.openocd.erase.params.quiet=-d0
tools.openocd.erase.pattern=

tools.openocd.bootloader.params.verbose=-d2
tools.openocd.bootloader.params.quiet=-d0
tools.openocd.bootloader.pattern="{path}/{cmd}" {bootloader.verbose} -f interface/{program.protocol}.cfg -c "{program.setup_command}" -f target/{upload.target}.cfg -c "init; halt; nrf51 mass_erase; program {{{runtime.platform.path}/cores/nRF5/SDK/components/softdevice/{softdevice}/hex/{softdevice}_{upload.target}_{softdeviceversion}_softdevice.hex}} verify reset; shutdown;"

#
# blackmagic probe upload
#
tools.blackmagicprobe.path={runtime.tools.openocd.path}
tools.blackmagicprobe.cmd=arm-none-eabi-gdb

tools.blackmagicprobe.upload.speed=230400

tools.blackmagicprobe.erase.params.verbose=
tools.blackmagicprobe.erase.params.quiet=-q --batch-silent
tools.blackmagicprobe.erase.pattern="{path}{cmd}" -quiet -ex "target extended-remote {serial.port}" -ex "monitor swdp_scan" -ex "attach 1" -ex "monitor erase mass" -ex "echo \nErase finished!\n" -ex "detach" -ex "quit"
tools.blackmagicprobe.erase.pattern.windows="{path}{cmd}" -quiet -ex "target extended-remote \\.\{serial.port}" -ex "monitor swdp_scan" -ex "attach 1" -ex "monitor erase mass" -ex "echo \nErase finished!\n" -ex "detach" -ex "quit"

tools.blackmagicprobe.upload.params.verbose=
tools.blackmagicprobe.upload.params.quiet=-q --batch-silent
tools.blackmagicprobe.upload.pattern="{path}{cmd}" -quiet -cd "{build.path}" -b {upload.speed} -l 10 -ex "set debug remote 0" -ex "set target-async off" -ex "set remotetimeout 10" -ex "set mem inaccessible-by-default off" -ex "set confirm off" -ex "set height 0" -ex "target extended-remote {serial.port}" -ex "monitor swdp_scan" -ex "attach 1" -ex "file {build.project_name}.elf" -ex "load" -ex "tbreak main" -ex "run" -ex "echo \nUpload complete!\n" -ex "quit"
tools.blackmagicprobe.upload.pattern.windows="{path}{cmd}" -quiet -cd "{build.path}" -b {upload.speed} -l 10 -ex "set debug remote 0" -ex "set target-async off" -ex "set remotetimeout 10" -ex "set mem inaccessible-by-default off" -ex "set confirm off" -ex "set height 0" -ex "target extended-remote \\.\{serial.port}" -ex "monitor swdp_scan" -ex "attach 1" -ex "file {build.project_name}.elf" -ex "load" -ex "tbreak main" -ex "run" -ex "echo \nUpload complete!\n" -ex "quit"

tools.blackmagicprobe.program.params.verbose=
tools.blackmagicprobe.program.params.quiet=-q --batch-silent
tools.blackmagicprobe.program.pattern="{path}{cmd}" -quiet -cd "{build.path}" -b {upload.speed} -l 10 -ex "set debug remote 0" -ex "set target-async off" -ex "set remotetimeout 10" -ex "set mem inaccessible-by-default off" -ex "set confirm off" -ex "set height 0" -ex "target extended-remote {serial.port}" -ex "monitor swdp_scan" -ex "attach 1" -ex "file {build.project_name}.elf" -ex "load" -ex "tbreak main" -ex "run" -ex "echo \nProgram complete!\n" -ex "quit"
tools.blackmagicprobe.program.pattern.windows="{path}{cmd}" -quiet -cd "{build.path}" -b {upload.speed} -l 10 -ex "set debug remote 0" -ex "set target-async off" -ex "set remotetimeout 10" -ex "set mem inaccessible-by-default off" -ex "set confirm off" -ex "set height 0" -ex "target extended-remote \\.\{serial.port}" -ex "monitor swdp_scan" -ex "attach 1" -ex "file {build.project_name}.elf" -ex "load" -ex "tbreak main" -ex "run" -ex "echo \nProgram complete!\n" -ex "quit"

tools.blackmagicprobe.bootloader.params.verbose=
tools.blackmagicprobe.bootloader.params.quiet=-q --batch-silent
tools.blackmagicprobe.bootloader.pattern="{path}{cmd}" -quiet -cd "{runtime.platform.path}/cores/nRF5/SDK/components/softdevice/{softdevice}/hex/" -ex "target extended-remote {serial.port}" -ex "monitor swdp_scan" -ex "attach 1" -ex "monitor erase mass" -ex "load {softdevice}_{upload.target}_{softdeviceversion}_softdevice.hex" -ex "echo \nSoftdevice upload complete!\n" -ex "detach" -ex "quit"
tools.blackmagicprobe.bootloader.pattern.windows="{path}{cmd}" -quiet -cd "{runtime.platform.path}/cores/nRF5/SDK/components/softdevice/{softdevice}/hex/" -ex "target extended-remote \\.\{serial.port}" -ex "monitor swdp_scan" -ex "attach 1" -ex "monitor erase mass" -ex "load {softdevice}_{upload.target}_{softdeviceversion}_softdevice.hex" -ex "echo \nSoftdevice upload complete!\n" -ex "detach" -ex "quit"
