Tupfile.lua
-- Utility functions -----------------------------------------------------------
function run_now(command)
local handle
handle = io.popen(command)
local output = handle:read("*a")
local rc = {handle:close()}
return rc[1], output
end
-- If we simply invoke python or python3 on a pristine Windows 10, it will try
-- to open the Microsoft Store which will not work and hang tup instead. The
-- command "python --version" does not open the Microsoft Store.
-- On some systems this may return a python2 command if Python3 is not installed.
function find_python3()
success, python_version = run_now("python --version 2>&1")
if success and string.match(python_version, "Python 3") then return "python -B" end
success, python_version = run_now("python3 --version 2>&1")
if success and string.match(python_version, "Python 3") then return "python3 -B" end
error("Python 3 not found.")
end
function add_pkg(pkg)
if pkg.is_included == true then
return
end
pkg.is_included = true
for _, file in pairs(pkg.code_files or {}) do
code_files += (pkg.root or '.')..'/'..file
end
for _, dir in pairs(pkg.include_dirs or {}) do
CFLAGS += '-I'..(pkg.root or '.')..'/'..dir
end
tup.append_table(CFLAGS, pkg.cflags or {})
tup.append_table(LDFLAGS, pkg.ldflags or {})
for _, pkg in pairs(pkg.include or {}) do
add_pkg(pkg)
end
end
function compile(src_file, obj_file)
compiler = (tup.ext(src_file) == 'c') and CC or CXX
tup.frule{
inputs={src_file},
command='^o^ '..compiler..' -c %f '..tostring(CFLAGS)..' -o %o',
outputs={obj_file}
}
end
-- Packages --------------------------------------------------------------------
stm32f1xx_hal_pkg = {
root = 'Board/Drivers/STM32F1xx_HAL_Driver',
include_dirs = {
'Inc',
'../CMSIS/Device/ST/STM32F1xx/Include',
'../CMSIS/Include',
},
code_files = {
'Src/stm32f1xx_hal_gpio_ex.c',
'Src/stm32f1xx_hal_adc.c',
'Src/stm32f1xx_hal_adc_ex.c',
'Src/stm32f1xx_hal.c',
'Src/stm32f1xx_hal_rcc.c',
'Src/stm32f1xx_hal_rcc_ex.c',
'Src/stm32f1xx_hal_gpio.c',
'Src/stm32f1xx_hal_dma.c',
'Src/stm32f1xx_hal_cortex.c',
'Src/stm32f1xx_hal_pwr.c',
'Src/stm32f1xx_hal_flash.c',
'Src/stm32f1xx_hal_flash_ex.c',
'Src/stm32f1xx_hal_exti.c',
'Src/stm32f1xx_hal_tim.c',
'Src/stm32f1xx_hal_tim_ex.c',
'Src/stm32f1xx_hal_pcd.c',
'Src/stm32f1xx_hal_pcd_ex.c',
'Src/stm32f1xx_ll_usb.c',
'Src/stm32f1xx_hal_uart.c'
},
cflags = {'-mcpu=cortex-m3'}
}
simpleFoc_firmware_pkg = {
root = '.',
include_dirs = {
'.',
'MotorControl',
},
code_files = {
'MotorControl/main.cpp',
},
-- cflags = {'-D_STM32_DEF_'},
}
board_v1 = {
root = 'Board',
include = {stm32f1xx_hal_pkg},
include_dirs = {
'Inc',
'Drivers/CMSIS/Device/ST/STM32F1xx/Include'
},
code_files = {
'./startup_stm32f103xb.s',
'Src/main.c',
'Src/stm32f1xx_hal_msp.c',
--[[ 'Src/stm32f4xx_hal_msp.c',
'Src/stm32f4xx_hal_timebase_TIM.c', ]]
'Src/stm32f1xx_it.c',
'Src/system_stm32f1xx.c',
--[[ 'Src/usb_device.c',
'Src/usbd_cdc_if.c',
'Src/usbd_conf.c',
'Src/usbd_desc.c',
'Src/can.c', ]]
--'Src/i2c.c',
},
cflags = {'-DSTM32F103xB', '-DHW_VERSION_MAJOR=1'},
ldflags = {
'-TBoard/STM32F103CBTx_FLASH.ld'
-- '-l:iar_cortexM4lf_math.a',
}
}
boards = {
["v1.0-12V"] = {include={board_v1}, cflags={"-DHW_VERSION_MINOR=0 -DHW_VERSION_VOLTAGE=12"}},
["v3.2"] = {include={board_v3}, cflags={"-DHW_VERSION_MINOR=2 -DHW_VERSION_VOLTAGE=24"}},
["v3.3"] = {include={board_v3}, cflags={"-DHW_VERSION_MINOR=3 -DHW_VERSION_VOLTAGE=24"}}
}
-- Toolchain setup -------------------------------------------------------------
CC='arm-none-eabi-gcc -std=c99'
CXX='arm-none-eabi-g++ -std=c++17 -Wno-register'
LINKER='arm-none-eabi-g++'
-- C-specific flags
CFLAGS += '-D__weak="__attribute__((weak))"'
CFLAGS += '-D__packed="__attribute__((__packed__))"'
CFLAGS += '-DUSE_HAL_DRIVER'
-- -w:关闭编译时的警告,编译后不显示任何warning,因为有时在编译之后编译器会显示一些例如数据转换之类的警告,这些警告是我们平时可以忽略的。
-- -Wall:编译后显示所有警告。
-- -W:类似-Wall,会显示警告,但是只显示编译器认为会出现错误的警告。
CFLAGS += '-mthumb'
CFLAGS += { '-Wall', '-fdata-sections', '-ffunction-sections'}
CFLAGS += '-g'
-- linker flags
LDFLAGS += '-lc -lm -lnosys' -- libs
-- LDFLAGS += '-mthumb -mfloat-abi=hard -specs=nosys.specs -specs=nano.specs -u _printf_float -u _scanf_float -Wl,--cref -Wl,--gc-sections'
LDFLAGS += '-mthumb -specs=nosys.specs -Wl,--cref -Wl,--gc-sections'
-- Handle Configuration Options ------------------------------------------------
-- Switch between board versions
boardversion = tup.getconfig("BOARD_VERSION")
if boardversion == "" then
error("board version not specified - take a look at tup.config.default")
elseif boards[boardversion] == nil then
error("unknown board version "..boardversion)
end
board = boards[boardversion]
-- --not
-- TODO: remove this setting
if tup.getconfig("USB_PROTOCOL") ~= "native" and tup.getconfig("USB_PROTOCOL") ~= "" then
error("CONFIG_USB_PROTOCOL is deprecated")
end
-- UART I/O settings
if tup.getconfig("UART_PROTOCOL") ~= "ascii" and tup.getconfig("UART_PROTOCOL") ~= "" then
error("CONFIG_UART_PROTOCOL is deprecated")
end
-- Compiler settings
if tup.getconfig("STRICT") == "true" then
CFLAGS += '-Werror'
end
if tup.getconfig("NO_DRM") == "true" then
CFLAGS += '-DNO_DRM'
end
-- debug build
if tup.getconfig("DEBUG") == "true" then
CFLAGS += '-gdwarf-2 -Og'
else
CFLAGS += '-O2'
end
-- Generate Tup Rules ----------------------------------------------------------
python_command = find_python3()
print('Using python command "'..python_command..'"')
-- TODO: use CI to verify that on PRs the enums.py file is consistent with the YAML.
-- Note: we currently check this file into source control for two reasons:
-- - Don't require tup to run in order to use odrivetool from the repo
-- - On Windows, tup is unhappy with writing outside of the tup directory
--tup.frule{command=python_command..' interface_generator_stub.py --definitions odrive-interface.yaml --template enums_template.j2 --output ../tools/odrive/enums.py'}
-- tup.frule{
-- command=python_command..' ../tools/odrive/version.py --output %o',
-- outputs={'autogen/version.c'}
-- }
-- Autogen files from YAML interface definitions
--root_interface = board.include[1].root_interface
--tup.frule{inputs={'fibre-cpp/interfaces_template.j2', extra_inputs='odrive-interface.yaml'}, command=python_command..' interface_generator_stub.py --definitions odrive-interface.yaml --template %f --output %o', outputs='autogen/interfaces.hpp'}
--tup.frule{inputs={'fibre-cpp/function_stubs_template.j2', extra_inputs='odrive-interface.yaml'}, command=python_command..' interface_generator_stub.py --definitions odrive-interface.yaml --template %f --output %o', outputs='autogen/function_stubs.hpp'}
--tup.frule{inputs={'fibre-cpp/endpoints_template.j2', extra_inputs='odrive-interface.yaml'}, command=python_command..' interface_generator_stub.py --definitions odrive-interface.yaml --generate-endpoints '..root_interface..' --template %f --output %o', outputs='autogen/endpoints.hpp'}
--tup.frule{inputs={'fibre-cpp/type_info_template.j2', extra_inputs='odrive-interface.yaml'}, command=python_command..' interface_generator_stub.py --definitions odrive-interface.yaml --template %f --output %o', outputs='autogen/type_info.hpp'}
add_pkg(board)
-- add_pkg(freertos_pkg)
-- add_pkg(cmsis_pkg)
-- add_pkg(stm32_usb_device_library_pkg)
--add_pkg(fibre_pkg)
add_pkg(simpleFoc_firmware_pkg)
for _, src_file in pairs(code_files) do
obj_file = "build/obj/"..src_file:gsub("/","_"):gsub("%.","")..".o"
object_files += obj_file
compile(src_file, obj_file)
end
tup.frule{
inputs=object_files,
command='^o^ '..LINKER..' %f '..tostring(CFLAGS)..' '..tostring(LDFLAGS)..
' -Wl,-Map=%O.map -o %o',
outputs={'build/simpleFoc.elf', extra_outputs={'build/simpleFoc.map'}}
}
-- display the size
tup.frule{inputs={'build/simpleFoc.elf'}, command='arm-none-eabi-size %f'}
-- create *.hex and *.bin output formats
tup.frule{inputs={'build/simpleFoc.elf'}, command='arm-none-eabi-objcopy -O ihex %f %o', outputs={'build/simpleFoc.hex'}}
tup.frule{inputs={'build/simpleFoc.elf'}, command='arm-none-eabi-objcopy -O binary -S %f %o', outputs={'build/simpleFoc.bin'}}
if tup.getconfig('ENABLE_DISASM') == 'true' then
tup.frule{inputs={'build/simpleFoc.elf'}, command='arm-none-eabi-objdump %f -dSC > %o', outputs={'build/simpleFoc.asm'}}
end |