Updated CMake scripts.
This commit is contained in:
parent
4ce56e6ee9
commit
2f2505152c
|
@ -7,16 +7,12 @@ project(ch32v307_template)
|
||||||
enable_language(CXX)
|
enable_language(CXX)
|
||||||
enable_language(ASM)
|
enable_language(ASM)
|
||||||
|
|
||||||
# Extra CFlags
|
|
||||||
set(TARGET_CFLAGS_EXTRA "")
|
|
||||||
set(TARGET_CXXFLAGS_EXTRA "")
|
|
||||||
set(TARGET_LDFLAGS_EXTRA "-Wl,--print-memory-usage")
|
|
||||||
|
|
||||||
# Different linker scripts
|
# Different linker scripts
|
||||||
set(TARGET_LDSCRIPT_FLASH "${CMAKE_SOURCE_DIR}/BSP/Ld/Link.ld")
|
set(TARGET_LDSCRIPT_FLASH "${CMAKE_SOURCE_DIR}/BSP/Ld/Link.ld")
|
||||||
|
set(TARGET_LDSCRIPT_RAM "${CMAKE_SOURCE_DIR}/BSP/Ld/Link.ld")
|
||||||
|
|
||||||
# Copy them from Makefile
|
# Copy them from Makefile
|
||||||
set(TARGET_C_SOURCES
|
set(TARGET_SOURCES
|
||||||
"BSP/Core/core_riscv.c"
|
"BSP/Core/core_riscv.c"
|
||||||
"BSP/Debug/debug.c"
|
"BSP/Debug/debug.c"
|
||||||
"BSP/Peripheral/src/ch32v30x_adc.c"
|
"BSP/Peripheral/src/ch32v30x_adc.c"
|
||||||
|
@ -45,6 +41,7 @@ set(TARGET_C_SOURCES
|
||||||
"BSP/Peripheral/src/ch32v30x_tim.c"
|
"BSP/Peripheral/src/ch32v30x_tim.c"
|
||||||
"BSP/Peripheral/src/ch32v30x_usart.c"
|
"BSP/Peripheral/src/ch32v30x_usart.c"
|
||||||
"BSP/Peripheral/src/ch32v30x_wwdg.c"
|
"BSP/Peripheral/src/ch32v30x_wwdg.c"
|
||||||
|
"BSP/Startup/startup_ch32v30x_D8C.S"
|
||||||
"src/board.c"
|
"src/board.c"
|
||||||
"src/ch32v30x_it.c"
|
"src/ch32v30x_it.c"
|
||||||
"src/main.c"
|
"src/main.c"
|
||||||
|
@ -52,11 +49,6 @@ set(TARGET_C_SOURCES
|
||||||
"src/system_ch32v30x.c"
|
"src/system_ch32v30x.c"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Copy them from Makefile
|
|
||||||
set(TARGET_ASM_SOURCES
|
|
||||||
"BSP/Startup/startup_ch32v30x_D8C.S"
|
|
||||||
)
|
|
||||||
|
|
||||||
# Copy them from Makefile
|
# Copy them from Makefile
|
||||||
set(TARGET_C_DEFINES
|
set(TARGET_C_DEFINES
|
||||||
)
|
)
|
||||||
|
@ -71,15 +63,15 @@ set(TARGET_C_INCLUDES
|
||||||
|
|
||||||
# Shared libraries linked with application
|
# Shared libraries linked with application
|
||||||
set(TARGET_LIBS
|
set(TARGET_LIBS
|
||||||
|
"c"
|
||||||
|
"m"
|
||||||
|
"nosys"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Shared library and linker script search paths
|
# Shared library and linker script search paths
|
||||||
set(TARGET_LIB_DIRECTORIES
|
set(TARGET_LIB_DIRECTORIES
|
||||||
)
|
)
|
||||||
|
|
||||||
# Device specific settings, goes to CFLAGS and LDFLAGS
|
|
||||||
set(TARGET_CFLAGS_HARDWARE "-march=rv32imafc -mabi=ilp32f")
|
|
||||||
|
|
||||||
# Conditional flags
|
# Conditional flags
|
||||||
# DEBUG
|
# DEBUG
|
||||||
set(CMAKE_C_FLAGS_DEBUG "-DDEBUG -Og -g")
|
set(CMAKE_C_FLAGS_DEBUG "-DDEBUG -Og -g")
|
||||||
|
@ -93,39 +85,59 @@ set(CMAKE_ASM_FLAGS_RELEASE "-DNDEBUG -O2 -flto")
|
||||||
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "-flto")
|
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "-flto")
|
||||||
|
|
||||||
# Final compiler flags
|
# Final compiler flags
|
||||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${TARGET_CFLAGS_HARDWARE} ${TARGET_CFLAGS_EXTRA} -Wall -fdata-sections -ffunction-sections")
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -fno-common -fno-builtin -ffreestanding -fdata-sections -ffunction-sections")
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TARGET_CFLAGS_HARDWARE} ${TARGET_CXXFLAGS_EXTRA} -Wall -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_ASM_FLAGS} ${CMAKE_C_FLAGS} -x assembler-with-cpp")
|
set(CMAKE_ASM_FLAGS "${CMAKE_C_FLAGS} -x assembler-with-cpp")
|
||||||
set(CMAKE_EXE_LINKER_FLAGS "-specs=nano.specs -specs=nosys.specs -Wl,--gc-sections -nostartfiles -nostdlib ${TARGET_LDFLAGS_EXTRA}")
|
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
|
||||||
|
|
||||||
# Include sub directories here
|
|
||||||
|
|
||||||
# Shared sources, includes and definitions
|
# Shared sources, includes and definitions
|
||||||
add_compile_definitions(${TARGET_C_DEFINES})
|
add_compile_definitions(${TARGET_C_DEFINES})
|
||||||
include_directories(${TARGET_C_INCLUDES})
|
include_directories(${TARGET_C_INCLUDES})
|
||||||
link_directories(${TARGET_LIB_DIRECTORIES})
|
link_directories(${TARGET_LIB_DIRECTORIES})
|
||||||
link_libraries(
|
link_libraries(${TARGET_LIBS})
|
||||||
${TARGET_LIBS}
|
|
||||||
"c"
|
|
||||||
"m"
|
|
||||||
"nosys"
|
|
||||||
)
|
|
||||||
|
|
||||||
# Main targets are added here
|
# Main targets are added here
|
||||||
|
|
||||||
# **** Internal Flash ****
|
# Create ELF
|
||||||
|
add_executable("${CMAKE_PROJECT_NAME}_FLASH.elf" ${TARGET_SOURCES})
|
||||||
|
target_compile_definitions("${CMAKE_PROJECT_NAME}_FLASH.elf"
|
||||||
|
PRIVATE ${TARGET_C_DEFINES_XIP}
|
||||||
|
)
|
||||||
|
target_link_options("${CMAKE_PROJECT_NAME}_FLASH.elf"
|
||||||
|
PRIVATE "-T${TARGET_LDSCRIPT_FLASH}"
|
||||||
|
PRIVATE "-Wl,--Map=${CMAKE_PROJECT_NAME}_FLASH.map"
|
||||||
|
)
|
||||||
|
set_property(TARGET "${CMAKE_PROJECT_NAME}_FLASH.elf" APPEND
|
||||||
|
PROPERTY ADDITIONAL_CLEAN_FILES "${CMAKE_PROJECT_NAME}_FLASH.map"
|
||||||
|
)
|
||||||
|
add_custom_command(OUTPUT "${CMAKE_PROJECT_NAME}_FLASH.hex"
|
||||||
|
COMMAND ${CMAKE_OBJCOPY} "-O" "ihex" "${CMAKE_PROJECT_NAME}_FLASH.elf" "${CMAKE_PROJECT_NAME}_FLASH.hex"
|
||||||
|
DEPENDS "${CMAKE_PROJECT_NAME}_FLASH.elf"
|
||||||
|
)
|
||||||
|
add_custom_target("${CMAKE_PROJECT_NAME}_FLASH_HEX" DEPENDS "${CMAKE_PROJECT_NAME}_FLASH.hex")
|
||||||
|
if(DEFINED TARGET_TOOLCHAIN_SIZE)
|
||||||
|
add_custom_command(TARGET "${CMAKE_PROJECT_NAME}_FLASH.elf" POST_BUILD
|
||||||
|
COMMAND ${TARGET_TOOLCHAIN_SIZE} "${CMAKE_PROJECT_NAME}_FLASH.elf"
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
|
||||||
# Create ELF
|
# Create ELF
|
||||||
add_executable("${CMAKE_PROJECT_NAME}_FLASH.elf" ${TARGET_C_SOURCES} ${TARGET_ASM_SOURCES})
|
add_executable("${CMAKE_PROJECT_NAME}_RAM.elf" ${TARGET_SOURCES})
|
||||||
target_link_options("${CMAKE_PROJECT_NAME}_FLASH.elf"
|
target_link_options("${CMAKE_PROJECT_NAME}_RAM.elf"
|
||||||
PRIVATE "-T${TARGET_LDSCRIPT_FLASH}"
|
PRIVATE "-T${TARGET_LDSCRIPT_RAM}"
|
||||||
PRIVATE "-Wl,-Map=${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}_FLASH.map,--cref"
|
PRIVATE "-Wl,--Map=${CMAKE_PROJECT_NAME}_RAM.map"
|
||||||
)
|
)
|
||||||
|
set_property(TARGET "${CMAKE_PROJECT_NAME}_RAM.elf" APPEND
|
||||||
add_custom_command(OUTPUT "${CMAKE_PROJECT_NAME}_FLASH.hex"
|
PROPERTY ADDITIONAL_CLEAN_FILES "${CMAKE_PROJECT_NAME}_RAM.map"
|
||||||
COMMAND ${CMAKE_OBJCOPY} "-O" "ihex" "${CMAKE_PROJECT_NAME}_FLASH.elf" "${CMAKE_PROJECT_NAME}_FLASH.hex"
|
)
|
||||||
DEPENDS "${CMAKE_PROJECT_NAME}_FLASH.elf"
|
add_custom_command(OUTPUT "${CMAKE_PROJECT_NAME}_RAM.hex"
|
||||||
)
|
COMMAND ${CMAKE_OBJCOPY} "-O" "ihex" "${CMAKE_PROJECT_NAME}_RAM.elf" "${CMAKE_PROJECT_NAME}_RAM.hex"
|
||||||
add_custom_target("${CMAKE_PROJECT_NAME}_FLASH_HEX" DEPENDS "${CMAKE_PROJECT_NAME}_FLASH.hex")
|
DEPENDS "${CMAKE_PROJECT_NAME}_RAM.elf"
|
||||||
|
)
|
||||||
# **** Cortex-M0 does not support SCB->VTOR configuration, so RAM debug is not possible. ****
|
add_custom_target("${CMAKE_PROJECT_NAME}_RAM_HEX" DEPENDS "${CMAKE_PROJECT_NAME}_RAM.hex")
|
||||||
|
if(DEFINED TARGET_TOOLCHAIN_SIZE)
|
||||||
|
add_custom_command(TARGET "${CMAKE_PROJECT_NAME}_RAM.elf" POST_BUILD
|
||||||
|
COMMAND ${TARGET_TOOLCHAIN_SIZE} "${CMAKE_PROJECT_NAME}_RAM.elf"
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|
|
@ -1,4 +1,17 @@
|
||||||
set(CMAKE_C_COMPILER riscv64-elf-gcc)
|
# Poor old Windows...
|
||||||
|
if(WIN32)
|
||||||
|
set(CMAKE_SYSTEM_NAME "Generic")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
set(CMAKE_C_COMPILER riscv64-elf-gcc)
|
||||||
set(CMAKE_CXX_COMPILER riscv64-elf-g++)
|
set(CMAKE_CXX_COMPILER riscv64-elf-g++)
|
||||||
|
|
||||||
|
# Optionally set size binary name, for elf section size reporting.
|
||||||
|
set(TARGET_TOOLCHAIN_SIZE arm-none-eabi-size)
|
||||||
|
|
||||||
|
set(CMAKE_C_FLAGS_INIT "-march=rv32imafc -mabi=ilp32f")
|
||||||
|
set(CMAKE_CXX_FLAGS_INIT "-march=rv32imafc -mabi=ilp32f")
|
||||||
|
set(CMAKE_EXE_LINKER_FLAGS_INIT "-specs=nano.specs -specs=nosys.specs -nostartfiles -nostdlib -Wl,--print-memory-usage -Wl,--no-warn-rwx-segments")
|
||||||
|
|
||||||
# Make CMake happy about those compilers
|
# Make CMake happy about those compilers
|
||||||
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
|
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
|
|
@ -24,7 +24,6 @@ void BOARD_Peripheral_Init(void) {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
void BOARD_SystemClock_Config(void) {
|
void BOARD_SystemClock_Config(void) {
|
||||||
|
|
||||||
// After you are done playing with RCC...
|
// After you are done playing with RCC...
|
||||||
SystemCoreClockUpdate();
|
SystemCoreClockUpdate();
|
||||||
}
|
}
|
|
@ -4,7 +4,6 @@
|
||||||
#define __IRQ_WEAK __attribute__((interrupt(), weak))
|
#define __IRQ_WEAK __attribute__((interrupt(), weak))
|
||||||
#define __IRQ_NAKED __attribute__((naked))
|
#define __IRQ_NAKED __attribute__((naked))
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* FreeRTOS supports both non-vectored and vectored exception model.
|
* FreeRTOS supports both non-vectored and vectored exception model.
|
||||||
* For non-vectored exception, use `freertos_risc_v_trap_handler`,
|
* For non-vectored exception, use `freertos_risc_v_trap_handler`,
|
||||||
|
@ -19,7 +18,7 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
__IRQ_WEAK void Default_Handler(void) {
|
__IRQ_WEAK void Default_Handler(void) {
|
||||||
for(;;) {
|
for (;;) {
|
||||||
/* Where are you from? */
|
/* Where are you from? */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,7 +28,7 @@ __IRQ_WEAK void Default_Handler(void) {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
__IRQ_WEAK void Fault_Handler(void) {
|
__IRQ_WEAK void Fault_Handler(void) {
|
||||||
for(;;) {
|
for (;;) {
|
||||||
/* Emmmmmmmmm? */
|
/* Emmmmmmmmm? */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,7 +38,7 @@ __IRQ_WEAK void Fault_Handler(void) {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
__IRQ_WEAK void Ecall_U_Handler(void) {
|
__IRQ_WEAK void Ecall_U_Handler(void) {
|
||||||
for(;;) {
|
for (;;) {
|
||||||
/* Who called me? */
|
/* Who called me? */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -49,7 +48,7 @@ __IRQ_WEAK void Ecall_U_Handler(void) {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
__IRQ_WEAK void Ecall_M_Handler(void) {
|
__IRQ_WEAK void Ecall_M_Handler(void) {
|
||||||
/* M mode ecall handler */
|
/* M mode ecall handler */
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -2,9 +2,8 @@
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "debug.h"
|
|
||||||
|
|
||||||
#include "board.h"
|
#include "board.h"
|
||||||
|
#include "debug.h"
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
/* Initialize board components. */
|
/* Initialize board components. */
|
||||||
|
@ -12,7 +11,6 @@ int main(void) {
|
||||||
BOARD_IOMUX_Init();
|
BOARD_IOMUX_Init();
|
||||||
BOARD_Peripheral_Init();
|
BOARD_Peripheral_Init();
|
||||||
|
|
||||||
|
|
||||||
/* Initialize UART for libc function calls. */
|
/* Initialize UART for libc function calls. */
|
||||||
USART_Printf_Init(115200);
|
USART_Printf_Init(115200);
|
||||||
Delay_Init(); /* Silly function, will be replaced next time. */
|
Delay_Init(); /* Silly function, will be replaced next time. */
|
||||||
|
@ -21,7 +19,7 @@ int main(void) {
|
||||||
|
|
||||||
uint32_t i = 0;
|
uint32_t i = 0;
|
||||||
|
|
||||||
for(;;) {
|
for (;;) {
|
||||||
Delay_Ms(500);
|
Delay_Ms(500);
|
||||||
printf("Dead loop @%lu\r\n", i++);
|
printf("Dead loop @%lu\r\n", i++);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,13 @@
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
void *_sbrk(ptrdiff_t incr) {
|
void *_sbrk(ptrdiff_t incr) {
|
||||||
extern char _end[];
|
extern char _end[];
|
||||||
extern char _heap_end[];
|
extern char _heap_end[];
|
||||||
static char *curbrk = _end;
|
static char *curbrk = _end;
|
||||||
|
|
||||||
void *ret;
|
void *ret;
|
||||||
|
|
||||||
if ((curbrk + incr < _end) || (curbrk + incr > _heap_end))
|
if ((curbrk + incr < _end) || (curbrk + incr > _heap_end)) return NULL - 1;
|
||||||
return NULL - 1;
|
|
||||||
|
|
||||||
curbrk += incr;
|
curbrk += incr;
|
||||||
ret = curbrk - incr;
|
ret = curbrk - incr;
|
||||||
|
@ -16,21 +15,24 @@ void *_sbrk(ptrdiff_t incr) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int _fstat(int file, struct stat *st) {
|
int _fstat(int file, struct stat *st) {
|
||||||
st->st_mode = S_IFCHR;
|
st->st_mode = S_IFCHR;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int _close(int file) {
|
int _close(int file) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int _isatty(int file) {
|
int _isatty(int file) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
off_t _lseek(int file, off_t pos, int whence) {
|
off_t _lseek(int file, off_t pos, int whence) {
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int _read(int file, char *ptr, int len) {
|
||||||
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user