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