[STM32F4] STM32F4的RCC时钟配置

[复制链接]
2499|4
 楼主| huahuagg 发表于 2017-12-15 17:32 | 显示全部楼层 |阅读模式
  1. /**
  2.   ******************************************************************************
  3.   * [url=home.php?mod=space&uid=288409]@file[/url]    RCC/RCC_ClockConfig/Src/main.c
  4.   * [url=home.php?mod=space&uid=187600]@author[/url]  MCD Application Team
  5.   * [url=home.php?mod=space&uid=895143]@version[/url] V1.2.6
  6.   * [url=home.php?mod=space&uid=212281]@date[/url]    06-May-2016
  7.   * [url=home.php?mod=space&uid=247401]@brief[/url]   This example describes how to use the RCC HAL API to configure the
  8.   *          system clock (SYSCLK) and modify the clock settings on run time.
  9.   ******************************************************************************
  10.   * @attention
  11.   *
  12.   * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  13.   *
  14.   * Redistribution and use in source and binary forms, with or without modification,
  15.   * are permitted provided that the following conditions are met:
  16.   *   1. Redistributions of source code must retain the above copyright notice,
  17.   *      this list of conditions and the following disclaimer.
  18.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  19.   *      this list of conditions and the following disclaimer in the documentation
  20.   *      and/or other materials provided with the distribution.
  21.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  22.   *      may be used to endorse or promote products derived from this software
  23.   *      without specific prior written permission.
  24.   *
  25.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35.   *
  36.   ******************************************************************************
  37.   */

  38. /* Includes ------------------------------------------------------------------*/
  39. #include "main.h"

  40. /** @addtogroup STM32F4xx_HAL_Examples
  41.   * @{
  42.   */

  43. /** @addtogroup RCC_ClockConfig
  44.   * @{
  45.   */

  46. /* Private typedef -----------------------------------------------------------*/
  47. /* Private define ------------------------------------------------------------*/
  48. /* Private macro -------------------------------------------------------------*/
  49. /* Private variables ---------------------------------------------------------*/
  50. static RCC_ClkInitTypeDef RCC_ClkInitStruct;
  51. static RCC_OscInitTypeDef RCC_OscInitStruct;

  52. /* Private function prototypes -----------------------------------------------*/
  53. static void SystemClockHSI_Config(void);
  54. static void SystemClockHSE_Config(void);
  55. static void Error_Handler(void);

  56. /* Private functions ---------------------------------------------------------*/

  57. /**
  58.   * @brief  Main program
  59.   * @param  None
  60.   * @retval None
  61.   */
  62. int main(void)
  63. {
  64.   /* STM32F4xx HAL library initialization:
  65.        - Configure the Flash prefetch, instruction and Data caches
  66.        - Configure the Systick to generate an interrupt each 1 msec
  67.        - Set NVIC Group Priority to 4
  68.        - Global MSP (MCU Support Package) initialization
  69.      */
  70.   HAL_Init();

  71.   /* Configure LED3, LED4, LED5 and LED6 */
  72.   BSP_LED_Init(LED3);
  73.   BSP_LED_Init(LED4);
  74.   BSP_LED_Init(LED5);
  75.   BSP_LED_Init(LED6);
  76.   
  77.   /* Configure USER Button, used to trigger an interrupt each time it's pressed.
  78.      In the ISR the PLL source will be changed from HSE to HSI, and vice versa. */
  79.   BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_EXTI);  

  80.   /* Enable Power Control clock */
  81.   __HAL_RCC_PWR_CLK_ENABLE();

  82.   /* The voltage scaling allows optimizing the power consumption when the device is
  83.      clocked below the maximum system frequency, to update the voltage scaling value
  84.      regarding system frequency refer to product datasheet.  */
  85.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  86.    
  87.   /* Enable HSE oscillator and configure the PLL to reach the max system frequency (168MHz)
  88.      when using HSE oscillator as PLL clock source. */
  89.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  90.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  91.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  92.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  93.   RCC_OscInitStruct.PLL.PLLM = 8;
  94.   RCC_OscInitStruct.PLL.PLLN = 336;
  95.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  96.   RCC_OscInitStruct.PLL.PLLQ = 7;
  97.   if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  98.   {
  99.     /* Initialization Error */
  100.     Error_Handler();
  101.   }
  102.   
  103.   /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers.
  104.      The SysTick 1 msec interrupt is required for the HAL process (Timeout management); by default
  105.      the configuration is done using the HAL_Init() API, and when the system clock configuration
  106.      is updated the SysTick configuration will be adjusted by the HAL_RCC_ClockConfig() API. */
  107.   RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  108.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  109.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  110.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;  
  111.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;  
  112.   if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  113.   {
  114.     /* Initialization Error */
  115.     Error_Handler();
  116.   }

  117.   /* STM32F405x/407x/415x/417x Revision Z devices: prefetch is supported  */
  118.   if (HAL_GetREVID() == 0x1001)
  119.   {
  120.     /* Enable the Flash prefetch */
  121.     __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
  122.   }

  123.   /* Output SYSCLK divided by 2 on MCO2 pin(PC9) */
  124.   HAL_RCC_MCOConfig(RCC_MCO2, RCC_MCO2SOURCE_SYSCLK, RCC_MCODIV_2);

  125.   /* Toggle some LEDs in an infinite loop */  
  126.   while (1)
  127.   {
  128.    /* Toggle LED3 */
  129.     BSP_LED_Toggle(LED3);
  130.     HAL_Delay(100);
  131.     /* Toggle LED4 */
  132.     BSP_LED_Toggle(LED4);
  133.     HAL_Delay(100);
  134.     /* Toggle LED6 */
  135.     BSP_LED_Toggle(LED6);
  136.     HAL_Delay(100);
  137.   }
  138. }

  139. /**
  140.   * @brief  EXTI line detection callbacks.
  141.   * @param  GPIO_Pin: Specifies the pins connected EXTI line
  142.   * @retval None
  143.   */
  144. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  145. {
  146.   if(GPIO_Pin == KEY_BUTTON_PIN)
  147.   {
  148.     if(__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLCFGR_PLLSRC_HSI)
  149.     {
  150.       /* Set SYSCLK frequency to 168 MHz, coming from the PLL which is clocked by the HSE */
  151.       SystemClockHSE_Config();
  152.     }
  153.     else /*  PLL source is HSE oscillator */
  154.     {
  155.       /* Set SYSCLK frequency to 168 MHz, coming from the PLL which is clocked by the HSI */
  156.       SystemClockHSI_Config();
  157.     }
  158.   }
  159. }

  160. /**
  161.   * @brief  Switch the PLL source from HSI to HSE, and select the PLL as SYSCLK
  162.   *         source.
  163.   *         The system Clock is configured as follow :
  164.   *            System Clock source            = PLL (HSE)
  165.   *            SYSCLK(Hz)                     = 168000000
  166.   *            HCLK(Hz)                       = 168000000
  167.   *            AHB Prescaler                  = 1
  168.   *            APB1 Prescaler                 = 4
  169.   *            APB2 Prescaler                 = 2
  170.   *            HSE Frequency(Hz)              = 8000000
  171.   *            PLL_M                          = 8
  172.   *            PLL_N                          = 336
  173.   *            PLL_P                          = 2
  174.   *            PLL_Q                          = 7
  175.   *            VDD(V)                         = 3.3
  176.   *            Main regulator output voltage  = Scale1 mode
  177.   *            Flash Latency(WS)              = 5
  178.   * @param  None
  179.   * @retval None
  180.   */
  181. void SystemClockHSE_Config(void)
  182. {

  183.   /* -1- Select HSI as system clock source to allow modification of the PLL configuration */
  184.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
  185.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  186.   if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  187.   {
  188.     /* Initialization Error */
  189.     Error_Handler();
  190.   }
  191.   
  192.   /* -2- Enable HSE Oscillator, select it as PLL source and finally activate the PLL */
  193.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  194.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  195.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  196.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  197.   RCC_OscInitStruct.PLL.PLLM = 8;
  198.   RCC_OscInitStruct.PLL.PLLN = 336;
  199.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  200.   RCC_OscInitStruct.PLL.PLLQ = 7;
  201.   if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  202.   {
  203.     /* Initialization Error */
  204.     Error_Handler();
  205.   }
  206.   
  207.   /* -3- Select the PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
  208.   RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  209.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  210.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  211.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;  
  212.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;  
  213.   if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  214.   {
  215.     /* Initialization Error */
  216.     Error_Handler();
  217.   }
  218.   
  219.   /* -4- Optional: Disable HSI Oscillator (if the HSI is no more needed by the application)*/
  220.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  221.   RCC_OscInitStruct.HSIState = RCC_HSI_OFF;
  222.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  223.   if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  224.   {
  225.     /* Initialization Error */
  226.     Error_Handler();
  227.   }
  228. }

  229. /**
  230.   * @brief  Switch the PLL source from HSI to HSE, and select the PLL as SYSCLK
  231.   *         source.
  232.   *         The system Clock is configured as follow :
  233.   *            System Clock source            = PLL (HSI)
  234.   *            SYSCLK(Hz)                     = 168000000
  235.   *            HCLK(Hz)                       = 168000000
  236.   *            AHB Prescaler                  = 1
  237.   *            APB1 Prescaler                 = 4
  238.   *            APB2 Prescaler                 = 2
  239.   *            HSI Frequency(Hz)              = 16000000
  240.   *            PLL_M                          = 16
  241.   *            PLL_N                          = 336
  242.   *            PLL_P                          = 2
  243.   *            VDD(V)                         = 3.3
  244.   *            Main regulator output voltage  = Scale1 mode
  245.   *            Flash Latency(WS)              = 5
  246.   * @param  None
  247.   * @retval None
  248.   */
  249. void SystemClockHSI_Config(void)
  250. {
  251.   /* -1- Select HSE as system clock source to allow modification of the PLL configuration */
  252.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
  253.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
  254.   if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  255.   {
  256.     /* Initialization Error */
  257.     Error_Handler();
  258.   }

  259.   /* -2- Enable HSI Oscillator, select it as PLL source and finally activate the PLL */
  260.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  261.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  262.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  263.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  264.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  265.   RCC_OscInitStruct.PLL.PLLM = 16;
  266.   RCC_OscInitStruct.PLL.PLLN = 336;
  267.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  268.   if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  269.   {
  270.     /* Initialization Error */
  271.     Error_Handler();
  272.   }
  273.   
  274.   /* -3- Select the PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
  275.   RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  276.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  277.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  278.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;  
  279.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;  
  280.   if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  281.   {
  282.     /* Initialization Error */
  283.     Error_Handler();
  284.   }
  285.   
  286.   /* -4- Optional: Disable HSE Oscillator (if the HSE is no more needed by the application) */
  287.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  288.   RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
  289.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  290.   if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  291.   {
  292.     /* Initialization Error */
  293.     Error_Handler();
  294.   }
  295. }

  296. /**
  297.   * @brief  This function is executed in case of error occurrence.
  298.   * @param  None
  299.   * @retval None
  300.   */
  301. static void Error_Handler(void)
  302. {
  303.   /* Turn LED5 on */
  304.   BSP_LED_On(LED5);
  305.   while(1)
  306.   {
  307.   }
  308. }

  309. #ifdef  USE_FULL_ASSERT

  310. /**
  311.   * @brief  Reports the name of the source file and the source line number
  312.   *         where the assert_param error has occurred.
  313.   * @param  file: pointer to the source file name
  314.   * @param  line: assert_param error line source number
  315.   * @retval None
  316.   */
  317. void assert_failed(uint8_t* file, uint32_t line)
  318. {
  319.   /* User can add his own implementation to report the file name and line number,
  320.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  321.   /* Infinite loop */
  322.   while (1)
  323.   {
  324.   }
  325. }
  326. #endif

  327. /**
  328.   * @}
  329.   */

  330. /**
  331.   * @}
  332.   */

  333. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/


 楼主| huahuagg 发表于 2017-12-15 18:03 | 显示全部楼层
其实ST应该在例子中给出计算公式比较好
xinpian101 发表于 2017-12-15 18:59 | 显示全部楼层
还是要看手册,虽然手册一千多页,不好看。。
xinpian101 发表于 2017-12-15 19:02 | 显示全部楼层
这个还是用cubeF配置吧。
稳稳の幸福 发表于 2017-12-15 20:28 | 显示全部楼层
还是用图形工具配置好,还是学好那个工具吧。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

159

主题

1430

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部