01. USART固件库概述3 B3 X/ N! q2 F. ]9 B
stm32f4xx_usart.h 函数的声明和类型的声明
7 ~( K% G1 F: _4 p9 V/ J
: f! K/ m" R, d( G7 istm32f4xx_usart.c 函数的实现
% Y+ R+ f+ `, J- }2 e' N0 n/ o; R% J
02. USART相关类型- s; m/ R1 h! C4 e
stm32f4xx_usart.h文件中& o- S, c" P9 y' P O
, K( a# I/ W% s, a/ k# [USART_InitTypeDef类型% n" `; ~+ c- E% F7 i! W
- E" f& q& ~9 W
$ i+ r* z. U; x* n8 d5 s. o% X- /**
' p, p. J# P4 H6 e - * @brief USART Init Structure definition
+ b J) @7 b! o( _" z - */ : e% ~ `' T: x( b9 h0 l
7 [, i* y4 M p" ?- typedef struct8 ]: ^ Q; h3 k9 D d
- {8 |6 r5 |/ G, J9 M( }! Z- R, y
- uint32_t USART_BaudRate; /*!< This member configures the USART communication baud rate./ d. M4 ~; o/ u6 ]' |
- The baud rate is computed using the following formula:* \. T1 o4 W6 |) {: l
- - IntegerDivider = ((PCLKx) / (8 * (OVR8+1) * (USART_InitStruct->USART_BaudRate)))1 J# `; \, j2 K9 W
- - FractionalDivider = ((IntegerDivider - ((u32) IntegerDivider)) * 8 * (OVR8+1)) + 0.5 - @) T) o$ B" r9 e( E
- Where OVR8 is the "oversampling by 8 mode" configuration bit in the CR1 register. */0 S1 G) X) Q8 f. @5 n( @
- $ H( v5 y4 ]) h" Z& \" V
- uint16_t USART_WordLength; /*!< Specifies the number of data bits transmitted or received in a frame.
5 T" ]0 w4 N9 O - This parameter can be a value of @ref USART_Word_Length */
$ u4 [3 b. ? c4 t2 W1 a - # Q$ Q& t) T% v( p) k
- uint16_t USART_StopBits; /*!< Specifies the number of stop bits transmitted.
! X9 q/ v7 A3 @5 S, i - This parameter can be a value of @ref USART_Stop_Bits *// l+ E8 m/ G4 n j" A5 D
- + A) {6 L1 U* i8 }" z* v% t. G
- uint16_t USART_Parity; /*!< Specifies the parity mode./ ?; _7 B+ {+ p5 } P* @
- This parameter can be a value of @ref USART_Parity
# g _/ K% y! X: l! J1 ~' E - @note When parity is enabled, the computed parity is inserted) y- s" J3 S( I3 ]; f/ n
- at the MSB position of the transmitted data (9th bit when
: t1 L& n4 ~+ G6 m9 R - the word length is set to 9 data bits; 8th bit when the
/ p( ]; T: u; p: e1 y/ x. k* O - word length is set to 8 data bits). */
) s' M. @% ^* V9 K
& y& y$ J* ~7 E/ V- uint16_t USART_Mode; /*!< Specifies whether the Receive or Transmit mode is enabled or disabled.4 b0 v$ K# V# g4 \" s2 i* x
- This parameter can be a value of @ref USART_Mode */$ l8 L2 O% G" S* ^' {) _; [/ N
- $ |0 b) j5 ]' m. o
- uint16_t USART_HardwareFlowControl; /*!< Specifies wether the hardware flow control mode is enabled% i" T: \1 D! J! w; T6 [9 x. K
- or disabled.8 N; I v, C' k
- This parameter can be a value of @ref USART_Hardware_Flow_Control */
5 W' _& b$ L9 H - } USART_InitTypeDef;
: a8 |+ s0 e+ d$ X1 b
复制代码
+ G# L5 o% d9 i* LUSART_ClockInitTypeDef类型0 D0 C, l$ B/ S: c, k8 x* u2 z
( `9 `9 A5 g4 U3 B- /** 8 E* E/ k& a* O+ V5 ~& z
- * @brief USART Clock Init Structure definition
! q& t4 l; e) o! M f* ~9 m) n" k: C - */ 8 U! l# D# h' n
- d" i+ B& o6 ?4 T
- typedef struct
% _/ ]1 N* a# A* V' X7 Q - {
/ \: a5 j3 X/ O$ ]# y - * B- N. n# |% d
- uint16_t USART_Clock; /*!< Specifies whether the USART clock is enabled or disabled.
& Y; J, ^5 T% X2 `1 v# D - This parameter can be a value of @ref USART_Clock */, ^& q7 `; h' H3 m
7 V. n' n& R. S- uint16_t USART_CPOL; /*!< Specifies the steady state of the serial clock.: Y T6 I8 w/ |7 C' G
- This parameter can be a value of @ref USART_Clock_Polarity */, Y* _5 c, k `) T/ `$ A( X6 R
( B% ?2 \- H* q0 o0 c t- N8 ?$ M1 Q- r: ]- uint16_t USART_CPHA; /*!< Specifies the clock transition on which the bit capture is made.
- @) F3 e/ L. N3 e( t - This parameter can be a value of @ref USART_Clock_Phase */9 H& E" |+ S& |( m, q |/ s
- 5 P* u7 r) J% D3 v# K: Q U# ?
- uint16_t USART_LastBit; /*!< Specifies whether the clock pulse corresponding to the last transmitted) x; }$ J' X+ w/ a2 s+ A
- data bit (MSB) has to be output on the SCLK pin in synchronous mode.
8 Y4 ~7 I8 q( J9 T# _; ~ i: L. c - This parameter can be a value of @ref USART_Last_Bit */
6 c, T' j# ^& g7 {* R( ?; s - } USART_ClockInitTypeDef;
复制代码 % T! E1 H$ B* F' o
USART外设
2 z$ R8 d7 | _; L9 }. l u
/ `& F1 Y3 A( W3 J) A3 v- /** @defgroup USART_Exported_Constants
8 H! f+ L6 y; u. Y5 d( O' K - * @{, q0 k0 @3 z# |8 `& _
- */
. ?+ X6 u+ K4 m- p, S% X - ( ` p/ l' {8 h- B" u: @
- #define IS_USART_ALL_PERIPH(PERIPH) (((PERIPH) == USART1) || \' [9 q g2 x! }- s1 y: Q6 G
- ((PERIPH) == USART2) || \, ^+ Y7 }3 A7 B) A" E7 Z
- ((PERIPH) == USART3) || \
0 e# |- l8 {6 D; J- z9 v5 ^ m - ((PERIPH) == UART4) || \5 H4 P1 D: |( o7 {
- ((PERIPH) == UART5) || \
& M m; ?7 J3 e0 p _ - ((PERIPH) == USART6) || \* ]) ? s8 s& P$ Q
- ((PERIPH) == UART7) || \
& H: A( A+ q" P* b2 _, L5 B4 ] - ((PERIPH) == UART8) || \
1 g$ [) V7 G) U3 _ - ((PERIPH) == UART9) || \
+ h9 ?4 L3 b6 ], p - ((PERIPH) == UART10))
% r* E; H4 r8 R5 B6 ?$ [
r3 q2 {- ~' S- #define IS_USART_1236_PERIPH(PERIPH) (((PERIPH) == USART1) || \
2 L% W! w) [$ m2 D3 O+ U8 G5 | - ((PERIPH) == USART2) || \
3 a: d$ s7 Q5 j: M# j! t& _ - ((PERIPH) == USART3) || \2 x: n: `' L) K! m% @. c* j
- ((PERIPH) == USART6))8 S: P8 s8 O% u, w
复制代码
8 C' [8 e6 d6 |" J- q0 aUSART_Word_Length) \2 L( h4 |( Q. z. G. m$ [
7 x! H) r. X5 A& {8 W& A" V a
: v3 m5 _6 ^; G4 B. i6 {- /** @defgroup USART_Word_Length
% ~9 z7 w: V2 a2 n. l0 X2 \ - * @{# l" N' k: _. n) U/ S: Z0 H
- */ + V* P3 V8 V' Y4 q# {! {
3 B$ u$ j! T, t- D9 }- #define USART_WordLength_8b ((uint16_t)0x0000)
# V/ K1 T$ C& q* D: l" X - #define USART_WordLength_9b ((uint16_t)0x1000)8 K5 c! W4 y5 ^
- $ j# [ s, h5 i1 x
- #define IS_USART_WORD_LENGTH(LENGTH) (((LENGTH) == USART_WordLength_8b) || \
, b# H) [# H( D7 b5 m$ B: w, A - ((LENGTH) == USART_WordLength_9b))
复制代码 & U' f( L- F0 l: Q
USART_Stop_Bits7 u* p3 p8 e# v3 m6 _% _* a5 i8 U
6 W- D6 s8 o3 o- /** @defgroup USART_Stop_Bits
# F1 P5 |* M5 S0 b! `1 u$ o - * @{
, G1 p% d* D* Y. o- t& U( H - */
; ~8 c! v2 Z2 p/ f- X; F - ' s& J8 Y: [4 e4 Q, _+ h
- #define USART_StopBits_1 ((uint16_t)0x0000), u5 J4 g$ [& F _& U) l- A4 E& c0 u
- #define USART_StopBits_0_5 ((uint16_t)0x1000)( H+ U* i4 V4 K- U4 B5 z( _
- #define USART_StopBits_2 ((uint16_t)0x2000). q/ X& \) }2 h2 h8 S6 _# _( v3 f
- #define USART_StopBits_1_5 ((uint16_t)0x3000)9 L3 d5 N( b2 w9 ^. |0 d; ?
- #define IS_USART_STOPBITS(STOPBITS) (((STOPBITS) == USART_StopBits_1) || \
5 ?3 {' ?3 h4 R7 { - ((STOPBITS) == USART_StopBits_0_5) || \
# P1 l# Q$ T; X0 r$ S' a! ^ - ((STOPBITS) == USART_StopBits_2) || \/ ?5 p4 U8 S4 `
- ((STOPBITS) == USART_StopBits_1_5))9 Z3 p: o0 t; A& \" ~
复制代码
' o* J, x% F4 J8 R) F4 lUSART_Parity
c' }- E* ?: z$ a) ^* U6 w2 Q: O! T, A, ^& m1 r+ n
- /** @defgroup USART_Parity 5 ~/ s% e8 {% J" }) n
- * @{6 I1 S7 x6 w" r0 x0 f
- */
, y7 C; H4 Q7 z6 | Y2 v. I - : h1 u+ ~. G% q* {' A5 e7 {6 {
- #define USART_Parity_No ((uint16_t)0x0000)
$ r1 [. }. c# w2 p5 u) @( d A - #define USART_Parity_Even ((uint16_t)0x0400)
! \, u; Q6 E" z" s; ?! ?' `4 U* c - #define USART_Parity_Odd ((uint16_t)0x0600)
- Q% L% g6 {& e: S - #define IS_USART_PARITY(PARITY) (((PARITY) == USART_Parity_No) || \3 G4 V% |! K; M) L3 ]; l
- ((PARITY) == USART_Parity_Even) || \3 G& f# W8 A! w
- ((PARITY) == USART_Parity_Odd))
复制代码 & {7 ~( y3 F7 Z6 I4 Q
USART_Mode- /** @defgroup USART_Mode
. p$ K. F& s+ s. P - * @{4 O" T1 E* a" t1 L9 N
- */
+ F) q, P' X8 q5 N+ C, ?( O7 }8 j
9 M" \7 @2 h8 O; H, N) B- #define USART_Mode_Rx ((uint16_t)0x0004)3 [ m' |3 ~, V8 Y+ b
- #define USART_Mode_Tx ((uint16_t)0x0008)0 |' A9 y) `, c9 b& t6 r7 k
- #define IS_USART_MODE(MODE) ((((MODE) & (uint16_t)0xFFF3) == 0x00) && ((MODE) != (uint16_t)0x00))
复制代码
+ k* \" f e/ @- E" |USART_Hardware_Flow_Control3 C9 r8 f7 L0 k; S5 ~( @ s
. F- z" }/ G' h8 e2 u" z4 o/ Y" u
- /** @defgroup USART_Hardware_Flow_Control ' S& N# G5 e; |
- * @{
% n$ `% Q, O( J+ h - */
4 C. {% g& w; ^( g0 F7 v - #define USART_HardwareFlowControl_None ((uint16_t)0x0000)1 S6 ^' M& O2 s# Z9 H1 `
- #define USART_HardwareFlowControl_RTS ((uint16_t)0x0100)% _, E! J$ d3 g( b# ~
- #define USART_HardwareFlowControl_CTS ((uint16_t)0x0200)
2 M/ l8 W( |( ?6 C u# ~0 k% B - #define USART_HardwareFlowControl_RTS_CTS ((uint16_t)0x0300)
3 m5 q1 C5 [8 ~. d - #define IS_USART_HARDWARE_FLOW_CONTROL(CONTROL)\
5 Y, ?; ?# T# Y- U6 z - (((CONTROL) == USART_HardwareFlowControl_None) || \
8 H- X) u1 {" K - ((CONTROL) == USART_HardwareFlowControl_RTS) || \
0 V2 v3 i3 A. t4 F- H1 W, } - ((CONTROL) == USART_HardwareFlowControl_CTS) || \/ x/ ~5 N& q* m+ I
- ((CONTROL) == USART_HardwareFlowControl_RTS_CTS))
复制代码
! B* ^9 T( [) Q5 b6 ?USART_Clock- /** @defgroup USART_Clock & s" u3 J7 E, R' _6 J1 H) b
- * @{
% \0 d5 `! E+ C/ v - */
" s" j& t2 T j& m% D2 @ - #define USART_Clock_Disable ((uint16_t)0x0000); I7 \4 J6 h; Y# \/ P, A8 @
- #define USART_Clock_Enable ((uint16_t)0x0800)4 y. j% b( I7 S3 o( z
- #define IS_USART_CLOCK(CLOCK) (((CLOCK) == USART_Clock_Disable) || \
1 X! A% d! d; Q' ?$ I7 O - ((CLOCK) == USART_Clock_Enable))
复制代码 ! o$ o7 e: p" R; I, d$ W
USART_Clock_Polarity
* e! i4 Q0 ~/ @ j( U# E) o. @8 v1 _$ G. \2 S7 ]* [9 T
- ' T% A) S9 `, p
- /** @defgroup USART_Clock_Polarity
1 t) ?7 b' F8 X8 I/ A - * @{
8 C4 ^" o/ o3 M% f - */3 b7 s$ n* N1 }5 E# {4 \8 z
- ! x C3 D0 u$ F
- #define USART_CPOL_Low ((uint16_t)0x0000)! w2 v" x( d& o8 K o, `
- #define USART_CPOL_High ((uint16_t)0x0400)
; K1 `. i2 q' o6 r- A - #define IS_USART_CPOL(CPOL) (((CPOL) == USART_CPOL_Low) || ((CPOL) == USART_CPOL_High))/ `/ T5 W/ a- w3 `# b7 g& O
复制代码
& V( d7 p1 N& d: K" |8 x; zUSART_Clock_Phase u2 ?) Q/ x# |) O& a/ ]
& T) r6 w- {+ D) p- h
- /** @defgroup USART_Clock_Phase
$ m6 @: m! q* E' y& b - * @{& h: d( v) y# v( {* K
- */$ g! [( K% l- |1 C6 V& |8 w+ ~
" b4 q5 r' n: m- A) R) D M1 Z- #define USART_CPHA_1Edge ((uint16_t)0x0000)4 M. h! }2 B7 Q( ?' p
- #define USART_CPHA_2Edge ((uint16_t)0x0200)
1 G& a+ y2 K; N8 G - #define IS_USART_CPHA(CPHA) (((CPHA) == USART_CPHA_1Edge) || ((CPHA) == USART_CPHA_2Edge))
复制代码
( T+ E1 x( S% TUSART_Last_Bit8 @& I3 i$ S+ l4 Z" X
. P ?% H& D! Y3 T1 @: w
- /** @defgroup USART_Last_Bit
1 _: @+ |" E0 l. V5 B5 {1 {. F7 _: S - * @{
' Y9 i" s6 a" L - */1 C8 {. b+ h7 x' f
! W5 J. H; ]( q4 c- #define USART_LastBit_Disable ((uint16_t)0x0000)
7 C% ~/ l9 e5 v: n, W - #define USART_LastBit_Enable ((uint16_t)0x0100)
6 [; e1 o9 |0 ^1 C - #define IS_USART_LASTBIT(LASTBIT) (((LASTBIT) == USART_LastBit_Disable) || \
- Z$ x9 x9 E/ M. @2 o- ] - ((LASTBIT) == USART_LastBit_Enable))
复制代码
0 m8 Z) q P! A$ hUSART_Interrupt_definition7 c; m1 R& s) ~3 s+ v7 S7 c
" M. q2 `, ^9 _+ l& {; X
- /** @defgroup USART_Interrupt_definition
& p3 E$ M* L. ~1 i# v6 K* c - * @{$ v! ]/ ]) Z- \
- */1 T8 I$ \: ?* R; j2 `
- # ~+ i0 U" u/ K5 Y/ H% x
- #define USART_IT_PE ((uint16_t)0x0028)
6 |+ R0 h* a* n- S - #define USART_IT_TXE ((uint16_t)0x0727)
* _2 \3 ~5 Z$ N" l7 r! d' V# C - #define USART_IT_TC ((uint16_t)0x0626): t- j, Y6 |6 d* e
- #define USART_IT_RXNE ((uint16_t)0x0525)
( q2 S/ y1 v0 J9 Z& M - #define USART_IT_ORE_RX ((uint16_t)0x0325) /* In case interrupt is generated if the RXNEIE bit is set */4 C% K8 I d# {) Q
- #define USART_IT_IDLE ((uint16_t)0x0424)- Q. K9 }' z; p/ ~$ r
- #define USART_IT_LBD ((uint16_t)0x0846)' F. S' r8 N2 _8 P1 m# E7 ?
- #define USART_IT_CTS ((uint16_t)0x096A)
* I; ~% z& S! K; x6 d1 w - #define USART_IT_ERR ((uint16_t)0x0060)$ |; {$ Y. Y H! L
- #define USART_IT_ORE_ER ((uint16_t)0x0360) /* In case interrupt is generated if the EIE bit is set */
+ u; ~5 A0 _8 J2 n0 H - #define USART_IT_NE ((uint16_t)0x0260)
0 S- w( |' T: ]& p - #define USART_IT_FE ((uint16_t)0x0160)
复制代码- #define IS_USART_CONFIG_IT(IT) (((IT) == USART_IT_PE) || ((IT) == USART_IT_TXE) || \
# G% R7 M. p6 r ` - ((IT) == USART_IT_TC) || ((IT) == USART_IT_RXNE) || \" j' N- [6 ^6 [$ L) r- E
- ((IT) == USART_IT_IDLE) || ((IT) == USART_IT_LBD) || \
" R6 L( U+ b4 C# q - ((IT) == USART_IT_CTS) || ((IT) == USART_IT_ERR))
) z* }) M; H$ N' e: S9 N. D - #define IS_USART_GET_IT(IT) (((IT) == USART_IT_PE) || ((IT) == USART_IT_TXE) || \
% [* T; |+ F7 f6 f; R+ Y2 O - ((IT) == USART_IT_TC) || ((IT) == USART_IT_RXNE) || \
. `; n6 u7 A8 {( n5 O) L3 d - ((IT) == USART_IT_IDLE) || ((IT) == USART_IT_LBD) || \3 p; }* M' O5 X- s+ M. i
- ((IT) == USART_IT_CTS) || ((IT) == USART_IT_ORE) || \" f" s+ G3 m7 v; l6 i
- ((IT) == USART_IT_ORE_RX) || ((IT) == USART_IT_ORE_ER) || \2 k1 S2 r! Q/ J1 ]* j, c9 I1 p
- ((IT) == USART_IT_NE) || ((IT) == USART_IT_FE))
4 k+ f: n; V. } - #define IS_USART_CLEAR_IT(IT) (((IT) == USART_IT_TC) || ((IT) == USART_IT_RXNE) || \8 U( \8 d6 K- H; L, G5 u0 y
- ((IT) == USART_IT_LBD) || ((IT) == USART_IT_CTS))
复制代码
! J* }0 Z" w8 w; s* D9 m* iUSART_DMA_Requests
! Q1 I% D( J3 I' g% ]. j
7 _2 E. H5 \3 u4 z$ p) N- /** @defgroup USART_DMA_Requests
, W$ n6 o. n7 r8 [9 X; G2 v; K1 B - * @{
) F1 v5 M+ E- @! i% c - */
) q" \" K( h7 n* o; `' d% J |
0 I. U. K/ ^: |! V" H7 C- #define USART_DMAReq_Tx ((uint16_t)0x0080)* h8 z# b7 [9 q% [, R; P& v: I
- #define USART_DMAReq_Rx ((uint16_t)0x0040)' X6 ~; n" D. _$ y4 J1 t
- #define IS_USART_DMAREQ(DMAREQ) ((((DMAREQ) & (uint16_t)0xFF3F) == 0x00) && ((DMAREQ) != (uint16_t)0x00))
复制代码 $ _* d5 i- I( t
USART_WakeUp_methods- /** @defgroup USART_WakeUp_methods
' Q+ g; \9 l) c& E - * @{
# C. ]$ {* c; H" O7 Y+ t, { - */
- k7 i/ }6 ]: t, B! h6 e" j - C* a) t& `! M3 {/ a
- #define USART_WakeUp_IdleLine ((uint16_t)0x0000): w; _6 N4 S* G8 O
- #define USART_WakeUp_AddressMark ((uint16_t)0x0800)4 Z$ O; q3 n( k% x& M) ]
- #define IS_USART_WAKEUP(WAKEUP) (((WAKEUP) == USART_WakeUp_IdleLine) || \2 z: O3 R: M, \7 z3 x
- ((WAKEUP) == USART_WakeUp_AddressMark))
复制代码 - v0 {9 n+ F! e) d. W. V
USART_LIN_Break_Detection_Length- /** @defgroup USART_LIN_Break_Detection_Length
$ n% w4 b* v& ^, n% p( k - * @{
( ] Y* |' Z8 i+ t) O - */1 l! _% m$ k9 |% K E: o/ J
* X7 @5 ~, |; L+ {. ^; k; N9 }- #define USART_LINBreakDetectLength_10b ((uint16_t)0x0000)4 t. \1 m& i3 C' G. ?3 G5 ]
- #define USART_LINBreakDetectLength_11b ((uint16_t)0x0020)
, Y2 p- |' W o) M5 R6 A1 w - #define IS_USART_LIN_BREAK_DETECT_LENGTH(LENGTH) \
+ \( _1 X. q8 }- m1 @ - (((LENGTH) == USART_LINBreakDetectLength_10b) || \4 k( W" ^+ v7 G7 [1 K+ g1 C
- ((LENGTH) == USART_LINBreakDetectLength_11b))
# t% ]1 e* ~* ^# C% k" q0 a. e! `
复制代码 5 |0 Y& c& ]2 y7 {" i
USART_IrDA_Low_Power
2 @# u' G. Z, P3 N7 Z# o
0 _6 ]* `/ m# Q7 Y4 Y7 K* V- /** @defgroup USART_IrDA_Low_Power 2 z% h+ b! Y; ~* Z9 o
- * @{" v: G1 b# R8 m: i
- */
, o7 n( y! g: E; \ X - ' H! z" n0 m3 F$ g- V# d
- #define USART_IrDAMode_LowPower ((uint16_t)0x0004)
}; H! f4 I0 z7 c* L% f: z - #define USART_IrDAMode_Normal ((uint16_t)0x0000)+ ?9 s6 C; \; T0 s& `
- #define IS_USART_IRDA_MODE(MODE) (((MODE) == USART_IrDAMode_LowPower) || \" E: b% V; R/ ^
- ((MODE) == USART_IrDAMode_Normal))
复制代码
5 G: Q, }- C' g* _+ H g# dUSART_Flags
( o6 ~5 d$ ~4 a1 T% q5 \7 ^9 i! r* O1 [5 @: u) i
- /** @defgroup USART_Flags 2 E% ^- F2 Z/ |- @+ p: e; w
- * @{
) d- A( }. q0 ?1 @ J - */' K4 [5 x3 g( r
/ x3 f, n7 e/ W- g- #define USART_FLAG_CTS ((uint16_t)0x0200)
- n0 }0 P, x/ j5 r5 j) o - #define USART_FLAG_LBD ((uint16_t)0x0100)' O9 c5 g0 T& [0 h& R3 B
- #define USART_FLAG_TXE ((uint16_t)0x0080)8 s) `0 L8 A9 O, W- ]4 {
- #define USART_FLAG_TC ((uint16_t)0x0040) j' d' E5 d. l" g" g
- #define USART_FLAG_RXNE ((uint16_t)0x0020)
) {! {8 ^. i3 b! K' @0 Y - #define USART_FLAG_IDLE ((uint16_t)0x0010)
9 I: q# y& |# ?4 s% f - #define USART_FLAG_ORE ((uint16_t)0x0008). W8 M( C4 H$ `: C
- #define USART_FLAG_NE ((uint16_t)0x0004)2 @; p( [: X( C: ?
- #define USART_FLAG_FE ((uint16_t)0x0002)
# ?% {. }( p' Q. s" p - #define USART_FLAG_PE ((uint16_t)0x0001)
% D h- \/ Y. a - #define IS_USART_FLAG(FLAG) (((FLAG) == USART_FLAG_PE) || ((FLAG) == USART_FLAG_TXE) || \
- C! J3 L, x! X7 \' v% F$ H - ((FLAG) == USART_FLAG_TC) || ((FLAG) == USART_FLAG_RXNE) || \- I& X- n. s$ R. Y d6 y, x
- ((FLAG) == USART_FLAG_IDLE) || ((FLAG) == USART_FLAG_LBD) || \+ D) d! H3 c# F" W9 |# h
- ((FLAG) == USART_FLAG_CTS) || ((FLAG) == USART_FLAG_ORE) || \
+ F B/ t; ?+ l8 a- C4 ^9 V - ((FLAG) == USART_FLAG_NE) || ((FLAG) == USART_FLAG_FE))
& t3 k' y- j% e& s$ h! y0 @ - 9 A# D( W% ^ K
- #define IS_USART_CLEAR_FLAG(FLAG) ((((FLAG) & (uint16_t)0xFC9F) == 0x00) && ((FLAG) != (uint16_t)0x00))& P( v1 E; U3 G5 N; n. n+ B0 s
- 9 E: H* a, A$ R; G. O. `
- #define IS_USART_BAUDRATE(BAUDRATE) (((BAUDRATE) > 0) && ((BAUDRATE) < 7500001)) F& j2 q+ m" c3 [6 g9 ~
- #define IS_USART_ADDRESS(ADDRESS) ((ADDRESS) <= 0xF)
* u0 C; l4 K' k! u8 Y) G - #define IS_USART_DATA(DATA) ((DATA) <= 0x1FF)
复制代码 ( Q! ^* L6 N: F h
03. USART相关其它宏3 ~+ Z" B/ s( {/ S) ?
USART_TypeDef
6 Q. v& l% ^2 o4 J- B
7 F; k7 }1 g4 L6 b ]1 M2 U+ i- /** 9 t5 }* }5 `' M* B7 l# z7 w% K: |" I+ X
- * @brief Universal Synchronous Asynchronous Receiver Transmitter
' T! B9 D) T" _0 l - */
, L6 B& y: }, Q. _" X! R$ W& E - 4 F( ~6 l; D- u+ w8 P
- typedef struct9 S: D- m& z; n
- {
( P" Y% @7 n0 R. h" F" W - __IO uint16_t SR; /*!< USART Status register, Address offset: 0x00 */ I2 L/ \0 p$ l1 ]2 i+ X8 J
- uint16_t RESERVED0; /*!< Reserved, 0x02 */# ]8 u3 P, L4 V) L5 @7 c! y
- __IO uint16_t DR; /*!< USART Data register, Address offset: 0x04 */- c+ ]0 p" z+ \5 s; k7 A% U1 W
- uint16_t RESERVED1; /*!< Reserved, 0x06 */
1 j0 E' @! Y0 @* w. ~3 i - __IO uint16_t BRR; /*!< USART Baud rate register, Address offset: 0x08 */$ H8 r0 {7 I# v" H" [6 e- g
- uint16_t RESERVED2; /*!< Reserved, 0x0A */ F, z0 s1 C2 p
- __IO uint16_t CR1; /*!< USART Control register 1, Address offset: 0x0C */# P- u6 L$ E; i$ Z
- uint16_t RESERVED3; /*!< Reserved, 0x0E */
# t6 }# Q4 f- l& q! O0 d - __IO uint16_t CR2; /*!< USART Control register 2, Address offset: 0x10 */
$ J+ A$ w! b- ^( ^ - uint16_t RESERVED4; /*!< Reserved, 0x12 */
( z" V- t: w; D9 f - __IO uint16_t CR3; /*!< USART Control register 3, Address offset: 0x14 */
+ p9 |& m6 t3 P8 s/ K) U5 f - uint16_t RESERVED5; /*!< Reserved, 0x16 */: O! I" V# ]! M9 N3 N! f
- __IO uint16_t GTPR; /*!< USART Guard time and prescaler register, Address offset: 0x18 */( i) x+ T0 r: K0 d
- uint16_t RESERVED6; /*!< Reserved, 0x1A */% `% @6 v+ \* |: a! q
- } USART_TypeDef;
) L- H) @. k# D! v9 T
复制代码- /******************************************************************************/' {; w7 P) f( F% @8 K. m8 A% O
- /* */
: w% |8 }% N* `- b+ \ - /* Universal Synchronous Asynchronous Receiver Transmitter */, ~: Q b( _6 h. z) S. @
- /* */
5 _) X8 z" ^# T2 X - /******************************************************************************/
- l' Z8 L4 H; a - /******************* Bit definition for USART_SR register *******************/& a* L+ \; `0 l( _* p
- #define USART_SR_PE ((uint16_t)0x0001) /*!<Parity Error */7 Q! J; f' k# U% k) A! Q9 e
- #define USART_SR_FE ((uint16_t)0x0002) /*!<Framing Error */
$ x: ~. k: L- [, ]7 g; j3 s/ R% E" x - #define USART_SR_NE ((uint16_t)0x0004) /*!<Noise Error Flag */
! y: o3 R$ l- W! W! R - #define USART_SR_ORE ((uint16_t)0x0008) /*!<OverRun Error */" x# s6 b6 Y% v# c+ r
- #define USART_SR_IDLE ((uint16_t)0x0010) /*!<IDLE line detected */( @! j7 A" S" k! [
- #define USART_SR_RXNE ((uint16_t)0x0020) /*!<Read Data Register Not Empty */
8 }" }' `/ x- R8 L - #define USART_SR_TC ((uint16_t)0x0040) /*!<Transmission Complete */9 `1 D1 m& b2 T4 e5 v% e: m) K' @; C2 [
- #define USART_SR_TXE ((uint16_t)0x0080) /*!<Transmit Data Register Empty */
* g% j! B% j" \* s2 d; R - #define USART_SR_LBD ((uint16_t)0x0100) /*!<LIN Break Detection Flag */
+ q- `6 B/ @+ R1 u4 a - #define USART_SR_CTS ((uint16_t)0x0200) /*!<CTS Flag */
8 X6 y4 W" Y8 `( r! y - 3 `; z \% B, y$ V: ]; W2 ?
- /******************* Bit definition for USART_DR register *******************/
) c! N7 x- @5 s" u& u7 A ~% R - #define USART_DR_DR ((uint16_t)0x01FF) /*!<Data value */' Y* J* ?0 m! g
, ]! k. l7 |0 C9 _( ]& E$ n- /****************** Bit definition for USART_BRR register *******************/: H0 F5 v$ j1 m1 S
- #define USART_BRR_DIV_Fraction ((uint16_t)0x000F) /*!<Fraction of USARTDIV */0 m% W' V! h$ [; ~6 C
- #define USART_BRR_DIV_Mantissa ((uint16_t)0xFFF0) /*!<Mantissa of USARTDIV */
& f: F# P$ }( k% W) ?6 t6 m
2 S, x- z X) ?+ A! k2 l- /****************** Bit definition for USART_CR1 register *******************/+ e+ j) o, h C, c! [4 w+ {
- #define USART_CR1_SBK ((uint16_t)0x0001) /*!<Send Break */4 J) P; Z n( x( ], i1 _
- #define USART_CR1_RWU ((uint16_t)0x0002) /*!<Receiver wakeup */
; X' Y0 ^! G: V - #define USART_CR1_RE ((uint16_t)0x0004) /*!<Receiver Enable */
: _. L- _3 @2 c* f# a - #define USART_CR1_TE ((uint16_t)0x0008) /*!<Transmitter Enable */- y5 A. n1 G" c, z1 B: D6 h, b
- #define USART_CR1_IDLEIE ((uint16_t)0x0010) /*!<IDLE Interrupt Enable */% M: ^) h8 Z* ]2 f
- #define USART_CR1_RXNEIE ((uint16_t)0x0020) /*!<RXNE Interrupt Enable */
6 S: {4 l* U. ]: { - #define USART_CR1_TCIE ((uint16_t)0x0040) /*!<Transmission Complete Interrupt Enable */
9 h) x" r% _' u2 R& e; M8 C - #define USART_CR1_TXEIE ((uint16_t)0x0080) /*!<PE Interrupt Enable */
% N# g7 Q9 A! _# i$ r& Q* i0 K - #define USART_CR1_PEIE ((uint16_t)0x0100) /*!<PE Interrupt Enable */! y7 ~6 E$ I7 B% C& g6 S
- #define USART_CR1_PS ((uint16_t)0x0200) /*!<Parity Selection */6 O! }9 r0 i& p$ q! D
- #define USART_CR1_PCE ((uint16_t)0x0400) /*!<Parity Control Enable */
5 Y' K3 b. p0 Y4 A) F1 K/ B - #define USART_CR1_WAKE ((uint16_t)0x0800) /*!<Wakeup method */
+ n( I( E* X, y+ ]" b - #define USART_CR1_M ((uint16_t)0x1000) /*!<Word length */
$ ~) w" e0 T6 s3 S& G; ` - #define USART_CR1_UE ((uint16_t)0x2000) /*!<USART Enable */
! F1 w; i2 b: J/ X - #define USART_CR1_OVER8 ((uint16_t)0x8000) /*!<USART Oversampling by 8 enable */% q5 `" m' f q: _# f
; |1 O+ `) w0 v. }6 \- /****************** Bit definition for USART_CR2 register *******************/
* s; t, t8 t$ z* C; H - #define USART_CR2_ADD ((uint16_t)0x000F) /*!<Address of the USART node */
2 w, m' [ P' \+ ^) J: }( S - #define USART_CR2_LBDL ((uint16_t)0x0020) /*!<LIN Break Detection Length */
* G; l1 V+ S3 c! T6 S# d4 Y$ W - #define USART_CR2_LBDIE ((uint16_t)0x0040) /*!<LIN Break Detection Interrupt Enable */2 Z1 p: ]6 ^6 q$ j
- #define USART_CR2_LBCL ((uint16_t)0x0100) /*!<Last Bit Clock pulse */( m6 g3 z) ?9 Q# w' ~
- #define USART_CR2_CPHA ((uint16_t)0x0200) /*!<Clock Phase */7 e& W! O2 M0 Q- V9 ^* ~3 a. h- d
- #define USART_CR2_CPOL ((uint16_t)0x0400) /*!<Clock Polarity */
0 p7 f( u) \5 F - #define USART_CR2_CLKEN ((uint16_t)0x0800) /*!<Clock Enable */
7 J1 F4 d8 {& f( _
3 F4 t7 q' D7 i3 I% R- #define USART_CR2_STOP ((uint16_t)0x3000) /*!<STOP[1:0] bits (STOP bits) */
: x/ J- Z( M7 D! K - #define USART_CR2_STOP_0 ((uint16_t)0x1000) /*!<Bit 0 */( s' X7 [/ }2 D7 q2 Z
- #define USART_CR2_STOP_1 ((uint16_t)0x2000) /*!<Bit 1 */
9 I3 m% R* [" Y; p' \- N+ R8 M% o
* ]9 L7 Y6 y3 K; R- #define USART_CR2_LINEN ((uint16_t)0x4000) /*!<LIN mode enable */
# F: Z' B/ E* D5 W& Z9 L6 i
& ?9 I3 \5 {+ n8 |& Y: j0 h% T, @- /****************** Bit definition for USART_CR3 register *******************/
- Z) v0 x+ ^. T/ O6 r - #define USART_CR3_EIE ((uint16_t)0x0001) /*!<Error Interrupt Enable */
e( p6 q" K: n6 S+ i( J+ X - #define USART_CR3_IREN ((uint16_t)0x0002) /*!<IrDA mode Enable */1 A0 K. g2 Z: ]/ v) V
- #define USART_CR3_IRLP ((uint16_t)0x0004) /*!<IrDA Low-Power */ b" L8 ]% \% R- x3 ]
- #define USART_CR3_HDSEL ((uint16_t)0x0008) /*!<Half-Duplex Selection */
8 [) C- U/ s) C* |) |4 k8 { - #define USART_CR3_NACK ((uint16_t)0x0010) /*!<Smartcard NACK enable */" n7 \3 ^, f0 K( T7 ^) j: [; R
- #define USART_CR3_SCEN ((uint16_t)0x0020) /*!<Smartcard mode enable */( {" A; M0 R( n0 I+ P1 D
- #define USART_CR3_DMAR ((uint16_t)0x0040) /*!<DMA Enable Receiver */5 S" S/ U. ]+ \0 o( Z( i
- #define USART_CR3_DMAT ((uint16_t)0x0080) /*!<DMA Enable Transmitter */
7 d: E/ u: Y m# d; B& R, P, g- x/ L - #define USART_CR3_RTSE ((uint16_t)0x0100) /*!<RTS Enable */$ M" @/ O( w4 w) b
- #define USART_CR3_CTSE ((uint16_t)0x0200) /*!<CTS Enable */' T) F# k1 q" P, Q
- #define USART_CR3_CTSIE ((uint16_t)0x0400) /*!<CTS Interrupt Enable */
$ b C3 _! m% c" x* U - #define USART_CR3_ONEBIT ((uint16_t)0x0800) /*!<USART One bit method enable */. P0 c! g( F9 q
- 2 ]* [5 {" d2 N5 u/ {
- /****************** Bit definition for USART_GTPR register ******************/
( T' `* {1 P2 s( P# j - #define USART_GTPR_PSC ((uint16_t)0x00FF) /*!<PSC[7:0] bits (Prescaler value) */: u) U7 H% p7 F
- #define USART_GTPR_PSC_0 ((uint16_t)0x0001) /*!<Bit 0 *// C6 R& o+ {8 J5 |
- #define USART_GTPR_PSC_1 ((uint16_t)0x0002) /*!<Bit 1 */
" U* J# c) b( U4 ?& E% R% Z7 s - #define USART_GTPR_PSC_2 ((uint16_t)0x0004) /*!<Bit 2 */( ?- P& M) j# r0 e$ Q
- #define USART_GTPR_PSC_3 ((uint16_t)0x0008) /*!<Bit 3 */
- w$ t& ^9 {+ A; Q, ^ - #define USART_GTPR_PSC_4 ((uint16_t)0x0010) /*!<Bit 4 */
5 e7 K; z( k! c! R- ^ - #define USART_GTPR_PSC_5 ((uint16_t)0x0020) /*!<Bit 5 *// y4 p# P) L2 b
- #define USART_GTPR_PSC_6 ((uint16_t)0x0040) /*!<Bit 6 */
: ~2 @5 D6 E" b) [9 `& g. P - #define USART_GTPR_PSC_7 ((uint16_t)0x0080) /*!<Bit 7 */" a7 k* T2 w& D$ I7 N
- : {/ |, B) Y) U l+ Q
- #define USART_GTPR_GT ((uint16_t)0xFF00) /*!<Guard time value */
复制代码 ) x- K, M9 I) d
04. USART相关函数6 H2 U$ ?1 f- ?* g& {& m
- /* Function used to set the USART configuration to the default reset state ***/
( @+ r; ^% R7 G - void USART_DeInit(USART_TypeDef* USARTx);8 f: i \, N6 o0 m- F% c: Z1 `" O
% I7 d; v- G4 ^& I8 ]0 C! I- /* Initialization and Configuration functions *********************************/
% e' g( b1 A. u- |3 F - void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct);
# C: L* ?; }7 b - void USART_StructInit(USART_InitTypeDef* USART_InitStruct);! h- o, {# }3 ?- [
- void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct);" E7 N7 u- J( d# s4 v: W! M
- void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct);8 k; o5 {4 u( k4 H
- void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState);
& Z8 ~/ Y+ U( }4 d1 l7 R( E/ u - void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler);+ y! C" b* j$ R' d
- void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState);
' {8 _% m4 t8 U! h- X" W- v+ H - void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState);
" j' Q% P8 j' i' D2 g4 \: G
8 W) G' l& c/ C& a6 J. v: E9 q- /* Data transfers functions ***************************************************/
% `$ y! l5 ~& b( `* h/ U! x7 `, e2 D - void USART_SendData(USART_TypeDef* USARTx, uint16_t Data);
; E, |) [7 G( K j0 a1 m( w - uint16_t USART_ReceiveData(USART_TypeDef* USARTx);- e1 z* D. w/ ?
' V% r/ X# S" b# ]6 ]+ g$ t0 t- /* Multi-Processor Communication functions ************************************/( O8 K# L0 v6 \( y% E
- void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address);, Z, V% t B0 m5 ^
- void USART_WakeUpConfig(USART_TypeDef* USARTx, uint16_t USART_WakeUp);
; v3 Q$ S/ U, I4 ] Q( j; s, S - void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState);* J! V, O9 e m2 S: W: V
& u: R+ e& f+ w" q; p; j- /* LIN mode functions *********************************************************/
& I/ H2 ?( E P( R& Z% ` - void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint16_t USART_LINBreakDetectLength);
Q$ V3 I) v4 l& L5 m* y* N - void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState);
K. s' f1 ^; A( S+ w8 \) T# [ - void USART_SendBreak(USART_TypeDef* USARTx);
* x: y& k# l% Q - $ [2 Q1 o; }9 c6 A* u* P: j
- /* Half-duplex mode function **************************************************/# u9 t* e" [+ G6 ~# s2 T
- void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState);
! @: Y( R. x# M% _3 a \, E
7 S6 Q6 t( {: V- /* Smartcard mode functions ***************************************************/
' W: }- A z8 n- `1 t- v6 @/ C3 b - void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState);' d+ A3 ], L( q
- void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState);; D) ~$ M. ]& d7 Q; ^7 G
- void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime);) Y: {1 S9 G$ u$ Q6 b$ w7 j
- / t" {- f; t& L- D! L( t( \
- /* IrDA mode functions ********************************************************/
& J9 W S( E# c - void USART_IrDAConfig(USART_TypeDef* USARTx, uint16_t USART_IrDAMode);5 \) Q# L7 Z( r8 l1 X2 \, [& L3 O
- void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState);! t! F( h6 ?( B7 b0 ]$ Z3 Q' t$ K( K
/ D2 n/ g) o9 r- /* DMA transfers management functions *****************************************/9 V$ k1 X0 f/ |, ]! [8 x) W
- void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState);
5 q* x8 p1 @8 ~ - 1 R1 l( a( E4 v7 g) F1 B
- /* Interrupts and flags management functions **********************************/
2 f" h2 @; |+ N- B% ^ - void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState);
4 K! H2 \: M" S* Y6 C" W - FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG);
! h4 O& X& G' E - void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG);
9 r; l ]- F& N3 Q2 E0 r' }+ z) L - ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT);# t2 P5 P1 P6 v7 C" p& x8 m) t+ e
- void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT);* z; `& ?, |4 W& V
复制代码 9 m! P/ l; f V! s; e1 x ^5 l& k: U
05. USART其它
5 p7 u* S0 E% o" M, FUSART基地址
0 ?6 x x5 \& z) d* l9 X% G0 T# I) Z6 E% d: X4 l
- #define USART2_BASE (APB1PERIPH_BASE + 0x4400)* N2 M: n" f6 w7 O: N4 V; u4 h
- #define USART3_BASE (APB1PERIPH_BASE + 0x4800): g- b9 L! k- c: `- }' r. ]" x
- #define UART4_BASE (APB1PERIPH_BASE + 0x4C00)+ c) @2 M- F* S! S
- #define UART5_BASE (APB1PERIPH_BASE + 0x5000)
s% x3 |0 L; ?" z6 t2 H o+ C) ?
, D* u* Y! e7 e& r3 [' A9 h- #define USART1_BASE (APB2PERIPH_BASE + 0x1000)6 g* c3 K7 `# o: i- {; o7 E
- #define USART6_BASE (APB2PERIPH_BASE + 0x1400)6 W% [- z7 J1 A5 w/ s) H5 H
- #define UART9_BASE (APB2PERIPH_BASE + 0x1800U)
( c4 L. x% Q, z% J6 c# q - #define UART10_BASE (APB2PERIPH_BASE + 0x1C00U)
复制代码 : \% q6 `9 U) ?4 ~( @
, W% L5 M Q" u) X) q
8 p) Z! a Q1 ~/ D/ }9 y. n |