STM32H7时钟配置问题
z7 ?2 X0 S6 z- r设备:stm32h735VGxxx5 p& z, S# ~) A+ j4 g8 f, t% J
1 d0 G/ D, ^4 J- r; [5 d
问题:STM32时钟配置不正确,一直没法启动。
, K1 V! _) T. i' H/ n; q, [) [4 g. n' S5 M
解决:原因是配置时钟的一些参数超出的范围,所以配置导致不成功。- k' ^# J9 S! X) W4 @5 t
. ?2 c9 N3 e# v$ ^ }) n下面是H735的时钟的一些解释,其中 Fvco 是有范围的,这边从文档上面查到,FVCO的最大配置值为836M,所以配置的时候一定要注意。) G, _1 P) x+ Y9 ]7 W$ x
并且plln, pllm,pllp,pllq,这些都是有范围限制的,如果配置错误,那么芯片将不会正常运行
`8 a/ b; b3 \; ~8 F* {' P. L% w% ^- n( i
- /*+ I; }. b7 ~2 O" N" F4 {* `1 V
- * 时钟设置函数; c4 l$ i j. K0 N( Z& i' y
- * Fvco: VCO 频率+ j9 K. n# W4 W
- * Fsys: 系统时钟频率,也是 PLL1 的 p 分频输出时钟频率" U( T* n5 m9 ]. F$ i& M
- * Fq: PLL1 的 q 分频输出时钟频率" _/ p y2 l. O+ y+ V" P5 O+ D# W
- * Fs: PLL 输入时钟频率,可以是 HSI,CSI,HSE 等. , p: x+ s! F' Q, }) G2 g( p
- * Fvco = Fs * (plln/pllm) = (Fs/DIVM)*DIVN;
4 m8 ^# ~1 ~) K! t4 f - * Fsys = Fvco/pllp = Fs * (plln/(pllm * pllp));4 [" x" H) H7 D% L+ p1 Z
- * Fq = Fvco/pllq = Fs * (plln/(pllm * pllq));- K9 ]/ d$ \: b
- *
) U+ H! ?: D7 }: V( M' Q1 _8 E - * 6 O' B }1 j) [3 a' {" ?: U3 X
- * plln: PLL1 倍频系数(PLL 倍频),取值范围:4~512.
+ ~* X, c0 X, k- K* g# A5 C - * pllm: PLL1 预分频系数(进 PLL 之前的分频),取值范围:2~56.5 f: B; `6 j9 ]+ Q$ j9 H
- * pllp: PLL1 的 p 分频系数(PLL 之后的分频),分频后作为系统时钟,取值范围:2~128: d P: K) U2 d2 e7 S7 \
- * pllq: PLL1 的 q 分频系数(PLL 之后的分频),取值范围:1~128.
4 A$ w. ^2 d3 X$ ?) B0 P% M - * CPU 频率(rcc_c_ck) = sys_d1cpre_ck = 400Mhz ~0 d9 a& V7 V; `
- * rcc_aclk = rcc_hclk3 = 200Mhz
( w7 S$ z7 H. m: F8 E2 r, ]1 s - * AHB1/2/3/4(rcc_hclk1/2/3/4) = 200Mhz : O' t9 O* p4 C& E* c/ Y9 ?
- * APB1/2/3/4(rcc_pclk1/2/3/4) = 100Mhz $ [/ d/ V9 C! z e. Z& P
- * FMC 时钟频率 =pll2_r_ck=((25/25) * 512/2) = 256Mhz
% b. P) @0 J) }$ ` - * 3 V# y: u- Y3 {/ K3 v& _6 z }
- * Example:# |1 L( k& y! A! F. Q& d( j; r
- * 外部晶振为 25M 的时候,推荐值:plln = 160, pllm = 5, pllp = 2, pllq = 4.' K4 I; w4 j3 C: h% V
- * 得到:Fvco = 25 * (160/5) = 800Mhz
0 D6 D a' w6 O2 h - * Fsys = 800/2 = 400Mhz
! I$ K: f" R/ l! X% j. H* D - * Fq = 800/4 = 200Mhz D. K* v" w, o" C
- */
- ~4 G5 f5 ? m& e/ N: Y - i& G& O: a! y; {2 _/ j
- #define PLL_N 160
: G% e9 x3 I n% [ - #define PLL_M 5
/ Z7 V1 C. J/ Z" W' I! S9 ]% v8 } - #define PLL_P 2
* R/ A8 a& V9 }! G% `* P" I - #define PLL_Q 4
" l% J. o5 f: Q: T6 R
, A9 U4 k/ w T2 T8 b# s) Y1 d8 q2 H- // 时钟设置函数( R4 q0 `. S8 v9 J# y
- static void SystemClock_Config(void)
- j2 c3 D6 Y5 w6 O. w - {& }# n; x" _+ C7 \2 j, O$ E3 O5 f
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
8 P- v, K4 b$ h! w+ ? - RCC_OscInitTypeDef RCC_OscInitStruct = {0};% o4 f7 D5 r: a6 T6 B1 U# J
- HAL_StatusTypeDef ret = HAL_OK;
* S. ^) [9 }( e: {$ t- o6 D - ! A7 y8 `4 L. I; c& h6 f8 O6 j1 k
- /*!< Supply configuration update enable */
: v+ y3 W# V' L$ N/ ]. J - HAL_PWREx_ConfigSupply(PWR_DIRECT_SMPS_SUPPLY);
, d) M) X/ P [' g8 \) N* q, O7 n! P- z
3 B* f" O J B- /* The voltage scaling allows optimizing the power consumption when the device is
; O5 ]) o. Y# R' m* T5 T8 Q0 t - clocked below the maximum system frequency, to update the voltage scaling value7 f. D# z7 s7 R8 S
- regarding system frequency refer to product datasheet. */5 y% d& ^! s# M' H. {: `! t! P' Z
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);
7 \2 Z- c+ _ c% D: t/ D \* b - ' Z$ q9 V5 c- L' [2 ]! T9 N6 U5 g0 z' y
- while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}# W1 F) v0 g- ?, _
- & Z3 L; A) r& D( m7 ]. i4 e
- /* Enable HSE Oscillator and activate PLL with HSE as source */
3 Y* n* [9 l0 N& L7 g! S - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;/ N7 e' |; U0 m; [
- RCC_OscInitStruct.HSEState = RCC_HSE_ON; L6 u# ?, Q* o3 P0 J
- RCC_OscInitStruct.HSIState = RCC_HSI_OFF;
0 q5 g4 q: ]4 D - RCC_OscInitStruct.CSIState = RCC_CSI_OFF;5 D- p% x' [2 F+ V5 e k" m
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;, B& _7 K; y! f; H1 Z; A+ E
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;% R5 b7 Z, c6 ^! K
- 8 g7 h$ H% B% I0 h/ R
- RCC_OscInitStruct.PLL.PLLM = PLL_M;+ S6 a2 ^1 H3 T" F! [; `
- RCC_OscInitStruct.PLL.PLLN = PLL_N; \/ d/ R+ @! V4 v" _" I$ \7 E
- RCC_OscInitStruct.PLL.PLLFRACN = 0;% w5 F6 Y" s" u* f, G1 _! `& x
- RCC_OscInitStruct.PLL.PLLP = PLL_P;5 U; D( u O3 I3 z; I; }6 R) u3 s
- RCC_OscInitStruct.PLL.PLLR = 2;5 x8 _; i( V4 C% t8 J2 C) `+ |
- RCC_OscInitStruct.PLL.PLLQ = PLL_Q;
8 W" G5 e2 F' x; S
$ y8 o+ \; i8 P& J- RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
5 p, C2 W0 o3 o5 J( W - RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_2;
! U$ ~8 R E5 r# x- p2 I4 Q; i - ret = HAL_RCC_OscConfig(&RCC_OscInitStruct);
# X: }9 {; D7 k# T7 y: m- q2 z! s - if(ret != HAL_OK)
+ A; N9 B. R _$ t - {
, a5 h- @: Z% f" P/ Q - while(1) {};
; d, s0 K+ h( p5 c. T& Z9 O; A# \ - }
1 F. Z8 a+ b$ d, F - 9 ]/ ^& j) a2 ~! K) \; i6 R
- /* Select PLL as system clock source and configure bus clocks dividers */1 K& T5 J5 s6 q2 P
- RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_D1PCLK1 | RCC_CLOCKTYPE_PCLK1 | \! v- S0 \% N) o1 C g
- RCC_CLOCKTYPE_PCLK2 | RCC_CLOCKTYPE_D3PCLK1);1 @5 u: K- S7 z' [) x; b
- 2 R4 W& X! g! p- G, `; I
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
( J, g) ]- c% }& ]* V% s - RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;. B3 g, W2 U2 T! U- i: l( }1 Z j
- RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;$ Q, \# U$ ]8 i$ y! Y4 N
- RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
' N8 a: G" `# e: P - RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;8 }4 o7 Z* _8 [: o2 ]. x
- RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;& o, [. F$ q9 o! n. a- ]% D- I T
- RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;! F' e c$ A, m* z9 p
- ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3);
* E) Q% o1 L5 Z3 s - if(ret != HAL_OK)
4 F' {3 y! M( M( `* S - {1 d) i4 X T$ _( k2 f+ `
- while(1) {};( U$ L$ d$ \7 P' U( S) Z
- }
4 f; X2 G( X& M. @+ A! A1 k - + b1 m( b6 X7 _& D7 W* q
- }4 d' F) R7 c/ L# s
复制代码 3 \1 E( o9 l+ ?8 Y
6 S6 ~$ W, K, j6 |+ i [ |