背景介绍
. |& }5 f8 h y8 k1 f笔者在开发的时候,习惯应用自己的一套函数扩展库与工程模板,因此每次开始新的项目开发时,就会将上一个项目工程直接复制一份,在其基础上进行开发。当新项目与原项目的单片机型号不一样的时候,就涉及到如何将工程中的单片机型号的修改过来的问题。
3 t, j3 J# H* b1 ]
5 i. L2 |6 Q5 o; i" {0 u本文将以工程中MCU型号从STM32F103C8Tx改为STM32F103VCTx为例,说明一下具体的操作步骤。" W4 o7 x( x+ D& c$ J
! h4 b( U! g, v8 B e2 k; F5 t' p操作步骤
7 d. e" p5 P3 ]; j修改工程文件.cproject
! E5 Y. G/ s6 ?0 a6 p$ @4 T: P用文本编辑器(NotePad++)打开.cproject工程文件,采用查找替换的方式,将工程文件中的原单片机型号“STM32F103C8”替换成“STM32F103VC”并保存。.project工程文件中没有需要修改的内容。
+ i% D- ^1 o# i& H- G% B8 ?
, Z$ N7 `( _* t7 ^' q; c! V删除部分工程文件
- D5 _" c" I! u$ _# V7 Z& G删除XXX.elf.launch文件与XXX.map文件,这两个文件默认在工程目录文件夹中,在设置Run/Debug Configurations的时候会自动重新生成。
. N4 M6 e9 K3 x; z1 j6 p9 h
% H h5 S1 ^+ X, o替换启动文件- p# V) U" X5 U Z+ L& _
将原来型号的启动文件(XXX.s文件)替换为startup_stm32f103vctx.s,这个启动文件可以通过在STM32CubeIDE中新建一个MCU型号为STM32F103VCTx的工程并自动生成代码而获得。也可以在源码中同时保留这些文件,而在Makefile中指定调用的文件名。
6 J1 y3 [. r6 S6 M5 P, k6 D0 U# L/ \$ e3 [
替换LinkerScript文件
! x6 O" t7 H; h2 g& x将原来型号的链接文件(XXX.ld文件)替换为STM32F103VCTX_FLASH.ld,这个链接文件可以通过在STM32CubeIDE中新建一个MCU型号为STM32F103VCTx的工程并自动生成代码而获得。也可以在源码中同时保留这些文件,而在Makefile中指定调用的文件名。- k7 l% A- l3 q& ]+ f
* Z! o+ j( L5 H4 K0 z" c8 A' y
当然也可以直接修改原来的链接文件:; K+ d( v9 [3 O7 w/ Y# `! c
* R5 K& Q$ C( ^# U: _- /* Highest address of the user mode stack */
, ?* w+ K' v1 D- T( T1 ]* O - _estack = ORIGIN(RAM) + LENGTH(RAM); /* end of "RAM" Ram type memory */
. d6 c5 L2 g( h/ w
m* ~- B- T* ?- _Min_Heap_Size = 0x200 ; /* required amount of heap */: B ^1 C3 w' W) o6 J; T5 o
- _Min_Stack_Size = 0x400 ; /* required amount of stack */
6 r" l1 u2 T& b4 N - ; w# I; V! W) z2 c0 \+ d; B# Y/ u
- /* Memories definition */
, S; u D# z3 V5 d6 I& j5 R2 ?* s - MEMORY5 t. y$ {3 a7 }- H2 |" p! c. o* L
- {
- Y5 G+ y2 Q" V1 c4 H - RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 48K
# _2 w" @, W" W; }8 b& u1 W - FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 256K7 \; ^% ?0 z) |* s5 N2 w
- }
复制代码 8 X( q+ R8 E% ]( l* w0 i& {
主要修改的地方就在堆栈空间分配与RAM, Flash空间的定义。3 x1 `! m- ~8 L1 g, Y9 L9 K; g
, b3 g5 a6 w! w8 k) H3 o0 V修改Makefile文件
3 v& ?/ }( Y8 q' r, p) Y1 t6 N主要修改涉及到单片机型号、启动文件、链接文件等的内容:
" w6 O1 _- \6 M8 E7 d4 _
5 J( u% Q0 h2 f% q( P1 i- LINK_SCRIPT = "STM32F103VCTX_FLASH.ld"8 D- h. p ]4 \) P3 e
- ! K& V7 {! N1 r4 ?% G" Q) D
- ASSEMBLER_FLAGS =-c -g -O0 -mcpu=cortex-m3 -mthumb \* D5 b! o0 ~+ t+ Y
- -D"STM32F10X_HD" -D"USE_STDPERIPH_DRIVER" -x assembler-with-cpp $(INCLUDE_DIR)9 O' X- o2 w4 |* }$ `& O, Y
- 3 ?% ~8 e" s' P- {6 R. n3 C- v
- COMPILER_FLAGS =-c -g -MMD -mcpu=cortex-m3 -O0 -Wall \0 p3 E4 v% Y8 Y; z1 U0 i# N' X
- -ffunction-sections -fdata-sections -mthumb \, ^$ V$ e7 c. F2 a0 F1 m+ j5 X
- -D"STM32F10X_HD" -D"USE_STDPERIPH_DRIVER" $(INCLUDE_DIR)
复制代码 # f: T% c i) q7 X0 P
启动文件由于属于汇编文件,因此只要替换即可:
9 t3 C; C! ], U3 R% x& Q% r; S7 j% c) n2 r& W% Z, {) ]; m
- SRCSASM := $(wildcard */*/*/*/*/*/*/*/*.s) \
5 V# i4 L0 M) S0 o8 @ - $(wildcard */*/*/*/*/*/*/*.s) \
7 i0 O( ~5 z0 W0 z7 F6 j# _ - $(wildcard */*/*/*/*/*/*.s) \0 R5 B6 q. z Y0 u2 ]- g
- $(wildcard */*/*/*/*/*.s) \
I- n0 \# n) N - $(wildcard */*/*/*/*.s) \! |/ N/ V5 X" t2 [6 C
- $(wildcard */*/*/*.s) \
, N3 ?: @+ G- W - $(wildcard */*/*.s) \
0 }4 v4 z, x, s" O6 v1 y: \2 s - $(wildcard */*.s)
x5 o9 H$ L3 ]9 D9 i
复制代码 " |& C7 q! r/ |9 `6 N
当然,如果存在多个启动文件,可以排除不匹配的型号对应的文件:
; O' O" z$ ?; A+ W9 \% j% O7 |" s/ G& I' T& S0 l" D5 W; p
- #SRCSASM := $(filter-out Source/StdPeriLib/Startup/startup_stm32f103vctx.s, $(SRCSASM))
1 K7 @" v% \/ k# x: b - SRCSASM := $(filter-out Source/StdPeriLib/Startup/startup_stm32f10x_md.s, $(SRCSASM))
复制代码 / `. P* O* G! `$ f+ y1 D
Alwhales库的修改
( A& A# q, z E7 Q) i6 H' i需要修改eeprom.h中关于flash划分作为eeprom的地址范围。7 P4 i/ O n. g# a2 L9 D! g
) K* i& M, B# \/ N. l8 L对于C8T6型号,flash的结束地址为“0x08010000”;
/ K( M; ?# r4 x' o) j; h对于VCT6型号,flash的结束地址为“0x08040000”;
/ [: e9 L: [$ b1 h! p9 t1 p' P* {0 P: ~/ l
- #if defined (STM32F10X_LD) || defined (STM32F10X_MD)
# j: M7 w O* S; B8 P3 s, b - #define PAGE_SIZE (uint16_t)0x400 /* Page size = 1KByte */" _, a* d& L+ _7 z! \2 `' Q2 u8 ~! X
- #define FLASH_END_ADDRESS 0x080100008 w4 f' m B9 o- g% p, V
9 ]4 M2 H( f! z. k8 e( W- #elif defined (STM32F10X_HD) || defined (STM32F10X_CL)9 |, Z, N$ j0 I+ K, E% p
- #define PAGE_SIZE (uint16_t)0x800 /* Page size = 2KByte */, a( h5 v! \) S; r$ Q* Z |6 x
- #define FLASH_END_ADDRESS 0x08040000
5 h/ E- \' H8 z" o - #endif
6 S- v# r! B& u' e% w5 W
$ Z3 s! Y' V) W* s. k- /* EEPROM start address in Flash */
; B. `1 @5 [! G5 h, x$ h \% `: V9 m - #define EEPROM_START_ADDRESS ((uint32_t)(FLASH_END_ADDRESS - 2 * PAGE_SIZE))
复制代码 3 L) K. z( T& ^0 ~% U: M
由于已经设置好了条件编译,只需要在合适的位置(默认在stm32f10x.h中):2 ^) v' \/ H Z! U/ o( m. I' \/ O
& Y( A+ L* @4 Q" X7 F
, N& {5 E- N% i
标准库的修改
3 U3 H. d' r) z9 ~) J4 G5 {在stm32f10x.h文件中,需要根据所使用的MCU型号,选择对应的宏定义:9 K L0 t: e6 b* i: s
; f* F( |$ L5 ]4 k
- /* Uncomment the line below according to the target STM32 device used in your8 i9 k( A/ O- |: H. S q
- application
9 g/ a8 }! ]- u& S5 _+ w/ N - */. a# Q2 \6 y4 t3 B" ]: `$ @
- $ p i8 b% p2 G$ R d2 P
- #if !defined (STM32F10X_LD) && !defined (STM32F10X_LD_VL) && !defined (STM32F10X_MD) && !defined (STM32F10X_MD_VL) && !defined (STM32F10X_HD) && !defined (STM32F10X_HD_VL) && !defined (STM32F10X_XL) && !defined (STM32F10X_CL) 6 ^- ]3 D, U: ?3 |; B( E
- /* #define STM32F10X_LD */ /*!< STM32F10X_LD: STM32 Low density devices */' l6 i9 i% C b, f4 c7 J
- /* #define STM32F10X_LD_VL */ /*!< STM32F10X_LD_VL: STM32 Low density Value Line devices */ 3 `8 j4 x/ S; b/ W z/ F* ^" G) R
- /* #define STM32F10X_MD */ /*!< STM32F10X_MD: STM32 Medium density devices */8 O `/ v1 V( H! H7 U
- /* #define STM32F10X_MD_VL */ /*!< STM32F10X_MD_VL: STM32 Medium density Value Line devices */ - {4 T& H/ I; S5 i6 Q$ F5 L1 W
- #define STM32F10X_HD /*!< STM32F10X_HD: STM32 High density devices */3 M g0 L/ N3 T3 Q
- /* #define STM32F10X_HD_VL */ /*!< STM32F10X_HD_VL: STM32 High density value line devices */
5 B& i' _- p; V - /* #define STM32F10X_XL */ /*!< STM32F10X_XL: STM32 XL-density devices */" y" s% i L ]* Z
- /* #define STM32F10X_CL */ /*!< STM32F10X_CL: STM32 Connectivity line devices */
$ @+ \7 ~; D+ W) {+ d - #endif9 Z+ S. H+ R7 l M
- /* Tip: To avoid modifying this file each time you need to switch between these
% O: l3 A' [: e& ]% F - devices, you can define the device in your toolchain compiler preprocessor.
: J* b Q4 R9 q( J4 M3 c
* y# F$ G% V, P8 T* T, G/ H! E9 d' p- - Low-density devices are STM32F101xx, STM32F102xx and STM32F103xx microcontrollers! x' Z4 t/ b2 n
- where the Flash memory density ranges between 16 and 32 Kbytes.
8 Y+ [5 \, ?+ i; i - - Low-density value line devices are STM32F100xx microcontrollers where the Flash# r/ |+ [% [$ S1 A* O+ [
- memory density ranges between 16 and 32 Kbytes.
6 n. a( o# \2 ~1 k. |4 Q - - Medium-density devices are STM32F101xx, STM32F102xx and STM32F103xx microcontrollers
; P) s% E0 A+ Q A6 V& i5 m6 g4 k C$ t$ x+ p - where the Flash memory density ranges between 64 and 128 Kbytes.
% {- }6 j) b7 v7 ]% t) T. `: F7 Y% h4 M - - Medium-density value line devices are STM32F100xx microcontrollers where the
2 S4 A8 {( C# g# [/ l4 X: _1 p8 ~ - Flash memory density ranges between 64 and 128 Kbytes.
) Y: ^ E% _- `" a1 w2 q4 n; U - - High-density devices are STM32F101xx and STM32F103xx microcontrollers where2 k, d8 X; q, q* {
- the Flash memory density ranges between 256 and 512 Kbytes./ v: [, k& t! u- J) M8 {
- - High-density value line devices are STM32F100xx microcontrollers where the 5 v% Q0 ]" {' Y: Z6 X. y+ a
- Flash memory density ranges between 256 and 512 Kbytes. 4 K. M: F0 m6 \+ o! C) Q# y& X
- - XL-density devices are STM32F101xx and STM32F103xx microcontrollers where
; k* |6 k p6 a - the Flash memory density ranges between 512 and 1024 Kbytes., p( p4 U$ y9 `/ a7 I! J1 L
- - Connectivity line devices are STM32F105xx and STM32F107xx microcontrollers.0 A U' P* T7 w
- */; _ m4 X6 P: p& B$ y* ^, w5 C
# g# s4 [' `+ j& i. P- t- 0 k3 p1 F& S' S9 v7 i
复制代码 ( Q" K1 j4 A8 S# P" w& ^/ z
|