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