1. 程式人生 > >STM32F系列ARM Cortex-M3核微控制器基礎之系統時鐘一

STM32F系列ARM Cortex-M3核微控制器基礎之系統時鐘一

STM32F系列ARM Cortex-M3核微控制器基礎之系統時鐘   本文章是基於STM32F103ZE微控制器,主要是詳細說明STM32F系列的特性,由於本人是初學者,出現錯誤是難免的,請大家見諒。韌體庫採用V3.5.0版本   下面是STM32的時鐘樹        
    為使講解更加清晰,我們把圖分解開來,一部分一部分來看:   
1、HSI高速內部時鐘,RC振盪器,頻率為8MHz。
2、HSE高速外部時鐘,可接石英/陶瓷諧振器,或接外部時鐘源,頻率範圍為4MHz~16MHz。 3、LSI低速內部時鐘,RC振盪器,頻率為40kHz。 4、LSE低速外部時鐘,接頻率為32.768kHz的石英晶體。 如果將時鐘系統看作是一顆大樹,那麼1,2,3,4部分可以看作為樹的根部。這裡是時鐘產生的地方,有關這部分的講解後面會提到。      下面說的是時鐘樹的主要軀幹部分: 圖中的SYSCLK是系統時鐘(最大為72MHz),有三種不同的時鐘源可以用來驅動系統時鐘:HSI振盪器時鐘,HSE振盪器時鐘,PLL時鐘。這三個時鐘源由SW確定。(SW是時鐘配置暫存器RCC_CFGR中的第1:和0位:系統時鐘切換 System clock switch)。
系統復位後, HSI振盪器被選為系統時鐘。當時鍾源被直接或通過PLL間接作為系統時鐘時,它將不能被停止。只有當目標時鐘源準備就緒了(經過啟動穩定階段的延遲或PLL穩定),從一個時鐘源到另一個時鐘源的切換才會發生。在被選擇時鐘源沒有就緒時,系統時鐘的切換不會發生。直至目標時鐘源就緒,才發生切換。在時鐘控制暫存器(RCC_CR)裡的狀態位指示哪個時鐘已經準備好了,哪個時鐘目前被用作系統時鐘。I2S音訊匯流排時鐘直接來源於系統時鐘。
  








              
                   










/**
  * @brief  Configures the system clock (SYSCLK).
  配置系統時鐘
  * @param  RCC_SYSCLKSource: specifies the clock source used as system clock.
  指定時鐘源作為系統時鐘
  *   This parameter can be one of the following values:
  *     @arg RCC_SYSCLKSource_HSI: HSI selected as system clock HSI選為系統時鐘
             #define RCC_SYSCLKSource_HSI ((uint32_t)0x00000000)
  *     @arg RCC_SYSCLKSource_HSE: HSE selected as system clock HSE選為系統時鐘
             #define RCC_SYSCLKSource_HSE ((uint32_t)0x00000001)
  *     @arg RCC_SYSCLKSource_PLLCLK: PLL selected as system clock PLL選為系統時鐘
             #define RCC_SYSCLKSource_PLLCLK ((uint32_t)0x00000002)
  * @retval None
  */
void RCC_SYSCLKConfig(uint32_t RCC_SYSCLKSource)
{
  uint32_t tmpreg = 0;
  /* Check the parameters */
  assert_param(IS_RCC_SYSCLK_SOURCE(RCC_SYSCLKSource));
  tmpreg = RCC->CFGR;
  /* Clear SW[1:0] bits 清除系統時鐘切換位*/
  tmpreg &= CFGR_SW_Mask;
  /* Set SW[1:0] bits according to RCC_SYSCLKSource value */
  tmpreg |= RCC_SYSCLKSource;
  /* Store the new value 設定新狀態*/
  RCC->CFGR = tmpreg;
}
/**
  * @brief  Returns the clock source used as system clock.
  返回系統時鐘選用的時鐘源
  * @param  None
  * @retval The clock source used as system clock. The returned value can
  *   be one of the following:
  *     - 0x00: HSI used as system clock HSI作為系統時鐘
  *     - 0x04: HSE used as system clock HSE作為系統時鐘
  *     - 0x08: PLL used as system clock PLL作為系統時鐘
  */
uint8_t RCC_GetSYSCLKSource(void)
{
  return ((uint8_t)(RCC->CFGR & CFGR_SWS_Mask));
}
 現在我們再從根部詳細說起:         高速外部時鐘訊號HSE由兩種時鐘源產生,即HSE使用者外部時鐘(HSE旁路)和HSE外部晶體/陶瓷諧振器。 1.第一種模式:外部時鐘源,它的頻率最高可達25MHz。使用者可以通過設定在時鐘控制暫存器中的HSEBYP和HSEON位來選擇這一模式。外部時鐘訊號(50%佔空比的方波、正弦波或三角波)必須連線到SOC_IN引腳,同時保證OSC_OUT懸空。










2.第二種模式為外部晶體/陶瓷諧振器。在這種情況下需要等晶體振盪器穩定才能採用。 下面該圖為這兩種方式的硬體接法:這兩種模式通過硬體配置和外部高速時鐘旁路位來配置。




































在時鐘控制暫存器RCC_CR中的HSERDY位用來指示高速外部振盪器是否穩定。在啟動時,直到這一位被硬體置’1’,時鐘才被釋放出來。如果在時鐘中斷暫存器RCC_CIR中允許產生中斷,將會產生相應中斷。HSE晶體可以通過設定時鐘控制暫存器裡RCC_CR中的HSEON位被啟動和關閉。
























/**
  * @brief  Configures the External High Speed oscillator (HSE).
  配置外部高速振盪器
  * @note   HSE can not be stopped if it is used directly or through the PLL as system clock.
  HSE不能被停止如果它已經作為系統時鐘在使用或者通過PLL作為時鐘在使用
  * @param  RCC_HSE: specifies the new state of the HSE.  指明HSE的新狀態
  *   This parameter can be one of the following values:  引數
  *     @arg RCC_HSE_OFF: HSE oscillator OFF   //#define RCC_HSE_OFF((uint32_t)0x00000000)   HSE晶體振盪器關        
  *     @arg RCC_HSE_ON: HSE oscillator ON     //#define RCC_HSE_ON ((uint32_t)0x00010000)   HSE晶體振盪器開
  *     @arg RCC_HSE_Bypass: HSE oscillator bypassed with external clock HSE                 HSE旁路模式 
                                               //#define RCC_HSE_Bypass ((uint32_t)0x00040000)
  * @retval None
  */
void RCC_HSEConfig(uint32_t RCC_HSE)
{
  /* Check the parameters 檢查引數*/
  assert_param(IS_RCC_HSE(RCC_HSE));
  //#define IS_RCC_HSE(HSE) (((HSE) == RCC_HSE_OFF) || ((HSE) == RCC_HSE_ON) || \
                         ((HSE) == RCC_HSE_Bypass))    //反斜槓起到換行作用,用於巨集定義和字串換行。其中巨集定義中使用居多。

  /* Reset HSEON and HSEBYP bits before configuring the HSE 復位HSE使能和HSE旁路------------------*/
  /* Reset HSEON bit */
  RCC->CR &= CR_HSEON_Reset;
  /* Reset HSEBYP bit */
  RCC->CR &= CR_HSEBYP_Reset;
  /* Configure HSE (RCC_HSE_OFF is already covered by the code section above) 復位HSE在上述復位過程中已經包含 */
  switch(RCC_HSE)
  {
    case RCC_HSE_ON:
      /* Set HSEON bit 使能HSE*/
      RCC->CR |= CR_HSEON_Set;
      break;
      
    case RCC_HSE_Bypass:
      /* Set HSEBYP and HSEON bits 使能HSE旁路和HSE時鐘*/
      RCC->CR |= CR_HSEBYP_Set | CR_HSEON_Set;
      break;
      
    default:
      break;
  }
}
/**
  * @brief  Waits for HSE start-up. 
  等待直到外部高速晶振穩定
  * @param  None
  * @retval An ErrorStatus enumuration value:    錯誤狀態祥表
  * - SUCCESS: HSE oscillator is stable and ready to use
  * - ERROR: HSE oscillator not yet ready
  typedef enum {ERROR = 0, SUCCESS = !ERROR} ErrorStatus;  ErrorStatus是列舉型別
  typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus;  FlagStatus是列舉型別
  */
ErrorStatus RCC_WaitForHSEStartUp(void)
{
  __IO uint32_t StartUpCounter = 0;
  ErrorStatus status = ERROR;
  FlagStatus HSEStatus = RESET;
  
  /* Wait till HSE is ready and if Time out is reached exit 直到HSE穩定或者起振閥值時間到了才退出*/
  do
  {
    HSEStatus = RCC_GetFlagStatus(RCC_FLAG_HSERDY);
    StartUpCounter++;  
  } while((StartUpCounter != HSE_STARTUP_TIMEOUT) && (HSEStatus == RESET));
  //#define HSE_STARTUP_TIMEOUT   ((uint16_t)0x0500) /*!< Time out for HSE start up */
  
  if (RCC_GetFlagStatus(RCC_FLAG_HSERDY) != RESET)
  {
    status = SUCCESS;  //HSE穩定
  }
  else
  {
    status = ERROR;    //HSE未能起振
  }  
  return (status);
}

高速外部時鐘訊號HSE可直接作為SYSCLK系統時鐘(通過SW選擇),也可以經過PLLXTPRE多路複用器選擇直接或者二分頻後輸給PLLMUL倍頻器後(RCC_PLLConfig函式中配置)作為系統時鐘。

這裡還有監控SYSCLK時鐘的特性,該特性跟HSE振盪器有關:   時鐘安全系統(CSS):時鐘安全系統可以通過軟體被啟用。一旦其被啟用,時鐘監測器將在 HSE 振盪器啟動延遲後被使能,並在 HSE 時鐘關閉後關閉。如果 HSE 時鐘發生故障, HSE 振盪器被自動關閉,時鐘失效事件將被送到高階定時器 (TIM1 TIM8) 的剎車輸入端,併產生時鐘安全中斷 CSSI ,允許軟體完成營救操作。此 CSSI 中斷連線到 Cortex™-M3 NMI 中斷 ( 不可遮蔽中斷 ) 注意: 一旦CSS被啟用,並且HSE時鐘出現故障, CSS中斷就產生,並且NMI也自動產生。 NMI將被不斷執行,直到CSS中斷掛起位被清除。因此,在NMI的處理程式中必須通過設定時鐘中斷暫存器(RCC_CIR)裡的CSSC位來清除CSS中斷。如果 HSE 振盪器被直接或間接地作為系統時鐘, ( 間接的意思是:它被作為 PLL 輸入時鐘,並且 PLL 時鐘被作為系統時鐘 ) ,時鐘故障將導致系統時鐘自動切換到 HSI 振盪器,同時外部 HSE 振盪器被關閉。在時鐘失效時,如果 HSE 振盪器時鐘 ( 被分頻或未被分頻 ) 是用作系統時鐘的 PLL 的輸入時鐘, PLL也將被關閉。









/**
  * @brief  Enables or disables the Clock Security System.
  使能或者失能CSS
  * @param  NewState: new state of the Clock Security System..
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void RCC_ClockSecuritySystemCmd(FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_FUNCTIONAL_STATE(NewState));
  *(__IO uint32_t *) CR_CSSON_BB = (uint32_t)NewState;
}

高速內部時鐘訊號HSI由內部8MHz的RC振盪器產生,可直接作為系統時鐘或在2分頻後作為PLL輸入。當HSI被用於作為PLL時鐘的輸入時,系統時鐘能得到的最大頻率是64MHz。HSI RC振盪器能夠在不需要任何外部器件的條件下提供系統時鐘。它的啟動時間比HSE晶體振盪器短。然而,即使在校準之後它的時鐘頻率精度仍較差。校準製造工藝決定了不同晶片的RC振盪器頻率會不同,這就是為什麼每個晶片的HSI時鐘頻率在出廠前已經被ST校準到1%(25°C)的原因。系統復位時,工廠校準值被裝載到時鐘控制暫存器的HSICAL[7:0]位。如果使用者的應用基於不同的電壓或環境溫度,這將會影響RC振盪器的精度。可以通過時鐘控制暫存器裡的HSITRIM[4:0]位來調整HSI頻率。時鐘控制暫存器中的HSIRDY位用來指示HSI RC振盪器是否穩定。在時鐘啟動過程中,直到這一位被硬體置’1’, HSI RC輸出時鐘才被釋放。 HSI RC可由時鐘控制暫存器中的HSION位來啟動和關閉。如果HSE晶體振盪器失效, HSI時鐘會被作為備用時鐘源。
/**
  * @brief  Adjusts the Internal High Speed oscillator (HSI) calibration value.
  調整HSI精度值
  * @param  HSICalibrationValue: specifies the calibration trimming value.
  *   This parameter must be a number between 0 and 0x1F.
  * @retval None
  */
void RCC_AdjustHSICalibrationValue(uint8_t HSICalibrationValue)
{
  uint32_t tmpreg = 0;
  /* Check the parameters */
  assert_param(IS_RCC_CALIBRATION_VALUE(HSICalibrationValue));
  tmpreg = RCC->CR;
  /* Clear HSITRIM[4:0] bits */
  tmpreg &= CR_HSITRIM_Mask;
  /* Set the HSITRIM[4:0] bits according to HSICalibrationValue value */
  tmpreg |= (uint32_t)HSICalibrationValue << 3;
  /* Store the new value */
  RCC->CR = tmpreg;
}
/**
  * @brief  Enables or disables the Internal High Speed oscillator (HSI).
            使能或失能內部高速晶振
  * @note   HSI can not be stopped if it is used directly or through the PLL as system clock.
            HSI不能被停止如果HSI直接作為系統時鐘或通過PLL間接作為系統時鐘
  * @param  NewState: new state of the HSI. This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void RCC_HSICmd(FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_FUNCTIONAL_STATE(NewState));
  //typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState;
  //#define IS_FUNCTIONAL_STATE(STATE) (((STATE) == DISABLE) || ((STATE) == ENABLE))
  *(__IO uint32_t *) CR_HSION_BB = (uint32_t)NewState;
  /* Alias word address of HSION bit */
  //#define PERIPH_BB_BASE ((uint32_t)0x42000000) /*!< Peripheral base address in the bit-band region */
  //#define CR_OFFSET (RCC_OFFSET + 0x00)   #define HSION_BitNumber  0x00
  //#define CR_HSION_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (HSION_BitNumber * 4))
}

下面來看看時鐘樹軀幹部分(即標號5處)的相關內容: 5.PLL可以用來倍頻HSI RC的輸出時鐘或HSE晶體輸出時鐘。PLL的設定(選擇HIS振盪器除2或HSE振盪器為PLL的輸入時鐘,和選擇倍頻因子)必須在其被啟用前完成。一旦PLL被啟用,這些引數就不能被改動。如果PLL中斷在時鐘中斷暫存器裡被允許,當PLL準備就緒時,可產生中斷申請。
/**
  * @brief  Configures the PLL clock source and multiplication factor.
  設定PLL時鐘源及倍頻係數
  * @note   This function must be used only when the PLL is disabled.
  當PLL使能時才能使用
  * @param  RCC_PLLSource: specifies the PLL entry clock source.   指定輸入PLL的時鐘源
  *   For @b STM32_Connectivity_line_devices or @b STM32_Value_line_devices, 
  *   this parameter can be one of the following values:
  *     @arg RCC_PLLSource_HSI_Div2: HSI oscillator clock divided by 2 selected as PLL clock entry
  *     @arg RCC_PLLSource_PREDIV1: PREDIV1 clock selected as PLL clock entry
  *   For @b other_STM32_devices, this parameter can be one of the following values:
  *     @arg RCC_PLLSource_HSI_Div2: HSI oscillator clock divided by 2 selected as PLL clock entry
             #define RCC_PLLSource_HSI_Div2 ((uint32_t)0x00000000)
  *     @arg RCC_PLLSource_HSE_Div1: HSE oscillator clock selected as PLL clock entry
             #define RCC_PLLSource_HSE_Div1 ((uint32_t)0x00010000)
  *     @arg RCC_PLLSource_HSE_Div2: HSE oscillator clock divided by 2 selected as PLL clock entry 
             #define RCC_PLLSource_HSE_Div2 ((uint32_t)0x00030000)
  * @param  RCC_PLLMul: specifies the PLL multiplication factor.
  *   For @b STM32_Connectivity_line_devices, this parameter can be RCC_PLLMul_x where x:{[4,9], 6_5}
  *   For @b other_STM32_devices, this parameter can be RCC_PLLMul_x where x:[2,16]  
  * @retval None
  */
void RCC_PLLConfig(uint32_t RCC_PLLSource, uint32_t RCC_PLLMul)
{
  uint32_t tmpreg = 0;


  /* Check the parameters */
  assert_param(IS_RCC_PLL_SOURCE(RCC_PLLSource));
  assert_param(IS_RCC_PLL_MUL(RCC_PLLMul));


  tmpreg = RCC->CFGR;
  /* Clear PLLSRC, PLLXTPRE and PLLMUL[3:0] bits */ 
  //清除PLL輸入時鐘源,HSE分頻器作為PLL輸入位,PLL倍頻係數位
  tmpreg &= CFGR_PLL_Mask;
  /* Set the PLL configuration bits 設定PLL配置位*/
  tmpreg |= RCC_PLLSource | RCC_PLLMul;
  /* Store the new value 配置*/
  RCC->CFGR = tmpreg;
}
/**
  * @brief  Enables or disables the PLL.
    使能或者使能PLL
  * @note   The PLL can not be disabled if it is used as system clock.
  * @param  NewState: new state of the PLL. This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void RCC_PLLCmd(FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_FUNCTIONAL_STATE(NewState));

  *(__IO uint32_t *) CR_PLLON_BB = (uint32_t)NewState;
}

接下來可以介紹一下時鐘樹的樹葉部分了: PLL時鐘的一個小流向(即是圖中所標C區域):如果需要在應用中使用USB介面, PLL必須被設定為輸出48或72MHZ時鐘,用於提供48MHz的USBCLK時鐘。
USB時鐘來源於PLLCLK,通過USB分頻器為USB外設提供48MHZ時鐘。由於USB時鐘分頻器只能1分頻和1.5分頻,所以PLL出來的時鐘頻率只能是48MHz和72MHz。
/**
  * @brief  Configures the USB clock (USBCLK).
  配置USB時鐘
  * @param  RCC_USBCLKSource: specifies the USB clock source. This clock is 
  *   derived from the PLL output.
  *   This parameter can be one of the following values:
  *     @arg RCC_USBCLKSource_PLLCLK_1Div5: PLL clock divided by 1,5 selected as USB 
  *                                     clock source ((uint8_t)0x00)
  *     @arg RCC_USBCLKSource_PLLCLK_Div1: PLL clock selected as USB clock source ((uint8_t)0x01)
  USB時鐘來源於PLL時鐘,可以對PLL進行1.5或1分頻作為USB時鐘輸出(48MHZ)
  * @retval None
  */
void RCC_USBCLKConfig(uint32_t RCC_USBCLKSource)
{
  /* Check the parameters */
  assert_param(IS_RCC_USBCLK_SOURCE(RCC_USBCLKSource));

  *(__IO uint32_t *) CFGR_USBPRE_BB = RCC_USBCLKSource;
}

下面是大樹枝葉最為茂密的地方了: 上面是以哪些時鐘源可以作為SYSCLK時鐘源展開的,下面說明由SYSCLK得到AHB時鐘(HCLK),低速APB1時鐘,高速APB2時鐘。AHB和APB2域的最大頻率是72MHz。 APB1域的最大允許頻率是36MHz。 配置AHB時鐘的頻率即分頻係數。這是時鐘數由SYSCLK到AHB的第一個節點(即圖中D標號下方的AHB預分頻器模組)。
/**
  * @brief  Configures the AHB clock (HCLK).
  配置AHB時鐘(HCLK)
  * @param  RCC_SYSCLK: defines the AHB clock divider. This clock is derived from 
  *   the system clock (SYSCLK).
  *   This parameter can be one of the following values:
  *     @arg RCC_SYSCLK_Div1: AHB clock = SYSCLK         ((uint32_t)0x00000000)
  *     @arg RCC_SYSCLK_Div2: AHB clock = SYSCLK/2       ((uint32_t)0x00000080)
  *     @arg RCC_SYSCLK_Div4: AHB clock = SYSCLK/4       ((uint32_t)0x00000090)
  *     @arg RCC_SYSCLK_Div8: AHB clock = SYSCLK/8       ((uint32_t)0x000000A0)
  *     @arg RCC_SYSCLK_Div16: AHB clock = SYSCLK/16     ((uint32_t)0x000000B0)
  *     @arg RCC_SYSCLK_Div64: AHB clock = SYSCLK/64     ((uint32_t)0x000000C0)
  *     @arg RCC_SYSCLK_Div128: AHB clock = SYSCLK/128   ((uint32_t)0x000000D0)
  *     @arg RCC_SYSCLK_Div256: AHB clock = SYSCLK/256   ((uint32_t)0x000000E0)
  *     @arg RCC_SYSCLK_Div512: AHB clock = SYSCLK/512   ((uint32_t)0x000000F0)
  * @retval None
  */
void RCC_HCLKConfig(uint32_t RCC_SYSCLK)
{
  uint32_t tmpreg = 0;
  /* Check the parameters */
  assert_param(IS_RCC_HCLK(RCC_SYSCLK));
  tmpreg = RCC->CFGR;
  /* Clear HPRE[3:0] bits */
  tmpreg &= CFGR_HPRE_Reset_Mask;
  /* Set HPRE[3:0] bits according to RCC_SYSCLK value */
  tmpreg |= RCC_SYSCLK;
  /* Store the new value */
  RCC->CFGR = tmpreg;
}
AHB時鐘下掛載了外設(如DMA,SRAM,FLITF,CRC,FSMC,SDIO)和APB1分頻器和APB2分頻器。RCC通過AHB時鐘(HCLK)8分頻後作為Cortex系統定時器(SysTick)的外部時鐘。通過對SysTick控制與狀態暫存器的設定,可選擇上述時鐘或Cortex(HCLK)時鐘作為SysTick時鐘。FCLK是Cortex™-M3的自由執行時鐘。
/**
  * @brief  Enables or disables the AHB peripheral clock.
  使能或者失能AHB外設時鐘
  * @param  RCC_AHBPeriph: specifies the AHB peripheral to gates its clock.
  *   
  *   For @b STM32_Connectivity_line_devices, this parameter can be any combination
  *   of the following values:        
  *     @arg RCC_AHBPeriph_DMA1
  *     @arg RCC_AHBPeriph_DMA2
  *     @arg RCC_AHBPeriph_SRAM
  *     @arg RCC_AHBPeriph_FLITF
  *     @arg RCC_AHBPeriph_CRC
  *     @arg RCC_AHBPeriph_OTG_FS    
  *     @arg RCC_AHBPeriph_ETH_MAC   
  *     @arg RCC_AHBPeriph_ETH_MAC_Tx
  *     @arg RCC_AHBPeriph_ETH_MAC_Rx
  * 
  *   For @b other_STM32_devices, this parameter can be any combination of the 
  *   following values:        
  *     @arg RCC_AHBPeriph_DMA1    ((uint32_t)0x00000001)
  *     @arg RCC_AHBPeriph_DMA2    ((uint32_t)0x00000002)
  *     @arg RCC_AHBPeriph_SRAM    ((uint32_t)0x00000004)
  *     @arg RCC_AHBPeriph_FLITF   ((uint32_t)0x00000010)
  *     @arg RCC_AHBPeriph_CRC     ((uint32_t)0x00000040)
  *     @arg RCC_AHBPeriph_FSMC    ((uint32_t)0x00000100)
  *     @arg RCC_AHBPeriph_SDIO    ((uint32_t)0x00000400)
  *   
  * @note SRAM and FLITF clock can be disabled only during sleep mode.
  SRAM和FLITF時鐘只能在睡眠模式下被失能
  * @param  NewState: new state of the specified peripheral clock.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void RCC_AHBPeriphClockCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_RCC_AHB_PERIPH(RCC_AHBPeriph));
  assert_param(IS_FUNCTIONAL_STATE(NewState));

  if (NewState != DISABLE)
  {
    RCC->AHBENR |= RCC_AHBPeriph;
	//AHBENR為AHB外設時鐘使能暫存器
  }
  else
  {
    RCC->AHBENR &= ~RCC_AHBPeriph;
  }
}

APB1分頻器,配置APB1分頻器係數,從而獲得低速APB1時鐘(PCLK1),最大不超過36MHZ。
/**
  * @brief  Configures the Low Speed APB clock (PCLK1).
  配置低速APB時鐘
  * @param  RCC_HCLK: defines the APB1 clock divider. This clock is derived from 
  *   the AHB clock (HCLK).
  *   This parameter can be one of the following values:
  *     @arg RCC_HCLK_Div1: APB1 clock = HCLK
  *     @arg RCC_HCLK_Div2: APB1 clock = HCLK/2
  *     @arg RCC_HCLK_Div4: APB1 clock = HCLK/4
  *     @arg RCC_HCLK_Div8: APB1 clock = HCLK/8
  *     @arg RCC_HCLK_Div16: APB1 clock = HCLK/16
  * @retval None
  */
void RCC_PCLK1Config(uint32_t RCC_HCLK)
{
  uint32_t tmpreg = 0;
  /* Check the parameters */
  assert_param(IS_RCC_PCLK(RCC_HCLK));
  tmpreg = RCC->CFGR;
  /* Clear PPRE1[2:0] bits */
  tmpreg &= CFGR_PPRE1_Reset_Mask;
  /* Set PPRE1[2:0] bits according to RCC_HCLK value */
  tmpreg |= RCC_HCLK;
  /* Store the new value */
  RCC->CFGR = tmpreg;
}
APB1分頻器下掛載的外設(如TIM2,TIM3,TIM4,TIM5, TIM6, TIM7,WWDG,SPI2, SPI3,USART2, USART3, USART4, USART5, I2C1, I2C2,USB, CAN1, BKP,PWR, DAC, )
/**
  * @brief  Enables or disables the Low Speed APB (APB1) peripheral clock.
  使能或者失能APB1外設時鐘
  * @param  RCC_APB1Periph: specifies the APB1 peripheral to gates its clock.
  *   This parameter can be any combination of the following values:
  *     @arg RCC_APB1Periph_TIM2, RCC_APB1Periph_TIM3, RCC_APB1Periph_TIM4,
  *          RCC_APB1Periph_TIM5, RCC_APB1Periph_TIM6, RCC_APB1Periph_TIM7,
  *          RCC_APB1Periph_WWDG, RCC_APB1Periph_SPI2, RCC_APB1Periph_SPI3,
  *          RCC_APB1Periph_USART2, RCC_APB1Periph_USART3, RCC_APB1Periph_USART4, 
  *          RCC_APB1Periph_USART5, RCC_APB1Periph_I2C1, RCC_APB1Periph_I2C2,
  *          RCC_APB1Periph_USB, RCC_APB1Periph_CAN1, RCC_APB1Periph_BKP,
  *          RCC_APB1Periph_PWR, RCC_APB1Periph_DAC, 
             
			 RCC_APB1Periph_CEC,
  *          RCC_APB1Periph_TIM12, RCC_APB1Periph_TIM13, RCC_APB1Periph_TIM14
  * @param  NewState: new state of the specified peripheral clock.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_RCC_APB1_PERIPH(RCC_APB1Periph));
  assert_param(IS_FUNCTIONAL_STATE(NewState));
  if (NewState != DISABLE)
  {
    RCC->APB1ENR |= RCC_APB1Periph;
  }
  else
  {
    RCC->APB1ENR &= ~RCC_APB1Periph;
  }
}
/**
  * @brief  Forces or releases Low Speed APB (APB1) peripheral reset.
  強制復位或置位APB1外設
  * @param  RCC_APB1Periph: specifies the APB1 peripheral to reset.
  *   This parameter can be any combination of the following values:
  *     @arg RCC_APB1Periph_TIM2, RCC_APB1Periph_TIM3, RCC_APB1Periph_TIM4,
  *          RCC_APB1Periph_TIM5, RCC_APB1Periph_TIM6, RCC_APB1Periph_TIM7,
  *          RCC_APB1Periph_WWDG, RCC_APB1Periph_SPI2, RCC_APB1Periph_SPI3,
  *          RCC_APB1Periph_USART2, RCC_APB1Periph_USART3, RCC_APB1Periph_USART4, 
  *          RCC_APB1Periph_USART5, RCC_APB1Periph_I2C1, RCC_APB1Periph_I2C2,
  *          RCC_APB1Periph_USB, RCC_APB1Periph_CAN1, RCC_APB1Periph_BKP,
  *          RCC_APB1Periph_PWR, RCC_APB1Periph_DAC, RCC_APB1Periph_CEC,
  *          RCC_APB1Periph_TIM12, RCC_APB1Periph_TIM13, RCC_APB1Periph_TIM14  
  * @param  NewState: new state of the specified peripheral clock.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void RCC_APB1PeriphResetCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_RCC_APB1_PERIPH(RCC_APB1Periph));
  assert_param(IS_FUNCTIONAL_STATE(NewState));
  if (NewState != DISABLE)
  {
    RCC->APB1RSTR |= RCC_APB1Periph;
	//APB1RSTR為APB1外設復位暫存器
  }
  else
  {
    RCC->APB1RSTR &= ~RCC_APB1Periph;
  }
}
APB2分頻器,配置APB2分頻器係數,從而獲得高速APB2時鐘(PCLK2),最大不超過72MHZ。
/**
  * @brief  Configures the High Speed APB clock (PCLK2).
    配置高速時鐘APB(PCLK2)
  * @param  RCC_HCLK: defines the APB2 clock divider. This clock is derived from 
  *   the AHB clock (HCLK).
  *   This parameter can be one of the following values:
  *     @arg RCC_HCLK_Div1: APB2 clock = HCLK
  *     @arg RCC_HCLK_Div2: APB2 clock = HCLK/2
  *     @arg RCC_HCLK_Div4: APB2 clock = HCLK/4
  *     @arg RCC_HCLK_Div8: APB2 clock = HCLK/8
  *     @arg RCC_HCLK_Div16: APB2 clock = HCLK/16
  * @retval None
  */
void RCC_PCLK2Config(uint32_t RCC_HCLK)
{
  uint32_t tmpreg = 0;
  /* Check the parameters */
  assert_param(IS_RCC_PCLK(RCC_HCLK));
  tmpreg = RCC->CFGR;
  /* Clear PPRE2[2:0] bits */
  tmpreg &= CFGR_PPRE2_Reset_Mask;
  /* Set PPRE2[2:0] bits according to RCC_HCLK value */
  tmpreg |= RCC_HCLK << 3;
  /* Store the new value */
  RCC->CFGR = tmpreg;
}
APB2分頻器下掛載的外設
/**
  * @brief  Enables or disables the High Speed APB (APB2) peripheral clock.
  使能或者失能APB2外設時鐘
  * @param  RCC_APB2Periph: specifies the APB2 peripheral to gates its clock.
  *   This parameter can be any combination of the following values:
  *     @arg RCC_APB2Periph_AFIO, RCC_APB2Periph_GPIOA, RCC_APB2Periph_GPIOB,
  *          RCC_APB2Periph_GPIOC, RCC_APB2Periph_GPIOD, RCC_APB2Periph_GPIOE,
  *          RCC_APB2Periph_GPIOF, RCC_APB2Periph_GPIOG, RCC_APB2Periph_ADC1,
  *          RCC_APB2Periph_ADC2, RCC_APB2Periph_TIM1, RCC_APB2Periph_SPI1,
  *          RCC_APB2Periph_TIM8, RCC_APB2Periph_USART1, RCC_APB2Periph_ADC3,
  
  *          RCC_APB2Periph_TIM15, RCC_APB2Periph_TIM16, RCC_APB2Periph_TIM17,
  *          RCC_APB2Periph_TIM9, RCC_APB2Periph_TIM10, RCC_APB2Periph_TIM11     
  * @param  NewState: new state of the specified peripheral clock.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));
  assert_param(IS_FUNCTIONAL_STATE(NewState));
  if (NewState != DISABLE)
  {
    RCC->APB2ENR |= RCC_APB2Periph;
	//APB2ENR為APB2外設時鐘使能暫存器
  }
  else
  {
    RCC->APB2ENR &= ~RCC_APB2Periph;
  }
}
/**
  * @brief  Forces or releases High Speed APB (APB2) peripheral reset.
  強制復位或置位APB2外設
  * @param  RCC_APB2Periph: specifies the APB2 peripheral to reset.
  *   This parameter can be any combination of the following values:
  *     @arg RCC_APB2Periph_AFIO, RCC_APB2Periph_GPIOA, RCC_APB2Periph_GPIOB,
  *          RCC_APB2Periph_GPIOC, RCC_APB2Periph_GPIOD, RCC_APB2Periph_GPIOE,
  *          RCC_APB2Periph_GPIOF, RCC_APB2Periph_GPIOG, RCC_APB2Periph_ADC1,
  *          RCC_APB2Periph_ADC2, RCC_APB2Periph_TIM1, RCC_APB2Periph_SPI1,
  *          RCC_APB2Periph_TIM8, RCC_APB2Periph_USART1, RCC_APB2Periph_ADC3,
  *          RCC_APB2Periph_TIM15, RCC_APB2Periph_TIM16, RCC_APB2Periph_TIM17,
  *          RCC_APB2Periph_TIM9, RCC_APB2Periph_TIM10, RCC_APB2Periph_TIM11  
  * @param  NewState: new state of the specified peripheral reset.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void RCC_APB2PeriphResetCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));
  assert_param(IS_FUNCTIONAL_STATE(NewState));
  if (NewState != DISABLE)
  {
    RCC->APB2RSTR |= RCC_APB2Periph;
	//APB2RSTR為APB2外設復位暫存器
  }
  else
  {
    RCC->APB2RSTR &= ~RCC_APB2Periph;
  }
}
ADC時鐘由高速APB2時鐘經2、 4、 6或8分頻後獲得。
/**
  * @brief  Configures the ADC clock (ADCCLK)
  配置ADC時鐘
  * @param  RCC_PCLK2: defines the ADC clock divider. This clock is derived from 
  *   the APB2 clock (PCLK2). ADC時鐘來源於高速APB2時鐘
  *   This parameter can be one of the following values:
  *     @arg RCC_PCLK2_Div2: ADC clock = PCLK2/2    ((uint32_t)0x00000000)
  *     @arg RCC_PCLK2_Div4: ADC clock = PCLK2/4    ((uint32_t)0x00004000)
  *     @arg RCC_PCLK2_Div6: ADC clock = PCLK2/6    ((uint32_t)0x00008000)
  *     @arg RCC_PCLK2_Div8: ADC clock = PCLK2/8    ((uint32_t)0x0000C000)
  * @retval None
  */
void RCC_ADCCLKConfig(uint32_t RCC_PCLK2)
{
  uint32_t tmpreg = 0;
  /* Check the parameters */
  assert_param(IS_RCC_ADCCLK(RCC_PCLK2));
  tmpreg = RCC->CFGR;
  /* Clear ADCPRE[1:0] bits 清除ADC預分頻位*/
  tmpreg &= CFGR_ADCPRE_Reset_Mask;
  /* Set ADCPRE[1:0] bits according to RCC_PCLK2 value */
  tmpreg |= RCC_PCLK2;
  /* Store the new value 配置*/
  RCC->CFGR = tmpreg;
}

NOTE:定時器時鐘頻率分配由硬體按以下2種情況自動設定:
1. 如果相應的APB預分頻係數是1,定時器的時鐘頻率與所在APB匯流排頻率一致。
2. 否則,定時器的時鐘頻率被設為與其相連的APB匯流排頻率的2倍。
這裡我們來看一個比較特別的一根小樹枝:
MCO時鐘輸出  微控制器允許輸出時鐘訊號到外部MCO引腳。相應的GPIO埠暫存器必須被配置為相應功能。 以下四個時鐘訊號可被選作MCO時鐘:
● SYSCLK
● HSI
● HSE
● 除2的PLL時鐘
時鐘的選擇由時鐘配置暫存器(RCC_CFGR)中的MCO[2:0]位控制。
/**
  * @brief  Selects the clock source to output on MCO pin.
  選擇輸出到MCO引腳的時鐘源
  * @param  RCC_MCO: specifies the clock source to output.
  *   
  *   For @b STM32_Connectivity_line_devices, this parameter can be one of the
  *   following values:       
  *     @arg RCC_MCO_NoClock: No clock selected
  *     @arg RCC_MCO_SYSCLK: System clock selected
  *     @arg RCC_MCO_HSI: HSI oscillator clock selected
  *     @arg RCC_MCO_HSE: HSE oscillator clock selected
  *     @arg RCC_MCO_PLLCLK_Div2: PLL clock divided by 2 selected
  *     @arg RCC_MCO_PLL2CLK: PLL2 clock selected                     
  *     @arg RCC_MCO_PLL3CLK_Div2: PLL3 clock divided by 2 selected   
  *     @arg RCC_MCO_XT1: External 3-25 MHz oscillator clock selected  
  *     @arg RCC_MCO_PLL3CLK: PLL3 clock selected 
  * 
  *   For  @b other_STM32_devices, this parameter can be one of the following values:        
  *     @arg RCC_MCO_NoClock: No clock selected
  *     @arg RCC_MCO_SYSCLK: System clock selected
  *     @arg RCC_MCO_HSI: HSI oscillator clock selected
  *     @arg RCC_MCO_HSE: HSE oscillator clock selected
  *     @arg RCC_MCO_PLLCLK_Div2: PLL clock divided by 2 selected
  *   
  * @retval None
  */
void RCC_MCOConfig(uint8_t RCC_MCO)
{
  /* Check the parameters */
  assert_param(IS_RCC_MCO(RCC_MCO));

  /* Perform Byte access to MCO bits to select the MCO source */
  *(__IO uint8_t *) CFGR_BYTE4_ADDRESS = RCC_MCO;
}

下面介紹低速時鐘(即是根部3和根部4):
低速外部時鐘LSE:LSE 晶體是一個 32.768kHz 的低速外部晶體或陶瓷諧振器。它為實時時鐘或者其他定時功能提供 一個低功耗且精確的時源。 LSE 晶體通過在備份域控制暫存器 (RCC_BDCR) 裡的 LSEON 位啟動和關閉。 在備份域控制暫存器 (RCC_BDCR) 裡的 LSERDY 指示 LSE 晶體振盪是否穩定。在啟動階段,直 到這個位被硬體置 ’1’ 後, LSE 時鐘訊號才被釋放出來。如果在時鐘中斷暫存器裡被允許,可產 生中斷申請。 外部時鐘源 (LSE 旁路 ) 在這個模式裡必須提供一個 32.768kHz 頻率的外部時鐘源。你可以通過設定在備份域控制暫存器 (RCC_BDCR) 裡的 LSEBYP LSEON 位來選擇這個模式。具有 50% 佔空比的外部時鐘訊號 ( 波、正弦波或三角波 ) 必須連到 OSC32_IN 引腳,同時保證 OSC32_OUT引腳懸空。

/**
  * @brief  Configures the External Low Speed oscillator (LSE).
  配置外部低速晶振
  * @param  RCC_LSE: specifies the new state of the LSE.
  *   This parameter can be one of the following values:
  *     @arg RCC_LSE_OFF: LSE oscillator OFF
  *     @arg RCC_LSE_ON: LSE oscillator ON
  *     @arg RCC_LSE_Bypass: LSE oscillator bypassed with external clock
  * @retval None
  */
void RCC_LSEConfig(uint8_t RCC_LSE)
{
  /* Check the parameters */
  assert_param(IS_RCC_LSE(RCC_LSE));
  /* Reset LSEON and LSEBYP bits before configuring the LSE ------------------*/
  /* Reset LSEON bit 復位外部低速振盪器使能位*/
  *(__IO uint8_t *) BDCR_ADDRESS = RCC_LSE_OFF;
  /* Reset LSEBYP bit 復位外部低速時鐘振盪器旁路*/
  *(__IO uint8_t *) BDCR_ADDRESS = RCC_LSE_OFF;
  /* Configure LSE (RCC_LSE_OFF is already covered by the code section above) */
  switch(RCC_LSE)
  {
    case RCC_LSE_ON:
      /* Set LSEON bit */
      *(__IO uint8_t *) BDCR_ADDRESS = RCC_LSE_ON;
      break;
      
    case RCC_LSE_Bypass:
      /* Set LSEBYP and LSEON bits */
      *(__IO uint8_t *) BDCR_ADDRESS = RCC_LSE_Bypass | RCC_LSE_ON;
      break;            
      
    default:
      break;      
  }
}

低速內部時鐘LSI:LSI RC擔當一個低功耗時鐘源的角色,它可以在停機和待機模式下保持執行,為獨立看門狗和自動喚醒單元提供時鐘。 LSI時鐘頻率大約40kHz(在30kHz和60kHz之間)。進一步資訊請參考資料手冊中有關電氣特性部分。LSI RC可以通過控制/狀態暫存器(RCC_CSR)裡的LSION位來啟動或關閉。在控制/狀態暫存器(RCC_CSR)裡的LSIRDY位指示低速內部振盪器是否穩定。在啟動階段,直到這個位被硬體設定為’1’後,此時鐘才被釋放。如果在時鐘中斷暫存器(RCC_CIR)裡被允許,將產生LSI中斷申請。注意: 只有大容量和互聯型產品可以進行LSI校準。 LSI校準可以通過校準內部低速振盪器LSI來補償其頻率偏移,從而獲得精度可接受的RTC時間基數,以及獨立看門狗(IWDG)的超時時間(當這些外設以LSI為時鐘源)。校準可以通過使用TIM5的輸入時鐘(TIM5_CLK)測量LSI時鐘頻率實現。測量以HSE的精度為保證,軟體可以通過調整RTC的20位預分頻器來獲得精確的RTC時鐘基數,以及通過計算得到精確的獨立看門狗(IWDG)的超時時間。
LSI校準步驟如下:
1. 開啟TIM5,設定通道4為輸入捕獲模式;
2. 設定AFIO_MAPR的TIM5_CH4_IREMAP位為’1’,在內部把LSI連線到TIM5的通道4;
3. 通過TIM5的捕獲/比較4事件或者中斷來測量LSI時鐘頻率;
4. 根據測量結果和期望的RTC時間基數和獨立看門狗的超時時間,設定20位預分頻器。
/**
  * @brief  Enables or disables the Internal Low Speed oscillator (LSI).
  使能或失能內部低速速晶振
  * @note   LSI can not be disabled if the IWDG is running.
  * @param  NewState: new state of the LSI. This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void RCC_LSICmd(FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_FUNCTIONAL_STATE(NewState));
  *(__IO uint32_t *) CSR_LSION_BB = (uint32_t)NewState;
}
如果獨立看門狗已經由硬體選項或軟體啟動, LSI振盪器將被強制在開啟狀態,並且不能被關閉。在LSI振盪器穩定後,時鐘供應給IWDG

下面我們來看看小樹枝(區域B):
RTC時鐘:通過設定備份域控制暫存器 (RCC_BDCR) 裡的 RTCSEL[1:0] 位, RTCCLK 時鐘源可以由 HSE/128 LSE LSI 時鐘提供。除非備份域復位,此選擇不能被改變。 LSE 時鐘在備份域裡,但 HSE LSI 時鐘不是。因此:
● 如果 LSE 被選為 RTC 時鐘: 只要 V BAT 維持供電,儘管 V DD 供電被切斷, RTC 仍繼續工作。
● 如果 LSI 被選為自動喚醒單元 (AWU) 時鐘: 如果 V DD 供電被切斷, AWU 狀態不能被保證。
● 如果 HSE 時鐘 128 分頻後作為 RTC 時鐘: 如果 V DD 供電被切斷或內部電壓調壓器被關閉 (1.8V 域的供電被切斷 ) ,則 RTC 狀態不確 定。
必須設定電源控制暫存器 ( 4.4.1 ) DPB ( 取消後備區域的防寫 ) 1
/**
  * @brief  Configures the RTC clock (RTCCLK).
  配置RTC時鐘
  * @note   Once the RTC clock is selected it can't be changed unless the Backup domain is reset.
            一旦RTC時鐘被選擇,該時鐘就不能被改變,除非備份域復位
  * @param  RCC_RTCCLKSource: specifies the RTC clock source.
  *   This parameter can be one of the following values:
  *     @arg RCC_RTCCLKSource_LSE: LSE selected as RTC clock
  *     @arg RCC_RTCCLKSource_LSI: LSI selected as RTC clock
  *     @arg RCC_RTCCLKSource_HSE_Div128: HSE clock divided by 128 selected as RTC clock
  * @retval None
  */
void RCC_RTCCLKConfig(uint32_t RCC_RTCCLKSource)
{
  /* Check the parameters */
  assert_param(IS_RCC_RTCCLK_SOURCE(RCC_RTCCLKSource));
  /* Select the RTC clock source */
  RCC->BDCR |= RCC_RTCCLKSource;
}

/**
  * @brief  Enables or disables the RTC clock.
  使能或者失能RTC時鐘
  * @note   This function must be used only after the RTC clock was selected using the RCC_RTCCLKConfig function.
  * @param  NewState: new state of the RTC clock. This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void RCC_RTCCLKCmd(FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_FUNCTIONAL_STATE(NewState));
  *(__IO uint32_t *) BDCR_RTCEN_BB = (uint32_t)NewState;
}


關於時鐘中斷的一些操作,先看看韌體中的函式,等後面用到時再仔細看看: 相關暫存器位在時鐘中斷暫存器(RCC_CIR)中。
/**
  * @brief  Enables or disables the specified RCC interrupts.
  使能或者失能指定的RCC中斷
  * @param  RCC_IT: specifies the RCC interrupt sources to be enabled or disabled.
  * 
  *   For @b STM32_Connectivity_line_devices, this parameter can be any combination
  *   of the following values        
  *     @arg RCC_IT_LSIRDY: LSI ready interrupt
  *     @arg RCC_IT_LSERDY: LSE ready interrupt
  *     @arg RCC_IT_HSIRDY: HSI ready interrupt
  *     @arg RCC_IT_HSERDY: HSE ready interrupt
  *     @arg RCC_IT_PLLRDY: PLL ready interrupt
  *     @arg RCC_IT_PLL2RDY: PLL2 ready interrupt
  *     @arg RCC_IT_PLL3RDY: PLL3 ready interrupt
  * 
  *   For @b other_STM32_devices, this parameter can be any combination of the 
  *   following values        
  *     @arg RCC_IT_LSIRDY: LSI ready interrupt ((uint8_t)0x01) LSI準備就緒中斷
  *     @arg RCC_IT_LSERDY: LSE ready interrupt ((uint8_t)0x02) LSE準備就緒中斷
  *     @arg RCC_IT_HSIRDY: HSI ready interrupt ((uint8_t)0x04) HSI準備就緒中斷
  *     @arg RCC_IT_HSERDY: HSE ready interrupt ((uint8_t)0x08) HSE準備就緒中斷
  *     @arg RCC_IT_PLLRDY: PLL ready interrupt ((uint8_t)0x10) PLL準備就緒中斷
  *       
  * @param  NewState: new state of the specified RCC interrupts.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void RCC_ITConfig(uint8_t RCC_IT, FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_RCC_IT(RCC_IT));
  assert_param(IS_FUNCTIONAL_STATE(NewState));
  if (NewState != DISABLE)
  {
    /* Perform Byte access to RCC_CIR bits to enable the selected interrupts 使能*/
    *(__IO uint8_t *) CIR_BYTE2_ADDRESS |= RCC_IT;
  }
  else
  {
    /* Perform Byte access to RCC_CIR bits to disable the selected interrupts 失能*/
    *(__IO uint8_t *) CIR_BYTE2_ADDRESS &= (uint8_t)~RCC_IT;
  }
}
/**
  * @brief  Checks whether the specified RCC interrupt has occurred or not.
  檢查指定RCC中斷是否發生
  * @param  RCC_IT: specifies the RCC interrupt source to check.
  *   
  *   For @b STM32_Connectivity_line_devices, this parameter can be one of the
  *   following values:
  *     @arg RCC_IT_LSIRDY: LSI ready interrupt
  *     @arg RCC_IT_LSERDY: LSE ready interrupt
  *     @arg RCC_IT_HSIRDY: HSI ready interrupt
  *     @arg RCC_IT_HSERDY: HSE ready interrupt
  *     @arg RCC_IT_PLLRDY: PLL ready interrupt
  *     @arg RCC_IT_PLL2RDY: PLL2 ready interrupt 
  *     @arg RCC_IT_PLL3RDY: PLL3 ready interrupt                      
  *     @arg RCC_IT_CSS: Clock Security System interrupt
  * 
  *   For @b other_STM32_devices, this parameter can be one of the following values:        
  *     @arg RCC_IT_LSIRDY: LSI ready interrupt  LSI準備就緒中斷
  *     @arg RCC_IT_LSERDY: LSE ready interrupt  LSE準備就緒中斷
  *     @arg RCC_IT_HSIRDY: HSI ready interrupt  HSI準備就緒中斷
  *     @arg RCC_IT_HSERDY: HSE ready interrupt  HSE準備就緒中斷
  *     @arg RCC_IT_PLLRDY: PLL ready interrupt  PLL準備就緒中斷
  *     @arg RCC_IT_CSS: Clock Security System interrupt 時鐘安全系統中斷
  *   
  * @retval The new state of RCC_IT (SET or RESET).
  */
ITStatus RCC_GetITStatus(uint8_t RCC_IT)
{
  ITStatus bitstatus = RESET;
  /* Check the parameters */
  assert_param(IS_RCC_GET_IT(RCC_IT));

  /* Check the status of the specified RCC interrupt */
  if ((RCC->CIR & RCC_IT) != (uint32_t)RESET)
  {
    bitstatus = SET;
  }
  else
  {
    bitstatus = RESET;
  }

  /* Return the RCC_IT status */
  return  bitstatus;
}

/**
  * @brief  Clears the RCC's interrupt pending bits.
  清除RCC中斷標誌位
  * @param  RCC_IT: specifies the interrupt pending bit to clear.
  *   
  *   For @b STM32_Connectivity_line_devices, this parameter can be any combination
  *   of the following values:
  *     @arg RCC_IT_LSIRDY: LSI ready interrupt
  *     @arg RCC_IT_LSERDY: LSE ready interrupt
  *     @arg RCC_IT_HSIRDY: HSI ready interrupt
  *     @arg RCC_IT_HSERDY: HSE ready interrupt
  *     @arg RCC_IT_PLLRDY: PLL ready interrupt
  *     @arg RCC_IT_PLL2RDY: PLL2 ready interrupt 
  *     @arg RCC_IT_PLL3RDY: PLL3 ready interrupt                      
  *     @arg RCC_IT_CSS: Clock Security System interrupt
  * 
  *   For @b other_STM32_devices, this parameter can be any combination of the
  *   following values:        
  *     @arg RCC_IT_LSIRDY: LSI ready interrupt
  *     @arg RCC_IT_LSERDY: LSE ready interrupt
  *     @arg RCC_IT_HSIRDY: HSI ready interrupt
  *     @arg RCC_IT_HSERDY: HSE ready interrupt
  *     @arg RCC_IT_PLLRDY: PLL ready interrupt
  *   
  *     @arg RCC_IT_CSS: Clock Security System interrupt
  * @retval None
  */
void RCC_ClearITPendingBit(uint8_t RCC_IT)
{
  /* Check the parameters */
  assert_param(IS_RCC_CLEAR_IT(RCC_IT));

  /* Perform Byte access to RCC_CIR[23:16] bits to clear the selected interrupt
     pending bits */
  *(__IO uint8_t *) CIR_BYTE3_ADDRESS = RCC_IT;
}



RCC庫函式中其他一些函式:
/**
  * @brief  Checks whether the specified RCC flag is set or not.
  檢查指定RCC標誌是否已經置位
  * @param  RCC_FLAG: specifies the flag to check.
  *   
  *   For @b STM32_Connectivity_line_devices, this parameter can be one of the
  *   following values:   互聯型產品
  *     @arg RCC_FLAG_HSIRDY: HSI oscillator clock ready
  *     @arg RCC_FLAG_HSERDY: HSE oscillator clock ready
  *     @arg RCC_FLAG_PLLRDY: PLL clock ready
  *     @arg RCC_FLAG_PLL2RDY: PLL2 clock ready      
  *     @arg RCC_FLAG_PLL3RDY: PLL3 clock ready                           
  *     @arg RCC_FLAG_LSERDY: LSE oscillator clock ready
  *     @arg RCC_FLAG_LSIRDY: LSI oscillator clock ready
  *     @arg RCC_FLAG_PINRST: Pin reset
  *     @arg RCC_FLAG_PORRST: POR/PDR reset
  *     @arg RCC_FLAG_SFTRST: Software reset
  *     @arg RCC_FLAG_IWDGRST: Independent Watchdog reset
  *     @arg RCC_FLAG_WWDGRST: Window Watchdog reset
  *     @arg RCC_FLAG_LPWRRST: Low Power reset
  * 
  *   For @b other_STM32_devices, this parameter can be one of the following values:        
  *     @arg RCC_FLAG_HSIRDY: HSI oscillator clock ready ((uint8_t)0x21)
  *     @arg RCC_FLAG_HSERDY: HSE oscillator clock ready ((uint8_t)0x31)
  *     @arg RCC_FLAG_PLLRDY: PLL clock ready            ((uint8_t)0x39)
  *     @arg RCC_FLAG_LSERDY: LSE oscillator clock ready ((uint8_t)0x41)
  *     @arg RCC_FLAG_LSIRDY: LSI oscillator clock ready ((uint8_t)0x61)
  *     @arg RCC_FLAG_PINRST: Pin reset                  ((uint8_t)0x7A)
  *     @arg RCC_FLAG_PORRST: POR/PDR reset              ((uint8_t)0x7B)
  *     @arg RCC_FLAG_SFTRST: Software reset             ((uint8_t)0x7C)
  *     @arg RCC_FLAG_IWDGRST: Independent Watchdog reset((uint8_t)0x7D)
  *     @arg RCC_FLAG_WWDGRST: Window Watchdog reset     ((uint8_t)0x7E)
  *     @arg RCC_FLAG_LPWRRST: Low Power reset           ((uint8_t)0x7F)
  *                                                                                              
  * @retval The new state of RCC_FLAG (SET or RESET).
    typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus;
  */
FlagStatus RCC_GetFlagStatus(uint8_t RCC_FLAG)
{
  uint32_t tmp = 0;
  uint32_t statusreg = 0;
  FlagStatus bitstatus = RESET;
  /* Check the parameters */
  assert_param(IS_RCC_FLAG(RCC_FLAG));

  /* Get the RCC register index */
  tmp = RCC_FLAG >> 5;
  if (tmp == 1)               /* The flag to check is in CR register */
  {
    statusreg = RCC->CR;
  }
  else if (tmp == 2)          /* The flag to check is in BDCR register */
  {
    statusreg = RCC->BDCR;
  }
  else                       /* The flag to check is in CSR register */
  {
    statusreg = RCC->CSR;
  }

  /* Get the flag position */  
  //RCC Flag Mask #define FLAG_Mask ((uint8_t)0x1F)
  tmp = RCC_FLAG & FLAG_Mask;
  if ((statusreg & ((uint32_t)1 << tmp)) != (uint32_t)RESET)
  {
    bitstatus = SET;
  }
  else
  {
    bitstatus = RESET;
  }

  /* Return the flag status */
  return bitstatus;
}
/**
  * @brief  Returns the frequencies of different on chip clocks.
  返回片上不同時鐘(SYSCLK,HCLK,PCLK1,PCLK2,ADCCLK)的頻率 單位為HZ 
  * @param  RCC_Clocks: pointer to a RCC_ClocksTypeDef structure which will hold
  *         the clocks frequencies.
  * @note   The result of this function could be not correct when using 
  *         fractional value for HSE crystal.
  * @retval None
  */
  //typedef struct
//  {
//  uint32_t SYSCLK_Frequency;  /*!< returns SYSCLK clock frequency expressed in Hz */
//  uint32_t HCLK_Frequency;    /*!< returns HCLK clock frequency expressed in