Guest User

Clock_Setting

a guest
Sep 8th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.97 KB | None | 0 0
  1. static void SystemClock_Config1(void)
  2. {
  3. //  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  4. //  RCC_OscInitTypeDef RCC_OscInitStruct;
  5.  
  6.   /* Enable Power Control clock */
  7.   //__HAL_RCC_PWR_CLK_ENABLE();
  8.   RCC->APB1ENR |= 0x10000000;   // Enable PWREN bit (page - 183 of RM)
  9.  
  10.  
  11.   /* The voltage scaling allows optimizing the power consumption when the device is
  12.      clocked below the maximum system frequency, to update the voltage scaling value
  13.      regarding system frequency refer to product datasheet.  */
  14.   //__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  15.   PWR->CR |= 0x00004000;    //VOS bit = 01 (page - 145 or RM)
  16.  
  17.   // Page - 216 of RM
  18.   RCC->CR |= 0x00010000;    // HSE ON
  19.   while((RCC->CR & 0x00020000) == 0);   // Wait till HSE is ready
  20.  
  21.   // Page - 226 of RM
  22.   RCC->PLLCFGR |= 0x00400000;   // PLL source is HSE
  23.   RCC->PLLCFGR |= 0x00000008;   // M = 8
  24.   RCC->PLLCFGR |= 0x00000000 + ((uint32_t)336 << 6);    // N = 336
  25.   RCC->PLLCFGR |= 0x00000000 + (((uint32_t)2 >> 1) - 1);    // P = 2
  26.   RCC->PLLCFGR |= 0x00000000 + ((uint32_t)7 << 24); // Q = 7
  27.  
  28.   RCC->CR |= 0x01000000;    // PLL ON
  29.   while((RCC->CR & 0x02000000) == 0);   // Wait till PLL is ready
  30.  
  31. //  /* Enable HSI Oscillator and activate PLL with HSI as source */
  32. //  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  33. //  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  34. //  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  35. //  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  36. //  RCC_OscInitStruct.PLL.PLLM = 8;
  37. //  RCC_OscInitStruct.PLL.PLLN = 336;
  38. //  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  39. //  RCC_OscInitStruct.PLL.PLLQ = 7;
  40. //  HAL_RCC_OscConfig(&RCC_OscInitStruct);
  41.  
  42.  
  43.   RCC->CFGR |= 0x00000002; // SW = 0b10 = PLL used as system clock
  44. //  while((RCC->CFGR & 0x00000008) == 0);   // Make sure SWS = 0b10 = PLL is really selected
  45.   RCC->CFGR |= 0x00000000;  // HPRE = 0 = Div by 1 (no division), AHB = 168MHz
  46.   RCC->CFGR |= 0x00000000 + ((uint32_t)5 << 10); // PPRE1 = 5, APB1 = div by 4 = 42MHz
  47.   RCC->CFGR |= 0x00000000 + ((uint32_t)4 << 13); // PPRE2 = 4, APB2 = div by 2 = 84MHz
  48.  
  49.   FLASH->ACR |= 0x00000005; // FLASH_LATENCY_5
  50.  
  51.  
  52. //  /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
  53. //  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  54. //  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  55. //  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  56. //  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  57. //  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  58. //  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
  59.  
  60.   idNumber = DBGMCU->IDCODE;
  61.   idNumber = idNumber >> 16;
  62.  
  63.   if(idNumber == 0x1001)
  64.   {
  65.       FLASH->ACR |= 0x00000100; // Enable prefetch buffer
  66.   }
  67. //  /* STM32F405x/407x/415x/417x Revision Z devices: prefetch is supported  */
  68. //  if (HAL_GetREVID() == 0x1001)
  69. //  {
  70. //    /* Enable the Flash prefetch */
  71. //    __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
  72. //  }
  73. }
Add Comment
Please, Sign In to add comment