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

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

[复制链接]
STMCU小助手 发布时间:2022-5-2 09:13
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
  1. /**8 u# |! g5 F+ C- N8 P) J* n1 }; O
  2.   * @brief  Enables or disables the High Speed APB (APB2) peripheral clock.# R( w" D3 \7 D. N& ]: x2 s: G- ~
  3.   * @param  RCC_APB2Periph: specifies the APB2 peripheral to gates its clock.
    9 l+ y7 ]8 ~2 K9 ?# S- l; E
  4.   *   This parameter can be any combination of the following values:- D6 g' G, _$ C0 Z3 {
  5.   *     @arg RCC_APB2Periph_AFIO, RCC_APB2Periph_GPIOA, RCC_APB2Periph_GPIOB,
    ' c! g8 i3 C  m& R4 P
  6.   *          RCC_APB2Periph_GPIOC, RCC_APB2Periph_GPIOD, RCC_APB2Periph_GPIOE,
    ! X. Q7 U% j3 v% z' W: m  d- j
  7.   *          RCC_APB2Periph_GPIOF, RCC_APB2Periph_GPIOG, RCC_APB2Periph_ADC1,- Q4 |4 o6 V; q% L& i2 F: e) P
  8.   *          RCC_APB2Periph_ADC2, RCC_APB2Periph_TIM1, RCC_APB2Periph_SPI1,% V3 e5 Y4 A1 r. P
  9.   *          RCC_APB2Periph_TIM8, RCC_APB2Periph_USART1, RCC_APB2Periph_ADC3,2 P5 u5 B* [, P1 z/ |
  10.   *          RCC_APB2Periph_TIM15, RCC_APB2Periph_TIM16, RCC_APB2Periph_TIM17,+ |5 i: N/ B/ `9 J6 L" m  Z
  11.   *          RCC_APB2Periph_TIM9, RCC_APB2Periph_TIM10, RCC_APB2Periph_TIM11     ( j0 e7 k: c  T# U
  12.   * @param  NewState: new state of the specified peripheral clock.: e3 }& p( ~+ O0 n$ U; O
  13.   *   This parameter can be: ENABLE or DISABLE.; h2 m$ y+ U, v& }
  14.   * @retval None7 y0 m( V- x1 d. `7 }
  15.   */
    8 p0 h- {% K! |1 s: E% |; s0 D
  16. void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)9 R# [9 A) I8 l  b/ g6 z1 m! k
  17. {
    " B! ?6 I: `! r4 `5 A
  18.   /* Check the parameters */
      d/ a& Q& H7 X3 O8 I9 N) j2 q
  19.   assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));
    ! q6 k! g; a: @, `/ G* {2 ]( s9 ^
  20.   assert_param(IS_FUNCTIONAL_STATE(NewState));
    7 y8 f( f1 U$ z, x7 K
  21.   if (NewState != DISABLE)7 n' o6 r2 c' w! A3 J* ~4 E5 I7 |. \
  22.   {* i5 G' e2 G2 F$ @  T
  23.     RCC->APB2ENR |= RCC_APB2Periph;3 X: [. e/ S+ J9 ^# t5 m
  24.   }
    2 W3 Z0 M: p0 x- I0 ^( T
  25.   else. m/ }7 U4 D/ ?
  26.   {
    9 I) C) ?: }3 J' M9 h& `7 h
  27.     RCC->APB2ENR &= ~RCC_APB2Periph;
    $ S. z) k2 v( _$ ?" l
  28.   }
    * Q# m+ |9 t5 e, R# d. l& x
  29. }
复制代码
) 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
  1. 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
  1. /** @defgroup APB2_peripheral % W* P3 x" [! P3 C0 b
  2.   * @{
    ) u: {: r( i/ h
  3.   */
    6 w" L+ ^* L, P9 ?, ]- \

  4. # X1 O' C& @8 t* r
  5. #define RCC_APB2Periph_AFIO              ((uint32_t)0x00000001)1 H6 ^; L" h4 Z: s4 _5 \2 h
  6. #define RCC_APB2Periph_GPIOA             ((uint32_t)0x00000004)
    / d4 M9 f/ R8 {9 r- G+ s. u2 Y2 d) z. [
  7. #define RCC_APB2Periph_GPIOB             ((uint32_t)0x00000008)5 t7 T; C3 t; E
  8. #define RCC_APB2Periph_GPIOC             ((uint32_t)0x00000010)
    " j- i6 h: @* x8 z: `2 ?# v) K
  9. #define RCC_APB2Periph_GPIOD             ((uint32_t)0x00000020)7 a  i4 [# \) x$ o
  10. #define RCC_APB2Periph_GPIOE             ((uint32_t)0x00000040)
    1 E' z; r5 K$ ^
  11. #define RCC_APB2Periph_GPIOF             ((uint32_t)0x00000080)! s  E6 V5 [2 D4 ~' f
  12. #define RCC_APB2Periph_GPIOG             ((uint32_t)0x00000100)8 H, F' z6 ]% ^/ \2 W& Q6 n
  13. #define RCC_APB2Periph_ADC1              ((uint32_t)0x00000200)
    5 U% j7 |- @  f/ z& P
  14. #define RCC_APB2Periph_ADC2              ((uint32_t)0x00000400)' u% v5 s- C( y* ~! I& W
  15. #define RCC_APB2Periph_TIM1              ((uint32_t)0x00000800)# K6 O" W' n4 a# ~" z5 C
  16. #define RCC_APB2Periph_SPI1              ((uint32_t)0x00001000)
    9 A3 q) ?" j' ^4 i  o; J
  17. #define RCC_APB2Periph_TIM8              ((uint32_t)0x00002000)5 ^  d2 i7 g9 r3 u( z
  18. #define RCC_APB2Periph_USART1            ((uint32_t)0x00004000)
    / }, V7 O3 r& j- @
  19. #define RCC_APB2Periph_ADC3              ((uint32_t)0x00008000)! g7 O# u  M2 @5 r& x' H
  20. #define RCC_APB2Periph_TIM15             ((uint32_t)0x00010000)
    8 i; V' N: H/ H+ f0 x# K& M
  21. #define RCC_APB2Periph_TIM16             ((uint32_t)0x00020000)2 n( G. S8 _( A7 p6 k
  22. #define RCC_APB2Periph_TIM17             ((uint32_t)0x00040000)0 W" E% N4 m# }! F; i5 H/ H
  23. #define RCC_APB2Periph_TIM9              ((uint32_t)0x00080000)' ?+ v1 i+ Q. q7 o
  24. #define RCC_APB2Periph_TIM10             ((uint32_t)0x00100000)$ p8 X- F' ?6 L3 M
  25. #define RCC_APB2Periph_TIM11             ((uint32_t)0x00200000)1 e' t+ g/ `9 Q6 q
  26. 6 @6 G1 F/ e( s
  27. #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 4T8[C(JI8{E4GB(`WEFK@VR.png ' S- s3 y* a3 p2 N' n
& p; q+ _/ k* v( G
  1. if (NewState != DISABLE)0 E& Q( T) R9 I" y* p& @
  2.   {
    # ~, `# j7 h1 `' j
  3.     RCC->APB2ENR |= RCC_APB2Periph;; v0 M3 I: T0 e! x/ H/ C
  4.   }
    % x" w4 ?  [( ?
  5.   else
    8 Q7 t- l- b# p7 E: W; k% R1 h; q% D4 Q
  6.   {2 q* C7 q1 ^8 W3 l1 E
  7.     RCC->APB2ENR &= ~RCC_APB2Periph;
    % ^# A6 d# U  B) f0 Y6 `0 p
  8.   }
复制代码

$ 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
  1. #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
  1. /**
    * y3 _# g5 Z5 P. y- t3 S
  2.   * @brief Reset and Clock Control
    9 {4 g$ W3 ?# F6 r/ i- R
  3.   */6 N% K8 g3 x, h- r2 h2 b

  4. ) w# k7 N+ d3 |
  5. typedef struct7 ~9 P; j- y+ J$ |* T, d
  6. {& \3 h7 z4 y8 [
  7.   __IO uint32_t CR;
    & D, ^! m3 g! G& U# m  R6 |4 b" R
  8.   __IO uint32_t CFGR;1 [- i8 |( y6 I# k+ n
  9.   __IO uint32_t CIR;
    " A+ ~! T3 f0 |5 J, o
  10.   __IO uint32_t APB2RSTR;
    - |9 s3 k; Z/ k
  11.   __IO uint32_t APB1RSTR;
    / ~: y" [; Z+ t3 c
  12.   __IO uint32_t AHBENR;5 z6 h# s6 I4 D9 z$ o" V
  13.   __IO uint32_t APB2ENR;4 [9 S/ h  o1 h
  14.   __IO uint32_t APB1ENR;& H3 ^/ Q5 k1 ~0 P5 g: z
  15.   __IO uint32_t BDCR;4 ?, y# y$ h" ~0 p5 Z/ t8 b" S
  16.   __IO uint32_t CSR;( D: a: ^  ~7 r5 H
  17. #ifdef STM32F10X_CL  
    % Z. i8 o  k& w& R# b$ ^) u. o
  18.   __IO uint32_t AHBRSTR;
    , @% p" s7 Q* K, x9 }
  19.   __IO uint32_t CFGR2;
    4 G9 N# M4 v" y& O
  20. #endif /* STM32F10X_CL */ 2 N3 i7 C, P3 C" ?4 @1 z. B  J
  21. #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)   
    1 I* `# {0 n, K5 S1 Y) G" z
  22.   uint32_t RESERVED0;0 T6 S% N$ k# K4 k3 g. N
  23.   __IO uint32_t CFGR2;- t! j3 L5 `+ P
  24. #endif /* STM32F10X_LD_VL || STM32F10X_MD_VL || STM32F10X_HD_VL */ ' E/ q8 o/ a! H" l0 g% j
  25. } 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
赞 收藏 评论0 发布时间:2022-5-2 09:13

举报

0个回答

所属标签

相似技术帖

官网相关资源

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