Updated FreeRTOS and CMake files.

This commit is contained in:
Yilin Sun 2023-04-10 23:59:21 +08:00
parent c4d41ac812
commit a32e65f574
Signed by: imi415
GPG Key ID: 17F01E106F9F5E0A
5 changed files with 86 additions and 59 deletions

View File

@ -2,21 +2,17 @@
cmake_minimum_required(VERSION 3.10)
project(ch32v307_freertos)
project(ch32v307_template)
enable_language(CXX)
enable_language(ASM)
# Extra CFlags
set(TARGET_CFLAGS_EXTRA "")
set(TARGET_CXXFLAGS_EXTRA "")
set(TARGET_LDFLAGS_EXTRA "-Wl,--print-memory-usage")
# Different linker scripts
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
set(TARGET_C_SOURCES
set(TARGET_SOURCES
"BSP/Core/core_riscv.c"
"BSP/Debug/debug.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_usart.c"
"BSP/Peripheral/src/ch32v30x_wwdg.c"
"BSP/Startup/startup_ch32v30x_D8C.S"
"src/board.c"
"src/ch32v30x_it.c"
"src/freertos_helper.c"
@ -53,11 +50,6 @@ set(TARGET_C_SOURCES
"src/system_ch32v30x.c"
)
# Copy them from Makefile
set(TARGET_ASM_SOURCES
"BSP/Startup/startup_ch32v30x_D8C.S"
)
# Copy them from Makefile
set(TARGET_C_DEFINES
)
@ -73,20 +65,20 @@ set(TARGET_C_INCLUDES
# Shared libraries linked with application
set(TARGET_LIBS
"freertos_kernel"
"c"
"m"
"nosys"
)
# Shared library and linker script search paths
set(TARGET_LIB_DIRECTORIES
)
# Device specific settings, goes to CFLAGS and LDFLAGS
set(TARGET_CFLAGS_HARDWARE "-march=rv32imafc -mabi=ilp32f")
# Conditional flags
# DEBUG
set(CMAKE_C_FLAGS_DEBUG "-DDEBUG -Og -g")
set(CMAKE_CXX_FLAGS_DEBUG "-DDEBUG -Og -g")
set(CMAKE_ASM_FLAGS_DEBUG "-DDEBUG -Og -g")
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")
@ -95,43 +87,69 @@ 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} ${TARGET_CFLAGS_HARDWARE} ${TARGET_CFLAGS_EXTRA} -Wall -fdata-sections -ffunction-sections")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TARGET_CFLAGS_HARDWARE} ${TARGET_CXXFLAGS_EXTRA} -Wall -fdata-sections -ffunction-sections")
set(CMAKE_ASM_FLAGS "${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_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_CONFIG_FILE_DIRECTORY "${CMAKE_SOURCE_DIR}/include" 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}
"c"
"m"
"nosys"
)
link_libraries(${TARGET_LIBS})
# 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
add_executable("${CMAKE_PROJECT_NAME}_FLASH.elf" ${TARGET_C_SOURCES} ${TARGET_ASM_SOURCES})
target_link_options("${CMAKE_PROJECT_NAME}_FLASH.elf"
PRIVATE "-T${TARGET_LDSCRIPT_FLASH}"
PRIVATE "-Wl,-Map=${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}_FLASH.map,--cref"
)
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")
# **** Cortex-M0 does not support SCB->VTOR configuration, so RAM debug is not possible. ****
add_executable("${CMAKE_PROJECT_NAME}_RAM.elf" ${TARGET_SOURCES})
target_link_options("${CMAKE_PROJECT_NAME}_RAM.elf"
PRIVATE "-T${TARGET_LDSCRIPT_RAM}"
PRIVATE "-Wl,--Map=${CMAKE_PROJECT_NAME}_RAM.map"
)
set_property(TARGET "${CMAKE_PROJECT_NAME}_RAM.elf" APPEND
PROPERTY ADDITIONAL_CLEAN_FILES "${CMAKE_PROJECT_NAME}_RAM.map"
)
add_custom_command(OUTPUT "${CMAKE_PROJECT_NAME}_RAM.hex"
COMMAND ${CMAKE_OBJCOPY} "-O" "ihex" "${CMAKE_PROJECT_NAME}_RAM.elf" "${CMAKE_PROJECT_NAME}_RAM.hex"
DEPENDS "${CMAKE_PROJECT_NAME}_RAM.elf"
)
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 +1 @@
Subproject commit 89cf6815ee9287d2b93dac797a12816f237ec2e8
Subproject commit b27fcf8991355f1ff9a4ed44fe95ab1bce8a0bc0

View File

@ -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++)
# 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
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")

View File

@ -3,7 +3,6 @@
#include <stdio.h>
#include "debug.h"
#include "board.h"
#include "FreeRTOS.h"
@ -28,14 +27,13 @@ int main(void) {
vTaskStartScheduler();
for(;;) {
for (;;) {
/* Never reaches here. */
}
}
void vTaskHello(void *pvParameters) {
for(;;) {
for (;;) {
printf("Hello world? @%lu\r\n", xTaskGetTickCount());
vTaskDelay(pdMS_TO_TICKS(500));
}

View File

@ -4,16 +4,15 @@
#include "task.h"
void *_sbrk(ptrdiff_t incr) {
extern char _end[];
extern char _heap_end[];
extern char _end[];
extern char _heap_end[];
static char *curbrk = _end;
void *ret;
taskENTER_CRITICAL();
if ((curbrk + incr < _end) || (curbrk + incr > _heap_end))
return NULL - 1;
if ((curbrk + incr < _end) || (curbrk + incr > _heap_end)) return NULL - 1;
curbrk += incr;
ret = curbrk - incr;
@ -23,21 +22,20 @@ void *_sbrk(ptrdiff_t incr) {
return ret;
}
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) {
return -1;
return -1;
}
int _isatty(int file) {
return -1;
return -1;
}
off_t _lseek(int file, off_t pos, int whence) {
return -1;
return -1;
}