Check program for STM32F303K8 System Clock

Dependencies:   mbed

Fork of study_step0 by Team_PjL

See
https://os.mbed.com/users/kenjiArai/notebook/nucleo-f303k8-hse-clock/#

Committer:
kenjiArai
Date:
Sat Sep 30 09:14:54 2017 +0000
Revision:
1:e162361e197f
Child:
2:68db9770a517
Check program for STM32F303K8 System Clock

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 1:e162361e197f 1 /* mbed Microcontroller Library
kenjiArai 1:e162361e197f 2 * Copyright (c) 2006-2017 ARM Limited
kenjiArai 1:e162361e197f 3 *
kenjiArai 1:e162361e197f 4 * Licensed under the Apache License, Version 2.0 (the "License");
kenjiArai 1:e162361e197f 5 * you may not use this file except in compliance with the License.
kenjiArai 1:e162361e197f 6 * You may obtain a copy of the License at
kenjiArai 1:e162361e197f 7 *
kenjiArai 1:e162361e197f 8 * http://www.apache.org/licenses/LICENSE-2.0
kenjiArai 1:e162361e197f 9 *
kenjiArai 1:e162361e197f 10 * Unless required by applicable law or agreed to in writing, software
kenjiArai 1:e162361e197f 11 * distributed under the License is distributed on an "AS IS" BASIS,
kenjiArai 1:e162361e197f 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kenjiArai 1:e162361e197f 13 * See the License for the specific language governing permissions and
kenjiArai 1:e162361e197f 14 * limitations under the License.
kenjiArai 1:e162361e197f 15 */
kenjiArai 1:e162361e197f 16
kenjiArai 1:e162361e197f 17 /**
kenjiArai 1:e162361e197f 18 * This file configures the system clock as follows:
kenjiArai 1:e162361e197f 19 *-----------------------------------------------------------------------------
kenjiArai 1:e162361e197f 20 * System clock source | 1- USE_PLL_HSE_EXTC | 3- USE_PLL_HSI
kenjiArai 1:e162361e197f 21 * | (external 8 MHz clock) | (internal 8 MHz)
kenjiArai 1:e162361e197f 22 * | 2- USE_PLL_HSE_XTAL |
kenjiArai 1:e162361e197f 23 * | (external 8 MHz xtal) |
kenjiArai 1:e162361e197f 24 *-----------------------------------------------------------------------------
kenjiArai 1:e162361e197f 25 * SYSCLK(MHz) | 72 | 64
kenjiArai 1:e162361e197f 26 *-----------------------------------------------------------------------------
kenjiArai 1:e162361e197f 27 * AHBCLK (MHz) | 72 | 64
kenjiArai 1:e162361e197f 28 *-----------------------------------------------------------------------------
kenjiArai 1:e162361e197f 29 * APB1CLK (MHz) | 36 | 32
kenjiArai 1:e162361e197f 30 *-----------------------------------------------------------------------------
kenjiArai 1:e162361e197f 31 * APB2CLK (MHz) | 72 | 64
kenjiArai 1:e162361e197f 32 *-----------------------------------------------------------------------------
kenjiArai 1:e162361e197f 33 * USB capable | NO | NO
kenjiArai 1:e162361e197f 34 *-----------------------------------------------------------------------------
kenjiArai 1:e162361e197f 35 */
kenjiArai 1:e162361e197f 36
kenjiArai 1:e162361e197f 37 // Original source file -> system_clock.c
kenjiArai 1:e162361e197f 38 // \targets\TARGET_STM\TARGET_STM32F3\TARGET_STM32F303x8\TARGET_NUCLEO_F303K8
kenjiArai 1:e162361e197f 39 // and
kenjiArai 1:e162361e197f 40 // Original source file -> stm32f3xx_hal_rcc.c
kenjiArai 1:e162361e197f 41 // \targets\TARGET_STM\TARGET_STM32F3\device\
kenjiArai 1:e162361e197f 42
kenjiArai 1:e162361e197f 43 // Modified by JH1PJL 2017-9-30
kenjiArai 1:e162361e197f 44
kenjiArai 1:e162361e197f 45 #include "mbed.h"
kenjiArai 1:e162361e197f 46 #include "stm32f3xx.h"
kenjiArai 1:e162361e197f 47 #include "mbed_assert.h"
kenjiArai 1:e162361e197f 48
kenjiArai 1:e162361e197f 49 extern Serial pc;
kenjiArai 1:e162361e197f 50 extern void cpu_freq();
kenjiArai 1:e162361e197f 51
kenjiArai 1:e162361e197f 52 //#define DEBUG
kenjiArai 1:e162361e197f 53
kenjiArai 1:e162361e197f 54 #ifdef DEBUG
kenjiArai 1:e162361e197f 55 #define DBG(...) pc.printf(__VA_ARGS__)
kenjiArai 1:e162361e197f 56 #else
kenjiArai 1:e162361e197f 57 #define DBG(...) {;}
kenjiArai 1:e162361e197f 58 #endif
kenjiArai 1:e162361e197f 59
kenjiArai 1:e162361e197f 60 uint8_t SetSysClock_PLL_HSE(uint8_t bypass);
kenjiArai 1:e162361e197f 61 uint8_t SetSysClock_PLL_HSI(void);
kenjiArai 1:e162361e197f 62 HAL_StatusTypeDef HAL_RCC_OscConfig_modify(RCC_OscInitTypeDef *);
kenjiArai 1:e162361e197f 63
kenjiArai 1:e162361e197f 64 /**
kenjiArai 1:e162361e197f 65 * @brief Configures the System clock source,
kenjiArai 1:e162361e197f 66 * PLL Multiplier and Divider factors,
kenjiArai 1:e162361e197f 67 * AHB/APBx prescalers and Flash settings
kenjiArai 1:e162361e197f 68 * @note This function should be called only once the RCC clock configuration
kenjiArai 1:e162361e197f 69 * is reset to the default reset state (done in SystemInit() function).
kenjiArai 1:e162361e197f 70 * @param None
kenjiArai 1:e162361e197f 71 * @retval None
kenjiArai 1:e162361e197f 72 */
kenjiArai 1:e162361e197f 73 void SetSysClock_HSE_none_Xtal(void)
kenjiArai 1:e162361e197f 74 {
kenjiArai 1:e162361e197f 75 /* 1- Try to start with HSE and external clock */
kenjiArai 1:e162361e197f 76 if (SetSysClock_PLL_HSE(1) == 0)
kenjiArai 1:e162361e197f 77 {
kenjiArai 1:e162361e197f 78 /* 2- If fail start with HSI clock */
kenjiArai 1:e162361e197f 79 SetSysClock_PLL_HSI();
kenjiArai 1:e162361e197f 80 }
kenjiArai 1:e162361e197f 81 }
kenjiArai 1:e162361e197f 82
kenjiArai 1:e162361e197f 83 /******************************************************************************/
kenjiArai 1:e162361e197f 84 /* PLL (clocked by HSE) used as System clock source */
kenjiArai 1:e162361e197f 85 /******************************************************************************/
kenjiArai 1:e162361e197f 86 uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
kenjiArai 1:e162361e197f 87 {
kenjiArai 1:e162361e197f 88 RCC_ClkInitTypeDef RCC_ClkInitStruct;
kenjiArai 1:e162361e197f 89 RCC_OscInitTypeDef RCC_OscInitStruct;
kenjiArai 1:e162361e197f 90
kenjiArai 1:e162361e197f 91 DBG("%u:\r\n", __LINE__);
kenjiArai 1:e162361e197f 92 /* Enable HSE oscillator and activate PLL with HSE as source */
kenjiArai 1:e162361e197f 93 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
kenjiArai 1:e162361e197f 94 if (bypass == 0) {
kenjiArai 1:e162361e197f 95 /* External 8 MHz xtal on OSC_IN/OSC_OUT */
kenjiArai 1:e162361e197f 96 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
kenjiArai 1:e162361e197f 97 DBG("%u:\r\n", __LINE__);
kenjiArai 1:e162361e197f 98 } else {
kenjiArai 1:e162361e197f 99 /* External 8 MHz clock on OSC_IN */
kenjiArai 1:e162361e197f 100 RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
kenjiArai 1:e162361e197f 101 DBG("%u:\r\n", __LINE__);
kenjiArai 1:e162361e197f 102 }
kenjiArai 1:e162361e197f 103 RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
kenjiArai 1:e162361e197f 104 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
kenjiArai 1:e162361e197f 105 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
kenjiArai 1:e162361e197f 106 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; // 72 MHz (8 MHz * 9)
kenjiArai 1:e162361e197f 107 DBG("%u:\r\n", __LINE__);
kenjiArai 1:e162361e197f 108 #ifndef DEBUG
kenjiArai 1:e162361e197f 109 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
kenjiArai 1:e162361e197f 110 #else
kenjiArai 1:e162361e197f 111 if (HAL_RCC_OscConfig_modify(&RCC_OscInitStruct) != HAL_OK) {
kenjiArai 1:e162361e197f 112 #endif
kenjiArai 1:e162361e197f 113 DBG("%u:\r\n", __LINE__);
kenjiArai 1:e162361e197f 114 return 0; // FAIL
kenjiArai 1:e162361e197f 115 }
kenjiArai 1:e162361e197f 116
kenjiArai 1:e162361e197f 117 /* Select PLL as system clock source and
kenjiArai 1:e162361e197f 118 configure the HCLK, PCLK1 and PCLK2 clocks dividers */
kenjiArai 1:e162361e197f 119 RCC_ClkInitStruct.ClockType =
kenjiArai 1:e162361e197f 120 (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK
kenjiArai 1:e162361e197f 121 | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
kenjiArai 1:e162361e197f 122 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 72 MHz
kenjiArai 1:e162361e197f 123 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 72 MHz
kenjiArai 1:e162361e197f 124 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; // 36 MHz
kenjiArai 1:e162361e197f 125 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 72 MHz
kenjiArai 1:e162361e197f 126 DBG("%u:\r\n", __LINE__);
kenjiArai 1:e162361e197f 127 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
kenjiArai 1:e162361e197f 128 DBG("%u:\r\n", __LINE__);
kenjiArai 1:e162361e197f 129 return 0; // FAIL
kenjiArai 1:e162361e197f 130 }
kenjiArai 1:e162361e197f 131 return 1; // OK
kenjiArai 1:e162361e197f 132 }
kenjiArai 1:e162361e197f 133
kenjiArai 1:e162361e197f 134 /******************************************************************************/
kenjiArai 1:e162361e197f 135 /* Configuration PLL (clocked by HSE) used as System clock source */
kenjiArai 1:e162361e197f 136 /******************************************************************************/
kenjiArai 1:e162361e197f 137 HAL_StatusTypeDef
kenjiArai 1:e162361e197f 138 HAL_RCC_OscConfig_modify(RCC_OscInitTypeDef *RCC_OscInitStruct)
kenjiArai 1:e162361e197f 139 {
kenjiArai 1:e162361e197f 140 uint32_t tickstart = 0U;
kenjiArai 1:e162361e197f 141
kenjiArai 1:e162361e197f 142 /* Check the parameters */
kenjiArai 1:e162361e197f 143 assert_param(RCC_OscInitStruct != NULL);
kenjiArai 1:e162361e197f 144 assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
kenjiArai 1:e162361e197f 145 DBG("%u:\r\n", __LINE__);
kenjiArai 1:e162361e197f 146
kenjiArai 1:e162361e197f 147 /*------------------------------- HSE Configuration ------------------------*/
kenjiArai 1:e162361e197f 148 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE)
kenjiArai 1:e162361e197f 149 == RCC_OSCILLATORTYPE_HSE)
kenjiArai 1:e162361e197f 150 {
kenjiArai 1:e162361e197f 151 DBG("%u:\r\n", __LINE__);
kenjiArai 1:e162361e197f 152 /* Check the parameters */
kenjiArai 1:e162361e197f 153 assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
kenjiArai 1:e162361e197f 154
kenjiArai 1:e162361e197f 155 /* When the HSE is used as system clock or clock source for PLL
kenjiArai 1:e162361e197f 156 in these cases it is not allowed to be disabled */
kenjiArai 1:e162361e197f 157 if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSE)
kenjiArai 1:e162361e197f 158 || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK)
kenjiArai 1:e162361e197f 159 && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSE)))
kenjiArai 1:e162361e197f 160 {
kenjiArai 1:e162361e197f 161 DBG("%u:\r\n", __LINE__);
kenjiArai 1:e162361e197f 162 if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)
kenjiArai 1:e162361e197f 163 && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
kenjiArai 1:e162361e197f 164 {
kenjiArai 1:e162361e197f 165 DBG("%u:\r\n", __LINE__);
kenjiArai 1:e162361e197f 166 return HAL_ERROR;
kenjiArai 1:e162361e197f 167 }
kenjiArai 1:e162361e197f 168 }
kenjiArai 1:e162361e197f 169 else
kenjiArai 1:e162361e197f 170 {
kenjiArai 1:e162361e197f 171 DBG("%u:\r\n", __LINE__);
kenjiArai 1:e162361e197f 172 /* Set the new HSE configuration ---------------------------------------*/
kenjiArai 1:e162361e197f 173 __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
kenjiArai 1:e162361e197f 174
kenjiArai 1:e162361e197f 175 #if defined(RCC_CFGR_PLLSRC_HSI_DIV2)
kenjiArai 1:e162361e197f 176 DBG("%u:\r\n", __LINE__);
kenjiArai 1:e162361e197f 177 /* Configure the HSE predivision factor --------------------------------*/
kenjiArai 1:e162361e197f 178 __HAL_RCC_HSE_PREDIV_CONFIG(RCC_OscInitStruct->HSEPredivValue);
kenjiArai 1:e162361e197f 179 #endif /* RCC_CFGR_PLLSRC_HSI_DIV2 */
kenjiArai 1:e162361e197f 180
kenjiArai 1:e162361e197f 181 /* Check the HSE State */
kenjiArai 1:e162361e197f 182 if(RCC_OscInitStruct->HSEState != RCC_HSE_OFF)
kenjiArai 1:e162361e197f 183 {
kenjiArai 1:e162361e197f 184 DBG("%u:\r\n", __LINE__);
kenjiArai 1:e162361e197f 185 // RCC->CR != 0x01000000;
kenjiArai 1:e162361e197f 186 // DBG("%u: RCC->CR=0x%x\r\n", __LINE__, RCC->CR);
kenjiArai 1:e162361e197f 187 // DBG("%u: RCC->CR=0x%x\r\n", __LINE__, RCC->CR);
kenjiArai 1:e162361e197f 188 /* Get Start Tick */
kenjiArai 1:e162361e197f 189 tickstart = HAL_GetTick();
kenjiArai 1:e162361e197f 190
kenjiArai 1:e162361e197f 191 /* Wait till HSE is ready */
kenjiArai 1:e162361e197f 192 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
kenjiArai 1:e162361e197f 193 {
kenjiArai 1:e162361e197f 194 if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
kenjiArai 1:e162361e197f 195 {
kenjiArai 1:e162361e197f 196 DBG("%u:\r\n", __LINE__);
kenjiArai 1:e162361e197f 197 return HAL_TIMEOUT;
kenjiArai 1:e162361e197f 198 }
kenjiArai 1:e162361e197f 199 }
kenjiArai 1:e162361e197f 200 // DBG("%u: RCC->CR=0x%x\r\n", __LINE__, RCC->CR);
kenjiArai 1:e162361e197f 201 }
kenjiArai 1:e162361e197f 202 else
kenjiArai 1:e162361e197f 203 {
kenjiArai 1:e162361e197f 204 DBG("%u:\r\n", __LINE__);
kenjiArai 1:e162361e197f 205 /* Get Start Tick */
kenjiArai 1:e162361e197f 206 tickstart = HAL_GetTick();
kenjiArai 1:e162361e197f 207
kenjiArai 1:e162361e197f 208 /* Wait till HSE is disabled */
kenjiArai 1:e162361e197f 209 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)
kenjiArai 1:e162361e197f 210 {
kenjiArai 1:e162361e197f 211 if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
kenjiArai 1:e162361e197f 212 {
kenjiArai 1:e162361e197f 213 DBG("%u:\r\n", __LINE__);
kenjiArai 1:e162361e197f 214 return HAL_TIMEOUT;
kenjiArai 1:e162361e197f 215 }
kenjiArai 1:e162361e197f 216 }
kenjiArai 1:e162361e197f 217 }
kenjiArai 1:e162361e197f 218 }
kenjiArai 1:e162361e197f 219 }
kenjiArai 1:e162361e197f 220 /*-------------------------------- PLL Configuration -----------------------*/
kenjiArai 1:e162361e197f 221 /* Check the parameters */
kenjiArai 1:e162361e197f 222 assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
kenjiArai 1:e162361e197f 223 if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE)
kenjiArai 1:e162361e197f 224 {
kenjiArai 1:e162361e197f 225 DBG("%u:\r\n", __LINE__);
kenjiArai 1:e162361e197f 226 /* Check if the PLL is used as system clock or not */
kenjiArai 1:e162361e197f 227 if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
kenjiArai 1:e162361e197f 228 {
kenjiArai 1:e162361e197f 229 DBG("%u:\r\n", __LINE__);
kenjiArai 1:e162361e197f 230 if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON)
kenjiArai 1:e162361e197f 231 {
kenjiArai 1:e162361e197f 232 DBG("%u:\r\n", __LINE__);
kenjiArai 1:e162361e197f 233 /* Check the parameters */
kenjiArai 1:e162361e197f 234 assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
kenjiArai 1:e162361e197f 235 assert_param(IS_RCC_PLL_MUL(RCC_OscInitStruct->PLL.PLLMUL));
kenjiArai 1:e162361e197f 236 #if defined(RCC_CFGR_PLLSRC_HSI_PREDIV)
kenjiArai 1:e162361e197f 237 assert_param(IS_RCC_PREDIV(RCC_OscInitStruct->PLL.PREDIV));
kenjiArai 1:e162361e197f 238 #endif
kenjiArai 1:e162361e197f 239
kenjiArai 1:e162361e197f 240 /* Disable the main PLL. */
kenjiArai 1:e162361e197f 241 __HAL_RCC_PLL_DISABLE();
kenjiArai 1:e162361e197f 242
kenjiArai 1:e162361e197f 243 /* Get Start Tick */
kenjiArai 1:e162361e197f 244 tickstart = HAL_GetTick();
kenjiArai 1:e162361e197f 245
kenjiArai 1:e162361e197f 246 /* Wait till PLL is disabled */
kenjiArai 1:e162361e197f 247 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
kenjiArai 1:e162361e197f 248 {
kenjiArai 1:e162361e197f 249 DBG("%u:\r\n", __LINE__);
kenjiArai 1:e162361e197f 250 if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
kenjiArai 1:e162361e197f 251 {
kenjiArai 1:e162361e197f 252 DBG("%u:\r\n", __LINE__);
kenjiArai 1:e162361e197f 253 return HAL_TIMEOUT;
kenjiArai 1:e162361e197f 254 }
kenjiArai 1:e162361e197f 255 }
kenjiArai 1:e162361e197f 256
kenjiArai 1:e162361e197f 257 #if defined(RCC_CFGR_PLLSRC_HSI_PREDIV)
kenjiArai 1:e162361e197f 258 DBG("%u:\r\n", __LINE__);
kenjiArai 1:e162361e197f 259 /* Configure the main PLL clock source,
kenjiArai 1:e162361e197f 260 predivider and multiplication factor. */
kenjiArai 1:e162361e197f 261 __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
kenjiArai 1:e162361e197f 262 RCC_OscInitStruct->PLL.PREDIV,
kenjiArai 1:e162361e197f 263 RCC_OscInitStruct->PLL.PLLMUL);
kenjiArai 1:e162361e197f 264 #else
kenjiArai 1:e162361e197f 265 DBG("%u:\r\n", __LINE__);
kenjiArai 1:e162361e197f 266 /* Configure the main PLL clock source and multiplication factor. */
kenjiArai 1:e162361e197f 267 __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
kenjiArai 1:e162361e197f 268 RCC_OscInitStruct->PLL.PLLMUL);
kenjiArai 1:e162361e197f 269 #endif /* RCC_CFGR_PLLSRC_HSI_PREDIV */
kenjiArai 1:e162361e197f 270 //??????????????????????????????????????????????????????????????????????????????
kenjiArai 1:e162361e197f 271 // YOU CANNOT USE DEBUG CODE "DBG("%u:\r\n", __LINE__);"
kenjiArai 1:e162361e197f 272 // BECAUSE CLOCK WILL CHANGE AND UART CLOCK IS DIFFERENT
kenjiArai 1:e162361e197f 273
kenjiArai 1:e162361e197f 274 /* Enable the main PLL. */
kenjiArai 1:e162361e197f 275 __HAL_RCC_PLL_ENABLE();
kenjiArai 1:e162361e197f 276
kenjiArai 1:e162361e197f 277 /* Get Start Tick */
kenjiArai 1:e162361e197f 278 tickstart = HAL_GetTick();
kenjiArai 1:e162361e197f 279
kenjiArai 1:e162361e197f 280 /* Wait till PLL is ready */
kenjiArai 1:e162361e197f 281 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
kenjiArai 1:e162361e197f 282 {
kenjiArai 1:e162361e197f 283 if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
kenjiArai 1:e162361e197f 284 {
kenjiArai 1:e162361e197f 285 return HAL_TIMEOUT;
kenjiArai 1:e162361e197f 286 }
kenjiArai 1:e162361e197f 287 }
kenjiArai 1:e162361e197f 288 }
kenjiArai 1:e162361e197f 289 else
kenjiArai 1:e162361e197f 290 {
kenjiArai 1:e162361e197f 291 /* Disable the main PLL. */
kenjiArai 1:e162361e197f 292 __HAL_RCC_PLL_DISABLE();
kenjiArai 1:e162361e197f 293
kenjiArai 1:e162361e197f 294 /* Get Start Tick */
kenjiArai 1:e162361e197f 295 tickstart = HAL_GetTick();
kenjiArai 1:e162361e197f 296
kenjiArai 1:e162361e197f 297 /* Wait till PLL is disabled */
kenjiArai 1:e162361e197f 298 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
kenjiArai 1:e162361e197f 299 {
kenjiArai 1:e162361e197f 300 if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
kenjiArai 1:e162361e197f 301 {
kenjiArai 1:e162361e197f 302 return HAL_TIMEOUT;
kenjiArai 1:e162361e197f 303 }
kenjiArai 1:e162361e197f 304 }
kenjiArai 1:e162361e197f 305 }
kenjiArai 1:e162361e197f 306 }
kenjiArai 1:e162361e197f 307 else
kenjiArai 1:e162361e197f 308 {
kenjiArai 1:e162361e197f 309 return HAL_ERROR;
kenjiArai 1:e162361e197f 310 }
kenjiArai 1:e162361e197f 311 }
kenjiArai 1:e162361e197f 312 return HAL_OK;
kenjiArai 1:e162361e197f 313 }
kenjiArai 1:e162361e197f 314
kenjiArai 1:e162361e197f 315 /******************************************************************************/
kenjiArai 1:e162361e197f 316 /* PLL (clocked by HSI) used as System clock source */
kenjiArai 1:e162361e197f 317 /******************************************************************************/
kenjiArai 1:e162361e197f 318 uint8_t SetSysClock_PLL_HSI(void)
kenjiArai 1:e162361e197f 319 {
kenjiArai 1:e162361e197f 320 RCC_ClkInitTypeDef RCC_ClkInitStruct;
kenjiArai 1:e162361e197f 321 RCC_OscInitTypeDef RCC_OscInitStruct;
kenjiArai 1:e162361e197f 322
kenjiArai 1:e162361e197f 323 /* Enable HSI oscillator and activate PLL with HSI as source */
kenjiArai 1:e162361e197f 324 RCC_OscInitStruct.OscillatorType =
kenjiArai 1:e162361e197f 325 RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSE;
kenjiArai 1:e162361e197f 326 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
kenjiArai 1:e162361e197f 327 RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
kenjiArai 1:e162361e197f 328 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
kenjiArai 1:e162361e197f 329 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
kenjiArai 1:e162361e197f 330 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
kenjiArai 1:e162361e197f 331 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16; // 64 MHz(8MHz/2*16)
kenjiArai 1:e162361e197f 332 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
kenjiArai 1:e162361e197f 333 return 0; // FAIL
kenjiArai 1:e162361e197f 334 }
kenjiArai 1:e162361e197f 335
kenjiArai 1:e162361e197f 336 /* Select PLL as system clock source and
kenjiArai 1:e162361e197f 337 configure the HCLK, PCLK1 and PCLK2 clocks dividers */
kenjiArai 1:e162361e197f 338 RCC_ClkInitStruct.ClockType =
kenjiArai 1:e162361e197f 339 (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK
kenjiArai 1:e162361e197f 340 | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
kenjiArai 1:e162361e197f 341 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 64 MHz
kenjiArai 1:e162361e197f 342 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 64 MHz
kenjiArai 1:e162361e197f 343 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; // 32 MHz
kenjiArai 1:e162361e197f 344 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 64 MHz
kenjiArai 1:e162361e197f 345 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
kenjiArai 1:e162361e197f 346 return 0; // FAIL
kenjiArai 1:e162361e197f 347 }
kenjiArai 1:e162361e197f 348 return 1; // OK
kenjiArai 1:e162361e197f 349 }
kenjiArai 1:e162361e197f 350
kenjiArai 1:e162361e197f 351 //***************************************************************************************************************************************************************
kenjiArai 1:e162361e197f 352 // Following source codes are in mbed-os latest version as of 2017-09-30
kenjiArai 1:e162361e197f 353 //***************************************************************************************************************************************************************
kenjiArai 1:e162361e197f 354 //------------------------------------------------------------------------------
kenjiArai 1:e162361e197f 355 // Original source file -> stm32f3xx_hal_rcc.c
kenjiArai 1:e162361e197f 356 // \targets\TARGET_STM\TARGET_STM32F3\device\
kenjiArai 1:e162361e197f 357 #if 0
kenjiArai 1:e162361e197f 358 #if 0
kenjiArai 1:e162361e197f 359 HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
kenjiArai 1:e162361e197f 360 {
kenjiArai 1:e162361e197f 361 uint32_t tickstart = 0U;
kenjiArai 1:e162361e197f 362
kenjiArai 1:e162361e197f 363 /* Check the parameters */
kenjiArai 1:e162361e197f 364 assert_param(RCC_OscInitStruct != NULL);
kenjiArai 1:e162361e197f 365 assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
kenjiArai 1:e162361e197f 366
kenjiArai 1:e162361e197f 367 /*------------------------------- HSE Configuration ------------------------*/
kenjiArai 1:e162361e197f 368 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
kenjiArai 1:e162361e197f 369 {
kenjiArai 1:e162361e197f 370 /* Check the parameters */
kenjiArai 1:e162361e197f 371 assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
kenjiArai 1:e162361e197f 372
kenjiArai 1:e162361e197f 373 /* When the HSE is used as system clock or clock source for PLL in these cases it is not allowed to be disabled */
kenjiArai 1:e162361e197f 374 if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSE)
kenjiArai 1:e162361e197f 375 || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSE)))
kenjiArai 1:e162361e197f 376 {
kenjiArai 1:e162361e197f 377 if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
kenjiArai 1:e162361e197f 378 {
kenjiArai 1:e162361e197f 379 return HAL_ERROR;
kenjiArai 1:e162361e197f 380 }
kenjiArai 1:e162361e197f 381 }
kenjiArai 1:e162361e197f 382 else
kenjiArai 1:e162361e197f 383 {
kenjiArai 1:e162361e197f 384 /* Set the new HSE configuration ---------------------------------------*/
kenjiArai 1:e162361e197f 385 __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
kenjiArai 1:e162361e197f 386
kenjiArai 1:e162361e197f 387 #if defined(RCC_CFGR_PLLSRC_HSI_DIV2)
kenjiArai 1:e162361e197f 388 /* Configure the HSE predivision factor --------------------------------*/
kenjiArai 1:e162361e197f 389 __HAL_RCC_HSE_PREDIV_CONFIG(RCC_OscInitStruct->HSEPredivValue);
kenjiArai 1:e162361e197f 390 #endif /* RCC_CFGR_PLLSRC_HSI_DIV2 */
kenjiArai 1:e162361e197f 391
kenjiArai 1:e162361e197f 392 /* Check the HSE State */
kenjiArai 1:e162361e197f 393 if(RCC_OscInitStruct->HSEState != RCC_HSE_OFF)
kenjiArai 1:e162361e197f 394 {
kenjiArai 1:e162361e197f 395 /* Get Start Tick */
kenjiArai 1:e162361e197f 396 tickstart = HAL_GetTick();
kenjiArai 1:e162361e197f 397
kenjiArai 1:e162361e197f 398 /* Wait till HSE is ready */
kenjiArai 1:e162361e197f 399 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
kenjiArai 1:e162361e197f 400 {
kenjiArai 1:e162361e197f 401 if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
kenjiArai 1:e162361e197f 402 {
kenjiArai 1:e162361e197f 403 return HAL_TIMEOUT;
kenjiArai 1:e162361e197f 404 }
kenjiArai 1:e162361e197f 405 }
kenjiArai 1:e162361e197f 406 }
kenjiArai 1:e162361e197f 407 else
kenjiArai 1:e162361e197f 408 {
kenjiArai 1:e162361e197f 409 /* Get Start Tick */
kenjiArai 1:e162361e197f 410 tickstart = HAL_GetTick();
kenjiArai 1:e162361e197f 411
kenjiArai 1:e162361e197f 412 /* Wait till HSE is disabled */
kenjiArai 1:e162361e197f 413 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)
kenjiArai 1:e162361e197f 414 {
kenjiArai 1:e162361e197f 415 if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
kenjiArai 1:e162361e197f 416 {
kenjiArai 1:e162361e197f 417 return HAL_TIMEOUT;
kenjiArai 1:e162361e197f 418 }
kenjiArai 1:e162361e197f 419 }
kenjiArai 1:e162361e197f 420 }
kenjiArai 1:e162361e197f 421 }
kenjiArai 1:e162361e197f 422 }
kenjiArai 1:e162361e197f 423 /*----------------------------- HSI Configuration --------------------------*/
kenjiArai 1:e162361e197f 424 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
kenjiArai 1:e162361e197f 425 {
kenjiArai 1:e162361e197f 426 /* Check the parameters */
kenjiArai 1:e162361e197f 427 assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
kenjiArai 1:e162361e197f 428 assert_param(IS_RCC_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
kenjiArai 1:e162361e197f 429
kenjiArai 1:e162361e197f 430 /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
kenjiArai 1:e162361e197f 431 if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSI)
kenjiArai 1:e162361e197f 432 || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSI)))
kenjiArai 1:e162361e197f 433 {
kenjiArai 1:e162361e197f 434 /* When HSI is used as system clock it will not disabled */
kenjiArai 1:e162361e197f 435 if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET) && (RCC_OscInitStruct->HSIState != RCC_HSI_ON))
kenjiArai 1:e162361e197f 436 {
kenjiArai 1:e162361e197f 437 return HAL_ERROR;
kenjiArai 1:e162361e197f 438 }
kenjiArai 1:e162361e197f 439 /* Otherwise, just the calibration is allowed */
kenjiArai 1:e162361e197f 440 else
kenjiArai 1:e162361e197f 441 {
kenjiArai 1:e162361e197f 442 /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
kenjiArai 1:e162361e197f 443 __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
kenjiArai 1:e162361e197f 444 }
kenjiArai 1:e162361e197f 445 }
kenjiArai 1:e162361e197f 446 else
kenjiArai 1:e162361e197f 447 {
kenjiArai 1:e162361e197f 448 /* Check the HSI State */
kenjiArai 1:e162361e197f 449 if(RCC_OscInitStruct->HSIState != RCC_HSI_OFF)
kenjiArai 1:e162361e197f 450 {
kenjiArai 1:e162361e197f 451 /* Enable the Internal High Speed oscillator (HSI). */
kenjiArai 1:e162361e197f 452 __HAL_RCC_HSI_ENABLE();
kenjiArai 1:e162361e197f 453
kenjiArai 1:e162361e197f 454 /* Get Start Tick */
kenjiArai 1:e162361e197f 455 tickstart = HAL_GetTick();
kenjiArai 1:e162361e197f 456
kenjiArai 1:e162361e197f 457 /* Wait till HSI is ready */
kenjiArai 1:e162361e197f 458 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
kenjiArai 1:e162361e197f 459 {
kenjiArai 1:e162361e197f 460 if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
kenjiArai 1:e162361e197f 461 {
kenjiArai 1:e162361e197f 462 return HAL_TIMEOUT;
kenjiArai 1:e162361e197f 463 }
kenjiArai 1:e162361e197f 464 }
kenjiArai 1:e162361e197f 465
kenjiArai 1:e162361e197f 466 /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
kenjiArai 1:e162361e197f 467 __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
kenjiArai 1:e162361e197f 468 }
kenjiArai 1:e162361e197f 469 else
kenjiArai 1:e162361e197f 470 {
kenjiArai 1:e162361e197f 471 /* Disable the Internal High Speed oscillator (HSI). */
kenjiArai 1:e162361e197f 472 __HAL_RCC_HSI_DISABLE();
kenjiArai 1:e162361e197f 473
kenjiArai 1:e162361e197f 474 /* Get Start Tick */
kenjiArai 1:e162361e197f 475 tickstart = HAL_GetTick();
kenjiArai 1:e162361e197f 476
kenjiArai 1:e162361e197f 477 /* Wait till HSI is disabled */
kenjiArai 1:e162361e197f 478 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET)
kenjiArai 1:e162361e197f 479 {
kenjiArai 1:e162361e197f 480 if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
kenjiArai 1:e162361e197f 481 {
kenjiArai 1:e162361e197f 482 return HAL_TIMEOUT;
kenjiArai 1:e162361e197f 483 }
kenjiArai 1:e162361e197f 484 }
kenjiArai 1:e162361e197f 485 }
kenjiArai 1:e162361e197f 486 }
kenjiArai 1:e162361e197f 487 }
kenjiArai 1:e162361e197f 488 /*------------------------------ LSI Configuration -------------------------*/
kenjiArai 1:e162361e197f 489 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
kenjiArai 1:e162361e197f 490 {
kenjiArai 1:e162361e197f 491 /* Check the parameters */
kenjiArai 1:e162361e197f 492 assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
kenjiArai 1:e162361e197f 493
kenjiArai 1:e162361e197f 494 /* Check the LSI State */
kenjiArai 1:e162361e197f 495 if(RCC_OscInitStruct->LSIState != RCC_LSI_OFF)
kenjiArai 1:e162361e197f 496 {
kenjiArai 1:e162361e197f 497 /* Enable the Internal Low Speed oscillator (LSI). */
kenjiArai 1:e162361e197f 498 __HAL_RCC_LSI_ENABLE();
kenjiArai 1:e162361e197f 499
kenjiArai 1:e162361e197f 500 /* Get Start Tick */
kenjiArai 1:e162361e197f 501 tickstart = HAL_GetTick();
kenjiArai 1:e162361e197f 502
kenjiArai 1:e162361e197f 503 /* Wait till LSI is ready */
kenjiArai 1:e162361e197f 504 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET)
kenjiArai 1:e162361e197f 505 {
kenjiArai 1:e162361e197f 506 if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
kenjiArai 1:e162361e197f 507 {
kenjiArai 1:e162361e197f 508 return HAL_TIMEOUT;
kenjiArai 1:e162361e197f 509 }
kenjiArai 1:e162361e197f 510 }
kenjiArai 1:e162361e197f 511 }
kenjiArai 1:e162361e197f 512 else
kenjiArai 1:e162361e197f 513 {
kenjiArai 1:e162361e197f 514 /* Disable the Internal Low Speed oscillator (LSI). */
kenjiArai 1:e162361e197f 515 __HAL_RCC_LSI_DISABLE();
kenjiArai 1:e162361e197f 516
kenjiArai 1:e162361e197f 517 /* Get Start Tick */
kenjiArai 1:e162361e197f 518 tickstart = HAL_GetTick();
kenjiArai 1:e162361e197f 519
kenjiArai 1:e162361e197f 520 /* Wait till LSI is disabled */
kenjiArai 1:e162361e197f 521 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != RESET)
kenjiArai 1:e162361e197f 522 {
kenjiArai 1:e162361e197f 523 if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
kenjiArai 1:e162361e197f 524 {
kenjiArai 1:e162361e197f 525 return HAL_TIMEOUT;
kenjiArai 1:e162361e197f 526 }
kenjiArai 1:e162361e197f 527 }
kenjiArai 1:e162361e197f 528 }
kenjiArai 1:e162361e197f 529 }
kenjiArai 1:e162361e197f 530 /*------------------------------ LSE Configuration -------------------------*/
kenjiArai 1:e162361e197f 531 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
kenjiArai 1:e162361e197f 532 {
kenjiArai 1:e162361e197f 533 FlagStatus pwrclkchanged = RESET;
kenjiArai 1:e162361e197f 534
kenjiArai 1:e162361e197f 535 /* Check the parameters */
kenjiArai 1:e162361e197f 536 assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
kenjiArai 1:e162361e197f 537
kenjiArai 1:e162361e197f 538 /* Update LSE configuration in Backup Domain control register */
kenjiArai 1:e162361e197f 539 /* Requires to enable write access to Backup Domain of necessary */
kenjiArai 1:e162361e197f 540 if(__HAL_RCC_PWR_IS_CLK_DISABLED())
kenjiArai 1:e162361e197f 541 {
kenjiArai 1:e162361e197f 542 __HAL_RCC_PWR_CLK_ENABLE();
kenjiArai 1:e162361e197f 543 pwrclkchanged = SET;
kenjiArai 1:e162361e197f 544 }
kenjiArai 1:e162361e197f 545
kenjiArai 1:e162361e197f 546 if(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
kenjiArai 1:e162361e197f 547 {
kenjiArai 1:e162361e197f 548 /* Enable write access to Backup domain */
kenjiArai 1:e162361e197f 549 SET_BIT(PWR->CR, PWR_CR_DBP);
kenjiArai 1:e162361e197f 550
kenjiArai 1:e162361e197f 551 /* Wait for Backup domain Write protection disable */
kenjiArai 1:e162361e197f 552 tickstart = HAL_GetTick();
kenjiArai 1:e162361e197f 553
kenjiArai 1:e162361e197f 554 while(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
kenjiArai 1:e162361e197f 555 {
kenjiArai 1:e162361e197f 556 if((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
kenjiArai 1:e162361e197f 557 {
kenjiArai 1:e162361e197f 558 return HAL_TIMEOUT;
kenjiArai 1:e162361e197f 559 }
kenjiArai 1:e162361e197f 560 }
kenjiArai 1:e162361e197f 561 }
kenjiArai 1:e162361e197f 562
kenjiArai 1:e162361e197f 563 /* Set the new LSE configuration -----------------------------------------*/
kenjiArai 1:e162361e197f 564 __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
kenjiArai 1:e162361e197f 565 /* Check the LSE State */
kenjiArai 1:e162361e197f 566 if(RCC_OscInitStruct->LSEState != RCC_LSE_OFF)
kenjiArai 1:e162361e197f 567 {
kenjiArai 1:e162361e197f 568 /* Get Start Tick */
kenjiArai 1:e162361e197f 569 tickstart = HAL_GetTick();
kenjiArai 1:e162361e197f 570
kenjiArai 1:e162361e197f 571 /* Wait till LSE is ready */
kenjiArai 1:e162361e197f 572 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
kenjiArai 1:e162361e197f 573 {
kenjiArai 1:e162361e197f 574 if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
kenjiArai 1:e162361e197f 575 {
kenjiArai 1:e162361e197f 576 return HAL_TIMEOUT;
kenjiArai 1:e162361e197f 577 }
kenjiArai 1:e162361e197f 578 }
kenjiArai 1:e162361e197f 579 }
kenjiArai 1:e162361e197f 580 else
kenjiArai 1:e162361e197f 581 {
kenjiArai 1:e162361e197f 582 /* Get Start Tick */
kenjiArai 1:e162361e197f 583 tickstart = HAL_GetTick();
kenjiArai 1:e162361e197f 584
kenjiArai 1:e162361e197f 585 /* Wait till LSE is disabled */
kenjiArai 1:e162361e197f 586 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET)
kenjiArai 1:e162361e197f 587 {
kenjiArai 1:e162361e197f 588 if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
kenjiArai 1:e162361e197f 589 {
kenjiArai 1:e162361e197f 590 return HAL_TIMEOUT;
kenjiArai 1:e162361e197f 591 }
kenjiArai 1:e162361e197f 592 }
kenjiArai 1:e162361e197f 593 }
kenjiArai 1:e162361e197f 594
kenjiArai 1:e162361e197f 595 /* Require to disable power clock if necessary */
kenjiArai 1:e162361e197f 596 if(pwrclkchanged == SET)
kenjiArai 1:e162361e197f 597 {
kenjiArai 1:e162361e197f 598 __HAL_RCC_PWR_CLK_DISABLE();
kenjiArai 1:e162361e197f 599 }
kenjiArai 1:e162361e197f 600 }
kenjiArai 1:e162361e197f 601
kenjiArai 1:e162361e197f 602 /*-------------------------------- PLL Configuration -----------------------*/
kenjiArai 1:e162361e197f 603 /* Check the parameters */
kenjiArai 1:e162361e197f 604 assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
kenjiArai 1:e162361e197f 605 if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE)
kenjiArai 1:e162361e197f 606 {
kenjiArai 1:e162361e197f 607 /* Check if the PLL is used as system clock or not */
kenjiArai 1:e162361e197f 608 if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
kenjiArai 1:e162361e197f 609 {
kenjiArai 1:e162361e197f 610 if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON)
kenjiArai 1:e162361e197f 611 {
kenjiArai 1:e162361e197f 612 /* Check the parameters */
kenjiArai 1:e162361e197f 613 assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
kenjiArai 1:e162361e197f 614 assert_param(IS_RCC_PLL_MUL(RCC_OscInitStruct->PLL.PLLMUL));
kenjiArai 1:e162361e197f 615 #if defined(RCC_CFGR_PLLSRC_HSI_PREDIV)
kenjiArai 1:e162361e197f 616 assert_param(IS_RCC_PREDIV(RCC_OscInitStruct->PLL.PREDIV));
kenjiArai 1:e162361e197f 617 #endif
kenjiArai 1:e162361e197f 618
kenjiArai 1:e162361e197f 619 /* Disable the main PLL. */
kenjiArai 1:e162361e197f 620 __HAL_RCC_PLL_DISABLE();
kenjiArai 1:e162361e197f 621
kenjiArai 1:e162361e197f 622 /* Get Start Tick */
kenjiArai 1:e162361e197f 623 tickstart = HAL_GetTick();
kenjiArai 1:e162361e197f 624
kenjiArai 1:e162361e197f 625 /* Wait till PLL is disabled */
kenjiArai 1:e162361e197f 626 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
kenjiArai 1:e162361e197f 627 {
kenjiArai 1:e162361e197f 628 if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
kenjiArai 1:e162361e197f 629 {
kenjiArai 1:e162361e197f 630 return HAL_TIMEOUT;
kenjiArai 1:e162361e197f 631 }
kenjiArai 1:e162361e197f 632 }
kenjiArai 1:e162361e197f 633
kenjiArai 1:e162361e197f 634 #if defined(RCC_CFGR_PLLSRC_HSI_PREDIV)
kenjiArai 1:e162361e197f 635 /* Configure the main PLL clock source, predivider and multiplication factor. */
kenjiArai 1:e162361e197f 636 __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
kenjiArai 1:e162361e197f 637 RCC_OscInitStruct->PLL.PREDIV,
kenjiArai 1:e162361e197f 638 RCC_OscInitStruct->PLL.PLLMUL);
kenjiArai 1:e162361e197f 639 #else
kenjiArai 1:e162361e197f 640 /* Configure the main PLL clock source and multiplication factor. */
kenjiArai 1:e162361e197f 641 __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
kenjiArai 1:e162361e197f 642 RCC_OscInitStruct->PLL.PLLMUL);
kenjiArai 1:e162361e197f 643 #endif /* RCC_CFGR_PLLSRC_HSI_PREDIV */
kenjiArai 1:e162361e197f 644 /* Enable the main PLL. */
kenjiArai 1:e162361e197f 645 __HAL_RCC_PLL_ENABLE();
kenjiArai 1:e162361e197f 646
kenjiArai 1:e162361e197f 647 /* Get Start Tick */
kenjiArai 1:e162361e197f 648 tickstart = HAL_GetTick();
kenjiArai 1:e162361e197f 649
kenjiArai 1:e162361e197f 650 /* Wait till PLL is ready */
kenjiArai 1:e162361e197f 651 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
kenjiArai 1:e162361e197f 652 {
kenjiArai 1:e162361e197f 653 if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
kenjiArai 1:e162361e197f 654 {
kenjiArai 1:e162361e197f 655 return HAL_TIMEOUT;
kenjiArai 1:e162361e197f 656 }
kenjiArai 1:e162361e197f 657 }
kenjiArai 1:e162361e197f 658 }
kenjiArai 1:e162361e197f 659 else
kenjiArai 1:e162361e197f 660 {
kenjiArai 1:e162361e197f 661 /* Disable the main PLL. */
kenjiArai 1:e162361e197f 662 __HAL_RCC_PLL_DISABLE();
kenjiArai 1:e162361e197f 663
kenjiArai 1:e162361e197f 664 /* Get Start Tick */
kenjiArai 1:e162361e197f 665 tickstart = HAL_GetTick();
kenjiArai 1:e162361e197f 666
kenjiArai 1:e162361e197f 667 /* Wait till PLL is disabled */
kenjiArai 1:e162361e197f 668 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
kenjiArai 1:e162361e197f 669 {
kenjiArai 1:e162361e197f 670 if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
kenjiArai 1:e162361e197f 671 {
kenjiArai 1:e162361e197f 672 return HAL_TIMEOUT;
kenjiArai 1:e162361e197f 673 }
kenjiArai 1:e162361e197f 674 }
kenjiArai 1:e162361e197f 675 }
kenjiArai 1:e162361e197f 676 }
kenjiArai 1:e162361e197f 677 else
kenjiArai 1:e162361e197f 678 {
kenjiArai 1:e162361e197f 679 return HAL_ERROR;
kenjiArai 1:e162361e197f 680 }
kenjiArai 1:e162361e197f 681 }
kenjiArai 1:e162361e197f 682
kenjiArai 1:e162361e197f 683 return HAL_OK;
kenjiArai 1:e162361e197f 684 }
kenjiArai 1:e162361e197f 685
kenjiArai 1:e162361e197f 686 #endif
kenjiArai 1:e162361e197f 687
kenjiArai 1:e162361e197f 688 // Original source file -> system_clock.c
kenjiArai 1:e162361e197f 689 // \targets\TARGET_STM\TARGET_STM32F3\TARGET_STM32F303x8\TARGET_NUCLEO_F303K8
kenjiArai 1:e162361e197f 690 #if 0
kenjiArai 1:e162361e197f 691 /* mbed Microcontroller Library
kenjiArai 1:e162361e197f 692 * Copyright (c) 2006-2017 ARM Limited
kenjiArai 1:e162361e197f 693 *
kenjiArai 1:e162361e197f 694 * Licensed under the Apache License, Version 2.0 (the "License");
kenjiArai 1:e162361e197f 695 * you may not use this file except in compliance with the License.
kenjiArai 1:e162361e197f 696 * You may obtain a copy of the License at
kenjiArai 1:e162361e197f 697 *
kenjiArai 1:e162361e197f 698 * http://www.apache.org/licenses/LICENSE-2.0
kenjiArai 1:e162361e197f 699 *
kenjiArai 1:e162361e197f 700 * Unless required by applicable law or agreed to in writing, software
kenjiArai 1:e162361e197f 701 * distributed under the License is distributed on an "AS IS" BASIS,
kenjiArai 1:e162361e197f 702 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kenjiArai 1:e162361e197f 703 * See the License for the specific language governing permissions and
kenjiArai 1:e162361e197f 704 * limitations under the License.
kenjiArai 1:e162361e197f 705 */
kenjiArai 1:e162361e197f 706
kenjiArai 1:e162361e197f 707 /**
kenjiArai 1:e162361e197f 708 * This file configures the system clock as follows:
kenjiArai 1:e162361e197f 709 *-----------------------------------------------------------------------------
kenjiArai 1:e162361e197f 710 * System clock source | 1- USE_PLL_HSE_EXTC | 3- USE_PLL_HSI
kenjiArai 1:e162361e197f 711 * | (external 8 MHz clock) | (internal 8 MHz)
kenjiArai 1:e162361e197f 712 * | 2- USE_PLL_HSE_XTAL |
kenjiArai 1:e162361e197f 713 * | (external 8 MHz xtal) |
kenjiArai 1:e162361e197f 714 *-----------------------------------------------------------------------------
kenjiArai 1:e162361e197f 715 * SYSCLK(MHz) | 72 | 64
kenjiArai 1:e162361e197f 716 *-----------------------------------------------------------------------------
kenjiArai 1:e162361e197f 717 * AHBCLK (MHz) | 72 | 64
kenjiArai 1:e162361e197f 718 *-----------------------------------------------------------------------------
kenjiArai 1:e162361e197f 719 * APB1CLK (MHz) | 36 | 32
kenjiArai 1:e162361e197f 720 *-----------------------------------------------------------------------------
kenjiArai 1:e162361e197f 721 * APB2CLK (MHz) | 72 | 64
kenjiArai 1:e162361e197f 722 *-----------------------------------------------------------------------------
kenjiArai 1:e162361e197f 723 * USB capable | NO | NO
kenjiArai 1:e162361e197f 724 *-----------------------------------------------------------------------------
kenjiArai 1:e162361e197f 725 */
kenjiArai 1:e162361e197f 726
kenjiArai 1:e162361e197f 727
kenjiArai 1:e162361e197f 728 #include "stm32f3xx.h"
kenjiArai 1:e162361e197f 729 #include "mbed_assert.h"
kenjiArai 1:e162361e197f 730
kenjiArai 1:e162361e197f 731 /*!< Uncomment the following line if you need to relocate your vector Table in
kenjiArai 1:e162361e197f 732 Internal SRAM. */
kenjiArai 1:e162361e197f 733 /* #define VECT_TAB_SRAM */
kenjiArai 1:e162361e197f 734 #define VECT_TAB_OFFSET 0x0 /*!< Vector Table base offset field.
kenjiArai 1:e162361e197f 735 This value must be a multiple of 0x200. */
kenjiArai 1:e162361e197f 736
kenjiArai 1:e162361e197f 737 // clock source is selected with CLOCK_SOURCE in json config
kenjiArai 1:e162361e197f 738 #define USE_PLL_HSE_EXTC 0x8 // Use external clock (ST Link MCO)
kenjiArai 1:e162361e197f 739 #define USE_PLL_HSE_XTAL 0x4 // Use external xtal (X3 on board - not provided by default)
kenjiArai 1:e162361e197f 740 #define USE_PLL_HSI 0x2 // Use HSI internal clock
kenjiArai 1:e162361e197f 741
kenjiArai 1:e162361e197f 742 #if ( ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) )
kenjiArai 1:e162361e197f 743 uint8_t SetSysClock_PLL_HSE(uint8_t bypass);
kenjiArai 1:e162361e197f 744 #endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */
kenjiArai 1:e162361e197f 745
kenjiArai 1:e162361e197f 746 #if ((CLOCK_SOURCE) & USE_PLL_HSI)
kenjiArai 1:e162361e197f 747 uint8_t SetSysClock_PLL_HSI(void);
kenjiArai 1:e162361e197f 748 #endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */
kenjiArai 1:e162361e197f 749
kenjiArai 1:e162361e197f 750 /**
kenjiArai 1:e162361e197f 751 * @brief Setup the microcontroller system
kenjiArai 1:e162361e197f 752 * Initialize the FPU setting, vector table location and the PLL configuration is reset.
kenjiArai 1:e162361e197f 753 * @param None
kenjiArai 1:e162361e197f 754 * @retval None
kenjiArai 1:e162361e197f 755 */
kenjiArai 1:e162361e197f 756 void SystemInit(void)
kenjiArai 1:e162361e197f 757 {
kenjiArai 1:e162361e197f 758 /* FPU settings ------------------------------------------------------------*/
kenjiArai 1:e162361e197f 759 #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
kenjiArai 1:e162361e197f 760 SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
kenjiArai 1:e162361e197f 761 #endif
kenjiArai 1:e162361e197f 762
kenjiArai 1:e162361e197f 763 /* Reset the RCC clock configuration to the default reset state ------------*/
kenjiArai 1:e162361e197f 764 /* Set HSION bit */
kenjiArai 1:e162361e197f 765 RCC->CR |= 0x00000001U;
kenjiArai 1:e162361e197f 766
kenjiArai 1:e162361e197f 767 /* Reset CFGR register */
kenjiArai 1:e162361e197f 768 RCC->CFGR &= 0xF87FC00CU;
kenjiArai 1:e162361e197f 769
kenjiArai 1:e162361e197f 770 /* Reset HSEON, CSSON and PLLON bits */
kenjiArai 1:e162361e197f 771 RCC->CR &= 0xFEF6FFFFU;
kenjiArai 1:e162361e197f 772
kenjiArai 1:e162361e197f 773 /* Reset HSEBYP bit */
kenjiArai 1:e162361e197f 774 RCC->CR &= 0xFFFBFFFFU;
kenjiArai 1:e162361e197f 775
kenjiArai 1:e162361e197f 776 /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE bits */
kenjiArai 1:e162361e197f 777 RCC->CFGR &= 0xFF80FFFFU;
kenjiArai 1:e162361e197f 778
kenjiArai 1:e162361e197f 779 /* Reset PREDIV1[3:0] bits */
kenjiArai 1:e162361e197f 780 RCC->CFGR2 &= 0xFFFFFFF0U;
kenjiArai 1:e162361e197f 781
kenjiArai 1:e162361e197f 782 /* Reset USARTSW[1:0], I2CSW and TIMs bits */
kenjiArai 1:e162361e197f 783 RCC->CFGR3 &= 0xFF00FCCCU;
kenjiArai 1:e162361e197f 784
kenjiArai 1:e162361e197f 785 /* Disable all interrupts */
kenjiArai 1:e162361e197f 786 RCC->CIR = 0x00000000U;
kenjiArai 1:e162361e197f 787
kenjiArai 1:e162361e197f 788 #ifdef VECT_TAB_SRAM
kenjiArai 1:e162361e197f 789 SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
kenjiArai 1:e162361e197f 790 #else
kenjiArai 1:e162361e197f 791 SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
kenjiArai 1:e162361e197f 792 #endif
kenjiArai 1:e162361e197f 793
kenjiArai 1:e162361e197f 794 }
kenjiArai 1:e162361e197f 795
kenjiArai 1:e162361e197f 796
kenjiArai 1:e162361e197f 797 /**
kenjiArai 1:e162361e197f 798 * @brief Configures the System clock source, PLL Multiplier and Divider factors,
kenjiArai 1:e162361e197f 799 * AHB/APBx prescalers and Flash settings
kenjiArai 1:e162361e197f 800 * @note This function should be called only once the RCC clock configuration
kenjiArai 1:e162361e197f 801 * is reset to the default reset state (done in SystemInit() function).
kenjiArai 1:e162361e197f 802 * @param None
kenjiArai 1:e162361e197f 803 * @retval None
kenjiArai 1:e162361e197f 804 */
kenjiArai 1:e162361e197f 805
kenjiArai 1:e162361e197f 806 void SetSysClock(void)
kenjiArai 1:e162361e197f 807 {
kenjiArai 1:e162361e197f 808 #if ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC)
kenjiArai 1:e162361e197f 809 /* 1- Try to start with HSE and external clock */
kenjiArai 1:e162361e197f 810 if (SetSysClock_PLL_HSE(1) == 0)
kenjiArai 1:e162361e197f 811 #endif
kenjiArai 1:e162361e197f 812 {
kenjiArai 1:e162361e197f 813 #if ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL)
kenjiArai 1:e162361e197f 814 /* 2- If fail try to start with HSE and external xtal */
kenjiArai 1:e162361e197f 815 if (SetSysClock_PLL_HSE(0) == 0)
kenjiArai 1:e162361e197f 816 #endif
kenjiArai 1:e162361e197f 817 {
kenjiArai 1:e162361e197f 818 #if ((CLOCK_SOURCE) & USE_PLL_HSI)
kenjiArai 1:e162361e197f 819 /* 3- If fail start with HSI clock */
kenjiArai 1:e162361e197f 820 if (SetSysClock_PLL_HSI() == 0)
kenjiArai 1:e162361e197f 821 #endif
kenjiArai 1:e162361e197f 822 {
kenjiArai 1:e162361e197f 823 while(1) {
kenjiArai 1:e162361e197f 824 MBED_ASSERT(1);
kenjiArai 1:e162361e197f 825 }
kenjiArai 1:e162361e197f 826 }
kenjiArai 1:e162361e197f 827 }
kenjiArai 1:e162361e197f 828 }
kenjiArai 1:e162361e197f 829
kenjiArai 1:e162361e197f 830 /* Output clock on MCO1 pin(PA8) for debugging purpose */
kenjiArai 1:e162361e197f 831 //HAL_RCC_MCOConfig(RCC_MCO, RCC_MCOSOURCE_SYSCLK, RCC_MCO_DIV1); // 72 MHz or 64 MHz
kenjiArai 1:e162361e197f 832 }
kenjiArai 1:e162361e197f 833
kenjiArai 1:e162361e197f 834 #if ( ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) )
kenjiArai 1:e162361e197f 835 /******************************************************************************/
kenjiArai 1:e162361e197f 836 /* PLL (clocked by HSE) used as System clock source */
kenjiArai 1:e162361e197f 837 /******************************************************************************/
kenjiArai 1:e162361e197f 838 uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
kenjiArai 1:e162361e197f 839 {
kenjiArai 1:e162361e197f 840 RCC_ClkInitTypeDef RCC_ClkInitStruct;
kenjiArai 1:e162361e197f 841 RCC_OscInitTypeDef RCC_OscInitStruct;
kenjiArai 1:e162361e197f 842
kenjiArai 1:e162361e197f 843 /* Enable HSE oscillator and activate PLL with HSE as source */
kenjiArai 1:e162361e197f 844 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
kenjiArai 1:e162361e197f 845 if (bypass == 0) {
kenjiArai 1:e162361e197f 846 RCC_OscInitStruct.HSEState = RCC_HSE_ON; /* External 8 MHz xtal on OSC_IN/OSC_OUT */
kenjiArai 1:e162361e197f 847 } else {
kenjiArai 1:e162361e197f 848 RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS; /* External 8 MHz clock on OSC_IN */
kenjiArai 1:e162361e197f 849 }
kenjiArai 1:e162361e197f 850 RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
kenjiArai 1:e162361e197f 851 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
kenjiArai 1:e162361e197f 852 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
kenjiArai 1:e162361e197f 853 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; // 72 MHz (8 MHz * 9)
kenjiArai 1:e162361e197f 854 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
kenjiArai 1:e162361e197f 855 return 0; // FAIL
kenjiArai 1:e162361e197f 856 }
kenjiArai 1:e162361e197f 857
kenjiArai 1:e162361e197f 858 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
kenjiArai 1:e162361e197f 859 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
kenjiArai 1:e162361e197f 860 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 72 MHz
kenjiArai 1:e162361e197f 861 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 72 MHz
kenjiArai 1:e162361e197f 862 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; // 36 MHz
kenjiArai 1:e162361e197f 863 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 72 MHz
kenjiArai 1:e162361e197f 864 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
kenjiArai 1:e162361e197f 865 return 0; // FAIL
kenjiArai 1:e162361e197f 866 }
kenjiArai 1:e162361e197f 867
kenjiArai 1:e162361e197f 868 /* Output clock on MCO1 pin(PA8) for debugging purpose */
kenjiArai 1:e162361e197f 869 //if (bypass == 0)
kenjiArai 1:e162361e197f 870 // HAL_RCC_MCOConfig(RCC_MCO, RCC_MCOSOURCE_HSE, RCC_MCO_DIV2); // 4 MHz with xtal
kenjiArai 1:e162361e197f 871 //else
kenjiArai 1:e162361e197f 872 // HAL_RCC_MCOConfig(RCC_MCO, RCC_MCOSOURCE_HSE, RCC_MCO_DIV1); // 8 MHz with ext clock
kenjiArai 1:e162361e197f 873
kenjiArai 1:e162361e197f 874 return 1; // OK
kenjiArai 1:e162361e197f 875 }
kenjiArai 1:e162361e197f 876 #endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */
kenjiArai 1:e162361e197f 877
kenjiArai 1:e162361e197f 878 #if ((CLOCK_SOURCE) & USE_PLL_HSI)
kenjiArai 1:e162361e197f 879 /******************************************************************************/
kenjiArai 1:e162361e197f 880 /* PLL (clocked by HSI) used as System clock source */
kenjiArai 1:e162361e197f 881 /******************************************************************************/
kenjiArai 1:e162361e197f 882 uint8_t SetSysClock_PLL_HSI(void)
kenjiArai 1:e162361e197f 883 {
kenjiArai 1:e162361e197f 884 RCC_ClkInitTypeDef RCC_ClkInitStruct;
kenjiArai 1:e162361e197f 885 RCC_OscInitTypeDef RCC_OscInitStruct;
kenjiArai 1:e162361e197f 886
kenjiArai 1:e162361e197f 887 /* Enable HSI oscillator and activate PLL with HSI as source */
kenjiArai 1:e162361e197f 888 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSE;
kenjiArai 1:e162361e197f 889 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
kenjiArai 1:e162361e197f 890 RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
kenjiArai 1:e162361e197f 891 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
kenjiArai 1:e162361e197f 892 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
kenjiArai 1:e162361e197f 893 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
kenjiArai 1:e162361e197f 894 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16; // 64 MHz (8 MHz/2 * 16)
kenjiArai 1:e162361e197f 895 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
kenjiArai 1:e162361e197f 896 return 0; // FAIL
kenjiArai 1:e162361e197f 897 }
kenjiArai 1:e162361e197f 898
kenjiArai 1:e162361e197f 899 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
kenjiArai 1:e162361e197f 900 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
kenjiArai 1:e162361e197f 901 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 64 MHz
kenjiArai 1:e162361e197f 902 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 64 MHz
kenjiArai 1:e162361e197f 903 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; // 32 MHz
kenjiArai 1:e162361e197f 904 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 64 MHz
kenjiArai 1:e162361e197f 905 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
kenjiArai 1:e162361e197f 906 return 0; // FAIL
kenjiArai 1:e162361e197f 907 }
kenjiArai 1:e162361e197f 908
kenjiArai 1:e162361e197f 909 /* Output clock on MCO1 pin(PA8) for debugging purpose */
kenjiArai 1:e162361e197f 910 //HAL_RCC_MCOConfig(RCC_MCO, RCC_MCOSOURCE_HSI, RCC_MCO_DIV1); // 8 MHz
kenjiArai 1:e162361e197f 911
kenjiArai 1:e162361e197f 912 return 1; // OK
kenjiArai 1:e162361e197f 913 }
kenjiArai 1:e162361e197f 914 #endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */
kenjiArai 1:e162361e197f 915
kenjiArai 1:e162361e197f 916 #endif