本文介绍ThreadX在arm-none-eabi-gcc
编译器下的移植方法。
1、ThreadX介绍和源码获取
threadx的介绍和源码获取请参考之前的博文:ThreadX在mdk(AC5)中的移植。
2、准备工作
本篇主要介绍threadx在corex-m7上的移植,编译器使用arm-none-eabi-gcc。
在移植之前,需要先搭建cortex-m7的gcc工程模板。模板也无需自己从头写,可以借助于stm32的开发工具STM32CubeMX
。如何使用STM32CubeMX不是本文的重点,本文的重点在threadx的移植上面。读者可以自行查阅如何使用STM32CubeMX创建gcc工程。
工程创建后可适当对Makefile文件进行修改,读者可以参考本文的Makefile。
3、ThreadX移植过程
3.1、拷贝ThreadX的源文件
- 1、在自己的工程目录下创建threadx-6.2.1文件夹。
- 2、将ThreadX源码目录中的ports和common目录拷贝到threadx-6.2.1中。
- 3、ports目录下仅保留
ports\cortex_m7\gnu
目录。 - 4、将ports\cortex_m7\gnu\example_build\tx_initialize_low_level.S文件拷贝到ports\cortex_m7\gnu\src中。
拷贝后的ports\cortex_m7\gnu\src:
3.2、Makefile文件修改
- 1、添加threadx-6.2.1/common/src目录和
threadx-6.2.1/ports/cortex_m7/gnu/src目录到SRC_DIR
变量中:
SRC_DIR += \
threadx-6.2.1/common/src \
threadx-6.2.1/ports/cortex_m7/gnu/src
- 2、添加threadx-6.2.1/common/inc目录和
threadx-6.2.1/ports/cortex_m7/gnu/inc目录到C_INCLUDES
变量中:
C_INCLUDES += \
-Ithreadx-6.2.1/common/inc \
-Ithreadx-6.2.1/ports/cortex_m7/gnu/inc
- 3、添加.S文件的编译规则
- 4、修改后的Makefile文件如下:
# ------------------------------------------------
# Generic Makefile (based on gcc)
# ------------------------------------------------
######################################
# target
######################################
TARGET = stm32f7
######################################
# building variables
######################################
# debug build?
DEBUG = 0
# optimization
OPT = -Os
# float printf
FLOAT_PTINT = 0
#######################################
# paths
#######################################
# Build path
BUILD_DIR = build
######################################
# source
######################################
# Sources dir
SRC_DIR := \
stm32f7_libraries/cmsis_m7/DeviceSupport/gcc \
stm32f7_libraries/stm32f7_stdperiph_driver/src \
threadx-6.2.1/common/src \
threadx-6.2.1/ports/cortex_m7/gnu/src \
application/main
# C sources
C_SOURCES := $(foreach dir, $(SRC_DIR), $(wildcard $(dir)/*.c))
# ASM sources
ASM_SOURCES := $(foreach dir, $(SRC_DIR), $(wildcard $(dir)/*.s))
ASM_SOURCES2 := $(foreach dir, $(SRC_DIR), $(wildcard $(dir)/*.S))
#######################################
# binaries
#######################################
PREFIX = arm-none-eabi-
# The gcc compiler bin path can be either defined in make command via GCC_PATH variable (> make GCC_PATH=xxx)
# either it can be added to the PATH environment variable.
ifdef GCC_PATH
CC = $(GCC_PATH)/$(PREFIX)gcc
AS = $(GCC_PATH)/$(PREFIX)gcc -x assembler-with-cpp
CP = $(GCC_PATH)/$(PREFIX)objcopy
SZ = $(GCC_PATH)/$(PREFIX)size
else
CC = $(PREFIX)gcc
AS = $(PREFIX)gcc -x assembler-with-cpp
CP = $(PREFIX)objcopy
SZ = $(PREFIX)size
endif
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S
#######################################
# CFLAGS
#######################################
# cpu
CPU = -mcpu=cortex-m7
# fpu
FPU = -mfpu=fpv5-d16
# float-abi
FLOAT-ABI = -mfloat-abi=soft
# mcu
MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)
# macros for gcc
# AS defines
AS_DEFS =
# C defines
C_DEFS = \
-D__VFP_FP__ \
-D__SOFTFP__ \
-DTX_INCLUDE_USER_DEFINE_FILE
# AS includes
AS_INCLUDES =
# C includes
C_INCLUDES = \
-Istm32f7_libraries/stm32f7_stdperiph_driver/inc \
-Istm32f7_libraries/cmsis_m7/CoreSupport \
-Ithreadx-6.2.1/common/inc \
-Ithreadx-6.2.1/ports/cortex_m7/gnu/inc \
-Iapplication/main
# compile gcc flags
ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
CFLAGS += $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -std=c99 -Wall -fdata-sections -ffunction-sections
ifeq ($(DEBUG), 1)
CFLAGS += -g -gdwarf-2
endif
# Generate dependency information
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"
#######################################
# LDFLAGS
#######################################
# link script
LDSCRIPT = stm32f7_libraries/cmsis_m7/DeviceSupport/gcc/startup_stm32f7_flash.ld
# libraries
LIBS = -lc -lm -lnosys
LIBDIR =
LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections
ifeq ($(FLOAT_PTINT), 1)
LDFLAGS += -Wl,-u_printf_float
endif
# default action: build all
all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin
#######################################
# build the application
#######################################
# list of objects
OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))
# list of ASM program objects
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))
vpath %.s $(sort $(dir $(ASM_SOURCES)))
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES2:.S=.o)))
vpath %.S $(sort $(dir $(ASM_SOURCES2)))
$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
$(AS) -c $(CFLAGS) $< -o $@
$(BUILD_DIR)/%.o: %.S Makefile | $(BUILD_DIR)
$(AS) -c $(CFLAGS) $< -o $@
$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
$(CC) $(OBJECTS) $(LDFLAGS) -o $@
$(SZ) $@
$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
$(HEX) $< $@
$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
$(BIN) $< $@
$(BUILD_DIR):
mkdir $@
#######################################
# clean up
#######################################
clean:
-rm -fR $(BUILD_DIR)
#######################################
# dependencies
#######################################
-include $(wildcard $(BUILD_DIR)/*.d)
# *** EOF ***
3.3、链接脚本修改
-
1、链接脚本中增加
__RAM_segment_used_end__
变量
-
2、修改后的lds文件
/*
******************************************************************************
**
** File : LinkerScript.ld
**
** Author :
**
** Abstract : Linker script for stm32_m7 series
** 1024Kbytes FLASH and 1024Kbytes RAM
**
** Set heap size, stack size and stack location according
** to application requirements.
**
** Set memory bank area and size if external memory is used.
**
** Target : Cortex-M7
**
** Distribution: The file is distributed “as is,” without any warranty
** of any kind.
**
*********************************************************************
*****************************************************************************
*/
/* Entry Point */
ENTRY(Reset_Handler)
/* Highest address of the user mode stack */
_estack = ORIGIN(RAM) + LENGTH(RAM); /* end of RAM */
/* Generate a link error if heap and stack don't fit into RAM */
_Min_Heap_Size = 0xA00; /* required amount of heap */
_Min_Stack_Size = 0x800; /* required amount of stack */
/* Specify the memory areas */
MEMORY
{
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
FLASH (rx) : ORIGIN = 0x80000000, LENGTH = 1024K
}
/* Define output sections */
SECTIONS
{
/* The startup code goes first into FLASH */
.isr_vector :
{
. = ALIGN(4);
KEEP(*(.isr_vector)) /* Startup code */
. = ALIGN(4);
} >FLASH
/* The program code and other data goes into FLASH */
.text :
{
. = ALIGN(4);
*(.text) /* .text sections (code) */
*(.text*) /* .text* sections (code) */
*(.glue_7) /* glue arm to thumb code */
*(.glue_7t) /* glue thumb to arm code */
*(.eh_frame)
KEEP (*(.init))
KEEP (*(.fini))
. = ALIGN(4);
_etext = .; /* define a global symbols at end of code */
} >FLASH
/* Constant data goes into FLASH */
.rodata :
{
. = ALIGN(4);
*(.rodata) /* .rodata sections (constants, strings, etc.) */
*(.rodata*) /* .rodata* sections (constants, strings, etc.) */
. = ALIGN(4);
} >FLASH
.ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH
.ARM : {
__exidx_start = .;
*(.ARM.exidx*)
__exidx_end = .;
} >FLASH
.preinit_array :
{
PROVIDE_HIDDEN (__preinit_array_start = .);
KEEP (*(.preinit_array*))
PROVIDE_HIDDEN (__preinit_array_end = .);
} >FLASH
.init_array :
{
PROVIDE_HIDDEN (__init_array_start = .);
KEEP (*(SORT(.init_array.*)))
KEEP (*(.init_array*))
PROVIDE_HIDDEN (__init_array_end = .);
} >FLASH
.fini_array :
{
PROVIDE_HIDDEN (__fini_array_start = .);
KEEP (*(SORT(.fini_array.*)))
KEEP (*(.fini_array*))
PROVIDE_HIDDEN (__fini_array_end = .);
} >FLASH
/* used by the startup to initialize data */
_sidata = LOADADDR(.data);
/* Initialized data sections goes into RAM, load LMA copy after code */
.data :
{
. = ALIGN(4);
_sdata = .; /* create a global symbol at data start */
*(.data) /* .data sections */
*(.data*) /* .data* sections */
. = ALIGN(4);
_edata = .; /* define a global symbol at data end */
} >RAM AT> FLASH
/* Uninitialized data section */
. = ALIGN(4);
.bss :
{
/* This is used by the startup in order to initialize the .bss secion */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
*(.bss*)
*(COMMON)
. = ALIGN(4);
_ebss = .; /* define a global symbol at bss end */
__bss_end__ = _ebss;
} >RAM
/* User_heap_stack section, used to check that there is enough RAM left */
._user_heap_stack :
{
. = ALIGN(8);
PROVIDE ( end = . );
PROVIDE ( _end = . );
. = . + _Min_Heap_Size;
. = . + _Min_Stack_Size;
. = ALIGN(8);
} >RAM
__RAM_segment_used_end__ = .;
/* Remove information from the standard libraries */
/DISCARD/ :
{
libc.a ( * )
libm.a ( * )
libgcc.a ( * )
}
.ARM.attributes 0 : { *(.ARM.attributes) }
}
3.4、底层初始化文件tx_initialize_low_level.S
修改
路径:threadx-6.2.1\ports\cortex_m7\gnu\src\tx_initialize_low_level.S
-
1、修改中断向量表标号
-
2、修改systick使用的系统时钟
至此,移植工作完成。
4、编写应用程序
- 1、在main.c中编写两个任务,然后在tx_application_define中创建这两个任务:
#include <stdio.h>
#include "tx_api.h"
#include "main.h"
#define THREAD1_PRIO 3
#define THREAD1_STACK_SIZE 1024
static TX_THREAD thread1;
uint8_t thread1_stack[THREAD1_STACK_SIZE];
#define THREAD2_PRIO 2
#define THREAD2_STACK_SIZE 1024
static TX_THREAD thread2;
uint8_t thread2_stack[THREAD2_STACK_SIZE];
void my_thread1_entry(ULONG thread_input)
{
/* Enter into a forever loop. */
while(1)
{
printf("threadx 1 application running...\r\n");
/* Sleep for 1000 tick. */
tx_thread_sleep(1000);
}
}
void my_thread2_entry(ULONG thread_input)
{
/* Enter into a forever loop. */
while(1)
{
printf("threadx 2 application running...\r\n");
/* Sleep for 1000 tick. */
tx_thread_sleep(1000);
}
}
void tx_application_define(void *first_unused_memory)
{
/* Create thread */
tx_thread_create(&thread1, "thread 1", my_thread1_entry, 0, &thread1_stack[0], THREAD1_STACK_SIZE, THREAD1_PRIO, THREAD1_PRIO, TX_NO_TIME_SLICE, TX_AUTO_START);
tx_thread_create(&thread2, "thread 2", my_thread2_entry, 0, &thread2_stack[0], THREAD2_STACK_SIZE, THREAD2_PRIO, THREAD2_PRIO, TX_NO_TIME_SLICE, TX_AUTO_START);
}
- 2、在main()函数中启动threadx内核:
#include <stdio.h>
#include "tx_api.h"
void main(void)
{
SystemClockConfig();
UART_init(USART0, 115200, USART_WordLength_8b, USART_StopBits_1b, USART_Parity_No);
tx_kernel_enter( );
}
至此,应用程序编写完成,可以编译、下载,运行,进行测试了。