STM32H7时钟配置问题
- P* R+ l, L! _设备:stm32h735VGxxx, j f7 B' O6 T
, s) D4 n9 Y8 p2 u1 H u问题:STM32时钟配置不正确,一直没法启动。9 W" Z) @9 Q/ ?. ?! i, n2 R
* c; W1 _1 `: P/ v$ M) k
解决:原因是配置时钟的一些参数超出的范围,所以配置导致不成功。
% t- M5 s% |/ j6 M6 h
2 z; m3 q) B5 B _& _+ [6 g下面是H735的时钟的一些解释,其中 Fvco 是有范围的,这边从文档上面查到,FVCO的最大配置值为836M,所以配置的时候一定要注意。# ?) o) K: I5 [
并且plln, pllm,pllp,pllq,这些都是有范围限制的,如果配置错误,那么芯片将不会正常运行3 j, f9 r( Y. P( v j! l
9 j( C, z( q6 s% s U8 {- /*/ B! z+ l5 ?0 ?. K2 b/ m4 }' c
- * 时钟设置函数
$ g) G2 e: ?& z) P. \ - * Fvco: VCO 频率
1 j$ ^9 n; b3 P2 @/ k6 B0 l - * Fsys: 系统时钟频率,也是 PLL1 的 p 分频输出时钟频率5 n/ F4 F1 Q m* W( T; u
- * Fq: PLL1 的 q 分频输出时钟频率
" N" E9 h! M/ y - * Fs: PLL 输入时钟频率,可以是 HSI,CSI,HSE 等.
. X1 E+ |7 T/ Y- V8 S8 c0 e& B! W - * Fvco = Fs * (plln/pllm) = (Fs/DIVM)*DIVN;0 d) |$ @. S9 {- j5 y' w% L" A2 V
- * Fsys = Fvco/pllp = Fs * (plln/(pllm * pllp));2 K8 F" ^, c5 r7 y2 f; _% K
- * Fq = Fvco/pllq = Fs * (plln/(pllm * pllq));7 k' t& j& O0 i" M6 }
- * ; k# n& d) I; C7 S0 g, T) p6 B) N
- *
% x9 A2 b8 p4 ?4 q) H' r% K - * plln: PLL1 倍频系数(PLL 倍频),取值范围:4~512.
) z% p# C2 H# s, o - * pllm: PLL1 预分频系数(进 PLL 之前的分频),取值范围:2~56.! D5 m/ M! Y2 R
- * pllp: PLL1 的 p 分频系数(PLL 之后的分频),分频后作为系统时钟,取值范围:2~1282 l* j' n1 x2 g1 U5 ~
- * pllq: PLL1 的 q 分频系数(PLL 之后的分频),取值范围:1~128.1 ]' `: l8 F: b# M c' B6 N
- * CPU 频率(rcc_c_ck) = sys_d1cpre_ck = 400Mhz 8 B9 O2 D; n; Y \
- * rcc_aclk = rcc_hclk3 = 200Mhz4 K+ }8 X# {( n
- * AHB1/2/3/4(rcc_hclk1/2/3/4) = 200Mhz
) M1 H1 O: Z. r - * APB1/2/3/4(rcc_pclk1/2/3/4) = 100Mhz
4 f; U/ N Y# F* E+ R - * FMC 时钟频率 =pll2_r_ck=((25/25) * 512/2) = 256Mhz
& X; N# i9 @9 u4 \ - *
" ~% p# ]: Y! `' L, H P U9 F - * Example:: K+ g% a& P$ U9 {" |! f/ N$ B! B# H2 b3 {
- * 外部晶振为 25M 的时候,推荐值:plln = 160, pllm = 5, pllp = 2, pllq = 4.
# u( L: x5 ~1 w9 O" s) W6 {( g6 p( a1 | - * 得到:Fvco = 25 * (160/5) = 800Mhz5 }" \! e8 r# j* G5 U& X
- * Fsys = 800/2 = 400Mhz8 V* j, ^1 [% J' @% p2 ^
- * Fq = 800/4 = 200Mhz0 n1 b0 K1 E# A' L9 d1 b7 L
- */
" w7 I6 }& s+ f4 [; ]/ z - : T0 w- b! r7 b" d8 N7 _, c
- #define PLL_N 1604 i5 h! g- R) ~% |* x
- #define PLL_M 5, ^, z8 ?) s3 b- j7 k+ E( k
- #define PLL_P 2
7 c7 l1 G- p* i/ h9 n* Q* X3 }7 X - #define PLL_Q 47 \$ ~) n+ T2 j
- : {4 K V2 w$ Z& O
- // 时钟设置函数 C3 P5 } c1 D: o9 P' `3 q* v
- static void SystemClock_Config(void)
& B3 {9 x2 Y$ O7 v: T+ ^2 \) d- R - {% S& [ B! N. a) E
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
( k5 P5 j' @1 a6 z" F1 j% i - RCC_OscInitTypeDef RCC_OscInitStruct = {0};; _% C; ~, Y5 T& A
- HAL_StatusTypeDef ret = HAL_OK;+ S5 @: T/ @& j$ v" D% K
: G1 I" B' g' B, j# a1 F- Z- t- /*!< Supply configuration update enable */; P# J" K3 L2 }; x; F+ @, b& |
- HAL_PWREx_ConfigSupply(PWR_DIRECT_SMPS_SUPPLY);
3 t/ e1 o* K& w5 A9 X( N - 4 } R2 o/ B4 [* y) I
- /* The voltage scaling allows optimizing the power consumption when the device is
+ O; n: X0 G4 y0 q - clocked below the maximum system frequency, to update the voltage scaling value6 i* Q9 l$ S" p& y$ Z4 S0 |
- regarding system frequency refer to product datasheet. */9 o3 H2 R$ X0 a# L
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);
5 G0 v! R$ q5 r - * }: g! Q } d
- while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
9 ?4 v7 N- z& s* m) W' y6 ~- E' ~
# M! E$ b. L3 M. I: b- /* Enable HSE Oscillator and activate PLL with HSE as source */
* o( u' j* ^5 ~- _' A: b% t - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;$ F) T$ G/ P0 n
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;3 z) W7 J' z' ]4 { y
- RCC_OscInitStruct.HSIState = RCC_HSI_OFF;
: a# u( j# p, |3 \( B2 h - RCC_OscInitStruct.CSIState = RCC_CSI_OFF;7 X/ n% Q2 t% x8 r/ k4 R4 x5 @
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
/ T7 p' o' v% m# m - RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
3 n+ ^5 i S* Y. d. U6 j
( I5 K2 m0 h5 n# ]- RCC_OscInitStruct.PLL.PLLM = PLL_M;
0 h0 }9 i/ m g; n- \ - RCC_OscInitStruct.PLL.PLLN = PLL_N;
( J! w8 G# \" A [( h4 G7 |$ S, b - RCC_OscInitStruct.PLL.PLLFRACN = 0;- j0 p* e" _9 k2 e2 O# m, X
- RCC_OscInitStruct.PLL.PLLP = PLL_P;1 V0 r+ J. g. s5 U% W
- RCC_OscInitStruct.PLL.PLLR = 2;% D3 f n: h5 M* R& P( {+ W9 N8 q
- RCC_OscInitStruct.PLL.PLLQ = PLL_Q;5 G# }( k: n$ H. u4 w6 n8 ]! E
9 E: V% ^; C/ N2 a+ F- RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;9 ?, {& b9 Q. D9 ~! h
- RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_2;
) F1 d4 n3 w% F! e - ret = HAL_RCC_OscConfig(&RCC_OscInitStruct);( v, s* |3 @. M4 \+ s/ {$ K
- if(ret != HAL_OK)
. t! x1 U6 o+ Y2 u. \3 p - {8 Z+ q. U0 H5 w: q. B/ P4 [/ B& E
- while(1) {};
# g! M" O2 S9 K# g - }8 q4 R* X& E3 X7 w
: Q, Z, o, g+ T# {1 e: y- /* Select PLL as system clock source and configure bus clocks dividers */& q, u3 @2 R/ d
- RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_D1PCLK1 | RCC_CLOCKTYPE_PCLK1 | \3 @: Q! X: e$ i9 |6 K
- RCC_CLOCKTYPE_PCLK2 | RCC_CLOCKTYPE_D3PCLK1);% W' f, H+ F, b3 H
- : N0 E7 I" j. ^% w9 C, U0 C
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;$ l9 @; D1 {7 g: }( [
- RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
0 ]; f* i- b4 I, ~* K7 l M+ {: @6 Q - RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;8 s( i6 C- \! T& [3 A3 ~
- RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
m; M- N6 J* f3 u, Q - RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;( q! |4 t9 R& o' e- M6 ~
- RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
9 m- B! R9 A4 K+ J# [2 D - RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
2 k; Z. x& e7 W6 B+ b5 m - ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3);
( Q' Z0 v z/ S% N - if(ret != HAL_OK)/ T, P" ~8 f" Y4 D
- {- Y" S# Y2 Y9 W# m' U' w
- while(1) {};. D2 D* J3 A; D# G+ J; E
- }
, @% T1 a% W' V+ H6 \& q5 @' \ - v1 l; m3 ?. Q/ S* q
- }& r* k8 s: ~' h+ y4 C6 I
复制代码 # b P/ j1 q; T. M, [
$ {! p# i$ r1 C" r/ w! w+ X |