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