BootLoader程序介绍
% E: {4 |+ s" x' V4 CBootLoader其实就是一段启动程序,它在芯片启动的时候首先被执行,它可以用来做一些硬件的初始化,当初始化完成之后跳转到对应的应用程序中去。例如,我们可以将Flash分为两个区,一个是启动程序区(0x0800 0000 - 0x0800 2000)大小为8K Bytes,剩下的为应用程序区(0x0800 2000 - 0x0801 0000)。芯片上电时先运行启动程序,然后跳转到应用程序区执行应用程序。0 ~- q) q2 {' a( X2 g U
- _9 M1 z' [' S1 o `" J0 M
源码实现
6 R" A) \6 k# J0 B基本的BootLoader程序仅实现了代码跳转的功能,因此全部在main.c文件中实现。
2 H, a7 W/ N [. I9 D9 h- K/ R' A
' i' d( o' c, k" t0 E2 ?源文件
. k' F3 v+ f$ D2 a- /*
- |) ~. l( |, P0 o4 q( V6 d" f - ******************************************************************************- \" w/ F2 h& y4 }- \
- File: main.c
" u2 F% h# }8 b. Y( H7 }7 X% d0 H - Info: Generated by Atollic TrueSTUDIO(R) 9.3.0 2020-09-07$ Z; @# n6 Q6 j5 ]; _5 v
7 O9 f4 J7 K5 o. A b- The MIT License (MIT)
I2 B: j `2 L) Q% n - Copyright (c) 2019 STMicroelectronics! p( z. b7 R0 w2 i0 T
- * S, Y$ ?& R. ^9 F7 w
- Permission is hereby granted, free of charge, to any person obtaining a copy. z0 r, j: a/ X1 ^
- of this software and associated documentation files (the "Software"), to deal
9 S5 r9 f! A. c: `2 r - in the Software without restriction, including without limitation the rights
9 s$ a$ Z+ J8 Z$ q/ B - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell( d! j# A" J2 }: N7 U
- copies of the Software, and to permit persons to whom the Software is0 n S) k" t7 m
- furnished to do so, subject to the following conditions:6 W9 Q4 ~& l/ A0 g7 ^4 O
- 2 c0 P) e* o3 ] {) f
- The above copyright notice and this permission notice shall be included in all; x& O8 v2 Y; w6 K
- copies or substantial portions of the Software.$ g# r$ R1 A* h/ S) b: m
- : p& B/ Z& p0 f; {
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/ i7 o: B' g: b) o7 R - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,* }! B" z8 B+ G7 d1 N
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
8 [& [$ W* V5 a4 l% P- J - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER$ |0 \' ^4 Z4 n3 R; ~9 ?/ V Y
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,7 J2 S/ q7 w4 A/ l C! [$ @6 d2 F
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ I& ]& o& U. j1 ~& d - SOFTWARE.
; A. X( T2 C( o" ?8 q - + S- c. ^. s# q, ^6 E9 s
- ******************************************************************************
0 E% @+ Y1 A/ G0 p - */
+ f/ H2 U- C$ w8 F6 Q) G5 M - ' H% X+ Z& p) o+ B. e
- /* Includes */5 ^9 T& I7 ^' E( O3 R4 d5 _1 W- r
- #include "stm32f4xx.h"( F& E. ~$ V( F/ K& p7 ]
- #include "delay.h"
% ]2 d. }( p4 X! h# k# x - /* Private macro */& W$ s N/ j$ j# y
) R5 h! F& r( Q- //用户扇区从第5扇区的起始位置开始启动8 W; ?! ^- G- C6 o- W
- #define FLASH_APP_ADDR 0x08010000! b4 Y( n2 H. k" Q
' H7 W5 k) F8 P- #define MCU_LED GPIOA_OUT(15)* G' f7 s$ b1 R$ M& F" C
- ( e! p0 `! n! w: g& T
- /* Private variables */7 O+ e! f# r z! j
- typedef void (*UserApplication)(void); //定义一个函数类型的参数.5 h# [6 `" G- j! u3 _0 x
- ! H2 q! b& L5 W8 P; F
- UserApplication userApp;
- H8 _9 N) i+ m8 L - / n9 G8 s8 g/ z5 _! i& ^
- /* Private function prototypes */
) @& o' p! \! u/ V1 C0 L - void ConfigLED( );/ C" \, J& g7 E) J
- void SystemBooting(uint8_t times, uint16_t msDelay);* w/ p0 S3 u" N; d- `9 l
- void IAPLoadApp(u32 appxaddr);
9 p+ s" f0 H, z. o* L j" g
9 p2 @* n- @: @( X2 W* l- /* Private functions */
! d! T) ^& m" a0 B5 y
6 a& O# H9 o6 S- 0 K% O1 C! d: B% j5 K
- /**! G6 Q+ C" K2 O* R
- **===========================================================================6 M) M5 d) T- K1 x! F3 G
- **
- E4 n- _1 o+ u E4 ~+ ]6 @ - ** Abstract: main program
, n- _! D. o+ I9 A4 h7 a - **
) O; a1 N" L+ q% a8 y5 C0 r - **===========================================================================- Z/ ?6 S" I# @. ^2 `& ^! ~4 d
- */
3 }2 S' ~0 w3 Z% w0 I9 d/ S - int main(void)
; J2 F$ e, `" P, ]; L' O7 Q% d - { {- N2 k1 r ?6 ]" Z1 I# W
- 9 U. s- t' V$ ^$ k# U0 ]0 w( u
- N& d2 N5 p, ?) w+ ^) }4 e6 \) L- /**
* R% J B& v5 T: B J - * IMPORTANT NOTE!
) n7 ?* B5 P. d! K - * The symbol VECT_TAB_SRAM needs to be defined when building the project
/ v5 O9 t2 E( Z; D$ g5 U - * if code has been located to RAM and interrupts are used.% \" W, N0 t3 P' c- g. v
- * Otherwise the interrupt table located in flash will be used.
! Q* Y7 o6 m; ~- J - * See also the <system_*.c> file and how the SystemInit() function updates- a: @" C& r& M7 i
- * SCB->VTOR register.
% b4 y6 _" K" j% Z/ ? - * E.g. SCB->VTOR = 0x20000000;
3 d. b" v& H4 E" { - */0 F# X3 F* @* L. t( ~5 \/ g
+ b2 e! T! t t H& f+ A! }# }" G- /* TODO - Add your application code here */) i) a2 f( ?. C3 q
# U# w4 j9 {4 W1 }. e, T* T- delay_init();
4 R; z) S1 T) w1 N: T - ConfigLED();) l! `+ E6 M! J7 A! }2 \- @; m
- SystemBooting(5,100);
' P+ _6 C( Y" |
% `; f: A F* M3 E1 c% W% b- i- //从第五扇区起始位置开始加载应用程序
k9 t$ y6 l$ C: g - IAPLoadApp(FLASH_APP_ADDR);0 S! K1 O* O" X) Q; o. V
/ U: v+ V: F" ?) N. \7 ^- while(1);
" b) e2 K! T0 d+ F4 y: [ - }
+ T3 k9 q+ k: k$ b
. ]3 ^0 G8 L. J4 ^3 z* H( k- void ConfigLED( )2 S, B9 J+ F7 ~) ~/ A
- {
) D5 `- u/ B; ^9 H. m - GPIO_InitTypeDef GPIO_InitStructure;
, I7 r! @! M# f( H0 m" P7 f9 _ - RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
! G7 Z$ h4 F5 t& M9 s* U$ m0 P; n - /**
. E, n2 z8 v6 s: N - * System State LED
( _$ O1 q, a( _+ r - */8 ]; u% L4 q& g1 ]9 _: D* m& p
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;% l4 Q8 z, V+ a& H7 v
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;* T1 z6 t1 e9 M$ ]! j
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
: H; F5 S) v& t$ [6 V, n) ^9 o9 @ - GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
# s; V/ N$ m, `$ k/ V. F - GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;( g" a Y% \! ]$ \3 s
" ^- [1 {9 x3 r3 F- e- GPIO_Init(GPIOA, &GPIO_InitStructure);
) f# L& L$ R/ i! W0 Q/ N& a7 g7 u - }6 P+ Z; S* [3 z7 I8 b j4 m' G' e6 U0 l0 L
/ ], t) d9 m6 j+ @" D- void SystemBooting(uint8_t times, uint16_t msDelay)% @5 {+ b0 \ C' u, D
- {2 }7 E. K& z' Y6 T2 G" f K
- for (uint8_t i = 0; i < times; i++)
( f# p# q, h+ G) _1 }( [2 [ - {& {+ s- V2 r- y/ \
- MCU_LED = 0;
5 Y) N% h# f( D/ ? - delay_ms(msDelay);( q9 x( H) B6 x) N! B( U* y# P0 [& P" P
- MCU_LED = 1;, s: J3 {' d5 n$ z5 @
- delay_ms(msDelay);0 `# T) i4 D2 x0 Q" V7 Y9 L
- }; W$ x* f2 ?$ c7 t$ q
- }
9 ` {$ ^) f3 L2 P$ C8 M - ! ?. N6 p9 B) }' S/ @$ H- A! b6 L
- void IAPLoadApp(u32 appxaddr)
" ~- k1 J3 p' ?( q3 Z0 J* o - {
7 F! C3 x. F! W$ l3 ~7 g+ p& R% x - if(((*(vu32*)appxaddr)&0x2FF00000)==0x20000000) //检查栈顶地址是否合法." ]- L( L" E& ], m
- {
5 F- W$ Z6 y3 {* Z7 D - userApp=(UserApplication)*(__IO vu32*)(appxaddr+4); //用户代码区第二个字为程序开始地址(复位地址)3 s6 z6 m M; S) g2 m8 v
- __set_MSP(*(__IO uint32_t*) appxaddr); //初始化APP堆栈指针(用户代码区的第一个字用于存放栈顶地址)
0 w8 `# X ?# A - userApp(); //跳转到APP.! y$ i# ~7 _- K* g X+ U
- }+ |' C% P/ g9 h* D( c
- }
复制代码 7 N! |8 F1 V) H3 ?( y
stm32f4_flash.ld
# a+ ~! z5 \9 X" b5 k设置BootLoader程序在flash中的位置为第1~2扇区,共32K空间。在笔者的项目中,第3~4扇区用来实现EEPROM的软件仿真,用户程序将从第5扇区开始启动。 m0 `2 V5 {6 f' ^: `9 r3 F1 Y
8 B: b7 P( q9 @5 k9 ]
- /* Specify the memory areas */. Q0 O- T; i$ u9 \% B: ^- g4 g( U5 a
- MEMORY: k. `2 H$ Q/ l! f: ~
- {
9 B* Q9 f- w' K - FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 32K3 j% ]. W! d4 M1 P' N3 Z
- RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
2 X4 }. o7 t( I2 \, ^) M, { - MEMORY_B1 (rx) : ORIGIN = 0x60000000, LENGTH = 0K: A1 S. Y# _- F) X
- CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 64K
/ M9 z9 b' h ^# w$ Q" [ - }
复制代码
/ D, G, G9 [6 \# _+ ]6 o+ [使用指南" K$ s7 Y' V$ w! g
用户程序需要从第5扇区的起始位置开始下载,同时需要设置中断向量的偏移地址。
* \9 [# n9 Y. y; n# {$ x" r9 S' Z" @
6 c2 e2 ^* r1 y! K& X设置用户程序下载位置(stm32f4_flash.ld)
5 G L% g. ]3 R, l7 o: W) Z$ b9 L在笔者的项目中,设置BootLoader程序在flash中的位置为第1~2扇区,共32K空间。第3~4扇区用来实现EEPROM的软件仿真,共32K空间。用户程序将从第5扇区开始启动。5 y2 y+ ?9 L9 L& ]$ N
# Y9 Y: o2 H5 Q" |2 x
- /* Memories definition */8 }. u8 ], }- j
- MEMORY: h) C) C3 i6 b g
- {
) ]! c7 A, {+ p) n& M2 ~: j - FLASH (rx) : ORIGIN = 0x8010000, LENGTH = 448K
% }, i7 g% z4 f% S9 c - RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
, b: c) q3 h* e1 f - CCMRAM (xrw) : ORIGIN = 0x10000000, LENGTH = 64K
; Y: Q2 h3 _6 [ K1 c - }
复制代码 + i: }3 c( ^) f5 [$ Z; F) W8 P$ ]! G1 f
修改中断向量表偏移量
& B8 B: `8 S$ u' G+ \ S' U在"system_stm32f4xx.c"文件中,修改宏定义:
5 P5 k0 I+ y% x! S
( E6 t) H7 ~" ^! B8 V- S- /*!< Uncomment the following line if you need to relocate your vector Table in3 ~$ w! d% P/ [/ V
- Internal SRAM. */
3 z) c0 e. o, |: B* V. i - /* #define VECT_TAB_SRAM */4 \7 e+ X2 u: s3 ^4 `" \
- #define VECT_TAB_OFFSET 0x10000 /*!< Vector Table base offset field.9 j9 A M# d9 Q4 Z. ]! [
- This value must be a multiple of 0x200. */
复制代码
% @4 I- {5 ^5 |在void SystemInit(void)中将会根据上述宏定义,设置中断向量表的偏移量:0 f _# Z4 U) {9 G$ c
& k6 B/ k" X! S! j; @3 | _
- /* Configure the Vector Table location add offset address ------------------*/
: }2 q- A" H) ~1 r* D - #ifdef VECT_TAB_SRAM& _! e' k$ S9 [9 O
- SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
0 @! {/ O# A% t+ v' @ - #else4 {& G s% d, F. I0 a* C( Z4 C
- SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
+ a. j0 w; Z; b0 b" \ - #endif
复制代码
! x) C/ z6 |6 }" `
5 A+ L8 n4 ?7 X
6 L1 m3 F3 u8 p3 E( g, E/ | |