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

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

[复制链接]
STMCU小助手 发布时间:2022-5-2 09:13
RCC_APB2PeriphClockCmd函数用来开启或关闭APB2外设时钟,

该函数总览如下,

  1. /**
  2.   * @brief  Enables or disables the High Speed APB (APB2) peripheral clock.
  3.   * @param  RCC_APB2Periph: specifies the APB2 peripheral to gates its clock.
  4.   *   This parameter can be any combination of the following values:
  5.   *     @arg RCC_APB2Periph_AFIO, RCC_APB2Periph_GPIOA, RCC_APB2Periph_GPIOB,
  6.   *          RCC_APB2Periph_GPIOC, RCC_APB2Periph_GPIOD, RCC_APB2Periph_GPIOE,
  7.   *          RCC_APB2Periph_GPIOF, RCC_APB2Periph_GPIOG, RCC_APB2Periph_ADC1,
  8.   *          RCC_APB2Periph_ADC2, RCC_APB2Periph_TIM1, RCC_APB2Periph_SPI1,
  9.   *          RCC_APB2Periph_TIM8, RCC_APB2Periph_USART1, RCC_APB2Periph_ADC3,
  10.   *          RCC_APB2Periph_TIM15, RCC_APB2Periph_TIM16, RCC_APB2Periph_TIM17,
  11.   *          RCC_APB2Periph_TIM9, RCC_APB2Periph_TIM10, RCC_APB2Periph_TIM11     
  12.   * @param  NewState: new state of the specified peripheral clock.
  13.   *   This parameter can be: ENABLE or DISABLE.
  14.   * @retval None
  15.   */
  16. void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
  17. {
  18.   /* Check the parameters */
  19.   assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));
  20.   assert_param(IS_FUNCTIONAL_STATE(NewState));
  21.   if (NewState != DISABLE)
  22.   {
  23.     RCC->APB2ENR |= RCC_APB2Periph;
  24.   }
  25.   else
  26.   {
  27.     RCC->APB2ENR &= ~RCC_APB2Periph;
  28.   }
  29. }
复制代码


如下,FunctionalState是枚举类型,DISABLE 值为0,ENABLE 值为1,使用该参数判断是否要开相应的时钟。

  1. typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState;
复制代码

在stm32f10x_rcc.h中,有APB2_peripheral的宏定义。

  1. /** @defgroup APB2_peripheral
  2.   * @{
  3.   */

  4. #define RCC_APB2Periph_AFIO              ((uint32_t)0x00000001)
  5. #define RCC_APB2Periph_GPIOA             ((uint32_t)0x00000004)
  6. #define RCC_APB2Periph_GPIOB             ((uint32_t)0x00000008)
  7. #define RCC_APB2Periph_GPIOC             ((uint32_t)0x00000010)
  8. #define RCC_APB2Periph_GPIOD             ((uint32_t)0x00000020)
  9. #define RCC_APB2Periph_GPIOE             ((uint32_t)0x00000040)
  10. #define RCC_APB2Periph_GPIOF             ((uint32_t)0x00000080)
  11. #define RCC_APB2Periph_GPIOG             ((uint32_t)0x00000100)
  12. #define RCC_APB2Periph_ADC1              ((uint32_t)0x00000200)
  13. #define RCC_APB2Periph_ADC2              ((uint32_t)0x00000400)
  14. #define RCC_APB2Periph_TIM1              ((uint32_t)0x00000800)
  15. #define RCC_APB2Periph_SPI1              ((uint32_t)0x00001000)
  16. #define RCC_APB2Periph_TIM8              ((uint32_t)0x00002000)
  17. #define RCC_APB2Periph_USART1            ((uint32_t)0x00004000)
  18. #define RCC_APB2Periph_ADC3              ((uint32_t)0x00008000)
  19. #define RCC_APB2Periph_TIM15             ((uint32_t)0x00010000)
  20. #define RCC_APB2Periph_TIM16             ((uint32_t)0x00020000)
  21. #define RCC_APB2Periph_TIM17             ((uint32_t)0x00040000)
  22. #define RCC_APB2Periph_TIM9              ((uint32_t)0x00080000)
  23. #define RCC_APB2Periph_TIM10             ((uint32_t)0x00100000)
  24. #define RCC_APB2Periph_TIM11             ((uint32_t)0x00200000)

  25. #define IS_RCC_APB2_PERIPH(PERIPH) ((((PERIPH) & 0xFFC00002) == 0x00) && ((PERIPH) != 0x00))
复制代码

APB2 外设时钟使能寄存器(RCC_APB2ENR)如下,可见APB2_peripheral的宏定义正好对应了寄存器RCC_APB2ENR的对应位。

IS_RCC_APB2_PERIPH(PERIPH)宏定义实现了输入判断,当输入参数不符合RCC_APB2Periph的数据格式时,返回0。

4T8[C(JI8{E4GB(`WEFK@VR.png

  1. if (NewState != DISABLE)
  2.   {
  3.     RCC->APB2ENR |= RCC_APB2Periph;
  4.   }
  5.   else
  6.   {
  7.     RCC->APB2ENR &= ~RCC_APB2Periph;
  8.   }
复制代码

这段代码是开启对应APB2外设时钟的主要实现语句。

如下,RCC是指向RCC_TypeDef结构体类型的指针,RCC_BASE就是RCC寄存器的映射地址,也就是RCC指针的值。

  1. #define RCC                 ((RCC_TypeDef *) RCC_BASE)
复制代码

RCC_TypeDef结构体的定义如下,

  1. /**
  2.   * @brief Reset and Clock Control
  3.   */

  4. typedef struct
  5. {
  6.   __IO uint32_t CR;
  7.   __IO uint32_t CFGR;
  8.   __IO uint32_t CIR;
  9.   __IO uint32_t APB2RSTR;
  10.   __IO uint32_t APB1RSTR;
  11.   __IO uint32_t AHBENR;
  12.   __IO uint32_t APB2ENR;
  13.   __IO uint32_t APB1ENR;
  14.   __IO uint32_t BDCR;
  15.   __IO uint32_t CSR;
  16. #ifdef STM32F10X_CL  
  17.   __IO uint32_t AHBRSTR;
  18.   __IO uint32_t CFGR2;
  19. #endif /* STM32F10X_CL */
  20. #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)   
  21.   uint32_t RESERVED0;
  22.   __IO uint32_t CFGR2;
  23. #endif /* STM32F10X_LD_VL || STM32F10X_MD_VL || STM32F10X_HD_VL */
  24. } RCC_TypeDef;
复制代码




收藏 评论0 发布时间:2022-5-2 09:13

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版