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

【经验分享】STM32F0使用FLASH

[复制链接]
STMCU小助手 发布时间:2021-11-22 22:03
STM32F0使用FLASH
0 h" E& f0 J& U2 P  D! |0 K
# k. n0 J; m. g# K1 c; {
  1. , r; T7 n% Y. U- k  y4 v8 ]; U$ }
  2. /* Private typedef -----------------------------------------------------------*/
      Q( x& j2 Y# `# @$ u+ }  F
  3. typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
    * {) e3 R( o! e- s6 }
  4. /* Private define ------------------------------------------------------------*/
    5 Y  ]' m/ Z+ ^
  5. #define FLASH_PAGE_SIZE         ((uint32_t)0x00000400)   /* FLASH Page Size */1 F7 y6 e% G% F2 r5 k8 b
  6. #define FLASH_USER_START_ADDR   ((uint32_t)0x08003000)   /* Start @ of user Flash area */
    & C/ D6 @$ V" J/ Z4 l( J" v8 z2 p& H+ B
  7. #define FLASH_USER_END_ADDR     ((uint32_t)0x08003000)   /* End @ of user Flash area */
    6 D, [$ M1 L7 d, P5 L3 P
  8. #define DATA_32                 ((uint32_t)0x12345678)* f, ^  a9 t  N" d# P4 _
  9. 3 }6 @. q9 I1 M  C5 c# x1 M
  10. /* Private macro -------------------------------------------------------------*/: s9 m; O# b1 W* Y2 k8 b1 W- n, n
  11. /* Private variables ---------------------------------------------------------*/2 V: z6 _) y; ~7 ^
  12. uint32_t EraseCounter = 0x00, Address = 0x00;/ y* x9 V! U# e% \) k3 S" _" ?
  13. uint32_t Data = 0x3210ABCD;
    1 d' C2 t! g& a# e
  14. uint32_t NbrOfPage = 0x00;4 j( g2 a% w# v# a) l/ A
  15. __IO FLASH_Status FLASHStatus = FLASH_COMPLETE;7 c/ G/ o$ @! s  [/ ?( M' E. \# N
  16. __IO TestStatus MemoryProgramStatus = PASSED;1 \2 L% J8 T+ o1 o6 B, r& L

  17. " H: G" A2 @1 F
  18. /* Private function prototypes -----------------------------------------------*/
    ( |! {- H( O/ x; H+ c* g% u
  19. /* Private functions ---------------------------------------------------------*/. ], I) t% v+ |% H1 J) F% ]
  20. & H7 X, ]  q1 C( H4 k. }1 Y
  21. ! W5 |' o0 }8 M
  22. //void delayus(uint16_t u16us)   
    ! J8 O" C" t9 H( N8 c1 v
  23. //{   
    ! F9 Y8 `0 l: S2 }
  24. //   uint16_t i,j;   + u$ X: ]! Y+ m) I) I* _
  25. //   5 H" V8 {! N8 n  B
  26. //    for(i=0;i<u16us;i++)   - S8 s" z& J- E! ~7 D$ c7 c
  27. //    {   ! q1 y$ \: s# w/ Y
  28. //    //   for(j=0;j<30;j++)   " ]- z/ B5 \' o) g! h% |
  29. //     {         
    ( B& a" T4 S0 f( ^. X
  30. //            __NOP();   ; y* K4 }) Q1 X% U/ u! I( j
  31. //     }              " M7 P. I; ~0 W; \
  32. //     }     9 z. l: [0 V. o1 \( Y: T3 B
  33. //}   
    3 d& p9 ?4 F, A( C$ v, L& Y  _
  34. void FlashRead()7 Q- y$ Z9 ~$ L8 Q
  35. {! ]3 R5 I5 C. `: E* m. J* Z9 [

  36. 1 y' g% U7 M6 \* Z7 k5 D! }$ S
  37. /* Lock the Flash to disable the flash control register access (recommended
    ' x! n3 ]; Q* A$ A
  38.      to protect the FLASH memory against possible unwanted operation) *********/
    8 D/ u, X) Y- V  C: V# G
  39.   FLASH_Lock(); $ T7 |& C# [3 v8 m% ?: ]
  40. & ^3 e% G( t2 _$ w

  41.   o, R1 u6 Y% A1 S- Z! K4 Y
  42.   /* Check if the programmed data is OK
    * k/ R% L8 F8 D; I& ^- x
  43.       MemoryProgramStatus = 0: data programmed correctly
    $ m6 R& h! V3 ^' D& B
  44.       MemoryProgramStatus != 0: number of words not programmed correctly ******/* y$ B" e7 w- H- E& F
  45.   Address = FLASH_USER_START_ADDR;- [2 d! `7 Z8 v% `1 c% r
  46. // MemoryProgramStatus = PASSED; 9 `" o' G3 ~6 ]9 z+ Z3 Y" `9 ^
  47. 2 x1 v0 s# B: I5 U6 ]. y( N$ U8 Z3 F
  48.     CONFIG_A = *(__IO uint16_t *)Address;    Address = Address + 2;
    . p; g; _8 I$ ?! n9 Z
  49.     CONFIG_B = *(__IO uint16_t *)Address;    Address = Address + 2;
    0 k' ], R) o4 d$ w$ v
  50.     CONFIG_C = *(__IO uint16_t *)Address;    Address = Address + 2;% ~  x: Q: h) f9 o
  51. 8 z- S- r, f& \: U7 Z% p. d
  52. }0 x! F) k% B+ H8 ?& X3 r
  53. ( F: T3 n$ P6 S- Y5 G; }/ `
  54. void FlashSave()
    $ u8 V, d' v' [7 r* d$ j
  55. {' \' M/ S8 S3 K& Q1 U: A& c
  56.    /*!< At this stage the microcontroller clock setting is already configured, * ?2 b& j- ^8 m; l# F; _: L+ h5 |
  57.        this is done through SystemInit() function which is called from startup9 _7 H! B$ i2 O8 a& H
  58.        file (startup_stm32f0xx.s) before to branch to application main.7 E3 c; y. c  A% j
  59.        To reconfigure the default setting of SystemInit() function, refer to
    ' Y( m8 ]! j1 r/ a2 ~
  60.        system_stm32f0xx.c file% V& i* Z' h) ?6 q2 W" ]8 Z4 F
  61.      */
    - a2 f& h; j/ D! c' M( h7 B9 O, m
  62. 7 j# [' X% I3 C2 j5 O9 p
  63.   /* Unlock the Flash to enable the flash control register access *************/ - _0 g! r  m( l1 S
  64.   FLASH_Unlock();
    2 }. K- n- N- g2 O5 [4 ~5 M

  65. , X+ `+ _, Y8 i
  66.   /* Erase the user Flash area
    - a! s5 W; O" k, {: o4 Q
  67.     (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/% ]7 ?6 `& m) a5 w- j

  68. . X0 M5 g9 p6 O* @0 i) B
  69.   /* Clear pending flags (if any) */  6 Z4 m# Q# k1 f$ P
  70.   FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);
    ( |/ L* z: }# D

  71. : t: q0 {; Z' \- M1 _$ R  }
  72.   /* Define the number of page to be erased */; h9 ?% m; V. @! q. g( J" r
  73. // NbrOfPage = (FLASH_USER_END_ADDR - FLASH_USER_START_ADDR) / FLASH_PAGE_SIZE;
    $ _9 s$ O4 r' f  ~: C

  74. ) M/ t2 q& ?: z

  75. 4 l3 k( s4 c5 q& W
  76.   /* Erase the FLASH pages */# h, O5 _6 r  C. I/ n0 g
  77. // for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)' l$ ]' d2 N4 \/ U  i$ Y- {) k
  78.   {
    + x, W9 K$ N. ~. Q/ }% O8 Q
  79.     if (FLASH_ErasePage(FLASH_USER_START_ADDR )!= FLASH_COMPLETE)4 E: t3 _5 S& f" K! m- D
  80.     {
    5 T0 m. E3 _3 b0 I1 e5 Q5 C1 X  F
  81.      /* Error occurred while sector erase.
    0 I& V  n+ H& H* ?6 u9 \/ [5 I+ ~! V
  82.          User can add here some code to deal with this error  */
    & O1 o8 y8 X2 h
  83.       while (1)
    4 ^3 @5 n2 o6 M( D" q: c! Q
  84.       {7 v( O. X) m( Z6 U
  85.         TM1620_DISPLAY_DOT(999,0,PAGEB);//B999标识存储器错误
    3 J( {2 b7 N( A# D* `- E0 a) t6 e
  86.       }# w" q( _4 ]1 I: i9 u) M1 |
  87.     }
    * O8 q( w0 m4 k  C
  88.   }0 q' h) ?! b+ y( y3 |
  89.   /* Program the user Flash area word by word) A* @7 Z" k( p/ H& q
  90.     (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/0 M$ G9 I8 ~- c( N: E
  91. 6 q" g* \$ x9 r7 H8 Q5 D
  92.   Address = FLASH_USER_START_ADDR;
    7 c$ o, ~& @1 H3 N) g

  93. & m+ x) ]" ?# C
  94. // while (Address < FLASH_USER_END_ADDR)
    ' w0 d- E* ]) H) y9 {& M6 o$ C
  95. //  {$ _8 y# L8 ]' e8 X: S2 _4 A
  96.     if (FLASH_ProgramHalfWord(Address, CONFIG_A) == FLASH_COMPLETE)
    & D) W$ S% R+ x% G* y
  97.     {
    ( m/ D1 L0 T0 o* r: v  f
  98. ; U3 i( I: s5 k5 _+ M+ D- Y
  99.     }
    $ S0 m. v% k7 |) f
  100.           else9 E& r7 v) `/ C) `, O$ O# n
  101.     {
    ! I/ P( o) }$ ]8 z/ T& E
  102.       /* Error occurred while writing data in Flash memory.
    ) i8 S/ k7 l  U3 \6 ?8 v
  103.          User can add here some code to deal with this error */4 N0 x9 Z% R. I
  104.      delayus(100);8 ]+ |" O6 p3 M; p1 |8 U- l

  105. $ n3 @4 D( V0 t- X! s3 M4 j

  106. 7 [- M! b. X% O; W) c- G

  107. 5 \% S3 n% p. g0 B( ~# o$ H. w* z
  108.     }! G, z  f* m2 i. m
  109.       Address = Address + 2;
    . H; D2 o$ v0 [" _
  110.     if (FLASH_ProgramHalfWord(Address, CONFIG_B) == FLASH_COMPLETE)
    # `' t1 x. V% Z: I/ s+ V4 b
  111.     {
    7 Y0 h+ o$ ], V8 L8 n/ X
  112. - z, B" x. H9 @. K
  113.     }" {  y8 B& }! y* g0 ?4 i/ ^
  114.                 else
    6 y" n$ e' B' h8 X; D
  115.     { $ e0 u9 F) K" g
  116.       /* Error occurred while writing data in Flash memory.
    , t5 s+ G1 h9 \) g/ z
  117.          User can add here some code to deal with this error */
    ' ^% u, Y3 Z; E: {7 ?
  118.           delayus(100);* Q, W: u8 B5 i$ `0 D' C! c
  119.     }
    * E# I, }0 K; V0 f: r  H9 p- e% B/ C
  120.     Address = Address + 2;
    / W! p7 W9 x6 Q. a! ~7 {: a
  121. 3 K6 A1 ~3 C& P- M1 _; F' q% [7 d
  122.     if (FLASH_ProgramHalfWord(Address, CONFIG_C) == FLASH_COMPLETE)
    # l) j6 \( M$ R7 a
  123.      {$ @" Y2 Y- \4 G8 D& C

  124. / O0 u/ i1 h$ L, S% y
  125.     }/ l0 M3 Y/ N2 J5 d1 S
  126.       else2 X2 a6 V. H4 \) @8 I
  127.     {
    9 C: N- M: W& w- f! W
  128.       /* Error occurred while writing data in Flash memory.
    * l4 Z( n7 ~& o7 W2 ~% A; @
  129.          User can add here some code to deal with this error */* m. Z, T' B, g' b+ Z! z$ G
  130.           delayus(100);
    - Y; l) P" f3 ?
  131.     }% {9 E# i8 Q7 A" j- p

  132. 5 E1 n7 R9 l! S% i1 E

  133. 6 N, ?" ~8 `; R3 U# r$ M5 J
  134. % R5 O5 J: U  p( `
  135. ! ~; F4 {  }/ e2 s" S# B. D* ^7 V
  136. }
复制代码

/ R8 o1 m  t6 z3 S" N* h* U* C+ y# B4 S$ P9 Q8 J

' q# V, X  |6 k! S6 C. W' c! d! _
收藏 评论0 发布时间:2021-11-22 22:03

举报

0个回答

所属标签

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32Cube扩展软件包
意法半导体边缘AI套件
ST - 理想汽车豪华SUV案例
ST意法半导体智能家居案例
STM32 ARM Cortex 32位微控制器
关注我们
st-img 微信公众号
st-img 手机版