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

【经验分享】STM32F0使用FLASH

[复制链接]
STMCU小助手 发布时间:2021-11-22 22:03
STM32F0使用FLASH  q- y( A0 I' L# i$ G- t
0 V1 n9 K' o; n  }& ]5 K7 d9 @8 v8 c

  1. 9 T' ]* T' m* a( O
  2. /* Private typedef -----------------------------------------------------------*/( H1 a5 l9 y2 B
  3. typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
    * W. R6 v) S  L% t4 j
  4. /* Private define ------------------------------------------------------------*/
    1 U: N3 w" j5 k. U
  5. #define FLASH_PAGE_SIZE         ((uint32_t)0x00000400)   /* FLASH Page Size */
    $ ]( l7 F9 _& L* B
  6. #define FLASH_USER_START_ADDR   ((uint32_t)0x08003000)   /* Start @ of user Flash area */: f$ M" R/ k4 L% z9 L2 d
  7. #define FLASH_USER_END_ADDR     ((uint32_t)0x08003000)   /* End @ of user Flash area */) F: N0 w# J' b! E& H5 ?; g5 w
  8. #define DATA_32                 ((uint32_t)0x12345678)
    - [1 j. \8 }( m6 c( E

  9. # _+ e& E% Y$ g) e; v
  10. /* Private macro -------------------------------------------------------------*/' G2 z, n  v6 g9 ?! X
  11. /* Private variables ---------------------------------------------------------*/
    0 g) Z7 D2 g* n( J; Q
  12. uint32_t EraseCounter = 0x00, Address = 0x00;# D; |' F: [( }/ X
  13. uint32_t Data = 0x3210ABCD;; Z  v" q0 H5 s
  14. uint32_t NbrOfPage = 0x00;
    / b' X# V, |' J) H8 |& ]3 R0 E1 ^
  15. __IO FLASH_Status FLASHStatus = FLASH_COMPLETE;3 g! U6 p- f; C. {& ^
  16. __IO TestStatus MemoryProgramStatus = PASSED;
    ! Y( P' w+ N8 A9 b+ Q8 @7 r
  17. . s7 s. t0 p3 u5 j- B
  18. /* Private function prototypes -----------------------------------------------*/+ [( |7 v5 p& L% y
  19. /* Private functions ---------------------------------------------------------*/
    8 c" N/ [% S4 @
  20. / n  Q" g' w- p+ D; F

  21. 8 u8 I! }  }* I9 G2 X
  22. //void delayus(uint16_t u16us)   
    , g& C+ Z  T, p- w  b! |6 Y
  23. //{   
    ; t5 M+ d  ]$ E% r& |
  24. //   uint16_t i,j;   9 o7 e/ D4 z, I' ]
  25. //   ; b- e# \' p$ E4 c  o
  26. //    for(i=0;i<u16us;i++)   & y( I/ S* ~/ x, B; {) s
  27. //    {   
    ; [. S+ v+ G/ u" _+ K9 j# f! N: \8 i
  28. //    //   for(j=0;j<30;j++)   
    8 d, m+ t9 i9 d. L7 U6 g6 U# A
  29. //     {         
    8 e8 Z! [9 ~3 `" _% g  t
  30. //            __NOP();   % E% M# Q+ w! X1 D6 ]. b, D5 x9 `
  31. //     }              # ~' ?3 w$ _# X5 W
  32. //     }     , a" ]" v" n! I  e" D' d
  33. //}   
    . K% C) g. l0 S( J* [; O0 h
  34. void FlashRead()
    0 g, y( {1 s" P- f6 }
  35. {9 u* d( `. b, k/ }3 h( f' y

  36. ( m8 q+ R4 B5 s: x3 Y# c
  37. /* Lock the Flash to disable the flash control register access (recommended* ?  w* X9 v' n, K" ?
  38.      to protect the FLASH memory against possible unwanted operation) *********/; c* c5 f) ?2 b
  39.   FLASH_Lock(); ! ?, U% b' M8 Y" l# `! j. h

  40. & p; W! S/ \# b, i, ^, d, m

  41. 4 P$ u5 |& v: L! d  k/ x
  42.   /* Check if the programmed data is OK
    , U; v! }6 I/ Z; B* M
  43.       MemoryProgramStatus = 0: data programmed correctly
    # c$ q  q7 _; s" d9 v% M7 [
  44.       MemoryProgramStatus != 0: number of words not programmed correctly ******/: Y' O. x# v( M5 o1 p
  45.   Address = FLASH_USER_START_ADDR;9 v/ v5 Y6 g% o! Y2 Q
  46. // MemoryProgramStatus = PASSED;
    / S" S7 g6 @$ k2 z. X" s* i: j* {

  47. 9 f" ~8 W! v7 w1 x- O0 t# U
  48.     CONFIG_A = *(__IO uint16_t *)Address;    Address = Address + 2;
    " e. N# @1 F0 }5 g
  49.     CONFIG_B = *(__IO uint16_t *)Address;    Address = Address + 2;
    ; A; a( L" o* w7 ?$ Y) \* H9 q
  50.     CONFIG_C = *(__IO uint16_t *)Address;    Address = Address + 2;
    $ B% T- _; f6 Z  ^- n. T# j

  51.   f' Z0 V' J. C* E
  52. }
    ( F& \' |& ~% t5 {3 [! H

  53. 6 f* t: w3 I' e# ~* ]7 ~
  54. void FlashSave()
    $ U  }& H; A/ Z) \: k5 ]
  55. {' f- a1 P6 P8 S! ]. L, k
  56.    /*!< At this stage the microcontroller clock setting is already configured,
    8 A  ^1 Y$ A, ^" C
  57.        this is done through SystemInit() function which is called from startup
    . t+ y9 @+ _  a5 y- ~$ Q  ]" }/ f% x
  58.        file (startup_stm32f0xx.s) before to branch to application main.1 W" d- E, b8 T' O& e
  59.        To reconfigure the default setting of SystemInit() function, refer to
    * S6 d2 F2 s# ]$ Q8 R. ?
  60.        system_stm32f0xx.c file8 x8 `; P: e4 T$ b: S
  61.      */
    0 m& r, H% l* _8 A+ f: L( N
  62. , H3 U% m2 Z/ A  g" B
  63.   /* Unlock the Flash to enable the flash control register access *************/ 0 O. l; d  s* R! {! J1 g
  64.   FLASH_Unlock();8 C0 u9 t3 J+ ^6 t8 I2 Z" t
  65. , A/ h# a7 {$ G4 r4 w4 M7 H
  66.   /* Erase the user Flash area
    ! R8 r' z  y! O
  67.     (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/+ K2 t4 p) ?6 f& F8 s0 y6 q
  68. # z" [% b& [3 T8 f/ ~! N
  69.   /* Clear pending flags (if any) */  
    # }4 m$ T/ s' ~
  70.   FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);
    ' K) [7 |# E/ _1 z1 V& X
  71. 9 l# P4 `. z2 M
  72.   /* Define the number of page to be erased */. x. D7 s6 s) Q# p) R) P
  73. // NbrOfPage = (FLASH_USER_END_ADDR - FLASH_USER_START_ADDR) / FLASH_PAGE_SIZE;
    + w0 R/ M  `. p9 p! J, G; I

  74. , `6 @! `7 c7 R) R  C: R' U4 B# z

  75. 3 D7 I! w( p3 ]. ^
  76.   /* Erase the FLASH pages */
    ) X2 {1 H4 _6 j, c' e
  77. // for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)! g" o( N/ Y5 b! o
  78.   {- S5 B; x4 q* z+ r/ N  S
  79.     if (FLASH_ErasePage(FLASH_USER_START_ADDR )!= FLASH_COMPLETE)
    1 `( z3 q2 `/ {
  80.     {& h; {2 o/ z. x# S% j, y) T
  81.      /* Error occurred while sector erase. . \. P  q4 b: p3 w7 S; b0 D
  82.          User can add here some code to deal with this error  */( L& ?1 O6 w3 N0 e9 i
  83.       while (1)" W$ N7 k8 }5 s4 x" D
  84.       {4 o4 h1 @( P' a7 M$ T9 V" e; ^
  85.         TM1620_DISPLAY_DOT(999,0,PAGEB);//B999标识存储器错误4 F; s1 l6 c% n- c
  86.       }1 t' j6 Z/ W( W, x
  87.     }
    9 l* X, M; m8 ]0 o; D- Q% l4 b! q% p
  88.   }' D/ O  p7 t) o" {
  89.   /* Program the user Flash area word by word
    $ v/ G) N; g9 [' n$ w( R. J
  90.     (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/( k9 ?( o5 k* b$ N( t

  91. , ]( }! S3 q7 V! S* d( M; K5 o
  92.   Address = FLASH_USER_START_ADDR;
    4 }; Y5 {+ ]' P& H! Z3 P

  93. * E, M3 F0 F4 c# R0 V
  94. // while (Address < FLASH_USER_END_ADDR)/ S7 f9 l' G" C; h
  95. //  {( T; e. W* p& |7 P( X+ c
  96.     if (FLASH_ProgramHalfWord(Address, CONFIG_A) == FLASH_COMPLETE)
    0 m6 Q( V: i! C+ U/ r1 ?; h- w
  97.     {
    ; }1 j# O4 d  o* {8 H
  98. ) _5 ^& S, {) W
  99.     }
    * Z% m& V/ U" z7 b
  100.           else1 d- n) }. @+ m. a+ S
  101.     {
    - p3 Z) l/ ~" M3 V/ N) L  N
  102.       /* Error occurred while writing data in Flash memory. # n& T$ l8 @* R5 Z
  103.          User can add here some code to deal with this error */0 W* x2 z+ b# T3 L/ I8 [
  104.      delayus(100);/ o/ z, E+ E7 F* V5 V

  105. 0 H( [/ P$ a( f+ [: d2 x9 |

  106. ( N3 k. [1 i- v+ W5 e7 @+ K" i

  107. & z7 A5 y* j& F" N3 k* s+ V2 J
  108.     }
    - V$ [( d2 `4 s, `, U6 V% T; i
  109.       Address = Address + 2;5 B. x. ^1 @  O
  110.     if (FLASH_ProgramHalfWord(Address, CONFIG_B) == FLASH_COMPLETE)
    / K. T' Q4 F  }' r; ]
  111.     {
    3 _% f5 B4 ?$ x

  112. % s# f0 v' w. y' w7 C3 W
  113.     }
    / S* _/ y" [8 H' T* x
  114.                 else7 x" k1 q3 s- G/ i) L$ {; P4 i
  115.     {   d9 }* I& j& R6 t1 k& H
  116.       /* Error occurred while writing data in Flash memory. ' X) B2 q  r$ o% Q( O6 R
  117.          User can add here some code to deal with this error */
    / Y, N; j  d7 X( L
  118.           delayus(100);
    ; u9 }' `" i' l, w- b
  119.     }
    ; w+ a3 t8 r. F" B; X$ {, Z
  120.     Address = Address + 2;
    / j8 H/ L# B- I7 ]; \3 M6 H1 c, t

  121. : @5 N3 f0 E% a4 X% l9 Q: X
  122.     if (FLASH_ProgramHalfWord(Address, CONFIG_C) == FLASH_COMPLETE) & j4 |! w) a. W& R& C3 s9 {% G% [
  123.      {
    9 g/ d' |. G7 `2 V

  124. * ]  j1 i& S% E- R$ n
  125.     }( j4 q- ~( Y3 X) G
  126.       else
    0 q8 X3 z$ a0 R
  127.     { - P$ ^( \+ g6 n
  128.       /* Error occurred while writing data in Flash memory.
    7 Q2 }; g0 F, s: F  u
  129.          User can add here some code to deal with this error */) ?' F5 z( Q2 ]- G/ H6 ~1 c" \
  130.           delayus(100);8 K! e) M! ]$ d' d
  131.     }2 d9 j, m% t/ w; X8 z

  132. & s+ M( A# A; v

  133. % i, m- u+ s) }( F* r0 P$ ~7 p
  134. 8 F/ i+ s0 t2 x! F. A& v
  135. - L  ~  Y: |. j9 A0 S8 O
  136. }
复制代码
# S( E9 u  Q9 z8 r

! A5 W; F; Z, B4 i. i! L. M8 r: ?; m) O  [+ o6 }4 E7 U
收藏 评论0 发布时间:2021-11-22 22:03

举报

0个回答

所属标签

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