src/TARGET_STM32F303xE/TARGET_NUCLEO_F303RE/system_clock.c

Thu, 15 Oct 2020 01:12:31 +0200

author
David Douard <david.douard@sdfa3.org>
date
Thu, 15 Oct 2020 01:12:31 +0200
changeset 27
8f2be7aaec00
permissions
-rw-r--r--

add QEI

27
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
1 /* mbed Microcontroller Library
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
2 * Copyright (c) 2006-2017 ARM Limited
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
3 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
4 * Licensed under the Apache License, Version 2.0 (the "License");
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
5 * you may not use this file except in compliance with the License.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
6 * You may obtain a copy of the License at
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
7 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
8 * http://www.apache.org/licenses/LICENSE-2.0
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
9 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
10 * Unless required by applicable law or agreed to in writing, software
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
11 * distributed under the License is distributed on an "AS IS" BASIS,
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
13 * See the License for the specific language governing permissions and
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
14 * limitations under the License.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
15 */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
16
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
17 /**
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
18 * This file configures the system clock as follows:
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
19 *-----------------------------------------------------------------------------
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
20 * System clock source | 1- USE_PLL_HSE_EXTC (external 8 MHz clock)
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
21 * | 2- USE_PLL_HSE_XTAL (external 8 MHz xtal)
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
22 * | 3- USE_PLL_HSI (internal 8 MHz)
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
23 *-----------------------------------------------------------------------------
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
24 * SYSCLK(MHz) | 72
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
25 * AHBCLK (MHz) | 72
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
26 * APB1CLK (MHz) | 36
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
27 * APB2CLK (MHz) | 72
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
28 * USB capable | YES
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
29 *-----------------------------------------------------------------------------
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
30 */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
31
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
32
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
33 #include "stm32f3xx.h"
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
34 #include "mbed_error.h"
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
35
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
36 // clock source is selected with CLOCK_SOURCE in json config
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
37 #define USE_PLL_HSE_EXTC 0x8 // Use external clock (ST Link MCO)
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
38 #define USE_PLL_HSE_XTAL 0x4 // Use external xtal (X3 on board - not provided by default)
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
39 #define USE_PLL_HSI 0x2 // Use HSI internal clock
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
40
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
41 #if ( ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) )
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
42 uint8_t SetSysClock_PLL_HSE(uint8_t bypass);
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
43 #endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
44
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
45 #if ((CLOCK_SOURCE) & USE_PLL_HSI)
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
46 uint8_t SetSysClock_PLL_HSI(void);
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
47 #endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
48
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
49 /**
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
50 * @brief Setup the microcontroller system
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
51 * Initialize the FPU setting, vector table location and the PLL configuration is reset.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
52 * @param None
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
53 * @retval None
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
54 */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
55 void SystemInit(void)
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
56 {
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
57 /* FPU settings ------------------------------------------------------------*/
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
58 #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
59 SCB->CPACR |= ((3UL << 10 * 2) | (3UL << 11 * 2)); /* set CP10 and CP11 Full Access */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
60 #endif
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
61
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
62 /* Reset the RCC clock configuration to the default reset state ------------*/
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
63 /* Set HSION bit */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
64 RCC->CR |= 0x00000001U;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
65
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
66 /* Reset CFGR register */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
67 RCC->CFGR &= 0xF87FC00CU;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
68
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
69 /* Reset HSEON, CSSON and PLLON bits */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
70 RCC->CR &= 0xFEF6FFFFU;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
71
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
72 /* Reset HSEBYP bit */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
73 RCC->CR &= 0xFFFBFFFFU;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
74
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
75 /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE bits */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
76 RCC->CFGR &= 0xFF80FFFFU;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
77
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
78 /* Reset PREDIV1[3:0] bits */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
79 RCC->CFGR2 &= 0xFFFFFFF0U;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
80
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
81 /* Reset USARTSW[1:0], I2CSW and TIMs bits */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
82 RCC->CFGR3 &= 0xFF00FCCCU;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
83
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
84 /* Disable all interrupts */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
85 RCC->CIR = 0x00000000U;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
86 }
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
87
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
88
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
89 /**
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
90 * @brief Configures the System clock source, PLL Multiplier and Divider factors,
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
91 * AHB/APBx prescalers and Flash settings
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
92 * @note This function should be called only once the RCC clock configuration
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
93 * is reset to the default reset state (done in SystemInit() function).
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
94 * @param None
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
95 * @retval None
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
96 */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
97
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
98 void SetSysClock(void)
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
99 {
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
100 #if ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC)
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
101 /* 1- Try to start with HSE and external clock */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
102 if (SetSysClock_PLL_HSE(1) == 0)
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
103 #endif
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
104 {
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
105 #if ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL)
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
106 /* 2- If fail try to start with HSE and external xtal */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
107 if (SetSysClock_PLL_HSE(0) == 0)
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
108 #endif
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
109 {
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
110 #if ((CLOCK_SOURCE) & USE_PLL_HSI)
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
111 /* 3- If fail start with HSI clock */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
112 if (SetSysClock_PLL_HSI() == 0)
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
113 #endif
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
114 {
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
115 {
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
116 error("SetSysClock failed\n");
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
117 }
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
118 }
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
119 }
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
120 }
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
121
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
122 /* Output clock on MCO2 pin(PC9) for debugging purpose */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
123 //HAL_RCC_MCOConfig(RCC_MCO2, RCC_MCO2SOURCE_SYSCLK, RCC_MCODIV_4);
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
124 }
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
125
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
126 #if ( ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) )
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
127 /******************************************************************************/
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
128 /* PLL (clocked by HSE) used as System clock source */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
129 /******************************************************************************/
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
130 uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
131 {
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
132 RCC_ClkInitTypeDef RCC_ClkInitStruct;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
133 RCC_OscInitTypeDef RCC_OscInitStruct;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
134 RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
135
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
136 /* Enable HSE oscillator and activate PLL with HSE as source */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
137 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
138 if (bypass == 0) {
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
139 RCC_OscInitStruct.HSEState = RCC_HSE_ON; /* External 8 MHz xtal on OSC_IN/OSC_OUT */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
140 } else {
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
141 RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS; /* External 8 MHz clock on OSC_IN */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
142 }
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
143 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
144 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
145 RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
146 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; // 72 MHz (8 MHz * 9)
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
147 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
148 return 0; // FAIL
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
149 }
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
150
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
151 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
152 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
153 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 72 MHz
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
154 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 72 MHz
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
155 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; // 36 MHz
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
156 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 72 MHz
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
157 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
158 return 0; // FAIL
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
159 }
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
160
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
161 RCC_PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
162 RCC_PeriphClkInit.USBClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
163 if (HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphClkInit) != HAL_OK) {
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
164 return 0; // FAIL
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
165 }
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
166
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
167 /* Output clock on MCO1 pin(PA8) for debugging purpose */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
168 //if (bypass == 0)
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
169 // HAL_RCC_MCOConfig(RCC_MCO, RCC_MCOSOURCE_HSE, RCC_MCO_DIV2); // 4 MHz with xtal
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
170 //else
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
171 // HAL_RCC_MCOConfig(RCC_MCO, RCC_MCOSOURCE_HSE, RCC_MCO_DIV1); // 8 MHz with ext clock
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
172
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
173 return 1; // OK
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
174 }
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
175 #endif /* ((CLOCK_SOURCE) & USE_PLL_HSE_XTAL) || ((CLOCK_SOURCE) & USE_PLL_HSE_EXTC) */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
176
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
177 #if ((CLOCK_SOURCE) & USE_PLL_HSI)
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
178 /******************************************************************************/
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
179 /* PLL (clocked by HSI) used as System clock source */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
180 /******************************************************************************/
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
181 uint8_t SetSysClock_PLL_HSI(void)
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
182 {
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
183 RCC_ClkInitTypeDef RCC_ClkInitStruct;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
184 RCC_OscInitTypeDef RCC_OscInitStruct;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
185 RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
186
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
187 /* Enable HSI oscillator and activate PLL with HSI as source */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
188 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSE;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
189 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
190 RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
191 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
192 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
193 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
194 RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
195 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; // 72 MHz (8 MHz/1 * 9)
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
196 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
197 return 0; // FAIL
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
198 }
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
199
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
200 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
201 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
202 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 72 MHz
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
203 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 72 MHz
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
204 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; // 36 MHz
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
205 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 72 MHz
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
206 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
207 return 0; // FAIL
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
208 }
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
209
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
210 RCC_PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
211 RCC_PeriphClkInit.USBClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
212 if (HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphClkInit) != HAL_OK) {
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
213 return 0; // FAIL
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
214 }
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
215
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
216 /* Output clock on MCO1 pin(PA8) for debugging purpose */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
217 //HAL_RCC_MCOConfig(RCC_MCO, RCC_MCOSOURCE_HSI, RCC_MCO_DIV1); // 8 MHz
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
218
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
219 return 1; // OK
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
220 }
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
221 #endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */

mercurial