Files
stm32h7-libopencm3/1gpio/xmake.lua
2025-09-28 23:33:51 +08:00

102 lines
3.2 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- 工程名
set_project("stm32h7")
-- 定义工具链
toolchain("arm-none-eabi")
set_kind("standalone")
set_sdkdir("/home/time/doc/mybin/arm-none-eabi")
toolchain_end()
-- 使用自定义的 arm-none-eabi 工具链
set_toolchains("arm-none-eabi")
-- 设置平台与架构
set_plat("linux")
set_arch("arm32")
-- 编译优化与调试
set_optimize("none")
set_symbols("debug")
-- 目标程序
target("gpio")
-- 设置目标类型,编译为可执行文件
set_kind("binary")
-- 添加源文件
add_files("user/src/*.c")
-- 设置头文件路径
add_includedirs("user/inc", {public = true})
-- 设置输出目录
set_targetdir("$(projectdir)/bin")
-- 让 elf 文件名跟随 target 名
on_load(function (target)
target:set("filename", target:name() .. ".elf")
-- 同时设置 map 文件输出到 bin
target:add("ldflags",
"-Wl,-Map=" .. path.join(target:targetdir(), target:name() .. ".map"),
{force = true}
)
end)
-- 架构 & FPU
add_cxflags("-mthumb", "-mcpu=cortex-m7", "-mfpu=fpv5-d16", "-mfloat-abi=hard", {force = true})
add_asflags("-mthumb", "-mcpu=cortex-m7", "-mfpu=fpv5-d16", "-mfloat-abi=hard", {force = true})
add_ldflags("-mthumb", "-mcpu=cortex-m7", "-mfpu=fpv5-d16", "-mfloat-abi=hard", {force = true})
-- 头文件路径 (libopencm3)
add_includedirs("../../libopencm3/include", {public = true})
-- 链接 libopencm3
add_linkdirs("../../libopencm3/lib")
add_links("opencm3_stm32h7")
-- 额外链接选项
add_ldflags("-T./cortex-m-generic.ld", "--static", "-nostartfiles", "-Wl,--gc-sections", {force = true})
add_syslinks("c", "gcc", "nosys")
-- 预处理宏
add_defines("STM32H7")
-- 生成额外的二进制文件
after_build(function (target)
local elf = target:targetfile()
local bindir = target:targetdir()
local name = target:name()
os.execv("arm-none-eabi-objcopy", {"-Obinary", elf, path.join(bindir, name .. ".bin")})
os.execv("arm-none-eabi-objcopy", {"-Oihex", elf, path.join(bindir, name .. ".hex")})
os.execv("arm-none-eabi-objcopy", {"-Osrec", elf, path.join(bindir, name .. ".srec")})
os.execv("arm-none-eabi-objdump", {"-S", elf}, {stdout = path.join(bindir, name .. ".list")})
end)
-- 自定义清理逻辑
on_clean(function (target)
os.rm("build")
os.rm("bin")
end)
-- 定义 flash 任务(使用 openocd
task("flash")
set_menu {
usage = "xmake flash [options]",
description = "Flash firmware to STM32H7 using OpenOCD.",
options = {
{'t', "target", "kv", nil, "Set the target name"}
}
}
on_run(function (args)
import("core.project.project")
local tname = args.target or (project.targets()[1] and project.targets()[1]:name())
assert(tname, "No target found!")
local t = project.target(tname)
assert(t, "Target " .. tname .. " not found!")
local elf = t:targetfile()
os.execv("openocd", {
"-f", "interface/stlink.cfg",
"-f", "target/stm32h7x.cfg",
"-c", "program " .. elf .. " verify reset exit"
})
end)