Stm32 clock Configuration

Source: Internet
Author: User
Tags prefetch

In stm32, there are five clock sources:HSI,HSE,LSI,LSE,PLL.

HSIIt is a high-speed internal clock, RC oscillator, and the frequency is 8 MHz.

HSEIt is a high-speed external clock, which can be connected to a Z/Ceramic Resonator or an external clock source. The frequency range is 4 MHz ~ 16 MHz.

LSIIt is a low-speed internal clock, RC oscillator, and the frequency is 40 kHz.

LSEIt is a Z crystal with a low-speed external clock and a connection frequency of 32.768khz.

PLLThe clock input source can be HSI/2, HSE or HSE/2. The multiplier value can be 2 ~ 16 times, but its output frequency cannot exceed 72 MHz.

 

InStm32If the external crystal oscillator is not used,Osc_inAndOsc_outMethod

If you use an internal RC oscillator instead of an external crystal oscillator, follow these steps:

1) for products with 100 or 144 feet, osc_in should be grounded and osc_out should be suspended. 2) for products with less than 100 feet, there are two methods: 2.1) osc_in and osc_out are grounded by a 10 k resistor respectively. This method improves EMC performance. 2.2) remap osc_in and osc_out to pd0 and pd1 respectively, and configure pd0 and pd1 as the push-pull output and output '0 '.

Use HSE clock,ProgramSet the clock parameters:

1. Reset the RCC register to the default rcc_deinit;

2. Enable the external high-speed clock crystal oscillator HSE rcc_hseconfig (rcc_hse_on );

3. Wait for the external high-speed clock crystal oscillator to work hsestartupstatus = rcc_waitforhsestartup ();

4. Set the AHB clock rcc_hclkconfig;

5. Set the high-speed AHB clock rcc_pclk2config;

6. Set the low-speed AHB clock rcc_pclk1config;

7. Set PLL rcc_pllconfig;

8. Enable PLL rcc_pllcmd (enable );

9. Wait for the PLL to work while (rcc_getflagstatus (rcc_flag_pllrdy) = reset)

10. Set the system clock rcc_sysclkconfig;

11. Determine if the PLL is a system clock while (rcc_getsysclksource ()! = 0x08)

12. Enable the peripheral clock rcc_apb2periphclockcmd ()/rcc_apb1periphclockcmd ()

 

 

The following is the RCC configuration function in the tm32 software Firmware Library (using an external 8 MHz Crystal Oscillator)

  /* **************************************** * ************************************ Function name: rcc_configuration * Description: RCC configuration (using an external 8 MHz Crystal Oscillator) * input: none * output: none * return: no *************************************** ***************************************  */  Void Rcc_configuration ( Void  ){  /*  Reset the peripheral RCC register to the default value  */  Rcc_deinit ();  /*  Set external high-speed crystal oscillator (HSE)  */ Rcc_hseconfig (rcc_hse_on );  //  RCC_HSE_ON--HSE crystal open (on)     /*  Wait for HSE to start  */  Hsestartupstatus = Rcc_waitforhsestartup ();  If (Hsestartupstatus = success) //  Success: HSE crystal oscillator stable and ready  {  /*  Set the AHB clock (hclk)  */ Rcc_hclkconfig (rcc_sysclk_div1 );  //  Rcc_sysclk_div1 -- AHB clock = system clock       /*  Set high-speed AHB clock (pclk2)  */  Rcc_pclk2config (rcc_hclk_div1 );  //  Rcc_hclk_div1 -- apb2 clock = hclk       /*  Set low-speed AHB clock (pclk1)  */  Rcc_pclk1config (rcc_hclk_div2 );  // Rcc_hclk_div2 -- apb1 clock = hclk/2      /*  Set the delay time period of flash memory  */  Flash_setlatency (flash_latency_2 );  //  Flash_latency_2 delay period    /*  Select the cache mode for flash prefetch.  */  Flash_prefetchbuffercmd (flash_prefetchbuffer_enable );  //  Prefetch refers to cache enabling       /*  Set the PLL clock source and frequency doubling factor */  Rcc_pllconfig (rcc_pllsource_hse_div1, rcc_pllmul_9 );  //  PLL input clock = HSE clock frequency; rcc_pllmul_9 -- PLL input clock x 9    /*  Enable PLL  */  Rcc_pllcmd (enable );  /*  Check whether the specified RCC flag (PLL ready flag) is set.  */        While (Rcc_getflagstatus (rcc_flag_pllrdy) = Reset ){}  /* Set the system clock (sysclk)  */  Rcc_sysclkconfig (rcc_sysclksource_pllclk );  //  Rcc_sysclksource_pllclk -- select PLL as the system clock      /*  The PLL returns the clock source used as the system clock.  */      While (Rcc_getsysclksource ()! = 0x08 ) //  0x08: PLL as the system clock  {}}  /* Enable or disable apb2 peripheral clock  */  Rcc_apb2periphclockcmd (rcc_apb2periph_gpioa | Rcc_apb2periph_gpiob | Rcc_apb2periph_gpioc, enable );  //  Rcc_apb2periph_gpioa gpioa clock  //  Rcc_apb2periph_gpiob gpiob clock  //  Rcc_apb2periph_gpioc gpioc clock  //  Rcc_apb2periph_gpiod gpiod clock  } 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.