|
| 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) | 100 | 100 |
| 26 | + *----------------------------------------------------------------------------- |
| 27 | + * AHBCLK (MHz) | 100 | 100 |
| 28 | + *----------------------------------------------------------------------------- |
| 29 | + * APB1CLK (MHz) | 50 | 50 |
| 30 | + *----------------------------------------------------------------------------- |
| 31 | + * APB2CLK (MHz) | 100 | 100 |
| 32 | + *----------------------------------------------------------------------------- |
| 33 | + * USB capable (48 MHz precise clock) | NO | NO |
| 34 | + *----------------------------------------------------------------------------- |
| 35 | +**/ |
| 36 | + |
| 37 | +#include "stm32f4xx.h" |
| 38 | + |
| 39 | +/*!< Uncomment the following line if you need to relocate your vector Table in |
| 40 | + Internal SRAM. */ |
| 41 | +/* #define VECT_TAB_SRAM */ |
| 42 | +#ifndef VECT_TAB_OFFSET |
| 43 | +#define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field. |
| 44 | + This value must be a multiple of 0x200. */ |
| 45 | +#endif |
| 46 | + |
| 47 | + |
| 48 | +/* Select the clock sources (other than HSI) to start with (0=OFF, 1=ON) */ |
| 49 | +#define USE_PLL_HSE_EXTC (0) /* Use external clock */ |
| 50 | +#define USE_PLL_HSE_XTAL (1) /* Use external xtal */ |
| 51 | + |
| 52 | +#if (USE_PLL_HSE_XTAL != 0) || (USE_PLL_HSE_EXTC != 0) |
| 53 | +uint8_t SetSysClock_PLL_HSE(uint8_t bypass); |
| 54 | +#endif |
| 55 | + |
| 56 | +uint8_t SetSysClock_PLL_HSI(void); |
| 57 | + |
| 58 | +/** |
| 59 | + * @brief Setup the microcontroller system |
| 60 | + * Initialize the FPU setting, vector table location and External memory |
| 61 | + * configuration. |
| 62 | + * @param None |
| 63 | + * @retval None |
| 64 | + */ |
| 65 | +void SystemInit(void) |
| 66 | +{ |
| 67 | + /* FPU settings ------------------------------------------------------------*/ |
| 68 | +#if (__FPU_PRESENT == 1) && (__FPU_USED == 1) |
| 69 | + SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */ |
| 70 | +#endif |
| 71 | + /* Reset the RCC clock configuration to the default reset state ------------*/ |
| 72 | + /* Set HSION bit */ |
| 73 | + RCC->CR |= (uint32_t)0x00000001; |
| 74 | + |
| 75 | + /* Reset CFGR register */ |
| 76 | + RCC->CFGR = 0x00000000; |
| 77 | + |
| 78 | + /* Reset HSEON, CSSON and PLLON bits */ |
| 79 | + RCC->CR &= (uint32_t)0xFEF6FFFF; |
| 80 | + |
| 81 | + /* Reset PLLCFGR register */ |
| 82 | + RCC->PLLCFGR = 0x24003010; |
| 83 | + |
| 84 | + /* Reset HSEBYP bit */ |
| 85 | + RCC->CR &= (uint32_t)0xFFFBFFFF; |
| 86 | + |
| 87 | + /* Disable all interrupts */ |
| 88 | + RCC->CIR = 0x00000000; |
| 89 | + |
| 90 | +#if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM) |
| 91 | + SystemInit_ExtMemCtl(); |
| 92 | +#endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */ |
| 93 | + |
| 94 | + /* Configure the Vector Table location add offset address ------------------*/ |
| 95 | +#ifdef VECT_TAB_SRAM |
| 96 | + SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */ |
| 97 | +#else |
| 98 | + SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */ |
| 99 | +#endif |
| 100 | + |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * @brief Configures the System clock source, PLL Multiplier and Divider factors, |
| 105 | + * AHB/APBx prescalers and Flash settings |
| 106 | + * @note This function should be called only once the RCC clock configuration |
| 107 | + * is reset to the default reset state (done in SystemInit() function). |
| 108 | + * @param None |
| 109 | + * @retval None |
| 110 | + */ |
| 111 | +void SetSysClock(void) |
| 112 | +{ |
| 113 | + /* 1- Try to start with HSE and external clock */ |
| 114 | +#if USE_PLL_HSE_EXTC != 0 |
| 115 | + if (SetSysClock_PLL_HSE(1) == 0) |
| 116 | +#endif |
| 117 | + { |
| 118 | + /* 2- If fail try to start with HSE and external xtal */ |
| 119 | +#if USE_PLL_HSE_XTAL != 0 |
| 120 | + if (SetSysClock_PLL_HSE(0) == 0) |
| 121 | +#endif |
| 122 | + { |
| 123 | + /* 3- If fail start with HSI clock */ |
| 124 | + if (SetSysClock_PLL_HSI() == 0) { |
| 125 | + while(1) { |
| 126 | + // [TODO] Put something here to tell the user that a problem occured... |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /* Output clock on MCO2 pin(PC9) for debugging purpose */ |
| 133 | + //HAL_RCC_MCOConfig(RCC_MCO2, RCC_MCO2SOURCE_SYSCLK, RCC_MCODIV_4); // 100 MHz / 4 = 25 MHz |
| 134 | +} |
| 135 | + |
| 136 | +#if (USE_PLL_HSE_XTAL != 0) || (USE_PLL_HSE_EXTC != 0) |
| 137 | +/******************************************************************************/ |
| 138 | +/* PLL (clocked by HSE) used as System clock source */ |
| 139 | +/******************************************************************************/ |
| 140 | +uint8_t SetSysClock_PLL_HSE(uint8_t bypass) |
| 141 | +{ |
| 142 | + RCC_ClkInitTypeDef RCC_ClkInitStruct; |
| 143 | + RCC_OscInitTypeDef RCC_OscInitStruct; |
| 144 | + |
| 145 | + /* The voltage scaling allows optimizing the power consumption when the device is |
| 146 | + clocked below the maximum system frequency, to update the voltage scaling value |
| 147 | + regarding system frequency refer to product datasheet. */ |
| 148 | + __HAL_RCC_PWR_CLK_ENABLE(); |
| 149 | + __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2); |
| 150 | + |
| 151 | + /* Enable HSE oscillator and activate PLL with HSE as source */ |
| 152 | + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; |
| 153 | + if (bypass == 0) { |
| 154 | + RCC_OscInitStruct.HSEState = RCC_HSE_ON; /* External 8 MHz xtal on OSC_IN/OSC_OUT */ |
| 155 | + } else { |
| 156 | + RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS; /* External 8 MHz clock on OSC_IN */ |
| 157 | + } |
| 158 | + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
| 159 | + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; |
| 160 | + //RCC_OscInitStruct.PLL.PLLM = 8; // VCO input clock = 1 MHz (8 MHz / 8) |
| 161 | + //RCC_OscInitStruct.PLL.PLLN = 400; // VCO output clock = 400 MHz (1 MHz * 400) |
| 162 | + RCC_OscInitStruct.PLL.PLLM = 13; // VCO input clock = 2 MHz (8 MHz / 4) |
| 163 | + RCC_OscInitStruct.PLL.PLLN = 192; // VCO output clock = 400 MHz (2 MHz * 200) |
| 164 | + RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4; // PLLCLK = 100 MHz (400 MHz / 4) |
| 165 | + RCC_OscInitStruct.PLL.PLLQ = 8; // USB clock = 44.44 MHz (400 MHz / 9) --> Not good for USB |
| 166 | + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
| 167 | + return 0; // FAIL |
| 168 | + } |
| 169 | + |
| 170 | + /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */ |
| 171 | + RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); |
| 172 | + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 100 MHz |
| 173 | + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 100 MHz |
| 174 | + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; // 50 MHz |
| 175 | + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 100 MHz |
| 176 | + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK) { |
| 177 | + return 0; // FAIL |
| 178 | + } |
| 179 | + |
| 180 | + /* Output clock on MCO1 pin(PA8) for debugging purpose */ |
| 181 | + |
| 182 | + //if (bypass == 0) |
| 183 | + // HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSE, RCC_MCODIV_2); // 4 MHz with xtal |
| 184 | + //else |
| 185 | + // HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSE, RCC_MCODIV_1); // 8 MHz with external clock |
| 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 | + __HAL_RCC_PWR_CLK_ENABLE(); |
| 203 | + __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2); |
| 204 | + |
| 205 | + /* Enable HSI oscillator and activate PLL with HSI as source */ |
| 206 | + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSE; |
| 207 | + RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
| 208 | + RCC_OscInitStruct.HSEState = RCC_HSE_OFF; |
| 209 | + RCC_OscInitStruct.HSICalibrationValue = 16; |
| 210 | + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
| 211 | + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; |
| 212 | + //RCC_OscInitStruct.PLL.PLLM = 16; // VCO input clock = 1 MHz (16 MHz / 16) |
| 213 | + //RCC_OscInitStruct.PLL.PLLN = 400; // VCO output clock = 400 MHz (1 MHz * 400) |
| 214 | + RCC_OscInitStruct.PLL.PLLM = 8; // VCO input clock = 2 MHz (16 MHz / 8) |
| 215 | + RCC_OscInitStruct.PLL.PLLN = 200; // VCO output clock = 400 MHz (2 MHz * 200) |
| 216 | + RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4; // PLLCLK = 100 MHz (400 MHz / 4) |
| 217 | + RCC_OscInitStruct.PLL.PLLQ = 9; // USB clock = 44.44 MHz (400 MHz / 9) --> Not good for USB |
| 218 | + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
| 219 | + return 0; // FAIL |
| 220 | + } |
| 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; // 100 MHz |
| 225 | + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 100 MHz |
| 226 | + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; // 50 MHz |
| 227 | + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 100 MHz |
| 228 | + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != 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 | +} |
| 237 | + |
| 238 | +/******************************************************************************/ |
| 239 | +/* Hard Fault Handler */ |
| 240 | +/******************************************************************************/ |
| 241 | +void HardFault_Handler(void) |
| 242 | +{ |
| 243 | + printf("Hard Fault\n"); |
| 244 | + NVIC_SystemReset(); |
| 245 | +} |
0 commit comments