RCC_APB2PeriphClockCmd函数用来开启或关闭APB2外设时钟,
6 L6 i, b* y& U: `6 H
$ Q* E3 a0 e% R该函数总览如下,
h* H; |. l2 X* g: c! P; G; p( s5 a: s# \
- /**" q$ \! r, {% Q# m' i. J
- * @brief Enables or disables the High Speed APB (APB2) peripheral clock.
: i! ?' z7 _8 s' {' r8 i - * @param RCC_APB2Periph: specifies the APB2 peripheral to gates its clock.
( g5 g8 p% B( o5 m1 Z5 u3 z - * This parameter can be any combination of the following values:
8 u* a, p( o& N$ {- ?9 s; X - * @arg RCC_APB2Periph_AFIO, RCC_APB2Periph_GPIOA, RCC_APB2Periph_GPIOB,
* \9 k: @7 z, D' J - * RCC_APB2Periph_GPIOC, RCC_APB2Periph_GPIOD, RCC_APB2Periph_GPIOE,( ] s2 O+ A7 P, G
- * RCC_APB2Periph_GPIOF, RCC_APB2Periph_GPIOG, RCC_APB2Periph_ADC1,
: Y9 o4 P2 m& y6 K - * RCC_APB2Periph_ADC2, RCC_APB2Periph_TIM1, RCC_APB2Periph_SPI1,
1 R+ z0 {* T+ N: D) ^ - * RCC_APB2Periph_TIM8, RCC_APB2Periph_USART1, RCC_APB2Periph_ADC3,
; a1 w0 {8 m9 C9 C - * RCC_APB2Periph_TIM15, RCC_APB2Periph_TIM16, RCC_APB2Periph_TIM17,9 C% k2 S; B! v: N8 ^' q! V' F
- * RCC_APB2Periph_TIM9, RCC_APB2Periph_TIM10, RCC_APB2Periph_TIM11
! P5 u# |6 }. U9 q* T - * @param NewState: new state of the specified peripheral clock.
# r, ?4 H* N/ L0 r9 l" j# w2 R0 D - * This parameter can be: ENABLE or DISABLE.+ l8 ^9 ]% r5 z7 E" ^4 z
- * @retval None
9 e/ a0 c7 Z; {6 v3 E$ W5 r" F - */
7 L/ w1 g# s% O1 Y- B p* D9 Z - void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
* m' d) Y1 F) R5 `* H: s - {
' e, _3 i- H2 |7 i- G - /* Check the parameters */4 s& b3 {& }# H0 e- l
- assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));
8 j5 S1 `0 f& V" W6 _ - assert_param(IS_FUNCTIONAL_STATE(NewState));8 [; e+ v# M: o) g
- if (NewState != DISABLE)
. B5 g6 Q4 H' n( X9 \0 @& O - {, h; ~$ ]! T' M+ D v4 q2 ~
- RCC->APB2ENR |= RCC_APB2Periph;* K) b0 u3 t2 ]6 k: ?9 [( ?6 @! S3 g
- }% r. v* D$ w$ ~ n1 t- O5 e
- else/ k1 k6 ?0 y; l; L& u
- {! P6 n* N9 i: f; A
- RCC->APB2ENR &= ~RCC_APB2Periph;
1 @3 ? s/ ~3 I/ m; E c - }
' A2 A) ^$ |6 H6 t# Y - }
复制代码 ; n( h5 h' w: L
9 h& \% R% P# ]3 G如下,FunctionalState是枚举类型,DISABLE 值为0,ENABLE 值为1,使用该参数判断是否要开相应的时钟。
# S: a$ K$ ^! _( l
) b7 A! a" p& }( |& I- typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState;
复制代码
e* S* S# u* E+ h% r在stm32f10x_rcc.h中,有APB2_peripheral的宏定义。) S6 v8 k0 t* k* a# m, z) |/ x
H. ^4 p1 @( B. m1 g
- /** @defgroup APB2_peripheral - n1 e4 l+ n/ a; a" j6 ^
- * @{
! H7 m) D C5 p - */
' ~" F. \5 R( X/ H/ Z, X
! g* \& b- U- a6 h/ ?1 \! [. i- #define RCC_APB2Periph_AFIO ((uint32_t)0x00000001)
( W4 G% X1 |- t- r$ G9 v - #define RCC_APB2Periph_GPIOA ((uint32_t)0x00000004)
% N5 j$ W1 U: Y8 _( F7 m - #define RCC_APB2Periph_GPIOB ((uint32_t)0x00000008)( v7 G6 Y7 \! p/ Y% g; } ^: |5 q
- #define RCC_APB2Periph_GPIOC ((uint32_t)0x00000010)2 \+ F6 o/ p# { n6 J6 W* Y# a0 [
- #define RCC_APB2Periph_GPIOD ((uint32_t)0x00000020)
6 C1 x; m. c) b - #define RCC_APB2Periph_GPIOE ((uint32_t)0x00000040)& T4 t5 m7 g4 d" o' S! R
- #define RCC_APB2Periph_GPIOF ((uint32_t)0x00000080)7 J# g ^# j# h1 @5 H
- #define RCC_APB2Periph_GPIOG ((uint32_t)0x00000100)
( X$ G5 v P3 G* R1 `1 M( ]: ` - #define RCC_APB2Periph_ADC1 ((uint32_t)0x00000200)9 f. Q( l3 H' x% C+ l
- #define RCC_APB2Periph_ADC2 ((uint32_t)0x00000400)
0 h. M/ y& x) q0 n4 z( b( Y - #define RCC_APB2Periph_TIM1 ((uint32_t)0x00000800)1 c G0 D. }- ~! E0 Q2 U
- #define RCC_APB2Periph_SPI1 ((uint32_t)0x00001000)3 P. ]; y) Y6 ], N+ s8 t/ m
- #define RCC_APB2Periph_TIM8 ((uint32_t)0x00002000)
. p7 B8 k K3 t' m4 m2 w - #define RCC_APB2Periph_USART1 ((uint32_t)0x00004000)' D: V' s3 e$ U9 C5 |& R
- #define RCC_APB2Periph_ADC3 ((uint32_t)0x00008000)' E, }8 i4 e. h1 i, a8 R, t
- #define RCC_APB2Periph_TIM15 ((uint32_t)0x00010000)$ N4 U2 c4 o8 R: U1 k. q$ E
- #define RCC_APB2Periph_TIM16 ((uint32_t)0x00020000)
" Y( w# e% D* Q - #define RCC_APB2Periph_TIM17 ((uint32_t)0x00040000)$ ^1 b1 v& I+ n
- #define RCC_APB2Periph_TIM9 ((uint32_t)0x00080000); P" R/ I+ Y* u) m6 {: o3 v
- #define RCC_APB2Periph_TIM10 ((uint32_t)0x00100000)0 V) n9 I+ H: M% P8 d0 R7 T J' N- W
- #define RCC_APB2Periph_TIM11 ((uint32_t)0x00200000)
- _ z& r& ?5 {. F
2 L: A/ s. m2 [: m! J9 w! X. k- #define IS_RCC_APB2_PERIPH(PERIPH) ((((PERIPH) & 0xFFC00002) == 0x00) && ((PERIPH) != 0x00))
复制代码 2 m% T7 C+ u( A
APB2 外设时钟使能寄存器(RCC_APB2ENR)如下,可见APB2_peripheral的宏定义正好对应了寄存器RCC_APB2ENR的对应位。% _, E) W" _$ Q& d7 v
4 Z; U4 h5 l+ A& I' J/ }5 T. _IS_RCC_APB2_PERIPH(PERIPH)宏定义实现了输入判断,当输入参数不符合RCC_APB2Periph的数据格式时,返回0。
# Y3 t) o, H: s5 U7 a- b- z' t$ A5 @- q; G. {$ P2 R5 Z: z
' f: u/ z4 X& \9 }# D
$ u6 ]7 M9 ?' P% i. e- if (NewState != DISABLE)
% }+ i* U: e; N! I - {6 p6 {: n3 m& o% O" G% D
- RCC->APB2ENR |= RCC_APB2Periph;: l, \0 w# k: `1 [
- }
, ^" Y# H" H! s+ L/ U! O K) v9 c - else
S! X6 f+ G: d$ c7 T1 ]# S: y - {, ]) Z; I. j; u' p
- RCC->APB2ENR &= ~RCC_APB2Periph;
, W) I, ]& h" l9 x) p9 ? - }
复制代码 ! E3 D7 f2 c, f$ u) s% c, N$ B! O
这段代码是开启对应APB2外设时钟的主要实现语句。
; W. T2 X$ n) K0 N8 }7 E
9 A1 Z. p, P. {! j如下,RCC是指向RCC_TypeDef结构体类型的指针,RCC_BASE就是RCC寄存器的映射地址,也就是RCC指针的值。
. _- ?8 l" v$ J! r$ \& Z9 s! B! {- x* W' m
- #define RCC ((RCC_TypeDef *) RCC_BASE)
复制代码 0 _6 z0 O; n# j& Y/ \# o3 w
RCC_TypeDef结构体的定义如下,
5 M* w2 n* q& `- R3 z- B4 W/ V% k' W, j) Q J% q& B
- /** % l) w# U- S* d( t2 N5 j
- * @brief Reset and Clock Control
c# T9 Y5 A1 Q4 S/ i. K' U - */. Q' r. Z3 h' d4 D A# R
' S) t& g% R2 g& p$ F9 M- typedef struct
/ G, U8 ?' L$ s1 \* `0 A; D* D - {5 p5 K% n' l& v6 |' C+ W' S
- __IO uint32_t CR;" O U; t8 O; l. s) E5 F
- __IO uint32_t CFGR;0 W& J. k5 r( [' v0 J, H) w1 t
- __IO uint32_t CIR;- V, _' y! T9 h; x) t- a Y
- __IO uint32_t APB2RSTR;
+ s' [. U6 O% K8 k - __IO uint32_t APB1RSTR;# M" ?' |, `8 P. C1 T7 j& r
- __IO uint32_t AHBENR;
1 u i% `+ x5 f0 c. Y - __IO uint32_t APB2ENR;
3 R9 R- d' _- @2 c( E% x - __IO uint32_t APB1ENR;
# L2 g, J: y y+ j - __IO uint32_t BDCR;
7 I" o% a+ E ]/ p3 v - __IO uint32_t CSR;
! l; D3 X6 U9 Y# ?3 x! W7 _# H - #ifdef STM32F10X_CL : U$ B) v: _0 \; ?+ l5 p, a m8 R
- __IO uint32_t AHBRSTR;; z. W' c3 G" T/ q: Y. f7 Z/ U
- __IO uint32_t CFGR2;
7 W% G" g& ~; j R - #endif /* STM32F10X_CL */
% `0 X" Y/ z/ i* U8 | - #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
! r9 P( S! W, O" I8 ^ - uint32_t RESERVED0;
0 S% F; k) }2 k2 @2 z - __IO uint32_t CFGR2;5 ^5 b" G* ]9 R" Q: U W7 z
- #endif /* STM32F10X_LD_VL || STM32F10X_MD_VL || STM32F10X_HD_VL */ & [- Z4 |0 _) J' Y2 Y
- } RCC_TypeDef;
复制代码
5 m1 P' [2 U' P0 P( ?5 h* I8 `& `
" b3 P" ^" A7 c* K$ H" |/ D9 v/ m
' Z4 L7 x) Z1 D% ^/ B. o1 `! [0 V |