STM32F0使用FLASH
9 v0 q# L' |& i3 _0 f% @ v$ c( W) N1 ]& [& q9 i& u
. N6 w9 n/ ^2 f% ?: W- R- /* Private typedef -----------------------------------------------------------*/
7 m: G/ P% r/ V& R- j* f - typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
; b7 H2 U- L: x1 b$ q) t& n - /* Private define ------------------------------------------------------------*/
8 C" V1 k8 S( G# N7 |1 T- N$ ~ - #define FLASH_PAGE_SIZE ((uint32_t)0x00000400) /* FLASH Page Size */) V) L; y0 |# }' h
- #define FLASH_USER_START_ADDR ((uint32_t)0x08003000) /* Start @ of user Flash area */4 R! R6 L( m+ @: J
- #define FLASH_USER_END_ADDR ((uint32_t)0x08003000) /* End @ of user Flash area */
+ G& R7 a" r" d R - #define DATA_32 ((uint32_t)0x12345678)7 ]: D/ H! M( j- c: |
- 9 f; x* o6 m' }; S2 p/ ^2 H
- /* Private macro -------------------------------------------------------------*/
% H3 f- y% W6 H6 m% _$ I& U - /* Private variables ---------------------------------------------------------*/: O% K+ I" x( n3 n$ ~ {0 v
- uint32_t EraseCounter = 0x00, Address = 0x00;5 Z3 T1 y6 E2 M8 q/ a n b
- uint32_t Data = 0x3210ABCD;
. u/ A6 m1 {6 i* N. a8 U - uint32_t NbrOfPage = 0x00;
$ J: r0 D# e5 H$ P - __IO FLASH_Status FLASHStatus = FLASH_COMPLETE;
9 d# V% x8 S! T- q - __IO TestStatus MemoryProgramStatus = PASSED;
& M3 ^0 }1 w: T( k" ~) Z1 x
- B- a0 W6 `% ^* `+ B' ~9 v- /* Private function prototypes -----------------------------------------------*/
: t' e/ j7 }9 O - /* Private functions ---------------------------------------------------------*/
P$ v9 {# C* ^3 F1 k - ( n( u6 Z1 V, C7 M
4 J3 N5 [' F) G8 c( \# a8 Z; Z- //void delayus(uint16_t u16us) , b/ P3 P7 I5 p1 A3 f s
- //{ 5 A) O+ {2 o; O+ i: m( J1 j
- // uint16_t i,j;
( `. ^9 l$ g+ k: e( K - // % L) h( Z+ J4 w( K; F" S
- // for(i=0;i<u16us;i++)
& ]- F3 g3 B/ W. Y! S3 O - // { 7 ]0 {1 n" e8 l. a& K; E! b
- // // for(j=0;j<30;j++)
& ]. @% m) B s+ `! u0 P( V - // {
5 F3 c- W, I% F - // __NOP(); 8 |. _6 s' ^$ w- |( B- h: b6 {
- // }
, o: u% u/ |8 h1 c, z3 S) U5 w! A - // } % \7 }+ [) Z$ A* P
- //}
6 V% z" Z- x8 Q% K. m3 B! K6 ? - void FlashRead()
$ y; P+ U! }- }; r8 }9 _6 X - {
! D6 ~/ q' e t+ C% b% g
, U" W$ x/ \. {3 `& I' o( r e, i: |- /* Lock the Flash to disable the flash control register access (recommended
( B- ^7 X. ?5 K" r - to protect the FLASH memory against possible unwanted operation) *********/
9 o O v9 Z1 r4 A. N# ] - FLASH_Lock(); 5 q* C7 M2 A7 i% V7 |4 N* T9 _
- 9 n* g5 w# S/ {* F
) m# Y$ L6 X5 s) M/ ~' O- /* Check if the programmed data is OK
* c$ @1 t* _+ K! b' ` - MemoryProgramStatus = 0: data programmed correctly$ N" f. Z' U. D
- MemoryProgramStatus != 0: number of words not programmed correctly ******/( ~2 R* c1 M* ?" l/ V2 |! b, f1 @1 e u
- Address = FLASH_USER_START_ADDR;) Z9 g& P! n! n) S
- // MemoryProgramStatus = PASSED;
. x5 m$ u/ B- D s - & `6 Z+ Z7 W3 E, C3 s: D- L
- CONFIG_A = *(__IO uint16_t *)Address; Address = Address + 2;7 Q4 |( L# J! @ {5 M+ W7 Q" B
- CONFIG_B = *(__IO uint16_t *)Address; Address = Address + 2;% y' i" d- l$ c, O: f- p) M3 p
- CONFIG_C = *(__IO uint16_t *)Address; Address = Address + 2;
5 B+ L" m' g2 @( j T5 d1 K4 N8 J
- K# d( c* k# H' l( |- }
# @& Q7 g/ g+ H/ `& Y1 w - 4 ^5 h8 U) o, m3 ]1 j6 Q$ y# i6 t
- void FlashSave()# l* }5 a' E% l
- {
) |! ?+ Z* ]3 O _ - /*!< At this stage the microcontroller clock setting is already configured, 7 Y3 _3 W! o* X/ L* W( G
- this is done through SystemInit() function which is called from startup
$ W; h W. _- `' E5 z - file (startup_stm32f0xx.s) before to branch to application main.
; D5 m8 Q) O0 O, t - To reconfigure the default setting of SystemInit() function, refer to
$ t7 K8 A( L6 S/ P% V - system_stm32f0xx.c file
z2 i! l1 j6 H9 r6 q6 T, i - */
0 ]' @+ r7 v5 R0 U3 m, R% X) ?% m) f
' m; T# L% B2 j: u! f- /* Unlock the Flash to enable the flash control register access *************/ 6 ~* o+ \" H1 B$ K; w! Y
- FLASH_Unlock();
! L4 }+ p u# g; r0 C
+ c q" o# O, C/ O" {) ?- /* Erase the user Flash area' a% S/ T" x, ~. b9 Z, E
- (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
3 e, F1 n# ?8 M$ ]7 }2 ^% Q' Y - 8 {5 Z2 n9 J" m D) r: s0 D7 y
- /* Clear pending flags (if any) */ E# l, |+ U* G, ]" r+ `6 e
- FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);
% p o) i9 r a$ H8 J; X
9 ?- i( V, b: M/ `2 T( p/ r- /* Define the number of page to be erased */' q5 z W$ `7 s+ ]9 }9 T
- // NbrOfPage = (FLASH_USER_END_ADDR - FLASH_USER_START_ADDR) / FLASH_PAGE_SIZE;. h+ a: |# b! m2 q* ^- C I! W
- * A F" m; _7 t. N6 L
# L5 F2 @/ P% p: W& V, g; G# n- /* Erase the FLASH pages */
; k8 a& G: B8 Y) T( S$ ]9 D - // for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)# X, a) W( w1 q, Y0 U* u& r+ j. a; f* U
- {* H4 O" y; [- D A
- if (FLASH_ErasePage(FLASH_USER_START_ADDR )!= FLASH_COMPLETE)/ i" e1 n( u( d1 \) l" A6 a- }
- {
7 i- m; s2 f$ _: }! s, J8 Q# P - /* Error occurred while sector erase. , u X2 x) B$ X% h
- User can add here some code to deal with this error */2 t" y! U( a- @# F. _
- while (1)
. [9 I$ ~' m! a0 y4 R - {" B- L2 V" |: [( i# e$ x5 v
- TM1620_DISPLAY_DOT(999,0,PAGEB);//B999标识存储器错误
! M- J( s# x3 l7 {6 {; z7 O - }, E: a2 ~3 S5 x
- }' \" L4 ~) V0 W: _# |8 R8 C3 E1 [
- }
+ g- ?) z" ^. C! u - /* Program the user Flash area word by word7 z7 I3 T+ c! [ F1 p8 `2 s
- (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
4 Z: J8 @/ |5 p( F1 c& `; r( K
: R- Z+ {; Y* }$ V: L8 {4 |- Address = FLASH_USER_START_ADDR;
; E& O4 F: n# G5 q - 6 `; N) k6 B% d) U6 n, X6 |
- // while (Address < FLASH_USER_END_ADDR); {, O1 M0 M+ K. K% I% I
- // {
. M- Z( X# R% r! C - if (FLASH_ProgramHalfWord(Address, CONFIG_A) == FLASH_COMPLETE)
, h: Q/ x5 o5 m; K; h+ O; ^- P - {' p8 A5 V7 e3 m9 N- Q
- 6 _7 K p2 z7 G5 H
- }
1 ?" n4 G7 @! G- N - else# s; e0 a7 K7 y' x+ v. L
- {
' V- U" d+ w6 p/ z% g, ^. @3 Z - /* Error occurred while writing data in Flash memory. ' W; \9 y+ Y9 C6 |8 b
- User can add here some code to deal with this error */; C+ s7 y$ E! L; i' }9 y7 P
- delayus(100);& M$ \) _* y- ^. ?3 a5 u `
# X: l4 h3 p' Y- [! M9 L- 3 B+ M! e" `; U t
- - ]+ O- s! R! c: y0 u2 _: z
- }& v: n3 w( G# b% H) X. u, _9 n
- Address = Address + 2;% e6 | } [" f* h, z( u
- if (FLASH_ProgramHalfWord(Address, CONFIG_B) == FLASH_COMPLETE)
9 k1 d3 W5 g! r- `+ W! ` - {' E& t& F4 Q2 v6 I: C( ~
7 R Y" f- C& [ |: I6 N3 E* H# F- }
/ i8 t4 S2 i5 y: ^ - else; L/ v* Y& y0 b9 t
- {
1 u3 J1 u4 ?1 c( K! l - /* Error occurred while writing data in Flash memory. 7 V6 }7 e1 t. g/ U5 L- `
- User can add here some code to deal with this error */
8 I/ B/ }+ |7 I# L - delayus(100);
) A, U* _: T% t' o% h) ]* ^& P* J2 n - }
) G& N+ R& b2 k' _# B - Address = Address + 2;( d$ O+ T* ~3 e+ V
- ) p- {1 k, b5 C: `% W% @% M# Q
- if (FLASH_ProgramHalfWord(Address, CONFIG_C) == FLASH_COMPLETE)
( x C7 n$ M5 p, o, z: o - {
& Q: [0 y0 U4 _2 U: |3 X& k
: X& T, J$ V& r2 S% I) U- }7 O' z& Y* e) j x% }) j
- else
* P$ q4 h' B$ d5 v - { 3 v3 [5 `9 x+ A9 i6 K- f4 e7 q
- /* Error occurred while writing data in Flash memory. ) z; P4 p0 g! a- j. k7 e
- User can add here some code to deal with this error */
$ R! M, L: ~+ e! H - delayus(100);9 B" \8 D2 U& L9 a
- }
! @0 ]% K l1 Y5 ~* S
% g9 k& _8 V$ |: ]' q' h8 J {( p- 4 N: @5 Y5 s/ }9 b! G4 T r5 {
5 y& B7 _# U' e, E; J- , B' O1 E: \" _) ~1 c: {
- }
复制代码 6 Y5 i1 x. z7 v' f& \$ @. g# @
. `% H) M v G
3 s( O, r* D% L1 Q; B) h0 X |