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