前言
- threadx的相关参考资料 论坛资料、微软官网
- 本次使用的开发板为普中科技–麒麟,核心芯片为 stm32f497zgt6
- 开发工具选择的是stm32cubemx(代码生成工具)+clion(代码编写工具)
- 编译构建环境选择的是arm-none-gcc编译
本次项目结构
CMakeList对应的配置
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
cmake_minimum_required(VERSION 3.27)# specify cross-compilers and tools
set(CMAKE_C_COMPILER arm-none-eabi-gcc)
set(CMAKE_CXX_COMPILER arm-none-eabi-g++)
set(CMAKE_ASM_COMPILER arm-none-eabi-gcc)
set(CMAKE_AR arm-none-eabi-ar)
set(CMAKE_OBJCOPY arm-none-eabi-objcopy)
set(CMAKE_OBJDUMP arm-none-eabi-objdump)
set(SIZE arm-none-eabi-size)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)# project settings
project(app_threadx C CXX ASM)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 11)
SET(CMAKE_C_FLAGS_DEBUG "-Og -g -ggdb3" CACHE INTERNAL "c debug compiler flags")
SET(CMAKE_CXX_FLAGS_DEBUG "-Og -g -ggdb3" CACHE INTERNAL "cxx debug compiler flags")
SET(CMAKE_ASM_FLAGS_DEBUG "-g -ggdb3" CACHE INTERNAL "asm debug compiler flags")add_compile_definitions(ARM_MATH_CM4;ARM_MATH_MATRIX_CHECK;ARM_MATH_ROUNDING)
add_compile_options(-mfloat-abi=hard -mfpu=fpv4-sp-d16)
add_link_options(-mfloat-abi=hard -mfpu=fpv4-sp-d16)add_compile_options(-mcpu=cortex-m4 -mthumb -mthumb-interwork)
add_compile_options(-ffunction-sections -fdata-sections -fno-common -fmessage-length=0)add_compile_options($<$<COMPILE_LANGUAGE:ASM>:-x$<SEMICOLON>assembler-with-cpp>)
if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release")message(STATUS "Maximum optimization for speed")add_compile_options(-Ofast)
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo")message(STATUS "Maximum optimization for speed, debug info included")add_compile_options(-Ofast -g)
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel")message(STATUS "Maximum optimization for size")add_compile_options(-Os)
else ()message(STATUS "Minimal optimization, debug info included")add_compile_options(-Og -g)
endif ()
add_definitions(-DDEBUG -DUSE_HAL_DRIVER -DSTM32F407xx)
set(COMMON_DRIVER_DIR ${CMAKE_CURRENT_LIST_DIR}/../Drivers)
list(APPEND HAL_INC ${COMMON_DRIVER_DIR}/STM32F4xx_HAL_Driver/Inc)
list(APPEND HAL_INC ${COMMON_DRIVER_DIR}/CMSIS/Include)
list(APPEND HAL_INC ${COMMON_DRIVER_DIR}/CMSIS/Device/ST/STM32F4xx/Include)
list(APPEND HAL_SRC ${COMMON_DRIVER_DIR}/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal*.c)
list(APPEND HAL_SRC ${COMMON_DRIVER_DIR}/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_sdmmc.c)
file(GLOB_RECURSE HAL_SRC ${HAL_SRC})
#[[------------------------------------------------硬件驱动库-------------------------------------------]]
list(APPEND BSP_DRIVER_INC ${COMMON_DRIVER_DIR}/Bsp/)
list(APPEND BSP_DRIVER_DRC ${COMMON_DRIVER_DIR}/Bsp/*.c)
file(GLOB_RECURSE BSP_DRIVER_DRC ${BSP_DRIVER_DRC})#[[------------------------------------------------THREAD X-------------------------------------------]]
add_definitions(-DTX_ENABLE_FPU_SUPPORT -DTX_ENABLE_STACK_CHECKING -DTX_INCLUDE_USER_DEFINE_FILE)
set(THREAD_X_DIR ${CMAKE_CURRENT_LIST_DIR}/../Component/threadx)
list(APPEND THREAD_X_INC ${THREAD_X_DIR}/common/inc)
list(APPEND THREAD_X_SRC ${THREAD_X_DIR}/common/src/*.c)
list(APPEND THREAD_X_INC ${CMAKE_CURRENT_LIST_DIR}/Middleware/threadx/inc)
list(APPEND THREAD_X_SRC ${CMAKE_CURRENT_LIST_DIR}/Middleware/threadx/src/*.*)
#[[------------------------------------------------THREAD X utility-------------------------------------------]]
add_definitions(-DTX_EXECUTION_PROFILE_ENABLE)
list(APPEND THREAD_X_UTILITY_INC ${THREAD_X_DIR}/utility/execution_profile_kit)
list(APPEND THREAD_X_UTILITY_SRC ${THREAD_X_DIR}/utility/execution_profile_kit/tx_execution_profile.c)
list(APPEND THREAD_X_INC ${THREAD_X_UTILITY_INC})
list(APPEND THREAD_X_SRC ${THREAD_X_UTILITY_SRC})
file(GLOB_RECURSE THREAD_X_SRC ${THREAD_X_SRC})
#[[-------------------------------------------------------------------------------------------]]
set(APP_INC${CMAKE_CURRENT_LIST_DIR}/Core/Inc
)
file(GLOB_RECURSE APP_SRC${CMAKE_CURRENT_LIST_DIR}/Core/Startup/startup_stm32f407zgtx.s${CMAKE_CURRENT_LIST_DIR}/Core/Src/*.c
)set(LINKER_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/Core/Startup/STM32F407ZGTX_FLASH.ld)
#[[-------------------------------------------------------------------------------------------]]
include_directories(${BSP_DRIVER_INC})
list(APPEND SOURCES ${BSP_DRIVER_DRC})include_directories(${THREAD_X_INC})
list(APPEND SOURCES ${THREAD_X_SRC})
include_directories(${APP_INC})
list(APPEND SOURCES ${APP_SRC})
include_directories(${HAL_INC})
list(APPEND SOURCES ${HAL_SRC})# lib# math lib
link_libraries(m)
link_libraries(c)
add_link_options(-Wl,-gc-sections,--print-memory-usage,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map)
add_link_options(-mcpu=cortex-m4 -mthumb -mthumb-interwork)
add_link_options(-T ${LINKER_SCRIPT})
add_executable(${PROJECT_NAME}.elf ${SOURCES} ${LINKER_SCRIPT})set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin)
add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILDCOMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}COMMENT "Building ${HEX_FILE}
Building ${BIN_FILE}")
threadx的使用
- 在应用层面和大多数的rtos操作方式大致相同,一般是创建线程、信号、互斥量;然后进入系统调度。
入口函数
tx_kernel_enter()函数的理解(个人)
- 是一个模板函数框架
- 通过相关的宏定义进行扩展对应的功能
- 对设备驱动部分使用的是_tx_initialize_low_level()–使用的汇编进行编写
- 扩展更加灵活,提供多个节点的宏定义