rcc.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /**
  2. * Тактирование STM32
  3. * http://avr-start.ru/?p=3709
  4. */
  5. /* Для внешнего кварца: */
  6. RCC->CR|=RCC_CR_HSEON; //Запускаем генератор HSE.
  7. while (!(RCC->CR & RCC_CR_HSERDY)) {}; // Ждем готовности
  8. RCC->CFGR &=~RCC_CFGR_SW; //Сбрасываем биты
  9. RCC->CFGR |= RCC_CFGR_SW_HSE; // Переходим на HSE
  10. /* Для внутреннего PLL */
  11. RCC->CFGR |= RCC_CFGR_PLLMULL6; //HSI / 2 * 6 = 24 MHz
  12. RCC->CR |= RCC_CR_PLLON; //enable PLL
  13. while((RCC->CR & RCC_CR_PLLRDY) == 0) {} //wait till PLL is ready
  14. RCC->CFGR &= ~RCC_CFGR_SW; //clear SW bits
  15. RCC->CFGR |= RCC_CFGR_SW_PLL; //select PLL as system clock
  16. while((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_1) {} //wait till PLL is used
  17. /* Для внешнего PLL */
  18. RCC->CR |= RCC_CR_HSEON; //enable HSE
  19. while((RCC->CR & RCC_CR_HSERDY) == 0) {} //wait till HSE is ready
  20. RCC->CFGR |= RCC_CFGR_HPRE_DIV1 | //HCLK = SYSCLK
  21. RCC_CFGR_PPRE2_DIV1 | //PCLK2 = HCLK
  22. RCC_CFGR_PPRE1_DIV1; //PCLK1 = HCLK
  23. RCC->CFGR &= ~RCC_CFGR_PLLMULL; //clear PLLMULL bits
  24. RCC->CFGR |= RCC_CFGR_PLLSRC_PREDIV1 | //PLL source = PREDIV1
  25. RCC_CFGR_PLLXTPRE_PREDIV1_Div2 | //HSE / 2 = 4 MHz
  26. RCC_CFGR_PLLMULL6; //4 * 6 = 24 MHz
  27. RCC->CR |= RCC_CR_PLLON; //enable PLL
  28. while((RCC->CR & RCC_CR_PLLRDY) == 0) {} //wait till PLL is ready
  29. RCC->CFGR &= ~RCC_CFGR_SW; //clear SW bits
  30. RCC->CFGR |= RCC_CFGR_SW_PLL; //select PLL as system clock
  31. while((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_1) {} //wait till PLL is used