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

【经验分享】STM32F0使用FLASH

[复制链接]
STMCU小助手 发布时间:2021-11-22 22:03
STM32F0使用FLASH
3 i6 w, F& D3 Z  O" _, l/ E. s+ o
: G& i( ~- m) i. E6 r

  1. : i+ F9 R, X" p+ r( h5 w' u/ [" e% d3 V& \
  2. /* Private typedef -----------------------------------------------------------*/
    1 J! Z2 ^% a5 E# P$ j6 O
  3. typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
    8 L  r) P4 ?# ~6 V6 E) g5 |0 j
  4. /* Private define ------------------------------------------------------------*/# }5 B4 M8 ]6 H5 r" a' x0 B! E
  5. #define FLASH_PAGE_SIZE         ((uint32_t)0x00000400)   /* FLASH Page Size */* b' f+ I- ?4 q* T$ d" f
  6. #define FLASH_USER_START_ADDR   ((uint32_t)0x08003000)   /* Start @ of user Flash area */
    8 y! i$ Q4 ?0 |2 X
  7. #define FLASH_USER_END_ADDR     ((uint32_t)0x08003000)   /* End @ of user Flash area */
    # C( E' D4 G: R
  8. #define DATA_32                 ((uint32_t)0x12345678)3 |6 k- j8 a) W$ r1 v

  9. % `8 d/ ~: n) Y6 D- K
  10. /* Private macro -------------------------------------------------------------*/
    ' ^- F. r4 |/ ]6 ^8 s9 {5 \
  11. /* Private variables ---------------------------------------------------------*/3 p( ?7 H1 V$ k7 S
  12. uint32_t EraseCounter = 0x00, Address = 0x00;2 f2 @4 T2 t' [% A
  13. uint32_t Data = 0x3210ABCD;
    + e! F7 u! X+ `( z8 m- U7 L1 B
  14. uint32_t NbrOfPage = 0x00;1 t; f8 l, u( \6 {# V
  15. __IO FLASH_Status FLASHStatus = FLASH_COMPLETE;
    . H  N+ k$ B8 L  t1 a
  16. __IO TestStatus MemoryProgramStatus = PASSED;8 j* V- Q2 M. x+ O3 u, g) u
  17. 0 j( r' M' b) o
  18. /* Private function prototypes -----------------------------------------------*/$ i3 \0 h. `7 u8 O$ W8 r
  19. /* Private functions ---------------------------------------------------------*/$ Y# {$ \) u' _& s4 X8 D
  20. ) i+ B2 H: U9 |( @  t0 d

  21. ! U# ?  V( h. O9 Q& O# e
  22. //void delayus(uint16_t u16us)   
      @5 R# B3 r' X3 S4 E
  23. //{   
    7 c1 S$ ^0 V7 X* a7 j  }
  24. //   uint16_t i,j;   
    ! i5 I4 c, S4 [: M& l4 C
  25. //   
    ( b1 j( N- _7 ?# M4 y
  26. //    for(i=0;i<u16us;i++)   
    * `1 g& a2 e+ p2 V' Q' K
  27. //    {   
    4 j/ j4 x, b. h" }$ i) S# g' s
  28. //    //   for(j=0;j<30;j++)   
    " |" }) f' _7 J, k8 y7 I
  29. //     {         4 n0 x9 B) R0 M  ~
  30. //            __NOP();   / _/ u! z3 F5 q" q! p
  31. //     }              
    . k/ y& Z. R4 S+ |6 w# x4 ]
  32. //     }     
    5 h; z& H$ P' \+ M# s+ b
  33. //}   5 E6 Q( I, M3 `1 Y7 k
  34. void FlashRead()
    . y  ?9 n9 ^; d5 |: I" y7 o
  35. {5 @- e% r8 a& z  p

  36. " ~/ z6 B6 u! {/ g, ~$ R6 ?
  37. /* Lock the Flash to disable the flash control register access (recommended5 y% @7 U! q/ O4 _1 ~4 R! B0 E
  38.      to protect the FLASH memory against possible unwanted operation) *********/, w: }2 B* x* p- @, y: q* {( o
  39.   FLASH_Lock();
    8 t/ J1 b% p$ E( {
  40. ( F4 D3 j! O: R2 h
  41.   l, z, h! F" f9 F
  42.   /* Check if the programmed data is OK
    $ b/ `2 Y6 R, o
  43.       MemoryProgramStatus = 0: data programmed correctly
    $ O* ~% F- p) R
  44.       MemoryProgramStatus != 0: number of words not programmed correctly ******/% J0 c8 m( j8 L, H6 s+ @4 [
  45.   Address = FLASH_USER_START_ADDR;
    ) y$ o% ^" _2 X& X/ ^3 ^# U5 K
  46. // MemoryProgramStatus = PASSED; 3 f3 e$ y0 x$ D3 `
  47.   d$ j" ^4 v0 i, b6 B1 ^. T
  48.     CONFIG_A = *(__IO uint16_t *)Address;    Address = Address + 2;
    0 l2 l/ l) P3 E$ \+ G) Z! B
  49.     CONFIG_B = *(__IO uint16_t *)Address;    Address = Address + 2;6 {3 M- o, ^1 N1 `7 H  p2 V
  50.     CONFIG_C = *(__IO uint16_t *)Address;    Address = Address + 2;9 {' U& x! y3 n; H1 t/ j
  51.   ?/ g. u/ m1 I( o) Q. b( y. D
  52. }
    6 X4 ~, i( e9 D! p1 f

  53. 5 a$ j: L( L/ s
  54. void FlashSave()
    " M/ Z. o3 d% ^/ W" j+ Q
  55. {2 M/ u( m2 u9 P
  56.    /*!< At this stage the microcontroller clock setting is already configured,
    ! O# ], {' c# ]! v1 p# O* B2 ^
  57.        this is done through SystemInit() function which is called from startup$ B  X$ R/ x% A" H: {8 @
  58.        file (startup_stm32f0xx.s) before to branch to application main.& I6 q; k% s6 A
  59.        To reconfigure the default setting of SystemInit() function, refer to# M% _0 g9 }; I
  60.        system_stm32f0xx.c file
    4 U% V" t6 o1 b
  61.      */* G  D$ R" l. n3 S

  62. 9 O9 ~4 y5 C2 Z/ j; U7 K+ s
  63.   /* Unlock the Flash to enable the flash control register access *************/ ! ?* I' A% q5 c: O" H
  64.   FLASH_Unlock();$ e! o, w7 q. q1 R
  65. 5 S; m2 C0 ]& a6 f
  66.   /* Erase the user Flash area
    + u! ^: X! [  C  s# Y& f/ m6 E
  67.     (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
    * l2 G+ q% W, _) w4 ~( W
  68. 1 |7 I) s/ b, i0 l6 }8 {' A# V
  69.   /* Clear pending flags (if any) */  
    ! [# f8 |3 j0 D* m0 s8 d/ t
  70.   FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);
    $ b" E3 Q/ Z6 H3 ^2 [
  71. % x$ P  T8 a, U+ b& b, z. N
  72.   /* Define the number of page to be erased */
    ( |( |7 ^( ]6 z  T* q
  73. // NbrOfPage = (FLASH_USER_END_ADDR - FLASH_USER_START_ADDR) / FLASH_PAGE_SIZE;9 C, a& r/ c- a3 X5 n$ A& u; x( u

  74. + B* P$ }/ t) |" `- `

  75. . _$ ]% D( n% ^/ m1 U, [' f" l
  76.   /* Erase the FLASH pages */
    . R. f( G6 r1 \: H
  77. // for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)
    $ q! e! h8 ^/ O! a; Y* ?
  78.   {
    & M2 a8 V2 g0 N* P$ K2 c
  79.     if (FLASH_ErasePage(FLASH_USER_START_ADDR )!= FLASH_COMPLETE)
    5 C0 |4 @; x$ f
  80.     {/ V! l( c; o2 u8 S8 L, q
  81.      /* Error occurred while sector erase.
    # K: ^. }" S9 W0 V6 n! |
  82.          User can add here some code to deal with this error  */
    ! d) z, E( o* {8 y, B
  83.       while (1)# p$ A, B1 U, p3 W8 d& Q, q
  84.       {4 ~# h- y5 Z$ `
  85.         TM1620_DISPLAY_DOT(999,0,PAGEB);//B999标识存储器错误
    # e; R# f+ o6 Y. }- ~8 l+ U
  86.       }: `& p; U( g+ t" g0 X% A
  87.     }
    . p" Z* J% m$ X+ u0 t
  88.   }
    7 k% \1 h6 A/ G
  89.   /* Program the user Flash area word by word4 [6 S7 k" E, ]. t( X5 H* ~/ z
  90.     (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/# T6 v5 v  P& M+ `

  91.   {3 _5 ?! c. q4 S( b# \" b2 Y
  92.   Address = FLASH_USER_START_ADDR;
    " J" k5 [% _5 H# L1 T5 f
  93. $ f! Q' {. F- X) b
  94. // while (Address < FLASH_USER_END_ADDR)
    & B$ u& C1 E& E1 _6 X0 c
  95. //  {; Q0 W4 x' j  ~
  96.     if (FLASH_ProgramHalfWord(Address, CONFIG_A) == FLASH_COMPLETE) ) j. H# N1 c( O
  97.     {8 i; u; G7 m) \; v7 X

  98. ! P$ B  z/ T2 q3 B, d) E& w' g8 M
  99.     }+ p# _. ~& ]3 m3 g% p
  100.           else
      X/ o# P& V$ Q5 T
  101.     {
    ' E7 ?6 X4 |- ]) ?) @/ p
  102.       /* Error occurred while writing data in Flash memory. & j! n" G4 @! N0 x
  103.          User can add here some code to deal with this error */, M' n# w" R6 f, s
  104.      delayus(100);" y$ N$ b) M  p) c: g! d

  105. ! R4 [6 L6 l; O$ Y% \9 W5 I
  106. - ?, P# ^' t! @& @: R
  107. ; \# K' y# `8 O2 k- Y
  108.     }' S- f; P+ m  b; s/ P' v: m" I
  109.       Address = Address + 2;
    ( l5 o* B3 T( J' f
  110.     if (FLASH_ProgramHalfWord(Address, CONFIG_B) == FLASH_COMPLETE)9 D' w+ O2 I# B# M
  111.     {6 D7 x0 S- U/ w/ v8 K. u

  112. " D* f+ Q" P6 \8 ~
  113.     }" n1 p" D* d! j: P6 \
  114.                 else
    ( ^- F- {$ W7 ]4 l6 Q
  115.     { ! D) z, X  q4 L/ W1 T9 M, [. D
  116.       /* Error occurred while writing data in Flash memory. 9 w5 ?) T- f* e- B7 h
  117.          User can add here some code to deal with this error */
    5 h, y# k/ C0 ]% n0 C7 }
  118.           delayus(100);2 E: }5 r. o/ }5 I+ y6 A) o
  119.     }
    6 ?" \) k& i& u$ N8 G% p
  120.     Address = Address + 2;
    ) D; W% J2 }2 M& u: u; N; R
  121. 8 \; r  m9 [( h
  122.     if (FLASH_ProgramHalfWord(Address, CONFIG_C) == FLASH_COMPLETE)
    5 l  D( O0 l7 U- t- ~  t+ P. P) B+ H
  123.      {
    9 R4 T- S9 ]# C6 e, I8 K

  124. 1 I, }0 E( G6 ^, |" \
  125.     }) \/ W; i+ s) d# c: Y* x( F
  126.       else4 j. t' v1 J& J+ e- W# H
  127.     {
    $ V6 x$ v. [  [# W
  128.       /* Error occurred while writing data in Flash memory.
    ; v* R( U" d6 V/ I" M. V* A
  129.          User can add here some code to deal with this error */
    - c* c8 W3 N& W& m
  130.           delayus(100);. h: ^$ l) Z. Z9 X- w2 W2 C+ b
  131.     }
    / ]. ]/ }0 \) G- M! i4 ^; B$ V
  132. 1 y4 v4 R$ w/ g1 G

  133. % u& C4 S& i# B6 s, C. b0 w. q

  134. . w5 ^/ h. \; x% i- P7 ~
  135. 5 _% V5 S+ Q0 ?$ `2 e
  136. }
复制代码
; O& F; y5 S; n9 Q
- U+ p) t8 v2 W6 u# E; V3 _

) z. ?' }. y) q+ G7 k' c" f
收藏 评论0 发布时间:2021-11-22 22:03

举报

0个回答

所属标签

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版