stm32 clock setting function

Publisher:Heavenly999Latest update time:2016-12-16 Source: eefocusKeywords:stm32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

This involves a very important register, the clock configuration register: RCC_CFGR



 1 #if defined (STM32F10X_LD_VL) || (defined STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)

 2 /* #define SYSCLK_FREQ_HSE    HSE_VALUE */

 3  #define SYSCLK_FREQ_24MHz  24000000

 4 #else

 5 /* #define SYSCLK_FREQ_HSE    HSE_VALUE */

 6 /* #define SYSCLK_FREQ_24MHz  24000000 */ 

 7 /* #define SYSCLK_FREQ_36MHz  36000000 */

 8 /* #define SYSCLK_FREQ_48MHz  48000000 */

 9 /* #define SYSCLK_FREQ_56MHz  56000000 */

10 #define SYSCLK_FREQ_72MHz  72000000

11 #endif


 1 /**

 2   * @brief  Setup the microcontroller system

 3   *         Initialize the Embedded Flash Interface, the PLL and update the 

 4   *         SystemCoreClock variable.

 5   * @note   This function should be used only after reset.

 6   * @param  None

 7   * @retval None

 8   */

 9 void SystemInit (void)

10 {

11   /* Reset the RCC clock configuration to the default reset state(for debug purpose) */

12   /* Set HSION bit */

13   RCC->CR |= (uint32_t)0x00000001;

14 

15   /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */

16 #ifndef STM32F10X_CL

17   RCC->CFGR &= (uint32_t)0xF8FF0000;

18 #else

19   RCC->CFGR &= (uint32_t)0xF0FF0000;

20 #endif /* STM32F10X_CL */   

21   

22   /* Reset HSEON, CSSON and PLLON bits */

23   RCC->CR &= (uint32_t)0xFEF6FFFF;

24 

25   /* Reset HSEBYP bit */

26   RCC->CR &= (uint32_t)0xFFFBFFFF;

27 

28   /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */

29   RCC->CFGR &= (uint32_t)0xFF80FFFF;

30 

31 #ifdef STM32F10X_CL

32   /* Reset PLL2ON and PLL3ON bits */

33   RCC->CR &= (uint32_t)0xEBFFFFFF;

34 

35   /* Disable all interrupts and clear pending bits  */

36   RCC->CIR = 0x00FF0000;

37 

38   /* Reset CFGR2 register */

39   RCC->CFGR2 = 0x00000000;

40 #elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)

41   /* Disable all interrupts and clear pending bits  */

42   RCC->CIR = 0x009F0000;

43 

44   /* Reset CFGR2 register */

45   RCC->CFGR2 = 0x00000000;      

46 #else

47   /* Disable all interrupts and clear pending bits  */

48   RCC->CIR = 0x009F0000;

49 #endif /* STM32F10X_CL */

50     

51 #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)

52   #ifdef DATA_IN_ExtSRAM

53     SystemInit_ExtMemCtl(); 

54   #endif /* DATA_IN_ExtSRAM */

55 #endif 

56 

57   /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */

58   /* Configure the Flash Latency cycles and enable prefetch buffer */

59   SetSysClock();

60 

61 #ifdef VECT_TAB_SRAM

62   SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */

63 #else

64   SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */

65 #endif 

66 }


 


/**

  * @brief  Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.

  * @param  None

  * @retval None

  */

static void SetSysClock(void)

{

#ifdef SYSCLK_FREQ_HSE

  SetSysClockToHSE();

#elif defined SYSCLK_FREQ_24MHz

  SetSysClockTo24();

#elif defined SYSCLK_FREQ_36MHz

  SetSysClockTo36();

#elif defined SYSCLK_FREQ_48MHz

  SetSysClockTo48();

#elif defined SYSCLK_FREQ_56MHz

  SetSysClockTo56();  

#elif defined SYSCLK_FREQ_72MHz

  SetSysClockTo72();

#endif


 Set the bit parameters of the RCC_CFGR register to match the external crystal oscillator to obtain a 72M system clock.



  1 /**

  2   * @brief  Sets System clock frequency to 72MHz and configure HCLK, PCLK2 

  3   *         and PCLK1 prescalers. 

  4   * @note   This function should be used only after reset.

  5   * @param  None

  6   * @retval None

  7   */

  8 static void SetSysClockTo72(void)

  9 {

 10   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;

 11   

 12   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/    

 13   /* Enable HSE */    

 14   RCC->CR |= ((uint32_t)RCC_CR_HSEON);

 15  

 16   /* Wait till HSE is ready and if Time out is reached exit */

 17   do

 18   {

 19     HSEStatus = RCC->CR & RCC_CR_HSERDY;

 20     StartUpCounter++;  

 21   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));

 22 

 23   if ((RCC->CR & RCC_CR_HSERDY) != RESET)

 24   {

 25     HSEStatus = (uint32_t)0x01;

 26   }

 27   else

 28   {

 29     HSEStatus = (uint32_t)0x00;

 30   }  

 31 

 32   if (HSEStatus == (uint32_t)0x01)

 33   {

 34     /* Enable Prefetch Buffer */

 35     FLASH->ACR |= FLASH_ACR_PRFTBE;

 36 

 37     /* Flash 2 wait state */

 38     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);

 39     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;    

 40 

 41  

 42     /* HCLK = SYSCLK */

 43 RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1; //AHB one frequency division

 44       

 45     /* PCLK2 = HCLK */

 46     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;

 47     

 48     /* PCLK1 = HCLK */

 49     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;

 50 

 51 #ifdef STM32F10X_CL

 52     /* Configure PLLs ------------------------------------------------------*/

 53     /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */

 54     /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */

 55         

 56     RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |

 57                               RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);

 58     RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |

 59                              RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);

 60   

 61     /* Enable PLL2 */

 62     RCC->CR |= RCC_CR_PLL2ON;

 63     /* Wait till PLL2 is ready */

 64     while((RCC->CR & RCC_CR_PLL2RDY) == 0)

 65     {

 66     }

 67     

 68    

 69     /* PLL configuration: PLLCLK = PREDIV1 * 9 = 72 MHz */ 

 70     RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);

 71     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 | 

 72                             RCC_CFGR_PLLMULL9); 

 73 #else    

 74     /*  PLL configuration: PLLCLK = HSE * 9 = 72 MHz */

 75     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |

 76                                         RCC_CFGR_PLLMULL));

 77     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL9);

 78 #endif /* STM32F10X_CL */

 79 

 80     /* Enable PLL */

 81     RCC->CR |= RCC_CR_PLLON;

 82 

 83     /* Wait till PLL is ready */

 84     while((RCC->CR & RCC_CR_PLLRDY) == 0)

 85     {

 86     }

 87     

 88     /* Select PLL as system clock source */

 89     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));

 90     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;    

 91 

 92     /* Wait till PLL is used as system clock source */

 93     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)

 94     {

 95     }

 96   }

 97   else

 98   { /* If HSE fails to start-up, the application will have wrong clock 

 99          configuration. User can add here some code to deal with this error */

100   }

101 }


Keywords:stm32 Reference address:stm32 clock setting function

Previous article:STM32 Hardware IIC
Next article:Bit-band operation in STM32

Recommended ReadingLatest update time:2024-11-16 17:52

Quickly learn Arm (22)--lpc2300 system clock
I have been busy with some work these days and have no time to look at Arm. The development board needs to be changed from 2478 to 2378, so I started to look at the information of 2378 again. In fact, the two are similar, but 2478 supports LCD, while 2378 does not, and the other differences are similar. I have talke
[Microcontroller]
Quickly learn Arm (22)--lpc2300 system clock
Problems encountered when jumping user code from bootloader in STM32 F4
Problems encountered when running the user program after the code jump: DMA2_Stream3_IRQHandler                                            DMA2_Stream4_IRQHandler                                         ETH_IRQHandler                                                          ETH_WKUP_IRQHandler                     
[Microcontroller]
STM32 defines constant array to FLASH fixed address
Method: static const uint8_t s_acBmpLogo030 __attribute__((at(0X800F000)))={0x80,0xC0,0xC0,0xC0,0xC0,0x80,xxxxxxx}   After compiling, you can see the assigned address in the .map file 0x0800f000   0x0000005c   Data   RO         4013    .ARM.__AT_0x0800F000  main.o   This is the flash data viewed from the debug window
[Microcontroller]
STM32 defines constant array to FLASH fixed address
STM32 records and plays infrared signals from air conditioner/TV remote control
Preparation: (1) Infrared receiver  (2) IR transmitter  (3) STM32 control board  (4) Jumper cables  (5) Air conditioner/TV remote control hardware 1. Module  IR transmitter VCC, GND, DATA; IR receiver VCC, GND, DATA; software To verify that infrared recording is reliable, try recording a large sign
[Microcontroller]
LPC2114 system clock settings
The LPC2114 microcontroller has three system clocks: Fosc is the crystal oscillator or external clock source, Fcclk is the clock supplied to the CPU after adjustment by the PLL, and Fpclk is the clock supplied to peripheral devices after the VPB divider. 1. PLL settings   The input clock frequency range accepted by
[Microcontroller]
STM32 Learning: DMA Detailed Explanation (2)
The DMA part I used is relatively simple. Of course, maybe this is a new thing, and I can't use its complex functions for the time being. The following is a question and answer format to express my thoughts. What is DMA used for?        Direct memory access is used to provide high-speed data transfer between perip
[Microcontroller]
STM32 CAN filter settings
It seems that many people still don't know how to set filters. It took me a long time to learn it. Let's first look at a register    CAN_TIxR (x=0~2) Send mailbox identification register Standard frame is 31~21 bits,    a total of 11 bits STID Extended frame is 20~3 bits,     a total of 29 bits EXID +STID 0~
[Microcontroller]
How to configure STM32 to use SPI communication
SPI is a high-speed, full-duplex, synchronous communication bus with simple principle and use, and occupies few pin resources. It is a commonly used communication method. STM32 usually has 2~3 SPI interfaces According to the STM32 manual: 1. Enable peripheral clock  2. Enable SCK, MOSI, MISO and NSS GPIO clocks  3. Pe
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号