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