Linux使用STM官方库开发

PC:Ubuntu 18.04.1,开发板:STM32F103C8T6

version

使用交叉编译器:gcc-arm-none-eabi

1
apt-get install gcc-arm-none-eabi

安装完成后输入arm-none-eabi- 然后按tab键看到很多文件 说明安装成功了
详细可以看我另两篇博文

下载stm32固件库
www.st.com/zh 下载,并解压(~/stm32/codes/stm_project)

1
2
root@tiger:~/stm32/codes# ls
en.stsw-stm32054.zip

创建目录
libs目录放stm32固件库,src放用户源码,inc放用户头文件

1
2
root@tiger:~/stm32/codes/stm_project# ls
inc libs src

创建Makefile.common
在主目录下创建Makefile.common文件,这个是通用Makefile文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# include Makefile

#This file is included in the general Makefile, the libs Makefile and the src Makefile
#Different optimize settings for library and source files can be realized by using arguments
#Compiler optimize settings:
# -O0 no optimize, reduce compilation time and make debugging produce the expected results (default).
# -O1 optimize, reduce code size and execution time, without much increase of compilation time.
# -O2 optimize, reduce code execution time compared to ‘O1’, increase of compilation time.
# -O3 optimize, turns on all optimizations, further increase of compilation time.
# -Os optimize for size, enables all ‘-O2’ optimizations that do not typically increase code size and other code size optimizations.
#Recommended optimize settings for release version: -O3
#Recommended optimize settings for debug version: -O0
#Valid parameters :
# OptLIB=0 --> optimize library files using the -O0 setting
# OptLIB=1 --> optimize library files using the -O1 setting
# OptLIB=2 --> optimize library files using the -O2 setting
# OptLIB=3 --> optimize library files using the -O3 setting
# OptLIB=s --> optimize library files using the -Os setting
# OptSRC=0 --> optimize source files using the -O0 setting
# OptSRC=1 --> optimize source files using the -O1 setting
# OptSRC=2 --> optimize source files using the -O2 setting
# OptSRC=3 --> optimize source files using the -O3 setting
# OptSRC=s --> optimize source files using the -Os setting
# all --> build all
# libs --> build libs only
# src --> build src only
# clean --> clean project
# tshow --> show optimize settings
#Example:
# make OptLIB=3 OptSRC=0 all tshow

TOP=$(shell readlink -f "$(dir $(lastword $(MAKEFILE_LIST)))")
PROGRAM=main
LIBDIR=$(TOP)/libs

#Adust the following line to the library in use
#=========add by hch 根据你的库不同,调整这个地方的库目录地址====================#
STMLIB=$(LIBDIR)/STM32F10x_StdPeriph_Lib_V3.5.0/Libraries
#=========add by hch 根据你的stm32芯片型号容量不同,修改这个地方的TypeOfMCU=======#
#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:
#============================================================================#
TypeOfMCU=STM32F10X_MD
#============================================================================#
TC=arm-none-eabi
CC=$(TC)-gcc
LD=$(TC)-ld -v
OBJCOPY=$(TC)-objcopy
AR=$(TC)-ar
GDB=$(TC)-gdb
INCLUDE=-I$(TOP)/inc
INCLUDE+=-I$(STMLIB)/CMSIS/CM3/CoreSupport
INCLUDE+=-I$(STMLIB)/CMSIS/CM3/DeviceSupport/ST/STM32F10x
INCLUDE+=-I$(STMLIB)/STM32F10x_StdPeriph_Driver/inc
COMMONFLAGS=-g -mcpu=cortex-m3 -mthumb
COMMONFLAGSlib=$(COMMONFLAGS)
#Commands for general Makefile and src Makefile
ifeq ($(OptSRC),0)
COMMONFLAGS+=-O0
InfoTextSrc=src (no optimize, -O0)
else ifeq ($(OptSRC),1)
COMMONFLAGS+=-O1
InfoTextSrc=src (optimize time+ size+, -O1)
else ifeq ($(OptSRC),2)
COMMONFLAGS+=-O2
InfoTextSrc=src (optimize time++ size+, -O2)
else ifeq ($(OptSRC),s)
 COMMONFLAGS+=-Os
InfoTextSrc=src (optimize size++, -Os)
else
COMMONFLAGS+=-O3
InfoTextSrc=src (full optimize, -O3)
endif
CFLAGS+=$(COMMONFLAGS) -Wall -Werror $(INCLUDE)
CFLAGS+=-D $(TypeOfMCU)
CFLAGS+=-D VECT_TAB_FLASH

#Commands for libs Makefile
ifeq ($(OptLIB),0)
COMMONFLAGSlib+=-O0
InfoTextLib=libs (no optimize, -O0)
else ifeq ($(OptLIB),1)
COMMONFLAGSlib+=-O1
InfoTextLib=libs (optimize time+ size+, -O1)
else ifeq ($(OptLIB),2)
COMMONFLAGSlib+=-O2
InfoTextLib=libs (optimize time++ size+, -O2)
else ifeq ($(OptLIB),s)
COMMONFLAGSlib+=-Os
InfoTextLib=libs (optimize size++, -Os)
else
COMMONFLAGSlib+=-O3
InfoTextLib=libs (full optimize, -O3)
endif
CFLAGSlib+=$(COMMONFLAGSlib) -Wall -Werror $(INCLUDE)
CFLAGSlib+=-D $(TypeOfMCU)
CFLAGSlib+=-D VECT_TAB_FLASH

编译固件库

1.首先编译固件库,将固件库编译成静态库,应用程序可以直接使用.

2.下载startup.c,路径:stm_project/libs/STM32F10x_StdPeriph_Lib_V3.5.0/Libraries/

CMSIS/CM3/DeviceSupport/ST/STM32F10x/startup/arm/

3.编写Makefile文件,路径:stm_project/libs/STM32F10x_StdPeriph_Lib_V3.5.0/Libraries/

STM32F10x_StdPeriph_Driver/src

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
include ../../../../../Makefile.common

LIBS+=libstm32.a
CFLAGSlib+=-c

all: $(LIBS)

libstm32.a:
@echo -n "Building $@ ..."
cd $(STMLIB)/CMSIS/CM3/DeviceSupport/ST/STM32F10x/ && \
$(CC) $(CFLAGSlib) system_stm32f10x.c
cd $(STMLIB)/CMSIS/CM3/DeviceSupport/ST/STM32F10x/startup/arm && \
$(CC) $(CFLAGSlib) startup.c
cd $(STMLIB)/STM32F10x_StdPeriph_Driver/src && \
$(CC) $(CFLAGSlib) *.c \
-D"assert_param(expr)=((void)0)"
$(AR) cr $(LIBDIR)/$@ \
$(STMLIB)/CMSIS/CM3/DeviceSupport/ST/STM32F10x/system_stm32f10x.o \
$(STMLIB)/CMSIS/CM3/DeviceSupport/ST/STM32F10x/startup/arm/startup.o \
$(STMLIB)/STM32F10x_StdPeriph_Driver/src/*.o
@echo "done."

.PHONY: clean

clean:
rm -f $(STMLIB)/CMSIS/CM3/DeviceSupport/ST/STM32F10x/system_stm32f10x.o
rm -f $(STMLIB)/CMSIS/CM3/DeviceSupport/ST/STM32F10x/startup/arm/startup.o
rm -f $(STMLIB)/STM32F10x_StdPeriph_Driver/src/*.o
rm -f $(LIBDIR)/$(LIBS)

建立ld文件
项目根目录建立linker.ld文件,根据芯片的内存以及flash容量
下面是文件需要修改的地方

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

编译用户文件
将用户应用程序也编译成静态库
~/stm32/codes/stm_project/src/Makefile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
include ../Makefile.common

LIBS+=app.a
CFLAGSlib+=-c

all: $(LIBS)

app.a:
@echo -n "Building $@ ..."
$(CC) $(CFLAGSlib) *.c
$(AR) cr $@ *.o
@echo "done."

.PHONY: clean

clean:
rm -f *.o
rm -f $(LIBS)

建立主Makefile
在主目录下,建立主Makefile文件
将固件库和用户应用程序编译成可执行文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
include Makefile.common
LDFLAGS=$(COMMONFLAGS) -fno-exceptions -ffunction-sections -fdata-sections -L$(LIBDIR) -nostartfiles -Wl,--gc-sections,-Tlinker.ld

LDLIBS+=-lm
LDLIBS+=-lstm32

STARTUP=startup.c

all: libs src
$(CC) -o $(PROGRAM).elf $(LDFLAGS) \
-Wl,--whole-archive \
src/app.a \
-Wl,--no-whole-archive \
$(LDLIBS)
$(OBJCOPY) -O ihex $(PROGRAM).elf $(PROGRAM).hex
$(OBJCOPY) -O binary $(PROGRAM).elf $(PROGRAM).bin
arm-none-eabi-readelf -a $(PROGRAM).elf > $(PROGRAM).info_elf
arm-none-eabi-size -d -B -t $(PROGRAM).elf > $(PROGRAM).info_size
arm-none-eabi-objdump -S $(PROGRAM).elf > $(PROGRAM).info_code
arm-none-eabi-nm -t d -S --size-sort -s $(PROGRAM).elf > $(PROGRAM).info_symbol

.PHONY: libs src clean

libs:
$(MAKE) -C libs/STM32F10x_StdPeriph_Lib_V3.5.0/Libraries/STM32F10x_StdPeriph_Driver/src
src:
$(MAKE) -C src
clean:
rm -f $(PROGRAM).elf $(PROGRAM).hex $(PROGRAM).bin $(PROGRAM).info_elf $(PROGRAM).info_size
rm -f $(PROGRAM).info_code
rm -f $(PROGRAM).info_symbol

根目录执行make生成二进制文件

1
2
3
root@tiger:~/stm32/codes/stm_project# ls
inc linker.ld main.elf main.info_code main.info_size Makefile src
libs main.bin main.hex main.info_elf main.info_symbol Makefile.common

loadbin烧写程序OK,输入r复位,g运行程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
J-Link>loadbin /home/hch/stm32/codes/stm_project/main.bin 0x8000000

**************************
WARNING: T-bit of XPSR is 0 but should be 1. Changed to 1.
**************************

Downloading file [/home/hch/stm32/codes/stm_project/main.bin]...
Comparing flash [100%] Done.
Erasing flash [100%] Done.
Programming flash [100%] Done.
Verifying flash [100%] Done.
J-Link: Flash download: Bank 0 @ 0x08000000: 1 range affected (5120 bytes)
J-Link: Flash download: Total time needed: 0.281s (Prepare: 0.025s, Compare: 0.004s, Erase: 0.111s, Program: 0.135s, Verify: 0.000s, Restore: 0.003s)
O.K.
J-Link>r
Reset delay: 0 ms
Reset type NORMAL: Resets core & peripherals via SYSRESETREQ & VECTRESET bit.
Reset: Halt core after reset via DEMCR.VC_CORERESET.
Reset: Reset device via AIRCR.SYSRESETREQ.
J-Link>g

项目源代码:https://github.com/huchanghui123/stm32_linux.git

------ 本文结束------