1.IAR 下载配置
! I( t5 h; U. d$ o0 B本次使用的硬件环境为MUCLEO-L476开发板,官方下载的软件包中打开IAR的工程文件,IAR的下载配置如下:+ l( k2 D8 {) t2 [7 ~5 j
默认配置是使用IAR 系统默认配置文件,我们勾选上Oveerride default .board file文件。
/ I4 w# K, l+ _8 r, G% t9 N从下载配置中看IAR环境下载使用的是flash loader 进行下载,使用的配置文件为 IAR 安装路径下的此文件$TOOLKIT_DIR$\config\flashloader\ST\FlashSTM32L4xxxG.board 官方的帮助文档中对flash loader 的描述如下,从如下描述中可知,flash loader 是运行在目标系统(MUCLEO-L476开发板)的一段实现特定flash 操作的接口函数集合,IAR 通过C-SPY将flash loader 加载到目标系统(MUCLEO-L476开发板)RAM中运行,完成目标falsh 的更新任务。 $ ?& }' `3 J) l* `# S$ `/ A3 b
The flash loader
9 i% Q* z( q- o6 wA flash loader is usually a rather small program which can program one or more flash memories.
5 O- A2 M I3 T. \ r' mThe flash loader consists of a small set of functions, mainly for erasing or writing designated
* t, |& Q, R8 T% w% d5 ]( P$ s, eportions of the flash memory. C-SPY downloads this program into RAM (it must be linked to an: C4 Y4 I1 @4 F) u3 b7 G
address in RAM). To run the program, C-SPY sets the PC to one of the functions in the flash9 j0 {7 u) e+ X( y2 p1 F* X
loader, writes data and directives for that function into a RAM buffer, and starts execution.
% Q8 N# k( @8 L: N K& pWhen the function returns, execution will hit a breakpoint. C-SPY will then know that the) L. b3 t- o K) t* y
function has finished and can proceed to make further
9 [: y' I+ k* \6 D* w# J/ r f从如下图片能更清晰的看出flash loader 的工作流程: ! d5 D5 b. `$ O# x5 M
- 通过 C-SPY 将 flash loader 程序加载到目标系统预留的RAM区域。
- 通过 C-SPY 将需要下载测程序加载到目标系统预留的RAM区域,如果可执行程序比较大预留的RAM空间可能不能一次完全放下可以分割成多次传输。
- 将 IMAGE 镜像通过 flash loader 将 IMAGE 镜像下载至flash.
- 镜像下载完成后可以释放预留的RAM空间,至此已经完成下载。) B& |! M) G1 X% p% c
5 W0 B$ ?# w6 u2. *.board 文件格式说明此*.board 配置文件的总入口,*.board 文件是什么文件,从IAR的帮助文档摘出如下说明,.board 文件是flash loader 下载镜像的配置文件被IAR 的C-SPY debug 对象引用。 , K& p5 M, N8 P0 A
| Ext. | Type of file
$ C, K. y$ O$ k) Z& V: P# _7 d$ B; H | Output from | Input to | | board | Configuration file for flash loader | Text editor | C-SPY |
4 s; X' ^! x, N1 m! K) v
$TOOLKIT_DIR$\config\flashloader\ST\FlashSTM32L4xxxG.board文件内容如下: - <?xml version="1.0" encoding="iso-8859-1"?>
8 Z0 Q5 F# K. j2 [/ { - 1 k% z V6 K, E ~
- <flash_board>
9 r6 V- S7 M7 t0 P5 ]7 ^ - <pass>6 C# @; J! M( c! d1 _5 [
- <loader>$TOOLKIT_DIR$\config\flashloader\ST\FlashSTM32L4xxxG.flash</loader>
+ k8 _, i7 ^% E" _8 E( ? - <range>CODE 0x08000000 0x080FFFFF</range>9 f5 t" [, ~4 h% \
- </pass>) [1 O! `$ e8 b) n
- </flash_board>
复制代码
: ]2 ~( H% Z. s2 {) F从如上xml 的配置指定了,flash 的配置说明及FLASH的范围:
' a- T, Q( o( [9 S0 v5 T3. *.flash 文件格式说明 从配置选项可以看出,此.board主要的配置信息是告诉flash loader 要根据Flash STM32L4xxxG.flash 文件的配置下载镜像,此.flash 的文件是什么文件呢,从iar 的文件说明可知,.flash 文件也是C-SPY加载的文件,主要定义了flash 的相关属性配置。
( `" z$ H/ C' ?" N' Y
| Ext. | Type of file | Output from | Input to | | flash | Configuration file for flash loader | Text editor | C-SPY |
& C; _/ L/ d2 d# g2 y- <?xml version="1.0" encoding="iso-8859-1"?>' E. U1 {* z3 R6 @
- , @/ z7 e6 R$ J2 F% }
- <flash_device>6 d8 k) A8 O/ [! G3 V% [
- <exe>$TOOLKIT_DIR$\config\flashloader\ST\FlashSTM32L4xxxRAM48K_DUALBANK.out</exe>
) m* y5 y0 _6 q, _1 \/ I5 D, [6 ] - <page>8</page>/ H: z7 i+ O9 G% A: g
- <block>512 0x800</block>
+ R% N0 ]. s6 ]5 I - <flash_base>0x08000000</flash_base>8 O% O% V% V! Y
- <macro>$TOOLKIT_DIR$\config\flashloader\ST\FlashSTM32L4xxx.mac</macro>8 w4 b2 ]3 \7 R1 _$ c
- <online>1</online>
- T& y. e E( D( G( R; c% r3 S8 E! w - <aggregate>1</aggregate>
( r3 q( X+ y' G4 h W" C1 [4 C9 ^ - <args_doc>"--skip_erase" -Don't erase blocks that read empty.</args_doc>% E* p+ d9 Y# {
- </flash_device>
复制代码如下是上述 xml 文件的配置项说明: To accommodate a large range of different flash memories, C-SPY uses a few concepts which/ f0 y" B3 {$ p& O$ b
detail the characteristics of flash memories.
u; n3 S& Q C+ w6 r- F$ aPage3 u- e _! l! @' Z2 I) [2 `. z2 s
A page is the smallest writable unit of the flash memory. Many flash
' j0 M9 Y( y- A3 M* @% {; Smemories cannot write less than for example 128 or 256 bytes in a
, K, Q; Z; J& L6 F4 a( M2 s- B4 ?single write operation. C-SPY will never request the flash loader to write
! H {. ?# [: Q0 Y. k! uanything smaller than a page, and uses padding if necessary to fill out a$ z) t' [) n( v
page. Of course, some flash memories have no such restrictions and can4 F' S1 j% P! a; S- L
specify a page size of 1 byte.' s# u4 c& l5 X O, H
Block
% M' ?; f% F! l. l5 [% f; TA block is the smallest erasable unit of the flash memory. For example,1 B- v# ]2 X! i0 q
a flash memory with a 256-byte page size could still require that flash
# C" c# c* A. J( Imemory should be erased in 4-Kbyte chunks. The block size must
6 x4 T4 y8 r0 talways be a multiple of the page size. A flash memory can consist of6 n# m; D6 [/ Z0 I ]7 ^. d. z
several blocks of different sizes. It can also lack such restrictions, in
1 [7 |4 \3 X( c& T& Twhich case the block size would be the same as the page size.
7 ^6 m& Q! I4 y; S: X+ E( j# ?& k* q) ^Base address
5 F3 Y0 P4 y1 J2 h6 PThis is the start address of the flash memory, when it is written. Some% [/ U5 t6 O) u" g9 I
flash memories are simply memory mapped into a fixed address range
( R/ H$ M" [5 J5 [: ?and the base address is then the start of that range. Other flash memories$ H, p$ j! W3 Q7 ~
are mapped into different addresses when being programmed and when
: ], P* K9 C. D$ Fthe application is later executing. The base address is then the address7 `4 V ]3 }1 w+ T5 m. M" Q
where these memories are mapped when being programmed. Yet other
- A6 }' F, Z, }6 X7 j( Mflash memories are not memory mapped at all, but work more like
0 [5 R. e3 g6 T; O7 ?$ Y9 Eexternal disk-like devices. The base address is then simply the preferred) A: }/ e# B& h1 U. r, n
address to be used for the start of the memory when it is being
# o& C$ m, p( V9 b+ p7 f# Sprogrammed.( t2 k& a. I" \
From the C-SPY perspective, a flash memory starts at a given address and consists of a sequence
* k: C3 K2 E! F# s1 gof blocks (possibly of different sizes), each of which consists of a number of pages. The. R' @' k9 N- D4 v7 c
sequence can also contain gaps. * P7 e+ r3 E# ~( I8 P
从上述配置文件解析: STM32L476 的 page 为8byte,共有512 个block 每个 block 大小为2k,总大小为1M,base 地址为0x0800 0000。 . `3 u& W- V: f( x- K$ h# l7 L
FLASH main features
2 k' p2 y5 g! d* q. I, ]/ h0 S• Up to 1 Mbyte of Flash memory with dual bank architecture supporting read-while-write+ A, w) l( `9 n! A
capability (RWW). j3 B* U4 y Z% p5 M
• Memory organization: 2 banks (Bank 1 and Bank 2)$ L! g1 \' @. z. D6 O
– main memory: 512 Kbyte per bank% o! @4 A9 ~; {
– information block: 32 Kbyte per bank, i( A& S( y/ L; U( A
• 72-bit wide data read (64 bits plus 8 ECC bits)
; ?) J& P% R: w3 S• 72-bit wide data write (64 bits plus 8 ECC bits)7 g9 C1 m& `. B0 t# y8 f
• Page erase (2 Kbyte), bank erase and mass erase (both banks) J. z: W3 H( \$ s8 F9 f+ s
从上述描述可以看出flash 的最小读写单位是64bit 8bit ECC,最小的编程单位是8byte,每个最小擦除单位为2kbyte,xml 配置和芯片手册里FLASH 的layout 布局是保持一致的。
0 J' [9 T/ e. L4 Z) B* @
7 ]+ E) D* L! ]3 Q) V4. *.mac 文件格式说明 .mac 文件定义了 C-SPY 调用的宏函数,如下宏函数会在 flash loader 的不同阶段调用宏函数。 ( I5 g6 b" `% R7 M/ W
# `- S. i; P+ a6 U7 K( t| Ext. | Type of file | Output from | Input to | | mac | C-SPY macro definition | Text editor | C-SPY |
macro Specifies the path to a C-SPY macro file, which will be loaded in
( m+ ?$ C2 K( Wconjunction with downloading the flash loader. There are three C-SPY+ Z- Z( i3 W' j
macro functions that will be called automatically if they are defined in3 j7 ]) G9 B" J: t: r
this macro file:/ Q# u, k3 V7 y$ A% d2 h% T# A
execUserFlashInit is called immediately before loading the flash
' ]! M3 h1 p" W# F$ Oloader.
+ g9 d* |. @% Q: D: q, U# O6 jexecUserFlashReset is called immediately after the reset that
: [: I7 ?6 _# ^6 |' J3 R# Hfollows the loading of the flash loader. O, M+ D/ R2 | h4 H
execUserFlashExit is called immediately after flash loading has0 i, h: j2 K3 {( @, w2 A
finished, but before the flash loader is unloaded. 5 g, h& Q8 u4 b8 F' c, c
FlashSTM32L4xxx.mac 内容如下: - __var RCC_CFGR;% W4 \4 z. t; u% T: k2 l
- __var RCC_CR;
' d( D9 i Z; C7 N) Y& j+ Y& b - __var RCC_CIR;2 h, K% k. n- E- E3 q
- __var IWDG_PR;
( M9 p# ^0 {# f! |% C - __var IWDG_RLR;6 }# g9 h0 G" q+ @3 `
- __var FLASH_ACR;. I6 _ d" z8 d+ {( ~0 R- N5 V
" A& {! P: r& s+ t0 D& U- execUserFlashInit()
1 }( B9 \% v# C/ H. T - {
8 s5 I& P& a( o2 I: r% E1 Q2 p - __var tmp;1 x% ?# @9 S7 {
- 3 c7 o- P8 @$ C0 E! L! N
- __message "Entry execUserFlashInit";
, b- ]: s0 g7 Q4 f3 B - 4 `* e" p! i3 V
- //Stop watchdogs when CPU is halted
8 c6 v" k8 p* ^# Q; o) }; j8 Q - __writeMemory32(__readMemory32(0xE0042008, "Memory") | 0x1800, 0xE0042008, "Memory");
1 [) h6 W; ~7 p+ F, Z
" R+ E, A1 C6 M% G5 E6 I4 x- //Check if hardware watchdog is enabled
3 u5 C/ K. w: b7 { S6 e - if(!((1<<16) & __readMemory32(0x40022020,"Memory")))* K4 A* b3 ?4 H6 r
- {
( z' ^3 W. P. R m! U; A - // wait PVU reset/ C0 H, `" o9 J
- while(0x3 & __readMemory32(0x4000300C,"Memory"));
0 l3 a/ s2 I/ C! g: q1 t - IWDG_PR = __readMemory32(0x40003004, "Memory");+ J. y K( w4 p0 |3 g# S# Q, V
- IWDG_RLR = __readMemory32(0x40003008, "Memory");3 }( k; d( f, T6 _: v
- $ k7 e( B/ P% T: d9 E) h: t
- // unlock WDT registers
# o' w5 V. u9 F6 ]# J - __writeMemory32(0x5555,0x40003000,"Memory");& {6 k, r' a7 S7 E- g
- // Prescaler
, B- B0 V8 n% l - __writeMemory32(0x7,0x40003004,"Memory");
& H, M+ p% y* T* k0 M6 t - // Reload
# V$ P; E6 [6 P' h5 D4 Y: b- L0 k - __writeMemory32(0xFFF,0x40003008,"Memory");$ f, c) i. Y* Z3 l% s& K
- // reload WDT
8 V4 J2 Y6 c6 B9 n: l1 ^' o - __writeMemory32(0xAAAA,0x40003000,"Memory");! \/ E7 l6 K; I- N$ h
- }
. @4 V+ M7 g$ C. N# |, _) l6 W$ n - 8 S5 t" ]4 } f9 G
- RCC_CR = __readMemory32(0x40021000, "Memory"); g! M$ f* R! j5 t( i: f; M
- RCC_CFGR = __readMemory32(0x40021008, "Memory");
% G7 w& w( b+ b Q& Q) x, i - RCC_CIR = __readMemory32(0x40021018, "Memory"); Y$ g$ C4 q" t' }0 T7 h% d
-
( y; P) ?4 o f - /*Enable HSI16 oscilator and select it as system clock*/
6 A, e, ^% J" \% F - __writeMemory32(0x00000000, 0x40021018, "Memory"); // RCC_CIR = 0;
4 |* P, T4 i; c$ E - __writeMemory32(RCC_CR | (1<<8), 0x40021000, "Memory"); // RCC_CR_HSION = 1;4 g5 W8 f6 m6 k7 E7 s: G' x, N
- while(!((1<<10) & __readMemory32(0x40021000,"Memory")));; ~1 C6 w! l7 w0 y% r8 U" L" U2 K! h
- tmp = (RCC_CFGR & ~(3<<0)) | (1<<0);) K% \ B! d) m, v
- __writeMemory32(tmp, 0x40021008, "Memory"); // RCC_CFGR_SW = 1;
8 B# D# G3 @3 F% r - / D$ [) Q8 q+ G3 y- x
- FLASH_ACR = __readMemory32(0x40022000, "Memory");
6 `! r4 H0 ]! U8 D+ V9 s5 {, H9 C' ? - tmp = (FLASH_ACR & ~(0x7<<0)) | (2<<0);
* o9 N, |, Z4 \3 u* W - tmp &= ~(3<<9);
' s7 ]' U* s! C% Q$ q9 ` - __writeMemory32(tmp, 0x40022000, "Memory"); // 2 WS (3 CPU cycles) & disable caches3 h* ^* n4 j. g+ `0 ]
- tmp |= (3<<11);! i+ P' l# ^0 ?$ W2 Y$ G' m/ r- e" I
- __writeMemory32(tmp, 0x40022000, "Memory"); // reset caches
5 I: J* p& J6 k6 r* ^+ W r - " A# X1 ~8 o i. C3 ]4 k
- if ((0xFF & __readMemory32(0x40022020,"Memory")) != 0xAA)
# d: _! G" _3 k( o8 R g0 F; C - {# E$ k9 S9 j- @2 q) t
- ; Q* o) A) ~* F, [, | Z
- if (!__messageBoxYesCancel("Do you want to perform mass erase to unlock the device?", "Unlocking device"))
2 P! g: `5 w2 r+ S2 t - {
% B6 W# y! P. |3 m+ B3 X7 |9 s - __abortLaunch("Unlock cancelled. Debug session cannot continue.");
+ D" L! i7 I& w - }
9 Q- I, Y# g& k) Q! Z1 F$ R
4 v$ B9 O+ ^& e. S1 F- __writeMemory32(0x45670123, 0x40022008, "Memory"); // FLASH->FKEYR = FLASH_KEY1;6 C. G. m. }8 F8 S; i
- __writeMemory32(0xCDEF89AB, 0x40022008, "Memory"); // FLASH->FKEYR = FLASH_KEY2;
8 p' N4 z- g' X+ f4 E4 H - // __writeMemory32(0x00000000, 0x40022000, "Memory"); // Flash 0 wait state
. c2 J5 X: K' F - __writeMemory32(0x08192A3B, 0x4002200C, "Memory"); // FLASH->OPTKEYR = FLASH_OPTKEY1;2 A; d2 z4 ^; F1 |4 C# j8 \
- __writeMemory32(0x4C5D6E7F, 0x4002200C, "Memory"); // FLASH->OPTKEYR = FLASH_OPTKEY2; 0 r8 k, V) d+ y
-
5 l" U3 R/ A/ ]5 v$ H9 k" s$ b0 l - __message "Setting FLASH readout protection level 0 (disabled)";6 {8 y) T8 d$ A4 U4 f- Z) ]
- : G5 z# j6 B9 C9 c
- __writeMemory32((__readMemory32(0x40022020,"Memory") & 0xFFFFFF00) | 0xAA, 0x40022020, "Memory"); // Disable readout protection: Y0 J; k; p; \! W
-
% w! Q$ X5 ?4 G9 ]% m" H- v" g$ Z% s - + r0 t2 [: L! S' k! V; D* f
- __writeMemory32((1<<17) | __readMemory32(0x40022014,"Memory"), 0x40022014, "Memory"); // Set the Options Start bit OPTSTRT
) f# I6 A: p( U
# D9 ~4 I2 c3 ~/ Z- while((1<<16) & __readMemory32(0x40022010,"Memory")); // Wait while FLASH busy; b" H5 X9 _+ V& w( g) ?$ x3 n" U
-
; B/ ~0 D. j. r9 K" b - __writeMemory32((1<<27) | __readMemory32(0x40022014,"Memory"), 0x40022014, "Memory"); // Set the OBL_LAUNCH to Force the option byte loading
( n+ J7 B& N) h( A' h$ e3 s - 7 p' g" k1 l, ?, r* I
- }! e, D% M, s% j* H: r: j( n& n
- }' d8 e M1 S* Z4 b7 y7 U
- $ A' ?4 }, [. }' T. h- P* l0 e
- execUserFlashExit()
% n4 \, {: R Y: E+ c - {
G0 C5 F b: v3 S7 @/ X - __message "Entry execUserFlashExit";
; k) x; H. W, P8 d' O/ ?) d - if(!((1<<16) & __readMemory32(0x40022020,"Memory")))
% d- P4 D" E5 `3 m/ |( K - {
0 c$ V9 v" E1 ~" i - __writeMemory32(0x5555,0x40003000,"Memory");! [1 L% P" g: l4 ]! @; Q
- __writeMemory32(IWDG_PR,0x40003004,"Memory");( m% l8 f0 T8 }0 V# z
- __writeMemory32(IWDG_RLR,0x40003008,"Memory");# A$ C+ x% y/ N7 D: x
- __writeMemory32(0xAAAA,0x40003000,"Memory");5 `6 Q5 ?) C- l
- }
$ O6 b. v7 C- o3 B* _2 p - 1 Q& Y; |. g) \
- //Restore registers modified earlier
1 y1 w4 Z& K' [" v - __writeMemory32(RCC_CFGR, 0x40021008, "Memory");
0 m: t. z& \' B4 Z2 ]# M - __writeMemory32(RCC_CR, 0x40021000, "Memory");8 e1 q, x. i1 K6 p* x1 D
- __writeMemory32(RCC_CIR, 0x40021018, "Memory");, v9 m( ^1 C( w% u+ r
) X" G2 q% U+ r) T- __writeMemory32(FLASH_ACR, 0x40022000, "Memory"); 5 c. k$ c. p1 c( P
- }
复制代码 ) M( U* J- _: {8 V( B' F
从上述.mac 文件中定义了如下宏函数,并在入口中添加了打印输出:
" s$ h; |# Y; i& ]. UexecUserFlashInit()
+ z$ R X5 E# A v/ p6 qexecUserFlashExit()
# [- { D( i% U4 K# ^% B+ t( D) s, S' T* C( T
5.下载验证
C, b. s& w7 h/ k" E, Kmacros 文件是可以被C-SPY 调用的执行文件,为了验证个人的猜想在execUserFlashInit 入口和出口追加了log 输出,来确认猜测是否正确。& ?- Y7 O* j9 E9 F
有了上述的配置信息及flash 的初始化处理,flash loader程序就可以吧编译的二进制下载到板子上执行了。准备下环境验证下上述猜测是否正确。
4 f6 n$ m4 w6 N, G; t- j$ z( d+ F
8 G% A" _) o# T$ g
从debug log 窗口可以发现mac 文件内定义的函数入口打印输出跟预期的是一致的,同时从log 个中也可以看出flash loader(FlashSTM32L4xxxRAM48K_DUALBANK.out) 程序是按照上述描述的方式被C-SPY 加载到目标系统。
% b0 G1 F' s. @$ w' g; M- Flash loader example: j+ {7 S3 K* s4 n! b/ X
- The following example shows the source code for a complete flash loader (except the source6 L% g1 y7 R( g( t2 ^" v9 m
- code for the framework), but with a flash programming algorithm which simply copies bytes7 C: z6 E8 \ O5 R6 {1 e/ w
- from the RAM buffer to the destination address:
" A, D8 A' A; D/ e1 x% ` - #include "flash_loader.h"
4 `$ X5 A4 `& g* d - uint32_t FlashInit(void *base_of_flash, uint32_t image_size,+ R0 g. P6 P" g/ c" |8 v7 O( x
- uint32_t link_address, uint32_t flags)
+ X) b' M; r$ ` - {
) J E% I, t: d6 D8 ^& _$ G - return RESULT_OK;4 ]# M5 R: t( k7 z
- }! W. N4 L2 s; W) w
- uint32_t FlashWrite(void *block_start,' Y! E1 G" Z$ | K9 k
- uint32_t offset_into_block,
8 b5 q' G4 x2 ^) j5 ]0 h! P) E& B - uint32_t count,
) {- t1 v2 ]2 V3 }; t - char const *buffer)7 i. b( M; Q7 I9 c7 z2 d0 [
- {
8 I$ j3 m# ?+ s - char *to = (char*)block_start + offset_into_block;
$ i" \8 ?# \" z! e5 f - while (count--)* N2 N$ C$ j6 X+ I1 @& |9 O
- {& [8 H3 O6 k& Z( Q/ A" ~
- *to++ = *buffer++;
' N3 R$ E3 C% \" i- y) ^ - }& _' r! N; }" s+ K1 @8 e7 k) n
- return RESULT_OK;# K G2 E7 r9 w( S% c3 Q
- }
) W! k/ }$ M& T - uint32_t FlashErase(void *block_start, uint32_t block_size)# ] g2 q$ a; h H# ~9 X5 p
- {* H( }: [- G1 F5 l
- char *p = (char*)block_start;& W, C1 `/ [8 e$ _# F; o
- while (block_size--)+ G5 R+ `! B }1 w5 a6 k7 C
- {
+ `+ n4 Z! R! [# S - *p++ = 0; k2 O, c I! s- [# J4 a
- }
1 |4 N% L( p F9 n# S - return RESULT_OK;
, ~$ g. b- ~) q. F. H' m1 \ - }3 F& r9 K w% ~
- The parameters to FlashWrite and FlashErase, in combination with the flash memory base
$ q, J$ i! n4 x9 Z- s - address given in FlashInit, fully specify the addresses of the portions of the flash memory to* p$ ?' R& R' C0 \
- be programmed. Thus, a given flash loader can be used for any number of different flash
( f7 L+ p8 N8 w$ I: U - devices, with different total size, page size, or block layout, provided that they all employ the
* X5 R- a9 y' \+ d- ]/ t - same flash programming algorithm. The flash memory configuration file (.flash) is used for
7 s$ y- n3 P+ r - specifying such variations between flash memories.$ R, _4 j) M; V+ v$ O
- The reference section at the end of this document describes all framework functions in detail.
复制代码 " j( v7 ~1 }6 v9 Q, \0 Q) w
从上述flash loader 的示例程序中实现了FlashInit/FlashWrite/FlashErase 三个api 函数,我们可以objdump 下flash loader 程序看下内部是否是按照上述方式实现了对应的接口。 - I8 T- Y, K5 H
- FlashInit:1 a: d4 w- U/ p. L/ O% Q! x' X
- 0x200000c4: 0xb510 PUSH {R4, LR}
7 Z/ d6 k+ M N9 z, L! } - 0x200000c6: 0x494a LDR.N R1, `.text_8` ; flash_ie
0 v" ~4 X" o$ Q+ V( Q: ~; x' I8 K - 0x200000c8: 0xf500 0x2200 ADD.W R2, R0, #524288 ; 0x80000/ a7 |2 V. j; `# y& ^, L
- 0x200000cc: 0x604a STR R2, [R1, #0x4]: v$ v( z& u% k; v" E
- 0x200000ce: 0x2300 MOVS R3, #06 S# K8 M: S" Z0 @4 H. u7 d1 s
- 0x200000d0: 0x2200 MOVS R2, #0
* }3 G# m, g6 ]+ O0 o/ [ - 0x200000d2: 0x700a STRB R2, [R1]
2 j4 h$ j3 P: H2 H - 0x200000d4: 0x9a02 LDR R2, [SP, #0x8]
7 z& X" a) o5 k# F+ h- Z/ A - 0x200000d6: 0xe000 B.N @200000da! }. G4 L" W2 g/ {. y
- @200000d8:
. n) {3 {7 y' ]( e' @ - 0x200000d8: 0x1c5b ADDS R3, R3, #1& @5 a2 t5 K! q) |" |2 O
- @200000da:/ i/ L) f: U; E) a
- 0x200000da: 0x4293 CMP R3, R21 v& X& e4 I S0 I' z$ h
- 0x200000dc: 0xdbfc BLT.N @200000d8% B1 @0 U. }" \
- 0x200000de: 0x4a45 LDR.N R2, `.text_9` ; 0x40022008 (1073881096)/ q; d5 G! s( e
- 0x200000e0: 0x6993 LDR R3, [R2, #0x18]' i" l( N# n+ t7 o$ A! p
- 0x200000e2: 0x029b LSLS R3, R3, #10: i F" M4 [8 d7 @/ S. n) ^
- 0x200000e4: 0xd507 BPL.N @200000f6
! R; M; N3 i6 N - 0x200000e6: 0x4b44 LDR.N R3, `.text_10` ; 0x1fff75e0 (536835552)
' n# h6 U1 M- u) E0 M. a+ | - 0x200000e8: 0x4c44 LDR.N R4, `.text_11` ; 0x3ffc00 (4193280)! v) h4 O( T3 q3 m2 u5 r
- 0x200000ea: 0x881b LDRH R3, [R3]
) r3 b, U6 K2 G, z" _3 J6 n$ l& |. z - 0x200000ec: 0xea04 0x2383 AND.W R3, R4, R3, LSL #10
& i* O4 _+ x/ \$ c - 0x200000f0: 0xeb00 0x0063 ADD.W R0, R0, R3, ASR #1
! g; [+ ` g3 A: Q - 0x200000f4: 0x6048 STR R0, [R1, #0x4]
, q, w: B+ l# y k0 J- G! L - @200000f6:
3 @1 q" |$ {) n0 ? - 0x200000f6: 0x4842 LDR.N R0, `.text_12` ; 0xe000e100 (-536813312)
/ w% j4 o5 ~* R! |, [, D1 q+ ` - 0x200000f8: 0x4b42 LDR.N R3, `.text_13` ; 0xe000e180 (-536813184)
1 W0 O: n# H' ?4 } - 0x200000fa: 0x6800 LDR R0, [R0]
! B0 X3 V2 F' L7 W1 U7 w - 0x200000fc: 0x06c0 LSLS R0, R0, #27
' i/ j8 w) k+ `9 q, [ p j; I - 0x200000fe: 0xbf44 ITT MI
; v1 a: L) {! Z: O2 v - 0x20000100: 0x2001 MOVMI R0, #1
. L: x" h5 R. y; k O - 0x20000102: 0x7008 STRBMI R0, [R1]9 Q( l; i( D7 _
- 0x20000104: 0x2010 MOVS R0, #16 ; 0x10
+ ]: g4 z) m( Q+ c& X% m, B - 0x20000106: 0x6018 STR R0, [R3]
" I+ `$ b* }! D5 G4 C1 X7 T. w. t - 0x20000108: 0x4b3f LDR.N R3, `.text_14` ; 0xc7000ff8 (-956297224)
6 Y0 s3 U1 w8 ]- o4 L3 G: j4 l+ e - 0x2000010a: 0x68d0 LDR R0, [R2, #0xc]4 f C: N( x# c K. l" x
- 0x2000010c: 0x4018 ANDS R0, R0, R3
3 Z% z; g8 n, x( c$ b* ` - 0x2000010e: 0x6088 STR R0, [R1, #0x8]* ]8 c% Z+ t4 N& i& z
- 0x20000110: 0x483e LDR.N R0, `.text_15` ; 0x45670123 (1164378403)
3 U- x. f. K. ]1 G; u) I - 0x20000112: 0x6010 STR R0, [R2]8 }5 `7 ^4 v9 u# y6 J" o0 |
- 0x20000114: 0x483e LDR.N R0, `.text_16` ; 0xcdef89ab (-839939669)
( T7 i/ L' R$ G! d - 0x20000116: 0x6010 STR R0, [R2]# C9 S2 [ e7 j9 p
- 0x20000118: 0x68d0 LDR R0, [R2, #0xc]
' ]+ m/ J5 q: V) H! D8 V# h - 0x2000011a: 0xf020 0x60c0 BIC.W R0, R0, #100663296 ; 0x60000002 t X, M4 z- O$ p" ^- u" a" t; F
- 0x2000011e: 0x60d0 STR R0, [R2, #0xc]" H# Q- \7 d$ m4 e
- 0x20000120: 0x68d0 LDR R0, [R2, #0xc]
: A9 t' G! \1 _; x) U0 ] - 0x20000122: 0xf040 0x7080 ORR.W R0, R0, #16777216 ; 0x10000000 L z: y. d; A R. G
- 0x20000126: 0x60d0 STR R0, [R2, #0xc]. V: N( [; f q5 M' c3 L V
- @20000128:
9 R- A/ b& `: i' K - 0x20000128: 0x6890 LDR R0, [R2, #0x8]$ q" S* Q& e8 c4 O* Z# X
- 0x2000012a: 0x03c0 LSLS R0, R0, #15
; B8 n. t' A+ M+ D. O - 0x2000012c: 0xd4fc BMI.N @20000128
3 z% G4 s' r$ l5 C; q# v9 i - 0x2000012e: 0x2000 MOVS R0, #0
6 H$ s4 {: F" A - 0x20000130: 0xbd10 POP {R4, PC}
' Q$ R4 |! F: r9 N/ L - `.text_5`:6 f- s& ?' q# U$ L ?
- FlashWrite:
/ d3 z0 }5 ~+ b: J% ^/ A9 ^3 r - 0x20000132: 0xb5f0 PUSH {R4-R7, LR}0 E# Y1 y% D' L% r- ~/ g
- 0x20000134: 0xf240 0x15ff MOVW R5, #511 ; 0x1ff
/ o# _+ g% N- e2 x$ I( J& { P, K1 Z - 0x20000138: 0x1844 ADDS R4, R0, R1
! ?5 C: P2 F5 J" b+ L; D: }! w; {5 H - 0x2000013a: 0x4936 LDR.N R1, `.text_17` ; 0x40022010 (1073881104)
. q7 x" V8 H' U+ i8 P9 M - 0x2000013c: 0x600d STR R5, [R1]- V/ R4 q a1 v/ r' {
- 0x2000013e: 0x2000 MOVS R0, #07 A5 O9 m# b8 g# y# W6 P
- 0x20000140: 0x684d LDR R5, [R1, #0x4]
. G# f, ~6 g1 b) w( n, f - 0x20000142: 0xf045 0x0501 ORR.W R5, R5, #13 Y- w! o2 ?. k7 W3 Q
- 0x20000146: 0x604d STR R5, [R1, #0x4]
% t4 d% t; T1 s& v! G - 0x20000148: 0xe000 B.N @2000014c
9 f3 H7 Q" T2 D6 V3 b) r0 I/ z - @2000014a:
% f n. [( f }8 d# Z2 E* ?. } - 0x2000014a: 0x3a08 SUBS R2, R2, #8- T8 V' f- x5 i5 s2 P
- @2000014c:0 q" k; e t& k9 z8 B$ w
- 0x2000014c: 0xb162 CBZ R2, @20000168
! H0 C+ r) }% ?( Z, O - 0x2000014e: 0xe8f3 0x6702 LDRD R6, R7, [R3], #0x8
6 c, p6 H0 v- N2 W# v( z9 C; ]1 x9 y( K - 0x20000152: 0xe8e4 0x6702 STRD R6, R7, [R4], #0x8
( h, Q2 @) \& @7 X) V" n1 j+ f, k6 }5 P - 0x20000156: 0xbf00 NOP0 ^. c& q/ n& O5 n$ D
- 0x20000158: 0xbf00 NOP
2 |* e" v4 E! B! Y2 g( Y4 r0 e - @2000015a:
: @2 |) v# h. I' a$ ~ - 0x2000015a: 0x680d LDR R5, [R1]
' P4 U! D! T4 c; A8 X - 0x2000015c: 0x03ed LSLS R5, R5, #15
" ~# F2 n$ j: v - 0x2000015e: 0xd4fc BMI.N @2000015a
. x. V8 p9 M: _3 g- w/ Q* O - 0x20000160: 0x680d LDR R5, [R1]
% a G9 c; V% Q5 R - 0x20000162: 0x07ed LSLS R5, R5, #31
2 E7 H ?: M; ]+ U# s - 0x20000164: 0xd4f1 BMI.N @2000014a8 }) B% @1 D" C) @; L$ ^& v( f3 s
- 0x20000166: 0x2001 MOVS R0, #1
' a& j. Y6 u8 y' H0 ^ - @20000168:
$ Z# n! Y) D8 k6 M6 C$ } - 0x20000168: 0x684a LDR R2, [R1, #0x4]
0 ^: j( b( f8 b* E - 0x2000016a: 0x0852 LSRS R2, R2, #12 _5 K* y: V4 r" t. g
- 0x2000016c: 0x0052 LSLS R2, R2, #1; X* u- O& I6 I& n
- 0x2000016e: 0x604a STR R2, [R1, #0x4]
% c: Y: h- e) @- G- c: L - 0x20000170: 0xbdf0 POP {R4-R7, PC}
1 W) t* l( o* Q/ l! H7 \: I: E+ A - `.text_6`: o, h M# f' }. M6 n* c% [ L
- FlashErase: X* Z& e0 ^0 D3 C; G3 X
- 0x20000172: 0x4a28 LDR.N R2, `.text_17` ; 0x40022010 (1073881104)' h$ C5 z* o3 M7 R' Q2 M
- 0x20000174: 0xf240 0x13ff MOVW R3, #511 ; 0x1ff8 o+ R! ? U8 _- C% s A
- 0x20000178: 0x6013 STR R3, [R2]6 I( s4 o) ^. x& u
- 0x2000017a: 0x4601 MOV R1, R0
. T! d0 o- ?, _) r3 Z - 0x2000017c: 0x6853 LDR R3, [R2, #0x4]0 k' p+ f; P, _9 z, ~5 v/ e
- 0x2000017e: 0xf36f 0x03cb BFC R3, #3, #9
. W. k1 E' M( ` S6 ], G( {- i9 P - 0x20000182: 0x6053 STR R3, [R2, #0x4]
7 T* c2 g$ }. m7 D; ~5 a6 n - 0x20000184: 0x2000 MOVS R0, #0
& p3 w% {1 x) s% t% I/ } - 0x20000186: 0x4b1a LDR.N R3, `.text_8` ; flash_ie3 P+ U. m* A* u1 m
- 0x20000188: 0x685b LDR R3, [R3, #0x4]
7 _% b- R8 U8 Y. Y+ l( t - 0x2000018a: 0x4299 CMP R1, R3
3 m! z1 e3 P! i; W0 u - 0x2000018c: 0xd203 BCS.N @20000196$ P5 c$ z: X) P1 |
- 0x2000018e: 0x0a09 LSRS R1, R1, #8
' v: h' [; K' Y - 0x20000190: 0xf401 0x61ff AND.W R1, R1, #2040 ; 0x7f8
A( F/ ]; z, ^- h+ t7 v; V - 0x20000194: 0xe007 B.N @200001a6: q$ c* P! r8 W# J8 e
- @20000196:
: P& }+ C8 R0 D. ]7 V3 N - 0x20000196: 0x1ac9 SUBS R1, R1, R3) I/ r& M7 o( B
- 0x20000198: 0x6853 LDR R3, [R2, #0x4]
. B1 }/ w' G$ v/ {/ x8 @ - 0x2000019a: 0x0a09 LSRS R1, R1, #8
5 X: ^! D4 Y0 W1 A5 {2 @& l1 W - 0x2000019c: 0xf443 0x6300 ORR.W R3, R3, #2048 ; 0x800$ q" R- u; }" K# B4 r- q c
- 0x200001a0: 0xf401 0x61ff AND.W R1, R1, #2040 ; 0x7f8
{5 I4 M( g6 n) @0 |8 e - 0x200001a4: 0x6053 STR R3, [R2, #0x4]
% E4 N9 L. K/ v - @200001a6:2 W0 N$ |8 M- _+ y
- 0x200001a6: 0xf041 0x0102 ORR.W R1, R1, #2' w! [% C: z% k* v% d
- 0x200001aa: 0x6853 LDR R3, [R2, #0x4]) v" l5 P' `% _* }
- 0x200001ac: 0x4319 ORRS R1, R1, R3- {1 w7 i: D4 S4 G& K7 P
- 0x200001ae: 0x6051 STR R1, [R2, #0x4]. X6 f0 S0 _# X' T3 J/ X3 {# ^
- 0x200001b0: 0x6851 LDR R1, [R2, #0x4]; W% r- i6 j3 g# y# H
- 0x200001b2: 0xf441 0x3180 ORR.W R1, R1, #65536 ; 0x10000
* }' S3 I$ @! y: C - 0x200001b6: 0x6051 STR R1, [R2, #0x4]3 w: o/ ]* H! i2 G/ g* L0 \( j7 m
- 0x200001b8: 0xbf00 NOP2 v/ \$ y# A, `6 S% n
- 0x200001ba: 0xbf00 NOP
4 e2 s" k+ E1 { - @200001bc:
- {% u( W; _9 N7 F4 [% a( c2 D1 S - 0x200001bc: 0x6811 LDR R1, [R2]6 y3 ^3 W6 i( c* s# R
- 0x200001be: 0x03c9 LSLS R1, R1, #15+ W# M+ G/ g# m: y1 t/ v$ E t
- 0x200001c0: 0xd4fc BMI.N @200001bc
& t2 `6 j( |0 ?0 u7 i3 r - 0x200001c2: 0x6811 LDR R1, [R2]( }. _+ H% }" Y/ V7 r1 A
- 0x200001c4: 0x07c9 LSLS R1, R1, #31' w9 H# v) i) V. M. R/ }1 j
- 0x200001c6: 0x6851 LDR R1, [R2, #0x4]
w2 o6 h4 e0 x - 0x200001c8: 0xf021 0x0102 BIC.W R1, R1, #2) D8 D, `( J- @5 w8 w5 O9 z ]
- 0x200001cc: 0xbf58 IT PL, f# f) E% a0 Z! p( h( H
- 0x200001ce: 0x2001 MOVPL R0, #1
. s7 c% Q3 X* R; X0 @5 X2 s - 0x200001d0: 0x6051 STR R1, [R2, #0x4]
; u* `/ x: s2 C4 S - 0x200001d2: 0x4770 BX LR
复制代码 & c# z6 g6 n( F5 C7 g4 z$ c
上述通过 ielfdumparm.exe FlashSTM32L4xxxRAM48K_DUALBANK.out -o FlashSTM32L4xxxRAM48K_DUALBANK.ASM --code 命令dump 出来的flash loader 程序,跟预期的保持一致实现了FlashInit/FlashWrite/FlashErase 这组API函数,而且通过反汇编的函数地址信息也可以看出对应的函数的link 地址并不是falsh 区域而是内部RAM 的地址,跟C-SPY 调用flash loader 程序的流程是一致的。 9 h9 E! V0 V, Z$ c$ B& ~. |6 o
# e! {7 G k; ]. U2 D0 R: [ |