STM32F0使用FLASH
. Z% {, C; y! L
4 l+ C h9 u: x l( t) T- 2 V& f: B6 {- |+ z H
- /* Private typedef -----------------------------------------------------------*/; o) s; P1 w$ q8 K3 O! K
- typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
' B3 v1 v0 Z8 q* I - /* Private define ------------------------------------------------------------*/7 V J, l6 ^5 j i
- #define FLASH_PAGE_SIZE ((uint32_t)0x00000400) /* FLASH Page Size */) F3 K. s9 J8 H; @
- #define FLASH_USER_START_ADDR ((uint32_t)0x08003000) /* Start @ of user Flash area */+ @, ]2 ~! w! U/ Y& g
- #define FLASH_USER_END_ADDR ((uint32_t)0x08003000) /* End @ of user Flash area */
* f7 E. _3 P" C& w - #define DATA_32 ((uint32_t)0x12345678); K9 c0 V7 l2 ]5 j I% R( d6 A
1 v4 J5 x( ~& g3 K- /* Private macro -------------------------------------------------------------*/# c. o: A% L8 W k
- /* Private variables ---------------------------------------------------------*/" _0 E: ?# b6 v& Y' U5 x; F5 P
- uint32_t EraseCounter = 0x00, Address = 0x00;5 Y2 C0 X# o) l. l2 s x5 R! a
- uint32_t Data = 0x3210ABCD;* G. i/ U& }% o9 W" m
- uint32_t NbrOfPage = 0x00; i2 ~) ~7 d5 F8 B
- __IO FLASH_Status FLASHStatus = FLASH_COMPLETE;
- n$ |) k: Q9 I M - __IO TestStatus MemoryProgramStatus = PASSED;- {) r+ k U/ _, \, ~( o
/ f" T1 t; K2 Y7 A- /* Private function prototypes -----------------------------------------------*/$ m" C, H/ G) w+ [5 l7 j: B
- /* Private functions ---------------------------------------------------------*/6 l; H( ~6 t( H/ f
. T, }& @9 U. s2 B
7 s0 y! ] `9 l/ ^- //void delayus(uint16_t u16us)
5 ^) ]/ ^8 t7 `# [* y1 h5 z2 r8 M - //{
+ j+ u! K3 Q& R/ Z2 ~+ o6 ^ - // uint16_t i,j; , F6 b4 T$ E, E, I& x3 X
- //
! i0 b2 z! p Z. d: ]" i! j - // for(i=0;i<u16us;i++)
7 T n2 e% ], b1 u5 u1 W - // {
' s; i2 n2 S" N - // // for(j=0;j<30;j++)
! u2 ?3 C! v- E0 | - // {
, N: h5 i& K# ^( c - // __NOP(); : T; |9 R3 b2 u' D" k8 u$ P; B
- // }
4 e) _/ Q: t y4 r - // }
" {. f1 u8 }1 K& o - //}
, n% t' M9 a/ O- j6 l - void FlashRead()8 ?( i- a4 w" ~( c
- {
: @8 |7 H( \* e0 f2 ~& |5 v! Z - 7 u0 \ O( P l! ?1 F2 I
- /* Lock the Flash to disable the flash control register access (recommended
/ B/ ]7 w' `9 G3 w& S - to protect the FLASH memory against possible unwanted operation) *********/
# p6 x* X+ p1 G$ I7 U: b% v8 ? - FLASH_Lock(); + {" P) d, U- @; i# ]
- & G5 |" \* D, x: }- I0 \1 k$ r" ?
- 2 T" Q# e& m* J8 `, C0 v$ g" h
- /* Check if the programmed data is OK
* X* X% p# `( K7 ~" W6 x - MemoryProgramStatus = 0: data programmed correctly# O. }9 ^4 ~4 P0 s. e& ]
- MemoryProgramStatus != 0: number of words not programmed correctly ******/- J8 l( e. D9 r- j( ^: {: v
- Address = FLASH_USER_START_ADDR;
p& }4 M, v5 g - // MemoryProgramStatus = PASSED; % M6 [: o# V8 e) I
* q2 D% V! ~+ ?( ?( s9 O- CONFIG_A = *(__IO uint16_t *)Address; Address = Address + 2; ^# d& c3 N% b- V' J2 Y5 m/ `
- CONFIG_B = *(__IO uint16_t *)Address; Address = Address + 2;7 c" L6 F7 y) k. b8 n+ \1 i
- CONFIG_C = *(__IO uint16_t *)Address; Address = Address + 2;
* D: W' Q3 K! l: e& a* o6 b
! z: @* w n# L0 h' o- }
/ n- w5 y* M* M D/ [. w - ! d- z# ~1 c7 ^. g# x9 V6 j
- void FlashSave()
6 X( ?( @* S5 ^ - {
+ l: L* x; a5 ` - /*!< At this stage the microcontroller clock setting is already configured,
" ^- N, P% j8 H. g& I$ l; C - this is done through SystemInit() function which is called from startup
& O% N2 n$ y+ B% v- p9 D" ? - file (startup_stm32f0xx.s) before to branch to application main.
% I1 K# N4 r& X8 D4 s - To reconfigure the default setting of SystemInit() function, refer to- ^: _5 d7 |+ H* y4 ]$ y
- system_stm32f0xx.c file3 Q P9 k, I6 G. _( S# o
- */
1 q I1 o; L0 h ~
+ T6 W$ B5 e ]) I- `2 ~% V2 B- /* Unlock the Flash to enable the flash control register access *************/ / C- J6 {0 u3 V, e6 _' Q. I, h
- FLASH_Unlock();3 x2 D) \) O& d
* l0 a* E) C y- /* Erase the user Flash area
7 \$ U! D/ D+ A! Q" K5 E5 c - (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
/ O1 j5 m9 x. c" G8 y - 0 E; ~, ]& _+ O; K' E" H
- /* Clear pending flags (if any) */
+ `8 A8 n+ C' ]4 @8 h. C - FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);
$ U2 C p8 C9 H/ e6 Q/ a, g9 h% S
! r0 Y, U" V* Z. s% U6 k7 H8 J- /* Define the number of page to be erased */
# ^6 S J6 G2 B; a - // NbrOfPage = (FLASH_USER_END_ADDR - FLASH_USER_START_ADDR) / FLASH_PAGE_SIZE;1 C: l* J% y7 ^ G' Z3 d# ]
- ! [ }* i. q f; Z
- 8 `6 P: i! N7 F
- /* Erase the FLASH pages */
+ T* c6 R4 l' \; r3 j2 W' V. u" T - // for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)
9 F! k/ }, H z7 Z [ - {
3 ^3 A. V K2 e% S - if (FLASH_ErasePage(FLASH_USER_START_ADDR )!= FLASH_COMPLETE)$ s" R1 b7 R! p" T+ p, T
- {
( q3 {' _3 a" Q8 T4 d" ?! f - /* Error occurred while sector erase.
% f1 f) t7 V; {5 [( y" w6 m5 n - User can add here some code to deal with this error */" W4 l0 S5 y" j" o! ?
- while (1)5 p) P" {0 h# j0 T! M; L9 O$ R ?
- {
) F( C5 K$ @: U5 g* c8 }5 B c - TM1620_DISPLAY_DOT(999,0,PAGEB);//B999标识存储器错误+ k) Q3 V6 d( t* t' y
- }( @! ^: q3 G/ |$ P; {, _$ T, ?
- }
/ ?# d* B4 c# Y, b) e - }
- i5 a( b) d: c9 K9 [. z - /* Program the user Flash area word by word2 b% q. J: I3 H! \& A* w
- (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
8 ]; S ^$ t* V# G
4 x R4 y X; v) y0 y8 |- Address = FLASH_USER_START_ADDR;
, P" x+ {4 ?* \% @6 a I - , N6 m9 D3 J; x' @2 h' |
- // while (Address < FLASH_USER_END_ADDR)' p, k# ^, X3 k% K" p
- // {
) r) W+ U# d3 J. R* U% r- Z - if (FLASH_ProgramHalfWord(Address, CONFIG_A) == FLASH_COMPLETE)
6 d% X7 H$ [: T - {
) f* X$ }0 M3 W8 s5 E3 ` - + ?6 O4 x6 z1 w( {' G: t+ Y- s
- }
/ J/ Z$ {; {6 A. |# o+ C5 G# T - else( y/ V, S4 J! F- k0 H# }7 F
- {
* B% w. T1 q8 Y% L - /* Error occurred while writing data in Flash memory.
3 I: b7 {/ z1 L) C* j - User can add here some code to deal with this error */- s9 E. p) G9 q' I( I! A4 B* I% h l
- delayus(100);
; S+ {% q% D6 B9 _- D2 q* e& a2 x - 1 _, _) O: m+ F2 w7 X
- p" @; }( _3 A; K$ I* A6 V
' s# B6 T+ U# c% ]/ A0 [- }
3 M; g1 b0 D* U; _* D - Address = Address + 2;
& [5 Q5 | f! I( U2 L, q( B - if (FLASH_ProgramHalfWord(Address, CONFIG_B) == FLASH_COMPLETE)& c* E6 E8 J7 c
- {
, i1 m4 N3 o* r$ i! [ - ! C( ` T7 n1 p9 F* `7 x& D# ]# F* A
- }, n& r9 n3 T) P8 Q9 k' x2 H
- else
$ Q( d& T/ J- [- g0 F9 O L$ _ - { * T3 \1 }6 s$ G/ Z( B7 T5 h. u) f
- /* Error occurred while writing data in Flash memory. : h; k2 c! v Z5 i( S
- User can add here some code to deal with this error */
9 ~- q7 G8 ?; U2 S2 q0 X - delayus(100);
, W+ r. r7 B) e - }
! G/ P; i u, e6 \$ U - Address = Address + 2;1 I) k1 m4 V1 k: V9 i
' n" _, u% v+ `4 Z6 k' f- if (FLASH_ProgramHalfWord(Address, CONFIG_C) == FLASH_COMPLETE) - r% h, c+ L4 p( ?) n7 j- f, @
- {4 U6 S. n2 x- ~, T8 s* M5 |8 K
- # S0 C! _5 C: @/ j# \5 S
- }. D1 }/ Y$ ?3 I8 E$ H* K6 F
- else7 I* U8 x: }0 x/ f' G8 F
- { ; R# W4 ~) x: Y& X# D
- /* Error occurred while writing data in Flash memory.
& C; C2 j; u: O H% i - User can add here some code to deal with this error */
( ~6 }+ v4 f4 l8 ], M; j - delayus(100);
. ^0 X2 b8 z0 s: @3 ?5 R8 K - }
" q. g* g; K. t8 q6 h# t) Q- P1 {8 R - 3 w$ `* t3 i" `9 u8 Z
4 i6 C1 d; V: d0 ^
; o# X' N: `( o1 q- 8 S8 [; G5 t9 y+ ^' L# Q! a+ ~
- }
复制代码 4 L D! t8 \ l$ t, m( N
- ]8 N1 `2 z( J8 j8 Q o1 ]7 k
: g1 Z$ s% [) ~$ L) R! P2 g |