首頁 > 軟體

Ubuntu下用arm-none-eabi-gcc編譯STM32F10x

2020-06-16 17:55:52

對於Ubuntu 14.04(我的是14.10),官方倉庫裡就有適用的交叉編譯器

apt-get install gcc-arm-none-eabi

對於較低的版本,可以使用https://launchpad.net/gcc-arm-embedded/提供的二進位制壓縮包,

以及https://launchpad.net/~terry.guo/+archive/ubuntu/gcc-arm-embedded提供的軟體源

add-apt-repository ppa:terry.guo/gcc-arm-embedded
apt-get update
apt-get install gcc-arm-none-eabi

以下是專案的makefile,CMSIS中核心支援使用2.00版本,硬體支援使用3.5版本,外設驅動為3.5版

makefile使用find找到所有.c和.s檔案,根據自動依賴進行編譯。編譯出來的檔案,根據stm32_f103ze_gcc.ld生成.bin和.hex檔案

TARGET=stm32
########################################################################
export CC             = arm-none-eabi-gcc
export AS             = arm-none-eabi-as
export LD             = arm-none-eabi-ld
export OBJCOPY            = arm-none-eabi-objcopy

TOP=$(shell pwd)
INC_FLAGS=    -I $(TOP)/lib/CMSIS_200/CM3/CoreSupport/             
        -I $(TOP)/lib/CMSIS_200/CM3/DeviceSupport/ST/STM32F10x     
        -I $(TOP)/lib/STM32F10x_StdPeriph_Driver/inc        
        -I $(TOP)/src

export CFLAGS= -W -Wall -g -mcpu=cortex-m3 -mthumb -D STM32F10X_HD -D USE_STDPERIPH_DRIVER $(INC_FLAGS) 
ASFLAGS= -W -Wall -g -Wall -mcpu=cortex-m3 -mthumb
########################################################################
C_SRC=$(shell find ./ -name '*.c')
C_OBJ=$(C_SRC:%.c=%.o)
C_DEP=$(C_SRC:%.c=%.cdep)

ASM_SRC=$(shell find ./ -name '*.s')
ASM_OBJ=$(ASM_SRC:%.s=%.o)
ASM_DEP=$(ASM_SRC:%.s=%.adep)

########################################################################
.PHONY: all clean
all:$(C_DEP) $(ASM_DEP) $(C_OBJ) $(ASM_OBJ)
    $(LD) $(C_OBJ) $(ASM_OBJ) -T stm32_f103ze_gcc.ld -o $(TARGET).elf 
    
    $(OBJCOPY) $(TARGET).elf  $(TARGET).bin -Obinary 
    $(OBJCOPY) $(TARGET).elf  $(TARGET).hex -Oihex
###################################
%.cdep:%.c
    $(CC) -MM $< > $@ $(CFLAGS)
sinclude $(C_DEP)
$(C_OBJ):%.o:%.c
    $(CC) -c $< -o $@ $(CFLAGS)
####################################
%.adep:%.s
    $(CC) -MM $< > $@ $(ASFLAGS)
sinclude $(ASM_DEP)
$(ASM_OBJ):%.o:%.s
    $(AS) -c $@ -o $@ $(ASFLAGS)
####################################    
clean:
    @for i in $(shell find ./ -name '*.o');do if [ -e $${i} ];then rm $${i};fi;done
    @for i in $(shell find ./ -name '*.cdep');do if [ -e $${i} ];then rm $${i};fi;done
    @for i in $(shell find ./ -name '*.adep');do if [ -e $${i} ];then rm $${i};fi;done
 
stm32_f103ze_gcc.ld的內容
 
_estack = 0x20000400;

MEMORY
{
    FLASH_ON_CHIP   (rx)    : ORIGIN = 0x08000000, LENGTH = 512K
    SRAM_ON_CHIP    (rwx)   : ORIGIN = 0x20000000, LENGTH = 64K
}

SECTIONS
{
    .text : {
        KEEP(*(.isr_vector))
        *(.text*)
        *(.rodata*)
        _etext = .;
    } > FLASH_ON_CHIP

    _sidata = .;

/*    .data : AT(ADDR(.text) + SIZEOF(.text)) {*/
    .data : AT(_sidata) {
        _sdata = .;
        *(vtable)
        *(.data*)
        _edata = .;
    } > SRAM_ON_CHIP

    .bss : {
        _sbss = .;
        *(.bss*)
        *(COMMON)
        _ebss = .;
    } > SRAM_ON_CHIP
}

 向上查詢makefile並執行的shell指令碼,可以用在geany的編譯快捷鍵中

cd $1;while [ ! -e ./Makefile ] ; do cd ..; path=`pwd` ; if [ "$path" = "/" ] ; then break; fi;done;if [ -e ./Makefile ] ;then make $2;fi

IT145.com E-mail:sddin#qq.com