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