Guest User

Clock_Setting_Correct

a guest
Sep 8th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.10 KB | None | 0 0
  1. static void SystemClock_Config(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 = 0;
  23.   RCC->PLLCFGR |= 0x00400000;   // PLL source is HSE
  24.   RCC->PLLCFGR |= 0x00000008;   // M = 8
  25.   RCC->PLLCFGR |= 0x00000000 + ((uint32_t)336 << 6);    // N = 336
  26.   RCC->PLLCFGR |= 0x00000000 + (((uint32_t)2 >> 1) - 1);    // P = 2
  27.   RCC->PLLCFGR |= 0x00000000 + ((uint32_t)7 << 24); // Q = 7
  28.  
  29.   RCC->CR |= 0x01000000;    // PLL ON
  30.   while((RCC->CR & 0x02000000) == 0);   // Wait till PLL is ready
  31.  
  32.   /* Enable HSI Oscillator and activate PLL with HSI as source */
  33. //  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  34. //  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  35. //  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  36. //  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  37. //  RCC_OscInitStruct.PLL.PLLM = 8;
  38. //  RCC_OscInitStruct.PLL.PLLN = 336;
  39. //  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  40. //  RCC_OscInitStruct.PLL.PLLQ = 7;
  41. //  HAL_RCC_OscConfig(&RCC_OscInitStruct);
  42.  
  43.  
  44. //  RCC->CFGR |= 0x00000002; // SW = 0b10 = PLL used as system clock
  45. //  while((RCC->CFGR & 0x00000008) == 0);   // Make sure SWS = 0b10 = PLL is really selected
  46.  
  47.   RCC->CFGR |= 0x00000000;  // HPRE = 0 = Div by 1 (no division), AHB = 168MHz
  48.   RCC->CFGR |= 0x00000000 + ((uint32_t)5 << 10); // PPRE1 = 5, APB1 = div by 4 = 42MHz
  49.   RCC->CFGR |= 0x00000000 + ((uint32_t)4 << 13); // PPRE2 = 4, APB2 = div by 2 = 84MHz
  50. //  MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, 2);
  51.   uint32_t temp = RCC->CFGR;
  52.   temp = temp | 0x00000002;
  53.   RCC->CFGR = temp;
  54.   FLASH->ACR |= 0x00000005; // FLASH_LATENCY_5
  55.  
  56.  
  57.   /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
  58. //  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  59. //  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  60. //  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  61. //  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  62. //  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  63. //  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
  64.  
  65.   idNumber = DBGMCU->IDCODE;
  66.   idNumber = idNumber >> 16;
  67.  
  68.   if(idNumber == 0x1001)
  69.   {
  70.       FLASH->ACR |= 0x00000100; // Enable prefetch buffer
  71.   }
  72. //  /* STM32F405x/407x/415x/417x Revision Z devices: prefetch is supported  */
  73. //  if (HAL_GetREVID() == 0x1001)
  74. //  {
  75. //    /* Enable the Flash prefetch */
  76. //    __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
  77. //  }
  78. }
Add Comment
Please, Sign In to add comment