# STM32 ARM Core and platform.
# ------------------------------
#
# For more info:
# https://arduino.github.io/arduino-cli/latest/platform-specification/

name=STM32 boards groups (Board to be selected from Tools submenu 'Board part number')
version=2.13.0-dev


# Define variables used multiple times in platform file
# --------------------
tool_suffix=
tool_suffix.windows=.exe

busybox=
busybox.windows={runtime.tools.STM32Tools.path}/win/busybox.exe

toolchain_dir={runtime.tools.xpack-arm-none-eabi-gcc-14.2.1-1.1.path}
openocd_dir={runtime.tools.xpack-openocd-0.12.0-6.path}

tools_bin_path.windows={runtime.tools.STM32Tools.path}/win
tools_bin_path.macosx={runtime.tools.STM32Tools.path}/macosx
tools_bin_path.linux={runtime.tools.STM32Tools.path}/linux

core_stm32_dir={build.core.path}/stm32
hal_dir={build.system.path}/Drivers/{build.series}_HAL_Driver
cmsis_dir={runtime.tools.CMSIS-6.2.0.path}/CMSIS
cmsis_dsp={runtime.tools.CMSIS_DSP-1.16.2.path}
cmsis_nn={runtime.tools.CMSIS_NN-7.0.0.path}
cmsis_dev_dir={build.system.path}/Drivers/CMSIS/Device/ST/{build.series}
usbd_core_dir={build.system.path}/Middlewares/ST/STM32_USB_Device_Library/Core
builtin_library_dir={build.core.path}/../../libraries
SrcWrapper_include_dir={builtin_library_dir}/SrcWrapper/inc
VirtIO_include_dir={builtin_library_dir}/VirtIO/inc
USBDevice_include_dir={builtin_library_dir}/USBDevice/inc


# STM compile variables
# ----------------------
compiler.stm.extra_include="-I{build.source.path}" "-I{build.core.path}/avr" "-I{core_stm32_dir}" "-I{SrcWrapper_include_dir}" "-I{SrcWrapper_include_dir}/LL" "-I{hal_dir}/Inc" "-I{hal_dir}/Src" "-I{build.system.path}/{build.series}" "-I{USBDevice_include_dir}" "-I{usbd_core_dir}/Inc" "-I{usbd_core_dir}/Src" "-I{VirtIO_include_dir}" {build.virtio_extra_include}
compiler.arm.cmsis.c.flags="-I{cmsis_dir}/Core/Include/" "-I{cmsis_dev_dir}/Include/" "-I{cmsis_dev_dir}/Source/Templates/gcc/" "-I{cmsis_dsp}/Include" "-I{cmsis_dsp}/PrivateInclude"

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

# EXPERIMENTAL feature: optimization flags
#  - this is alpha and may be subject to change without notice
compiler.optimization_flags={build.flags.optimize} {build.flags.debug}
compiler.optimization_flags.release={build.flags.optimize} {build.flags.debug}
compiler.optimization_flags.debug=-Og -g

compiler.path={toolchain_dir}/bin/

compiler.S.cmd=arm-none-eabi-gcc
compiler.c.cmd=arm-none-eabi-gcc
compiler.cpp.cmd=arm-none-eabi-g++
compiler.ar.cmd=arm-none-eabi-gcc-ar
compiler.c.elf.cmd=arm-none-eabi-gcc
compiler.objcopy.cmd=arm-none-eabi-objcopy
compiler.elf2hex.cmd=arm-none-eabi-objcopy
compiler.libraries.ldflags=

compiler.extra_flags=-mcpu={build.mcu} {build.fpu} {build.float-abi} -DVECT_TAB_OFFSET={build.flash_offset} -DUSE_HAL_DRIVER -DUSE_FULL_LL_DRIVER -mthumb "@{build.opt.path}"

compiler.S.flags={compiler.extra_flags} -c -x assembler-with-cpp {compiler.stm.extra_include}

compiler.c.flags={compiler.extra_flags} -c {compiler.optimization_flags} {compiler.warning_flags} -std={compiler.c.std} -ffunction-sections -fdata-sections --param max-inline-insns-single=500 -MMD {compiler.stm.extra_include}

compiler.cpp.flags={compiler.extra_flags} -c {compiler.optimization_flags} {compiler.warning_flags} -std={compiler.cpp.std} -ffunction-sections -fdata-sections -fno-threadsafe-statics --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -fno-use-cxa-atexit -MMD {compiler.stm.extra_include}

compiler.ar.flags=rcs

compiler.c.elf.flags=-mcpu={build.mcu} {build.fpu} {build.float-abi} -mthumb {compiler.optimization_flags} {build.flags.ldspecs} -Wl,--defsym=LD_FLASH_OFFSET={build.flash_offset} -Wl,--defsym=LD_MAX_SIZE={upload.maximum_size} -Wl,--defsym=LD_MAX_DATA_SIZE={upload.maximum_data_size} -Wl,--cref -Wl,--check-sections -Wl,--gc-sections -Wl,--entry=Reset_Handler -Wl,--unresolved-symbols=report-all -Wl,--warn-common

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.elf2hex.flags=-O ihex

compiler.ldflags=-Wl,--no-warn-rwx-segments
compiler.size.cmd=arm-none-eabi-size
compiler.define=-DARDUINO=

# These can be overridden in boards.txt
build.st_extra_flags=
build.extra_flags=
build.bootloader_flags=
build.ldscript=ldscript.ld
build.variant_h=variant_generic.h

# These can be overridden in platform.local.txt
compiler.c.st_extra_flags={build.peripheral_pins}
compiler.c.extra_flags=
compiler.c.std=gnu17
compiler.c.elf.extra_flags=
compiler.cpp.extra_flags=
compiler.cpp.std=gnu++17
compiler.S.st_extra_flags={build.startup_file}
compiler.S.extra_flags=
compiler.ar.extra_flags=
compiler.elf2bin.extra_flags=
compiler.elf2hex.extra_flags=

# USB Flags
# ---------
build.usb_flags=-DUSBCON {build.usb_speed} -DUSBD_VID={build.vid} -DUSBD_PID={build.pid} -DHAL_PCD_MODULE_ENABLED

# Specify defaults for vid/pid
# Boards should specify either both, or neither of these.
vid.0=0x0483
pid.0=0x5740
build.vid={vid.0}
build.pid={pid.0}
upload.vid.0=0x0483
upload.pid.0=0xdf11
upload.vid={upload.vid.0}
upload.pid={upload.pid.0}
upload.address=0x8000000
upload.mode=UR
upload.start=0x8000000
upload.parity=even

# To customize the USB manufacturer or product string, must add defines
# for them, e.g.:
#    some_board.build.extra_flags='-DUSB_MANUFACTURER_STRING="My Company"' '-DUSB_PRODUCT_STRING="My Product"'
# This cannot be done using build variables and specifying the -D
# options here, since then the default would be a defined, but empty macro
# that the preprocessor cannot detect.

# VirtIO RPMsg Serial Flags
build.virtio_flags=-DVIRTIOCON -DNO_ATOMIC_64_SUPPORT -DMETAL_INTERNAL -DMETAL_MAX_DEVICE_REGIONS=2 -DVIRTIO_SLAVE_ONLY -DVIRTIO_LOG
build.virtio_extra_include="-I{build.system.path}/Middlewares/OpenAMP" "-I{build.system.path}/Middlewares/OpenAMP/open-amp/lib/include" "-I{build.system.path}/Middlewares/OpenAMP/libmetal/lib/include" "-I{build.system.path}/Middlewares/OpenAMP/virtual_driver"

# Build information's
build.info.flags=-D{build.series} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} -DBOARD_NAME="{build.board}" -DVARIANT_H="{build.variant_h}"

# Defaults config
build.xSerial=-DHAL_UART_MODULE_ENABLED
build.enable_usb=
build.usb_speed=
build.enable_virtio=
build.peripheral_pins=
build.startup_file=
build.fpu=
build.float-abi=
build.flags.optimize=-Os
build.flags.debug=-DNDEBUG
build.flags.ldspecs=--specs=nano.specs

# Pre and post build hooks
build.opt.name=build.opt
build.opt.path={build.path}/sketch/{build.opt.name}

extras.path={build.system.path}/extras

# Create {build.opt} if not exists in the output sketch dir and force include of SrcWrapper library
recipe.hooks.prebuild.1.pattern="{busybox}" sh "{extras.path}/prebuild.sh" "{build.path}" "{build.source.path}" "{runtime.platform.path}" "usb={build.enable_usb}" "virtio={build.enable_virtio}"

# compile patterns
# ---------------------

## Compile c files
recipe.c.o.pattern="{compiler.path}{compiler.c.cmd}" {compiler.c.flags} {build.info.flags} {compiler.c.st_extra_flags} {compiler.c.extra_flags} {build.st_extra_flags} {build.extra_flags} {compiler.arm.cmsis.c.flags} {includes} "{source_file}" -o "{object_file}"

## Compile c++ files
recipe.cpp.o.pattern="{compiler.path}{compiler.cpp.cmd}" {compiler.cpp.flags} {build.info.flags} {compiler.cpp.extra_flags} {build.st_extra_flags} {build.extra_flags} {compiler.arm.cmsis.c.flags} {includes} "{source_file}" -o "{object_file}"

## Compile S files
recipe.S.o.pattern="{compiler.path}{compiler.S.cmd}" {compiler.S.flags} {build.info.flags} {compiler.S.st_extra_flags} {compiler.S.extra_flags} {build.st_extra_flags} {build.extra_flags} {compiler.arm.cmsis.c.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}" {compiler.c.elf.flags} "-Wl,--default-script={build.variant.path}/{build.ldscript}" "-Wl,--script={build.system.path}/ldscript.ld" "-Wl,-Map,{build.path}/{build.project_name}.map" {compiler.c.elf.extra_flags} {compiler.ldflags} -o "{build.path}/{build.project_name}.elf" "-L{build.path}" -Wl,--start-group {object_files} {compiler.libraries.ldflags} "{archive_file_path}" -lc -Wl,--end-group -lm -lgcc -lstdc++

## Create output (.bin file)
recipe.objcopy.bin.pattern="{compiler.path}{compiler.objcopy.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"

build.preferred_out_format=bin

## Save binary
recipe.output.tmp_file={build.project_name}.{build.preferred_out_format}
recipe.output.save_file={build.project_name}.{build.board}.{build.preferred_out_format}

## Compute size
recipe.size.pattern="{compiler.path}{compiler.size.cmd}" -A "{build.path}/{build.project_name}.elf"
recipe.size.regex=^(?:\.text|\.data|\.rodata)\s+([0-9]+).*
recipe.size.regex.data=^(?:\.data|\.bss|\.noinit)\s+([0-9]+).*
recipe.size.regex.eeprom=^(?:\.eeprom)\s+([0-9]+).*


# Uploader tool
# -------------------

# Upload to board via mass storage
tools.massStorageCopy.cmd=massStorageCopy.sh
tools.massStorageCopy.cmd.windows=massStorageCopy.bat
tools.massStorageCopy.upload.params.verbose=
tools.massStorageCopy.upload.params.quiet=
tools.massStorageCopy.upload.pattern="{tools_bin_path}/{cmd}" {upload.verbose} -I "{build.path}/{build.project_name}.bin" -O "{node}"

# STM32CubeProgrammer upload
tools.stm32CubeProg.path={runtime.tools.STM32Tools.path}
tools.stm32CubeProg.cmd=stm32CubeProg.sh
tools.stm32CubeProg.upload.params.verbose=
tools.stm32CubeProg.upload.params.quiet=
tools.stm32CubeProg.upload.pattern="{busybox}" sh "{path}/{cmd}" -i {upload.protocol} -f "{build.path}/{build.project_name}.bin" -o {build.flash_offset} {upload.options}

# blackmagic upload for generic STM32
tools.bmp_upload.cmd=arm-none-eabi-gdb
tools.bmp_upload.path={toolchain_dir}/bin
tools.bmp_upload.upload.speed=230400
tools.bmp_upload.upload.params.verbose=-batch
tools.bmp_upload.upload.params.quiet=--batch-silent
tools.bmp_upload.upload.pattern="{path}/{cmd}" -nx -b {upload.speed} {upload.verbose} -ex "set confirm off" -ex "target extended-remote {serial.port}" -ex "monitor swdp_scan" -ex "attach 1" -ex "load" -ex "compare-sections" -ex "kill" "{build.path}/{build.project_name}.elf"
tools.bmp_upload.upload.pattern.windows="{path}/{cmd}" -nx -b {upload.speed} {upload.verbose} -ex "set confirm off" -ex "target extended-remote \\.\{serial.port}" -ex "monitor swdp_scan" -ex "attach 1" -ex "load" -ex "compare-sections" -ex "kill" "{build.path}/{build.project_name}.elf"

# HID flash 2.2 (HID bootloader v2.2 for STM32F1 and STM32F4 series)
tools.hid_upload.script=hid-flash.sh
tools.hid_upload.path={runtime.tools.STM32Tools.path}
tools.hid_upload.upload.params.verbose=-d
tools.hid_upload.upload.params.quiet=n
tools.hid_upload.upload.pattern="{busybox}" sh "{path}/{script}" "{build.path}/{build.project_name}.bin" {serial.port.file}

# Upload using Maple bootloader over DFU
tools.maple_upload.script=maple_upload.sh
tools.maple_upload.path={runtime.tools.STM32Tools.path}
tools.maple_upload.upload.params.verbose=-d
tools.maple_upload.upload.params.quiet=n
tools.maple_upload.upload.pattern="{busybox}" sh "{path}/{script}" {serial.port.file} {upload.altID} {upload.vid} {upload.pid} "{build.path}/{build.project_name}.bin"

# STM32MP1 self-contained shell script
tools.remoteproc_gen.path={runtime.tools.STM32Tools.path}
tools.remoteproc_gen.script=run_arduino_gen.sh
tools.remoteproc_gen.upload.params.verbose=
tools.remoteproc_gen.upload.params.quiet=
tools.remoteproc_gen.upload.pattern="{busybox}" sh "{path}/{script}" generate "{build.path}/{build.project_name}.elf" "{build.path}/run_arduino_{build.project_name}.sh"

# OpenOCD sketch upload
tools.openocd_upload.path={openocd_dir}
tools.openocd_upload.cmd=bin/openocd
tools.openocd_upload.cmd.windows=bin/openocd.exe
tools.openocd_upload.upload.params.verbose=-d2
tools.openocd_upload.upload.params.quiet=-d0
tools.openocd_upload.upload.pattern="{path}/{cmd}" {upload.verbose} -f interface/{upload.protocol}.cfg -f target/{openocd.target}.cfg -c "program {{build.path}/{build.project_name}.elf} verify reset exit"

#
# Debugger
#
debug.executable={build.path}/{build.project_name}.elf
debug.toolchain=gcc
debug.toolchain.path={toolchain_dir}/bin
debug.toolchain.prefix=arm-none-eabi
debug.server=openocd
debug.server.openocd.path={openocd_dir}/bin/openocd
debug.server.openocd.scripts_dir={openocd_dir}/openocd/scripts
# Common config
debug.server.openocd.scripts.0=interface/stlink.cfg
debug.server.openocd.scripts.1={runtime.platform.path}/debugger/select_dapdirect.cfg
debug.server.openocd.scripts.2=target/{openocd.target}.cfg
