STM32F0使用FLASH
( Y" L. L/ s1 g" o; Y& `1 M: R, B; Y" j# [( @& m9 D P9 {2 Z
- ; O- B. f' x% V0 `- I: }
- /* Private typedef -----------------------------------------------------------*/* Z, m! h; o0 A9 z- Q8 I6 j0 I
- typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;; ?; N% w% N( L# J! d& m* T
- /* Private define ------------------------------------------------------------*/
9 ^) Y( c' M( E1 A - #define FLASH_PAGE_SIZE ((uint32_t)0x00000400) /* FLASH Page Size */. r; n1 L; I) |' U
- #define FLASH_USER_START_ADDR ((uint32_t)0x08003000) /* Start @ of user Flash area */4 I0 P7 J( @ h6 f- d" B. y
- #define FLASH_USER_END_ADDR ((uint32_t)0x08003000) /* End @ of user Flash area */# q! j* r% \/ f2 s- j
- #define DATA_32 ((uint32_t)0x12345678)
% H' T3 O# k: W. b+ A# m0 ? - & n/ U- x% Q5 M' G
- /* Private macro -------------------------------------------------------------*/
6 `7 t9 N; r: Q' n7 c - /* Private variables ---------------------------------------------------------*/
$ h5 ^: T1 g: U3 O - uint32_t EraseCounter = 0x00, Address = 0x00;
# [) q6 O5 D9 W) Q - uint32_t Data = 0x3210ABCD;% V/ G3 y3 T7 B% T% G% C
- uint32_t NbrOfPage = 0x00;
7 }( Z) a( \. q/ | - __IO FLASH_Status FLASHStatus = FLASH_COMPLETE;* M, B) g( H& V0 {5 P" i) Z L
- __IO TestStatus MemoryProgramStatus = PASSED;! D/ k* p' H) y, b% X
1 |; a8 V, y( ]5 N& q& p j- /* Private function prototypes -----------------------------------------------*/
+ J* ^: L( n' l% k' G - /* Private functions ---------------------------------------------------------*/
7 P! m, {5 s8 e/ o! y0 n - - |: ]& i$ x2 l" z: E
' h1 w& O* b" @( c- //void delayus(uint16_t u16us) : M4 @# o( G, {
- //{ 4 [9 ]; ^8 E) f; w% e
- // uint16_t i,j;
" [8 s7 a8 ]9 x9 M' N4 j - //
3 k1 i4 E2 W' c& s$ c - // for(i=0;i<u16us;i++)
) f( N1 J% s3 A3 @5 X x - // {
- j6 H* Z* M" r - // // for(j=0;j<30;j++) , i8 ?2 d! S9 L7 d: X. W$ p+ L
- // {
6 T8 U( f$ N5 J$ ] - // __NOP(); 1 _7 s' l: A/ v# ^" M
- // } 3 q' A$ `( Y# b r, D
- // } . {+ g+ `; h. _8 T, D9 ]- ~. `
- //} ) o9 E6 [* X1 x b$ N
- void FlashRead()
, Y* G& w m# y7 o( D! H$ d& u D4 J - {; X+ f5 c4 |+ M) B( k/ X6 R0 F
- ) ^2 j4 O$ H% x5 X! d3 c% X# k* `
- /* Lock the Flash to disable the flash control register access (recommended
- V( @+ U, f, X# r; [) c8 m5 g/ d - to protect the FLASH memory against possible unwanted operation) *********/
' S. ^6 z; V+ u - FLASH_Lock(); 0 ?# l1 v9 v2 }. G% q# ^/ k
- . I1 z3 j2 w( G3 x7 \
3 ?( T; L) v1 @+ g7 M/ ?9 ?, T- /* Check if the programmed data is OK ! b: a. w' h% Y6 j, `3 B
- MemoryProgramStatus = 0: data programmed correctly$ |" \) a; {$ r2 _" \3 ?8 \
- MemoryProgramStatus != 0: number of words not programmed correctly ******/
* {0 o0 @& C- E - Address = FLASH_USER_START_ADDR;
% B! B; E* g. n: ] - // MemoryProgramStatus = PASSED;
4 ~6 n) m# V' C+ a# {$ o - ) h) L; X9 Q( W" I+ R0 X3 d7 S
- CONFIG_A = *(__IO uint16_t *)Address; Address = Address + 2;
: M: c6 W$ `) M: a% S8 S; D - CONFIG_B = *(__IO uint16_t *)Address; Address = Address + 2;
- z# o* l E5 S- I" ]9 H+ B, L - CONFIG_C = *(__IO uint16_t *)Address; Address = Address + 2;
0 F6 V/ K$ i5 i! B% _% H& Y
% F( g r; ^" I C I- }- O. v7 y* I; l* ~' ~) h" U4 U
- 9 `/ ]4 B5 x6 h$ ]8 V- l
- void FlashSave()8 ?. }9 p0 k2 o: y
- {
9 b) [' P1 |# x+ v/ D - /*!< At this stage the microcontroller clock setting is already configured, $ K1 S5 E, g6 g2 I; V* A
- this is done through SystemInit() function which is called from startup4 H2 k7 N5 y# _/ Y; S. f! Q
- file (startup_stm32f0xx.s) before to branch to application main.6 M9 Z* S: P7 l4 {9 L
- To reconfigure the default setting of SystemInit() function, refer to
, [) B7 `7 j. Q0 }1 E - system_stm32f0xx.c file0 o1 P2 X- F2 k7 n& l, f
- */3 {! _& H- g" A2 k
- , R% I9 B9 f# ?
- /* Unlock the Flash to enable the flash control register access *************/ / G) c( ^$ R' M7 q8 n) A0 m
- FLASH_Unlock();
0 X& y; ~5 j* A& M# _2 D. @, s
) Z1 \9 V* j0 n; I+ }- /* Erase the user Flash area
1 D; N1 J/ c. _/ _( ~6 j - (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/$ T& C( T6 U. w3 ^
' r+ g8 D2 S1 b' W1 Z; `- /* Clear pending flags (if any) */ 1 q: g6 l* I" l6 @
- FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR); 3 P: ^. r) p* D7 I; \/ H1 A# _
f. @7 c% q9 a6 s, Z% J" R5 ]3 X- /* Define the number of page to be erased */+ w" y/ _+ h* W( V- H
- // NbrOfPage = (FLASH_USER_END_ADDR - FLASH_USER_START_ADDR) / FLASH_PAGE_SIZE;1 g2 O- E% x$ |0 M/ W6 P
: W, e7 V2 b" F K: m# h- a6 r- j8 v0 \0 g
- /* Erase the FLASH pages */& |5 i- i% W. p
- // for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)' N. Q6 i' W& S3 h9 X; L
- {
+ L R0 c0 S0 \0 p - if (FLASH_ErasePage(FLASH_USER_START_ADDR )!= FLASH_COMPLETE)' u) T e, l# |' F4 k
- {( ^- |6 \) v& J* \# q6 O% n) e# z
- /* Error occurred while sector erase. 0 S6 z$ f& Q- o: l
- User can add here some code to deal with this error */2 a* t2 R, M$ b# v; {
- while (1); |" q$ D! k5 z- O/ }
- {% _! o$ C/ V4 D/ k# ~3 j
- TM1620_DISPLAY_DOT(999,0,PAGEB);//B999标识存储器错误
3 q% A) O# Q# J n# b8 H, b+ f0 Q - }
1 e' s3 c; C$ p, ~9 G - } G/ d% y% N- b: s& O/ Q
- }# W+ p8 [2 t. E+ V
- /* Program the user Flash area word by word
- ^( ]! ]! P( o( V) o. R- \7 U - (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
6 p9 h' H5 i% `9 Z - 1 @0 \# R% Q4 w9 O: k
- Address = FLASH_USER_START_ADDR;
! Z, d9 V4 g C/ S- B7 i. { - 2 ^( Z. \% X9 N; P
- // while (Address < FLASH_USER_END_ADDR)
5 f6 M0 m# x/ a, M' n- f - // {. K! V a, k5 M2 D9 P& J( H9 C* }' l
- if (FLASH_ProgramHalfWord(Address, CONFIG_A) == FLASH_COMPLETE)
, C" i p) r2 M4 u4 o - {6 q$ T* n9 k0 H2 j( O6 i
% L& d' u; n- q- }
: F# V2 |- a$ H- {- T! M - else
3 w$ p' P/ D/ S1 ^' t - { % y- N% V6 C/ O! i6 ?
- /* Error occurred while writing data in Flash memory. ; m3 x9 m% ?+ v0 X% c5 y
- User can add here some code to deal with this error */2 h+ J& S: h3 J7 i4 [$ ?0 K# z
- delayus(100);
1 X# X/ E B% w3 L - - b# s% c3 b; J; l6 f* G1 Z) r$ J
- d4 [% B' \' C( d
6 r6 k7 X5 |0 X! E- }
; l ]* s# N; |, j; G - Address = Address + 2;' s6 [ J3 p( a
- if (FLASH_ProgramHalfWord(Address, CONFIG_B) == FLASH_COMPLETE), t$ G, J4 _/ w1 f
- {! v, j( Y% d- e" F" Y
6 f5 t+ z3 U e, I' B- p: j) K) ~- }0 a, F2 L5 [2 ^0 U) Z8 Q
- else2 y3 T8 r+ G6 J) y4 x6 Y7 Q8 F3 e
- {
2 P; p; N# e* x7 _ Y6 i- K$ ~ - /* Error occurred while writing data in Flash memory.
! R( |5 j3 u6 q& x9 q - User can add here some code to deal with this error */% s* `6 [6 I9 ]: e5 |0 d9 V1 ~
- delayus(100);
3 Q* v# ]' [ D. s# }# ~ - } r% g) y( h: O$ e1 q$ q
- Address = Address + 2;# m8 v7 p2 m: B. t7 A# Q
6 T. g V6 E0 N- if (FLASH_ProgramHalfWord(Address, CONFIG_C) == FLASH_COMPLETE)
" M! N% x: P! M1 m4 h6 q3 \ - {9 |4 r, P4 Z g f# B" B
/ T: L4 i+ i7 _$ U, Y- }+ h0 I5 _2 C& U9 R* N
- else4 Y9 [6 P: x$ u
- {
5 _& B/ q/ G$ n: e - /* Error occurred while writing data in Flash memory.
( @ e2 n+ k8 X- z. } - User can add here some code to deal with this error */7 e: c S9 q) ^1 z
- delayus(100);: t M8 _9 w7 `* D! T
- }
( }+ z ~( G0 s- j- z - , P% e) D& y' W8 i
& F: Q" j2 `* k# ], F
* X. B1 Z# v p/ A. j M; S$ b- # c4 Y1 x# K4 M3 O- w
- }
复制代码
- 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 |