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