]> Zhao Yanbai Git Server - acecode.git/commitdiff
SysClk手动设置为72MHz,时钟切为外部高速时钟HSE
authorAceVest <zhaoyanbai@126.com>
Sat, 1 Jun 2019 08:03:10 +0000 (16:03 +0800)
committerAceVest <zhaoyanbai@126.com>
Sat, 1 Jun 2019 08:03:10 +0000 (16:03 +0800)
learn/stm32/F103HAL/Src/System.c
learn/stm32/F103HAL/Src/main.c

index 3abf807c20d27ef84fdba215ab8cb50ed55ba53a..ee1d86b7458e5887d46af918fba08c87ec283ba9 100644 (file)
@@ -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();
+       }
+}
index e6d0a59833dd49efbaef3b970ee8951953dc3209..4a05107ff0a890ff31abcbc4221943fd6762c804 100644 (file)
@@ -56,9 +56,7 @@ void SystemClock_Config(void);
 \r
 /* Private user code ---------------------------------------------------------*/\r
 /* USER CODE BEGIN 0 */\r
-void SystemPreInit();\r
-void SystemSetup();\r
-void SystemLoop();\r
+void SystemEntry();\r
 /* USER CODE END 0 */\r
 \r
 /**\r
@@ -68,7 +66,8 @@ void SystemLoop();
 int main(void)\r
 {\r
   /* USER CODE BEGIN 1 */\r
-\r
+       SystemEntry();\r
+       return 0; // never\r
   /* USER CODE END 1 */\r
   \r
 \r
@@ -85,20 +84,19 @@ int main(void)
   SystemClock_Config();\r
 \r
   /* USER CODE BEGIN SysInit */\r
-  SystemPreInit();\r
+\r
   /* USER CODE END SysInit */\r
 \r
   /* Initialize all configured peripherals */\r
   MX_GPIO_Init();\r
   /* USER CODE BEGIN 2 */\r
-  SystemSetup();\r
+\r
   /* USER CODE END 2 */\r
 \r
   /* Infinite loop */\r
   /* USER CODE BEGIN WHILE */\r
   while (1)\r
   {\r
-         SystemLoop();\r
     /* USER CODE END WHILE */\r
 \r
     /* USER CODE BEGIN 3 */\r