STM32 standard peripheral library function SetSysClockTo72(void)

Publisher:binggegeLatest update time:2018-12-21 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The article http://blog.csdn.net/qq_29344757/article/details/73479924 introduces the clock system of STM32. Now, taking the SetSysClockTo72() function of the STM32 standard peripheral library as an example, we will introduce the programming of RCC.


With the foundation of the previous article, learning RCC is no longer so difficult and boring, at least I think so. The SetSysClockTo72(void) function is the default system clock setting function when we use the peripheral library. 


Write the picture description here


As marked in the figure above, the core function of this function is to set these 5 points. 


(1) Set HCLK, HCLK = SYSCLK 


(2) Set PCLK2, PCLK2 = HCLK 


(3) Set PCLK1, PCLK1 = HCLK / 2 


(4) Set the PLL clock source and PLL multiplication factor 


(5) Select PLL as the system clock source, that is, PLLCLK = SYSCLK 


In general, the system uses the HSE clock source, and then HSE is multiplied by PLL as the system clock. The usual configuration is HSE = 8M, and the multiplication factor of PLL is 9, then the system clock SYSCLK = 8M * 9 = 72MHz, from which it can be deduced that HCLK = PCLK2 = 72MHz, PCLK1 = 36MHz. 


PCLK2 is a high-speed bus clock. The high-speed peripherals on the chip are mounted on this bus, such as all GPIO, SPI1, USART1, etc.: 


Write the picture description here


The following code is extracted from the standard peripheral library file system_stm32f10x.c, and the interconnection-related code is deleted. This function directly operates the register. For register operations, please refer to the relevant chapters of "STM32 Chinese Reference Manual_V10.pdf". 


The program flow is: 


(1) Enable HSE and wait for HSE to stabilize 


(2) Set the APB2, APB1, and AHB frequency division coefficients 


(3) Set the PLL clock source and PLL multiplication factor 


(4) Turn on PLL and wait for PLL to stabilize 


(5) Read the clock switching status to ensure that PLLCLK is selected as the system clock


static void SetSysClockTo72(void)

{

    __IO uint32_t StartUpCounter = 0, HSEStatus = 0;


    //1. Enable HSE

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


    //Wait for HSE to stabilize, StartUpCounter is used to judge the timeout processing flag

    do

    {

        HSEStatus = RCC->CR & RCC_CR_HSERDY;

        StartUpCounter++;  

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


    //If HSE is stable, HSEStatus = 0x01

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

    {

        HSEStatus = (uint32_t)0x01;

    }

    else //HSE has timed out and is not stable yet

    {

        HSEStatus = (uint32_t)0x00;

    }  


    //2. HSE started successfully

    if (HSEStatus == (uint32_t)0x01)

    {

        // Enable flash pre-storage buffer, flash settings related

        FLASH->ACR |= FLASH_ACR_PRFTBE; 

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

        FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;    


        //Undivided SYSCLK, HCLK = SYSCLK

        RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;


        //Undivided HCLK, PCLK2 = HCLK

        RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;


        //2-divided HCLK, PCLK1 = HCLK / 2

        RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;


        //3. Set the PLL clock source and PLL multiplication factor to 9, that is, PLLCLK is equal to 8M * 9 = 72M

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

                                            RCC_CFGR_PLLMULL));

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


        //4. Enable PLL

        RCC->CR |= RCC_CR_PLLON;


        //Wait for PLL to stabilize

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


        //5. Select PLL as the system clock source

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

        RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;    


        //Read the clock switching status success flag

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

    }

    else

    {

        //HSE startup failed, the user can program a prompt

    }

}


Keywords:STM32 Reference address:STM32 standard peripheral library function SetSysClockTo72(void)

Previous article:STM32 serial USART communication
Next article:STM32 GPIO internal structure and related registers

Recommended ReadingLatest update time:2024-11-16 13:48

STM32 clock selection
The system clock selection is performed at startup. At reset, the internal 8MHz RC oscillator is selected as the default CPU clock. Select an external 4~16MHz clock with failure monitoring; when the external clock fails, it will be isolated and a corresponding Likewise, full interrupt management of the PLL clock can
[Microcontroller]
About STM32 serial port printf output debugging information problem
1. Problems encountered (using HAL library) In the process of using STM32, we usually use printf to redirect the serial port to output debugging information for program development and debugging. We found the redirection code part on the Internet and added it to the serial port code file, as follows: UART_HandleTy
[Microcontroller]
Solve STM32 HardFault_Handler error logging
The most common reasons for HardFault_Handler errors are array out of bounds and pointer flying. In fact, these two are similar. They all cause errors by accessing places that should not be accessed. It's like a person with poor direction, walking aimlessly to a strange place... and then getting lost... and then nothi
[Microcontroller]
Solve STM32 HardFault_Handler error logging
stm32-led-serial-port-PWM
Because of the project needs, I learned about the GPIO, serial port, PWM, and interrupt parts of stm32. Here is a summary for us to learn together. All programs have been tested in practice and the output is correct. Paste the configuration program of GPIO, serial port, PWM (timer) as follows 1. Enable the periphe
[Microcontroller]
STM32 programming uses TIM timer input capture function infrared remote control decoding
1. Main program #include "sys.h" #include "usart.h" #include "led.h" #include "lcd.h" #include "delay.h" #include "remote.h" #include "stm32f10x_tim.h" /* Program function: Use the timer input capture function of STM32 for infrared decoding.          When the key of the infrared remote control is pressed,
[Microcontroller]
STM32 timer time calculation method
    The timer in STM32 has many uses:     1. The system clock (SysTick)     setting is very simple. The following is the setting for generating a 1ms interrupt and a function for generating a 10ms delay:     void RCC_Configuration(void)     {     RCC_ClocksTypeDef RCC_ClockFreq;     SystemInit();/Derived from th
[Microcontroller]
STM32 study notes: standby wake-up
1. Brief Introduction 1.1 Low power mode: After a system or power reset, the microcontroller is in the running state. When the CPU does not need to continue running, you can use a variety of low-power modes to save power, such as: waiting for an external event, the common key wake-up. The user needs to select the b
[Microcontroller]
STM32 study notes: standby wake-up
Wireless fire detection and alarm system based on information fusion technology
    The application of fire has played an important role in the development of human civilization, and fire has always threatened human life and caused significant losses of life and property. Especially in recent years, with the rapid development of the national economy, the further improvement of urbanization, the in
[Microcontroller]
Wireless fire detection and alarm system based on information fusion technology
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号