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