Advertisement
Evgeniy_Yaroshenko

RCC

May 21st, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.72 KB | None | 0 0
  1. #include "RCC.h"
  2. //-----------------------------------------------------------------------------
  3. #define HSI_VALUE           16000000    ///< HSI VALUE [Hz]
  4. //-----------------------------------------------------------------------------
  5. #define HSE_VALUE           8000000 ///< HSE VALUE [Hz]
  6. //-----------------------------------------------------------------------------
  7. #define HSE_STARTUP_TIMEOUT 1000000
  8. //-----------------------------------------------------------------------------
  9. //-----------------------------------------------------------------------------
  10. #define PLL_M               4           ///< PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
  11. //-----------------------------------------------------------------------------
  12. #define PLL_N               84
  13. //-----------------------------------------------------------------------------
  14. #define PLL_P               4           ///< SYSCLK = PLL_VCO / PLL_P
  15. //-----------------------------------------------------------------------------
  16.  
  17. clockState_type clock_init(void){
  18.     /******************************************************************************/
  19.     /*            PLL (clocked by HSE) used as System clock source                */
  20.     /******************************************************************************/
  21.     __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  22.     clockState_type state;
  23.  
  24.     /* Enable HSE */
  25.     RCC->CR |= ((uint32_t) RCC_CR_HSEON);
  26.  
  27.     /* Wait till HSE is ready and if Time out is reached exit */
  28.     do{
  29.         HSEStatus = RCC->CR & RCC_CR_HSERDY;
  30.         StartUpCounter++;
  31.     }while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  32.  
  33.     if((RCC->CR & RCC_CR_HSERDY) != RESET){
  34.         HSEStatus = (uint32_t) 0x01;
  35.     }else{
  36.         HSEStatus = (uint32_t) 0x00;
  37.     }
  38.  
  39.     if(HSEStatus == (uint32_t) 0x01){
  40.         /* Select regulator voltage output Scale 1 mode, System frequency up to 168 MHz */
  41.         RCC->APB1ENR |= RCC_APB1ENR_PWREN;
  42.         PWR->CR |= PWR_CR_VOS;
  43.  
  44.         /* HCLK = SYSCLK / 1*/
  45.         RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
  46.  
  47.         /* PCLK2 = HCLK / 4*/
  48.         RCC->CFGR |= RCC_CFGR_PPRE2_DIV4;
  49.  
  50.         /* PCLK1 = HCLK / 4*/
  51.         RCC->CFGR |= RCC_CFGR_PPRE1_DIV4;
  52.  
  53.         /* Configure the main PLL */
  54.         RCC->PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P >> 1) - 1) << 16) | (RCC_PLLCFGR_PLLSRC_HSE);
  55.  
  56.         /* Enable the main PLL */
  57.         RCC->CR |= RCC_CR_PLLON;
  58.  
  59.         /* Wait till the main PLL is ready */
  60.         while((RCC->CR & RCC_CR_PLLRDY) == 0){
  61.         }
  62.  
  63.         /* Configure Flash prefetch, Instruction cache, Data cache and wait state */
  64.         FLASH->ACR = FLASH_ACR_ICEN | FLASH_ACR_DCEN | FLASH_ACR_LATENCY_5WS;
  65.  
  66.         /* Select the main PLL as system clock source */
  67.         //RCC->CFGR &= (uint32_t) ((uint32_t) ~(RCC_CFGR_SW));
  68.         RCC->CFGR |= RCC_CFGR_SW_PLL;
  69.  
  70.         /* Wait till the main PLL is used as system clock source */
  71.         while((RCC->CFGR & (uint32_t) RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL){
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement