2019-11-30 12:11:30 -05:00
|
|
|
cmake_minimum_required(VERSION 3.13)
|
2019-11-02 10:02:24 +01:00
|
|
|
project(candleLightFirmware C ASM)
|
2019-10-26 16:53:43 +02:00
|
|
|
|
2022-01-05 14:04:59 -08:00
|
|
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
|
|
|
|
2019-10-26 16:53:43 +02:00
|
|
|
add_compile_options(
|
2022-11-10 23:10:56 +01:00
|
|
|
--specs=nano.specs
|
|
|
|
--specs=nosys.specs
|
2022-11-22 15:37:53 +01:00
|
|
|
-O2
|
2022-11-10 23:10:56 +01:00
|
|
|
-Wall
|
|
|
|
-Werror
|
|
|
|
-Wextra
|
2022-10-05 22:30:06 -04:00
|
|
|
-Wstrict-prototypes
|
2022-11-10 23:10:56 +01:00
|
|
|
-fdata-sections
|
|
|
|
-ffat-lto-objects
|
2022-10-05 22:30:06 -04:00
|
|
|
-ffreestanding
|
2022-11-10 23:10:56 +01:00
|
|
|
-ffunction-sections
|
|
|
|
-flto
|
|
|
|
-fmessage-length=0
|
|
|
|
-fsigned-char
|
|
|
|
-g3
|
|
|
|
-mthumb
|
|
|
|
-std=gnu11
|
2019-10-26 16:53:43 +02:00
|
|
|
)
|
|
|
|
|
2022-01-06 17:38:41 -05:00
|
|
|
#need these later, per-platform
|
|
|
|
set(CPUFLAGS_F0 -mcpu=cortex-m0)
|
|
|
|
set(CPUFLAGS_F4 -mcpu=cortex-m4)
|
2022-10-31 21:51:17 -04:00
|
|
|
set(CPUFLAGS_G0 -mcpu=cortex-m0)
|
2022-01-06 17:38:41 -05:00
|
|
|
|
2019-10-26 16:53:43 +02:00
|
|
|
add_link_options(
|
2022-10-05 22:30:06 -04:00
|
|
|
--specs=nano.specs
|
|
|
|
--specs=nosys.specs
|
2022-11-10 23:10:56 +01:00
|
|
|
-Wall
|
|
|
|
-Wextra
|
|
|
|
-g3
|
|
|
|
-mthumb
|
2022-11-10 23:12:31 +01:00
|
|
|
LINKER:--gc-sections
|
2022-11-10 23:13:18 +01:00
|
|
|
LINKER:--print-memory-usage
|
2019-10-26 16:53:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
add_subdirectory(libs/STM32_HAL)
|
|
|
|
add_subdirectory(libs/STM32_USB_Device_Library)
|
2020-11-01 14:58:41 -05:00
|
|
|
set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake )
|
2019-10-26 16:53:43 +02:00
|
|
|
|
2021-04-06 11:59:41 -04:00
|
|
|
|
|
|
|
# Add a custom target that produces version.h, plus
|
|
|
|
# a dummy output that's not actually produced, in order
|
|
|
|
# to force version.hmake to always be re-run before the build
|
|
|
|
|
|
|
|
|
2022-10-05 22:30:06 -04:00
|
|
|
add_custom_target(version_h
|
|
|
|
BYPRODUCTS "${CMAKE_CURRENT_BINARY_DIR}/version.h"
|
2021-04-06 11:59:41 -04:00
|
|
|
COMMAND ${CMAKE_COMMAND}
|
2022-10-05 22:30:06 -04:00
|
|
|
-D SRCDIR="${CMAKE_CURRENT_SOURCE_DIR}"
|
|
|
|
-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/gitversion.cmake
|
2021-04-06 11:59:41 -04:00
|
|
|
)
|
|
|
|
|
2019-10-26 16:53:43 +02:00
|
|
|
set(
|
2022-10-05 22:30:06 -04:00
|
|
|
SOURCE_FILES
|
|
|
|
include/config.h
|
|
|
|
|
|
|
|
include/gs_usb.h
|
|
|
|
include/usbd_desc.h src/usbd_desc.c
|
|
|
|
include/usbd_gs_can.h src/usbd_gs_can.c
|
|
|
|
src/usbd_conf.c
|
|
|
|
|
|
|
|
include/can.h src/can.c
|
2022-12-05 14:40:05 -05:00
|
|
|
include/device.h
|
2022-10-05 22:30:06 -04:00
|
|
|
include/dfu.h src/dfu.c
|
|
|
|
include/gpio.h src/gpio.c
|
|
|
|
include/led.h src/led.c
|
|
|
|
include/timer.h src/timer.c
|
|
|
|
include/util.h src/util.c
|
|
|
|
|
|
|
|
src/startup.c
|
|
|
|
src/main.c
|
|
|
|
src/interrupts.c
|
|
|
|
|
|
|
|
${CMAKE_CURRENT_BINARY_DIR}/version.h
|
2019-10-26 16:53:43 +02:00
|
|
|
)
|
|
|
|
|
2019-11-04 20:20:36 -05:00
|
|
|
|
|
|
|
####### some helpers to generate targets
|
2020-11-01 14:58:41 -05:00
|
|
|
|
|
|
|
## objcopy to produce .bin file
|
2019-10-27 16:09:02 +01:00
|
|
|
function(make_bin_file target)
|
2022-10-05 22:30:06 -04:00
|
|
|
add_custom_command(
|
|
|
|
TARGET ${target} POST_BUILD
|
|
|
|
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
|
|
|
BYPRODUCTS ${target}.bin
|
|
|
|
COMMAND ${CMAKE_OBJCOPY} -O binary ${target} ${target}.bin
|
|
|
|
)
|
2019-10-27 16:09:02 +01:00
|
|
|
endfunction()
|
|
|
|
|
2020-11-01 14:58:41 -05:00
|
|
|
## report size
|
2019-10-27 16:09:02 +01:00
|
|
|
function(show_object_size target)
|
2022-10-05 22:30:06 -04:00
|
|
|
string(REPLACE "objcopy" "size" CMAKE_OBJSIZE "${CMAKE_OBJCOPY}")
|
|
|
|
add_custom_command(
|
|
|
|
TARGET ${target} POST_BUILD
|
|
|
|
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
|
|
|
COMMAND ${CMAKE_OBJSIZE} ${target}
|
|
|
|
)
|
2019-10-27 16:09:02 +01:00
|
|
|
endfunction()
|
|
|
|
|
2020-11-01 14:58:41 -05:00
|
|
|
find_package(DFUSuffix)
|
|
|
|
## run dfu-suffix to append DFU stuff and signature; generate relevant flash-* target
|
|
|
|
# TODO ? : run with execute_proces(... OUTPUT_QUIET ) instead of '... 1>/dev/null'
|
|
|
|
|
|
|
|
function(dfu_flash target)
|
|
|
|
if (DFU_SUFFIX_EXECUTABLE)
|
|
|
|
add_custom_command( TARGET ${target}
|
|
|
|
BYPRODUCTS ${target}.dfu
|
|
|
|
COMMAND ${CMAKE_OBJCOPY} -O binary ${target} ${target}.dfu
|
|
|
|
COMMAND ${DFU_SUFFIX_EXECUTABLE} --add ${target}.dfu --vid 1d50 --pid 606f 1>/dev/null
|
|
|
|
COMMENT "create and sign dfu bin file: ${TGTNAME}_fw"
|
|
|
|
)
|
2022-10-05 22:30:06 -04:00
|
|
|
|
2020-11-01 14:58:41 -05:00
|
|
|
add_custom_target( flash-${target}
|
|
|
|
dfu-util -a 0 -R -s 0x08000000 -D ${target}.dfu
|
|
|
|
)
|
|
|
|
else()
|
|
|
|
add_custom_target( flash-${target}
|
|
|
|
dfu-util -d 1d50:606f -a 0 -R -s 0x08000000 -D ${target}.bin
|
2022-10-05 22:30:06 -04:00
|
|
|
)
|
2020-11-01 14:58:41 -05:00
|
|
|
endif()
|
2019-10-30 20:36:42 +01:00
|
|
|
endfunction()
|
|
|
|
|
2020-11-01 14:58:41 -05:00
|
|
|
|
2022-11-17 12:33:01 -05:00
|
|
|
|
|
|
|
######### ldscripts are mostly identical.
|
|
|
|
# There's many ways of handling this :
|
|
|
|
# - external script (e.g. python), see e.g. libopencm3
|
|
|
|
# - preprocess .ldscript with gcc (some caveats, since .ld syntax != C)
|
|
|
|
# - concatenate files with file(APPEND...) : I wasn't able to make this work
|
|
|
|
# - configure_file().
|
|
|
|
#
|
|
|
|
# With configure_file(), the ldscripts are generated
|
|
|
|
# at configure time.
|
|
|
|
|
|
|
|
function(populate_ldscript)
|
|
|
|
set(prefix LDV)
|
|
|
|
set(options)
|
|
|
|
set(oneValueArgs
|
|
|
|
CPU_FAMILY
|
|
|
|
FLASH_START
|
|
|
|
FLASH_SIZE
|
|
|
|
RAM_START
|
|
|
|
RAM_SIZE
|
|
|
|
STACK_SIZE
|
|
|
|
HEAP_SIZE)
|
|
|
|
set(multiValueArgs)
|
|
|
|
cmake_parse_arguments("${prefix}" "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
|
|
|
|
|
|
|
|
# now we should have function-scope vars like LDV_FLASH_SIZE etc.
|
|
|
|
# Produce a new file in the current builddir
|
|
|
|
|
|
|
|
configure_file(ldscripts/ldscript_base.inc ${${prefix}_CPU_FAMILY}_processed.ld)
|
|
|
|
endfunction()
|
|
|
|
|
|
|
|
|
|
|
|
##################
|
|
|
|
# generate ldscripts for all supported targets.
|
|
|
|
# If this gets out of hand, an external solution may be better
|
|
|
|
# (e.g. like libopencm3 does with a python script)
|
|
|
|
|
|
|
|
populate_ldscript(CPU_FAMILY STM32F042X6
|
|
|
|
FLASH_START 0x08000000
|
|
|
|
FLASH_SIZE 32k
|
|
|
|
RAM_START 0x20000000
|
|
|
|
RAM_SIZE 6k
|
|
|
|
STACK_SIZE 1k
|
2022-11-11 22:45:52 +01:00
|
|
|
HEAP_SIZE 0k
|
2022-11-17 12:33:01 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
populate_ldscript(CPU_FAMILY STM32F072XB
|
|
|
|
FLASH_START 0x08000000
|
|
|
|
FLASH_SIZE 128k
|
|
|
|
RAM_START 0x20000000
|
|
|
|
RAM_SIZE 16k
|
|
|
|
STACK_SIZE 2k
|
2022-11-11 22:45:52 +01:00
|
|
|
HEAP_SIZE 0k
|
2022-11-17 12:33:01 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
populate_ldscript(CPU_FAMILY STM32F407XE
|
|
|
|
FLASH_START 0x08000000
|
|
|
|
FLASH_SIZE 512k
|
|
|
|
RAM_START 0x20000000
|
|
|
|
RAM_SIZE 128k
|
|
|
|
STACK_SIZE 2k
|
2022-11-10 22:32:44 +01:00
|
|
|
HEAP_SIZE 1k
|
2022-11-17 12:33:01 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
populate_ldscript(CPU_FAMILY STM32G0B1XK
|
|
|
|
FLASH_START 0x08000000
|
|
|
|
FLASH_SIZE 512k
|
|
|
|
RAM_START 0x20000000
|
|
|
|
RAM_SIZE 144k
|
|
|
|
STACK_SIZE 2k
|
2022-11-10 22:32:44 +01:00
|
|
|
HEAP_SIZE 1k
|
2022-11-17 12:33:01 -05:00
|
|
|
)
|
|
|
|
|
2019-11-04 20:20:36 -05:00
|
|
|
######### commands for adding each target have a lot in common: make helper func.
|
|
|
|
# Split into two categories, F042-based and F072-based.
|
|
|
|
|
2022-12-05 14:08:22 -05:00
|
|
|
function(add_target_common TGTNAME CPU_FAMILY)
|
2019-11-04 20:20:36 -05:00
|
|
|
add_executable(${TGTNAME}_fw ${SOURCE_FILES})
|
2021-04-06 11:59:41 -04:00
|
|
|
add_dependencies(${TGTNAME}_fw version_h)
|
2022-10-05 22:30:06 -04:00
|
|
|
target_include_directories(${TGTNAME}_fw PRIVATE include/ ${CMAKE_CURRENT_BINARY_DIR})
|
2022-11-17 12:33:01 -05:00
|
|
|
target_link_options(${TGTNAME}_fw PRIVATE -T ${CPU_FAMILY}_processed.ld)
|
2022-10-05 22:30:06 -04:00
|
|
|
make_bin_file(${TGTNAME}_fw)
|
|
|
|
dfu_flash(${TGTNAME}_fw)
|
|
|
|
show_object_size(${TGTNAME}_fw)
|
2019-11-04 20:20:36 -05:00
|
|
|
endfunction()
|
|
|
|
|
2022-12-05 14:08:22 -05:00
|
|
|
function(add_f042_target TGTNAME)
|
|
|
|
add_target_common(${TGTNAME} STM32F042X6)
|
|
|
|
target_compile_options(${TGTNAME}_fw BEFORE PRIVATE ${CPUFLAGS_F0})
|
|
|
|
target_compile_definitions(${TGTNAME}_fw PRIVATE BOARD_${TGTNAME} STM32F0)
|
2022-12-05 14:40:05 -05:00
|
|
|
target_sources(${TGTNAME}_fw PRIVATE "src/device/device_f0.c")
|
2022-12-05 14:08:22 -05:00
|
|
|
target_link_options(${TGTNAME}_fw BEFORE PRIVATE ${CPUFLAGS_F0})
|
|
|
|
target_link_libraries(${TGTNAME}_fw PRIVATE STM32_HAL_STM32F042x6 STM32_USB_Device_Library_STM32F042x6)
|
|
|
|
endfunction()
|
|
|
|
|
2019-11-04 20:20:36 -05:00
|
|
|
function(add_f072_target TGTNAME)
|
2022-12-05 14:08:22 -05:00
|
|
|
add_target_common(${TGTNAME} STM32F072XB)
|
2022-10-05 22:30:06 -04:00
|
|
|
target_compile_options(${TGTNAME}_fw BEFORE PRIVATE ${CPUFLAGS_F0})
|
|
|
|
target_compile_definitions(${TGTNAME}_fw PRIVATE BOARD_${TGTNAME} STM32F0)
|
2022-12-05 14:40:05 -05:00
|
|
|
target_sources(${TGTNAME}_fw PRIVATE "src/device/device_f0.c")
|
2022-10-05 22:30:06 -04:00
|
|
|
target_link_options(${TGTNAME}_fw BEFORE PRIVATE ${CPUFLAGS_F0})
|
|
|
|
target_link_libraries(${TGTNAME}_fw PRIVATE STM32_HAL_STM32F072xB STM32_USB_Device_Library_STM32F072xB)
|
2022-12-05 14:08:22 -05:00
|
|
|
|
2019-11-04 20:20:36 -05:00
|
|
|
endfunction()
|
|
|
|
|
2022-01-05 14:04:59 -08:00
|
|
|
function(add_f407_target TGTNAME)
|
2022-12-05 14:08:22 -05:00
|
|
|
add_target_common(${TGTNAME} STM32F407XE)
|
2022-10-05 22:30:06 -04:00
|
|
|
target_compile_definitions(${TGTNAME}_fw PRIVATE BOARD_${TGTNAME} STM32F4)
|
|
|
|
target_compile_options(${TGTNAME}_fw BEFORE PRIVATE ${CPUFLAGS_F4})
|
2022-12-05 14:40:05 -05:00
|
|
|
target_sources(${TGTNAME}_fw PRIVATE "src/device/device_f4.c")
|
2022-10-05 22:30:06 -04:00
|
|
|
target_link_options(${TGTNAME}_fw BEFORE PRIVATE ${CPUFLAGS_F4})
|
|
|
|
target_link_libraries(${TGTNAME}_fw PRIVATE STM32_HAL_STM32F407xE STM32_USB_Device_Library_STM32F407xE)
|
2022-01-05 14:04:59 -08:00
|
|
|
endfunction()
|
|
|
|
|
2022-10-31 21:51:17 -04:00
|
|
|
function(add_g0b1_target TGTNAME)
|
2022-12-05 14:08:22 -05:00
|
|
|
add_target_common(${TGTNAME} STM32G0B1XK)
|
2022-10-31 21:51:17 -04:00
|
|
|
target_compile_definitions(${TGTNAME}_fw PRIVATE BOARD_${TGTNAME} STM32G0)
|
|
|
|
target_compile_options(${TGTNAME}_fw BEFORE PRIVATE ${CPUFLAGS_G0})
|
2022-12-05 14:51:57 -05:00
|
|
|
target_sources(${TGTNAME}_fw PRIVATE "src/device/device_g0.c")
|
2022-10-31 21:51:17 -04:00
|
|
|
target_link_options(${TGTNAME}_fw BEFORE PRIVATE ${CPUFLAGS_G0})
|
|
|
|
target_link_libraries(${TGTNAME}_fw PRIVATE STM32_HAL_STM32G0B1xK STM32_USB_Device_Library_STM32G0B1xK)
|
|
|
|
endfunction()
|
|
|
|
|
2019-11-04 20:20:36 -05:00
|
|
|
########## generate list of targets.
|
|
|
|
# the "_fw" part is appended automatically
|
2022-10-31 21:51:17 -04:00
|
|
|
set(TGTF042_LIST "cantact" "canalyze" "canable" "usb2can" "cannette")
|
2023-04-19 21:16:10 -07:00
|
|
|
set(TGTF072_LIST "candleLight" "CANable_MKS" "CONVERTDEVICE_xCAN" "DSD_TECH_SH_C30A" "FYSETC_UCAN")
|
2022-10-31 21:51:17 -04:00
|
|
|
set(TGTF407_LIST "STM32F4_DevBoard")
|
2022-12-19 22:34:08 +08:00
|
|
|
set(TGTG0B1_LIST "budgetcan" "CONVERTDEVICE_xCANFD")
|
2019-11-04 20:20:36 -05:00
|
|
|
|
2022-10-31 21:51:17 -04:00
|
|
|
foreach (TGTNAME IN LISTS TGTF042_LIST)
|
2019-11-04 20:26:57 -05:00
|
|
|
option(BUILD_${TGTNAME} "Build firmware for \"${TGTNAME}\" (default=yes)" ON)
|
|
|
|
if (BUILD_${TGTNAME})
|
|
|
|
add_f042_target(${TGTNAME})
|
|
|
|
endif()
|
2019-11-04 20:20:36 -05:00
|
|
|
endforeach()
|
|
|
|
|
2022-10-31 21:51:17 -04:00
|
|
|
foreach (TGTNAME IN LISTS TGTF072_LIST)
|
2019-11-04 20:26:57 -05:00
|
|
|
option(BUILD_${TGTNAME} "Build firmware for \"${TGTNAME}\" (default=yes)" ON)
|
|
|
|
if (BUILD_${TGTNAME})
|
|
|
|
add_f072_target(${TGTNAME})
|
|
|
|
endif()
|
2019-11-04 20:20:36 -05:00
|
|
|
endforeach()
|
|
|
|
|
2022-10-31 21:51:17 -04:00
|
|
|
foreach (TGTNAME IN LISTS TGTF407_LIST)
|
2022-01-05 14:04:59 -08:00
|
|
|
option(BUILD_${TGTNAME} "Build firmware for \"${TGTNAME}\" (default=yes)" ON)
|
|
|
|
if (BUILD_${TGTNAME})
|
|
|
|
add_f407_target(${TGTNAME})
|
|
|
|
endif()
|
|
|
|
endforeach()
|
|
|
|
|
2022-10-31 21:51:17 -04:00
|
|
|
foreach (TGTNAME IN LISTS TGTG0B1_LIST)
|
|
|
|
option(BUILD_${TGTNAME} "Build firmware for \"${TGTNAME}\" (default=yes)" OFF)
|
|
|
|
if (BUILD_${TGTNAME})
|
|
|
|
add_g0b1_target(${TGTNAME})
|
|
|
|
endif()
|
|
|
|
endforeach()
|
|
|
|
|
2019-11-04 20:20:36 -05:00
|
|
|
message("*******************")
|
|
|
|
message("You may now:\n\t-compile all targets ('make')\n\t-compile a single target (e.g. 'make cantact_fw'")
|
|
|
|
message("\t-flash a device (e.g. 'make flash-cantact_fw'")
|