Ubuntu下使用GCC开发STM32的环境的搭建

注:从ubuntu linux下建立stm32开发环境: GCC安装以及工程Makefile建立转载。


1、 STM 32 GCC 安装

stm32 属于arm cortex-m系列thumb指令集,所以给arm用的arm-none-eabi就可以了,首先是下载,下载其中的gcc-arm-none-eabi-[版本号]-linux.tar.bz2,解压到你知道的目录会产生 gcc-arm-none-eabi的文件夹。

把该编译器添加到你的环境中:

1 sudo gedit  ~/.bashrc

在最后一行添加:

1 export PATH=$PATH:/your_stm_gcc_dir/gcc-arm-none-eabi-4_8-2013q4/bin

因为我之前有添加过树莓派的编译器了,所以实际上是这样的:

1 export PATH=$PATH:/your_pi_gcc_dir/tools-master/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin/:/your_stm_gcc_dir/gcc-arm-none-eabi-4_8-2013q4/bin

两个编译器环境中间用冒号隔开;

注销后测试:

1 arm-none-eabi-gcc -v

可以查看到该编译器的版本,就表示可以了。

2、工程环境的建立

(1)新建个工程文件夹,及其目录

1 mkdir stm_project
2 cd stm_project
3 mkdir libs
4 mkdir src
5 mkdir inc

(2)可以下载官方固件库,也可以下载第三方固件库。

下载好固件库之后,解压在libs目录下。然后就是编译固件库。

需要在工程根目录下新建一个Make.common文件,此文件不管是在编译固件库、还是在编译源文件的时候都会用到。需要根据你的自己的情况修改其中两个变量的值:STMLIB和TypeOfMCU。

1 # include Makefile
2  
3 #This file is included in the general Makefile, the libs Makefile and the src Makefile
4 #Different optimize settings for library and source files can be realized by using arguments
5 #Compiler optimize settings:
6 # -O0 no optimize, reduce compilation time and make debugging produce the expected results (default).
7 # -O1 optimize, reduce code size and execution time, without much increase of compilation time.
8 # -O2 optimize, reduce code execution time compared to ‘O1’, increase of compilation time.
9 # -O3 optimize, turns on all optimizations, further increase of compilation time.
10 # -Os optimize for size, enables all ‘-O2’ optimizations that do not typically increase code size and other code size optimizations.
11 #Recommended optimize settings for release version: -O3
12 #Recommended optimize settings for debug version: -O0
13 #Valid parameters :
14 # OptLIB=0 --> optimize library files using the -O0 setting
15 # OptLIB=1 --> optimize library files using the -O1 setting
16 # OptLIB=2 --> optimize library files using the -O2 setting
17 # OptLIB=3 --> optimize library files using the -O3 setting
18 # OptLIB=s --> optimize library files using the -Os setting
19 # OptSRC=0 --> optimize source files using the -O0 setting
20 # OptSRC=1 --> optimize source files using the -O1 setting
21 # OptSRC=2 --> optimize source files using the -O2 setting
22 # OptSRC=3 --> optimize source files using the -O3 setting
23 # OptSRC=s --> optimize source files using the -Os setting
24 # all --> build all
25 # libs --> build libs only
26 # src --> build src only
27 # clean --> clean project
28 # tshow --> show optimize settings
29 #Example:
30 # make OptLIB=3 OptSRC=0 all tshow
31  
32 TOP=$(shell readlink -f "$(dir $(lastword $(MAKEFILE_LIST)))")
33 PROGRAM=main
34 LIBDIR=$(TOP)/libs
35  
36 #Adust the following line to the library in use
37 #=========add by embbnux 根据你的库不同,调整这个地方的库目录地址====================#
38 STMLIB=$(LIBDIR)/STM32_USB-FS-Device_Lib_V4.0.0/Libraries
39 #=========add by embbnux 根据你的stm32芯片型号容量不同,修改这个地方的TypeOfMCU=======#
40 #Adjust TypeOfMCU in use, see CMSIS file "stm32f10x.h"#STM32F103RBT (128KB FLASH, 20KB RAM) --> STM32F10X_MD#TypeOfMCU=STM32F10X_MD#STM32F103RET (512KB FLASH, 64KB RAM) --> STM32F10X_HD#STM32F103ZET (512KB FLASH, 64KB RAM) --> STM32F10X_HD
41 #============================================================================#
42 TypeOfMCU=STM32F10X_HD
43 #============================================================================#
44 TC=arm-none-eabi
45 CC=$(TC)-gcc
46 LD=$(TC)-ld -v
47 OBJCOPY=$(TC)-objcopy
48 AR=$(TC)-ar
49 GDB=$(TC)-gdb
50 INCLUDE=-I$(TOP)/inc
51 INCLUDE+=-I$(STMLIB)/CMSIS/Include
52 INCLUDE+=-I$(STMLIB)/CMSIS/Device/ST/STM32F10x/Include
53 INCLUDE+=-I$(STMLIB)/CMSIS/Device/ST/STM32F10x/Source/Templates
54 INCLUDE+=-I$(STMLIB)/STM32F10x_StdPeriph_Driver/inc
55 INCLUDE+=-I$(STMLIB)/STM32_USB-FS-Device_Driver/inc
56 COMMONFLAGS=-g -mcpu=cortex-m3 -mthumb
57 COMMONFLAGSlib=$(COMMONFLAGS)
58 #Commands for general Makefile and src Makefile
59 ifeq ($(OptSRC),0)
60     COMMONFLAGS+=-O0
61     InfoTextSrc=src (no optimize, -O0)
62 else ifeq ($(OptSRC),1)
63     COMMONFLAGS+=-O1
64     InfoTextSrc=src (optimize time+ size+, -O1)
65 else ifeq ($(OptSRC),2)
66     COMMONFLAGS+=-O2
67     InfoTextSrc=src (optimize time++ size+, -O2)
68 else ifeq ($(OptSRC),s)
69     COMMONFLAGS+=-Os
70     InfoTextSrc=src (optimize size++, -Os)
71 else
72     COMMONFLAGS+=-O3
73     InfoTextSrc=src (full optimize, -O3)
74 endif
75 CFLAGS+=$(COMMONFLAGS) -Wall -Werror $(INCLUDE)
76 CFLAGS+=-D $(TypeOfMCU)
77 CFLAGS+=-D VECT_TAB_FLASH
78  
79 #Commands for libs Makefile
80 ifeq ($(OptLIB),0)
81     COMMONFLAGSlib+=-O0
82     InfoTextLib=libs (no optimize, -O0)
83 else ifeq ($(OptLIB),1)
84     COMMONFLAGSlib+=-O1
85     InfoTextLib=libs (optimize time+ size+, -O1)
86 else ifeq ($(OptLIB),2)
87     COMMONFLAGSlib+=-O2
88     InfoTextLib=libs (optimize time++ size+, -O2)
89 else ifeq ($(OptLIB),s)
90     COMMONFLAGSlib+=-Os
91     InfoTextLib=libs (optimize size++, -Os)
92 else
93     COMMONFLAGSlib+=-O3
94     InfoTextLib=libs (full optimize, -O3)
95 endif
96 CFLAGSlib+=$(COMMONFLAGSlib) -Wall -Werror $(INCLUDE)
97 CFLAGSlib+=-D $(TypeOfMCU)
98 CFLAGSlib+=-D VECT_TAB_FLASH

然后在libs目录下新建Makefile文件,用于编译固件库。其中牵涉到路径的问题要根据个人的情况进行修改。

1 # libs Makefile
2 include ../Makefile.common
3 LIBS+=libstm32.a
4 CFLAGSlib+=-c
5  
6 all: libs
7  
8 libs: $(LIBS)
9  
10 libstm32.a:
11  @echo -n "Building $@ ..."
12  @cd $(STMLIB)/CMSIS/Device/ST/STM32F10x/Source/Templates && \
13  $(CC) $(CFLAGSlib) \
14  system_stm32f10x.c
15  @cd $(STMLIB)/STM32F10x_StdPeriph_Driver/src && \
16  $(CC) $(CFLAGSlib) \
17  -D"assert_param(expr)=((void)0)" \
18  -I../../CMSIS/Include \
19  -I../../CMSIS/Device/ST/STM32F10x/Include \
20  -I../inc \
21  *.c
22 # @cd $(STMLIB)/STM32_USB-FS-Device_Driver/src && \
23 # $(CC) $(CFLAGSlib) \
24 # -D"assert_param(expr)=((void)0)" \
25 # -I../../CMSIS/Include \
26 # -I../../CMSIS/Device/ST/STM32F10x/Include \
27 # -I../inc \
28 # *.c
29  @$(AR) cr $(LIBDIR)/$@ \
30  $(STMLIB)/CMSIS/Device/ST/STM32F10x/Source/Templates/system_stm32f10x.o \
31  $(STMLIB)/STM32F10x_StdPeriph_Driver/src/*.o \
32 # $(STMLIB)/STM32_USB-FS-Device_Driver/src/*.o
33  @echo "done."
34 .PHONY: libs clean tshow
35  
36 clean:
37  rm -f $(STMLIB)/CMSIS/Device/ST/STM32F10x/Source/Templates/system_stm32f10x.o
38  rm -f $(STMLIB)/STM32F10x_StdPeriph_Driver/src/*.o
39  rm -f $(STMLIB)/STM32_USB-FS-Device_Driver/src/*.o
40  rm -f $(LIBS)
41 tshow:
42  @echo "######################################################################################################"
43  @echo "################# optimize settings: $(InfoTextLib), $(InfoTextSrc)"
44  @echo "######################################################################################################"
编译该库:
1 make clean
2 make

就会在lib目录下生成libstm32.a,这个就是编译好的静态库了。

(3)建立工程编译ld文件

这个ld文件,在编译时告诉编译器把代码放到什么地址,根据芯片的内存以及flash容量不同有所调整。

在工程根目录下新建linker.ld文件

代码较长,请到我的资源里面下载,或者查看参考pdf里面的:http://download.csdn.net/detail/canyue102/6778837

这里说明需要修改的地方,根据芯片型号不同,选择相应的RAM FLASH大小。

1 MEMORY {
2  /*Adust LENGTH to RAMsize of target MCU:*/
3  /*STM32F103RBT --> 20K*/
4  /*RAM (RWX) : ORIGIN = 0x20000000 , LENGTH = 20K*/
5  /*STM32F103RET --> 64K*/
6  /*STM32F103ZET --> 64K*/
7  RAM (RWX) : ORIGIN = 0x20000000 , LENGTH = 64K
8  EXTSRAM (RWX) : ORIGIN = 0x68000000 , LENGTH = 0
9  /*Adust LENGTH to (FLASHsize - FeePROMsize) of target MCU:*/
10  /*STM32F103RBT --> 126K*/
11  FLASH (RX) : ORIGIN = 0x08000000 , LENGTH = 126K
12  /*STM32F103RET --> 508K*/
13  /*FLASH (RX) : ORIGIN = 0x08000000 , LENGTH = 508K*/
14  /*STM32F103ZET --> 508K*/
15  FLASH (RX) : ORIGIN = 0x08000000 , LENGTH = 508K
16  /*Adust ORIGIN to (0x08000000 + (FLASHsize-FeePROMsize)) of target MCU*/
17  /*and adust LENGTH to FeePROMsize allocated:*/
18  /*STM32F103RBT --> 0x08000000+126K, 2K*/
19  EEMUL (RWX) : ORIGIN = 0x08000000+126K, LENGTH = 2K
20  /*STM32F103RET --> 0x08000000+508K, 4K*/
21  /*EEMUL (RWX) : ORIGIN = 0x08000000+508K, LENGTH = 4K*/
22 }
在工程根目录下新建Makefile文件:

# general Makefile
2  
3 include Makefile.common
4 LDFLAGS=$(COMMONFLAGS) -fno-exceptions -ffunction-sections -fdata-sections -L$(LIBDIR) -nostartfiles -Wl,--gc-sections,-Tlinker.ld
5  
6

你可能感兴趣的:(Ubuntu下使用GCC开发STM32的环境的搭建)