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

【经验分享】STM32F0使用FLASH

[复制链接]
STMCU小助手 发布时间:2021-11-22 22:03
STM32F0使用FLASH
9 v0 q# L' |& i3 _0 f% @  v$ c( W) N1 ]& [& q9 i& u

  1. . N6 w9 n/ ^2 f% ?: W- R
  2. /* Private typedef -----------------------------------------------------------*/
    7 m: G/ P% r/ V& R- j* f
  3. typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
    ; b7 H2 U- L: x1 b$ q) t& n
  4. /* Private define ------------------------------------------------------------*/
    8 C" V1 k8 S( G# N7 |1 T- N$ ~
  5. #define FLASH_PAGE_SIZE         ((uint32_t)0x00000400)   /* FLASH Page Size */) V) L; y0 |# }' h
  6. #define FLASH_USER_START_ADDR   ((uint32_t)0x08003000)   /* Start @ of user Flash area */4 R! R6 L( m+ @: J
  7. #define FLASH_USER_END_ADDR     ((uint32_t)0x08003000)   /* End @ of user Flash area */
    + G& R7 a" r" d  R
  8. #define DATA_32                 ((uint32_t)0x12345678)7 ]: D/ H! M( j- c: |
  9. 9 f; x* o6 m' }; S2 p/ ^2 H
  10. /* Private macro -------------------------------------------------------------*/
    % H3 f- y% W6 H6 m% _$ I& U
  11. /* Private variables ---------------------------------------------------------*/: O% K+ I" x( n3 n$ ~  {0 v
  12. uint32_t EraseCounter = 0x00, Address = 0x00;5 Z3 T1 y6 E2 M8 q/ a  n  b
  13. uint32_t Data = 0x3210ABCD;
    . u/ A6 m1 {6 i* N. a8 U
  14. uint32_t NbrOfPage = 0x00;
    $ J: r0 D# e5 H$ P
  15. __IO FLASH_Status FLASHStatus = FLASH_COMPLETE;
    9 d# V% x8 S! T- q
  16. __IO TestStatus MemoryProgramStatus = PASSED;
    & M3 ^0 }1 w: T( k" ~) Z1 x

  17. - B- a0 W6 `% ^* `+ B' ~9 v
  18. /* Private function prototypes -----------------------------------------------*/
    : t' e/ j7 }9 O
  19. /* Private functions ---------------------------------------------------------*/
      P$ v9 {# C* ^3 F1 k
  20. ( n( u6 Z1 V, C7 M

  21. 4 J3 N5 [' F) G8 c( \# a8 Z; Z
  22. //void delayus(uint16_t u16us)   , b/ P3 P7 I5 p1 A3 f  s
  23. //{   5 A) O+ {2 o; O+ i: m( J1 j
  24. //   uint16_t i,j;   
    ( `. ^9 l$ g+ k: e( K
  25. //   % L) h( Z+ J4 w( K; F" S
  26. //    for(i=0;i<u16us;i++)   
    & ]- F3 g3 B/ W. Y! S3 O
  27. //    {   7 ]0 {1 n" e8 l. a& K; E! b
  28. //    //   for(j=0;j<30;j++)   
    & ]. @% m) B  s+ `! u0 P( V
  29. //     {         
    5 F3 c- W, I% F
  30. //            __NOP();   8 |. _6 s' ^$ w- |( B- h: b6 {
  31. //     }              
    , o: u% u/ |8 h1 c, z3 S) U5 w! A
  32. //     }     % \7 }+ [) Z$ A* P
  33. //}   
    6 V% z" Z- x8 Q% K. m3 B! K6 ?
  34. void FlashRead()
    $ y; P+ U! }- }; r8 }9 _6 X
  35. {
    ! D6 ~/ q' e  t+ C% b% g

  36. , U" W$ x/ \. {3 `& I' o( r  e, i: |
  37. /* Lock the Flash to disable the flash control register access (recommended
    ( B- ^7 X. ?5 K" r
  38.      to protect the FLASH memory against possible unwanted operation) *********/
    9 o  O  v9 Z1 r4 A. N# ]
  39.   FLASH_Lock(); 5 q* C7 M2 A7 i% V7 |4 N* T9 _
  40. 9 n* g5 w# S/ {* F

  41. ) m# Y$ L6 X5 s) M/ ~' O
  42.   /* Check if the programmed data is OK
    * c$ @1 t* _+ K! b' `
  43.       MemoryProgramStatus = 0: data programmed correctly$ N" f. Z' U. D
  44.       MemoryProgramStatus != 0: number of words not programmed correctly ******/( ~2 R* c1 M* ?" l/ V2 |! b, f1 @1 e  u
  45.   Address = FLASH_USER_START_ADDR;) Z9 g& P! n! n) S
  46. // MemoryProgramStatus = PASSED;
    . x5 m$ u/ B- D  s
  47. & `6 Z+ Z7 W3 E, C3 s: D- L
  48.     CONFIG_A = *(__IO uint16_t *)Address;    Address = Address + 2;7 Q4 |( L# J! @  {5 M+ W7 Q" B
  49.     CONFIG_B = *(__IO uint16_t *)Address;    Address = Address + 2;% y' i" d- l$ c, O: f- p) M3 p
  50.     CONFIG_C = *(__IO uint16_t *)Address;    Address = Address + 2;
    5 B+ L" m' g2 @( j  T5 d1 K4 N8 J

  51. - K# d( c* k# H' l( |
  52. }
    # @& Q7 g/ g+ H/ `& Y1 w
  53. 4 ^5 h8 U) o, m3 ]1 j6 Q$ y# i6 t
  54. void FlashSave()# l* }5 a' E% l
  55. {
    ) |! ?+ Z* ]3 O  _
  56.    /*!< At this stage the microcontroller clock setting is already configured, 7 Y3 _3 W! o* X/ L* W( G
  57.        this is done through SystemInit() function which is called from startup
    $ W; h  W. _- `' E5 z
  58.        file (startup_stm32f0xx.s) before to branch to application main.
    ; D5 m8 Q) O0 O, t
  59.        To reconfigure the default setting of SystemInit() function, refer to
    $ t7 K8 A( L6 S/ P% V
  60.        system_stm32f0xx.c file
      z2 i! l1 j6 H9 r6 q6 T, i
  61.      */
    0 ]' @+ r7 v5 R0 U3 m, R% X) ?% m) f

  62. ' m; T# L% B2 j: u! f
  63.   /* Unlock the Flash to enable the flash control register access *************/ 6 ~* o+ \" H1 B$ K; w! Y
  64.   FLASH_Unlock();
    ! L4 }+ p  u# g; r0 C

  65. + c  q" o# O, C/ O" {) ?
  66.   /* Erase the user Flash area' a% S/ T" x, ~. b9 Z, E
  67.     (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
    3 e, F1 n# ?8 M$ ]7 }2 ^% Q' Y
  68. 8 {5 Z2 n9 J" m  D) r: s0 D7 y
  69.   /* Clear pending flags (if any) */    E# l, |+ U* G, ]" r+ `6 e
  70.   FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);
    % p  o) i9 r  a$ H8 J; X

  71. 9 ?- i( V, b: M/ `2 T( p/ r
  72.   /* Define the number of page to be erased */' q5 z  W$ `7 s+ ]9 }9 T
  73. // NbrOfPage = (FLASH_USER_END_ADDR - FLASH_USER_START_ADDR) / FLASH_PAGE_SIZE;. h+ a: |# b! m2 q* ^- C  I! W
  74. * A  F" m; _7 t. N6 L

  75. # L5 F2 @/ P% p: W& V, g; G# n
  76.   /* Erase the FLASH pages */
    ; k8 a& G: B8 Y) T( S$ ]9 D
  77. // for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)# X, a) W( w1 q, Y0 U* u& r+ j. a; f* U
  78.   {* H4 O" y; [- D  A
  79.     if (FLASH_ErasePage(FLASH_USER_START_ADDR )!= FLASH_COMPLETE)/ i" e1 n( u( d1 \) l" A6 a- }
  80.     {
    7 i- m; s2 f$ _: }! s, J8 Q# P
  81.      /* Error occurred while sector erase. , u  X2 x) B$ X% h
  82.          User can add here some code to deal with this error  */2 t" y! U( a- @# F. _
  83.       while (1)
    . [9 I$ ~' m! a0 y4 R
  84.       {" B- L2 V" |: [( i# e$ x5 v
  85.         TM1620_DISPLAY_DOT(999,0,PAGEB);//B999标识存储器错误
    ! M- J( s# x3 l7 {6 {; z7 O
  86.       }, E: a2 ~3 S5 x
  87.     }' \" L4 ~) V0 W: _# |8 R8 C3 E1 [
  88.   }
    + g- ?) z" ^. C! u
  89.   /* Program the user Flash area word by word7 z7 I3 T+ c! [  F1 p8 `2 s
  90.     (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
    4 Z: J8 @/ |5 p( F1 c& `; r( K

  91. : R- Z+ {; Y* }$ V: L8 {4 |
  92.   Address = FLASH_USER_START_ADDR;
    ; E& O4 F: n# G5 q
  93. 6 `; N) k6 B% d) U6 n, X6 |
  94. // while (Address < FLASH_USER_END_ADDR); {, O1 M0 M+ K. K% I% I
  95. //  {
    . M- Z( X# R% r! C
  96.     if (FLASH_ProgramHalfWord(Address, CONFIG_A) == FLASH_COMPLETE)
    , h: Q/ x5 o5 m; K; h+ O; ^- P
  97.     {' p8 A5 V7 e3 m9 N- Q
  98. 6 _7 K  p2 z7 G5 H
  99.     }
    1 ?" n4 G7 @! G- N
  100.           else# s; e0 a7 K7 y' x+ v. L
  101.     {
    ' V- U" d+ w6 p/ z% g, ^. @3 Z
  102.       /* Error occurred while writing data in Flash memory. ' W; \9 y+ Y9 C6 |8 b
  103.          User can add here some code to deal with this error */; C+ s7 y$ E! L; i' }9 y7 P
  104.      delayus(100);& M$ \) _* y- ^. ?3 a5 u  `

  105. # X: l4 h3 p' Y- [! M9 L
  106. 3 B+ M! e" `; U  t
  107. - ]+ O- s! R! c: y0 u2 _: z
  108.     }& v: n3 w( G# b% H) X. u, _9 n
  109.       Address = Address + 2;% e6 |  }  [" f* h, z( u
  110.     if (FLASH_ProgramHalfWord(Address, CONFIG_B) == FLASH_COMPLETE)
    9 k1 d3 W5 g! r- `+ W! `
  111.     {' E& t& F4 Q2 v6 I: C( ~

  112. 7 R  Y" f- C& [  |: I6 N3 E* H# F
  113.     }
    / i8 t4 S2 i5 y: ^
  114.                 else; L/ v* Y& y0 b9 t
  115.     {
    1 u3 J1 u4 ?1 c( K! l
  116.       /* Error occurred while writing data in Flash memory. 7 V6 }7 e1 t. g/ U5 L- `
  117.          User can add here some code to deal with this error */
    8 I/ B/ }+ |7 I# L
  118.           delayus(100);
    ) A, U* _: T% t' o% h) ]* ^& P* J2 n
  119.     }
    ) G& N+ R& b2 k' _# B
  120.     Address = Address + 2;( d$ O+ T* ~3 e+ V
  121. ) p- {1 k, b5 C: `% W% @% M# Q
  122.     if (FLASH_ProgramHalfWord(Address, CONFIG_C) == FLASH_COMPLETE)
    ( x  C7 n$ M5 p, o, z: o
  123.      {
    & Q: [0 y0 U4 _2 U: |3 X& k

  124. : X& T, J$ V& r2 S% I) U
  125.     }7 O' z& Y* e) j  x% }) j
  126.       else
    * P$ q4 h' B$ d5 v
  127.     { 3 v3 [5 `9 x+ A9 i6 K- f4 e7 q
  128.       /* Error occurred while writing data in Flash memory. ) z; P4 p0 g! a- j. k7 e
  129.          User can add here some code to deal with this error */
    $ R! M, L: ~+ e! H
  130.           delayus(100);9 B" \8 D2 U& L9 a
  131.     }
    ! @0 ]% K  l1 Y5 ~* S

  132. % g9 k& _8 V$ |: ]' q' h8 J  {( p
  133. 4 N: @5 Y5 s/ }9 b! G4 T  r5 {

  134. 5 y& B7 _# U' e, E; J
  135. , B' O1 E: \" _) ~1 c: {
  136. }
复制代码
6 Y5 i1 x. z7 v' f& \$ @. g# @

. `% H) M  v  G
3 s( O, r* D% L1 Q; B) h0 X
赞 收藏 评论0 发布时间:2021-11-22 22:03

举报

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