不知道为为什么,STM32 LL库并没有FLASH的库函数,于是决定写一个
4 j$ w! T d: ]2 ^8 H
$ F, `( Z( ]% Y' YLL库的特色,就是一个函数,只占用一个指令周期,比如,设置GPIO输出的状态,就是LL_GPIO_SetOutputPin();
1 e( j; Y8 ^9 W/ N ]9 O. r- __STATIC_INLINE void LL_GPIO_SetOutputPin(GPIO_TypeDef *GPIOx, uint32_t PinMask)
" s1 s. ^4 z! N, L3 y - {
, D% @% ]3 U! _1 D4 s+ {8 n - WRITE_REG(GPIOx->BSRR, PinMask);" Q5 r: ~" E8 T0 D' K% h7 |- S' }" j; Y
- }
复制代码 于是我根据这种风格,已经常用的操作写了一个.h和.c文件
% |! c+ m* Z. b: Y# R1 O4 g) V) r [4 _
头文件代码# b7 Y0 ` o, S* f; v, L7 d
- #ifndef __STM32F0xx_LL_FLASH_EX_H
, J. t! e! [2 @ - #define __STM32F0xx_LL_FLASH_EX_H
$ M+ t6 |9 O2 c6 ^ - 1 u+ a( M2 T& ?- m* f; J# d& `
- #include "stm32f0xx.h"
* C- ] T/ U# u+ m9 U b - - x, g7 Y7 S' B3 g% @5 r
- 8 X9 ^6 L5 w' r9 S6 V
- #define FLASH_FLAG_BSY FLASH_SR_BSY /*!< FLASH Busy flag */ 4 L4 c. a0 G' E( o2 p* N. C4 i$ p
- #define FLASH_FLAG_PGERR FLASH_SR_PGERR /*!< FLASH Programming error flag */
7 b' {3 K3 f$ Y" n* f. ]+ @ - #define FLASH_FLAG_WRPERR FLASH_SR_WRPERR /*!< FLASH Write protected error flag */
9 l6 t* b6 C- r U2 H - #define FLASH_FLAG_EOP FLASH_SR_EOP /*!< FLASH End of Operation flag */
$ b; ]) N& Y2 V/ N3 O
& S) q8 d+ ?6 u8 ~- #define FLASH_TYPEERASE_PAGES FLASH_CR_PER /*!< FLASH_CR_PER */
& {7 O/ V- @5 b a4 Z5 a5 _; u - #define FLASH_TYPEERASE_MASSERASE FLASH_CR_MER /*!< MASSERASE */ A/ ?3 S* [( X$ V, T" t7 X: r: H; q
- $ B K5 C, A3 C$ w
+ j( O/ P7 T. ]8 i; k. e, |- #if defined(STM32F030x6) || defined(STM32F030x8) || defined(STM32F031x6) || defined(STM32F038xx) \; _# G% `$ L$ E9 e
- || defined(STM32F051x8) || defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F058xx) || defined(STM32F070x6)
; p' G6 n8 X# |. _* y: r Q* p+ A - #define FLASH_PAGE_SIZE 0x400U) N. G) n; Z- X Z
- #endif /* STM32F030x6 || STM32F030x8 || STM32F031x6 || STM32F051x8 || STM32F042x6 || STM32F048xx || STM32F058xx || STM32F070x6 */1 |% w6 [, b( D) M- B
- - t/ `4 f- b3 G7 @! ~/ p
- #if defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F070xB) \0 V! i, X: i( O3 r" R
- || defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F030xC)4 p, Y$ v1 F( _/ A: v$ x
- #define FLASH_PAGE_SIZE 0x800U
K* S+ |7 @ F7 c& }1 Z - #endif /* STM32F071xB || STM32F072xB || STM32F078xx || STM32F091xC || STM32F098xx || STM32F030xC */8 J8 Q) f3 {- V) O; s
- ( y1 i! a F" p7 c
- typedef enum {
2 T- C5 ^ Q6 a' Y8 F - ProgaraType_DATA64,
1 }5 r6 f; [! X$ J' Y8 ]& S - ProgaraType_DATA32,
! R# C4 r' h o0 q# ]4 k; } - ProgaraType_DATA166 J* Q7 B4 m9 `+ ]; p0 x3 P! P) I
- }ProgaramDataType;
5 V' A0 m, U& H* T3 L/ E - ) f& i2 V8 v7 x' ?# r
- typedef enum {\! J) h# v: X( i" U& X" h5 [
- FLASH_Lock=1U,Flash_Unlock=!FLASH_Lock\8 L1 l/ w+ t' _! g5 t# b
- }FlashStates;) M1 [! [; Q( A6 S) Q4 X$ e6 ]
) `) C, k% f% Y& r, D |- /* Set the OBL_Launch bit to launch the option byte loading */" Q% I6 I7 [; R) y& Q, ?
- __STATIC_INLINE void LL_FLASH_SET_OBL_Launch(FLASH_TypeDef *FLASHx)
1 c4 [4 ^" B4 g' a- d - {
. m. A5 r* h# M+ g, p$ i4 `0 d - SET_BIT(FLASHx->CR, FLASH_CR_OBL_LAUNCH);
, f+ F) s6 |# S0 B# @ a; E - }
/ g3 ?+ C9 m+ y; G5 d0 \5 s - __STATIC_INLINE void LL_FLASH_Lock(FLASH_TypeDef *FLASHx), s$ J; _1 W& ?6 Q( D$ W5 W
- {
( x2 {! I4 K% @$ M2 I0 C }4 S - SET_BIT(FLASHx->CR, FLASH_CR_LOCK);
& H* m6 q2 V/ p/ @7 c! ^* O7 ^ - }8 h8 D& b2 {) E5 H! [; R& K7 A
) q5 t$ c ~/ I$ |. k! [- $ O. u5 T# Z1 _' o( j# L
- /* @brief Set flash erase type.
! K0 }0 y, M p5 l - * @param FLASH_TYPEERASE specifies the FLASH flags to clear.
# z, L) O+ p! g% v0 h - * This parameter can be any combination of the following values:6 A$ L/ W0 Z) z- u! e" g
- * @arg @ref FLASH_TYPEERASE_PAGES PAGES Erase; w3 ^5 O* X, l6 c3 M: Y
- * @arg @ref FLASH_TYPEERASE_MASSERASE FLASH Write protected error flag * _4 @0 a m: \& d/ ~7 t
- * @retval none*/
' L1 L5 w8 d2 g" A - 6 x# Y1 e2 E* d9 i( _' w$ q
- __STATIC_INLINE void LL_FLASH_SetTypeErase(FLASH_TypeDef *FLASHx,uint32_t FLASH_TYPEERASE)
& e" ]0 c4 E* f0 w$ n* Z - {6 Q5 e: a& n/ u) N3 m$ }
- SET_BIT(FLASHx->CR, FLASH_TYPEERASE);1 x% c( j, K: i' f& K
- }0 v# n) C1 f6 }0 |9 Y
- /* @brief Set flash erase ADDR.
- i F. j- Q4 E, [# b! H - * This parameter can be any combination of the following values: ~% z3 t3 e, i( |# H/ E2 m
- * @arg @ref EraseADDR uint32_t value( c: \( e- x8 ^5 v
- * @retval none*/3 g( g8 k# C$ ~/ x) ]
- ) A3 s+ e! B$ P I6 [1 B
- __STATIC_INLINE void LL_FLASH_SetEraseADDR(FLASH_TypeDef *FLASHx,uint32_t EraseADDR)6 G( i' @" z8 W; C4 r- O. S U
- {7 M6 z6 F! }8 u. l* `
- WRITE_REG(FLASHx->AR, EraseADDR);' {( d7 i2 a1 w3 b8 T8 t
- }
) z1 \+ ]9 j3 i; m2 g; ] - /* @brief Set flash erase ADDR.
" S+ T1 w1 i+ B - * This parameter can be any combination of the following values:
) J5 a& L0 `! K) V$ K - * @arg @ref EraseADDR uint32_t value
* Q! q# l9 z# f! n; u* k) L$ a - * @retval none*/( b* u7 {$ E, O5 {! Q" h7 u
- , |7 Q+ @1 B& I& S" o
- __STATIC_INLINE void LL_FLASH_StartErase(FLASH_TypeDef *FLASHx)
* V) a. A7 u F" a3 @: ~% q, Q - {" F8 F$ ^" y( t& L3 X0 M$ G, J
- SET_BIT(FLASHx->CR, FLASH_CR_STRT);
/ D6 J) M8 G# x! {$ t+ Q - }
2 {; A5 S- ?0 t" O. F5 v S
6 C q; A! v! u- a2 m7 P( B3 r- /* @brief Clear the specified FLASH flag.* `/ j$ A; H: A) ? o6 @
- * @param __FLAG__ specifies the FLASH flags to clear.
' q% V) t& b7 @% Q. X) H3 u - * This parameter can be any combination of the following values:, |* G+ O9 R$ ]/ D
- * @arg @ref FLASH_FLAG_EOP FLASH End of Operation flag
$ M4 O4 y* k+ t+ q9 Z, V" r - * @arg @ref FLASH_FLAG_WRPERR FLASH Write protected error flag
$ K6 t8 Z6 m$ M1 A. r9 b - * @arg @ref FLASH_FLAG_PGERR FLASH Programming error flag
, B: t; m% R( I3 n4 X% k( T! E, z - * @retval none*/
$ m, E6 w6 w; l4 ^' v( H
. ? X v: i9 G* f- H- __STATIC_INLINE void LL_FLASH_ClearFlag(FLASH_TypeDef *FLASHx,uint32_t STATE_FLAG)
; j% T& k6 A4 C* T - {; |5 R( K- s- L* |; ~/ j2 \3 M
- WRITE_REG(FLASHx->SR, STATE_FLAG);" u+ P$ m& b W4 w% i' O0 ?8 w, S8 Q
- }
% M4 s+ k. K; ~
1 J' ?, e& w; e9 Z Z- /*get bit flash bsy*/
7 [9 Z' c, f. ] - __STATIC_INLINE uint32_t LL_FLASH_IsActiveFlag_BSY(FLASH_TypeDef *FLASHx)
) l* @1 R1 q% Q3 \3 F: \) Z$ u" u - {& W# L4 T: t' z, c9 W
- return (READ_BIT(FLASHx->SR, FLASH_SR_BSY) == (FLASH_SR_BSY));
) n* R3 Z* b7 D4 f - }
( U2 u* y9 z) Z& {3 f2 b - /*get end of operation bilt*/
, ^2 [! n M# ]" F: N - __STATIC_INLINE uint32_t LL_FLASH_IsActiveFlag_EOP(FLASH_TypeDef *FLASHx)
$ r9 E+ ~3 D% {# k* V0 v8 | - {& {: s" p+ j" i/ o* h2 V
- return (READ_BIT(FLASHx->SR, FLASH_SR_EOP) == (FLASH_SR_EOP));
% u3 F; C( _! T, O - }1 d& ?0 }: x3 C) H( `
- /*clear end of operation bilt*/9 g* N" `# ~2 {& X1 m* |3 i
- __STATIC_INLINE void LL_FLASH_ClearFlag_EOP(FLASH_TypeDef *FLASHx)% K3 f9 u$ i$ _8 `4 |
- {9 K2 s9 x' g# C2 Z3 y0 m
- SET_BIT(FLASHx->SR, FLASH_SR_EOP);//EOP bit Set clear X: J S+ a. V0 ]+ b* l
- }
. x$ l% @% ]( }( n2 g5 N - /* @brief Set flash erase type.4 f+ k8 p2 g4 a% t( l- {! ]
- * @param FLASH_TYPEERASE specifies the FLASH flags to clear. T1 Y4 M& y7 B o4 m7 K9 x6 ^" K! w
- * This parameter can be any combination of the following values:
4 y7 J) R+ d! ]! U - * @arg @ref FLASH_TYPEERASE_PAGES PAGES Erase# A6 F5 s1 y% p6 [
- * @arg @ref FLASH_TYPEERASE_MASSERASE FLASH Write protected error flag / P" @) z; E# k' Y, @1 \1 w9 |: V
- * @retval none*/
5 ^9 E7 @/ B7 \0 ^" A - __STATIC_INLINE void LL_FLASH_DisenableErase(FLASH_TypeDef *FLASHx,uint32_t FLASH_TYPEERASE)- L$ ^( b1 h( g7 h' p6 t
- {+ F- U n# [# G1 @% C0 C
- CLEAR_BIT(FLASHx->CR, FLASH_TYPEERASE);& F2 T8 X& a. {; v6 N) l
- }
& X' U0 ?* T9 n8 q) s) q) y3 v
' {/ `4 P& ?; \/ N0 N% M: e9 I- /*EnableProgram*/& L) J! A6 J% J
- __STATIC_INLINE void LL_FLASH_EnableProgram(FLASH_TypeDef *FLASHx)
3 e9 m4 i) D4 r, N1 I& n6 a - {) r, }" e: L) e9 O+ d, S
- SET_BIT(FLASHx->CR,FLASH_CR_PG);" N8 ] }" }' m
- }8 V) `' V* ? j1 K2 s+ I9 W
- /*DisenableProgram*/7 _6 U+ r8 ]$ n, P1 R
- __STATIC_INLINE void LL_FLASH_DisenableProgram(FLASH_TypeDef *FLASHx)
1 Q5 M7 a/ G! w8 T0 D - {+ d" a) ?# ]; Z0 W8 N
- CLEAR_BIT(FLASHx->CR,FLASH_CR_PG);/ J. }* g& z, M, {' k2 ]
- }1 S' b" J4 ~: C* N; u8 n6 Z* M
- /*read flash's states of lock or unlock*/) V9 T# [8 y- l% k5 j% X# h
- __STATIC_INLINE FlashStates LL_FLASH_LockState(FLASH_TypeDef *FLASHx)7 F& R; `' [ t+ j. ?5 u* q
- {
6 e% K2 H! K( ^6 c/ } - return (FlashStates)(READ_BIT(FLASHx->CR,FLASH_CR_LOCK));4 B' O0 { R' m
- }; r0 o- P# K& n* Y* z- }5 I
- /*set key for flash*/
. P* z; {3 X+ d - __STATIC_INLINE void LL_FLASh_SetKey(FLASH_TypeDef *FLASHx,uint32_t key)
' g* T+ v. R0 x - {, g+ y/ | h8 x# H
- WRITE_REG(FLASH->KEYR,key);
# f% C: c1 K! S2 t# x - }/ M9 u, h# F8 v" C |8 o# d
$ H' X o: E& |: {5 d. r: D$ n/ t
+ S1 ~5 d; p- G8 M- G0 y) _: M7 E; S- LL_StatusTypeDef LL_Flash_Unlock(void);
& t. O- U% h& { - LL_StatusTypeDef LL_Flash_PageErase(uint32_t page_addr,uint16_t NbPages);
& B+ [# X3 A- L- u) H6 | - LL_StatusTypeDef LL_FLASH_Program(ProgaramDataType ProgramType,uint32_t flash_addr,uint64_t data);
0 r, [" _$ `2 @ - #endif
复制代码 # Q: U1 g, y9 f( ], G. w- d
C文件代码( d F3 N; ?4 y9 k
- #include "stm32f0xx_ll_flash_ex.h"
% Y& w; q; s/ \
4 e T* g0 m6 i5 ^" \* ^7 @6 I- / P" I% M, [' Q( A! @
- void static LL_FLASH_Program_TwoBtye(uint32_t flash_addr,uint16_t data)+ o5 A: e P5 V) w6 k' a- g
- {- ]0 a( `: A2 C" W+ p0 w
- LL_FLASH_EnableProgram(FLASH);: \$ A* t! x# y3 x6 w7 ~& x) U$ d
- *(__IO uint16_t*)(flash_addr) = data;! G' h9 Q- B `1 ?
- }. \ h1 L) I' M# `7 D! L
( r, t* E$ V( g4 n" G' E- LL_StatusTypeDef LL_Flash_Unlock(void)' O( C2 I; Z" V1 O I6 H6 P% G2 D
- {
& m. U3 e( f2 R$ L2 _: z - while (LL_FLASH_IsActiveFlag_BSY(FLASH)) 2 f1 c/ h: v& P, s" J
- {
8 f( r/ z/ s) }/ s7 S" ? - } + o) V2 [4 t+ h. U% n
- if (LL_FLASH_LockState(FLASH))
! e" g Z8 g1 p! { - {
( T: U. {3 y# g2 s$ i6 T - LL_FLASh_SetKey(FLASH,FLASH_KEY1);5 Z' o$ u( c2 M7 ~* p' L
- LL_FLASh_SetKey(FLASH,FLASH_KEY2);( k \+ Y) r7 [' ?2 H! u
- }7 A( ]% j+ R V
- return LL_OK;
1 z3 Y3 `& |6 p9 C. i& d - }
( M# M1 P1 v4 U9 T$ P/ w; Z# Z - % A# O1 `$ r$ ?1 J
- LL_StatusTypeDef LL_Flash_PageErase(uint32_t page_addr,uint16_t Nb)4 n3 s3 I; l, t* x1 G
- {
* V; q& e$ x8 K) w$ G$ |3 n( f O5 K0 n - uint32_t End_addr = 10* FLASH_PAGE_SIZE +page_addr;
7 ]% Q/ C H( u6 a# t9 M& _5 k - uint32_t Start_addr = page_addr;- K$ q( ^; i' O+ X# W0 g: c
- for(;Start_addr < End_addr;(Start_addr += FLASH_PAGE_SIZE))
/ S6 A; I, B1 Y7 v - {3 k1 v T s5 D. o
- LL_FLASH_SetTypeErase(FLASH,FLASH_TYPEERASE_PAGES);
7 w1 Q; A/ g- R% w& s7 R0 e" s - LL_FLASH_SetEraseADDR(FLASH,Start_addr);
. J6 t7 w) J* y' Q& m* s - LL_FLASH_StartErase(FLASH); 1 ?7 R Z3 z5 Q7 j8 X
- while (LL_FLASH_IsActiveFlag_BSY(FLASH)) O- o+ K$ C# h* n2 b: g
- {
) b. j1 c4 h1 V) \% g) M - }
( l6 [/ k7 }4 i$ J% W - if (LL_FLASH_IsActiveFlag_EOP(FLASH))
' `) z0 Y# q) F& l( V: v, |8 W+ X - {
. Q% N* A' S" [6 M - LL_FLASH_ClearFlag_EOP(FLASH);; $ D6 g. {9 v3 N! B: I
- } % n1 _8 V1 |0 [
- else
+ {' |/ {3 d+ D: L: f - {
& V5 v: v, V6 L" L K - return LL_ERROR;
, J- n1 A8 [/ e! ` - }4 a, R0 y! R/ w1 k2 j
- LL_FLASH_DisenableErase(FLASH,FLASH_TYPEERASE_PAGES);$ b7 t2 ~% d6 o7 h8 W
- }/ v) O V8 _6 J) w0 s
- return LL_OK;, `+ @6 t x7 F2 T
- }0 V. k! Y9 O% `8 P
2 P! I" R7 Z8 w. P% x; y% @- LL_StatusTypeDef LL_FLASH_Program(ProgaramDataType ProgramType,uint32_t flash_addr,uint64_t data)8 u# A+ j" u4 b9 A
- { v9 y- S. @2 t1 K. d% b
- - ?- f7 \: R/ S, p9 E7 M4 i( L& g
- uint8_t index = 0U;
2 x+ q- H: G6 s6 p/ H - uint8_t nbiterations = 0U;
* H+ u% ^% s% F1 A7 u6 B - . O4 p" O8 n2 o) b/ }& B" a
- if(ProgramType == ProgaraType_DATA16)
/ O5 |! o4 B8 E4 ?, n" G' w - nbiterations = 1U;
/ P7 @+ Q _, z$ L; t% I7 y- N6 m5 [& w - else if(ProgramType == ProgaraType_DATA32)
( O6 o( z) R9 f. p' c$ } - nbiterations = 2U;7 l, k+ R& x3 Q* m V- B
- else; K5 E6 R& R9 ^) A
- nbiterations = 4U;
$ }& u, A$ c. f7 g* c/ @* u" V7 { - for(index = 0U; index < nbiterations; index++)
7 {* Q9 w8 D& a3 m: r. e - {
* X/ w! H) e" l1 n) ^ - LL_FLASH_Program_TwoBtye((flash_addr + (2U*index)), (uint16_t)(data >> (16U*index)));
9 ?; ~( j6 W* V4 M) l; A* A4 l: o% p3 Y7 H - }1 c- e. {: u# o. i5 G- B
- " @, g; F' K" k8 e+ y3 h
- while (LL_FLASH_IsActiveFlag_BSY(FLASH))
" d# j1 F+ h) m - {7 I6 i* U. _. u& ~( H- d
- }4 {* _1 L1 h2 n$ w# K# B! S
- if (LL_FLASH_IsActiveFlag_EOP(FLASH))
" H9 o% u/ @" X% M% {0 L8 I - {! c8 P3 Y7 C8 \
- LL_FLASH_ClearFlag_EOP(FLASH);' Q2 x# y7 g* M6 J, y/ {; {
- }
# S* m# J. J" A. ^% @- d - else4 H( y E& }; p" q- u, d, j0 b
- {
- T$ m7 t: U8 _ - return LL_ERROR;/ i! c3 @9 L7 [) D) T0 v1 O. Y
- }
0 p4 n4 k0 w9 p9 ] - LL_FLASH_DisenableProgram(FLASH);
1 g" P, ^: B8 X) W, D, r! r - return LL_OK;
: }- z9 c7 S; E' `2 i& t - }
6 @3 y, S8 i! w1 w" H: e) f5 _
复制代码
) J) ]6 D' R1 ^: E& c
0 a. S) [" o. y' I4 \ |