STM32F0使用FLASH
0 h" E& f0 J& U2 P D! |0 K
# k. n0 J; m. g# K1 c; {- , r; T7 n% Y. U- k y4 v8 ]; U$ }
- /* Private typedef -----------------------------------------------------------*/
Q( x& j2 Y# `# @$ u+ } F - typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
* {) e3 R( o! e- s6 } - /* Private define ------------------------------------------------------------*/
5 Y ]' m/ Z+ ^ - #define FLASH_PAGE_SIZE ((uint32_t)0x00000400) /* FLASH Page Size */1 F7 y6 e% G% F2 r5 k8 b
- #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 - #define FLASH_USER_END_ADDR ((uint32_t)0x08003000) /* End @ of user Flash area */
6 D, [$ M1 L7 d, P5 L3 P - #define DATA_32 ((uint32_t)0x12345678)* f, ^ a9 t N" d# P4 _
- 3 }6 @. q9 I1 M C5 c# x1 M
- /* Private macro -------------------------------------------------------------*/: s9 m; O# b1 W* Y2 k8 b1 W- n, n
- /* Private variables ---------------------------------------------------------*/2 V: z6 _) y; ~7 ^
- uint32_t EraseCounter = 0x00, Address = 0x00;/ y* x9 V! U# e% \) k3 S" _" ?
- uint32_t Data = 0x3210ABCD;
1 d' C2 t! g& a# e - uint32_t NbrOfPage = 0x00;4 j( g2 a% w# v# a) l/ A
- __IO FLASH_Status FLASHStatus = FLASH_COMPLETE;7 c/ G/ o$ @! s [/ ?( M' E. \# N
- __IO TestStatus MemoryProgramStatus = PASSED;1 \2 L% J8 T+ o1 o6 B, r& L
" H: G" A2 @1 F- /* Private function prototypes -----------------------------------------------*/
( |! {- H( O/ x; H+ c* g% u - /* Private functions ---------------------------------------------------------*/. ], I) t% v+ |% H1 J) F% ]
- & H7 X, ] q1 C( H4 k. }1 Y
- ! W5 |' o0 }8 M
- //void delayus(uint16_t u16us)
! J8 O" C" t9 H( N8 c1 v - //{
! F9 Y8 `0 l: S2 } - // uint16_t i,j; + u$ X: ]! Y+ m) I) I* _
- // 5 H" V8 {! N8 n B
- // for(i=0;i<u16us;i++) - S8 s" z& J- E! ~7 D$ c7 c
- // { ! q1 y$ \: s# w/ Y
- // // for(j=0;j<30;j++) " ]- z/ B5 \' o) g! h% |
- // {
( B& a" T4 S0 f( ^. X - // __NOP(); ; y* K4 }) Q1 X% U/ u! I( j
- // } " M7 P. I; ~0 W; \
- // } 9 z. l: [0 V. o1 \( Y: T3 B
- //}
3 d& p9 ?4 F, A( C$ v, L& Y _ - void FlashRead()7 Q- y$ Z9 ~$ L8 Q
- {! ]3 R5 I5 C. `: E* m. J* Z9 [
1 y' g% U7 M6 \* Z7 k5 D! }$ S- /* Lock the Flash to disable the flash control register access (recommended
' x! n3 ]; Q* A$ A - to protect the FLASH memory against possible unwanted operation) *********/
8 D/ u, X) Y- V C: V# G - FLASH_Lock(); $ T7 |& C# [3 v8 m% ?: ]
- & ^3 e% G( t2 _$ w
o, R1 u6 Y% A1 S- Z! K4 Y- /* Check if the programmed data is OK
* k/ R% L8 F8 D; I& ^- x - MemoryProgramStatus = 0: data programmed correctly
$ m6 R& h! V3 ^' D& B - MemoryProgramStatus != 0: number of words not programmed correctly ******/* y$ B" e7 w- H- E& F
- Address = FLASH_USER_START_ADDR;- [2 d! `7 Z8 v% `1 c% r
- // MemoryProgramStatus = PASSED; 9 `" o' G3 ~6 ]9 z+ Z3 Y" `9 ^
- 2 x1 v0 s# B: I5 U6 ]. y( N$ U8 Z3 F
- CONFIG_A = *(__IO uint16_t *)Address; Address = Address + 2;
. p; g; _8 I$ ?! n9 Z - CONFIG_B = *(__IO uint16_t *)Address; Address = Address + 2;
0 k' ], R) o4 d$ w$ v - CONFIG_C = *(__IO uint16_t *)Address; Address = Address + 2;% ~ x: Q: h) f9 o
- 8 z- S- r, f& \: U7 Z% p. d
- }0 x! F) k% B+ H8 ?& X3 r
- ( F: T3 n$ P6 S- Y5 G; }/ `
- void FlashSave()
$ u8 V, d' v' [7 r* d$ j - {' \' M/ S8 S3 K& Q1 U: A& c
- /*!< At this stage the microcontroller clock setting is already configured, * ?2 b& j- ^8 m; l# F; _: L+ h5 |
- this is done through SystemInit() function which is called from startup9 _7 H! B$ i2 O8 a& H
- file (startup_stm32f0xx.s) before to branch to application main.7 E3 c; y. c A% j
- To reconfigure the default setting of SystemInit() function, refer to
' Y( m8 ]! j1 r/ a2 ~ - system_stm32f0xx.c file% V& i* Z' h) ?6 q2 W" ]8 Z4 F
- */
- a2 f& h; j/ D! c' M( h7 B9 O, m - 7 j# [' X% I3 C2 j5 O9 p
- /* Unlock the Flash to enable the flash control register access *************/ - _0 g! r m( l1 S
- FLASH_Unlock();
2 }. K- n- N- g2 O5 [4 ~5 M
, X+ `+ _, Y8 i- /* Erase the user Flash area
- a! s5 W; O" k, {: o4 Q - (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/% ]7 ?6 `& m) a5 w- j
. X0 M5 g9 p6 O* @0 i) B- /* Clear pending flags (if any) */ 6 Z4 m# Q# k1 f$ P
- FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);
( |/ L* z: }# D
: t: q0 {; Z' \- M1 _$ R }- /* Define the number of page to be erased */; h9 ?% m; V. @! q. g( J" r
- // NbrOfPage = (FLASH_USER_END_ADDR - FLASH_USER_START_ADDR) / FLASH_PAGE_SIZE;
$ _9 s$ O4 r' f ~: C
) M/ t2 q& ?: z
4 l3 k( s4 c5 q& W- /* Erase the FLASH pages */# h, O5 _6 r C. I/ n0 g
- // for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)' l$ ]' d2 N4 \/ U i$ Y- {) k
- {
+ x, W9 K$ N. ~. Q/ }% O8 Q - if (FLASH_ErasePage(FLASH_USER_START_ADDR )!= FLASH_COMPLETE)4 E: t3 _5 S& f" K! m- D
- {
5 T0 m. E3 _3 b0 I1 e5 Q5 C1 X F - /* Error occurred while sector erase.
0 I& V n+ H& H* ?6 u9 \/ [5 I+ ~! V - User can add here some code to deal with this error */
& O1 o8 y8 X2 h - while (1)
4 ^3 @5 n2 o6 M( D" q: c! Q - {7 v( O. X) m( Z6 U
- TM1620_DISPLAY_DOT(999,0,PAGEB);//B999标识存储器错误
3 J( {2 b7 N( A# D* `- E0 a) t6 e - }# w" q( _4 ]1 I: i9 u) M1 |
- }
* O8 q( w0 m4 k C - }0 q' h) ?! b+ y( y3 |
- /* Program the user Flash area word by word) A* @7 Z" k( p/ H& q
- (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/0 M$ G9 I8 ~- c( N: E
- 6 q" g* \$ x9 r7 H8 Q5 D
- Address = FLASH_USER_START_ADDR;
7 c$ o, ~& @1 H3 N) g
& m+ x) ]" ?# C- // while (Address < FLASH_USER_END_ADDR)
' w0 d- E* ]) H) y9 {& M6 o$ C - // {$ _8 y# L8 ]' e8 X: S2 _4 A
- if (FLASH_ProgramHalfWord(Address, CONFIG_A) == FLASH_COMPLETE)
& D) W$ S% R+ x% G* y - {
( m/ D1 L0 T0 o* r: v f - ; U3 i( I: s5 k5 _+ M+ D- Y
- }
$ S0 m. v% k7 |) f - else9 E& r7 v) `/ C) `, O$ O# n
- {
! I/ P( o) }$ ]8 z/ T& E - /* Error occurred while writing data in Flash memory.
) i8 S/ k7 l U3 \6 ?8 v - User can add here some code to deal with this error */4 N0 x9 Z% R. I
- delayus(100);8 ]+ |" O6 p3 M; p1 |8 U- l
$ n3 @4 D( V0 t- X! s3 M4 j
7 [- M! b. X% O; W) c- G
5 \% S3 n% p. g0 B( ~# o$ H. w* z- }! G, z f* m2 i. m
- Address = Address + 2;
. H; D2 o$ v0 [" _ - if (FLASH_ProgramHalfWord(Address, CONFIG_B) == FLASH_COMPLETE)
# `' t1 x. V% Z: I/ s+ V4 b - {
7 Y0 h+ o$ ], V8 L8 n/ X - - z, B" x. H9 @. K
- }" { y8 B& }! y* g0 ?4 i/ ^
- else
6 y" n$ e' B' h8 X; D - { $ e0 u9 F) K" g
- /* Error occurred while writing data in Flash memory.
, t5 s+ G1 h9 \) g/ z - User can add here some code to deal with this error */
' ^% u, Y3 Z; E: {7 ? - delayus(100);* Q, W: u8 B5 i$ `0 D' C! c
- }
* E# I, }0 K; V0 f: r H9 p- e% B/ C - Address = Address + 2;
/ W! p7 W9 x6 Q. a! ~7 {: a - 3 K6 A1 ~3 C& P- M1 _; F' q% [7 d
- if (FLASH_ProgramHalfWord(Address, CONFIG_C) == FLASH_COMPLETE)
# l) j6 \( M$ R7 a - {$ @" Y2 Y- \4 G8 D& C
/ O0 u/ i1 h$ L, S% y- }/ l0 M3 Y/ N2 J5 d1 S
- else2 X2 a6 V. H4 \) @8 I
- {
9 C: N- M: W& w- f! W - /* Error occurred while writing data in Flash memory.
* l4 Z( n7 ~& o7 W2 ~% A; @ - User can add here some code to deal with this error */* m. Z, T' B, g' b+ Z! z$ G
- delayus(100);
- Y; l) P" f3 ? - }% {9 E# i8 Q7 A" j- p
5 E1 n7 R9 l! S% i1 E
6 N, ?" ~8 `; R3 U# r$ M5 J- % R5 O5 J: U p( `
- ! ~; F4 { }/ e2 s" S# B. D* ^7 V
- }
复制代码
/ 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! _ |