STM32F0使用FLASH q- y( A0 I' L# i$ G- t
0 V1 n9 K' o; n }& ]5 K7 d9 @8 v8 c
9 T' ]* T' m* a( O- /* Private typedef -----------------------------------------------------------*/( H1 a5 l9 y2 B
- typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
* W. R6 v) S L% t4 j - /* Private define ------------------------------------------------------------*/
1 U: N3 w" j5 k. U - #define FLASH_PAGE_SIZE ((uint32_t)0x00000400) /* FLASH Page Size */
$ ]( l7 F9 _& L* B - #define FLASH_USER_START_ADDR ((uint32_t)0x08003000) /* Start @ of user Flash area */: f$ M" R/ k4 L% z9 L2 d
- #define FLASH_USER_END_ADDR ((uint32_t)0x08003000) /* End @ of user Flash area */) F: N0 w# J' b! E& H5 ?; g5 w
- #define DATA_32 ((uint32_t)0x12345678)
- [1 j. \8 }( m6 c( E
# _+ e& E% Y$ g) e; v- /* Private macro -------------------------------------------------------------*/' G2 z, n v6 g9 ?! X
- /* Private variables ---------------------------------------------------------*/
0 g) Z7 D2 g* n( J; Q - uint32_t EraseCounter = 0x00, Address = 0x00;# D; |' F: [( }/ X
- uint32_t Data = 0x3210ABCD;; Z v" q0 H5 s
- uint32_t NbrOfPage = 0x00;
/ b' X# V, |' J) H8 |& ]3 R0 E1 ^ - __IO FLASH_Status FLASHStatus = FLASH_COMPLETE;3 g! U6 p- f; C. {& ^
- __IO TestStatus MemoryProgramStatus = PASSED;
! Y( P' w+ N8 A9 b+ Q8 @7 r - . s7 s. t0 p3 u5 j- B
- /* Private function prototypes -----------------------------------------------*/+ [( |7 v5 p& L% y
- /* Private functions ---------------------------------------------------------*/
8 c" N/ [% S4 @ - / n Q" g' w- p+ D; F
8 u8 I! } }* I9 G2 X- //void delayus(uint16_t u16us)
, g& C+ Z T, p- w b! |6 Y - //{
; t5 M+ d ]$ E% r& | - // uint16_t i,j; 9 o7 e/ D4 z, I' ]
- // ; b- e# \' p$ E4 c o
- // for(i=0;i<u16us;i++) & y( I/ S* ~/ x, B; {) s
- // {
; [. S+ v+ G/ u" _+ K9 j# f! N: \8 i - // // for(j=0;j<30;j++)
8 d, m+ t9 i9 d. L7 U6 g6 U# A - // {
8 e8 Z! [9 ~3 `" _% g t - // __NOP(); % E% M# Q+ w! X1 D6 ]. b, D5 x9 `
- // } # ~' ?3 w$ _# X5 W
- // } , a" ]" v" n! I e" D' d
- //}
. K% C) g. l0 S( J* [; O0 h - void FlashRead()
0 g, y( {1 s" P- f6 } - {9 u* d( `. b, k/ }3 h( f' y
( m8 q+ R4 B5 s: x3 Y# c- /* Lock the Flash to disable the flash control register access (recommended* ? w* X9 v' n, K" ?
- to protect the FLASH memory against possible unwanted operation) *********/; c* c5 f) ?2 b
- FLASH_Lock(); ! ?, U% b' M8 Y" l# `! j. h
& p; W! S/ \# b, i, ^, d, m
4 P$ u5 |& v: L! d k/ x- /* Check if the programmed data is OK
, U; v! }6 I/ Z; B* M - MemoryProgramStatus = 0: data programmed correctly
# c$ q q7 _; s" d9 v% M7 [ - MemoryProgramStatus != 0: number of words not programmed correctly ******/: Y' O. x# v( M5 o1 p
- Address = FLASH_USER_START_ADDR;9 v/ v5 Y6 g% o! Y2 Q
- // MemoryProgramStatus = PASSED;
/ S" S7 g6 @$ k2 z. X" s* i: j* {
9 f" ~8 W! v7 w1 x- O0 t# U- CONFIG_A = *(__IO uint16_t *)Address; Address = Address + 2;
" e. N# @1 F0 }5 g - CONFIG_B = *(__IO uint16_t *)Address; Address = Address + 2;
; A; a( L" o* w7 ?$ Y) \* H9 q - CONFIG_C = *(__IO uint16_t *)Address; Address = Address + 2;
$ B% T- _; f6 Z ^- n. T# j
f' Z0 V' J. C* E- }
( F& \' |& ~% t5 {3 [! H
6 f* t: w3 I' e# ~* ]7 ~- void FlashSave()
$ U }& H; A/ Z) \: k5 ] - {' f- a1 P6 P8 S! ]. L, k
- /*!< At this stage the microcontroller clock setting is already configured,
8 A ^1 Y$ A, ^" C - this is done through SystemInit() function which is called from startup
. t+ y9 @+ _ a5 y- ~$ Q ]" }/ f% x - file (startup_stm32f0xx.s) before to branch to application main.1 W" d- E, b8 T' O& e
- To reconfigure the default setting of SystemInit() function, refer to
* S6 d2 F2 s# ]$ Q8 R. ? - system_stm32f0xx.c file8 x8 `; P: e4 T$ b: S
- */
0 m& r, H% l* _8 A+ f: L( N - , H3 U% m2 Z/ A g" B
- /* Unlock the Flash to enable the flash control register access *************/ 0 O. l; d s* R! {! J1 g
- FLASH_Unlock();8 C0 u9 t3 J+ ^6 t8 I2 Z" t
- , A/ h# a7 {$ G4 r4 w4 M7 H
- /* Erase the user Flash area
! R8 r' z y! O - (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/+ K2 t4 p) ?6 f& F8 s0 y6 q
- # z" [% b& [3 T8 f/ ~! N
- /* Clear pending flags (if any) */
# }4 m$ T/ s' ~ - FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);
' K) [7 |# E/ _1 z1 V& X - 9 l# P4 `. z2 M
- /* Define the number of page to be erased */. x. D7 s6 s) Q# p) R) P
- // NbrOfPage = (FLASH_USER_END_ADDR - FLASH_USER_START_ADDR) / FLASH_PAGE_SIZE;
+ w0 R/ M `. p9 p! J, G; I
, `6 @! `7 c7 R) R C: R' U4 B# z
3 D7 I! w( p3 ]. ^- /* Erase the FLASH pages */
) X2 {1 H4 _6 j, c' e - // for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)! g" o( N/ Y5 b! o
- {- S5 B; x4 q* z+ r/ N S
- if (FLASH_ErasePage(FLASH_USER_START_ADDR )!= FLASH_COMPLETE)
1 `( z3 q2 `/ { - {& h; {2 o/ z. x# S% j, y) T
- /* Error occurred while sector erase. . \. P q4 b: p3 w7 S; b0 D
- User can add here some code to deal with this error */( L& ?1 O6 w3 N0 e9 i
- while (1)" W$ N7 k8 }5 s4 x" D
- {4 o4 h1 @( P' a7 M$ T9 V" e; ^
- TM1620_DISPLAY_DOT(999,0,PAGEB);//B999标识存储器错误4 F; s1 l6 c% n- c
- }1 t' j6 Z/ W( W, x
- }
9 l* X, M; m8 ]0 o; D- Q% l4 b! q% p - }' D/ O p7 t) o" {
- /* Program the user Flash area word by word
$ v/ G) N; g9 [' n$ w( R. J - (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/( k9 ?( o5 k* b$ N( t
, ]( }! S3 q7 V! S* d( M; K5 o- Address = FLASH_USER_START_ADDR;
4 }; Y5 {+ ]' P& H! Z3 P
* E, M3 F0 F4 c# R0 V- // while (Address < FLASH_USER_END_ADDR)/ S7 f9 l' G" C; h
- // {( T; e. W* p& |7 P( X+ c
- if (FLASH_ProgramHalfWord(Address, CONFIG_A) == FLASH_COMPLETE)
0 m6 Q( V: i! C+ U/ r1 ?; h- w - {
; }1 j# O4 d o* {8 H - ) _5 ^& S, {) W
- }
* Z% m& V/ U" z7 b - else1 d- n) }. @+ m. a+ S
- {
- p3 Z) l/ ~" M3 V/ N) L N - /* Error occurred while writing data in Flash memory. # n& T$ l8 @* R5 Z
- User can add here some code to deal with this error */0 W* x2 z+ b# T3 L/ I8 [
- delayus(100);/ o/ z, E+ E7 F* V5 V
0 H( [/ P$ a( f+ [: d2 x9 |
( N3 k. [1 i- v+ W5 e7 @+ K" i
& z7 A5 y* j& F" N3 k* s+ V2 J- }
- V$ [( d2 `4 s, `, U6 V% T; i - Address = Address + 2;5 B. x. ^1 @ O
- if (FLASH_ProgramHalfWord(Address, CONFIG_B) == FLASH_COMPLETE)
/ K. T' Q4 F }' r; ] - {
3 _% f5 B4 ?$ x
% s# f0 v' w. y' w7 C3 W- }
/ S* _/ y" [8 H' T* x - else7 x" k1 q3 s- G/ i) L$ {; P4 i
- { d9 }* I& j& R6 t1 k& H
- /* Error occurred while writing data in Flash memory. ' X) B2 q r$ o% Q( O6 R
- User can add here some code to deal with this error */
/ Y, N; j d7 X( L - delayus(100);
; u9 }' `" i' l, w- b - }
; w+ a3 t8 r. F" B; X$ {, Z - Address = Address + 2;
/ j8 H/ L# B- I7 ]; \3 M6 H1 c, t
: @5 N3 f0 E% a4 X% l9 Q: X- if (FLASH_ProgramHalfWord(Address, CONFIG_C) == FLASH_COMPLETE) & j4 |! w) a. W& R& C3 s9 {% G% [
- {
9 g/ d' |. G7 `2 V
* ] j1 i& S% E- R$ n- }( j4 q- ~( Y3 X) G
- else
0 q8 X3 z$ a0 R - { - P$ ^( \+ g6 n
- /* Error occurred while writing data in Flash memory.
7 Q2 }; g0 F, s: F u - User can add here some code to deal with this error */) ?' F5 z( Q2 ]- G/ H6 ~1 c" \
- delayus(100);8 K! e) M! ]$ d' d
- }2 d9 j, m% t/ w; X8 z
& s+ M( A# A; v
% i, m- u+ s) }( F* r0 P$ ~7 p- 8 F/ i+ s0 t2 x! F. A& v
- - L ~ Y: |. j9 A0 S8 O
- }
复制代码 # S( E9 u Q9 z8 r
! A5 W; F; Z, B4 i. i! L. M8 r: ?; m) O [+ o6 }4 E7 U
|