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

【经验分享】STM32F0使用FLASH

[复制链接]
STMCU小助手 发布时间:2021-11-22 22:03
STM32F0使用FLASH
( Y" L. L/ s1 g" o; Y& `1 M: R, B; Y" j# [( @& m9 D  P9 {2 Z
  1. ; O- B. f' x% V0 `- I: }
  2. /* Private typedef -----------------------------------------------------------*/* Z, m! h; o0 A9 z- Q8 I6 j0 I
  3. typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;; ?; N% w% N( L# J! d& m* T
  4. /* Private define ------------------------------------------------------------*/
    9 ^) Y( c' M( E1 A
  5. #define FLASH_PAGE_SIZE         ((uint32_t)0x00000400)   /* FLASH Page Size */. r; n1 L; I) |' U
  6. #define FLASH_USER_START_ADDR   ((uint32_t)0x08003000)   /* Start @ of user Flash area */4 I0 P7 J( @  h6 f- d" B. y
  7. #define FLASH_USER_END_ADDR     ((uint32_t)0x08003000)   /* End @ of user Flash area */# q! j* r% \/ f2 s- j
  8. #define DATA_32                 ((uint32_t)0x12345678)
    % H' T3 O# k: W. b+ A# m0 ?
  9. & n/ U- x% Q5 M' G
  10. /* Private macro -------------------------------------------------------------*/
    6 `7 t9 N; r: Q' n7 c
  11. /* Private variables ---------------------------------------------------------*/
    $ h5 ^: T1 g: U3 O
  12. uint32_t EraseCounter = 0x00, Address = 0x00;
    # [) q6 O5 D9 W) Q
  13. uint32_t Data = 0x3210ABCD;% V/ G3 y3 T7 B% T% G% C
  14. uint32_t NbrOfPage = 0x00;
    7 }( Z) a( \. q/ |
  15. __IO FLASH_Status FLASHStatus = FLASH_COMPLETE;* M, B) g( H& V0 {5 P" i) Z  L
  16. __IO TestStatus MemoryProgramStatus = PASSED;! D/ k* p' H) y, b% X

  17. 1 |; a8 V, y( ]5 N& q& p  j
  18. /* Private function prototypes -----------------------------------------------*/
    + J* ^: L( n' l% k' G
  19. /* Private functions ---------------------------------------------------------*/
    7 P! m, {5 s8 e/ o! y0 n
  20. - |: ]& i$ x2 l" z: E

  21. ' h1 w& O* b" @( c
  22. //void delayus(uint16_t u16us)   : M4 @# o( G, {
  23. //{   4 [9 ]; ^8 E) f; w% e
  24. //   uint16_t i,j;   
    " [8 s7 a8 ]9 x9 M' N4 j
  25. //   
    3 k1 i4 E2 W' c& s$ c
  26. //    for(i=0;i<u16us;i++)   
    ) f( N1 J% s3 A3 @5 X  x
  27. //    {   
    - j6 H* Z* M" r
  28. //    //   for(j=0;j<30;j++)   , i8 ?2 d! S9 L7 d: X. W$ p+ L
  29. //     {         
    6 T8 U( f$ N5 J$ ]
  30. //            __NOP();   1 _7 s' l: A/ v# ^" M
  31. //     }              3 q' A$ `( Y# b  r, D
  32. //     }     . {+ g+ `; h. _8 T, D9 ]- ~. `
  33. //}   ) o9 E6 [* X1 x  b$ N
  34. void FlashRead()
    , Y* G& w  m# y7 o( D! H$ d& u  D4 J
  35. {; X+ f5 c4 |+ M) B( k/ X6 R0 F
  36. ) ^2 j4 O$ H% x5 X! d3 c% X# k* `
  37. /* Lock the Flash to disable the flash control register access (recommended
    - V( @+ U, f, X# r; [) c8 m5 g/ d
  38.      to protect the FLASH memory against possible unwanted operation) *********/
    ' S. ^6 z; V+ u
  39.   FLASH_Lock(); 0 ?# l1 v9 v2 }. G% q# ^/ k
  40. . I1 z3 j2 w( G3 x7 \

  41. 3 ?( T; L) v1 @+ g7 M/ ?9 ?, T
  42.   /* Check if the programmed data is OK ! b: a. w' h% Y6 j, `3 B
  43.       MemoryProgramStatus = 0: data programmed correctly$ |" \) a; {$ r2 _" \3 ?8 \
  44.       MemoryProgramStatus != 0: number of words not programmed correctly ******/
    * {0 o0 @& C- E
  45.   Address = FLASH_USER_START_ADDR;
    % B! B; E* g. n: ]
  46. // MemoryProgramStatus = PASSED;
    4 ~6 n) m# V' C+ a# {$ o
  47. ) h) L; X9 Q( W" I+ R0 X3 d7 S
  48.     CONFIG_A = *(__IO uint16_t *)Address;    Address = Address + 2;
    : M: c6 W$ `) M: a% S8 S; D
  49.     CONFIG_B = *(__IO uint16_t *)Address;    Address = Address + 2;
    - z# o* l  E5 S- I" ]9 H+ B, L
  50.     CONFIG_C = *(__IO uint16_t *)Address;    Address = Address + 2;
    0 F6 V/ K$ i5 i! B% _% H& Y

  51. % F( g  r; ^" I  C  I
  52. }- O. v7 y* I; l* ~' ~) h" U4 U
  53. 9 `/ ]4 B5 x6 h$ ]8 V- l
  54. void FlashSave()8 ?. }9 p0 k2 o: y
  55. {
    9 b) [' P1 |# x+ v/ D
  56.    /*!< At this stage the microcontroller clock setting is already configured, $ K1 S5 E, g6 g2 I; V* A
  57.        this is done through SystemInit() function which is called from startup4 H2 k7 N5 y# _/ Y; S. f! Q
  58.        file (startup_stm32f0xx.s) before to branch to application main.6 M9 Z* S: P7 l4 {9 L
  59.        To reconfigure the default setting of SystemInit() function, refer to
    , [) B7 `7 j. Q0 }1 E
  60.        system_stm32f0xx.c file0 o1 P2 X- F2 k7 n& l, f
  61.      */3 {! _& H- g" A2 k
  62. , R% I9 B9 f# ?
  63.   /* Unlock the Flash to enable the flash control register access *************/ / G) c( ^$ R' M7 q8 n) A0 m
  64.   FLASH_Unlock();
    0 X& y; ~5 j* A& M# _2 D. @, s

  65. ) Z1 \9 V* j0 n; I+ }
  66.   /* Erase the user Flash area
    1 D; N1 J/ c. _/ _( ~6 j
  67.     (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/$ T& C( T6 U. w3 ^

  68. ' r+ g8 D2 S1 b' W1 Z; `
  69.   /* Clear pending flags (if any) */  1 q: g6 l* I" l6 @
  70.   FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR); 3 P: ^. r) p* D7 I; \/ H1 A# _

  71.   f. @7 c% q9 a6 s, Z% J" R5 ]3 X
  72.   /* Define the number of page to be erased */+ w" y/ _+ h* W( V- H
  73. // NbrOfPage = (FLASH_USER_END_ADDR - FLASH_USER_START_ADDR) / FLASH_PAGE_SIZE;1 g2 O- E% x$ |0 M/ W6 P

  74. : W, e7 V2 b" F  K: m# h
  75.   a6 r- j8 v0 \0 g
  76.   /* Erase the FLASH pages */& |5 i- i% W. p
  77. // for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)' N. Q6 i' W& S3 h9 X; L
  78.   {
    + L  R0 c0 S0 \0 p
  79.     if (FLASH_ErasePage(FLASH_USER_START_ADDR )!= FLASH_COMPLETE)' u) T  e, l# |' F4 k
  80.     {( ^- |6 \) v& J* \# q6 O% n) e# z
  81.      /* Error occurred while sector erase. 0 S6 z$ f& Q- o: l
  82.          User can add here some code to deal with this error  */2 a* t2 R, M$ b# v; {
  83.       while (1); |" q$ D! k5 z- O/ }
  84.       {% _! o$ C/ V4 D/ k# ~3 j
  85.         TM1620_DISPLAY_DOT(999,0,PAGEB);//B999标识存储器错误
    3 q% A) O# Q# J  n# b8 H, b+ f0 Q
  86.       }
    1 e' s3 c; C$ p, ~9 G
  87.     }  G/ d% y% N- b: s& O/ Q
  88.   }# W+ p8 [2 t. E+ V
  89.   /* Program the user Flash area word by word
    - ^( ]! ]! P( o( V) o. R- \7 U
  90.     (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
    6 p9 h' H5 i% `9 Z
  91. 1 @0 \# R% Q4 w9 O: k
  92.   Address = FLASH_USER_START_ADDR;
    ! Z, d9 V4 g  C/ S- B7 i. {
  93. 2 ^( Z. \% X9 N; P
  94. // while (Address < FLASH_USER_END_ADDR)
    5 f6 M0 m# x/ a, M' n- f
  95. //  {. K! V  a, k5 M2 D9 P& J( H9 C* }' l
  96.     if (FLASH_ProgramHalfWord(Address, CONFIG_A) == FLASH_COMPLETE)
    , C" i  p) r2 M4 u4 o
  97.     {6 q$ T* n9 k0 H2 j( O6 i

  98. % L& d' u; n- q
  99.     }
    : F# V2 |- a$ H- {- T! M
  100.           else
    3 w$ p' P/ D/ S1 ^' t
  101.     { % y- N% V6 C/ O! i6 ?
  102.       /* Error occurred while writing data in Flash memory. ; m3 x9 m% ?+ v0 X% c5 y
  103.          User can add here some code to deal with this error */2 h+ J& S: h3 J7 i4 [$ ?0 K# z
  104.      delayus(100);
    1 X# X/ E  B% w3 L
  105. - b# s% c3 b; J; l6 f* G1 Z) r$ J

  106. - d4 [% B' \' C( d

  107. 6 r6 k7 X5 |0 X! E
  108.     }
    ; l  ]* s# N; |, j; G
  109.       Address = Address + 2;' s6 [  J3 p( a
  110.     if (FLASH_ProgramHalfWord(Address, CONFIG_B) == FLASH_COMPLETE), t$ G, J4 _/ w1 f
  111.     {! v, j( Y% d- e" F" Y

  112. 6 f5 t+ z3 U  e, I' B- p: j) K) ~
  113.     }0 a, F2 L5 [2 ^0 U) Z8 Q
  114.                 else2 y3 T8 r+ G6 J) y4 x6 Y7 Q8 F3 e
  115.     {
    2 P; p; N# e* x7 _  Y6 i- K$ ~
  116.       /* Error occurred while writing data in Flash memory.
    ! R( |5 j3 u6 q& x9 q
  117.          User can add here some code to deal with this error */% s* `6 [6 I9 ]: e5 |0 d9 V1 ~
  118.           delayus(100);
    3 Q* v# ]' [  D. s# }# ~
  119.     }  r% g) y( h: O$ e1 q$ q
  120.     Address = Address + 2;# m8 v7 p2 m: B. t7 A# Q

  121. 6 T. g  V6 E0 N
  122.     if (FLASH_ProgramHalfWord(Address, CONFIG_C) == FLASH_COMPLETE)
    " M! N% x: P! M1 m4 h6 q3 \
  123.      {9 |4 r, P4 Z  g  f# B" B

  124. / T: L4 i+ i7 _$ U, Y
  125.     }+ h0 I5 _2 C& U9 R* N
  126.       else4 Y9 [6 P: x$ u
  127.     {
    5 _& B/ q/ G$ n: e
  128.       /* Error occurred while writing data in Flash memory.
    ( @  e2 n+ k8 X- z. }
  129.          User can add here some code to deal with this error */7 e: c  S9 q) ^1 z
  130.           delayus(100);: t  M8 _9 w7 `* D! T
  131.     }
    ( }+ z  ~( G0 s- j- z
  132. , P% e) D& y' W8 i

  133. & F: Q" j2 `* k# ], F

  134. * X. B1 Z# v  p/ A. j  M; S$ b
  135. # c4 Y1 x# K4 M3 O- w
  136. }
复制代码

- d4 q, m# [- C7 j  T5 R* ~4 B/ ?
( q  J4 Q) e4 ]6 j5 @; e
% [6 C( a9 Q, T" u- B9 X) P
赞 收藏 评论0 发布时间:2021-11-22 22:03

举报

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