DMA 原理从 0 到 1:为什么它能解放 CPU?一文讲透 DMA 的工作流程
【福利三:雨露均沾·逢7狂欢】之五:STM32系统时钟PLL故障排除1例
【福利三:雨露均沾·逢7狂欢】之三:AI帮我查BUG
福利三:雨露均沾·逢7狂欢】之二:STM32F407VG串口通信乱码故障的排除
基于STM32F103的I2C主从机通信
OpenBLT移植到STM32F103战舰开发板上适用于所有STM32F103系列的Bootloader
TFT LCD 与 FSMC 的硬件连接大容量 STM32F10xxx FSMC 接口
单片机:初学者该了解的STM32F103基础知识
2025软件工具兔哥知道
STM32之继电器模块
微信公众号
手机版
分享一个
#ifndef RCC_HPP_% `3 ]- B, c$ S! G5 u( H$ |' m! V
#define RCC_HPP_, h& b/ r- `! ]/ ~+ J& b
#include "STM32F103.h"
3 Y6 V5 v+ \+ s" }! ?5 S8 s+ [. @3 t
namespace MCU
{
namespace STM32F103
{
struct RegisterRCC
{
volatile union: X% L5 [. y' u" S2 [( i/ b
{
volatile uint32_t Value;
" {1 O1 R* M6 y2 p0 b- C
volatile struct
{' n( {* j6 R; M( y
volatile uint32_t HSION : 1;6 D7 ^; s% ^5 u/ Q; ~3 w2 _" P" Y+ j
volatile const uint32_t HSIRDY : 1;7 j+ Q. U" z% b
volatile uint32_t RESERVED1 : 1;$ _3 L) o8 p# Q3 M, @! {4 G
volatile uint32_t HSITRIM : 5;( T9 w- H* a! w; P" T, d! v9 l
volatile const uint32_t HSICAL : 8;* R/ G7 ~( m9 ]5 M. _
volatile uint32_t HSEON : 1;6 c# C7 c3 r9 }9 s( A
volatile const uint32_t HSERDY : 1;- @/ m3 p7 r/ A- d
volatile uint32_t HSEBYP : 1;; R1 {1 z7 t7 P* e7 G2 D( D5 `
volatile uint32_t CSSON : 1;
volatile uint32_t RESERVDE2 : 4;8 q8 l8 [" J. z6 ?$ h
volatile uint32_t PLLON : 1;$ ^# h' B6 Q' D ]
volatile const uint32_t PLLRDY : 1;
volatile uint32_t RESERVDE3 : 6;
}Bit;
}CR;* S5 g" L! c ~1 t0 p
$ i' ?- V @4 o1 ]/ n( x
volatile union
{4 d3 ?9 p/ C5 F
volatile uint32_t Value;! G! U8 V" V0 W# @
volatile struct$ T2 i5 v- }* M1 q( A* V2 y
{
volatile uint32_t SW : 2;
volatile const uint32_t SWS : 2;7 r; l7 N3 D. z; Z/ `6 @6 o/ ?3 U
volatile uint32_t HPRE : 4;; f1 W- F7 D, Y2 w/ g$ U
volatile uint32_t PPRE1 : 3;. M/ `9 v& L" c' a& Q+ A
volatile uint32_t PPRE2 : 3;
volatile uint32_t ADCPRE : 2;
volatile uint32_t PLLSRC : 1;/ n6 D2 a" y: H! K. ~
volatile uint32_t PLLXTPRE : 1;- @! L0 W/ I+ b: v) E
volatile uint32_t PLLMUL : 4;0 R4 ]6 y8 i6 o4 ~
volatile uint32_t USBPRE : 1;
volatile uint32_t RESERVED1 : 1;' J9 ]! O/ d' x4 w
volatile uint32_t MCO : 3;6 E# B$ Z8 o g8 K6 j
volatile uint32_t RESERVED2 : 5;
}Bit;: `' n0 H5 n# N+ x% ]4 g
}CFGR;+ Z. c8 ~8 _2 |/ }2 l' t y* H3 F
volatile union
{
volatile uint32_t Value;
volatile struct6 j; f9 N6 T1 ~% r% o
{8 k" ] U( a0 A; [
volatile const uint32_t LSIRDYF : 1;$ b* J+ k' x) ~! Z& U! l
volatile const uint32_t LSERDYF : 1;( m- y9 Y9 J* B9 v7 {3 u7 C
volatile const uint32_t HSIRDYF : 1;4 l/ t7 z5 w$ O' N8 y/ t7 j+ c
volatile const uint32_t HSERDYF : 1;9 O! {2 D% t2 n3 K" N* m% q- R
volatile const uint32_t PLLRDYF : 1;5 d/ C% c: x1 l$ |( S
volatile const uint32_t PLL2RDYF : 1;
volatile const uint32_t PLL3RDYF : 1;. K2 t( y9 ]( k8 H% M) F- l: [
volatile const uint32_t CSSF : 1;
volatile uint32_t LSIRDYIE : 1;6 C5 x/ i% C: i, g% ~/ Y
volatile uint32_t LSERDYIE : 1;
volatile uint32_t HSIRDYIE : 1;
volatile uint32_t HSERDYIE : 1;' U; ~7 V$ J! v' `4 M7 j$ Q2 b9 t
volatile uint32_t PLLRDYIE : 1;
volatile const uint32_t PLL2RDYE : 1;, _+ G8 v; X5 d
volatile const uint32_t PLL3RDYE : 1;
volatile uint32_t RESERVED1 : 1;
volatile uint32_t LSIRDYC : 1;
volatile uint32_t LSERDYC : 1;. F' I) `, S) _" o% ?0 v
volatile uint32_t HISRDYC : 1;7 H( j3 R4 H$ A G- c: Q7 H% q
volatile uint32_t HSERDYC : 1;
volatile uint32_t PLLRDYC : 1;
volatile uint32_t PLL2RDYC : 1;
volatile uint32_t PLL3RDYC : 1;
volatile uint32_t CSSC : 1;2 _1 Y) S! E# l& i
volatile uint32_t RESERVED2 : 8;
}Bit;/ _+ y' \; s) N9 F; h; g& q
}CIR;
0 @3 m6 l8 d5 d- x
volatile union. Q3 g, f# _: c6 ^$ @
{4 w c3 B) a# {# e' B6 r: e
volatile uint32_t Value;
2 P9 c: K, \3 p# K$ o5 V
volatile struct
{& f) ?: b/ C3 U- M6 ]
volatile uint16_t AFIORST : 1;
volatile uint16_t RESERVED1 : 1;0 J( U( ~2 M6 o- |; R2 \
volatile uint16_t IOPARST : 1;& r( K5 m1 |9 @9 x/ z3 N
volatile uint16_t IOPBRST : 1;
volatile uint16_t IOPCRST : 1;
volatile uint16_t IOPDRST : 1;1 D' h" R: i, X; t2 R
volatile uint16_t IOPERST : 1;/ S; U6 F4 J" s7 e% ~2 {
volatile uint16_t IOPFRST : 1;
volatile uint16_t IOPGRST : 1;
volatile uint16_t ADC1RST : 1;
volatile uint16_t ADC2RST : 1;
volatile uint16_t TIM1RST : 1;. R' _+ s% x( v$ J, `* h2 m- g; M
volatile uint16_t SPI1RST : 1;
volatile uint16_t TIM8RST : 1;
volatile uint16_t USART1RST : 1;
volatile uint16_t ADC3RST : 1;
volatile uint16_t RESERVED2 : 16;
}Bit;- W2 A" S/ X0 }' ^" S
}APB2RSTR;
volatile union5 x' M- q, Y7 q* i
{
volatile uint32_t Value;
volatile struct
{
volatile uint32_t TIM2RST : 1;1 ^7 n f8 _% [# L7 m
volatile uint32_t TIM3RST : 1;
volatile uint32_t TIM4RST : 1;
volatile uint32_t TIM5RST : 1;& @/ `+ ~* G! k+ z, V+ {
volatile uint32_t TIM6RST : 1;
volatile uint32_t TIM7RST : 1;. t6 q* ]$ j* x7 w5 g( ~1 Y
volatile uint32_t RESERVED1 : 2;) ~% y* w* X5 G4 `
volatile uint32_t RESERVED2 : 3;8 y' Y8 O: \6 M
volatile uint32_t WWDGRST : 1;; V- R- |0 y, z: `. N+ X* A, b, n. R
volatile uint32_t RESERVED3 : 2;( |; i, r' c3 V. Z4 _( J# X8 b
volatile uint32_t SPI2RST : 1;" U: @ k& B3 I. X5 q4 F
volatile uint32_t SPI3RST : 1;: h/ I/ y5 u: X% |5 J! J r( w
volatile uint32_t RESERVED4 : 1;
volatile uint32_t USART2RST : 1;
volatile uint32_t USART3RST : 1;
volatile uint32_t USART4RST : 1;
volatile uint32_t USART5RST : 1;3 u% L4 n: N7 w5 E! n
volatile uint32_t I2C1RST : 1; O R* j9 s& v2 Y; ~
volatile uint32_t I2C2RST : 1;. `' Z8 S( Y0 e4 M
volatile uint32_t USBRST : 1;
volatile uint32_t RESERVED5 : 1;( d# k- V6 [1 @0 H& g' \
volatile uint32_t CANRST : 1;
volatile uint32_t RESERVED6 : 1;
volatile uint32_t BKPRST : 1;
volatile uint32_t PWRRST : 1;
volatile uint32_t DACRST : 1;
volatile uint32_t RESERVED7 : 2;; Q- R2 O7 Z$ [* x8 l* j
}Bit;
}APB1RSTR;
4 h) v2 q9 L+ o) J' Z y1 S4 p" I$ H
volatile union2 M( E7 J* i( D# {
{% K/ s, n0 W# s5 h% A
volatile uint32_t Value;2 m* z9 f! }6 q# [( F0 D7 r1 p( A
+ r# g) S4 ]% |) i
volatile struct5 Z( G( G+ S1 q
{
volatile uint16_t DMA1EN : 1;
volatile uint16_t DMA2EN : 1;
volatile uint16_t SRAMEN : 1;& o% w, `; c1 J. J3 d2 p+ }
volatile uint16_t RESERVED1 : 1;, M u) x4 }7 U: L% A0 D% ?
volatile uint16_t FLITFEN : 1;
volatile uint16_t RESERVED2 : 1;1 `2 }) M# B4 ^( X
volatile uint16_t CRCEN : 1;
volatile uint16_t RESERVED3 : 1;# w5 i* a! g- E% u7 a: v
volatile uint16_t FSMCEN : 1;
volatile uint16_t RESERVED4 : 1;
volatile uint16_t SDIOEN : 1;
volatile uint16_t RESERVED5 : 5;
volatile uint16_t RESERVED6 : 16;
}Bit;% g) V/ y1 k7 n
}AHBENR;
volatile union
{" d( }$ x4 f3 S+ u2 ]/ ^
volatile uint32_t Value;
. O9 R3 Y. d. K; }* [
volatile struct
{
volatile uint16_t AFIOEN : 1;
volatile uint16_t RESERVED1 : 1;
volatile uint16_t IOPAEN : 1;
volatile uint16_t IOPBEN : 1;, h% O O3 Q9 s. y. y
volatile uint16_t IOPCEN : 1;3 J x" R( r8 C& j; i
volatile uint16_t IOPDEN : 1;
volatile uint16_t IOPEEN : 1;
volatile uint16_t IOPFEN : 1;% d9 {2 _+ ~0 D- _6 h
volatile uint16_t IOPGEN : 1;
volatile uint16_t ADC1EN : 1;+ K( c, m+ X4 N! {
volatile uint16_t ADC2EN : 1;
volatile uint16_t TIM1EN : 1;
volatile uint16_t SPI1EN : 1;% S7 T% ]( O! P+ R; j P+ O
volatile uint16_t TIM8EN : 1;% i0 ~9 _( i7 o0 I5 v& _2 d
volatile uint16_t USART1EN : 1;
volatile uint16_t ADC3EN : 1;% ~8 z( M& |1 _1 n
volatile uint16_t RESERVED2 : 16;2 b1 U, |# {1 y$ h8 Z! w P: Z+ `& u
}Bit;5 p* P; ~5 v, F. r1 k( O
}APB2ENR;
volatile union
{( P6 C4 { a4 x1 k! z
volatile uint32_t Value;, d* N& P8 Y( l. q _/ c
volatile struct
{
volatile uint32_t TIM2EN : 1;
volatile uint32_t TIM3EN : 1;4 x6 [7 B* @" i0 A6 X
volatile uint32_t TIM4EN : 1;
volatile uint32_t TIM5EN : 1;
volatile uint32_t TIM6EN : 1;6 X k6 d: @6 l( f2 ^: \
volatile uint32_t TIM7EN : 1;
volatile uint32_t RESERVED1 : 2;
volatile uint32_t RESERVED2 : 3;
volatile uint32_t WWDGEN : 1;8 e$ }% Y3 i3 X+ S
volatile uint32_t RESERVED3 : 2;
volatile uint32_t SPI2EN : 1;4 @# G( `. m- W/ ]4 R* {
volatile uint32_t SPI3EN : 1;
volatile uint32_t RESERVED4 : 1;- F: x; }& V& V2 W4 T$ W M
volatile uint32_t USART2EN : 1;
volatile uint32_t USART3EN : 1;6 z ~$ v. H3 z* Q" V" \* I
volatile uint32_t USART4EN : 1;
volatile uint32_t USART5EN : 1;: k7 m0 F( w' Y: X9 X
volatile uint32_t I2C1EN : 1;3 z9 T( l4 N) w6 M& `
volatile uint32_t I2C2EN : 1;8 E( W! R6 L6 o1 j/ N/ s
volatile uint32_t USBEN : 1;
volatile uint32_t RESERVED5 : 1;# ~$ ?$ v. D0 ~1 w5 l3 v+ q
volatile uint32_t CANEN : 1;( J o( }2 B; k9 U
volatile uint32_t RESERVED6 : 1;4 P; B" p' x4 |2 B. E; l
volatile uint32_t BKPEN : 1;: Z, P- b) x; i) [
volatile uint32_t PWREN : 1;8 N6 u4 H: X$ u0 ^' q( n E
volatile uint32_t DACEN : 1;
volatile uint32_t RESERVED7 : 2;
}Bit;
}APB1ENR;
6 r( ^* B( i7 F( y7 U* Y
volatile union
{1 y9 n# v1 R; c m0 \9 E4 a
volatile uint32_t Value;
f; O! s: `0 R4 }' Q/ R( o9 n$ m
volatile struct
{
volatile uint32_t LSEON : 1;
volatile const uint32_t LSERDY : 1;
volatile uint32_t LSEBYP : 1;. }: l- d/ y U' I1 a. m
volatile uint32_t RESERVED1 : 5;7 R2 X+ z5 s, ? e
volatile uint32_t RTCSEL : 2;8 E& [1 D) v0 H: j* Y9 p2 j0 W
volatile uint32_t RESERVED2 : 5;
volatile uint32_t RTCEN : 1;
volatile uint32_t BDRST : 1;/ a" X: _9 k5 P6 a- v
volatile uint32_t RESERVED3 : 7;
volatile uint32_t RESERVED4 : 8;
}Bit;8 s E/ }' i. E: _
}BDCR;* c1 y7 \$ E0 y0 R* A1 _0 _
volatile union
{
volatile uint32_t Value;$ k2 Q6 P& ]9 V: I! Y* k
" t+ q( W3 k" B" N/ S- q
volatile struct7 { m* K8 q: g
{
volatile uint32_t LSION : 1;' ^: P8 t1 [2 ]* A; |0 O
volatile const uint32_t LSIRDY : 1;" M. R: N2 y v4 V9 w
volatile uint32_t RESERVED1 : 6;0 S- J4 L* f1 j. R4 w
volatile uint32_t RESERVED2 : 8;
volatile uint32_t RESERVED3 : 8;! y' R1 P! V/ L# K) F- J
volatile uint32_t RMVF : 1;+ a! I9 s$ x# k! }& Y1 Y$ {
volatile uint32_t RESERVED4 : 1;
volatile uint32_t PINRSTF : 1;
volatile uint32_t PORRSTF : 1;+ Y3 k0 R* o% S! E$ F
volatile uint32_t SFTRSTF : 1;, e: F) b% q* H2 H
volatile uint32_t IWDGRSTF : 1; t# w) F5 O5 @/ a5 d6 T% j- x
volatile uint32_t WWDGRSTF : 1;
volatile uint32_t LPWRRSTF : 1;% W% `/ G# M1 v6 J0 s% i% f
}Bit;, ^+ T9 L$ E6 D! O, O: z& B O
}CSR;
};7 b+ U( w9 Z( G2 z
C7 h4 o6 ?. Y/ P0 e0 R
enum RCC_PLL_MUL
{6 |( B: @- n' G7 b" u5 R8 D4 C
RCC_PLL_MUL2 = 0b0000,
RCC_PLL_MUL3 = 0b0001,
RCC_PLL_MUL4 = 0b0010,, ^8 `$ Y. B! |
RCC_PLL_MUL5 = 0b0011,
RCC_PLL_MUL6 = 0b0100,
RCC_PLL_MUL7 = 0b0101,
RCC_PLL_MUL8 = 0b0110,1 V' S/ ^, z+ Z2 Q- |& L3 A
RCC_PLL_MUL9 = 0b0111,$ y5 s6 }& ?$ q, S3 E- u! m
RCC_PLL_MUL10 = 0b1000,
RCC_PLL_MUL11 = 0b1001,4 b$ U+ U$ k+ M
RCC_PLL_MUL12 = 0b1010,
RCC_PLL_MUL13 = 0b1011,4 v" ?* @* [6 W2 [4 |8 d: q) b
RCC_PLL_MUL14 = 0b1100,
RCC_PLL_MUL15 = 0b1101,
RCC_PLL_MUL16 = 0b1110,- f; e6 T7 K& r; l0 B; c
};
: \# G3 \8 j T2 _
enum RCC_PLL_XTPRE
{
RCC_PLL_XTPRE_DIV1 = 0b0,3 }6 H' R/ z, \9 P) b* z1 H& Q
RCC_PLL_XTPRE_DIV2 = 0b1,
};
) Z% D$ n: \, c y% I/ j
enum RCC_PLL_SRC
{ l% {$ m& ~( e6 p* ^* `
RCC_PLL_SRC_HSI_DIV2 = 0b0,
RCC_PLL_SRC_HSE = 0b1,4 g$ \) X+ R! @# Q
};. C: ?, K3 Y/ z9 F+ I
8 R" P2 m; o% s
enum RCC_HPRE0 U- H& X( i1 ?! a* N- E2 d$ g
{1 x2 G7 o: q' P) w" ~ p' j7 H( r" y+ X
RCC_HPRE_DIV1 = 0b0000,
RCC_HPRE_DIV2 = 0b1000,( z8 L8 y7 r& _# S6 q$ |
RCC_HPRE_DIV4 = 0b1001, Q3 n, @* E8 p8 D: N
RCC_HPRE_DIV8 = 0b1010,
RCC_HPRE_DIV16 = 0b1011,
RCC_HPRE_DIV64 = 0b1100,+ } Y: O% K' l$ y6 E8 u# N
RCC_HPRE_DIV128 = 0b1101,
RCC_HPRE_DIV256 = 0b1110,1 Q+ K0 z8 D) X
RCC_HPRE_DIV512 = 0b1111,
};
; W6 y& p4 N6 w, l$ Z! r
enum RCC_PPRE
{
RCC_PPRE_DIV1 = 0b000,
RCC_PPRE_DIV2 = 0b100,# Y9 v) n: z0 U# }+ d+ B, E
RCC_PPRE_DIV4 = 0b101,
RCC_PPRE_DIV8 = 0b110,( f5 j" y2 H+ H, s. f( r% _
RCC_PPRE_DIV16 = 0b111,
};
3 s( S2 O3 K! d( L3 d0 i
enum RCC_SW
{+ t0 E3 s, F# M" h) b
RCC_SW_HSI = 0b00,0 Q8 H, \9 \" h E& F( G+ d
RCC_SW_HSE = 0b01,& i7 Y e: {! x) }3 ~2 i& H
RCC_SW_PLL = 0b10,
};
! ~! v3 j; v& k1 c/ m/ `' ?
class CRCC
{
public:
//保存外部晶体频率 在EnableHSE方法中初始化 用于计算系统频率
static uint32_t HSE;
! ?7 F1 Y' r5 u* m6 P" H
volatile struct RegisterRCC *pRegister = (volatile struct RegisterRCC *)(RCC_BASE);& B8 L2 l4 @6 a8 G
( k# ?# W, A/ j6 U9 s% ~
//开启外部高速时钟并等待时钟就绪,如果时钟不能就绪则一直等待- M, y. d: m7 z7 W
//HSE:晶体频率 单位:Hz
void EnableHSE(uint32_t HSE)6 @! n7 {+ U) V# w/ {, v7 f
{ Q7 D( O; Q: m o6 g/ A
CRCC::HSE = HSE;
this->pRegister->CR.Bit.HSEON = 1;7 |/ e# H3 s( U6 Z9 f
while(!this->pRegister->CR.Bit.HSERDY);' ~$ N; d/ r$ h: X
}
//设置PLL参数,必须在PLL关闭的状态下1 \+ P* r6 Q: O
void SetPLL(RCC_PLL_MUL MUL, RCC_PLL_XTPRE XTPRE, RCC_PLL_SRC SRC)# j6 X3 `: ]' h
{
this->pRegister->CFGR.Bit.PLLMUL = MUL;2 x0 r9 H, T5 _; V
this->pRegister->CFGR.Bit.PLLXTPRE = XTPRE;
this->pRegister->CFGR.Bit.PLLSRC = SRC;
}
3 E8 q* }8 n4 l( M
//开启PLL时钟并等待时钟就绪,如果时钟不能就绪则一直等待5 J* n# k6 b2 j' D
void EnablePLL(void)
{
this->pRegister->CR.Bit.PLLON = 1;. v" Y( b( B' M5 g1 k
while(!this->pRegister->CR.Bit.PLLRDY);2 @; V$ w( G& U- X2 Q9 {; [9 e# W
}( L3 r& u2 f8 p8 v$ P- {
% ^! S) w8 t5 v% e7 `. @
//切换系统时钟源并等待切换就绪,如果切换不能就绪则一直等待7 W# i- ]) y# k# F
void SetSysClkSource(RCC_SW SW)0 ^6 w& D9 ]) d
{
this->pRegister->CFGR.Bit.SW = SW;1 a* B) P. H/ T
while(this->pRegister->CFGR.Bit.SWS != SW);+ U' T- h& V' z+ B
}+ p B* }/ Q2 D+ y! L
//获取系统时钟频率 单位:Hz/ ~3 H w- r1 y
uint32_t GetSYSCLK(void)
{
if(this->pRegister->CFGR.Bit.SWS == RCC_SW_HSI)
{ [% P( h+ D/ L3 w2 q* y
//系统时钟为HSI: s! o# } y! G4 ]( S# ^ T9 n
return 8000000;
}
else if(this->pRegister->CFGR.Bit.SWS == RCC_SW_HSE)' H6 L" _ W, d! |2 e; s& O
{
//系统时钟为HSE
return CRCC::HSE;; G# A7 L# N+ j) N5 X# _( M3 G ^* T9 x
}
else if(this->pRegister->CFGR.Bit.SWS == RCC_SW_PLL)
{6 d$ ~( s0 p8 Y; y! d2 m
//系统时钟为PLL" J2 c1 m8 r3 ~! {5 Z
uint32_t SRC, XTPRE, MUL;
if(this->pRegister->CFGR.Bit.PLLSRC == RCC_PLL_SRC_HSI_DIV2)3 v" ?) J8 F! f/ z
{
//PPL输入时钟源为HSI/25 n, F/ S' c3 n2 v
SRC = 8000000;
XTPRE = 2;
}
else
{
//PLL输入时钟源为HSE x1 z1 d; w3 B& [
SRC = CRCC::HSE;5 o- M a2 S! B9 R& t. |# w, D
if(this->pRegister->CFGR.Bit.PLLXTPRE == RCC_PLL_XTPRE_DIV1)! Q0 v. ?' T1 `5 m O8 G8 I
{
//PLL输入时钟源为HSE/2
XTPRE = 1;+ S" x6 R: I& m( a6 ~
}
else7 l- d. B. j; S$ b' F8 p. r
{: c5 S1 i* _/ I6 |! o9 B, j8 z' H
//PLL输入时钟源为HSE/2
XTPRE = 2;
}! c0 J V6 o, e; X- m
}9 U2 q% z7 }% ~7 p
switch((uint32_t)this->pRegister->CFGR.Bit.PLLMUL)
{
case RCC_PLL_MUL2: MUL = 2; break;& _( ]# ]* y2 }: c
case RCC_PLL_MUL3: MUL = 3; break;
case RCC_PLL_MUL4: MUL = 4; break;
case RCC_PLL_MUL5: MUL = 5; break;
case RCC_PLL_MUL6: MUL = 6; break;
case RCC_PLL_MUL7: MUL = 7; break;
case RCC_PLL_MUL8: MUL = 8; break;, m2 g8 F5 ], \$ H
case RCC_PLL_MUL9: MUL = 9; break;2 z$ H1 ^9 S1 d; O- J1 y
case RCC_PLL_MUL10: MUL = 10; break;
case RCC_PLL_MUL11: MUL = 11; break;; f' r( u) G( O' F- k
case RCC_PLL_MUL12: MUL = 12; break; i8 c% z+ q( E9 |8 ]4 q
case RCC_PLL_MUL13: MUL = 13; break;1 C# B1 D1 B! B+ Q* [. G6 ^* F
case RCC_PLL_MUL14: MUL = 14; break;
case RCC_PLL_MUL15: MUL = 15; break;
case RCC_PLL_MUL16: MUL = 16; break;
default: MUL = 16; break;
}
return SRC / XTPRE * MUL;
}
else
return 8000000;
}* g5 o0 F7 f# {( c9 c0 e. }
//获取AHB总线频率 单位:Hz
uint32_t GetHCLK(void)& }, V( o2 R" R. k5 j- i
{
uint32_t Prescaler;, r5 C( J H+ [) R* R5 R/ ^3 Y" c
9 \- C- m% E' ~' o4 }$ \4 N/ z
switch((uint32_t)this->pRegister->CFGR.Bit.HPRE) d2 Y1 c* v2 B
{4 e1 x5 F! H4 i7 Z' N: y
case RCC_HPRE_DIV1: Prescaler = 1; break;
case RCC_HPRE_DIV2: Prescaler = 2; break;
case RCC_HPRE_DIV4: Prescaler = 4; break;
case RCC_HPRE_DIV8: Prescaler = 8; break;
case RCC_HPRE_DIV16: Prescaler = 16; break;, J" I% L' Z- D9 |, w; q0 N/ ]1 m, j
case RCC_HPRE_DIV64: Prescaler = 64; break;3 Q/ x, |- [! l! _
case RCC_HPRE_DIV128: Prescaler = 128; break;
case RCC_HPRE_DIV256: Prescaler = 256; break; O2 `; X* r, p% T4 U
case RCC_HPRE_DIV512: Prescaler = 512; break;
default: Prescaler = 1; break; Z7 N7 M4 V7 e* k
}
2 t: N6 A- Q* X" X$ u% X
return this->GetSYSCLK() / Prescaler;
}1 Q- p. w6 M8 X! J) n
% H9 @; V' T4 U, [, ]; ~% d+ E
//获取APB1总线频率 单位:Hz7 O, ?! s# w" @- n
uint32_t GetPCLK1(void)' a0 b# `& e B: ?
{
uint32_t Prescaler;5 H- ^) A1 S" f9 X- h4 P
z( y/ D4 T& H
switch((uint32_t)this->pRegister->CFGR.Bit.PPRE1)
{
case RCC_PPRE_DIV1: Prescaler = 1; break;
case RCC_PPRE_DIV2: Prescaler = 2; break;
case RCC_PPRE_DIV4: Prescaler = 4; break;% G$ H5 D: m8 T8 ?1 w
case RCC_PPRE_DIV8: Prescaler = 8; break;
case RCC_PPRE_DIV16: Prescaler = 16; break;1 }( h& ?6 g/ G& _, T5 Y3 @
default: Prescaler = 1; break;
}* Q1 C" C9 B, D2 `# s% j
return this->GetHCLK() / Prescaler; q' R) z3 L) T3 Z) x9 l) I
}: N; H) ~; K$ u. t @
//获取APB2总线频率 单位:Hz
uint32_t GetPCLK2(void)( w/ n' p- ]) }( `' B, [5 g$ m
{6 J& |8 S Q( V
uint32_t Prescaler;& O; x+ }% L9 c/ U! r- S8 B
switch((uint32_t)this->pRegister->CFGR.Bit.PPRE2)7 U9 q3 X4 L& I- Y, I# E+ U
{9 C2 T6 {" |6 N$ x+ ^0 t
case RCC_PPRE_DIV1: Prescaler = 1; break;2 i Y! b1 J/ f+ d9 C" l
case RCC_PPRE_DIV2: Prescaler = 2; break;
case RCC_PPRE_DIV4: Prescaler = 4; break;
case RCC_PPRE_DIV8: Prescaler = 8; break;* T: u- u. w0 g; ?6 M
case RCC_PPRE_DIV16: Prescaler = 16; break;1 Y. W) F1 ?+ [( ~
default: Prescaler = 1; break;
}' p/ j$ N) n& c" G6 l
return this->GetHCLK() / Prescaler;
}
};* T" {( a" V+ L
uint32_t CRCC::HSE = 0;2 p; x+ F) M% R Y
}" M# S% A( W; f3 G& h7 j9 i; ^
}
#endif // !RCC_HPP_5 b) D4 O' Y i
3 o( v* s: u7 u. v! N( I+ x: D3 Z
这是对RCC的封装,其实对寄存器的封装只是C++写单片机的小问题.还有几个问题要解决.: J. [- q8 d) _: O- e; p- O+ j
第一:如何把中断连接到任何对象的方法,这个问题本质就是如何利用指针调用对象方法,实现后可以更好的发挥面向对象的多态思路,实现一些高灵活度的编程.我用模板类实现了一个类似C#中的委托对象,然后利用这个对象还可以实现类似C#中的事件,完成消息的多播.当然这是需要付出代价的,这个模板类要用到虚函数表,经过实测这个消耗是可以接受的,多1us左右吧.这个模板类的实现具体可以用Bing国际版搜索FastDelegate./ s) i( `$ h% f6 s
第二:如何new和delete,这个必须通过OS实现.我使用FreeRTOS,然后全局重载new和delete运算符到pvPortMalloc和pvPortFree.不过能定义声明的对象尽量定义声明,不要频繁new和delete因为FreeRTOS的内存管理还是很弱鸡的.实现的目的只是为了提高程序的灵活度.让一些代码可以更快地适配一些硬件.我通常都是只new不delete/ d- M! @6 y4 G7 @
6 v+ S3 p+ s1 u; ?$ p
为了更好更快地更换MCU,应该定义一个HAL层,里面全是虚基类,抽象类似但又不同的硬件.然后顶层基于HAL开发,底层继承HAL的虚基类并做具体实现.不过虚基类又牵涉虚函数表,会有性能损失.在对性能要求高的地方还是针对不同的硬件进行直接控制.
6 f; x4 [+ ~; x* l3 j7 y" k
为了更好的进行C++编程,我用gcc编译器.使用这个编译器时一定要注意,编译优化可能导致运行结果不正确,我通常是用O2或Os,一些特殊的函数还需要标记为O0.开发环境是Visual Studio 2019(个人版 免费) + VisualGDB(我买了正版).VisualGDB V5.5R2这个版本真是超级好用,自动完成,代码着色,代码格式化都是杠杠的.这个环境中在线仿真是ST-Link 2 + OpenOCD,可以设置端点,可以实时读取寄存器值,等等.# ~2 G' E4 x5 \# B# }4 V; c3 m& j8 O
总之,C++配上这个开发环境生产力大大提高,代码的可复用性也大大提高.另外,我的项目已经投产了.0 `8 s# ?+ f; @
) ~3 K6 c) K' T( N$ v* X
/ c5 _8 |! s, e/ u# S
8 i% f. M1 z' \
还更新吗,能出一期环境搭建的教程吗,配一个Demo工程