SystemInit函数并没有进行时钟的配置,因此需要自行编写时钟初始化函数Clock_Init:
void Clock_Init(uint32_t plln, uint32_t pllm, uint32_t pllp, uint32_t pllq)
{
HAL_StatusTypeDef ret = HAL_OK;
RCC_OscInitTypeDef RCC_OscConfig;
RCC_ClkInitTypeDef RCC_ClkConfig;
__HAL_RCC_PWR_CLK_ENABLE(); // Enable clock of power
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscConfig.OscillatorType = RCC_OSCILLATORTYPE_HSE; // Set HSE as clock source
RCC_OscConfig.HSEState = RCC_HSE_ON; // Enable HSE
RCC_OscConfig.PLL.PLLState = RCC_PLL_ON; // Enable PLL
RCC_OscConfig.PLL.PLLSource = RCC_PLLSOURCE_HSE; // Set HSE as source of PLL
RCC_OscConfig.PLL.PLLM = pllm; // Set M as pllm, which in range 2 to 63
RCC_OscConfig.PLL.PLLN = plln; // Set N as plln, which in range 64 to 432
RCC_OscConfig.PLL.PLLP = pllp; // Set P as pllp, which in 2, 4, 6 and 8
RCC_OscConfig.PLL.PLLQ = pllq; // Set Q as pllq, which in range 2 to 15
ret = HAL_RCC_OscConfig(&RCC_OscConfig); // Config RCC
if(ret != HAL_OK) while(1); // Error
// Config PLL as system clock source and config HCLK, PCLK1 and PCLK2
RCC_ClkConfig.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkConfig.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // Config PLL as system clock source
RCC_ClkConfig.AHBCLKDivider = RCC_SYSCLK_DIV1; // AHB Divider 1
RCC_ClkConfig.APB1CLKDivider = RCC_HCLK_DIV4; // APB1 Divider 4
RCC_ClkCOnfig.APB2CLKDivider = RCC_HCLK_DIV2; // APB2 Divider 2
ret=HAL_RCC_ClockConfig(&RCC_ClkInitStructure,FLASH_LATENCY_5); // Config RCC Clock
if(ret != HAL_OK) while(1); // Error
// STM32F405x/407x/415x/417x Z 版本的器件支持预取功能
if (HAL_GetREVID() == 0x1001)
{
__HAL_FLASH_PREFETCH_BUFFER_ENABLE(); //使能 flash 预取
}
}
|