|
| 1 | +/* mbed Microcontroller Library |
| 2 | +* Copyright (c) 2006-2017 ARM Limited |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +/** |
| 18 | + * This file configures the system clock as follows: |
| 19 | + *----------------------------------------------------------------- |
| 20 | + * System clock source | 1- USE_PLL_HSE_EXTC (external 8 MHz clock) |
| 21 | + * | 2- USE_PLL_HSE_XTAL (external 8 MHz xtal) |
| 22 | + * | 3- USE_PLL_HSI (internal 16 MHz) |
| 23 | + *----------------------------------------------------------------- |
| 24 | + * SYSCLK(MHz) | 32 |
| 25 | + * AHBCLK (MHz) | 32 |
| 26 | + * APB1CLK (MHz) | 32 |
| 27 | + * USB capable | YES |
| 28 | + *----------------------------------------------------------------- |
| 29 | + */ |
| 30 | + |
| 31 | +#include "stm32l0xx.h" |
| 32 | +#include "mbed_assert.h" |
| 33 | + |
| 34 | +/*!< Uncomment the following line if you need to relocate your vector Table in |
| 35 | + Internal SRAM. */ |
| 36 | +/* #define VECT_TAB_SRAM */ |
| 37 | +#define VECT_TAB_OFFSET 0x00U /*!< Vector Table base offset field. |
| 38 | + This value must be a multiple of 0x100. */ |
| 39 | + |
| 40 | +#define USE_PLL_HSE_EXTC 0x8 // Use external clock (ST Link MCO) |
| 41 | +#define USE_PLL_HSE_XTAL 0x4 // Use external xtal (X3 on board - not provided by default) |
| 42 | +#define USE_PLL_HSI 0x2 // Use HSI internal clock |
| 43 | + |
| 44 | +#if ( ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) ) |
| 45 | +uint8_t SetSysClock_PLL_HSE(uint8_t bypass); |
| 46 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */ |
| 47 | + |
| 48 | +#if ((CLOCK_SOURCE) & USE_PLL_HSI) |
| 49 | +uint8_t SetSysClock_PLL_HSI(void); |
| 50 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */ |
| 51 | + |
| 52 | + |
| 53 | +/** |
| 54 | + * @brief Setup the microcontroller system. |
| 55 | + * @param None |
| 56 | + * @retval None |
| 57 | + */ |
| 58 | +void SystemInit (void) |
| 59 | +{ |
| 60 | + /*!< Set MSION bit */ |
| 61 | + RCC->CR |= (uint32_t)0x00000100U; |
| 62 | + |
| 63 | + /*!< Reset SW[1:0], HPRE[3:0], PPRE1[2:0], PPRE2[2:0], MCOSEL[2:0] and MCOPRE[2:0] bits */ |
| 64 | + RCC->CFGR &= (uint32_t) 0x88FF400CU; |
| 65 | + |
| 66 | + /*!< Reset HSION, HSIDIVEN, HSEON, CSSON and PLLON bits */ |
| 67 | + RCC->CR &= (uint32_t)0xFEF6FFF6U; |
| 68 | + |
| 69 | + /*!< Reset HSI48ON bit */ |
| 70 | + RCC->CRRCR &= (uint32_t)0xFFFFFFFEU; |
| 71 | + |
| 72 | + /*!< Reset HSEBYP bit */ |
| 73 | + RCC->CR &= (uint32_t)0xFFFBFFFFU; |
| 74 | + |
| 75 | + /*!< Reset PLLSRC, PLLMUL[3:0] and PLLDIV[1:0] bits */ |
| 76 | + RCC->CFGR &= (uint32_t)0xFF02FFFFU; |
| 77 | + |
| 78 | + /*!< Disable all interrupts */ |
| 79 | + RCC->CIER = 0x00000000U; |
| 80 | + |
| 81 | + /* Configure the Vector Table location add offset address ------------------*/ |
| 82 | +#ifdef VECT_TAB_SRAM |
| 83 | + SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */ |
| 84 | +#else |
| 85 | + SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */ |
| 86 | +#endif |
| 87 | + |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * @brief Configures the System clock source, PLL Multiplier and Divider factors, |
| 92 | + * AHB/APBx prescalers and Flash settings |
| 93 | + * @note This function should be called only once the RCC clock configuration |
| 94 | + * is reset to the default reset state (done in SystemInit() function). |
| 95 | + * @param None |
| 96 | + * @retval None |
| 97 | + */ |
| 98 | +void SetSysClock(void) |
| 99 | +{ |
| 100 | +#if ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) |
| 101 | + /* 1- Try to start with HSE and external clock */ |
| 102 | + if (SetSysClock_PLL_HSE(1) == 0) |
| 103 | +#endif |
| 104 | + { |
| 105 | +#if ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) |
| 106 | + /* 2- If fail try to start with HSE and external xtal */ |
| 107 | + if (SetSysClock_PLL_HSE(0) == 0) |
| 108 | +#endif |
| 109 | + { |
| 110 | +#if ((CLOCK_SOURCE) & USE_PLL_HSI) |
| 111 | + /* 3- If fail start with HSI clock */ |
| 112 | + if (SetSysClock_PLL_HSI() == 0) |
| 113 | +#endif |
| 114 | + { |
| 115 | + while(1) { |
| 116 | + MBED_ASSERT(1); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /* Output clock on MCO1 pin(PA8) for debugging purpose */ |
| 123 | + //HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_SYSCLK, RCC_MCODIV_1); |
| 124 | + //HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSI48, RCC_MCODIV_1); |
| 125 | +} |
| 126 | + |
| 127 | +#if ( ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) ) |
| 128 | +/******************************************************************************/ |
| 129 | +/* PLL (clocked by HSE) used as System clock source */ |
| 130 | +/******************************************************************************/ |
| 131 | +uint8_t SetSysClock_PLL_HSE(uint8_t bypass) |
| 132 | +{ |
| 133 | + RCC_ClkInitTypeDef RCC_ClkInitStruct; |
| 134 | + RCC_OscInitTypeDef RCC_OscInitStruct; |
| 135 | + |
| 136 | + /* Used to gain time after DeepSleep in case HSI is used */ |
| 137 | + if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET) { |
| 138 | + return 0; |
| 139 | + } |
| 140 | + |
| 141 | + /* The voltage scaling allows optimizing the power consumption when the device is |
| 142 | + clocked below the maximum system frequency, to update the voltage scaling value |
| 143 | + regarding system frequency refer to product datasheet. */ |
| 144 | + __PWR_CLK_ENABLE(); |
| 145 | + __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); |
| 146 | + |
| 147 | + /* Enable HSE and HSI48 oscillators and activate PLL with HSE as source */ |
| 148 | + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI48; |
| 149 | + if (bypass == 0) { |
| 150 | + RCC_OscInitStruct.HSEState = RCC_HSE_ON; /* External 8 MHz xtal on OSC_IN/OSC_OUT */ |
| 151 | + } else { |
| 152 | + RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS; /* External 8 MHz clock on OSC_IN */ |
| 153 | + } |
| 154 | + RCC_OscInitStruct.HSIState = RCC_HSI_OFF; |
| 155 | + RCC_OscInitStruct.HSI48State = RCC_HSI48_ON; /* For USB and RNG clock */ |
| 156 | + // PLLCLK = (8 MHz * 8)/2 = 32 MHz |
| 157 | + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
| 158 | + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; |
| 159 | + RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_8; |
| 160 | + RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2; |
| 161 | + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
| 162 | + return 0; // FAIL |
| 163 | + } |
| 164 | + |
| 165 | + /* Select HSI48 as USB clock source */ |
| 166 | + RCC_PeriphCLKInitTypeDef PeriphClkInitStruct; |
| 167 | + PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USB; |
| 168 | + PeriphClkInitStruct.UsbClockSelection = RCC_USBCLKSOURCE_HSI48; |
| 169 | + if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { |
| 170 | + return 0; // FAIL |
| 171 | + } |
| 172 | + |
| 173 | + /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */ |
| 174 | + RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); |
| 175 | + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 32 MHz |
| 176 | + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 32 MHz |
| 177 | + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; // 32 MHz |
| 178 | + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 32 MHz |
| 179 | + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) { |
| 180 | + return 0; // FAIL |
| 181 | + } |
| 182 | + |
| 183 | + /* Output clock on MCO1 pin(PA8) for debugging purpose */ |
| 184 | + //if (bypass == 0) |
| 185 | + // HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSE, RCC_MCODIV_2); // 4 MHz |
| 186 | + //else |
| 187 | + // HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSE, RCC_MCODIV_1); // 8 MHz |
| 188 | + |
| 189 | + return 1; // OK |
| 190 | +} |
| 191 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */ |
| 192 | + |
| 193 | +#if ((CLOCK_SOURCE) & USE_PLL_HSI) |
| 194 | +/******************************************************************************/ |
| 195 | +/* PLL (clocked by HSI) used as System clock source */ |
| 196 | +/******************************************************************************/ |
| 197 | +uint8_t SetSysClock_PLL_HSI(void) |
| 198 | +{ |
| 199 | + RCC_ClkInitTypeDef RCC_ClkInitStruct; |
| 200 | + RCC_OscInitTypeDef RCC_OscInitStruct; |
| 201 | + RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit; |
| 202 | + |
| 203 | + /* The voltage scaling allows optimizing the power consumption when the device is |
| 204 | + clocked below the maximum system frequency, to update the voltage scaling value |
| 205 | + regarding system frequency refer to product datasheet. */ |
| 206 | + __PWR_CLK_ENABLE(); |
| 207 | + __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); |
| 208 | + |
| 209 | + /* Enable HSI and HSI48 oscillators and activate PLL with HSI as source */ |
| 210 | + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSI48; |
| 211 | + RCC_OscInitStruct.HSEState = RCC_HSE_OFF; |
| 212 | + RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
| 213 | + RCC_OscInitStruct.HSICalibrationValue = 16; |
| 214 | + RCC_OscInitStruct.HSI48State = RCC_HSI48_ON; /* For USB and RNG clock */ |
| 215 | + // PLLCLK = (16 MHz * 4)/2 = 32 MHz |
| 216 | + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
| 217 | + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; |
| 218 | + RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_4; |
| 219 | + RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2; |
| 220 | + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
| 221 | + return 0; // FAIL |
| 222 | + } |
| 223 | + |
| 224 | + /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */ |
| 225 | + RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); |
| 226 | + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 32 MHz |
| 227 | + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 32 MHz |
| 228 | + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; // 32 MHz |
| 229 | + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 32 MHz |
| 230 | + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) { |
| 231 | + return 0; // FAIL |
| 232 | + } |
| 233 | + |
| 234 | + RCC_PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB; |
| 235 | + RCC_PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_HSI48; |
| 236 | + if (HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphClkInit) != HAL_OK) { |
| 237 | + return 0; // FAIL |
| 238 | + } |
| 239 | + |
| 240 | + /* Output clock on MCO1 pin(PA8) for debugging purpose */ |
| 241 | + //HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSI, RCC_MCODIV_1); // 16 MHz |
| 242 | + |
| 243 | + return 1; // OK |
| 244 | +} |
| 245 | +#endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */ |
0 commit comments