你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】STM32F103固件库源码解析——RCC_APB2PeriphClockCmd

[复制链接]
STMCU小助手 发布时间:2022-5-2 09:13
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
  1. /**
    $ K9 g* S5 p% d
  2.   * @brief  Enables or disables the High Speed APB (APB2) peripheral clock.
    ( M, ?4 C7 E2 x* j8 r
  3.   * @param  RCC_APB2Periph: specifies the APB2 peripheral to gates its clock.
    . [: d: U- Q! b" X( i
  4.   *   This parameter can be any combination of the following values:
    ( O# e/ A" s% p! a
  5.   *     @arg RCC_APB2Periph_AFIO, RCC_APB2Periph_GPIOA, RCC_APB2Periph_GPIOB,0 m6 b$ p7 V: M) @, h7 _. X
  6.   *          RCC_APB2Periph_GPIOC, RCC_APB2Periph_GPIOD, RCC_APB2Periph_GPIOE,4 O1 Q& w& J4 `# V; B, e
  7.   *          RCC_APB2Periph_GPIOF, RCC_APB2Periph_GPIOG, RCC_APB2Periph_ADC1,
    & A% t* n( p: @( w8 f- U. y
  8.   *          RCC_APB2Periph_ADC2, RCC_APB2Periph_TIM1, RCC_APB2Periph_SPI1,
    ) P8 k+ }4 A; v9 w1 r# w7 b6 G
  9.   *          RCC_APB2Periph_TIM8, RCC_APB2Periph_USART1, RCC_APB2Periph_ADC3,; [/ o8 L* `. ]  Y* s
  10.   *          RCC_APB2Periph_TIM15, RCC_APB2Periph_TIM16, RCC_APB2Periph_TIM17,4 X# ?9 p0 v8 K7 f6 ^. ~
  11.   *          RCC_APB2Periph_TIM9, RCC_APB2Periph_TIM10, RCC_APB2Periph_TIM11     
    ; [1 D! R( ]& x! C
  12.   * @param  NewState: new state of the specified peripheral clock.
    3 f  N/ u0 C$ l
  13.   *   This parameter can be: ENABLE or DISABLE.1 I/ o; [  s  X' a" z, U% [1 M5 A
  14.   * @retval None
    2 u2 P. U+ a0 t* q$ j( i9 O% w
  15.   */' `7 B' N3 v; T: r- J
  16. void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)4 J$ D2 S0 l# k
  17. {
    2 [. u$ G. T, m  A
  18.   /* Check the parameters */2 F5 z, s; }6 ~! I  |5 k, ?  {
  19.   assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));" G$ H! @, b; J+ S* x9 S3 r
  20.   assert_param(IS_FUNCTIONAL_STATE(NewState));
    $ G2 q3 ^2 O7 T6 k/ o+ s+ [, H
  21.   if (NewState != DISABLE)
    . r& V5 g" b7 I9 ^7 Y( `
  22.   {
    0 Y. v4 k# T: p7 c7 H- e2 @2 Q
  23.     RCC->APB2ENR |= RCC_APB2Periph;
    % z) T: \1 F) S( w; I
  24.   }
    / t; ~: q' ]  l' _4 T/ V" {/ y
  25.   else- H, l6 |4 i" z
  26.   {9 z0 C- n% x0 N9 V9 x$ f
  27.     RCC->APB2ENR &= ~RCC_APB2Periph;3 m) w' y/ \: C
  28.   }; r6 G' Q% J# _; H/ u, O+ L  w
  29. }
复制代码
" 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
  1. 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
  1. /** @defgroup APB2_peripheral
      ]$ e9 h1 R; U+ h; {/ R
  2.   * @{: O, i  B0 s" \3 b2 ?/ Z
  3.   */
    9 L' }! U$ Y- D0 b2 N% l1 Y& T: U4 J

  4. 5 E9 y! A( e5 D' V- c
  5. #define RCC_APB2Periph_AFIO              ((uint32_t)0x00000001)
    : n7 O! s1 Z- p7 X) s. r
  6. #define RCC_APB2Periph_GPIOA             ((uint32_t)0x00000004)
    & A" p6 ]* Q6 ^. A
  7. #define RCC_APB2Periph_GPIOB             ((uint32_t)0x00000008)# k  I+ s  @3 j
  8. #define RCC_APB2Periph_GPIOC             ((uint32_t)0x00000010)
    & L5 g' U! G3 f% Y6 x0 }% G3 J7 g
  9. #define RCC_APB2Periph_GPIOD             ((uint32_t)0x00000020)! H- c$ c/ n6 z; B# J" n
  10. #define RCC_APB2Periph_GPIOE             ((uint32_t)0x00000040)
    ! s8 j2 V' \1 h3 [" K4 L  G
  11. #define RCC_APB2Periph_GPIOF             ((uint32_t)0x00000080)
    - z' a) r; F- G7 f/ G0 b
  12. #define RCC_APB2Periph_GPIOG             ((uint32_t)0x00000100)
    * R# K- k% E# _5 t! c
  13. #define RCC_APB2Periph_ADC1              ((uint32_t)0x00000200)
    ( T. h/ r: y. {
  14. #define RCC_APB2Periph_ADC2              ((uint32_t)0x00000400)
    9 Q% m) T$ [' K" k
  15. #define RCC_APB2Periph_TIM1              ((uint32_t)0x00000800)1 p( o5 q- g0 W' m1 @
  16. #define RCC_APB2Periph_SPI1              ((uint32_t)0x00001000)0 X2 g; b# d9 _$ b2 d
  17. #define RCC_APB2Periph_TIM8              ((uint32_t)0x00002000)( n" a7 v4 n2 h5 d- ~3 Q1 u
  18. #define RCC_APB2Periph_USART1            ((uint32_t)0x00004000)
    / m3 h+ ~$ z3 \9 z- L" B0 s. Z! L
  19. #define RCC_APB2Periph_ADC3              ((uint32_t)0x00008000)
    6 k* ?! t8 _" r) {
  20. #define RCC_APB2Periph_TIM15             ((uint32_t)0x00010000)
    ! Z( R% n1 [/ \: j' s$ `" e
  21. #define RCC_APB2Periph_TIM16             ((uint32_t)0x00020000)
    # f$ t" n2 l; R- u5 E1 S
  22. #define RCC_APB2Periph_TIM17             ((uint32_t)0x00040000)
    / l% z) F- r. [; u
  23. #define RCC_APB2Periph_TIM9              ((uint32_t)0x00080000)
    8 T9 [9 c; I( J
  24. #define RCC_APB2Periph_TIM10             ((uint32_t)0x00100000)" v& K' g6 H* F  R# v
  25. #define RCC_APB2Periph_TIM11             ((uint32_t)0x00200000)
    * r% E' e3 l0 w) b$ e8 s0 a

  26. $ I: P! s. \9 g( m4 p+ T7 e- ?% `
  27. #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: { 4T8[C(JI8{E4GB(`WEFK@VR.png 3 Y9 U" Z, H0 b  t  ?
9 f" z# i. y7 q/ U4 k
  1. if (NewState != DISABLE)' \1 b- ]5 P  V8 M  b9 ]" {7 p; Z
  2.   {
    0 k! l6 f- B$ a
  3.     RCC->APB2ENR |= RCC_APB2Periph;& J% L! |+ ?6 A& o
  4.   }
    ) Z! ]5 b* }' c( Y* D0 T2 N
  5.   else
    9 }% J& I' s  T( Y! ^
  6.   {
    + T7 \/ G6 B7 Q- I) m" W
  7.     RCC->APB2ENR &= ~RCC_APB2Periph;- Q: ^6 i* r9 |, m5 b- ~
  8.   }
复制代码
' 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
  1. #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
  1. /**
    . |" Z9 J$ k! F5 x3 j: Y, a
  2.   * @brief Reset and Clock Control3 x2 G( J/ y0 h" i4 d
  3.   */
    $ A) H( a! V" H# G; X+ ^" [) k
  4. 5 ?2 V1 H* ], U6 c: e% O  e8 }
  5. typedef struct
    2 S9 a+ ]& j& [; H
  6. {
    ! P$ E* N1 ~% n' k7 P
  7.   __IO uint32_t CR;
    ( \% Y, c, T" ~9 ~8 G% c
  8.   __IO uint32_t CFGR;, _" P6 Z1 ?# @  Z0 p
  9.   __IO uint32_t CIR;* Y% f% P  |' E
  10.   __IO uint32_t APB2RSTR;
    / r! a- w4 H& a4 h/ a* [
  11.   __IO uint32_t APB1RSTR;
    ) }0 @6 }, _7 Q6 K# j' p
  12.   __IO uint32_t AHBENR;) _1 I) J" Y9 x0 w  }& L
  13.   __IO uint32_t APB2ENR;
    $ d+ X  c' x) h1 M" H
  14.   __IO uint32_t APB1ENR;
    # ?' G$ e3 a9 B$ a2 T4 w
  15.   __IO uint32_t BDCR;: }, u+ ]: S7 ^2 J; ]- G% \
  16.   __IO uint32_t CSR;; C& h) J" y' B- Z. I- ^
  17. #ifdef STM32F10X_CL  7 Q( @( q8 d& s- {! o' O1 r- @; s
  18.   __IO uint32_t AHBRSTR;* ^3 N+ C6 ]" l- b1 n0 m# V
  19.   __IO uint32_t CFGR2;
    $ a# a, O, }, Y5 C
  20. #endif /* STM32F10X_CL */ ( Z/ T5 [; Q9 _; h
  21. #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)   8 S4 ~& C. @( \! G/ U
  22.   uint32_t RESERVED0;) ^8 X  V+ _7 _
  23.   __IO uint32_t CFGR2;, K8 P. Z3 l& R( s
  24. #endif /* STM32F10X_LD_VL || STM32F10X_MD_VL || STM32F10X_HD_VL */ 7 f$ t" i  h# Y/ i0 E
  25. } 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- `
赞 收藏 评论0 发布时间:2022-5-2 09:13

举报

0个回答

所属标签

相似技术帖

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版