STM32原有的MDK工程下移植到GCC環境

移植是以正點原子STMF429工程爲基礎:

目錄

1. 增加 STM32F429IGTx_FLASH.ld  這個文件存放芯片內存信息

2. 增加 Makefile 文件

3. 替換 CORE/startup_stm32f429xx.s 文件  在STM32庫中可以查到,共三個版本 GCC、MDK、IAR

4. 增加 CORE/cmsis_gcc.h 文件 GCC環境

5. 修改 SYSTEM/src/sys.c 文件 GCC環境

6. 修改 MALLOC/malloc.c 內存管理 GCC內存段

7. 修改 HARDWARE/src/ltdc.c 中顯存位置

8. 因GCC優化問題,會出現內存不夠情況,STM32F429IGTx_FLASH.ld 增加堆棧空間,MALLOC/malloc.c減小內部內存管理空間



1. 增加 STM32F429IGTx_FLASH.ld  這個文件存放芯片內存信息

            STM32F429IGTx_FLASH.ld主要存放的是芯片內存的信息,堆棧大小,RAM,Flash大小, MEMORY{ }中存放的內存段,程序中有使用明確內存的地址的地方要在這裏定義。


/* Entry Point */
ENTRY(Reset_Handler)

/* Highest address of the user mode stack */
_estack = 0x20020000;    /* end of RAM */
/* Generate a link error if heap and stack don't fit into RAM */
_Min_Heap_Size = 0x400;      /* required amount of heap  */
_Min_Stack_Size = 0x800; /* required amount of stack */

/*
RAM 	內部RAM
FlASH 	內部FLASH
EXTSRAM	外部SRAM
CCMSRAM	內部CCM
EXTABLE	malloc.c中內存管理用
CCMTABLE	malloc.c中內存管理用
LCDMEM	顯存
*/


/* Specify the memory areas */
MEMORY
{
RAM (xrw)       : ORIGIN = 0x20000000, LENGTH = 128K
FLASH (rx)      : ORIGIN = 0x08040000, LENGTH = 768K
EXTSRAM(xrw)    : ORIGIN = 0XC01F4000, LENGTH = 28912K
CCMSRAM(xrw)    : ORIGIN = 0X10000000, LENGTH = 60K
EXTABLE(xrw)    : ORIGIN = 0xC1E30000, LENGTH = 1807K
CCMTABLE(xrw)   : ORIGIN = 0x1000f000, LENGTH = 4K
LCDMEM(xrw)     : ORIGIN = 0xC0000000, LENGTH = 1500K
}

/* 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


  /* Remove information from the standard libraries */
  /DISCARD/ :
  {
    libc.a ( * )
    libm.a ( * )
    libgcc.a ( * )
  }

  .ARM.attributes 0 : { *(.ARM.attributes) }
/* 可用外部內存 */
  .extsram 0XC01F4000 (NOLOAD):
  {
    . = ALIGN(4);
	*(.exsram)
	*(.exsram*)
	. = ALIGN(4);

  } AT > EXTSRAM
/* 可用CCM內存 */
  .ccmsram 0x10000000 (NOLOAD):
  {
    . = ALIGN(4);
	*(.ccmsram)
	*(.ccmsram*)
	. = ALIGN(4);

  } AT > CCMSRAM
/* 外部內存管理 */  
  .exttable 0xC1E30000 (NOLOAD):
  {
   . = ALIGN(4);
	*(.extable)
	*(.extable*)
   . = ALIGN(4);
  } AT > EXTABLE
/* 內部CCM內存管理 */
  .ccmtable 0x1000f000 (NOLOAD):
  {
   . = ALIGN(4);
	*(.ccmtable)
	*(.ccmtable*)
   . = ALIGN(4);
  } AT > CCMTABLE
/* LCD顯存 */
  .lcdmem 0xC0000000 (NOLOAD):
  {
   . = ALIGN(4);
	*(.lcdmem)
	*(.lcdmem*)
   . = ALIGN(4);
  } AT > LCDMEM
}



2. 增加 Makefile 文件

        Makefile 可以用STM32cubeMX直接生成,其格式也可以參考。這裏放一個用STM32cubeMX生成的

######################################
# target
######################################
TARGET = 12


######################################
# building variables
######################################
# debug build?
DEBUG = 1
# optimization
OPT = -Og


#######################################
# paths
#######################################
# Build path
BUILD_DIR = build

######################################
# source
######################################
# C sources
C_SOURCES =  \
Src/main.c \


# ASM sources
ASM_SOURCES =  \
startup_stm32f439xx.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-m4

# fpu
FPU = -mfpu=fpv4-sp-d16

# float-abi
FLOAT-ABI = -mfloat-abi=hard

# mcu
MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)

# macros for gcc
# AS defines
AS_DEFS = 

# C defines
C_DEFS =  \
-DUSE_HAL_DRIVER \
-DSTM32F439xx


# AS includes
AS_INCLUDES =  \
-I\Inc

# C includes
C_INCLUDES =  \
-IInc \



# compile gcc flags
ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections

CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -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 = STM32F439IGTx_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

# 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)))

$(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)/$(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. 替換 CORE/startup_stm32f429xx.s 文件  在STM32庫中可以查到,共三個版本 GCC、MDK、IAR


4. 增加 CORE/cmsis_gcc.h 文件 GCC環境


5. 修改 SYSTEM/src/sys.c 文件 GCC環境

       文件底部的彙編部分需要修改


//THUMB指令不支持彙編內聯
//採用如下方法實現執行彙編指令WFI  
void WFI_SET (void)
{
	__asm__ __volatile__("WFI");

}

//關閉所有中斷(但是不包括fault和NMI中斷)
void INTX_DISABLE(void)
{
	
	__asm__ __volatile__("CPSID   I");
	__asm__ __volatile__("BX      LR");
  
}
//開啓所有中斷
void INTX_ENABLE(void)
{
	__asm__ __volatile__("CPSIE   I");
	__asm__ __volatile__("BX      LR");
}
//設置棧頂地址
//addr:棧頂地址
void MSR_MSP(u32 addr) 
{
	__asm__ __volatile__("MSR MSP, r0");
	__asm__ __volatile__("BX r14");

}

6. 修改 MALLOC/malloc.c 內存管理 GCC內存段

      malloc.c中含有內存管理,需要和STM32F429IGTx_FLASH.ld關聯起來,修改文件開始爲

//內存池(32字節對齊) __align(32)
u8 mem1base[MEM1_MAX_SIZE];														//內部SRAM內存池
u8 mem2base[MEM2_MAX_SIZE] __attribute__((section(".extsram")));			//外部SRAM內存池
u8 mem3base[MEM3_MAX_SIZE] __attribute__((section(".ccmsram")));					//內部CCM內存池
//內存管理表
u32 mem1mapbase[MEM1_ALLOC_TABLE_SIZE];													//內部SRAM內存池MAP
u32 mem2mapbase[MEM2_ALLOC_TABLE_SIZE]  __attribute__((section(".exttable")));	//外部SRAM內存池MAP
u32 mem3mapbase[MEM3_ALLOC_TABLE_SIZE] 	__attribute__((section(".ccmtable")));	//內部CCM內存池MAP

7. 修改 HARDWARE/src/ltdc.c 中顯存位置

      如果要使用顯示屏,要自己定義顯存位置,方法和 malloc.c 的方法類似。


8. 因GCC優化問題,會出現內存不夠情況,STM32F429IGTx_FLASH.ld 增加堆棧空間,MALLOC/malloc.c減小內部內存管理空間


 

 

有疑問可以留言

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章