RCC_APB2PeriphClockCmd函数用来开启或关闭APB2外设时钟,
( F! c; I( l* @) |& J& \
# T3 I( w1 T f2 D# D该函数总览如下,
) T1 q% s# |( Y, Y6 E( h8 f
6 F0 D1 w# ?$ o- /**8 u# |! g5 F+ C- N8 P) J* n1 }; O
- * @brief Enables or disables the High Speed APB (APB2) peripheral clock.# R( w" D3 \7 D. N& ]: x2 s: G- ~
- * @param RCC_APB2Periph: specifies the APB2 peripheral to gates its clock.
9 l+ y7 ]8 ~2 K9 ?# S- l; E - * This parameter can be any combination of the following values:- D6 g' G, _$ C0 Z3 {
- * @arg RCC_APB2Periph_AFIO, RCC_APB2Periph_GPIOA, RCC_APB2Periph_GPIOB,
' c! g8 i3 C m& R4 P - * RCC_APB2Periph_GPIOC, RCC_APB2Periph_GPIOD, RCC_APB2Periph_GPIOE,
! X. Q7 U% j3 v% z' W: m d- j - * RCC_APB2Periph_GPIOF, RCC_APB2Periph_GPIOG, RCC_APB2Periph_ADC1,- Q4 |4 o6 V; q% L& i2 F: e) P
- * RCC_APB2Periph_ADC2, RCC_APB2Periph_TIM1, RCC_APB2Periph_SPI1,% V3 e5 Y4 A1 r. P
- * RCC_APB2Periph_TIM8, RCC_APB2Periph_USART1, RCC_APB2Periph_ADC3,2 P5 u5 B* [, P1 z/ |
- * RCC_APB2Periph_TIM15, RCC_APB2Periph_TIM16, RCC_APB2Periph_TIM17,+ |5 i: N/ B/ `9 J6 L" m Z
- * RCC_APB2Periph_TIM9, RCC_APB2Periph_TIM10, RCC_APB2Periph_TIM11 ( j0 e7 k: c T# U
- * @param NewState: new state of the specified peripheral clock.: e3 }& p( ~+ O0 n$ U; O
- * This parameter can be: ENABLE or DISABLE.; h2 m$ y+ U, v& }
- * @retval None7 y0 m( V- x1 d. `7 }
- */
8 p0 h- {% K! |1 s: E% |; s0 D - void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)9 R# [9 A) I8 l b/ g6 z1 m! k
- {
" B! ?6 I: `! r4 `5 A - /* Check the parameters */
d/ a& Q& H7 X3 O8 I9 N) j2 q - assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));
! q6 k! g; a: @, `/ G* {2 ]( s9 ^ - assert_param(IS_FUNCTIONAL_STATE(NewState));
7 y8 f( f1 U$ z, x7 K - if (NewState != DISABLE)7 n' o6 r2 c' w! A3 J* ~4 E5 I7 |. \
- {* i5 G' e2 G2 F$ @ T
- RCC->APB2ENR |= RCC_APB2Periph;3 X: [. e/ S+ J9 ^# t5 m
- }
2 W3 Z0 M: p0 x- I0 ^( T - else. m/ }7 U4 D/ ?
- {
9 I) C) ?: }3 J' M9 h& `7 h - RCC->APB2ENR &= ~RCC_APB2Periph;
$ S. z) k2 v( _$ ?" l - }
* Q# m+ |9 t5 e, R# d. l& x - }
复制代码 ) w/ j9 d3 L/ s6 U: M, A( b* n
" X5 v: ~ t7 e" ^$ x0 ^如下,FunctionalState是枚举类型,DISABLE 值为0,ENABLE 值为1,使用该参数判断是否要开相应的时钟。
/ q( w5 ?# t$ Q3 _( n2 l2 y
! S5 d: G' ?8 p0 D% s) o: c- typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState;
复制代码
% I8 m. v9 F; I" p( `在stm32f10x_rcc.h中,有APB2_peripheral的宏定义。, t+ M5 a9 }7 q4 }+ J3 K
1 \) M1 R: h. {* q! P. |/ w- /** @defgroup APB2_peripheral % W* P3 x" [! P3 C0 b
- * @{
) u: {: r( i/ h - */
6 w" L+ ^* L, P9 ?, ]- \
# X1 O' C& @8 t* r- #define RCC_APB2Periph_AFIO ((uint32_t)0x00000001)1 H6 ^; L" h4 Z: s4 _5 \2 h
- #define RCC_APB2Periph_GPIOA ((uint32_t)0x00000004)
/ d4 M9 f/ R8 {9 r- G+ s. u2 Y2 d) z. [ - #define RCC_APB2Periph_GPIOB ((uint32_t)0x00000008)5 t7 T; C3 t; E
- #define RCC_APB2Periph_GPIOC ((uint32_t)0x00000010)
" j- i6 h: @* x8 z: `2 ?# v) K - #define RCC_APB2Periph_GPIOD ((uint32_t)0x00000020)7 a i4 [# \) x$ o
- #define RCC_APB2Periph_GPIOE ((uint32_t)0x00000040)
1 E' z; r5 K$ ^ - #define RCC_APB2Periph_GPIOF ((uint32_t)0x00000080)! s E6 V5 [2 D4 ~' f
- #define RCC_APB2Periph_GPIOG ((uint32_t)0x00000100)8 H, F' z6 ]% ^/ \2 W& Q6 n
- #define RCC_APB2Periph_ADC1 ((uint32_t)0x00000200)
5 U% j7 |- @ f/ z& P - #define RCC_APB2Periph_ADC2 ((uint32_t)0x00000400)' u% v5 s- C( y* ~! I& W
- #define RCC_APB2Periph_TIM1 ((uint32_t)0x00000800)# K6 O" W' n4 a# ~" z5 C
- #define RCC_APB2Periph_SPI1 ((uint32_t)0x00001000)
9 A3 q) ?" j' ^4 i o; J - #define RCC_APB2Periph_TIM8 ((uint32_t)0x00002000)5 ^ d2 i7 g9 r3 u( z
- #define RCC_APB2Periph_USART1 ((uint32_t)0x00004000)
/ }, V7 O3 r& j- @ - #define RCC_APB2Periph_ADC3 ((uint32_t)0x00008000)! g7 O# u M2 @5 r& x' H
- #define RCC_APB2Periph_TIM15 ((uint32_t)0x00010000)
8 i; V' N: H/ H+ f0 x# K& M - #define RCC_APB2Periph_TIM16 ((uint32_t)0x00020000)2 n( G. S8 _( A7 p6 k
- #define RCC_APB2Periph_TIM17 ((uint32_t)0x00040000)0 W" E% N4 m# }! F; i5 H/ H
- #define RCC_APB2Periph_TIM9 ((uint32_t)0x00080000)' ?+ v1 i+ Q. q7 o
- #define RCC_APB2Periph_TIM10 ((uint32_t)0x00100000)$ p8 X- F' ?6 L3 M
- #define RCC_APB2Periph_TIM11 ((uint32_t)0x00200000)1 e' t+ g/ `9 Q6 q
- 6 @6 G1 F/ e( s
- #define IS_RCC_APB2_PERIPH(PERIPH) ((((PERIPH) & 0xFFC00002) == 0x00) && ((PERIPH) != 0x00))
复制代码
/ |4 D8 b- c# a2 N8 p' ]2 u6 }APB2 外设时钟使能寄存器(RCC_APB2ENR)如下,可见APB2_peripheral的宏定义正好对应了寄存器RCC_APB2ENR的对应位。
# j0 a) A% F" O* I+ N& s+ S! ~0 P4 z* K7 g5 R# |' O( O$ I4 J4 L
IS_RCC_APB2_PERIPH(PERIPH)宏定义实现了输入判断,当输入参数不符合RCC_APB2Periph的数据格式时,返回0。$ G% l8 j9 _" B" `
$ g+ m' ?. T! Z2 V
' S- s3 y* a3 p2 N' n
& p; q+ _/ k* v( G
- if (NewState != DISABLE)0 E& Q( T) R9 I" y* p& @
- {
# ~, `# j7 h1 `' j - RCC->APB2ENR |= RCC_APB2Periph;; v0 M3 I: T0 e! x/ H/ C
- }
% x" w4 ? [( ? - else
8 Q7 t- l- b# p7 E: W; k% R1 h; q% D4 Q - {2 q* C7 q1 ^8 W3 l1 E
- RCC->APB2ENR &= ~RCC_APB2Periph;
% ^# A6 d# U B) f0 Y6 `0 p - }
复制代码
$ y8 S* r9 S. W7 h% {6 x) K这段代码是开启对应APB2外设时钟的主要实现语句。( z- X! R& t7 S* [
^2 f1 M% k9 ]. @- K
如下,RCC是指向RCC_TypeDef结构体类型的指针,RCC_BASE就是RCC寄存器的映射地址,也就是RCC指针的值。; a2 ^% G6 e# A4 E5 _
( U& J' h3 y5 p1 Y
- #define RCC ((RCC_TypeDef *) RCC_BASE)
复制代码
0 W; u4 E% }' Z' ` t) qRCC_TypeDef结构体的定义如下,
2 o( y$ O! }* i, X( w' H
+ y+ A. |, O3 C" p- /**
* y3 _# g5 Z5 P. y- t3 S - * @brief Reset and Clock Control
9 {4 g$ W3 ?# F6 r/ i- R - */6 N% K8 g3 x, h- r2 h2 b
) w# k7 N+ d3 |- typedef struct7 ~9 P; j- y+ J$ |* T, d
- {& \3 h7 z4 y8 [
- __IO uint32_t CR;
& D, ^! m3 g! G& U# m R6 |4 b" R - __IO uint32_t CFGR;1 [- i8 |( y6 I# k+ n
- __IO uint32_t CIR;
" A+ ~! T3 f0 |5 J, o - __IO uint32_t APB2RSTR;
- |9 s3 k; Z/ k - __IO uint32_t APB1RSTR;
/ ~: y" [; Z+ t3 c - __IO uint32_t AHBENR;5 z6 h# s6 I4 D9 z$ o" V
- __IO uint32_t APB2ENR;4 [9 S/ h o1 h
- __IO uint32_t APB1ENR;& H3 ^/ Q5 k1 ~0 P5 g: z
- __IO uint32_t BDCR;4 ?, y# y$ h" ~0 p5 Z/ t8 b" S
- __IO uint32_t CSR;( D: a: ^ ~7 r5 H
- #ifdef STM32F10X_CL
% Z. i8 o k& w& R# b$ ^) u. o - __IO uint32_t AHBRSTR;
, @% p" s7 Q* K, x9 } - __IO uint32_t CFGR2;
4 G9 N# M4 v" y& O - #endif /* STM32F10X_CL */ 2 N3 i7 C, P3 C" ?4 @1 z. B J
- #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
1 I* `# {0 n, K5 S1 Y) G" z - uint32_t RESERVED0;0 T6 S% N$ k# K4 k3 g. N
- __IO uint32_t CFGR2;- t! j3 L5 `+ P
- #endif /* STM32F10X_LD_VL || STM32F10X_MD_VL || STM32F10X_HD_VL */ ' E/ q8 o/ a! H" l0 g% j
- } RCC_TypeDef;
复制代码
: |, B; d" F: ^" c" J* R
& a( m1 v2 b+ x& n7 h! C4 C, j& {7 H/ Z" @) w8 V9 ~9 @- O
6 d# z) X0 M2 u! m k* L' T |