你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【NUCLEO-L476RG】IAR flash loader 下载流程简要分析

[复制链接]
andey 发布时间:2022-12-31 12:51
1.IAR 下载配置
! I( t5 h; U. d$ o0 B本次使用的硬件环境为MUCLEO-L476开发板,官方下载的软件包中打开IAR的工程文件,IAR的下载配置如下:
IAR_DOWNLOAD.png
+ l( k2 D8 {) t2 [7 ~5 j
默认配置是使用IAR 系统默认配置文件,我们勾选上Oveerride default .board file文件。
IAR_OVERRIDE.png

/ 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
flah_load.png
  • 通过 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 fromInput 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文件内容如下:
  1. <?xml version="1.0" encoding="iso-8859-1"?>
    8 Z0 Q5 F# K. j2 [/ {
  2. 1 k% z  V6 K, E  ~
  3. <flash_board>
    9 r6 V- S7 M7 t0 P5 ]7 ^
  4.   <pass>6 C# @; J! M( c! d1 _5 [
  5.     <loader>$TOOLKIT_DIR$\config\flashloader\ST\FlashSTM32L4xxxG.flash</loader>
    + k8 _, i7 ^% E" _8 E( ?
  6.     <range>CODE 0x08000000 0x080FFFFF</range>9 f5 t" [, ~4 h% \
  7.   </pass>) [1 O! `$ e8 b) n
  8. </flash_board>
复制代码

: ]2 ~( H% Z. s2 {) F
从如上xml 的配置指定了,flash 的配置说明及FLASH的范围:

' a- T, Q( o( [9 S0 v5 T
3. *.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
  1. <?xml version="1.0" encoding="iso-8859-1"?>' E. U1 {* z3 R6 @
  2. , @/ z7 e6 R$ J2 F% }
  3. <flash_device>6 d8 k) A8 O/ [! G3 V% [
  4.   <exe>$TOOLKIT_DIR$\config\flashloader\ST\FlashSTM32L4xxxRAM48K_DUALBANK.out</exe>
    ) m* y5 y0 _6 q, _1 \/ I5 D, [6 ]
  5.   <page>8</page>/ H: z7 i+ O9 G% A: g
  6.   <block>512 0x800</block>
    + R% N0 ]. s6 ]5 I
  7.   <flash_base>0x08000000</flash_base>8 O% O% V% V! Y
  8.   <macro>$TOOLKIT_DIR$\config\flashloader\ST\FlashSTM32L4xxx.mac</macro>8 w4 b2 ]3 \7 R1 _$ c
  9.   <online>1</online>
    - T& y. e  E( D( G( R; c% r3 S8 E! w
  10.   <aggregate>1</aggregate>  
    ( r3 q( X+ y' G4 h  W" C1 [4 C9 ^
  11.   <args_doc>"--skip_erase" -Don't erase blocks that read empty.</args_doc>% E* p+ d9 Y# {
  12. </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。
flash_page.png
. `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) V
4. *.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 内容如下:
  1. __var RCC_CFGR;% W4 \4 z. t; u% T: k2 l
  2. __var RCC_CR;
    ' d( D9 i  Z; C7 N) Y& j+ Y& b
  3. __var RCC_CIR;2 h, K% k. n- E- E3 q
  4. __var IWDG_PR;
    ( M9 p# ^0 {# f! |% C
  5. __var IWDG_RLR;6 }# g9 h0 G" q+ @3 `
  6. __var FLASH_ACR;. I6 _  d" z8 d+ {( ~0 R- N5 V

  7. " A& {! P: r& s+ t0 D& U
  8. execUserFlashInit()
    1 }( B9 \% v# C/ H. T
  9. {
    8 s5 I& P& a( o2 I: r% E1 Q2 p
  10. __var tmp;1 x% ?# @9 S7 {
  11. 3 c7 o- P8 @$ C0 E! L! N
  12. __message "Entry execUserFlashInit";
    , b- ]: s0 g7 Q4 f3 B
  13. 4 `* e" p! i3 V
  14.   //Stop watchdogs when CPU is halted
    8 c6 v" k8 p* ^# Q; o) }; j8 Q
  15.   __writeMemory32(__readMemory32(0xE0042008, "Memory") | 0x1800, 0xE0042008, "Memory");
    1 [) h6 W; ~7 p+ F, Z

  16. " R+ E, A1 C6 M% G5 E6 I4 x
  17.   //Check if hardware watchdog is enabled
    3 u5 C/ K. w: b7 {  S6 e
  18.   if(!((1<<16) & __readMemory32(0x40022020,"Memory")))* K4 A* b3 ?4 H6 r
  19.   {
    ( z' ^3 W. P. R  m! U; A
  20.     // wait PVU reset/ C0 H, `" o9 J
  21.     while(0x3 & __readMemory32(0x4000300C,"Memory"));
    0 l3 a/ s2 I/ C! g: q1 t
  22.     IWDG_PR  = __readMemory32(0x40003004, "Memory");+ J. y  K( w4 p0 |3 g# S# Q, V
  23.     IWDG_RLR = __readMemory32(0x40003008, "Memory");3 }( k; d( f, T6 _: v
  24.   $ k7 e( B/ P% T: d9 E) h: t
  25.     // unlock WDT registers
    # o' w5 V. u9 F6 ]# J
  26.     __writeMemory32(0x5555,0x40003000,"Memory");& {6 k, r' a7 S7 E- g
  27.     // Prescaler
    , B- B0 V8 n% l
  28.     __writeMemory32(0x7,0x40003004,"Memory");
    & H, M+ p% y* T* k0 M6 t
  29.     // Reload
    # V$ P; E6 [6 P' h5 D4 Y: b- L0 k
  30.     __writeMemory32(0xFFF,0x40003008,"Memory");$ f, c) i. Y* Z3 l% s& K
  31.     // reload WDT
    8 V4 J2 Y6 c6 B9 n: l1 ^' o
  32.     __writeMemory32(0xAAAA,0x40003000,"Memory");! \/ E7 l6 K; I- N$ h
  33.   }
    . @4 V+ M7 g$ C. N# |, _) l6 W$ n
  34. 8 S5 t" ]4 }  f9 G
  35.   RCC_CR   = __readMemory32(0x40021000, "Memory");  g! M$ f* R! j5 t( i: f; M
  36.   RCC_CFGR = __readMemory32(0x40021008, "Memory");
    % G7 w& w( b+ b  Q& Q) x, i
  37.   RCC_CIR  = __readMemory32(0x40021018, "Memory");  Y$ g$ C4 q" t' }0 T7 h% d
  38.   
    ( y; P) ?4 o  f
  39.   /*Enable HSI16 oscilator and select it as system clock*/
    6 A, e, ^% J" \% F
  40.   __writeMemory32(0x00000000, 0x40021018, "Memory");             // RCC_CIR  = 0;
    4 |* P, T4 i; c$ E
  41.   __writeMemory32(RCC_CR | (1<<8), 0x40021000, "Memory");   // RCC_CR_HSION  = 1;4 g5 W8 f6 m6 k7 E7 s: G' x, N
  42.   while(!((1<<10) & __readMemory32(0x40021000,"Memory")));; ~1 C6 w! l7 w0 y% r8 U" L" U2 K! h
  43.   tmp = (RCC_CFGR & ~(3<<0)) | (1<<0);) K% \  B! d) m, v
  44.   __writeMemory32(tmp, 0x40021008, "Memory");                                 // RCC_CFGR_SW  = 1;
    8 B# D# G3 @3 F% r
  45.   / D$ [) Q8 q+ G3 y- x
  46.   FLASH_ACR = __readMemory32(0x40022000, "Memory");
    6 `! r4 H0 ]! U8 D+ V9 s5 {, H9 C' ?
  47.   tmp = (FLASH_ACR & ~(0x7<<0)) | (2<<0);
    * o9 N, |, Z4 \3 u* W
  48.   tmp &= ~(3<<9);
    ' s7 ]' U* s! C% Q$ q9 `
  49.   __writeMemory32(tmp, 0x40022000, "Memory"); // 2 WS (3 CPU cycles) & disable caches3 h* ^* n4 j. g+ `0 ]
  50.   tmp |= (3<<11);! i+ P' l# ^0 ?$ W2 Y$ G' m/ r- e" I
  51.   __writeMemory32(tmp, 0x40022000, "Memory"); // reset caches
    5 I: J* p& J6 k6 r* ^+ W  r
  52.   " A# X1 ~8 o  i. C3 ]4 k
  53.   if ((0xFF & __readMemory32(0x40022020,"Memory")) != 0xAA)
    # d: _! G" _3 k( o8 R  g0 F; C
  54.   {# E$ k9 S9 j- @2 q) t
  55.     ; Q* o) A) ~* F, [, |  Z
  56.     if (!__messageBoxYesCancel("Do you want to perform mass erase to unlock the device?", "Unlocking device"))
    2 P! g: `5 w2 r+ S2 t
  57.     {
    % B6 W# y! P. |3 m+ B3 X7 |9 s
  58.       __abortLaunch("Unlock cancelled. Debug session cannot continue.");
    + D" L! i7 I& w
  59.     }
    9 Q- I, Y# g& k) Q! Z1 F$ R

  60. 4 v$ B9 O+ ^& e. S1 F
  61.     __writeMemory32(0x45670123, 0x40022008, "Memory"); // FLASH->FKEYR = FLASH_KEY1;6 C. G. m. }8 F8 S; i
  62.     __writeMemory32(0xCDEF89AB, 0x40022008, "Memory"); // FLASH->FKEYR = FLASH_KEY2;
    8 p' N4 z- g' X+ f4 E4 H
  63. //    __writeMemory32(0x00000000, 0x40022000, "Memory"); // Flash 0 wait state
    . c2 J5 X: K' F
  64.     __writeMemory32(0x08192A3B, 0x4002200C, "Memory"); // FLASH->OPTKEYR = FLASH_OPTKEY1;2 A; d2 z4 ^; F1 |4 C# j8 \
  65.     __writeMemory32(0x4C5D6E7F, 0x4002200C, "Memory"); // FLASH->OPTKEYR = FLASH_OPTKEY2; 0 r8 k, V) d+ y
  66.       
    5 l" U3 R/ A/ ]5 v$ H9 k" s$ b0 l
  67.     __message "Setting FLASH readout protection level 0 (disabled)";6 {8 y) T8 d$ A4 U4 f- Z) ]
  68.   : G5 z# j6 B9 C9 c
  69.     __writeMemory32((__readMemory32(0x40022020,"Memory") & 0xFFFFFF00) | 0xAA, 0x40022020, "Memory");     // Disable readout protection: Y0 J; k; p; \! W
  70.    
    % w! Q$ X5 ?4 G9 ]% m" H- v" g$ Z% s
  71.     + r0 t2 [: L! S' k! V; D* f
  72.     __writeMemory32((1<<17) | __readMemory32(0x40022014,"Memory"), 0x40022014, "Memory");                 // Set the Options Start bit OPTSTRT
    ) f# I6 A: p( U

  73. # D9 ~4 I2 c3 ~/ Z
  74.     while((1<<16) & __readMemory32(0x40022010,"Memory"));  // Wait while FLASH busy; b" H5 X9 _+ V& w( g) ?$ x3 n" U
  75.         
    ; B/ ~0 D. j. r9 K" b
  76.     __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
  77.       7 p' g" k1 l, ?, r* I
  78.   }! e, D% M, s% j* H: r: j( n& n
  79. }' d8 e  M1 S* Z4 b7 y7 U
  80. $ A' ?4 }, [. }' T. h- P* l0 e
  81. execUserFlashExit()
    % n4 \, {: R  Y: E+ c
  82. {
      G0 C5 F  b: v3 S7 @/ X
  83. __message "Entry execUserFlashExit";
    ; k) x; H. W, P8 d' O/ ?) d
  84.   if(!((1<<16) & __readMemory32(0x40022020,"Memory")))
    % d- P4 D" E5 `3 m/ |( K
  85.   {
    0 c$ V9 v" E1 ~" i
  86.     __writeMemory32(0x5555,0x40003000,"Memory");! [1 L% P" g: l4 ]! @; Q
  87.     __writeMemory32(IWDG_PR,0x40003004,"Memory");( m% l8 f0 T8 }0 V# z
  88.     __writeMemory32(IWDG_RLR,0x40003008,"Memory");# A$ C+ x% y/ N7 D: x
  89.     __writeMemory32(0xAAAA,0x40003000,"Memory");5 `6 Q5 ?) C- l
  90.   }
    $ O6 b. v7 C- o3 B* _2 p
  91. 1 Q& Y; |. g) \
  92.   //Restore registers modified earlier
    1 y1 w4 Z& K' [" v
  93.   __writeMemory32(RCC_CFGR, 0x40021008, "Memory");
    0 m: t. z& \' B4 Z2 ]# M
  94.   __writeMemory32(RCC_CR, 0x40021000, "Memory");8 e1 q, x. i1 K6 p* x1 D
  95.   __writeMemory32(RCC_CIR, 0x40021018, "Memory");, v9 m( ^1 C( w% u+ r

  96. ) X" G2 q% U+ r) T
  97.   __writeMemory32(FLASH_ACR, 0x40022000, "Memory"); 5 c. k$ c. p1 c( P
  98. }
复制代码
) M( U* J- _: {8 V( B' F
从上述.mac 文件中定义了如下宏函数,并在入口中添加了打印输出:

" s$ h; |# Y; i& ]. U
execUserFlashInit()

+ z$ R  X5 E# A  v/ p6 q
execUserFlashExit()

# [- {  D( i% U4 K# ^% B+ t( D) s, S' T* C( T
5.下载验证

  C, b. s& w7 h/ k" E, K
macros 文件是可以被C-SPY 调用的执行文件,为了验证个人的猜想在execUserFlashInit 入口和出口追加了log 输出,来确认猜测是否正确。& ?- Y7 O* j9 E9 F
有了上述的配置信息及flash 的初始化处理,flash loader程序就可以吧编译的二进制下载到板子上执行了。准备下环境验证下上述猜测是否正确。

4 f6 n$ m4 w6 N, G
debug_loader.png
; 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
  1. Flash loader example: j+ {7 S3 K* s4 n! b/ X
  2. The following example shows the source code for a complete flash loader (except the source6 L% g1 y7 R( g( t2 ^" v9 m
  3. code for the framework), but with a flash programming algorithm which simply copies bytes7 C: z6 E8 \  O5 R6 {1 e/ w
  4. from the RAM buffer to the destination address:
    " A, D8 A' A; D/ e1 x% `
  5. #include "flash_loader.h"
    4 `$ X5 A4 `& g* d
  6. uint32_t FlashInit(void *base_of_flash, uint32_t image_size,+ R0 g. P6 P" g/ c" |8 v7 O( x
  7. uint32_t link_address, uint32_t flags)
    + X) b' M; r$ `
  8. {
    ) J  E% I, t: d6 D8 ^& _$ G
  9. return RESULT_OK;4 ]# M5 R: t( k7 z
  10. }! W. N4 L2 s; W) w
  11. uint32_t FlashWrite(void *block_start,' Y! E1 G" Z$ |  K9 k
  12. uint32_t offset_into_block,
    8 b5 q' G4 x2 ^) j5 ]0 h! P) E& B
  13. uint32_t count,
    ) {- t1 v2 ]2 V3 }; t
  14. char const *buffer)7 i. b( M; Q7 I9 c7 z2 d0 [
  15. {
    8 I$ j3 m# ?+ s
  16. char *to = (char*)block_start + offset_into_block;
    $ i" \8 ?# \" z! e5 f
  17. while (count--)* N2 N$ C$ j6 X+ I1 @& |9 O
  18. {& [8 H3 O6 k& Z( Q/ A" ~
  19. *to++ = *buffer++;
    ' N3 R$ E3 C% \" i- y) ^
  20. }& _' r! N; }" s+ K1 @8 e7 k) n
  21. return RESULT_OK;# K  G2 E7 r9 w( S% c3 Q
  22. }
    ) W! k/ }$ M& T
  23. uint32_t FlashErase(void *block_start, uint32_t block_size)# ]  g2 q$ a; h  H# ~9 X5 p
  24. {* H( }: [- G1 F5 l
  25. char *p = (char*)block_start;& W, C1 `/ [8 e$ _# F; o
  26. while (block_size--)+ G5 R+ `! B  }1 w5 a6 k7 C
  27. {
    + `+ n4 Z! R! [# S
  28. *p++ = 0;  k2 O, c  I! s- [# J4 a
  29. }
    1 |4 N% L( p  F9 n# S
  30. return RESULT_OK;
    , ~$ g. b- ~) q. F. H' m1 \
  31. }3 F& r9 K  w% ~
  32. The parameters to FlashWrite and FlashErase, in combination with the flash memory base
    $ q, J$ i! n4 x9 Z- s
  33. address given in FlashInit, fully specify the addresses of the portions of the flash memory to* p$ ?' R& R' C0 \
  34. be programmed. Thus, a given flash loader can be used for any number of different flash
    ( f7 L+ p8 N8 w$ I: U
  35. devices, with different total size, page size, or block layout, provided that they all employ the
    * X5 R- a9 y' \+ d- ]/ t
  36. same flash programming algorithm. The flash memory configuration file (.flash) is used for
    7 s$ y- n3 P+ r
  37. specifying such variations between flash memories.$ R, _4 j) M; V+ v$ O
  38. 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
  1.                 FlashInit:1 a: d4 w- U/ p. L/ O% Q! x' X
  2.   0x200000c4: 0xb510         PUSH   {R4, LR}
    7 Z/ d6 k+ M  N9 z, L! }
  3.   0x200000c6: 0x494a         LDR.N  R1, `.text_8`          ; flash_ie
    0 v" ~4 X" o$ Q+ V( Q: ~; x' I8 K
  4.   0x200000c8: 0xf500 0x2200  ADD.W  R2, R0, #524288        ; 0x80000/ a7 |2 V. j; `# y& ^, L
  5.   0x200000cc: 0x604a         STR    R2, [R1, #0x4]: v$ v( z& u% k; v" E
  6.   0x200000ce: 0x2300         MOVS   R3, #06 S# K8 M: S" Z0 @4 H. u7 d1 s
  7.   0x200000d0: 0x2200         MOVS   R2, #0
    * }3 G# m, g6 ]+ O0 o/ [
  8.   0x200000d2: 0x700a         STRB   R2, [R1]
    2 j4 h$ j3 P: H2 H
  9.   0x200000d4: 0x9a02         LDR    R2, [SP, #0x8]
    7 z& X" a) o5 k# F+ h- Z/ A
  10.   0x200000d6: 0xe000         B.N    @200000da! }. G4 L" W2 g/ {. y
  11.                 @200000d8:
    . n) {3 {7 y' ]( e' @
  12.   0x200000d8: 0x1c5b         ADDS   R3, R3, #1& @5 a2 t5 K! q) |" |2 O
  13.                 @200000da:/ i/ L) f: U; E) a
  14.   0x200000da: 0x4293         CMP    R3, R21 v& X& e4 I  S0 I' z$ h
  15.   0x200000dc: 0xdbfc         BLT.N  @200000d8% B1 @0 U. }" \
  16.   0x200000de: 0x4a45         LDR.N  R2, `.text_9`          ; 0x40022008 (1073881096)/ q; d5 G! s( e
  17.   0x200000e0: 0x6993         LDR    R3, [R2, #0x18]' i" l( N# n+ t7 o$ A! p
  18.   0x200000e2: 0x029b         LSLS   R3, R3, #10: i  F" M4 [8 d7 @/ S. n) ^
  19.   0x200000e4: 0xd507         BPL.N  @200000f6
    ! R; M; N3 i6 N
  20.   0x200000e6: 0x4b44         LDR.N  R3, `.text_10`         ; 0x1fff75e0 (536835552)
    ' n# h6 U1 M- u) E0 M. a+ |
  21.   0x200000e8: 0x4c44         LDR.N  R4, `.text_11`         ; 0x3ffc00 (4193280)! v) h4 O( T3 q3 m2 u5 r
  22.   0x200000ea: 0x881b         LDRH   R3, [R3]
    ) r3 b, U6 K2 G, z" _3 J6 n$ l& |. z
  23.   0x200000ec: 0xea04 0x2383  AND.W  R3, R4, R3, LSL #10
    & i* O4 _+ x/ \$ c
  24.   0x200000f0: 0xeb00 0x0063  ADD.W  R0, R0, R3, ASR #1
    ! g; [+ `  g3 A: Q
  25.   0x200000f4: 0x6048         STR    R0, [R1, #0x4]
    , q, w: B+ l# y  k0 J- G! L
  26.                 @200000f6:
    3 @1 q" |$ {) n0 ?
  27.   0x200000f6: 0x4842         LDR.N  R0, `.text_12`         ; 0xe000e100 (-536813312)
    / w% j4 o5 ~* R! |, [, D1 q+ `
  28.   0x200000f8: 0x4b42         LDR.N  R3, `.text_13`         ; 0xe000e180 (-536813184)
    1 W0 O: n# H' ?4 }
  29.   0x200000fa: 0x6800         LDR    R0, [R0]
    ! B0 X3 V2 F' L7 W1 U7 w
  30.   0x200000fc: 0x06c0         LSLS   R0, R0, #27
    ' i/ j8 w) k+ `9 q, [  p  j; I
  31.   0x200000fe: 0xbf44         ITT    MI
    ; v1 a: L) {! Z: O2 v
  32.   0x20000100: 0x2001         MOVMI  R0, #1
    . L: x" h5 R. y; k  O
  33.   0x20000102: 0x7008         STRBMI R0, [R1]9 Q( l; i( D7 _
  34.   0x20000104: 0x2010         MOVS   R0, #16                ; 0x10
    + ]: g4 z) m( Q+ c& X% m, B
  35.   0x20000106: 0x6018         STR    R0, [R3]
    " I+ `$ b* }! D5 G4 C1 X7 T. w. t
  36.   0x20000108: 0x4b3f         LDR.N  R3, `.text_14`         ; 0xc7000ff8 (-956297224)
    6 Y0 s3 U1 w8 ]- o4 L3 G: j4 l+ e
  37.   0x2000010a: 0x68d0         LDR    R0, [R2, #0xc]4 f  C: N( x# c  K. l" x
  38.   0x2000010c: 0x4018         ANDS   R0, R0, R3
    3 Z% z; g8 n, x( c$ b* `
  39.   0x2000010e: 0x6088         STR    R0, [R1, #0x8]* ]8 c% Z+ t4 N& i& z
  40.   0x20000110: 0x483e         LDR.N  R0, `.text_15`         ; 0x45670123 (1164378403)
    3 U- x. f. K. ]1 G; u) I
  41.   0x20000112: 0x6010         STR    R0, [R2]8 }5 `7 ^4 v9 u# y6 J" o0 |
  42.   0x20000114: 0x483e         LDR.N  R0, `.text_16`         ; 0xcdef89ab (-839939669)
    ( T7 i/ L' R$ G! d
  43.   0x20000116: 0x6010         STR    R0, [R2]# C9 S2 [  e7 j9 p
  44.   0x20000118: 0x68d0         LDR    R0, [R2, #0xc]
    ' ]+ m/ J5 q: V) H! D8 V# h
  45.   0x2000011a: 0xf020 0x60c0  BIC.W  R0, R0, #100663296     ; 0x60000002 t  X, M4 z- O$ p" ^- u" a" t; F
  46.   0x2000011e: 0x60d0         STR    R0, [R2, #0xc]" H# Q- \7 d$ m4 e
  47.   0x20000120: 0x68d0         LDR    R0, [R2, #0xc]
    : A9 t' G! \1 _; x) U0 ]
  48.   0x20000122: 0xf040 0x7080  ORR.W  R0, R0, #16777216      ; 0x10000000 L  z: y. d; A  R. G
  49.   0x20000126: 0x60d0         STR    R0, [R2, #0xc]. V: N( [; f  q5 M' c3 L  V
  50.                 @20000128:
    9 R- A/ b& `: i' K
  51.   0x20000128: 0x6890         LDR    R0, [R2, #0x8]$ q" S* Q& e8 c4 O* Z# X
  52.   0x2000012a: 0x03c0         LSLS   R0, R0, #15
    ; B8 n. t' A+ M+ D. O
  53.   0x2000012c: 0xd4fc         BMI.N  @20000128
    3 z% G4 s' r$ l5 C; q# v9 i
  54.   0x2000012e: 0x2000         MOVS   R0, #0
    6 H$ s4 {: F" A
  55.   0x20000130: 0xbd10         POP    {R4, PC}
    ' Q$ R4 |! F: r9 N/ L
  56.                 `.text_5`:6 f- s& ?' q# U$ L  ?
  57.                 FlashWrite:
    / d3 z0 }5 ~+ b: J% ^/ A9 ^3 r
  58.   0x20000132: 0xb5f0         PUSH   {R4-R7, LR}0 E# Y1 y% D' L% r- ~/ g
  59.   0x20000134: 0xf240 0x15ff  MOVW   R5, #511               ; 0x1ff
    / o# _+ g% N- e2 x$ I( J& {  P, K1 Z
  60.   0x20000138: 0x1844         ADDS   R4, R0, R1
    ! ?5 C: P2 F5 J" b+ L; D: }! w; {5 H
  61.   0x2000013a: 0x4936         LDR.N  R1, `.text_17`         ; 0x40022010 (1073881104)
    . q7 x" V8 H' U+ i8 P9 M
  62.   0x2000013c: 0x600d         STR    R5, [R1]- V/ R4 q  a1 v/ r' {
  63.   0x2000013e: 0x2000         MOVS   R0, #07 A5 O9 m# b8 g# y# W6 P
  64.   0x20000140: 0x684d         LDR    R5, [R1, #0x4]
    . G# f, ~6 g1 b) w( n, f
  65.   0x20000142: 0xf045 0x0501  ORR.W  R5, R5, #13 Y- w! o2 ?. k7 W3 Q
  66.   0x20000146: 0x604d         STR    R5, [R1, #0x4]
    % t4 d% t; T1 s& v! G
  67.   0x20000148: 0xe000         B.N    @2000014c
    9 f3 H7 Q" T2 D6 V3 b) r0 I/ z
  68.                 @2000014a:
    % f  n. [( f  }8 d# Z2 E* ?. }
  69.   0x2000014a: 0x3a08         SUBS   R2, R2, #8- T8 V' f- x5 i5 s2 P
  70.                 @2000014c:0 q" k; e  t& k9 z8 B$ w
  71.   0x2000014c: 0xb162         CBZ    R2, @20000168
    ! H0 C+ r) }% ?( Z, O
  72.   0x2000014e: 0xe8f3 0x6702  LDRD   R6, R7, [R3], #0x8
    6 c, p6 H0 v- N2 W# v( z9 C; ]1 x9 y( K
  73.   0x20000152: 0xe8e4 0x6702  STRD   R6, R7, [R4], #0x8
    ( h, Q2 @) \& @7 X) V" n1 j+ f, k6 }5 P
  74.   0x20000156: 0xbf00         NOP0 ^. c& q/ n& O5 n$ D
  75.   0x20000158: 0xbf00         NOP
    2 |* e" v4 E! B! Y2 g( Y4 r0 e
  76.                 @2000015a:
    : @2 |) v# h. I' a$ ~
  77.   0x2000015a: 0x680d         LDR    R5, [R1]
    ' P4 U! D! T4 c; A8 X
  78.   0x2000015c: 0x03ed         LSLS   R5, R5, #15
    " ~# F2 n$ j: v
  79.   0x2000015e: 0xd4fc         BMI.N  @2000015a
    . x. V8 p9 M: _3 g- w/ Q* O
  80.   0x20000160: 0x680d         LDR    R5, [R1]
    % a  G9 c; V% Q5 R
  81.   0x20000162: 0x07ed         LSLS   R5, R5, #31
    2 E7 H  ?: M; ]+ U# s
  82.   0x20000164: 0xd4f1         BMI.N  @2000014a8 }) B% @1 D" C) @; L$ ^& v( f3 s
  83.   0x20000166: 0x2001         MOVS   R0, #1
    ' a& j. Y6 u8 y' H0 ^
  84.                 @20000168:
    $ Z# n! Y) D8 k6 M6 C$ }
  85.   0x20000168: 0x684a         LDR    R2, [R1, #0x4]
    0 ^: j( b( f8 b* E
  86.   0x2000016a: 0x0852         LSRS   R2, R2, #12 _5 K* y: V4 r" t. g
  87.   0x2000016c: 0x0052         LSLS   R2, R2, #1; X* u- O& I6 I& n
  88.   0x2000016e: 0x604a         STR    R2, [R1, #0x4]
    % c: Y: h- e) @- G- c: L
  89.   0x20000170: 0xbdf0         POP    {R4-R7, PC}
    1 W) t* l( o* Q/ l! H7 \: I: E+ A
  90.                 `.text_6`:  o, h  M# f' }. M6 n* c% [  L
  91.                 FlashErase:  X* Z& e0 ^0 D3 C; G3 X
  92.   0x20000172: 0x4a28         LDR.N  R2, `.text_17`         ; 0x40022010 (1073881104)' h$ C5 z* o3 M7 R' Q2 M
  93.   0x20000174: 0xf240 0x13ff  MOVW   R3, #511               ; 0x1ff8 o+ R! ?  U8 _- C% s  A
  94.   0x20000178: 0x6013         STR    R3, [R2]6 I( s4 o) ^. x& u
  95.   0x2000017a: 0x4601         MOV    R1, R0
    . T! d0 o- ?, _) r3 Z
  96.   0x2000017c: 0x6853         LDR    R3, [R2, #0x4]0 k' p+ f; P, _9 z, ~5 v/ e
  97.   0x2000017e: 0xf36f 0x03cb  BFC    R3, #3, #9
    . W. k1 E' M( `  S6 ], G( {- i9 P
  98.   0x20000182: 0x6053         STR    R3, [R2, #0x4]
    7 T* c2 g$ }. m7 D; ~5 a6 n
  99.   0x20000184: 0x2000         MOVS   R0, #0
    & p3 w% {1 x) s% t% I/ }
  100.   0x20000186: 0x4b1a         LDR.N  R3, `.text_8`          ; flash_ie3 P+ U. m* A* u1 m
  101.   0x20000188: 0x685b         LDR    R3, [R3, #0x4]
    7 _% b- R8 U8 Y. Y+ l( t
  102.   0x2000018a: 0x4299         CMP    R1, R3
    3 m! z1 e3 P! i; W0 u
  103.   0x2000018c: 0xd203         BCS.N  @20000196$ P5 c$ z: X) P1 |
  104.   0x2000018e: 0x0a09         LSRS   R1, R1, #8
    ' v: h' [; K' Y
  105.   0x20000190: 0xf401 0x61ff  AND.W  R1, R1, #2040          ; 0x7f8
      A( F/ ]; z, ^- h+ t7 v; V
  106.   0x20000194: 0xe007         B.N    @200001a6: q$ c* P! r8 W# J8 e
  107.                 @20000196:
    : P& }+ C8 R0 D. ]7 V3 N
  108.   0x20000196: 0x1ac9         SUBS   R1, R1, R3) I/ r& M7 o( B
  109.   0x20000198: 0x6853         LDR    R3, [R2, #0x4]
    . B1 }/ w' G$ v/ {/ x8 @
  110.   0x2000019a: 0x0a09         LSRS   R1, R1, #8
    5 X: ^! D4 Y0 W1 A5 {2 @& l1 W
  111.   0x2000019c: 0xf443 0x6300  ORR.W  R3, R3, #2048          ; 0x800$ q" R- u; }" K# B4 r- q  c
  112.   0x200001a0: 0xf401 0x61ff  AND.W  R1, R1, #2040          ; 0x7f8
      {5 I4 M( g6 n) @0 |8 e
  113.   0x200001a4: 0x6053         STR    R3, [R2, #0x4]
    % E4 N9 L. K/ v
  114.                 @200001a6:2 W0 N$ |8 M- _+ y
  115.   0x200001a6: 0xf041 0x0102  ORR.W  R1, R1, #2' w! [% C: z% k* v% d
  116.   0x200001aa: 0x6853         LDR    R3, [R2, #0x4]) v" l5 P' `% _* }
  117.   0x200001ac: 0x4319         ORRS   R1, R1, R3- {1 w7 i: D4 S4 G& K7 P
  118.   0x200001ae: 0x6051         STR    R1, [R2, #0x4]. X6 f0 S0 _# X' T3 J/ X3 {# ^
  119.   0x200001b0: 0x6851         LDR    R1, [R2, #0x4]; W% r- i6 j3 g# y# H
  120.   0x200001b2: 0xf441 0x3180  ORR.W  R1, R1, #65536         ; 0x10000
    * }' S3 I$ @! y: C
  121.   0x200001b6: 0x6051         STR    R1, [R2, #0x4]3 w: o/ ]* H! i2 G/ g* L0 \( j7 m
  122.   0x200001b8: 0xbf00         NOP2 v/ \$ y# A, `6 S% n
  123.   0x200001ba: 0xbf00         NOP
    4 e2 s" k+ E1 {
  124.                 @200001bc:
    - {% u( W; _9 N7 F4 [% a( c2 D1 S
  125.   0x200001bc: 0x6811         LDR    R1, [R2]6 y3 ^3 W6 i( c* s# R
  126.   0x200001be: 0x03c9         LSLS   R1, R1, #15+ W# M+ G/ g# m: y1 t/ v$ E  t
  127.   0x200001c0: 0xd4fc         BMI.N  @200001bc
    & t2 `6 j( |0 ?0 u7 i3 r
  128.   0x200001c2: 0x6811         LDR    R1, [R2]( }. _+ H% }" Y/ V7 r1 A
  129.   0x200001c4: 0x07c9         LSLS   R1, R1, #31' w9 H# v) i) V. M. R/ }1 j
  130.   0x200001c6: 0x6851         LDR    R1, [R2, #0x4]
      w2 o6 h4 e0 x
  131.   0x200001c8: 0xf021 0x0102  BIC.W  R1, R1, #2) D8 D, `( J- @5 w8 w5 O9 z  ]
  132.   0x200001cc: 0xbf58         IT     PL, f# f) E% a0 Z! p( h( H
  133.   0x200001ce: 0x2001         MOVPL  R0, #1
    . s7 c% Q3 X* R; X0 @5 X2 s
  134.   0x200001d0: 0x6051         STR    R1, [R2, #0x4]
    ; u* `/ x: s2 C4 S
  135.   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: [
资料分割线
FlashLoaderGuide.ENU.pdf (233.17 KB, 下载次数: 8)
赞 收藏 评论0 发布时间:2022-12-31 12:51

举报

0个回答

所属标签

相似技术帖

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版