From: AceVest Date: Sat, 1 Jun 2019 08:03:10 +0000 (+0800) Subject: SysClk手动设置为72MHz,时钟切为外部高速时钟HSE X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/zlib_tech.html?a=commitdiff_plain;h=fb85e70368a47ebaa1b6bcdf7459d91d929d9bf4;p=acecode.git SysClk手动设置为72MHz,时钟切为外部高速时钟HSE --- diff --git a/learn/stm32/F103HAL/Src/System.c b/learn/stm32/F103HAL/Src/System.c index 3abf807..ee1d86b 100644 --- a/learn/stm32/F103HAL/Src/System.c +++ b/learn/stm32/F103HAL/Src/System.c @@ -9,24 +9,71 @@ #include "stm32f1xx_hal.h" void SystemPreInit() { - __HAL_RCC_GPIOB_CLK_ENABLE(); + } #define LED_Pin GPIO_PIN_6 #define LED_GPIO_Port GPIOB -void SystemSetup() { +void SystemClock_Init() { + RCC_OscInitTypeDef RCC_OscInitStruct; + RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; + RCC_OscInitStruct.HSEState = RCC_HSE_ON; + RCC_OscInitStruct.HSIState = RCC_HSI_ON; + RCC_OscInitStruct.LSEState = RCC_LSE_OFF; + RCC_OscInitStruct.LSIState = RCC_LSI_OFF; + RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6; // 外部晶振是12MHz,所以为了得到72MHz,只需要乘以6 + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; + if(HAL_OK != HAL_RCC_OscConfig(&RCC_OscInitStruct)) { + Error_Handler(); + } + + RCC_ClkInitTypeDef RCC_ClkInitStruct; + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; + RCC_ClkInitStruct.APB1CLKDivider= RCC_HCLK_DIV2; + RCC_ClkInitStruct.APB2CLKDivider= RCC_HCLK_DIV1; + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK1; + if(HAL_OK != HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2)) { + Error_Handler(); + } +} + +void GPIO_Init() { + __HAL_RCC_GPIOB_CLK_ENABLE(); + // GPIO 初始化,需要启用相应GPIO Port的时钟 GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD; GPIO_InitStruct.Pin = LED_Pin; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; - HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct); } + +void SystemSetup() { + + HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_2); + + SystemClock_Init(); + + GPIO_Init(); +} + void SystemLoop() { HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin); HAL_Delay(1000); } + + +void SystemEntry() { + + SystemSetup(); + + while(1) { + SystemLoop(); + } +} diff --git a/learn/stm32/F103HAL/Src/main.c b/learn/stm32/F103HAL/Src/main.c index e6d0a59..4a05107 100644 --- a/learn/stm32/F103HAL/Src/main.c +++ b/learn/stm32/F103HAL/Src/main.c @@ -56,9 +56,7 @@ void SystemClock_Config(void); /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ -void SystemPreInit(); -void SystemSetup(); -void SystemLoop(); +void SystemEntry(); /* USER CODE END 0 */ /** @@ -68,7 +66,8 @@ void SystemLoop(); int main(void) { /* USER CODE BEGIN 1 */ - + SystemEntry(); + return 0; // never /* USER CODE END 1 */ @@ -85,20 +84,19 @@ int main(void) SystemClock_Config(); /* USER CODE BEGIN SysInit */ - SystemPreInit(); + /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); /* USER CODE BEGIN 2 */ - SystemSetup(); + /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { - SystemLoop(); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */