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