feng wang / mbed-STM32F103C8T6

Dependents:   Bob_Curtain_Sample_0506

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SysClockConf.cpp Source File

SysClockConf.cpp

00001 /*
00002   ******************************************************************************
00003   * @file    SysClockConf.c
00004   * @author  Zoltan Hudak
00005   * @version 
00006   * @date    05-July-2016
00007   * @brief   System Clock configuration for STM32F103C8T6
00008   ******************************************************************************
00009   * @attention
00010   *
00011   * <h2><center>&copy; COPYRIGHT(c) 2016 Zoltan Hudak <hudakz@inbox.com>
00012   *
00013   * All rights reserved.
00014 
00015  This program is free software: you can redistribute it and/or modify
00016  it under the terms of the GNU General Public License as published by
00017  the Free Software Foundation, either version 3 of the License, or
00018  (at your option) any later version.
00019 
00020  This program is distributed in the hope that it will be useful,
00021  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  GNU General Public License for more details.
00024 
00025  You should have received a copy of the GNU General Public License
00026  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00027   */
00028 
00029 #include "SysClockConf.h"
00030 #include "mbed.h"
00031 
00032 bool HSE_SystemClock_Config(void) {
00033     RCC_OscInitTypeDef RCC_OscInitStruct;
00034 
00035     RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
00036     RCC_OscInitStruct.HSEState = RCC_HSE_ON;
00037     RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
00038     RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
00039     RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
00040     RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
00041 
00042     if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
00043         return false;
00044     }
00045 
00046     RCC_ClkInitTypeDef RCC_ClkInitStruct;
00047 
00048     RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
00049                                 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
00050     RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
00051     RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
00052     RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
00053     RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
00054     if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
00055         return false;
00056     }
00057 
00058     RCC_PeriphCLKInitTypeDef PeriphClkInit;
00059 
00060     PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC|RCC_PERIPHCLK_USB;
00061     PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
00062     //PeriphClkInit.UsbClockSelection = RCC_USBPLLCLK_DIV1_5;
00063     if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
00064         return false;
00065     }
00066     return true;
00067 }
00068 
00069 bool confSysClock(void) {
00070     HAL_RCC_DeInit();
00071     if (!HSE_SystemClock_Config()) {
00072         return false;
00073     }
00074     SystemCoreClockUpdate();
00075     return true;
00076 }
00077 
00078 
00079