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

STM32F407.FLASH读写经验

[复制链接]
STMCU小助手 发布时间:2023-1-3 19:04
FLASH1.FLASH Introduce
5 @6 U1 W- o! ?$ f, z
** Main memory :** It is divided into 4 individual 16KB A sector 、1 individual 64KB Sectors and 7 individual 128KB A sector .boot0/boot1 All of them GND From the 0x08000000 Start running code .
** System memory :** The device starts from the system memory in bootstrap mode , It is mainly used to store chips bootloader Code , This code is solidified inside the chip when leaving the factory . Used to download code to main memory . When boot0 Pick up 3.3V、boot1 Pick up GND Start running from here , That is, enter the serial port download mode .

; I; y( h; s, j* P& g0 q
, z. ~$ A! @( v4 `( Y
**OTP Area :**512 byte OTP( One time programmable ), For storing user data .OTP There are additional... In the area 16 Bytes , Used to lock the corresponding OTP Data blocks .
** Option bytes :** Used to configure read / write protection 、BOR Level 、 Software / Reset of hardware watchdog and device in standby or stop mode .
! h# e4 N  C8 K& ~
1.1 Programming and erasing of flash memory
STM32F4 The flash programming of is performed by 6 individual 32 Bit register control :
(1)FLASH Access control register (FLASH_ACR)
(2)FLASH Secret key register (FLASH_KEYR)
(3)FLASH Option key register (FLASH_OPTKEYR)
(4)FLASH Status register (FLASH_SR)
(5)FLASH Control register (FLASH_CR)
(6)FLASH Option control register (FLASH_OPTCR)
STM32F4 After reset ,FLASH Programming operations are protected , Cannot write FLASH_CR.
FALSH_CR The unlocking steps are :
(1) Write 0X45670123 To FLASH_KEYR
(2) Write 0XCDEF89AB To FLASH_KEYR
STM32F4 The number of programmed bits of flash memory passes through FLASH_CR Of PSIZE Field settings ,PSIZE The setting of must match the supply voltage .

2 b% ]8 ?' m) ~, z, ]$ `
" T2 h' g$ c" h6 |0 i. Z- [
STM32F4 Standard programming steps :
(1) Check FLASH_SR Medium BSY position , Ensure that no... Is currently being performed FLASH operation .
(2) take FLASH_CR In register PG Location 1, Activate FLASH Programming .
(3) Perform write operations
(4) wait for BSY A reset , Complete a program .
( e& M9 t$ ]. S- \
Sector erase steps :
(1) Check FLASH_CR Of LOCK Whether to unlock , If you don't unlock it first
(2) Check FLASH_SR In register BSY position , Ensure that no... Is currently being performed FLASH operation

& ~0 E  P" Y) @
(3) stay FLASH_CR In the register , take SER Location 1, And from the main memory block 12 Select the sector to erase from the sectors (SNB)
( n% c& i6 S/ h
(4) take FLASH_CR In register STRT Location 1, Trigger erase operation
(5) wait for BSY A reset
  z* F+ E4 L. p/ C$ {/ A
2. Related registers
6 M8 w+ w" u3 Z% `* y3 P
2 K+ q3 Z) a$ b$ I
3. Configuration method" B) Q- V$ B0 z0 j
3.1 Inside flash Step by step
- D1 S  F, p3 @( ]1 |/ w3.1.1 Unlock and lock$ y8 i; |2 P7 k; a3 S4 y% u9 P8 ?

0 `2 d* z7 c, N$ @7 y
( z- A' I/ y- W) I/ n# c, y
  1. #define FLASH_KEY1 ((uint32_t)0x45670123)
    0 m; E4 v/ I  A) \6 F7 y6 _
  2. #define FLASH_KEY2 ((uint32_t)0xCDEF89AB)
    * o4 z; M5 X/ U# _. u8 @+ G
  3. #define FLASH_CR_LOCK ((uint32_t)0x80000000)
    # _/ S+ @' {, W  U
  4. // Unlock function ' Z  ]' E  @& o+ Q9 |/ A/ t
  5. void FLASH_Unlock(void)
    $ {8 V0 x" K% @  |8 x
  6. {- L8 Z3 \9 e5 n; |: G& Z
  7. 4 k9 U0 ]! m9 [5 e' o8 A5 z9 H3 p7 Y* y
  8. if((FLASH->CR & FLASH_CR_LOCK) != RESET)3 T/ n5 |0 a* M. _# W& u3 J' \) D
  9. {
    6 b0 w& V* H* Y$ `, j
  10. 8 g# J' t) ~1 O* c7 ^& v- z
  11. /* Authorize the FLASH Registers access */$ j* k! R/ N# a" J9 d
  12. FLASH->KEYR = FLASH_KEY1;/ F3 s# f$ `7 m8 ^$ {
  13. FLASH->KEYR = FLASH_KEY2;
    . u/ m! R$ ~' m* h, u
  14. }
    , q/ X: C* z% p/ [1 o* V
  15. }
    * h7 \) N  u$ p* w
  16. // Lock function
    ! o* I+ Q+ X' T) m
  17. void FLASH_Lock(void)
    6 j% G  c& b# K
  18. {- `7 P! m4 A1 l8 x$ }. h, L/ }

  19. + v; h9 z# Z" d# b3 Q$ H# K) h
  20. /* Set the LOCK Bit to lock the FLASH Registers access */
    6 ?: k# Y: H" T) A2 J! l6 ]
  21. FLASH->CR |= FLASH_CR_LOCK;
    5 j7 _6 O- R3 n/ g% h% c+ q
  22. }
复制代码
7 I3 v+ G( J  h% i' C

- _0 ?9 }  h% C6 I7 l% ^. P
* ]( q( T3 d1 @: V" m) ?# p8 p7 r
3.1.2 Write operations1 ^4 B) W) D) D
  1. // Write doubleword ! p8 Z7 Z% w8 ?
  2. FLASH_Status FLASH_ProgramDoubleWord(uint32_t Address, uint64_t Data)
    ( n" W3 A! H( w; l
  3. {% t2 I+ x5 A) e% R2 b: a# ]

  4.   ?  |# K8 H2 u! `. A
  5. FLASH_Status status = FLASH_COMPLETE;
    7 D9 I5 E' M4 `1 d6 p$ R
  6. /* Check the parameters */
    % m$ l& W) ?8 i8 I+ B% b& ?
  7. assert_param(IS_FLASH_ADDRESS(Address));! T( V9 E5 [2 `
  8. /* Wait for last operation to be completed */
    - O, ^: d! @4 g) K
  9. status = FLASH_WaitForLastOperation();& t' d2 `6 p9 G' {
  10. if(status == FLASH_COMPLETE)  G. C/ y% [  ^8 {7 [; A
  11. {' h1 k+ S% I+ W* I; D: a! k" Q+ H

  12. 2 p& f# l" U. }; t
  13. /* if the previous operation is completed, proceed to program the new data */
    / \& }/ n' D3 n
  14. FLASH->CR &= CR_PSIZE_MASK;
    + _* v, Z2 z5 G9 D( s, y; T* E
  15. FLASH->CR |= FLASH_PSIZE_DOUBLE_WORD;
    % }) t1 U9 |8 I3 w7 _; Q" ~
  16. FLASH->CR |= FLASH_CR_PG;
    8 l# F1 |- _+ \; N. l+ w
  17. *(__IO uint64_t*)Address = Data;
    % `) R. x! e! @
  18. /* Wait for last operation to be completed */
    & O5 z: g. ?: e9 a2 q
  19. status = FLASH_WaitForLastOperation();
    ) y. t0 t2 |6 `+ k& q/ `0 q
  20. /* if the program operation is completed, disable the PG Bit */7 F4 M- E! e8 w$ k
  21. FLASH->CR &= (~FLASH_CR_PG);
    4 @" N# W+ D7 l
  22. }/ p$ h" h- l. c2 ?* }- T/ S: @
  23. /* Return the Program Status */
    7 `' f' w& \1 S1 H' N9 X: V$ x# P
  24. return status;& W( U# k# ^9 ?7 z
  25. }
    ) B$ c2 [# y: W# {
  26. // Write word
    4 o' c. S$ E& m4 u$ u, i% Y2 ~
  27. FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)
    ' X9 w; N) Z* `! F5 g$ B
  28. {! U$ ^- p5 B7 }* ?, I. q6 ~9 |  H' [3 O

  29. , v1 x7 b) z8 P( m+ R% F/ ~
  30. FLASH_Status status = FLASH_COMPLETE;6 w0 j/ g" g  X2 ~
  31. /* Check the parameters */: A, p% [- s8 q% k8 n
  32. assert_param(IS_FLASH_ADDRESS(Address));
    0 j) L7 Z! R# i5 [  {- m
  33. /* Wait for last operation to be completed *// E& h: o3 s: i0 C5 w
  34. status = FLASH_WaitForLastOperation();
    : _  n5 @1 D# B0 Y% }+ W
  35. if(status == FLASH_COMPLETE)7 g; R* d' U- ?: l) D& y
  36. {- L1 ]" F( s& x, a" U' t) [
  37. 8 O/ L9 F8 ?- J& \6 W$ i' c
  38. /* if the previous operation is completed, proceed to program the new data */
      C) U( P. F+ l6 N9 ^" i
  39. FLASH->CR &= CR_PSIZE_MASK;* q- }2 P+ w1 |& ]! q9 N2 I8 x
  40. FLASH->CR |= FLASH_PSIZE_WORD;$ x( w- W, ~+ d. G! s
  41. FLASH->CR |= FLASH_CR_PG;
    " y3 @  B% E& |6 ]6 v( a' h
  42. *(__IO uint32_t*)Address = Data;
    ; Q: D$ `- p9 g& z2 v  \/ R! a  I
  43. /* Wait for last operation to be completed */
    ! C- ^- Y: B& R$ b$ n6 x) S" i
  44. status = FLASH_WaitForLastOperation();
    ' F4 S4 K$ e4 d
  45. /* if the program operation is completed, disable the PG Bit */
    " |9 b8 }/ u: z# L" r, g" m
  46. FLASH->CR &= (~FLASH_CR_PG);! `& F" q3 G% ~) ?' ?
  47. }
    1 O6 w  w$ R& I
  48. /* Return the Program Status */
    . t. O9 d4 h2 D5 e& O
  49. return status;% S' r9 E! y% G; Q( {  a
  50. }
    : \; O8 m5 r' _/ c# Y
  51. // Write half word   e) s" s, ]# E5 p# D# F8 l
  52. FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)
    . M: p! t0 k4 Z* \% J
  53. {( l) ?+ i1 X* a6 L2 l% s% v

  54. * I* g  G1 `" A- i; f, ~4 Z
  55. FLASH_Status status = FLASH_COMPLETE;
    / \) U$ s! s$ D* @  L" E4 k3 M3 m
  56. /* Check the parameters */' q) f  S& N' i+ A( W
  57. assert_param(IS_FLASH_ADDRESS(Address));
    . m' ^% d! O. j+ X
  58. /* Wait for last operation to be completed */
    # \# \- _* s; e( Y5 N% a3 x
  59. status = FLASH_WaitForLastOperation();! i* O" z# }$ G9 @* |: m
  60. if(status == FLASH_COMPLETE)0 n( u) @; I! A- a+ C: F
  61. {
    $ {; n8 y# g( `! [( m1 f# v

  62. 8 c9 e/ g- ?- Q- W7 A
  63. /* if the previous operation is completed, proceed to program the new data */
    # M# A. z- i( T5 N0 F1 G+ }( t2 W* q
  64. FLASH->CR &= CR_PSIZE_MASK;2 O. Q, E7 c1 {! a  b" `
  65. FLASH->CR |= FLASH_PSIZE_HALF_WORD;% h: M3 f# x; U
  66. FLASH->CR |= FLASH_CR_PG;7 t$ [9 e8 `9 x4 d' p! _
  67. *(__IO uint16_t*)Address = Data;
    % l. e  ^% B+ T# D2 n
  68. /* Wait for last operation to be completed */
    2 v' s' S" c; I/ S
  69. status = FLASH_WaitForLastOperation();  d" ~% X, T3 ]3 u
  70. /* if the program operation is completed, disable the PG Bit */
    7 e& X" T3 N9 X6 r
  71. FLASH->CR &= (~FLASH_CR_PG);
    7 s1 D/ \* k$ j; X
  72. }
    / C  n' A. l  `& U9 ~8 D
  73. /* Return the Program Status */
    ) l7 M! ]$ ]/ d; b
  74. return status;
    - z2 m& a1 Q+ x* y. J
  75. }+ `( I: H: S/ k4 I! m% s  {
  76. // Write Bytes
    ' P/ c  G- m" {$ d( ^) z4 O* s
  77. FLASH_Status FLASH_ProgramByte(uint32_t Address, uint8_t Data)
    2 c( Z& N" K4 M8 O
  78. {
    ; f; ]& Y. `( z% {! ?

  79. : U4 {) P; }" z7 k' T. l: _
  80. FLASH_Status status = FLASH_COMPLETE;& z$ N/ b3 \1 @, n+ z5 @
  81. /* Check the parameters */
    8 [9 J3 Q) S. z  ]! ?: V
  82. assert_param(IS_FLASH_ADDRESS(Address));
    + s0 k7 ^! P( R2 D& ~
  83. /* Wait for last operation to be completed */
    1 k) o, B+ s+ L4 T6 e2 t  W; I
  84. status = FLASH_WaitForLastOperation();
    - D! \6 v7 K: A/ n2 u
  85. if(status == FLASH_COMPLETE)
    ( C, t' n9 c( W8 P, F: U0 V
  86. {# z9 u$ \# I& X8 Z2 {
  87. " `+ ]9 `) ?, F
  88. /* if the previous operation is completed, proceed to program the new data */- N( Q$ H3 K# m4 T$ Q
  89. FLASH->CR &= CR_PSIZE_MASK;3 X! U1 }# |. Y7 i/ b" o
  90. FLASH->CR |= FLASH_PSIZE_BYTE;' F- X0 |6 x3 z% r
  91. FLASH->CR |= FLASH_CR_PG;6 R( a  ~0 K5 d% o
  92. *(__IO uint8_t*)Address = Data;* ~6 E6 P/ N7 c9 S$ Q
  93. /* Wait for last operation to be completed */! ?! ?; C9 S. q1 E7 M5 Q/ m
  94. status = FLASH_WaitForLastOperation();
    1 I: V! X; d/ N7 |; d
  95. /* if the program operation is completed, disable the PG Bit */
    / |4 k8 R0 [8 v; G* R, W+ B
  96. FLASH->CR &= (~FLASH_CR_PG);
    % Q% ?6 X* N/ J: Y; L6 ?2 J
  97. }
    ( M: ~* Y' X' d
  98. /* Return the Program Status */! O5 _! {; q% g
  99. return status;/ C/ m. y4 q4 l! @2 D
  100. }
复制代码

% p) V2 K) j, e7 k% G# V
7 z: H  \' ]  e- k( {3.1.3 Erase operation
+ A' z2 A2 M+ U
  1. //FLASH_Sector: Select erase sector - I: A& q! E' s: {- ]
  2. //VoltageRange: Select the power supply voltage range 7 `3 g7 z0 {$ y+ ^4 N$ |+ s
  3. FLASH_Status FLASH_EraseSector(uint32_t FLASH_Sector, uint8_t VoltageRange);3 O3 Z" P4 \& |+ d
  4. FLASH_Status FLASH_EraseAllSectors(uint8_t VoltageRange);
    % m7 B( W; `, F! ~
  5. FLASH_Status FLASH_EraseAllBank1Sectors(uint8_t VoltageRange);
    + n$ C; l; [0 k1 l: r% ~% H' C
  6. FLASH_Status FLASH_EraseAllBank2Sectors(uint8_t VoltageRange);
复制代码

. o7 C  |2 M2 z4 U% k
: c( ?) S, l9 ^3.1.4 obtain FLASH state3 i3 d& A$ j$ ]* ~5 q
  1. FLASH_Status FLASH_GetStatus(void). _7 }, F0 s. @2 T1 t
  2. {! q, z  M: l4 y; `

  3. ! O' m  K, ]4 A* c5 f4 m# \
  4. FLASH_Status flashstatus = FLASH_COMPLETE;+ q1 F! k1 \( H3 n7 F( Q
  5. if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY), @) N+ W, W& ^- p  N" c
  6. {$ h& ]& T! l; U
  7. 6 r) a6 [( v' o6 N$ m+ w$ j. W
  8. flashstatus = FLASH_BUSY;
    $ `; U/ b3 T; s, h5 S/ {# W: Z
  9. }
    + }/ ~, U0 e+ e/ {' _) ?$ l5 L
  10. else
    & Q; C5 J' K; r" n7 b; E
  11. {
    7 p( y/ ]7 E( n; Z' k) a5 ^( E1 E0 j

  12. 8 t6 \. n0 P7 u$ y) `3 @  n+ Z
  13. if((FLASH->SR & FLASH_FLAG_WRPERR) != (uint32_t)0x00)" ^8 y( x, q$ B
  14. {
    " y0 Y6 ~2 t8 Y( W7 f

  15. 0 J% q) m( K1 b) x
  16. flashstatus = FLASH_ERROR_WRP;: G* w# R1 f% P. x7 A
  17. }
    + \, B6 q6 ~/ B# a& H, ^7 o2 T6 x
  18. else
    9 M7 A9 N, Q3 Z( V0 G
  19. {2 u/ w9 @# T! e5 q# o9 M

  20. 2 H2 m( p4 @) s) Q
  21. if((FLASH->SR & FLASH_FLAG_RDERR) != (uint32_t)0x00)
    ( ~9 l! k( ^1 b% l- V$ m" g
  22. {& L, c$ N$ e( V4 b" R+ ?

  23. 8 {+ U4 g! v% c$ ?+ f" j
  24. flashstatus = FLASH_ERROR_RD;2 ?  R+ {+ j7 ^* Z! C) z
  25. }* w# p2 T/ G5 }7 Z
  26. else
    2 ?; v% d+ `, o6 B
  27. {
    6 d+ L8 v1 k( _4 h8 M( N$ H6 K

  28. ) ?) V5 C8 u+ ]) ]! }6 R" z+ T
  29. if((FLASH->SR & (uint32_t)0xE0) != (uint32_t)0x00)
      x% z  s, f5 ]+ G) J* W
  30. {
    7 C* Z/ ]2 g) i1 b

  31. + [8 L& {$ h/ S5 U2 l0 f9 q  E$ [
  32. flashstatus = FLASH_ERROR_PROGRAM;* s  T! z  T, F' @
  33. }6 H+ n, p, r2 i0 B/ S, f# e
  34. else
    6 J) y' J, U" f4 ~( ~
  35. {
    . @( L7 g  _: [6 e

  36. # a; s, Q# J/ M) d
  37. if((FLASH->SR & FLASH_FLAG_OPERR) != (uint32_t)0x00)( N/ z; q7 n, I+ U7 b- h
  38. {8 m9 B  J. c* [  F

  39. # O7 E1 q3 F1 L- s
  40. flashstatus = FLASH_ERROR_OPERATION;& x5 N0 Q% i4 @) m
  41. }4 {& M" ^7 ~2 ?3 V
  42. else
    6 p8 [) X: F1 y# p4 N: f  W5 o9 F
  43. {
    ( }' O! d) F( q& V: i( }  o
  44. 7 B/ z# x3 |4 R: t# }3 a' ?5 Z
  45. flashstatus = FLASH_COMPLETE;# d$ b5 t# E6 d: `
  46. }
    - A, K7 M' [1 M5 R( r& T
  47. }7 |& C( F8 s- ^3 Q' T
  48. }
    ( Q- G' U# A) z( G/ X! k
  49. }
    ( O  H% G! C7 t+ q* B6 d# _
  50. }  l7 J. E0 |2 _/ g! F6 H( Q
  51. /* Return the FLASH Status */' S) M! t$ j7 Q0 W
  52. return flashstatus;9 ^) f- }# ~' [9 k
  53. }
复制代码
: h* K3 k* B4 J
6 d. ~7 q' U2 ?: n7 r5 i6 u
The return value is defined by the enumeration type
  1. typedef enum. V6 ?2 V1 J6 N
  2. {9 Y3 e0 _  ?) h8 w9 U- f, ~! v
  3. 9 _( I! e% O7 \! U
  4. FLASH_BUSY = 1,// Operation busy
    0 I) t( \+ p5 r# t% h4 t8 Z
  5. FLASH_ERROR_RD,// Read protection error , ~4 ?# u+ _; i3 H" o6 ~
  6. FLASH_ERROR_PGS,// Programming sequence error
    9 V$ h3 t+ [; ]; M: {
  7. FLASH_ERROR_PGP,// Programming parallel bit error
    8 f, l! o& m- O0 R$ l/ z4 J; T
  8. FLASH_ERROR_PGA,// Programming alignment error
    2 U# I6 r# Y  D9 K$ i& @
  9. FLASH_ERROR_WRP,// Write protect error
    7 t) q  p2 I& U! x
  10. FLASH_ERROR_PROGRAM,// Programming error
    0 l$ P4 z/ f5 H) }8 |, |
  11. FLASH_ERROR_OPERATION,// Wrong operation
    " ?: b$ h. f2 ]3 m  j0 C5 [
  12. FLASH_COMPLETE// End of operation
    5 R& }4 l% q" M: \7 {0 u
  13. }FLASH_Status;
复制代码

2 F: C  b1 L, [4 C
3.1.5 Wait for the operation to complete
When performing flash write operation , Any read operation to flash memory will lock the bus , After the write operation is completed Read operation can be carried out correctly ; When writing or erasing , Cannot read code or data . So before each operation , We all have to wait for the last operation to complete before this operation can start .
  1. FLASH_Status FLASH_WaitForLastOperation(void)
    " \5 X0 |0 ~+ f4 M0 F  D" L1 m; j
  2. {
    . O4 H; @) u7 r& \2 u6 \9 {2 g. {
  3. - K9 U7 D8 Z  p1 r  r
  4. __IO FLASH_Status status = FLASH_COMPLETE;
    5 P% m% ]" _0 X
  5. /* Check for the FLASH Status */* l9 y& V: f- l4 \, O, G
  6. status = FLASH_GetStatus();
    & Q0 G' T+ M1 B/ \. s: U- J+ v
  7. /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset. Even if the FLASH operation fails, the BUSY flag will be reset and an error flag will be set */
    2 i+ o3 i3 `" ]' M5 [
  8. while(status == FLASH_BUSY)
    * g0 N1 l6 q9 Z; j9 w+ U. t. P
  9. {
    1 A4 I: G) {7 m: F
  10. / d: e' n; ?# P: \# ^5 D5 h- s6 [& y5 s" E/ |
  11. status = FLASH_GetStatus();
    6 b/ u! ^' Y! q3 r$ O. k; v. T' ^4 b
  12. }
    . v( [& x% B7 g, [: M
  13. /* Return the operation status */
    - d. I8 e6 ^0 r- f5 R6 C" _# h- r2 l
  14. return status;. n0 m; [- l7 Y& l2 [1 h
  15. }
复制代码

% m0 C, b6 R# [& Q6 R$ G
3.1.6 Read FLASH Specify address data
! w: O7 ]9 P# a
  1. // Read the word at the specified address (32 Bit data )
    # }! r* Y) u( |1 l, k
  2. //faddr: Read the address   Q* w# G6 d7 H0 w) ~  h% u; i
  3. // Return value : Corresponding data . 3 O, }( a6 o& t; _+ N5 D* [
  4. u32 STMFLASH_ReadWord(u32 faddr)
    0 f! D- I; w' a  O7 D3 U
  5. {
    . J' N9 n- U+ B: q' S- ^
  6. return *(u32*)faddr; }
复制代码
0 d+ i, `2 X  n- Q$ y% k- t

5 ?% v. [6 B' }- q  M( a# c
, y! p  p9 {" y1 g" B3 X
3.2FLASH Read data functions
/ e: M( x% g, r; `3 {7 c! A0 L: i! T3 i
  1. // Read the data of the specified length from the specified address 1 a4 E& ?5 ?* {2 H
  2. //ReadAddr: Initial address
    $ P& h& z& h0 ?" ^
  3. //pBuffer: Data pointer
    # k5 _' i; F+ W+ k) I; M& @+ o4 I
  4. //NumToRead: word (4 position ) Count
    4 P2 L9 @2 C+ z
  5. void STM32_FLASH_Read(u32 ReadAddr,u32 *pBuffer,u32 NumToRead)/ y& v, x) e% N% r2 m+ N7 L/ v* b3 L# G
  6. {
    7 t! W/ M" b2 p3 q7 N: [
  7. & _! o7 f, F) Q1 z" s, E- X
  8. u32 i;8 G7 H6 J0 w; `8 P
  9. for(i=0;i<NumToRead;i++)
    2 d- B7 e# D9 a% Z* M2 G
  10. {5 R( N( n5 R; ]6 H

  11. - J1 h; r0 ]% W. b+ h- f
  12. pBuffer[i]=STM32_FLASH_ReadWord(ReadAddr);// Read 4 Bytes .2 v/ }) l6 U/ `% f
  13. ReadAddr+=4;// The offset 4 Bytes . 3 C( U8 c0 r/ ]# s1 e0 ^) U0 P& W
  14. }
    % v& s$ u* G. _  D7 ^! m
  15. }
复制代码

) X) A  Z) h& h& O7 z5 u% I: @+ J4 m# n3 z4 K( C& i* `: p
7 S. a& `0 R% ?! g% o* f& F
3.3FLASH Write data functions
" E( {6 u! ^! b5 z+ `
  1. // Write data of the specified length from the specified address
    0 k* j' M! T8 [8 @
  2. // Particular attention : because STM32F4 The sector of is too big , There is no way to save sector data locally , So this function . J' X9 f: }; y2 B+ B0 Q
  3. // Write the address if not 0XFF, Then the whole sector will be erased without saving the sector data . therefore 0 ~0 W8 t; T) T: e6 {
  4. // Write non 0XFF The address of , This will result in data loss of the entire sector . It is recommended to make sure that the sector is 8 m: D+ f* d" ?+ e! r0 O/ m
  5. // No important data , It's best to wipe out the whole sector first , Then slowly write back . - n7 f* h: H, F
  6. // The function pair OTP The area is also effective ! It can be used to write OTP District !6 R; z' `' d- }$ d3 Q2 @
  7. //OTP Area address range :0X1FFF7800~0X1FFF7A0F1 g3 t1 Z; p9 a* [5 T6 b
  8. //WriteAddr: Initial address ( This address must be 4 Multiple !!)/ {! ~9 h; G3 C5 l# d( P! i
  9. //pBuffer: Data pointer : p9 N% e, Y4 Z1 j/ X9 ~" |, C
  10. //NumToWrite: word (32 position ) Count ( Is to write 32 Number of bit data .)
    7 }+ k9 Y! G2 X8 w- E) T
  11. void STM32_FLASH_Write(u32 WriteAddr,u32 *pBuffer,u32 NumToWrite)0 |: Z5 a4 Y: t. ]1 W
  12. {7 \+ M4 l! L$ Y/ D; W

  13. * m" h: A4 P! x8 n# S
  14. FLASH_Status status = FLASH_COMPLETE;* F) X$ A" g/ ?8 t
  15. u32 addrx=0;
    8 q+ Y9 d; K" ?, U& {+ [3 L! E6 G
  16. u32 endaddr=0;5 p+ V0 x/ c, Y/ E) Q# P0 b
  17. if(WriteAddr<STM32_FLASH_BASE||WriteAddr%4)return; // Illegal address
    6 O# Y3 j! S' h- C  d) R
  18. FLASH_Unlock(); // Unlock
    ) [# w9 ^. s9 ?. [" L
  19. FLASH_DataCacheCmd(DISABLE);//FLASH During erasure , Data caching must be disabled
    1 T3 @3 F! J( e( b; [
  20. addrx=WriteAddr; // The starting address for writing ) v( l& w: s; C% ^+ p' ^1 U% e9 J
  21. endaddr=WriteAddr+NumToWrite*4; // Write end address 8 n* h( e- m# n# _6 L
  22. if(addrx<0X1FFF0000) // Only the main storage area , To perform the erase operation !!
    ' [4 t3 U, t! w/ P5 S- G
  23. {, m, Y% P: q% X0 f( w. @
  24. 3 }/ o) A9 }9 \
  25. while(addrx<endaddr) // Clear all obstacles .( Right FFFFFFFF The place of , Erase first )3 E$ E: T& {9 @8 ]
  26. {
    , f4 m  P- \, C/ ]' a6 p
  27.   V0 i4 J/ Q' T/ F4 s$ M+ b- E
  28. if(STM32_FLASH_ReadWord(addrx)!=0XFFFFFFFF)// There are not 0XFFFFFFFF The place of , To erase this sector
    % S$ `# a$ q% D; z# U) L6 q
  29. {" x' I1 Q' W2 A* e/ {9 O' j+ J
  30. % T" X. h* J7 B  B' p
  31. status=FLASH_EraseSector(STM32_FLASH_GetFlashSector(addrx),VoltageRange_3);//VCC=2.7~3.6V Between !!
    . \# ?' @# d$ w1 o  j/ }& p# f; O' M: N
  32. if(status!=FLASH_COMPLETE)break; // Something went wrong ( I- x7 E- i7 v! F$ t- K  H* v
  33. }else addrx+=4;
    & Y6 ?" T4 T# r
  34. }
    6 g& |# V8 D/ P* n( J4 ?
  35. }
    , h$ O8 n8 S: ?& W8 V
  36. if(status==FLASH_COMPLETE)0 o; J7 s" `- s) ]" P7 _
  37. {
    6 b5 x4 t  e0 ?6 n% f
  38. # W; h  b# `  R0 E
  39. while(WriteAddr<endaddr)// Writing data / G7 W3 r- r3 n* S7 F7 ?
  40. {! v& U$ v0 L( l5 N& S0 @' w2 k

  41. % O- g" y' q; c+ B
  42. if(FLASH_ProgramWord(WriteAddr,*pBuffer)!=FLASH_COMPLETE)// Write data
    4 v' ?$ `( F7 t+ t* {- T
  43. {+ X( ?6 V2 i- Z. g  z7 ^

  44. : E) L7 o6 U+ k& X0 L
  45. break; // Write exception 4 w+ |' w0 ]0 I* o. D
  46. }$ o" Y# }6 i, Q2 y  ]
  47. WriteAddr+=4;: u# N2 _  }# J* _2 w) ?0 l9 Z
  48. pBuffer++;# u7 B& o; ^& ?1 W* C
  49. }
    # U/ J% a3 E% H8 \% r
  50. }
    + D. o- a" a5 p* y1 W
  51. FLASH_DataCacheCmd(ENABLE); //FLASH Erase end , Turn on the data cache 5 x5 ^8 L4 e( L$ ]$ X! S
  52. FLASH_Lock();// locked # Q! t8 }8 @" i  @
  53. }
复制代码
  1. // Save the usage of this macro definition , Very good
    2 p1 C+ s1 A0 Y  n! b0 T8 \
  2. const u8 text_buf[]="www.prechin.net";
    + ~* k+ H4 V# \
  3. #define TEXTLEN sizeof(text_buf)
    . v% Q6 F# o! m2 t/ X
  4. #define TEXTSIZE (TEXTLEN/4+((TEXTLEN%4)?1:0))
复制代码

8 f) v* @% K0 [# s
# ^2 Q: e# @* |! V7 [0 S9 ?
If the explanation is not in place, I hope you can point out , There is something I need to explain , I hope you will put forward , I'll issue a document to explain .

( k. P$ q6 A2 v! @5 p1 s- x0 W
转载自:Autumn grass fire

! ^$ S1 x7 Z. }2 a
* O( y$ g- j" ]% z- O: M3 c
收藏 评论0 发布时间:2023-1-3 19:04

举报

0个回答
关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新和工艺
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版