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