1.在main函数中加入启用读保护功能。
( \/ D( B8 c1 [, p7 j# p; \& `, Z. J5 e5 R% H+ I
4 f6 {) j/ Q! ?6 k0 b( I
; U5 Y' ?' Z/ U% I
2.读保护函数
+ k9 \4 X; {* r
8 f2 l0 T$ n/ T) p+ }$ d" @( l/ Y& {
; @2 {7 R* a* M
9 v9 v4 {& ^! }1 ] i
' X4 V; r. ]0 S# Q% ]4 w/ D% `6 O
- int main(void)! W% w7 f: h2 [: E, v$ S. L
- {
% a8 N' i2 Q. a% [( C# i - Flash_EnableReadProtection();3 }: ^5 W7 _) w+ I K
- While(1)
4 I& r" o) a3 g8 X: a - {
" B( s4 H$ T0 {( X5 Q) Z - # ^* V! B3 r* b, e6 D8 l& [! i
- }
. Z8 w I7 \) e/ o( B - }5 [: w8 ]) n& l; V. L: ]5 a! `( u
- ! v( y2 }2 _- u; V% p
- void Flash_EnableReadProtection(void)
- {* U6 l; B+ O- _5 H: s: K* r1 _ - {# b+ _* @ Z* F
) z- T/ S- n( `7 h0 |9 L- FLASH_OBProgramInitTypeDef OBInit;
% a' \7 f# h% p7 C* n
2 g) m* A+ f+ n4 U q) y- q- __HAL_FLASH_PREFETCH_BUFFER_DISABLE();; f6 d0 s/ O: O' G, {
- 3 v/ M9 }! t- r: U( i* W
- HAL_FLASHEx_OBGetConfig(&OBInit);( n0 U5 c$ | q' L* Z2 e
- if(OBInit.RDPLevel == OB_RDP_LEVEL_0)- {# M g! C; i0 y/ E
- {
4 q8 c2 z& w, Z3 f7 b# ]5 [9 D - OBInit.OptionType = OPTIONBYTE_RDP;5 F! c* I& m4 P& u
- OBInit.RDPLevel = OB_RDP_LEVEL_1;; X$ O! Y5 k! E! U) U% s) i! [2 K; d
- HAL_FLASH_Unlock();
: A- x/ H! z( Z- w& n - HAL_FLASH_OB_Unlock();
) r0 \) F5 w$ z ]4 i2 N. Z - HAL_FLASHEx_OBProgram(&OBInit);
3 U L2 k. P7 N - HAL_FLASH_OB_Lock();
. r5 [! w9 z+ X0 y: D. o+ p - HAL_FLASH_Lock();
0 ~% h8 ~3 L7 a& Q - }
! B& D4 M( U. w; b4 [3 h6 w& s - __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
/ l3 V O# ~# c s7 w8 n
0 y2 D5 V3 y8 R# g' S- }
5 R! F- @$ D/ ?0 [1 d/ i* F/ V, q - 2 N) h3 a: D, P8 j+ l6 c5 ]
- void Flash_DisableReadProtection(void)
! u3 Y, Q0 i. h7 z+ ]2 b - {* A) m# J/ d2 c3 |4 G6 F+ q, ]
- ' H; q' {: [0 I9 t; U/ b( l+ s
- FLASH_OBProgramInitTypeDef OBInit;7 i% J+ m5 M6 s: E+ O( D
- 4 ~5 W: u# d0 E! {2 @; T( z
- __HAL_FLASH_PREFETCH_BUFFER_DISABLE();( d. z6 H/ M& f; @& H* n9 F
8 c# {8 l% ?, U6 b" K% }4 n, s- HAL_FLASHEx_OBGetConfig(&OBInit);
" R) C# `1 L) S5 e - if(OBInit.RDPLevel == OB_RDP_LEVEL_1)( S' Y, d5 g* |, I+ Q
- {2 _- d, h6 K" \9 e9 @
- OBInit.OptionType = OPTIONBYTE_RDP;/ P; z$ o# u ~7 o" L6 J
- OBInit.RDPLevel = OB_RDP_LEVEL_0;
6 t9 G6 T$ @: q' M) J& \ - HAL_FLASH_Unlock();$ h4 O) M; e. a2 r; t
- HAL_FLASH_OB_Unlock();- Y1 n0 C- y, i1 j0 n+ X) X
- HAL_FLASHEx_OBProgram(&OBInit);
- p' g% y, Y" x" }+ N0 A - HAL_FLASH_OB_Lock();
0 F6 u2 A' P. c - HAL_FLASH_Lock();
7 N* G7 S4 `( A# {& q - }( j! ?7 U9 u7 p
- __HAL_FLASH_PREFETCH_BUFFER_ENABLE();- b) L8 _, I( A) U
- $ O7 z5 @! |/ p; Y
- } {! |9 g0 k+ O& c5 o) I \5 ^
复制代码
8 `2 z! I) k% D) {
/ N- P- x, @: Z8 g/ g |