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

【经验分享】STM32F0使用FLASH

[复制链接]
STMCU小助手 发布时间:2021-11-22 22:03
STM32F0使用FLASH5 ?3 C0 m$ y$ O* |( a' _

* m4 Y0 a' |/ v3 h* r
  1. 5 O' \  A5 }2 x4 V
  2. /* Private typedef -----------------------------------------------------------*/4 W1 q( k- U- f( }
  3. typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
    $ T9 h+ y7 ^0 I( L
  4. /* Private define ------------------------------------------------------------*/; _  x  _3 r# [# {! g9 M/ k5 M. `
  5. #define FLASH_PAGE_SIZE         ((uint32_t)0x00000400)   /* FLASH Page Size *// ], N# D6 q' k0 y7 ^+ j
  6. #define FLASH_USER_START_ADDR   ((uint32_t)0x08003000)   /* Start @ of user Flash area */
    * x2 z0 a5 S) N3 G% H
  7. #define FLASH_USER_END_ADDR     ((uint32_t)0x08003000)   /* End @ of user Flash area */
    : Q4 T' h+ L0 \% D& Y& Z0 E) ?
  8. #define DATA_32                 ((uint32_t)0x12345678): _5 |8 n" l3 \: [" z6 W  G
  9. * R8 x' H7 [' U+ z5 O% ?
  10. /* Private macro -------------------------------------------------------------*/7 R" W2 O6 x! I. |* L0 `, S
  11. /* Private variables ---------------------------------------------------------*/" D2 M6 V  ]/ e8 I) s
  12. uint32_t EraseCounter = 0x00, Address = 0x00;
    $ r# v: u! F: D- b( J7 s& h* c6 e
  13. uint32_t Data = 0x3210ABCD;
    0 Q  ~+ I# H) V. o
  14. uint32_t NbrOfPage = 0x00;5 Z9 g# I& `) L; H7 u: k0 ]
  15. __IO FLASH_Status FLASHStatus = FLASH_COMPLETE;1 Z( e: r6 p6 J$ Z3 W3 s1 ^) E
  16. __IO TestStatus MemoryProgramStatus = PASSED;
    2 f6 u$ \6 M3 g! @. a: b4 M$ b

  17. 2 L& m- x' V; i2 {) M
  18. /* Private function prototypes -----------------------------------------------*/& g. p6 B9 m# p8 d4 a+ a
  19. /* Private functions ---------------------------------------------------------*/; r# ]8 E* B3 S! y8 `
  20. 9 U1 N. I& J' j8 [7 K4 c
  21. : e! q6 J: N, D  k0 C( o- f9 h
  22. //void delayus(uint16_t u16us)   # Z+ J6 j  \, d% G
  23. //{   
    9 h4 I9 H3 f: q$ u  _2 Q/ j
  24. //   uint16_t i,j;   2 o9 J' B: g, I- c, ?8 I1 [
  25. //   
    - h2 r2 p* T8 j
  26. //    for(i=0;i<u16us;i++)   
    " H; U" B3 [( M' w4 |
  27. //    {   
    ' }4 i# l- ^. A, R$ M. R- w0 _$ B
  28. //    //   for(j=0;j<30;j++)   + Z8 `6 Q  o4 v& z7 F& S  s
  29. //     {         / G& Y- s- I! l( S; I- u
  30. //            __NOP();   
    ; T* x6 Z( ^9 e% p8 Y
  31. //     }              
    ( `6 q5 q, P; [8 F" V( X
  32. //     }     1 Y! M, b  B) w% D9 p3 _  Y& }
  33. //}   ' a. l8 ]4 z- g! U
  34. void FlashRead()5 S2 c1 `2 c. Q9 Q/ i: j; X
  35. {, Y/ a1 C) h0 _3 ^0 A
  36. ' X) Z0 }9 j) K4 |6 a7 G0 v2 N
  37. /* Lock the Flash to disable the flash control register access (recommended% @% `- Z2 p1 S
  38.      to protect the FLASH memory against possible unwanted operation) *********/5 |1 F+ j- ?1 c1 ^- R
  39.   FLASH_Lock();
    # Q3 \; K9 y0 S4 X

  40. 4 q# }0 ~- }' ^+ Z- r; i1 z; L; o
  41. ' |7 F% k3 }8 A
  42.   /* Check if the programmed data is OK . _  K9 p+ \3 R" \
  43.       MemoryProgramStatus = 0: data programmed correctly+ q  Q  U$ a/ i# W
  44.       MemoryProgramStatus != 0: number of words not programmed correctly ******/! g* p1 C; @, r0 ?* l
  45.   Address = FLASH_USER_START_ADDR;
    3 ]4 ?, U& y$ l* K
  46. // MemoryProgramStatus = PASSED;
    2 D  e3 \; b0 X5 b# H0 h6 _- H  f

  47. 0 P. O: w: b0 O7 D8 K
  48.     CONFIG_A = *(__IO uint16_t *)Address;    Address = Address + 2;  \! U$ C0 P) C
  49.     CONFIG_B = *(__IO uint16_t *)Address;    Address = Address + 2;
    8 e/ L5 N! U9 L; A5 M- k+ ?
  50.     CONFIG_C = *(__IO uint16_t *)Address;    Address = Address + 2;) G4 E& O# h8 `/ l% G; d
  51. " s& y0 C9 T3 c) q- ?& U
  52. }
    ! k9 v1 ~, `' E( u
  53.   {2 D( ?2 ?0 ?! k! g5 K
  54. void FlashSave()
    , c: n. J' ~" w8 @
  55. {* v. x0 I% S/ J! M- ?" g; o
  56.    /*!< At this stage the microcontroller clock setting is already configured,
    7 D2 }* b+ I* A5 j% f# M
  57.        this is done through SystemInit() function which is called from startup) b2 U& R. @8 G" G4 r  f) b
  58.        file (startup_stm32f0xx.s) before to branch to application main.1 e& G# j- _( _; T6 E1 _7 ?
  59.        To reconfigure the default setting of SystemInit() function, refer to
    1 Q! _) K* _, Z! z0 O
  60.        system_stm32f0xx.c file, I: K! D( @, ?, t; I
  61.      */
    3 J+ J4 l# E9 M! g

  62. 0 a0 K  l6 r) S" c2 i
  63.   /* Unlock the Flash to enable the flash control register access *************/ , Y6 k) E: F, p  }0 T
  64.   FLASH_Unlock();7 p! E/ v; I6 @: H$ M
  65. 1 \3 k$ ?6 E0 I, B' `
  66.   /* Erase the user Flash area4 O+ K: Q$ ^- W+ m3 x  i
  67.     (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
    - y, N' `- R0 g% \+ p# u
  68. - S7 ]9 u0 P5 Y6 Y; K+ T
  69.   /* Clear pending flags (if any) */  
    ! `# O1 s; _; C& Y. h3 w
  70.   FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);
    5 z4 ^( s, y8 Y% H* ^  T
  71.   E1 v: u/ d. q$ K7 S* b
  72.   /* Define the number of page to be erased */
    : Z: B3 y; ]% F7 [( t1 i
  73. // NbrOfPage = (FLASH_USER_END_ADDR - FLASH_USER_START_ADDR) / FLASH_PAGE_SIZE;
    9 o* H0 J4 h, n2 G

  74. ' R, s7 e- q. @7 H' b4 Y. a! ]

  75. * h; R+ ~: _) d2 |, `! A
  76.   /* Erase the FLASH pages */
    1 g' L2 Z- s2 Y
  77. // for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)! Y4 Y( V& u' Y) k" |
  78.   {, Q2 T* x: V7 s* O) u* T
  79.     if (FLASH_ErasePage(FLASH_USER_START_ADDR )!= FLASH_COMPLETE)
    5 {  k/ T! s3 s4 j- ]  v
  80.     {
    ' k! [* m7 J/ ~
  81.      /* Error occurred while sector erase.
    ! ?( N6 t! Q9 R) F
  82.          User can add here some code to deal with this error  */
    : F6 J! d4 q8 {4 i) J& {( s
  83.       while (1)$ I2 n5 ]* E* r6 G" ]2 B
  84.       {9 _" {8 ~3 y7 ~4 l! L
  85.         TM1620_DISPLAY_DOT(999,0,PAGEB);//B999标识存储器错误+ r/ S* C+ f# K+ U
  86.       }
    5 b( _2 ~: }, |4 h  e" ?4 ^
  87.     }1 ]1 _9 C- K9 k  ]& y7 T# p9 C
  88.   }
    6 ]( W- O% l- R  j- ^3 r. B1 P
  89.   /* Program the user Flash area word by word
    7 @3 J, b3 O) [, \1 A
  90.     (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/9 v( h# ]1 L0 X' v; d

  91. " n" {+ W, T- g" E# [+ v3 B* K2 ]
  92.   Address = FLASH_USER_START_ADDR;& {2 O3 x4 s5 }# C# T
  93. $ X9 k0 r: |$ B
  94. // while (Address < FLASH_USER_END_ADDR)& m4 z5 Q- ^+ S' B" [/ N
  95. //  {
    ! Y1 Y  k: t! d# w3 i
  96.     if (FLASH_ProgramHalfWord(Address, CONFIG_A) == FLASH_COMPLETE)
    ) k0 j* c0 l7 d. M
  97.     {
    $ Q/ I% o2 P4 l; x4 V! v

  98. ' @) W1 E' E- H& O5 o9 B0 d
  99.     }
    # h% O' v7 t, n* x4 p$ o8 f
  100.           else
    . X0 a* T6 N  E7 Q2 @
  101.     {
    6 Z. ?! ?$ w2 F. V" t5 D* J) L7 {
  102.       /* Error occurred while writing data in Flash memory.
    4 g4 S$ y& t: S
  103.          User can add here some code to deal with this error */
    1 @! c$ s+ }2 H
  104.      delayus(100);
    * G0 x. H( Y6 I. b1 r

  105.   m7 c9 @$ b0 H4 S* p! L# o* Q
  106. # [' g" M! i* K4 u7 ~: R! J( r

  107. ! d' i% w9 o7 ]% r+ G' A, k
  108.     }, I+ @/ S$ z- R+ Z. p# M2 a
  109.       Address = Address + 2;5 G8 ]# x& z" a, [+ j) x2 j' d+ {
  110.     if (FLASH_ProgramHalfWord(Address, CONFIG_B) == FLASH_COMPLETE); h- @  |% I  m# g& a
  111.     {
    % u7 ?. G1 F2 N; N# x1 g

  112.   p/ g% U( R9 I: N
  113.     }
    0 `  m9 q! K: k6 ~4 _
  114.                 else6 ^' ?. B. f* k/ L' I
  115.     { 3 ]/ o5 I4 M: W% [5 d) t
  116.       /* Error occurred while writing data in Flash memory.
    * S; s9 X) e" M+ D! T& S5 Y
  117.          User can add here some code to deal with this error */
    + k/ o9 U, R' ?* I) F8 c7 y
  118.           delayus(100);
    . {$ q. ^; m) L9 Y8 y7 u: s
  119.     }
    5 C- v. R2 _* Y6 v
  120.     Address = Address + 2;
    ( R( O6 [6 g" S$ b
  121. - ^8 k1 e  D# R$ u
  122.     if (FLASH_ProgramHalfWord(Address, CONFIG_C) == FLASH_COMPLETE) ) X4 r4 }1 e4 L; x0 a; U: [
  123.      {: v; N3 c8 m; A5 u

  124. " b3 G/ d0 U0 p5 d5 I% I4 }
  125.     }' v/ C3 d/ X7 G+ P# ]
  126.       else5 |3 }9 O* [. X2 j
  127.     {
    / L8 h% E6 z. u
  128.       /* Error occurred while writing data in Flash memory. * f) [0 B; I( v  R1 f7 ]3 W
  129.          User can add here some code to deal with this error */
    : `# V: i; W! d; E+ c* p  G
  130.           delayus(100);) ~: E: p2 y  f7 E. \/ G$ d2 E
  131.     }6 `2 z6 |0 W3 T5 F: f2 g- I

  132. 2 V* `% @- k6 b$ A

  133. # E% p) N' l" Z/ u6 u
  134. , ?% }6 v% k5 W! R4 ~( a* ?$ I

  135. 4 @4 J9 K$ ~( q5 u5 \- h) C
  136. }
复制代码

6 S4 ?" l7 }& @( t
) V' h) M- A9 s2 j2 ]
- @+ M3 u& ]6 ~0 v9 V
赞 收藏 评论0 发布时间:2021-11-22 22:03

举报

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