You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

152 lines
4.5 KiB

# SPDX-License-Identifier: MIT
cmake_minimum_required(VERSION 3.10)
if (NOT CMAKE_TOOLCHAIN_FILE)
set(CMAKE_TOOLCHAIN_FILE xpack-riscv.cmake)
endif()
project(ch32v307_firmware C CXX ASM)
set(CHIPSET ch32v307)
# linker scripts
set(TARGET_LDSCRIPT_FLASH "${CMAKE_SOURCE_DIR}/${CHIPSET}/Ld/Link.ld")
# Copy them from Makefile
set(TARGET_SOURCES
"${CHIPSET}/Core/core_riscv.c"
"${CHIPSET}/Debug/debug.c"
"${CHIPSET}/Peripheral/src/ch32v30x_adc.c"
"${CHIPSET}/Peripheral/src/ch32v30x_bkp.c"
"${CHIPSET}/Peripheral/src/ch32v30x_can.c"
"${CHIPSET}/Peripheral/src/ch32v30x_crc.c"
"${CHIPSET}/Peripheral/src/ch32v30x_dac.c"
"${CHIPSET}/Peripheral/src/ch32v30x_dbgmcu.c"
"${CHIPSET}/Peripheral/src/ch32v30x_dma.c"
"${CHIPSET}/Peripheral/src/ch32v30x_dvp.c"
"${CHIPSET}/Peripheral/src/ch32v30x_eth.c"
"${CHIPSET}/Peripheral/src/ch32v30x_exti.c"
"${CHIPSET}/Peripheral/src/ch32v30x_flash.c"
"${CHIPSET}/Peripheral/src/ch32v30x_fsmc.c"
"${CHIPSET}/Peripheral/src/ch32v30x_gpio.c"
"${CHIPSET}/Peripheral/src/ch32v30x_i2c.c"
"${CHIPSET}/Peripheral/src/ch32v30x_iwdg.c"
"${CHIPSET}/Peripheral/src/ch32v30x_misc.c"
"${CHIPSET}/Peripheral/src/ch32v30x_opa.c"
"${CHIPSET}/Peripheral/src/ch32v30x_pwr.c"
"${CHIPSET}/Peripheral/src/ch32v30x_rcc.c"
"${CHIPSET}/Peripheral/src/ch32v30x_rng.c"
"${CHIPSET}/Peripheral/src/ch32v30x_rtc.c"
"${CHIPSET}/Peripheral/src/ch32v30x_sdio.c"
"${CHIPSET}/Peripheral/src/ch32v30x_spi.c"
"${CHIPSET}/Peripheral/src/ch32v30x_tim.c"
"${CHIPSET}/Peripheral/src/ch32v30x_usart.c"
"${CHIPSET}/Peripheral/src/ch32v30x_wwdg.c"
"${CHIPSET}/Startup/startup_ch32v30x_D8C.S"
"src/board.c"
"src/ch32v30x_it.c"
"src/freertos_helper.c"
"src/main.c"
"src/syscalls.c"
"src/system_ch32v30x.c"
)
# Copy them from Makefile
set(TARGET_C_DEFINES
)
# Copy them from Makefile
set(TARGET_C_INCLUDES
"${CHIPSET}/Core"
"${CHIPSET}/Debug"
"${CHIPSET}/Peripheral/inc"
"include"
)
# Shared libraries linked with application
set(TARGET_LIBS
"freertos_kernel"
"c"
"m"
"nosys"
)
# Shared library and linker script search paths
set(TARGET_LIB_DIRECTORIES
)
# Conditional flags
# DEBUG
set(CMAKE_C_FLAGS_DEBUG "-DDEBUG -O0 -g")
set(CMAKE_CXX_FLAGS_DEBUG "-DDEBUG -O0 -g")
set(CMAKE_ASM_FLAGS_DEBUG "-DDEBUG -O0 -g")
# RELEASE
set(CMAKE_C_FLAGS_RELEASE "-DNDEBUG -O2 -flto")
set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG -O2 -flto")
set(CMAKE_ASM_FLAGS_RELEASE "-DNDEBUG -O2 -flto")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "-flto")
# Final compiler flags
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -fno-common -fno-builtin -ffreestanding -fdata-sections -ffunction-sections")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fno-common -fno-builtin -ffreestanding -fdata-sections -ffunction-sections")
set(CMAKE_ASM_FLAGS "${CMAKE_C_FLAGS} -x assembler-with-cpp")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
# Include sub directories here
set(FREERTOS_PORT "GCC_RISC_V_WCH_RISC_V4A_FPU" CACHE STRING "")
set(FREERTOS_HEAP "4" CACHE STRING "")
add_library(freertos_config INTERFACE)
target_include_directories(freertos_config SYSTEM INTERFACE include)
add_subdirectory(lib/FreeRTOS-Kernel)
# Shared sources, includes and definitions
add_compile_definitions(${TARGET_C_DEFINES})
include_directories(${TARGET_C_INCLUDES})
link_directories(${TARGET_LIB_DIRECTORIES})
link_libraries(${TARGET_LIBS})
# Main targets are added here
# Create ELF
add_executable(${CMAKE_PROJECT_NAME} ${TARGET_SOURCES})
target_compile_definitions(${CMAKE_PROJECT_NAME}
PRIVATE ${TARGET_C_DEFINES_XIP}
)
set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES
OUTPUT_NAME "${CMAKE_PROJECT_NAME}.elf"
)
target_link_options(${CMAKE_PROJECT_NAME}
PRIVATE "-T${TARGET_LDSCRIPT_FLASH}"
PRIVATE "-Wl,--Map=${CMAKE_PROJECT_NAME}.map"
)
set(PROJECT_CLEAN_FILES
${CMAKE_PROJECT_NAME}.map
${CMAKE_PROJECT_NAME}.hex
)
set_property(TARGET ${CMAKE_PROJECT_NAME}
APPEND
PROPERTY ADDITIONAL_CLEAN_FILES ${PROJECT_CLEAN_FILES}
)
add_custom_command(TARGET ${CMAKE_PROJECT_NAME}
POST_BUILD
COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${CMAKE_PROJECT_NAME}> ${CMAKE_PROJECT_NAME}.hex
COMMAND ${CMAKE_SIZE} ${CMAKE_PROJECT_NAME}.elf
VERBATIM
)
add_custom_target(flash
COMMAND wchisp flash $<TARGET_FILE:${CMAKE_PROJECT_NAME}>
DEPENDS ${CMAKE_PROJECT_NAME}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
VERBATIM
COMMENT "Flashing device"
)