Subversion Repositories LedShow

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /**
  2.   ******************************************************************************
  3.   * @file    stm32f1xx_ll_rcc.c
  4.   * @author  MCD Application Team
  5.   * @brief   RCC LL module driver.
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  10.   *
  11.   * Redistribution and use in source and binary forms, with or without modification,
  12.   * are permitted provided that the following conditions are met:
  13.   *   1. Redistributions of source code must retain the above copyright notice,
  14.   *      this list of conditions and the following disclaimer.
  15.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  16.   *      this list of conditions and the following disclaimer in the documentation
  17.   *      and/or other materials provided with the distribution.
  18.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  19.   *      may be used to endorse or promote products derived from this software
  20.   *      without specific prior written permission.
  21.   *
  22.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  26.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  28.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32.   *
  33.   ******************************************************************************
  34.   */
  35. #if defined(USE_FULL_LL_DRIVER)
  36.  
  37. /* Includes ------------------------------------------------------------------*/
  38. #include "stm32f1xx_ll_rcc.h"
  39. #ifdef  USE_FULL_ASSERT
  40.   #include "stm32_assert.h"
  41. #else
  42.   #define assert_param(expr) ((void)0U)
  43. #endif /* USE_FULL_ASSERT */
  44. /** @addtogroup STM32F1xx_LL_Driver
  45.   * @{
  46.   */
  47.  
  48. #if defined(RCC)
  49.  
  50. /** @defgroup RCC_LL RCC
  51.   * @{
  52.   */
  53.  
  54. /* Private types -------------------------------------------------------------*/
  55. /* Private variables ---------------------------------------------------------*/
  56. /* Private constants ---------------------------------------------------------*/
  57. /* Private macros ------------------------------------------------------------*/
  58. /** @addtogroup RCC_LL_Private_Macros
  59.   * @{
  60.   */
  61. #if defined(RCC_PLLI2S_SUPPORT)
  62. #define IS_LL_RCC_I2S_CLKSOURCE(__VALUE__)     (((__VALUE__) == LL_RCC_I2S2_CLKSOURCE) \
  63.                                              || ((__VALUE__) == LL_RCC_I2S3_CLKSOURCE))
  64. #endif /* RCC_PLLI2S_SUPPORT */
  65.  
  66. #if defined(USB) || defined(USB_OTG_FS)
  67. #define IS_LL_RCC_USB_CLKSOURCE(__VALUE__)    (((__VALUE__) == LL_RCC_USB_CLKSOURCE))
  68. #endif /* USB */
  69.  
  70. #define IS_LL_RCC_ADC_CLKSOURCE(__VALUE__)    (((__VALUE__) == LL_RCC_ADC_CLKSOURCE))
  71. /**
  72.   * @}
  73.   */
  74.  
  75. /* Private function prototypes -----------------------------------------------*/
  76. /** @defgroup RCC_LL_Private_Functions RCC Private functions
  77.   * @{
  78.   */
  79. uint32_t RCC_GetSystemClockFreq(void);
  80. uint32_t RCC_GetHCLKClockFreq(uint32_t SYSCLK_Frequency);
  81. uint32_t RCC_GetPCLK1ClockFreq(uint32_t HCLK_Frequency);
  82. uint32_t RCC_GetPCLK2ClockFreq(uint32_t HCLK_Frequency);
  83. uint32_t RCC_PLL_GetFreqDomain_SYS(void);
  84. #if defined(RCC_PLLI2S_SUPPORT)
  85. uint32_t RCC_PLLI2S_GetFreqDomain_I2S(void);
  86. #endif /* RCC_PLLI2S_SUPPORT */
  87. #if defined(RCC_PLL2_SUPPORT)
  88. uint32_t RCC_PLL2_GetFreqClockFreq(void);
  89. #endif /* RCC_PLL2_SUPPORT */
  90. /**
  91.   * @}
  92.   */
  93.  
  94. /* Exported functions --------------------------------------------------------*/
  95. /** @addtogroup RCC_LL_Exported_Functions
  96.   * @{
  97.   */
  98.  
  99. /** @addtogroup RCC_LL_EF_Init
  100.   * @{
  101.   */
  102.  
  103. /**
  104.   * @brief  Reset the RCC clock configuration to the default reset state.
  105.   * @note   The default reset state of the clock configuration is given below:
  106.   *         - HSI ON and used as system clock source
  107.   *         - HSE PLL, PLL2 & PLL3 are OFF
  108.   *         - AHB, APB1 and APB2 prescaler set to 1.
  109.   *         - CSS, MCO OFF
  110.   *         - All interrupts disabled
  111.   * @note   This function doesn't modify the configuration of the
  112.   *         - Peripheral clocks
  113.   *         - LSI, LSE and RTC clocks
  114.   * @retval An ErrorStatus enumeration value:
  115.   *         - SUCCESS: RCC registers are de-initialized
  116.   *         - ERROR: not applicable
  117.   */
  118. ErrorStatus LL_RCC_DeInit(void)
  119. {
  120.   /* Set HSION bit */
  121.   LL_RCC_HSI_Enable();
  122.  
  123.   /* Wait for HSI READY bit */
  124.   while(LL_RCC_HSI_IsReady() != 1U)
  125.   {}
  126.  
  127.   /* Configure HSI as system clock source */
  128.   LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_HSI);
  129.  
  130.   /* Wait till clock switch is ready */
  131.   while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_HSI)
  132.   {}
  133.  
  134.   /* Reset PLLON bit */
  135.   CLEAR_BIT(RCC->CR, RCC_CR_PLLON);
  136.  
  137.   /* Wait for PLL READY bit to be reset */
  138.   while(LL_RCC_PLL_IsReady() != 0U)
  139.   {}
  140.  
  141.   /* Reset CFGR register */
  142.   LL_RCC_WriteReg(CFGR, 0x00000000U);
  143.  
  144.   /* Reset HSEON, HSEBYP & CSSON bits */
  145.   CLEAR_BIT(RCC->CR, (RCC_CR_CSSON | RCC_CR_HSEON | RCC_CR_HSEBYP));
  146.  
  147. #if defined(RCC_CR_PLL2ON)
  148.   /* Reset PLL2ON bit */
  149.   CLEAR_BIT(RCC->CR, RCC_CR_PLL2ON);
  150. #endif /* RCC_CR_PLL2ON */
  151.  
  152. #if defined(RCC_CR_PLL3ON)
  153.   /* Reset PLL3ON bit */
  154.   CLEAR_BIT(RCC->CR, RCC_CR_PLL3ON);
  155. #endif /* RCC_CR_PLL3ON */
  156.  
  157.   /* Set HSITRIM bits to the reset value */
  158.   LL_RCC_HSI_SetCalibTrimming(0x10U);
  159.  
  160. #if defined(RCC_CFGR2_PREDIV1)
  161.   /* Reset CFGR2 register */
  162.   LL_RCC_WriteReg(CFGR2, 0x00000000U);
  163. #endif /* RCC_CFGR2_PREDIV1 */
  164.  
  165.   /* Disable all interrupts */
  166.   LL_RCC_WriteReg(CIR, 0x00000000U);
  167.  
  168.   /* Clear reset flags */
  169.   LL_RCC_ClearResetFlags();
  170.  
  171.   return SUCCESS;
  172. }
  173.  
  174. /**
  175.   * @}
  176.   */
  177.  
  178. /** @addtogroup RCC_LL_EF_Get_Freq
  179.   * @brief  Return the frequencies of different on chip clocks;  System, AHB, APB1 and APB2 buses clocks
  180.   *         and different peripheral clocks available on the device.
  181.   * @note   If SYSCLK source is HSI, function returns values based on HSI_VALUE(**)
  182.   * @note   If SYSCLK source is HSE, function returns values based on HSE_VALUE(***)
  183.   * @note   If SYSCLK source is PLL, function returns values based on
  184.   *         HSI_VALUE(**) or HSE_VALUE(***) multiplied/divided by the PLL factors.
  185.   * @note   (**) HSI_VALUE is a defined constant but the real value may vary
  186.   *              depending on the variations in voltage and temperature.
  187.   * @note   (***) HSE_VALUE is a defined constant, user has to ensure that
  188.   *               HSE_VALUE is same as the real frequency of the crystal used.
  189.   *               Otherwise, this function may have wrong result.
  190.   * @note   The result of this function could be incorrect when using fractional
  191.   *         value for HSE crystal.
  192.   * @note   This function can be used by the user application to compute the
  193.   *         baud-rate for the communication peripherals or configure other parameters.
  194.   * @{
  195.   */
  196.  
  197. /**
  198.   * @brief  Return the frequencies of different on chip clocks;  System, AHB, APB1 and APB2 buses clocks
  199.   * @note   Each time SYSCLK, HCLK, PCLK1 and/or PCLK2 clock changes, this function
  200.   *         must be called to update structure fields. Otherwise, any
  201.   *         configuration based on this function will be incorrect.
  202.   * @param  RCC_Clocks pointer to a @ref LL_RCC_ClocksTypeDef structure which will hold the clocks frequencies
  203.   * @retval None
  204.   */
  205. void LL_RCC_GetSystemClocksFreq(LL_RCC_ClocksTypeDef *RCC_Clocks)
  206. {
  207.   /* Get SYSCLK frequency */
  208.   RCC_Clocks->SYSCLK_Frequency = RCC_GetSystemClockFreq();
  209.  
  210.   /* HCLK clock frequency */
  211.   RCC_Clocks->HCLK_Frequency   = RCC_GetHCLKClockFreq(RCC_Clocks->SYSCLK_Frequency);
  212.  
  213.   /* PCLK1 clock frequency */
  214.   RCC_Clocks->PCLK1_Frequency  = RCC_GetPCLK1ClockFreq(RCC_Clocks->HCLK_Frequency);
  215.  
  216.   /* PCLK2 clock frequency */
  217.   RCC_Clocks->PCLK2_Frequency  = RCC_GetPCLK2ClockFreq(RCC_Clocks->HCLK_Frequency);
  218. }
  219.  
  220. #if defined(RCC_CFGR2_I2S2SRC)
  221. /**
  222.   * @brief  Return I2Sx clock frequency
  223.   * @param  I2SxSource This parameter can be one of the following values:
  224.   *         @arg @ref LL_RCC_I2S2_CLKSOURCE
  225.   *         @arg @ref LL_RCC_I2S3_CLKSOURCE
  226.   * @retval I2S clock frequency (in Hz)
  227.   */
  228. uint32_t LL_RCC_GetI2SClockFreq(uint32_t I2SxSource)
  229. {
  230.   uint32_t i2s_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  231.  
  232.   /* Check parameter */
  233.   assert_param(IS_LL_RCC_I2S_CLKSOURCE(I2SxSource));
  234.  
  235.   /* I2S1CLK clock frequency */
  236.   switch (LL_RCC_GetI2SClockSource(I2SxSource))
  237.   {
  238.     case LL_RCC_I2S2_CLKSOURCE_SYSCLK:        /*!< System clock selected as I2S clock source */
  239.     case LL_RCC_I2S3_CLKSOURCE_SYSCLK:
  240.       i2s_frequency = RCC_GetSystemClockFreq();
  241.       break;
  242.  
  243.     case LL_RCC_I2S2_CLKSOURCE_PLLI2S_VCO:    /*!< PLLI2S oscillator clock selected as I2S clock source */
  244.     case LL_RCC_I2S3_CLKSOURCE_PLLI2S_VCO:
  245.     default:
  246.       i2s_frequency = RCC_PLLI2S_GetFreqDomain_I2S() * 2U;
  247.       break;
  248.   }
  249.  
  250.   return i2s_frequency;
  251. }
  252. #endif /* RCC_CFGR2_I2S2SRC */
  253.  
  254. #if defined(USB) || defined(USB_OTG_FS)
  255. /**
  256.   * @brief  Return USBx clock frequency
  257.   * @param  USBxSource This parameter can be one of the following values:
  258.   *         @arg @ref LL_RCC_USB_CLKSOURCE
  259.   * @retval USB clock frequency (in Hz)
  260.   *         @arg @ref LL_RCC_PERIPH_FREQUENCY_NO indicates that oscillator (HSI), HSE or PLL is not ready
  261.   */
  262. uint32_t LL_RCC_GetUSBClockFreq(uint32_t USBxSource)
  263. {
  264.   uint32_t usb_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  265.  
  266.   /* Check parameter */
  267.   assert_param(IS_LL_RCC_USB_CLKSOURCE(USBxSource));
  268.  
  269.   /* USBCLK clock frequency */
  270.   switch (LL_RCC_GetUSBClockSource(USBxSource))
  271.   {
  272. #if defined(RCC_CFGR_USBPRE)  
  273.     case LL_RCC_USB_CLKSOURCE_PLL:        /* PLL clock used as USB clock source */
  274.       if (LL_RCC_PLL_IsReady())
  275.       {
  276.         usb_frequency = RCC_PLL_GetFreqDomain_SYS();
  277.       }
  278.       break;
  279.  
  280.     case LL_RCC_USB_CLKSOURCE_PLL_DIV_1_5:        /* PLL clock divided by 1.5 used as USB clock source */
  281.     default:
  282.       if (LL_RCC_PLL_IsReady())
  283.       {
  284.         usb_frequency = (RCC_PLL_GetFreqDomain_SYS() * 3U) / 2U;
  285.       }
  286.       break;
  287. #endif /* RCC_CFGR_USBPRE */
  288. #if defined(RCC_CFGR_OTGFSPRE)
  289.     /* USBCLK = PLLVCO/2
  290.               = (2 x PLLCLK) / 2
  291.               = PLLCLK */
  292.     case LL_RCC_USB_CLKSOURCE_PLL_DIV_2:        /* PLL clock used as USB clock source */
  293.       if (LL_RCC_PLL_IsReady())
  294.       {
  295.         usb_frequency = RCC_PLL_GetFreqDomain_SYS();
  296.       }
  297.       break;
  298.  
  299.     /* USBCLK = PLLVCO/3
  300.               = (2 x PLLCLK) / 3 */
  301.     case LL_RCC_USB_CLKSOURCE_PLL_DIV_3:        /* PLL clock divided by 3 used as USB clock source */
  302.     default:
  303.       if (LL_RCC_PLL_IsReady())
  304.       {
  305.         usb_frequency = (RCC_PLL_GetFreqDomain_SYS() * 2U) / 3U;
  306.       }
  307.       break;
  308. #endif /* RCC_CFGR_OTGFSPRE */
  309.   }
  310.  
  311.   return usb_frequency;
  312. }
  313. #endif /* USB */
  314.  
  315. /**
  316.   * @brief  Return ADCx clock frequency
  317.   * @param  ADCxSource This parameter can be one of the following values:
  318.   *         @arg @ref LL_RCC_ADC_CLKSOURCE
  319.   * @retval ADC clock frequency (in Hz)
  320.   */
  321. uint32_t LL_RCC_GetADCClockFreq(uint32_t ADCxSource)
  322. {
  323.   uint32_t adc_prescaler = 0U;
  324.   uint32_t adc_frequency = 0U;
  325.  
  326.   /* Check parameter */
  327.   assert_param(IS_LL_RCC_ADC_CLKSOURCE(ADCxSource));
  328.  
  329.   /* Get ADC prescaler */
  330.   adc_prescaler = LL_RCC_GetADCClockSource(ADCxSource);
  331.  
  332.   /* ADC frequency = PCLK2 frequency / ADC prescaler (2, 4, 6 or 8) */
  333.   adc_frequency = RCC_GetPCLK2ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()))
  334.                   / (((adc_prescaler >> POSITION_VAL(ADCxSource)) + 1U) * 2U);
  335.  
  336.   return adc_frequency;
  337. }
  338.  
  339. /**
  340.   * @}
  341.   */
  342.  
  343. /**
  344.   * @}
  345.   */
  346.  
  347. /** @addtogroup RCC_LL_Private_Functions
  348.   * @{
  349.   */
  350.  
  351. /**
  352.   * @brief  Return SYSTEM clock frequency
  353.   * @retval SYSTEM clock frequency (in Hz)
  354.   */
  355. uint32_t RCC_GetSystemClockFreq(void)
  356. {
  357.   uint32_t frequency = 0U;
  358.  
  359.   /* Get SYSCLK source -------------------------------------------------------*/
  360.   switch (LL_RCC_GetSysClkSource())
  361.   {
  362.     case LL_RCC_SYS_CLKSOURCE_STATUS_HSI:  /* HSI used as system clock  source */
  363.       frequency = HSI_VALUE;
  364.       break;
  365.  
  366.     case LL_RCC_SYS_CLKSOURCE_STATUS_HSE:  /* HSE used as system clock  source */
  367.       frequency = HSE_VALUE;
  368.       break;
  369.  
  370.     case LL_RCC_SYS_CLKSOURCE_STATUS_PLL:  /* PLL used as system clock  source */
  371.       frequency = RCC_PLL_GetFreqDomain_SYS();
  372.       break;
  373.  
  374.     default:
  375.       frequency = HSI_VALUE;
  376.       break;
  377.   }
  378.  
  379.   return frequency;
  380. }
  381.  
  382. /**
  383.   * @brief  Return HCLK clock frequency
  384.   * @param  SYSCLK_Frequency SYSCLK clock frequency
  385.   * @retval HCLK clock frequency (in Hz)
  386.   */
  387. uint32_t RCC_GetHCLKClockFreq(uint32_t SYSCLK_Frequency)
  388. {
  389.   /* HCLK clock frequency */
  390.   return __LL_RCC_CALC_HCLK_FREQ(SYSCLK_Frequency, LL_RCC_GetAHBPrescaler());
  391. }
  392.  
  393. /**
  394.   * @brief  Return PCLK1 clock frequency
  395.   * @param  HCLK_Frequency HCLK clock frequency
  396.   * @retval PCLK1 clock frequency (in Hz)
  397.   */
  398. uint32_t RCC_GetPCLK1ClockFreq(uint32_t HCLK_Frequency)
  399. {
  400.   /* PCLK1 clock frequency */
  401.   return __LL_RCC_CALC_PCLK1_FREQ(HCLK_Frequency, LL_RCC_GetAPB1Prescaler());
  402. }
  403.  
  404. /**
  405.   * @brief  Return PCLK2 clock frequency
  406.   * @param  HCLK_Frequency HCLK clock frequency
  407.   * @retval PCLK2 clock frequency (in Hz)
  408.   */
  409. uint32_t RCC_GetPCLK2ClockFreq(uint32_t HCLK_Frequency)
  410. {
  411.   /* PCLK2 clock frequency */
  412.   return __LL_RCC_CALC_PCLK2_FREQ(HCLK_Frequency, LL_RCC_GetAPB2Prescaler());
  413. }
  414.  
  415. /**
  416.   * @brief  Return PLL clock frequency used for system domain
  417.   * @retval PLL clock frequency (in Hz)
  418.   */
  419. uint32_t RCC_PLL_GetFreqDomain_SYS(void)
  420. {
  421.   uint32_t pllinputfreq = 0U, pllsource = 0U;
  422.  
  423.   /* PLL_VCO = (HSE_VALUE, HSI_VALUE or PLL2 / PLL Predivider) * PLL Multiplicator */
  424.  
  425.   /* Get PLL source */
  426.   pllsource = LL_RCC_PLL_GetMainSource();
  427.  
  428.   switch (pllsource)
  429.   {
  430.     case LL_RCC_PLLSOURCE_HSI_DIV_2: /* HSI used as PLL clock source */
  431.       pllinputfreq = HSI_VALUE / 2U;
  432.       break;
  433.  
  434.     case LL_RCC_PLLSOURCE_HSE:       /* HSE used as PLL clock source */
  435.       pllinputfreq = HSE_VALUE / (LL_RCC_PLL_GetPrediv() + 1U);
  436.       break;
  437.  
  438. #if defined(RCC_PLL2_SUPPORT)
  439.     case LL_RCC_PLLSOURCE_PLL2:       /* PLL2 used as PLL clock source */
  440.       pllinputfreq = RCC_PLL2_GetFreqClockFreq() / (LL_RCC_PLL_GetPrediv() + 1U);
  441.       break;
  442. #endif /* RCC_PLL2_SUPPORT */
  443.  
  444.     default:
  445.       pllinputfreq = HSI_VALUE / 2U;
  446.       break;
  447.   }
  448.   return __LL_RCC_CALC_PLLCLK_FREQ(pllinputfreq, LL_RCC_PLL_GetMultiplicator());
  449. }
  450.  
  451. #if defined(RCC_PLL2_SUPPORT)
  452. /**
  453.   * @brief  Return PLL clock frequency used for system domain
  454.   * @retval PLL clock frequency (in Hz)
  455.   */
  456. uint32_t RCC_PLL2_GetFreqClockFreq(void)
  457. {
  458.   return __LL_RCC_CALC_PLL2CLK_FREQ(HSE_VALUE, LL_RCC_PLL2_GetMultiplicator(), LL_RCC_HSE_GetPrediv2());
  459. }
  460. #endif /* RCC_PLL2_SUPPORT */
  461.  
  462. #if defined(RCC_PLLI2S_SUPPORT)
  463. /**
  464.   * @brief  Return PLL clock frequency used for system domain
  465.   * @retval PLL clock frequency (in Hz)
  466.   */
  467. uint32_t RCC_PLLI2S_GetFreqDomain_I2S(void)
  468. {
  469.   return __LL_RCC_CALC_PLLI2SCLK_FREQ(HSE_VALUE, LL_RCC_PLLI2S_GetMultiplicator(), LL_RCC_HSE_GetPrediv2());
  470. }
  471. #endif /* RCC_PLLI2S_SUPPORT */
  472.  
  473. /**
  474.   * @}
  475.   */
  476.  
  477. /**
  478.   * @}
  479.   */
  480.  
  481. #endif /* defined(RCC) */
  482.  
  483. /**
  484.   * @}
  485.   */
  486.  
  487. #endif /* USE_FULL_LL_DRIVER */
  488.  
  489. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  490.