Skip to content

Instantly share code, notes, and snippets.

@xingrz
Created October 23, 2023 08:06
Show Gist options
  • Save xingrz/e8d3ef4028db03ffe35b0460a03366b7 to your computer and use it in GitHub Desktop.
Save xingrz/e8d3ef4028db03ffe35b0460a03366b7 to your computer and use it in GitHub Desktop.
Build CH569EVT with CMake
export GNURISCV_TOOLCHAIN_PATH=$TOOLCHAINS/xpack-riscv-none-embed-gcc-8.2.0
cmake -G Ninja -B build -DEXAM=EXAM/GPIO && cmake --build build
cmake_minimum_required(VERSION 3.20)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR riscv32)
set(CMAKE_C_COMPILER_FORCED 1)
set(CMAKE_CXX_COMPILER_FORCED 1)
set(GNURISCV_TOOLCHAIN_PATH "$ENV{GNURISCV_TOOLCHAIN_PATH}")
if(NOT GNURISCV_TOOLCHAIN_PATH)
message(FATAL_ERROR "GNURISCV_TOOLCHAIN_PATH is not set")
endif()
set(TOOLCHAIN_HOME ${GNURISCV_TOOLCHAIN_PATH})
set(CROSS_COMPILE_TARGET riscv-none-embed)
set(CROSS_COMPILE ${TOOLCHAIN_HOME}/bin/${CROSS_COMPILE_TARGET}-)
set(CMAKE_C_COMPILER ${CROSS_COMPILE}gcc)
set(CMAKE_CXX_COMPILER ${CROSS_COMPILE}g++)
set(CMAKE_LINKER ${CROSS_COMPILE}ld)
set(CMAKE_OBJCOPY ${CROSS_COMPILE}objcopy)
set(CMAKE_SIZE ${CROSS_COMPILE}size)
project(CH569EVT C ASM)
add_executable(app)
set(CMAKE_EXECUTABLE_SUFFIX .elf)
if(NOT EXAM)
message(FATAL_ERROR "EXAM is not set")
endif()
file(GLOB_RECURSE EXAM_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/${EXAM}/User/*.c)
target_sources(app PRIVATE ${EXAM_SOURCES})
target_sources(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/EXAM/SRC/Startup/startup_CH56x.S)
target_sources(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/EXAM/SRC/RVMSIS/core_riscv.c)
target_include_directories(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/EXAM/SRC/RVMSIS)
file(GLOB PERIPH_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/EXAM/SRC/Peripheral/src/*.c)
target_sources(app PRIVATE ${PERIPH_SOURCES})
target_include_directories(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/EXAM/SRC/Peripheral/inc)
target_compile_options(app PRIVATE
-march=rv32imac_zicsr
-mabi=ilp32
-msmall-data-limit=8
-mno-save-restore
-Os
-fmessage-length=0
-fsigned-char
-ffunction-sections
-fdata-sections
)
target_link_options(app PRIVATE
-g
-T ${CMAKE_CURRENT_SOURCE_DIR}/${EXAM}/.ld
-nostartfiles
-Wl,--gc-sections
-Wl,--print-memory-usage
-Wl,-Map,CH569EVT.map
--specs=nano.specs
--specs=nosys.specs
)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/app.bin
COMMAND ${CMAKE_OBJCOPY}
-O binary
${CMAKE_CURRENT_BINARY_DIR}/app.elf
${CMAKE_CURRENT_BINARY_DIR}/app.bin
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/app.elf
)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/app.hex
COMMAND ${CMAKE_OBJCOPY}
-O ihex
${CMAKE_CURRENT_BINARY_DIR}/app.elf
${CMAKE_CURRENT_BINARY_DIR}/app.hex
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/app.elf
)
add_custom_target(
build ALL
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/app.bin
${CMAKE_CURRENT_BINARY_DIR}/app.hex
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment