01. SPI简介
' [0 `. F# e. {3 ]% U5 m- _SPI 是英语 Serial Peripheral interface 的缩写,顾名思义就是串行外围设备接口。是 Motorola首先在其 MC68HCXX 系列处理器上定义的。SPI 接口主要应用在 EEPROM,FLASH,实时时钟,AD 转换器,还有数字信号处理器和数字信号解码器之间。SPI,是一种高速的,全双工,同步的通信总线,并且在芯片的管脚上只占用四根线,节约了芯片的管脚,同时为 PCB 的布局上节省空间,提供方便,正是出于这种简单易用的特性,现在越来越多的芯片集成了这种通信协议,STM32F4 也有 SPI 接口。$ [3 D y! s" l
3 d# U T* y! x1 ]; ? z: `
02. 相关类型* C. b2 F# s, H) ^0 v
SPI Init structure definition
* W* k- C, u6 _+ X( v+ `. l
8 ^- ~ E- W: ?7 R* T% W" c+ M+ i# I- /** 7 F; Y9 e2 o+ S9 s4 C8 e2 d
- * @brief SPI Init structure definition
, Q; t% ?# P& z0 f - */
N+ u: G) l3 u1 D
. W, F1 U7 L" i7 s( H; G5 ^$ h- typedef struct( h( \8 |" W7 Q9 S) O$ [
- {
) q9 G9 B- p3 t# o - uint16_t SPI_Direction; /*!< Specifies the SPI unidirectional or bidirectional data mode." ^ R% U! D( u' J; h7 N; `
- This parameter can be a value of @ref SPI_data_direction */! d" [6 M- z( A
- 7 D- n: W4 r( ~3 o
- uint16_t SPI_Mode; /*!< Specifies the SPI operating mode.- ^* z$ Y4 ?* f2 T
- This parameter can be a value of @ref SPI_mode */5 V1 X+ T+ ?' R& h# G, \
- ' L$ K, |( s& |& \; X( E
- uint16_t SPI_DataSize; /*!< Specifies the SPI data size.
' i% f6 N% ?" Y- t" E# k - This parameter can be a value of @ref SPI_data_size */
* m/ m& D4 Y" _4 D2 ^
) v+ R( c& D+ h- uint16_t SPI_CPOL; /*!< Specifies the serial clock steady state.
# C6 m6 I. U! R+ P+ q - This parameter can be a value of @ref SPI_Clock_Polarity */) u) T& S E7 p- |' q; w
- - |8 ]% M5 {' w' M
- uint16_t SPI_CPHA; /*!< Specifies the clock active edge for the bit capture., O# R: h' }$ X2 \4 J1 J2 }
- This parameter can be a value of @ref SPI_Clock_Phase *// }+ H# S% U3 t# \1 n3 h% J
- 3 c7 Y1 t+ \& L" w% q
- uint16_t SPI_NSS; /*!< Specifies whether the NSS signal is managed by
1 l# g, g$ j8 _" ^- [& F - hardware (NSS pin) or by software using the SSI bit., J b! G' Q2 n' I# A, f+ |' t
- This parameter can be a value of @ref SPI_Slave_Select_management */
9 ^5 b; g& w9 G3 ?- b - ' b. q, G/ F/ E+ f5 S
- uint16_t SPI_BaudRatePrescaler; /*!< Specifies the Baud Rate prescaler value which will be
* p9 d" b; m6 v" `$ a - used to configure the transmit and receive SCK clock.! v! f9 |# F: X
- This parameter can be a value of @ref SPI_BaudRate_Prescaler% a) K4 _2 Y: q* ^0 A" e
- @note The communication clock is derived from the master% q! j8 C: t2 ?
- clock. The slave clock does not need to be set. */8 f$ j, I: ?5 C6 ?) m& J: @: J
- " x+ U: R8 ?# Y+ L- s4 n, t0 y
- uint16_t SPI_FirstBit; /*!< Specifies whether data transfers start from MSB or LSB bit.6 t7 b, `2 @* X8 ?- i: a1 ?$ J
- This parameter can be a value of @ref SPI_MSB_LSB_transmission */2 }9 k9 P7 m+ N
% q2 p1 E; U; I& ~! i1 s- uint16_t SPI_CRCPolynomial; /*!< Specifies the polynomial used for the CRC calculation. */1 C8 j0 z9 C3 D6 {6 @ _
- }SPI_InitTypeDef;
复制代码
/ S% _9 }5 A7 N2 yI2S Init structure definition# O. V% n9 L% V- B" C0 R" Y
& Q7 c p) m: W/ M0 Y* z4 g
- /**
( k) T8 Z( w0 U' h - * @brief I2S Init structure definition
" ~+ v% R& B$ S3 k - */
- r$ F: K/ _' b7 P0 R
! \: l3 ~1 s6 I! w, l0 {- typedef struct/ _3 o d. Q, w7 F- `! F* D6 e5 Z
- {3 ^$ h7 ]3 X) t7 i. N% h
- % d9 i7 @7 K3 B8 n
- uint16_t I2S_Mode; /*!< Specifies the I2S operating mode.4 U1 J" @4 ~8 q1 [/ s/ X
- This parameter can be a value of @ref I2S_Mode */
# X: H: m/ f+ i! N: U2 M' t1 t
. J9 O$ J* h9 J& ^) K) F7 V: I- uint16_t I2S_Standard; /*!< Specifies the standard used for the I2S communication.6 F/ Z, S1 h4 |- ]! a& \' `# J# {7 k
- This parameter can be a value of @ref I2S_Standard */
; t5 L: ?5 ?7 R' B - 3 R _5 g6 b/ m; n. p3 I% V) H; V
- uint16_t I2S_DataFormat; /*!< Specifies the data format for the I2S communication.
& H9 b" j9 ^; Z2 W M2 p* v - This parameter can be a value of @ref I2S_Data_Format */
' g# r# \4 X2 ~& l R
4 U0 _% D7 l- R! a3 V" i- uint16_t I2S_MCLKOutput; /*!< Specifies whether the I2S MCLK output is enabled or not.9 q# |; \3 W" |! ]' q
- This parameter can be a value of @ref I2S_MCLK_Output */0 A) Z. }! f6 U: j! I
1 b8 c. Y1 b2 z& x, d4 E, W W* d- uint32_t I2S_AudioFreq; /*!< Specifies the frequency selected for the I2S communication.- E, B. L6 M) K7 `4 r9 g8 E
- This parameter can be a value of @ref I2S_Audio_Frequency */+ V. y$ K% n; z6 A
- 2 m( f) i% i9 k: Z2 T
- uint16_t I2S_CPOL; /*!< Specifies the idle state of the I2S clock.
( M/ b5 {) o4 R' T. i* k" a - This parameter can be a value of @ref I2S_Clock_Polarity */
& e8 Z- K3 B% ~0 U - }I2S_InitTypeDef;
复制代码 . S- c8 } @! W& x) Q( s. P: M9 @
SPI_Exported_Constants
! C. R2 G$ j& M3 }
. {0 a/ g+ U9 J5 W- /** @defgroup SPI_Exported_Constants! g' i4 a3 ?, k! n$ E
- * @{2 \. z3 Q2 s+ H% N0 G6 l0 k
- */
* z' o. i' \" h0 I0 h - : v/ w9 \! v" i% |' o- S2 J
- #define IS_SPI_ALL_PERIPH(PERIPH) (((PERIPH) == SPI1) || \9 X4 m! n) ^) ]1 Q9 [$ t3 g6 \
- ((PERIPH) == SPI2) || \+ P7 m% C6 y$ g) E E
- ((PERIPH) == SPI3) || \) F) e9 @+ C2 h$ s/ o! `6 l
- ((PERIPH) == SPI4) || \
6 V$ _9 `$ J1 @4 u8 O) L3 c: r - ((PERIPH) == SPI5) || \( A. o% I4 a4 |# l; s p
- ((PERIPH) == SPI6))9 ~4 I5 z4 D! |' ` Z
% {3 c) L, i0 O1 v# x. P# {% |+ K- #define IS_SPI_ALL_PERIPH_EXT(PERIPH) (((PERIPH) == SPI1) || \1 U( }* S& _, }& H0 S s
- ((PERIPH) == SPI2) || \
, K! j! |2 h1 @% ?: s! K - ((PERIPH) == SPI3) || \% j3 y W+ e% u
- ((PERIPH) == SPI4) || \% d# I' T6 z& |* j
- ((PERIPH) == SPI5) || \9 C1 ^& t( J1 }4 e: ] d* X
- ((PERIPH) == SPI6) || \
) _+ R0 u/ U4 p6 m1 h - ((PERIPH) == I2S2ext) || \; q% u5 R; ]# P* t4 ~
- ((PERIPH) == I2S3ext)). i, v* N: R; n7 h7 k; V9 ]6 C8 Z
; T2 \; @1 q& ]" E* y' ~- #define IS_SPI_23_PERIPH(PERIPH) (((PERIPH) == SPI2) || \: t [) Y% N1 c3 b2 q" {5 g8 W. t
- ((PERIPH) == SPI3))# r0 z& _' t- H9 v! q7 s7 n
5 w; G; a: H' ]0 N3 J: v, P- #define IS_SPI_23_PERIPH_EXT(PERIPH) (((PERIPH) == SPI2) || \
0 V. h" M( `, e& ` - ((PERIPH) == SPI3) || \: I* K0 A0 |( K: } ^+ A( E- _
- ((PERIPH) == I2S2ext) || \ ?$ `- l0 c8 j) S
- ((PERIPH) == I2S3ext))
% E2 E. o, h, Q/ ~ - 4 p( c5 g% a0 w, n; E4 d. O
- #define IS_I2S_EXT_PERIPH(PERIPH) (((PERIPH) == I2S2ext) || \
$ T$ r Y. D( Z - ((PERIPH) == I2S3ext))
复制代码
5 ^( e# d1 K, v6 ^5 FSPI_data_direction( ^4 `* a; J0 n7 R* G/ S3 P
! r( r: U7 {) K- /** @defgroup SPI_data_direction 3 q* d5 b( ]2 i5 R- y; t
- * @{
( D; I, U% M" ~ ~) _5 y4 v- `5 N - */
" A: ?" `: ^, L2 s9 u$ E" B - % m7 N3 X% B4 F ?1 G& V4 m5 Z- k
- #define SPI_Direction_2Lines_FullDuplex ((uint16_t)0x0000)' R* l3 `# P2 Q% T# m7 T
- #define SPI_Direction_2Lines_RxOnly ((uint16_t)0x0400)
1 Y' ?- K' m" e3 ~0 ? - #define SPI_Direction_1Line_Rx ((uint16_t)0x8000)7 ?9 u' G5 M- c& f$ T
- #define SPI_Direction_1Line_Tx ((uint16_t)0xC000)5 \$ [% C u: l% x2 Z; ^( d; P; G
- #define IS_SPI_DIRECTION_MODE(MODE) (((MODE) == SPI_Direction_2Lines_FullDuplex) || \
# p @7 ^& D" g - ((MODE) == SPI_Direction_2Lines_RxOnly) || \, ~% p6 a* b, W7 G
- ((MODE) == SPI_Direction_1Line_Rx) || \
, _( x7 k6 t; C - ((MODE) == SPI_Direction_1Line_Tx))
复制代码 " r) M0 d2 S% D) ~4 g' Y
SPI_mode9 ~7 Q8 m' h+ | y& K; R% u7 d
' p' \0 l% X+ b6 {3 i q
- /** @defgroup SPI_mode
2 Z8 O3 d- W+ G/ r( z- m q; l' Z - * @{
* u/ t- z( x W! D - */7 W0 j R1 X4 u' T M: z
: R# r% {5 h* U5 t; D9 U2 k- #define SPI_Mode_Master ((uint16_t)0x0104)
9 ~5 N2 t4 {, z - #define SPI_Mode_Slave ((uint16_t)0x0000)1 b, y" {6 k* ^8 u, t# M, R3 I8 T% y
- #define IS_SPI_MODE(MODE) (((MODE) == SPI_Mode_Master) || \4 E. k$ `7 v' z4 V/ ?3 d
- ((MODE) == SPI_Mode_Slave))
复制代码
" {, n# Q* w! o5 p& ?* vSPI_data_size- o8 {0 ]+ q; q0 ?
8 ~5 a6 j6 ` a7 k, ?" u- /** @defgroup SPI_data_size , c7 d- n7 W* z' @+ P
- * @{
- X$ B% i4 m5 {0 h+ }! X - */
5 Y$ y4 f- m3 s# w1 S - # X y8 i# } H: X! F5 J7 Y
- #define SPI_DataSize_16b ((uint16_t)0x0800)
4 j4 B& W5 ^0 X$ P) l$ g% T - #define SPI_DataSize_8b ((uint16_t)0x0000)+ X6 C, H7 t1 o3 Y9 d5 g) y
- #define IS_SPI_DATASIZE(DATASIZE) (((DATASIZE) == SPI_DataSize_16b) || \
8 r! ]$ m' Y' y. `3 O3 m - ((DATASIZE) == SPI_DataSize_8b))
复制代码
! e. U' H5 J8 U5 D, p% \SPI_Clock_Polarity0 s& j; M4 O5 ], m, Y
) _& T( Z% x6 F- j
- /** @defgroup SPI_Clock_Polarity 4 E8 G, D" X( I
- * @{
% A3 ~+ Q1 q$ A- W5 f+ L8 X+ W - */
# {7 u) ~' h0 m! ^" W
- r9 r- I6 I, i! A W) X- O- #define SPI_CPOL_Low ((uint16_t)0x0000)$ @( F! w+ {5 t/ s, \- E3 l$ }1 A
- #define SPI_CPOL_High ((uint16_t)0x0002)
, x; @0 C" n0 E1 H - #define IS_SPI_CPOL(CPOL) (((CPOL) == SPI_CPOL_Low) || \6 o, |3 b- ?+ \' x
- ((CPOL) == SPI_CPOL_High))
复制代码
3 a# C8 {3 ^1 K- sSPI_Clock_Phase& ~! ~7 ?/ u2 c7 v
' E) }; J( W+ Q" E% P- /** @defgroup SPI_Clock_Phase ) w" L$ M7 F" ]9 C
- * @{
# y5 G- F3 S5 p: [: s/ V - */( W2 k9 T, |+ Z. r+ P
: U) S' n+ w4 U4 _6 |( v( N- #define SPI_CPHA_1Edge ((uint16_t)0x0000)3 ~) A6 r6 z+ f9 H
- #define SPI_CPHA_2Edge ((uint16_t)0x0001)6 k& }: V' o5 r: ~
- #define IS_SPI_CPHA(CPHA) (((CPHA) == SPI_CPHA_1Edge) || \, w4 q2 Y/ y; Y0 s9 }1 f
- ((CPHA) == SPI_CPHA_2Edge))
复制代码
; p2 P; d# P1 F$ B6 h% \- T9 JSPI_Slave_Select_management3 ^/ }% c; X" i
) B. |0 Z, k) I: q* R: s$ P1 `- /** @defgroup SPI_Slave_Select_management
$ k( i; j( j. t3 l/ f2 }+ m( } - * @{
! S5 f+ _+ ~+ w. D6 u - */
& M7 \& l/ s# g* A) O: _! V" j, ~& Q - + M) s1 I4 X& G! D: Q5 X
- #define SPI_NSS_Soft ((uint16_t)0x0200)7 M1 s. B) Y+ b1 Q
- #define SPI_NSS_Hard ((uint16_t)0x0000)
" m& V& L( `( ~ s/ y - #define IS_SPI_NSS(NSS) (((NSS) == SPI_NSS_Soft) || \. U# }/ F5 E6 I/ l4 e; P$ T* W S; ~
- ((NSS) == SPI_NSS_Hard))
复制代码 7 a3 k- ~. v7 `+ U
SPI_BaudRate_Prescaler6 e" A; G: d# V( D- k
( I$ C: |2 A y, h5 f- /** @defgroup SPI_BaudRate_Prescaler 7 ~. w- p9 m( r, M6 l/ C. P+ `
- * @{
1 C3 M1 q/ Q% m9 j4 S - */6 t( I% `" i& u$ P1 { P
# ]5 W% Q4 I% e* w7 Y% E8 M1 c) K- #define SPI_BaudRatePrescaler_2 ((uint16_t)0x0000)
) R& \. J+ n9 Y V - #define SPI_BaudRatePrescaler_4 ((uint16_t)0x0008)$ p: l% ?7 J+ | H
- #define SPI_BaudRatePrescaler_8 ((uint16_t)0x0010)8 G' }( [8 v1 P$ i9 ?# ?
- #define SPI_BaudRatePrescaler_16 ((uint16_t)0x0018)
) U( ]* {" @4 ^2 `6 k" s - #define SPI_BaudRatePrescaler_32 ((uint16_t)0x0020)
7 O2 ]" ] s l) g6 N% Y) b - #define SPI_BaudRatePrescaler_64 ((uint16_t)0x0028), N0 E+ U9 f% r% {5 e3 Q
- #define SPI_BaudRatePrescaler_128 ((uint16_t)0x0030)
/ D& _! X; t q7 B) P/ f) r3 X) \ - #define SPI_BaudRatePrescaler_256 ((uint16_t)0x0038)1 l0 }! ~1 E& L7 n1 I7 b' l
- #define IS_SPI_BAUDRATE_PRESCALER(PRESCALER) (((PRESCALER) == SPI_BaudRatePrescaler_2) || \# X% B# } a4 J( T
- ((PRESCALER) == SPI_BaudRatePrescaler_4) || \4 J$ Q7 W: r6 L' T
- ((PRESCALER) == SPI_BaudRatePrescaler_8) || \2 t k4 w% z' S6 o. b: B5 a# p. J4 I
- ((PRESCALER) == SPI_BaudRatePrescaler_16) || \
% y* l) @& f7 w* m9 X7 |# D - ((PRESCALER) == SPI_BaudRatePrescaler_32) || \
; B8 J& |1 }: U! ]2 v7 f - ((PRESCALER) == SPI_BaudRatePrescaler_64) || \
; u& v/ v" z( W0 C - ((PRESCALER) == SPI_BaudRatePrescaler_128) || \! z d- f/ m7 ^, {* v
- ((PRESCALER) == SPI_BaudRatePrescaler_256))3 h) p; Z0 _6 G& Q& [/ y
复制代码 2 |( W- X7 b* [" k) H
SPI_MSB_LSB_transmission$ f$ V+ r8 P5 e; E
" I$ K2 r* M" k1 K% ?- /** @defgroup SPI_MSB_LSB_transmission
+ Q. |, V1 W7 h- d7 t - * @{
, x- _) M$ e) i$ |1 B - */
7 b& g3 ?: ]5 g5 f8 C$ m, }) r
* F- v1 j9 v3 H! w3 l- #define SPI_FirstBit_MSB ((uint16_t)0x0000)
, K) K& t! m. l - #define SPI_FirstBit_LSB ((uint16_t)0x0080)' g) t9 }7 h. J5 U* ?9 t3 t2 D- }
- #define IS_SPI_FIRST_BIT(BIT) (((BIT) == SPI_FirstBit_MSB) || \. ]) S q1 o& I9 p0 j3 E7 B
- ((BIT) == SPI_FirstBit_LSB))
复制代码 ( n% l( ^+ m. o. {. `7 P3 N0 N, c
SPI_I2S_Mode
+ T3 D" j( h# `( q2 @ n* D' D
4 U, ?2 E' I9 F/ q" B- /** @defgroup SPI_I2S_Mode
2 r/ G. U" X1 x8 f/ V" Z$ d1 m - * @{
* g& T/ A( C& t6 X/ m - */ Y6 Y% \/ g. B
- 7 E- g$ @- p7 h. |2 |, t1 T6 m
- #define I2S_Mode_SlaveTx ((uint16_t)0x0000)
% ?" h' f1 t6 G2 w% S - #define I2S_Mode_SlaveRx ((uint16_t)0x0100)
: ~ ^7 b% o0 v7 c6 B - #define I2S_Mode_MasterTx ((uint16_t)0x0200)" t! F/ R7 B. Z R! R% j
- #define I2S_Mode_MasterRx ((uint16_t)0x0300)
N! E, m7 j$ j3 N; O - #define IS_I2S_MODE(MODE) (((MODE) == I2S_Mode_SlaveTx) || \
, p/ [ N0 W7 \) X3 c - ((MODE) == I2S_Mode_SlaveRx) || \
( B; |' v" D1 j! z, }; L - ((MODE) == I2S_Mode_MasterTx)|| \2 Y% w. p- ^% P" k6 p0 p, ^! @
- ((MODE) == I2S_Mode_MasterRx))
复制代码
8 ~6 b3 P5 a' t6 q$ wSPI_I2S_Standard: O. P+ ?; n* l# f+ r
* U- s3 p/ |; `6 p# C, a# A- ; |" C0 a1 }4 D6 a8 O
- /** @defgroup SPI_I2S_Standard $ e( _. C( a( H- X/ j1 C
- * @{
! k) o A- ?* R# ~ - */8 x, r& a4 D( Y2 ]% J e
, P. B1 u! y3 U- #define I2S_Standard_Phillips ((uint16_t)0x0000)
. q/ E5 ~/ F" O" x - #define I2S_Standard_MSB ((uint16_t)0x0010)' m$ ]' E! X- x; _9 d7 i
- #define I2S_Standard_LSB ((uint16_t)0x0020)
8 P1 V( Y0 \5 j9 T: ^ - #define I2S_Standard_PCMShort ((uint16_t)0x0030)" b; `3 i4 l, j1 I$ x1 q
- #define I2S_Standard_PCMLong ((uint16_t)0x00B0)
E. ~* D' y$ K1 i3 P* }9 Y7 F7 {, g - #define IS_I2S_STANDARD(STANDARD) (((STANDARD) == I2S_Standard_Phillips) || \
+ j5 ~% |1 @, o8 ~( W4 z+ z - ((STANDARD) == I2S_Standard_MSB) || \
( o: U: Z9 \& B X5 b S" a0 t - ((STANDARD) == I2S_Standard_LSB) || \: E' A2 @6 z/ P, e2 H6 e9 u. ^
- ((STANDARD) == I2S_Standard_PCMShort) || \
4 s$ S B. y3 e( q3 Y" n1 j; O - ((STANDARD) == I2S_Standard_PCMLong))
复制代码 , P0 h% [& u: m
SPI_I2S_Data_Format
; q. g1 T2 S6 M8 }+ u0 Z* g1 `, A% V! t: [7 Y% Z4 g$ y. i- Z# M& t/ B
- /** @defgroup SPI_I2S_Data_Format - Q) Y# D' @3 [ Y
- * @{' f2 R: f( R) w$ o1 ^- e
- */" e, c$ g5 ]( \" l) ^
- # f# x6 m7 T/ R* b( @ C. U
- #define I2S_DataFormat_16b ((uint16_t)0x0000)
0 U! C, Z( ?$ z0 p6 C# x" } p - #define I2S_DataFormat_16bextended ((uint16_t)0x0001)! h2 G% \6 X$ r6 R0 m7 B$ D6 c% ~
- #define I2S_DataFormat_24b ((uint16_t)0x0003)
9 s$ d* }1 K7 e- @ - #define I2S_DataFormat_32b ((uint16_t)0x0005)( b- v+ V5 {6 E9 A! D
- #define IS_I2S_DATA_FORMAT(FORMAT) (((FORMAT) == I2S_DataFormat_16b) || \! l, I1 O6 u; g! s: Y( O
- ((FORMAT) == I2S_DataFormat_16bextended) || \
/ A6 c4 ?+ j' _2 j1 ?. d - ((FORMAT) == I2S_DataFormat_24b) || \. p6 Z# H2 Q( k. o' M
- ((FORMAT) == I2S_DataFormat_32b))
复制代码
* s0 `1 O& f, `4 H$ N" iSPI_I2S_MCLK_Output
: `5 q, s- @ X9 v; z
% A! e' M7 C0 h/ G+ d2 \- /** @defgroup SPI_I2S_MCLK_Output 1 A% H" G. u/ r2 h+ `/ }& J+ f
- * @{8 B: ?1 Z: `4 c! g# u( @% h' z
- */" E3 g+ c" ~! ^' n1 z p
- 2 |' U% a) X1 U
- #define I2S_MCLKOutput_Enable ((uint16_t)0x0200)
& V8 l5 r( H) l+ \ - #define I2S_MCLKOutput_Disable ((uint16_t)0x0000)* Q2 a6 [6 w5 {. n* T/ H
- #define IS_I2S_MCLK_OUTPUT(OUTPUT) (((OUTPUT) == I2S_MCLKOutput_Enable) || \
l" n, d2 @5 }0 Q - ((OUTPUT) == I2S_MCLKOutput_Disable))
复制代码 0 X8 V) [: f6 y/ j# g( ]' q* q
SPI_I2S_Audio_Frequency4 C- j2 x P1 _: g8 N( q
; z2 n) X: s6 K+ P
- /** @defgroup SPI_I2S_Audio_Frequency
9 A, X! q$ r p2 s% r$ k - * @{! ^2 ^$ P% b. \0 c
- */
2 l1 p2 s8 e( d6 @) ]+ W' k - - [! c4 c: S) t8 v) F9 ?* F
- #define I2S_AudioFreq_192k ((uint32_t)192000)$ \' p$ T4 A: W# w
- #define I2S_AudioFreq_96k ((uint32_t)96000)
- D3 e' y6 \1 m6 G5 K - #define I2S_AudioFreq_48k ((uint32_t)48000)
5 X9 l- J9 y( e - #define I2S_AudioFreq_44k ((uint32_t)44100)3 @7 Y- [/ t% J, r6 U- D
- #define I2S_AudioFreq_32k ((uint32_t)32000): E$ }! h& ~* c2 N
- #define I2S_AudioFreq_22k ((uint32_t)22050)# Y8 a0 Y; W6 g8 }1 O
- #define I2S_AudioFreq_16k ((uint32_t)16000)# k, p2 w# d: @/ M$ H& j
- #define I2S_AudioFreq_11k ((uint32_t)11025)
5 p$ P+ f: ~- {0 v+ t - #define I2S_AudioFreq_8k ((uint32_t)8000)( [/ p, d* O0 F$ |" L! Q
- #define I2S_AudioFreq_Default ((uint32_t)2)
$ R; z, A" W2 j' ?' I8 }
) r. d4 b% A4 }5 y5 U- #define IS_I2S_AUDIO_FREQ(FREQ) ((((FREQ) >= I2S_AudioFreq_8k) && \! g/ ?! o; s* U' q* ?: ?% A: B
- ((FREQ) <= I2S_AudioFreq_192k)) || \
% g2 s7 S' ?0 U" B - ((FREQ) == I2S_AudioFreq_Default))
复制代码
1 n$ |, h+ U e2 l2 C" S' }SPI_I2S_Clock_Polarity
! [" ]" m$ _$ }7 u F2 T7 j; x. t4 V& v' r- y
- /** @defgroup SPI_I2S_Clock_Polarity $ n3 s# @; O, S9 T) e
- * @{" C! B; t. Q2 s/ d- X
- */
$ m" B& k5 \/ A& F9 ?+ }0 _ - 6 P% }2 j1 Y" F7 e2 b
- #define I2S_CPOL_Low ((uint16_t)0x0000)& ?- p& ~7 K: b3 o2 W. v
- #define I2S_CPOL_High ((uint16_t)0x0008)
- x) R5 ~+ t5 A% \ - #define IS_I2S_CPOL(CPOL) (((CPOL) == I2S_CPOL_Low) || \ G1 c" ]% u+ I6 \( z1 q! Q
- ((CPOL) == I2S_CPOL_High))
复制代码
$ e/ l$ ]7 F$ L2 j( v/ S7 m9 \SPI_I2S_DMA_transfer_requests) u9 a: u2 B' S4 n* M5 J
* y" d: x1 V0 @
- /** @defgroup SPI_I2S_DMA_transfer_requests
2 p9 D2 c9 b3 @) }' M+ _2 @ - * @{
9 i# _$ [8 V7 m7 v# Q - */
1 d! i2 @5 L# w+ J9 N2 F3 c7 D1 o5 [
2 @+ R$ P2 ~* c/ ?- #define SPI_I2S_DMAReq_Tx ((uint16_t)0x0002)
4 X* q1 R8 N! ~/ I9 c - #define SPI_I2S_DMAReq_Rx ((uint16_t)0x0001)+ C+ H+ g5 A% ~; |+ |' e
- #define IS_SPI_I2S_DMAREQ(DMAREQ) ((((DMAREQ) & (uint16_t)0xFFFC) == 0x00) && ((DMAREQ) != 0x00))
复制代码 ) Q7 k) a, S6 J6 u3 F* U8 b8 a
SPI_NSS_internal_software_management
9 Y$ Q7 Q& u' L I$ S
0 Q* p, {1 @. \+ y- /** @defgroup SPI_NSS_internal_software_management l3 v# @. \( W) W6 R
- * @{/ Z e$ ~" g5 C9 D; ?, } Z+ \
- */3 e# ~' I- b: h2 z4 G! [( W
- }7 ^; X; }% M- #define SPI_NSSInternalSoft_Set ((uint16_t)0x0100)
/ ?3 T( `, ]2 d/ c+ O - #define SPI_NSSInternalSoft_Reset ((uint16_t)0xFEFF)0 ?" P& l5 _! _/ E- h& ]0 ?
- #define IS_SPI_NSS_INTERNAL(INTERNAL) (((INTERNAL) == SPI_NSSInternalSoft_Set) || \" t" A9 w8 C& O; s: W) O& q
- ((INTERNAL) == SPI_NSSInternalSoft_Reset))
复制代码
2 K) y5 b' r! R: F: Z! ?( NSPI_CRC_Transmit_Receive
: A# p1 L% y3 v, [, V+ I# U# ]
2 {3 @8 h. r( Y/ N% j# P- /** @defgroup SPI_CRC_Transmit_Receive
$ B4 [/ n" k9 s3 ^+ Q q: V - * @{
+ S e; O$ A3 }( x - */9 ] h) O) u6 C) N2 ~. I
! ~% D% C/ m' l- #define SPI_CRC_Tx ((uint8_t)0x00)5 Q. ~# l% m7 G" L/ u
- #define SPI_CRC_Rx ((uint8_t)0x01)$ M7 G- t) u8 ^7 z g
- #define IS_SPI_CRC(CRC) (((CRC) == SPI_CRC_Tx) || ((CRC) == SPI_CRC_Rx))
复制代码 |* C% Y1 n* N2 v
SPI_direction_transmit_receive
$ q z4 u1 N2 C7 W- e% G0 d$ P0 F2 x% o! m
- /** @defgroup SPI_direction_transmit_receive
4 a8 ?# K0 ~. J* G8 ^ - * @{
' j; L% A$ E' a* t' A$ } - */
' v- c- `- V5 `( e - 5 Q6 K) F' i/ u+ m1 I* N/ E
- #define SPI_Direction_Rx ((uint16_t)0xBFFF)5 [6 Q; m. z8 e$ I; ]
- #define SPI_Direction_Tx ((uint16_t)0x4000)
# }6 d! w2 r( l- w5 N - #define IS_SPI_DIRECTION(DIRECTION) (((DIRECTION) == SPI_Direction_Rx) || \
; u" ?4 l" o; U& r) ? - ((DIRECTION) == SPI_Direction_Tx))
复制代码 # q- f+ [# F, K2 A0 ]
SPI_I2S_interrupts_definition6 ?# b. w: ?% @! ~
; l* G! s8 G! }* h4 c9 j: R- /** @defgroup SPI_I2S_interrupts_definition " V0 y. ?1 Y, C; F
- * @{' S! h# \& l/ `" S* |/ F" e
- */
0 r" q! m0 v% P* q1 d
& L4 L% f- |& {2 h6 \9 p- #define SPI_I2S_IT_TXE ((uint8_t)0x71)2 R6 i7 @( r' ~* n/ W; u& V
- #define SPI_I2S_IT_RXNE ((uint8_t)0x60)
4 k9 K, {4 I* U y# B0 I - #define SPI_I2S_IT_ERR ((uint8_t)0x50)0 p4 C; B. ^& w' Q1 a% i% ~9 H
- #define I2S_IT_UDR ((uint8_t)0x53)* j0 R/ L4 L \$ q) Q
- #define SPI_I2S_IT_TIFRFE ((uint8_t)0x58)
& p/ W! t& \$ B& ]# b - 7 h. b! N) t. F& v1 v; n p
- #define IS_SPI_I2S_CONFIG_IT(IT) (((IT) == SPI_I2S_IT_TXE) || \$ _5 E# F$ V( ]
- ((IT) == SPI_I2S_IT_RXNE) || \
- ]$ {1 m8 g4 B' {7 J - ((IT) == SPI_I2S_IT_ERR))( u" {1 i( s5 ^$ @) h) G
0 D8 G. E; Z4 Z; ~- #define SPI_I2S_IT_OVR ((uint8_t)0x56)7 `! ~6 O0 i: J4 r, J1 h7 {9 Y
- #define SPI_IT_MODF ((uint8_t)0x55)5 m8 E5 l. D' a
- #define SPI_IT_CRCERR ((uint8_t)0x54)- h: E8 j d, ]6 |5 c$ J
+ W0 a/ D% M8 ~$ I% {1 o- #define IS_SPI_I2S_CLEAR_IT(IT) (((IT) == SPI_IT_CRCERR))
& N% m& d% o% o: i S, l" l/ A - ) y- @: F$ c* F) `- _. k
- #define IS_SPI_I2S_GET_IT(IT) (((IT) == SPI_I2S_IT_RXNE)|| ((IT) == SPI_I2S_IT_TXE) || \
7 K/ e* r: ]) x& e* ~! P - ((IT) == SPI_IT_CRCERR) || ((IT) == SPI_IT_MODF) || \
- s! c7 |% ]& z" ]3 C - ((IT) == SPI_I2S_IT_OVR) || ((IT) == I2S_IT_UDR) ||\9 M4 Z& k: b+ M- ]- }9 m& Q) U
- ((IT) == SPI_I2S_IT_TIFRFE))
复制代码
& i6 E8 O& _( O3 E3 b, n$ X5 SSPI_I2S_flags_definition
: h$ r0 K) P# E4 Q& u& D% l" _8 A. Q% \0 p, O0 i: p
- /** @defgroup SPI_I2S_flags_definition
' h8 e+ p: O c7 v - * @{: {0 t! g5 q1 S3 s
- */
! G8 X+ j* ^: }. H4 a( g5 D. m5 y
4 g- q+ E5 J, ]4 ?9 O N, g- Z- #define SPI_I2S_FLAG_RXNE ((uint16_t)0x0001)
7 x" ^. X8 H7 o' G. p2 w - #define SPI_I2S_FLAG_TXE ((uint16_t)0x0002)/ |$ }# @5 ?7 i) o: u0 K% {* i
- #define I2S_FLAG_CHSIDE ((uint16_t)0x0004)2 P1 p; G" s+ ]' P9 R5 S
- #define I2S_FLAG_UDR ((uint16_t)0x0008)
2 Z7 d% c8 F* t9 \! D6 X; X - #define SPI_FLAG_CRCERR ((uint16_t)0x0010)
' _" B5 ]3 W# L! |! S) n$ N3 G% { x - #define SPI_FLAG_MODF ((uint16_t)0x0020)
. {; E: J8 W$ Z2 c4 y# u8 K - #define SPI_I2S_FLAG_OVR ((uint16_t)0x0040)
! a+ q1 P; V5 I4 }- Q+ k9 g! a - #define SPI_I2S_FLAG_BSY ((uint16_t)0x0080)
. w4 }; F# S& s - #define SPI_I2S_FLAG_TIFRFE ((uint16_t)0x0100)
! d! w7 z* [, C
7 c. M6 A3 }6 l( q2 U: Z# f- #define IS_SPI_I2S_CLEAR_FLAG(FLAG) (((FLAG) == SPI_FLAG_CRCERR))
6 L) j$ R) c" a7 z+ ]- q9 N - #define IS_SPI_I2S_GET_FLAG(FLAG) (((FLAG) == SPI_I2S_FLAG_BSY) || ((FLAG) == SPI_I2S_FLAG_OVR) || \
9 f/ G2 x: A! V8 x% ]7 m - ((FLAG) == SPI_FLAG_MODF) || ((FLAG) == SPI_FLAG_CRCERR) || \6 v4 l3 r+ u" V! ]& n6 r+ i+ u5 g
- ((FLAG) == I2S_FLAG_UDR) || ((FLAG) == I2S_FLAG_CHSIDE) || \
& o$ r4 R9 K: l6 }# m8 x - ((FLAG) == SPI_I2S_FLAG_TXE) || ((FLAG) == SPI_I2S_FLAG_RXNE)|| \
* p2 X" i! e) A - ((FLAG) == SPI_I2S_FLAG_TIFRFE))
复制代码 3 u# `! E- d2 ~$ S- Q
03. 相关函数
, V; U7 q" b8 G$ R; u- /* Exported macro ------------------------------------------------------------*/7 u& [7 B! Z1 T" t
- /* Exported functions --------------------------------------------------------*/ |- ?$ N+ K; ]7 ]' m+ v% p
/ }! }/ Y9 v' n/ z% i- /* Function used to set the SPI configuration to the default reset state *****/ , P6 a! |& j0 s7 E% V$ [
- void SPI_I2S_DeInit(SPI_TypeDef* SPIx);
3 n9 M' u5 B9 H4 K( C; s - 6 y+ B4 H# _7 z7 ]! A( Q5 k
- /* Initialization and Configuration functions *********************************/
" {- G8 A. e" }$ _6 F+ c( p; Z - void SPI_Init(SPI_TypeDef* SPIx, SPI_InitTypeDef* SPI_InitStruct);
7 q9 ^8 o) F. V3 U: l5 ]- }/ r - void I2S_Init(SPI_TypeDef* SPIx, I2S_InitTypeDef* I2S_InitStruct);
& m6 D, {( E- j7 K' H - void SPI_StructInit(SPI_InitTypeDef* SPI_InitStruct);. x8 M3 d; C: I8 S9 w7 [
- void I2S_StructInit(I2S_InitTypeDef* I2S_InitStruct);
9 Q2 Y- c' n$ k. |+ C- D" m - void SPI_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState);
3 Z+ Z7 \( U+ G: y! E - void I2S_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState);
) U& ]& s+ ?+ r& r a - void SPI_DataSizeConfig(SPI_TypeDef* SPIx, uint16_t SPI_DataSize);* _! c6 O9 i$ d9 w9 u" B( |, _6 l
- void SPI_BiDirectionalLineConfig(SPI_TypeDef* SPIx, uint16_t SPI_Direction);, q( t" v7 B( L& p" y8 X
- void SPI_NSSInternalSoftwareConfig(SPI_TypeDef* SPIx, uint16_t SPI_NSSInternalSoft);/ L' J+ a) d! r- Y1 ]
- void SPI_SSOutputCmd(SPI_TypeDef* SPIx, FunctionalState NewState);; z1 E! s# p( q
- void SPI_TIModeCmd(SPI_TypeDef* SPIx, FunctionalState NewState);
$ \9 n' P# t' \8 C( z - . C% H2 l* Q E: B- l- n
- void I2S_FullDuplexConfig(SPI_TypeDef* I2Sxext, I2S_InitTypeDef* I2S_InitStruct);
7 ]' k* j) l1 ^$ A; g
% ]7 P, J% {5 i' T- /* Data transfers functions ***************************************************/ / T% v9 I) a9 i2 k7 ?0 d$ P* k
- void SPI_I2S_SendData(SPI_TypeDef* SPIx, uint16_t Data);
1 W0 J3 W& M/ R6 F - uint16_t SPI_I2S_ReceiveData(SPI_TypeDef* SPIx);+ a. b% @5 E1 T3 ]" }
1 {: V- p. L, V$ X) u7 x- /* Hardware CRC Calculation functions *****************************************/
4 k1 j( A4 ?! ~( L% u - void SPI_CalculateCRC(SPI_TypeDef* SPIx, FunctionalState NewState);" J1 {" U; i9 T" Y6 ^& O* H
- void SPI_TransmitCRC(SPI_TypeDef* SPIx);
. Q/ r7 @5 q* L y - uint16_t SPI_GetCRC(SPI_TypeDef* SPIx, uint8_t SPI_CRC);* r, b- w7 Y O9 I- ]
- uint16_t SPI_GetCRCPolynomial(SPI_TypeDef* SPIx);
: Z1 s' F$ z" I, Y. ?: F - , {: P$ Z; |1 N
- /* DMA transfers management functions *****************************************/. j& G& _ b( V: A
- void SPI_I2S_DMACmd(SPI_TypeDef* SPIx, uint16_t SPI_I2S_DMAReq, FunctionalState NewState);4 E) n; v4 }; g/ A
. Y( u4 F! a6 p# ?. W& J$ a- /* Interrupts and flags management functions **********************************/
4 ?( x2 G/ {% C) a - void SPI_I2S_ITConfig(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT, FunctionalState NewState);
5 N* O- x: L7 t% i% a3 _ - FlagStatus SPI_I2S_GetFlagStatus(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG);/ Y8 a& R2 i7 H) _8 ~
- void SPI_I2S_ClearFlag(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG);! p' {0 H! }. X; k( A/ r
- ITStatus SPI_I2S_GetITStatus(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT);1 F5 M8 s, T8 U5 ~ N
- void SPI_I2S_ClearITPendingBit(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT);
复制代码
5 @8 Z! c0 p& B) V04. 结构体封装% I# \$ f4 v5 r0 M
- /** 4 r; X. k. @4 a# ^ E( D9 r4 d* X' y
- * @brief Serial Peripheral Interface
- ~/ ?* X" J! @7 ^5 Y - */
x$ |% e/ E1 _
# t, M4 O3 M4 v& L- typedef struct7 b3 h7 a* k o4 Q
- {; Y1 ^* i* B" A5 J y
- __IO uint16_t CR1; /*!< SPI control register 1 (not used in I2S mode), Address offset: 0x00 */; x) C6 F, p! y K3 [* h
- uint16_t RESERVED0; /*!< Reserved, 0x02 */# |5 D( Q4 t6 ?: d$ W; }: ?+ Z8 N
- __IO uint16_t CR2; /*!< SPI control register 2, Address offset: 0x04 */
+ y H3 u8 ]$ h9 A5 X - uint16_t RESERVED1; /*!< Reserved, 0x06 */
' I# _: z7 n4 y4 G3 [7 } r - __IO uint16_t SR; /*!< SPI status register, Address offset: 0x08 */
# Q6 o4 P) o6 a - uint16_t RESERVED2; /*!< Reserved, 0x0A */
5 M; n9 O0 W) Z6 A& p1 ^8 B! Z - __IO uint16_t DR; /*!< SPI data register, Address offset: 0x0C */1 F& b! M* V+ R, M" `9 h [( ^
- uint16_t RESERVED3; /*!< Reserved, 0x0E */
+ f* s9 R" T( r* ]; K" u - __IO uint16_t CRCPR; /*!< SPI CRC polynomial register (not used in I2S mode), Address offset: 0x10 */
6 ?* L! e- R0 w+ Y( C" n - uint16_t RESERVED4; /*!< Reserved, 0x12 */" N' u- p. j z% }$ R
- __IO uint16_t RXCRCR; /*!< SPI RX CRC register (not used in I2S mode), Address offset: 0x14 */) l0 o; y g2 n- B
- uint16_t RESERVED5; /*!< Reserved, 0x16 */
) H; h, E/ h, q5 K0 @ - __IO uint16_t TXCRCR; /*!< SPI TX CRC register (not used in I2S mode), Address offset: 0x18 */
8 o( |8 c/ W/ @+ C6 E4 a - uint16_t RESERVED6; /*!< Reserved, 0x1A */7 g# k1 N9 q* |4 d; ~. X
- __IO uint16_t I2SCFGR; /*!< SPI_I2S configuration register, Address offset: 0x1C */
F; V9 @( E0 l9 Q, ^ - uint16_t RESERVED7; /*!< Reserved, 0x1E */
0 w/ r( }+ X h; d( a- Z/ j - __IO uint16_t I2SPR; /*!< SPI_I2S prescaler register, Address offset: 0x20 */! s- F, Z* y# @! J1 O) ]! I% _, n
- uint16_t RESERVED8; /*!< Reserved, 0x22 */
$ o+ Z2 g# f, n* @6 g% N- S - } SPI_TypeDef;
复制代码 1 p5 S [* N+ M. h2 a) f# P/ T) }
! Y; p( r' v, _& v7 l; ^ ^; n0 Y& J0 h3 g9 h" i; |
|