RCC_APB2PeriphClockCmd函数用来开启或关闭APB2外设时钟,* t* s# \, m3 u' o4 S
5 R" _! j9 f5 F$ V' E ~8 m. ]
该函数总览如下,6 T+ `: f1 j- Q# ]5 p7 N/ B1 a
% x/ u! j2 _9 P% h- /**
$ K9 g* S5 p% d - * @brief Enables or disables the High Speed APB (APB2) peripheral clock.
( M, ?4 C7 E2 x* j8 r - * @param RCC_APB2Periph: specifies the APB2 peripheral to gates its clock.
. [: d: U- Q! b" X( i - * This parameter can be any combination of the following values:
( O# e/ A" s% p! a - * @arg RCC_APB2Periph_AFIO, RCC_APB2Periph_GPIOA, RCC_APB2Periph_GPIOB,0 m6 b$ p7 V: M) @, h7 _. X
- * RCC_APB2Periph_GPIOC, RCC_APB2Periph_GPIOD, RCC_APB2Periph_GPIOE,4 O1 Q& w& J4 `# V; B, e
- * RCC_APB2Periph_GPIOF, RCC_APB2Periph_GPIOG, RCC_APB2Periph_ADC1,
& A% t* n( p: @( w8 f- U. y - * RCC_APB2Periph_ADC2, RCC_APB2Periph_TIM1, RCC_APB2Periph_SPI1,
) P8 k+ }4 A; v9 w1 r# w7 b6 G - * RCC_APB2Periph_TIM8, RCC_APB2Periph_USART1, RCC_APB2Periph_ADC3,; [/ o8 L* `. ] Y* s
- * RCC_APB2Periph_TIM15, RCC_APB2Periph_TIM16, RCC_APB2Periph_TIM17,4 X# ?9 p0 v8 K7 f6 ^. ~
- * RCC_APB2Periph_TIM9, RCC_APB2Periph_TIM10, RCC_APB2Periph_TIM11
; [1 D! R( ]& x! C - * @param NewState: new state of the specified peripheral clock.
3 f N/ u0 C$ l - * This parameter can be: ENABLE or DISABLE.1 I/ o; [ s X' a" z, U% [1 M5 A
- * @retval None
2 u2 P. U+ a0 t* q$ j( i9 O% w - */' `7 B' N3 v; T: r- J
- void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)4 J$ D2 S0 l# k
- {
2 [. u$ G. T, m A - /* Check the parameters */2 F5 z, s; }6 ~! I |5 k, ? {
- assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));" G$ H! @, b; J+ S* x9 S3 r
- assert_param(IS_FUNCTIONAL_STATE(NewState));
$ G2 q3 ^2 O7 T6 k/ o+ s+ [, H - if (NewState != DISABLE)
. r& V5 g" b7 I9 ^7 Y( ` - {
0 Y. v4 k# T: p7 c7 H- e2 @2 Q - RCC->APB2ENR |= RCC_APB2Periph;
% z) T: \1 F) S( w; I - }
/ t; ~: q' ] l' _4 T/ V" {/ y - else- H, l6 |4 i" z
- {9 z0 C- n% x0 N9 V9 x$ f
- RCC->APB2ENR &= ~RCC_APB2Periph;3 m) w' y/ \: C
- }; r6 G' Q% J# _; H/ u, O+ L w
- }
复制代码 " O# x8 G7 Z7 i, s+ N. g1 k
" m9 t; x" U' O& y) q如下,FunctionalState是枚举类型,DISABLE 值为0,ENABLE 值为1,使用该参数判断是否要开相应的时钟。0 h& E. G4 x% j4 t0 m; T
3 f& X# b4 G+ W P$ Y
- typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState;
复制代码 2 n0 H: Y: R) ^0 T7 ~9 [: j
在stm32f10x_rcc.h中,有APB2_peripheral的宏定义。
7 P% p4 n5 v' v6 H% A9 B7 b
; O) `+ c- r g$ b8 l" D8 u- /** @defgroup APB2_peripheral
]$ e9 h1 R; U+ h; {/ R - * @{: O, i B0 s" \3 b2 ?/ Z
- */
9 L' }! U$ Y- D0 b2 N% l1 Y& T: U4 J
5 E9 y! A( e5 D' V- c- #define RCC_APB2Periph_AFIO ((uint32_t)0x00000001)
: n7 O! s1 Z- p7 X) s. r - #define RCC_APB2Periph_GPIOA ((uint32_t)0x00000004)
& A" p6 ]* Q6 ^. A - #define RCC_APB2Periph_GPIOB ((uint32_t)0x00000008)# k I+ s @3 j
- #define RCC_APB2Periph_GPIOC ((uint32_t)0x00000010)
& L5 g' U! G3 f% Y6 x0 }% G3 J7 g - #define RCC_APB2Periph_GPIOD ((uint32_t)0x00000020)! H- c$ c/ n6 z; B# J" n
- #define RCC_APB2Periph_GPIOE ((uint32_t)0x00000040)
! s8 j2 V' \1 h3 [" K4 L G - #define RCC_APB2Periph_GPIOF ((uint32_t)0x00000080)
- z' a) r; F- G7 f/ G0 b - #define RCC_APB2Periph_GPIOG ((uint32_t)0x00000100)
* R# K- k% E# _5 t! c - #define RCC_APB2Periph_ADC1 ((uint32_t)0x00000200)
( T. h/ r: y. { - #define RCC_APB2Periph_ADC2 ((uint32_t)0x00000400)
9 Q% m) T$ [' K" k - #define RCC_APB2Periph_TIM1 ((uint32_t)0x00000800)1 p( o5 q- g0 W' m1 @
- #define RCC_APB2Periph_SPI1 ((uint32_t)0x00001000)0 X2 g; b# d9 _$ b2 d
- #define RCC_APB2Periph_TIM8 ((uint32_t)0x00002000)( n" a7 v4 n2 h5 d- ~3 Q1 u
- #define RCC_APB2Periph_USART1 ((uint32_t)0x00004000)
/ m3 h+ ~$ z3 \9 z- L" B0 s. Z! L - #define RCC_APB2Periph_ADC3 ((uint32_t)0x00008000)
6 k* ?! t8 _" r) { - #define RCC_APB2Periph_TIM15 ((uint32_t)0x00010000)
! Z( R% n1 [/ \: j' s$ `" e - #define RCC_APB2Periph_TIM16 ((uint32_t)0x00020000)
# f$ t" n2 l; R- u5 E1 S - #define RCC_APB2Periph_TIM17 ((uint32_t)0x00040000)
/ l% z) F- r. [; u - #define RCC_APB2Periph_TIM9 ((uint32_t)0x00080000)
8 T9 [9 c; I( J - #define RCC_APB2Periph_TIM10 ((uint32_t)0x00100000)" v& K' g6 H* F R# v
- #define RCC_APB2Periph_TIM11 ((uint32_t)0x00200000)
* r% E' e3 l0 w) b$ e8 s0 a
$ I: P! s. \9 g( m4 p+ T7 e- ?% `- #define IS_RCC_APB2_PERIPH(PERIPH) ((((PERIPH) & 0xFFC00002) == 0x00) && ((PERIPH) != 0x00))
复制代码
@( W) w6 P1 V' L cAPB2 外设时钟使能寄存器(RCC_APB2ENR)如下,可见APB2_peripheral的宏定义正好对应了寄存器RCC_APB2ENR的对应位。0 C" H: m7 {3 R/ d8 m, g; E$ ^1 ]6 u+ v
1 a5 T. F0 Z. ~+ Z1 e
IS_RCC_APB2_PERIPH(PERIPH)宏定义实现了输入判断,当输入参数不符合RCC_APB2Periph的数据格式时,返回0。
' d4 |( J) g7 \, K5 y* Q; ^2 G
8 p" q, O7 Z$ {/ H8 h: {
3 Y9 U" Z, H0 b t ?
9 f" z# i. y7 q/ U4 k
- if (NewState != DISABLE)' \1 b- ]5 P V8 M b9 ]" {7 p; Z
- {
0 k! l6 f- B$ a - RCC->APB2ENR |= RCC_APB2Periph;& J% L! |+ ?6 A& o
- }
) Z! ]5 b* }' c( Y* D0 T2 N - else
9 }% J& I' s T( Y! ^ - {
+ T7 \/ G6 B7 Q- I) m" W - RCC->APB2ENR &= ~RCC_APB2Periph;- Q: ^6 i* r9 |, m5 b- ~
- }
复制代码 ' U2 U8 L8 [# B
这段代码是开启对应APB2外设时钟的主要实现语句。
; u6 c- a4 A$ F* R7 Z" v& C: b: b# d* y4 v [9 @2 U
如下,RCC是指向RCC_TypeDef结构体类型的指针,RCC_BASE就是RCC寄存器的映射地址,也就是RCC指针的值。
# h3 C3 D" D" P$ {7 F5 A
) c i. p, F' ? @/ c& U- #define RCC ((RCC_TypeDef *) RCC_BASE)
复制代码 4 \8 k c% j8 n& Y: e( g, l7 d
RCC_TypeDef结构体的定义如下,
y5 b. A8 _) y0 d
; x8 K2 r, ]1 {' L2 }4 j- /**
. |" Z9 J$ k! F5 x3 j: Y, a - * @brief Reset and Clock Control3 x2 G( J/ y0 h" i4 d
- */
$ A) H( a! V" H# G; X+ ^" [) k - 5 ?2 V1 H* ], U6 c: e% O e8 }
- typedef struct
2 S9 a+ ]& j& [; H - {
! P$ E* N1 ~% n' k7 P - __IO uint32_t CR;
( \% Y, c, T" ~9 ~8 G% c - __IO uint32_t CFGR;, _" P6 Z1 ?# @ Z0 p
- __IO uint32_t CIR;* Y% f% P |' E
- __IO uint32_t APB2RSTR;
/ r! a- w4 H& a4 h/ a* [ - __IO uint32_t APB1RSTR;
) }0 @6 }, _7 Q6 K# j' p - __IO uint32_t AHBENR;) _1 I) J" Y9 x0 w }& L
- __IO uint32_t APB2ENR;
$ d+ X c' x) h1 M" H - __IO uint32_t APB1ENR;
# ?' G$ e3 a9 B$ a2 T4 w - __IO uint32_t BDCR;: }, u+ ]: S7 ^2 J; ]- G% \
- __IO uint32_t CSR;; C& h) J" y' B- Z. I- ^
- #ifdef STM32F10X_CL 7 Q( @( q8 d& s- {! o' O1 r- @; s
- __IO uint32_t AHBRSTR;* ^3 N+ C6 ]" l- b1 n0 m# V
- __IO uint32_t CFGR2;
$ a# a, O, }, Y5 C - #endif /* STM32F10X_CL */ ( Z/ T5 [; Q9 _; h
- #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL) 8 S4 ~& C. @( \! G/ U
- uint32_t RESERVED0;) ^8 X V+ _7 _
- __IO uint32_t CFGR2;, K8 P. Z3 l& R( s
- #endif /* STM32F10X_LD_VL || STM32F10X_MD_VL || STM32F10X_HD_VL */ 7 f$ t" i h# Y/ i0 E
- } RCC_TypeDef;
复制代码
" P6 B$ A6 X I, d7 ?& y
0 k' w, S, C- W2 G4 `! m
" c3 r+ x+ M) a* z0 I
( a' B( @/ ]! V" s, Y" B; D5 @5 R8 M- ` |