|
| 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- PLL_HSE_EXTC | 3- PLL_HSI |
| 21 | + * | (external 8 MHz clock) | (internal 16 MHz) |
| 22 | + * | 2- PLL_HSE_XTAL | |
| 23 | + * | (external 8 MHz xtal) | |
| 24 | + *----------------------------------------------------------------------------- |
| 25 | + * SYSCLK(MHz) | 24 | 32 |
| 26 | + *----------------------------------------------------------------------------- |
| 27 | + * AHBCLK (MHz) | 24 | 32 |
| 28 | + *----------------------------------------------------------------------------- |
| 29 | + * APB1CLK (MHz) | 24 | 32 |
| 30 | + *----------------------------------------------------------------------------- |
| 31 | + * APB2CLK (MHz) | 24 | 32 |
| 32 | + *----------------------------------------------------------------------------- |
| 33 | + * USB capable (48 MHz precise clock) | YES | NO |
| 34 | + *----------------------------------------------------------------------------- |
| 35 | + ****************************************************************************** |
| 36 | + */ |
| 37 | + |
| 38 | +#include "stm32l1xx.h" |
| 39 | + |
| 40 | +/*!< Uncomment the following line if you need to relocate your vector Table in |
| 41 | + Internal SRAM. */ |
| 42 | +/* #define VECT_TAB_SRAM */ |
| 43 | +#define VECT_TAB_OFFSET 0x0 /*!< Vector Table base offset field. |
| 44 | + This value must be a multiple of 0x200. */ |
| 45 | + |
| 46 | +/* Select the clock sources (other than HSI) to start with (0=OFF, 1=ON) */ |
| 47 | +#define USE_PLL_HSE_EXTC (0) /* Use external clock */ |
| 48 | +#define USE_PLL_HSE_XTAL (1) /* Use external xtal */ |
| 49 | + |
| 50 | + |
| 51 | +#if (USE_PLL_HSE_XTAL != 0) || (USE_PLL_HSE_EXTC != 0) |
| 52 | +uint8_t SetSysClock_PLL_HSE(uint8_t bypass); |
| 53 | +#endif |
| 54 | + |
| 55 | +uint8_t SetSysClock_PLL_HSI(void); |
| 56 | + |
| 57 | + |
| 58 | +/** |
| 59 | + * @brief Setup the microcontroller system. |
| 60 | + * Initialize the Embedded Flash Interface, the PLL and update the |
| 61 | + * SystemCoreClock variable. |
| 62 | + * @param None |
| 63 | + * @retval None |
| 64 | + */ |
| 65 | +void SystemInit (void) |
| 66 | +{ |
| 67 | + /*!< Set MSION bit */ |
| 68 | + RCC->CR |= (uint32_t)0x00000100; |
| 69 | + |
| 70 | + /*!< Reset SW[1:0], HPRE[3:0], PPRE1[2:0], PPRE2[2:0], MCOSEL[2:0] and MCOPRE[2:0] bits */ |
| 71 | + RCC->CFGR &= (uint32_t)0x88FFC00C; |
| 72 | + |
| 73 | + /*!< Reset HSION, HSEON, CSSON and PLLON bits */ |
| 74 | + RCC->CR &= (uint32_t)0xEEFEFFFE; |
| 75 | + |
| 76 | + /*!< Reset HSEBYP bit */ |
| 77 | + RCC->CR &= (uint32_t)0xFFFBFFFF; |
| 78 | + |
| 79 | + /*!< Reset PLLSRC, PLLMUL[3:0] and PLLDIV[1:0] bits */ |
| 80 | + RCC->CFGR &= (uint32_t)0xFF02FFFF; |
| 81 | + |
| 82 | + /*!< Disable all interrupts */ |
| 83 | + RCC->CIR = 0x00000000; |
| 84 | + |
| 85 | +#ifdef DATA_IN_ExtSRAM |
| 86 | + SystemInit_ExtMemCtl(); |
| 87 | +#endif /* DATA_IN_ExtSRAM */ |
| 88 | + |
| 89 | +#ifdef VECT_TAB_SRAM |
| 90 | + SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */ |
| 91 | +#else |
| 92 | + SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */ |
| 93 | +#endif |
| 94 | + |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * @brief Configures the System clock source, PLL Multiplier and Divider factors, |
| 99 | + * AHB/APBx prescalers and Flash settings |
| 100 | + * @note This function should be called only once the RCC clock configuration |
| 101 | + * is reset to the default reset state (done in SystemInit() function). |
| 102 | + * @param None |
| 103 | + * @retval None |
| 104 | + */ |
| 105 | +void SetSysClock(void) |
| 106 | +{ |
| 107 | + /* 1- Try to start with HSE and external clock */ |
| 108 | +#if USE_PLL_HSE_EXTC != 0 |
| 109 | + if (SetSysClock_PLL_HSE(1) == 0) |
| 110 | +#endif |
| 111 | + { |
| 112 | + /* 2- If fail try to start with HSE and external xtal */ |
| 113 | +#if USE_PLL_HSE_XTAL != 0 |
| 114 | + if (SetSysClock_PLL_HSE(0) == 0) |
| 115 | +#endif |
| 116 | + { |
| 117 | + /* 3- If fail start with HSI clock */ |
| 118 | + if (SetSysClock_PLL_HSI() == 0) { |
| 119 | + while(1) { |
| 120 | + // [TODO] Put something here to tell the user that a problem occured... |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + /* Output clock on MCO1 pin(PA8) for debugging purpose */ |
| 127 | + //HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_SYSCLK, RCC_MCODIV_1); |
| 128 | +} |
| 129 | + |
| 130 | +#if (USE_PLL_HSE_XTAL != 0) || (USE_PLL_HSE_EXTC != 0) |
| 131 | +/******************************************************************************/ |
| 132 | +/* PLL (clocked by HSE) used as System clock source */ |
| 133 | +/******************************************************************************/ |
| 134 | +uint8_t SetSysClock_PLL_HSE(uint8_t bypass) |
| 135 | +{ |
| 136 | + RCC_ClkInitTypeDef RCC_ClkInitStruct; |
| 137 | + RCC_OscInitTypeDef RCC_OscInitStruct; |
| 138 | + |
| 139 | + if (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) |
| 140 | + return 1; // already on HSE PLL, could occur from deepsleep waking |
| 141 | + |
| 142 | + /* Used to gain time after DeepSleep in case HSI is used */ |
| 143 | + if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET) { |
| 144 | + return 0; |
| 145 | + } |
| 146 | + |
| 147 | + /* The voltage scaling allows optimizing the power consumption when the device is |
| 148 | + clocked below the maximum system frequency, to update the voltage scaling value |
| 149 | + regarding system frequency refer to product datasheet. */ |
| 150 | + __PWR_CLK_ENABLE(); |
| 151 | + __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); |
| 152 | + |
| 153 | + /* Enable HSE and HSI48 oscillators and activate PLL with HSE as source */ |
| 154 | + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; |
| 155 | + if (bypass == 0) { |
| 156 | + RCC_OscInitStruct.HSEState = RCC_HSE_ON; /* External 8 MHz xtal on OSC_IN/OSC_OUT */ |
| 157 | + } else { |
| 158 | + RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS; /* External 8 MHz clock on OSC_IN */ |
| 159 | + } |
| 160 | + RCC_OscInitStruct.HSIState = RCC_HSI_OFF; |
| 161 | + // SYSCLK = 24 MHz ((8 MHz * 6) / 2) |
| 162 | + // USBCLK = 48 MHz (8 MHz * 6) --> USB OK |
| 163 | + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
| 164 | + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; |
| 165 | + RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6; |
| 166 | + RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV2; |
| 167 | + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
| 168 | + return 0; // FAIL |
| 169 | + } |
| 170 | + |
| 171 | + /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */ |
| 172 | + RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); |
| 173 | + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 24 MHz |
| 174 | + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 24 MHz |
| 175 | + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; // 24 MHz |
| 176 | + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 24 MHz |
| 177 | + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) { |
| 178 | + return 0; // FAIL |
| 179 | + } |
| 180 | + |
| 181 | + /* Output clock on MCO1 pin(PA8) for debugging purpose */ |
| 182 | + //if (bypass == 0) |
| 183 | + //HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSE, RCC_MCODIV_2); // 4 MHz |
| 184 | + //else |
| 185 | + //HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSE, RCC_MCODIV_1); // 8 MHz |
| 186 | + |
| 187 | + return 1; // OK |
| 188 | +} |
| 189 | +#endif |
| 190 | + |
| 191 | +/******************************************************************************/ |
| 192 | +/* PLL (clocked by HSI) used as System clock source */ |
| 193 | +/******************************************************************************/ |
| 194 | +uint8_t SetSysClock_PLL_HSI(void) |
| 195 | +{ |
| 196 | + RCC_ClkInitTypeDef RCC_ClkInitStruct; |
| 197 | + RCC_OscInitTypeDef RCC_OscInitStruct; |
| 198 | + |
| 199 | + /* The voltage scaling allows optimizing the power consumption when the device is |
| 200 | + clocked below the maximum system frequency, to update the voltage scaling value |
| 201 | + regarding system frequency refer to product datasheet. */ |
| 202 | + __PWR_CLK_ENABLE(); |
| 203 | + __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); |
| 204 | + |
| 205 | + /* Enable HSI oscillator and activate PLL with HSI as source */ |
| 206 | + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; |
| 207 | + RCC_OscInitStruct.HSEState = RCC_HSE_OFF; |
| 208 | + RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
| 209 | + // SYSCLK = 32 MHz ((16 MHz * 4) / 2) |
| 210 | + // USBCLK = 64 MHz (16 MHz * 4) --> USB not possible |
| 211 | + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
| 212 | + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; |
| 213 | + RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4; |
| 214 | + RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV2; |
| 215 | + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
| 216 | + return 0; // FAIL |
| 217 | + } |
| 218 | + |
| 219 | + /* Poll VOSF bit of in PWR_CSR. Wait until it is reset to 0 */ |
| 220 | + while (__HAL_PWR_GET_FLAG(PWR_FLAG_VOS) != RESET) {}; |
| 221 | + |
| 222 | + /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */ |
| 223 | + RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); |
| 224 | + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 32 MHz |
| 225 | + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 32 MHz |
| 226 | + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; // 32 MHz |
| 227 | + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 32 MHz |
| 228 | + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) { |
| 229 | + return 0; // FAIL |
| 230 | + } |
| 231 | + |
| 232 | + /* Output clock on MCO1 pin(PA8) for debugging purpose */ |
| 233 | + //HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSI, RCC_MCODIV_1); // 16 MHz |
| 234 | + |
| 235 | + return 1; // OK |
| 236 | +} |
0 commit comments