你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】SPI相关函数和类型

[复制链接]
STMCU小助手 发布时间:2022-3-27 17:00
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
  1. /** ! j7 ^# ]/ X8 L% w3 M9 R6 l8 m
  2.   * @brief  SPI Init structure definition  
    ) e; h! F; E( P% w. d6 o5 X/ R
  3.   */1 t* u5 Y7 U, r# o

  4. ; S  y. P. N! i- n5 E6 f
  5. typedef struct1 c" h8 O, _7 r( @/ i
  6. {
    ( M- J9 J# y7 P4 h* }+ ]0 R
  7.   uint16_t SPI_Direction;           /*!< Specifies the SPI unidirectional or bidirectional data mode.
    * c' d0 Q" h3 [" p" {
  8.                                          This parameter can be a value of @ref SPI_data_direction */
    ' G: Q4 m7 ?- [- G* y% w" L
  9. / {, h$ R) f" c) U$ r
  10.   uint16_t SPI_Mode;                /*!< Specifies the SPI operating mode.- h3 a7 e  G5 x) D: n
  11.                                          This parameter can be a value of @ref SPI_mode */2 x( O# X) q3 u( h

  12. ( N# E0 {& Q4 |$ q( A$ O
  13.   uint16_t SPI_DataSize;            /*!< Specifies the SPI data size.# _- g, d9 K2 c6 S4 e4 h
  14.                                          This parameter can be a value of @ref SPI_data_size */" m" L' T; p0 ?* G) z5 q4 W

  15. # d/ G! v( X) `: s! E1 {
  16.   uint16_t SPI_CPOL;                /*!< Specifies the serial clock steady state.
    4 R+ {: z* K( M) n9 G$ o& b* V
  17.                                          This parameter can be a value of @ref SPI_Clock_Polarity */* u1 S( ]9 G( \  y) f. W

  18. 3 m* x' V: z6 q6 o# r. X; J; k' k5 d0 C
  19.   uint16_t SPI_CPHA;                /*!< Specifies the clock active edge for the bit capture.
    8 m" ~0 r, Z. J3 b
  20.                                          This parameter can be a value of @ref SPI_Clock_Phase */
    : K4 T- N) p7 y8 J

  21. " k5 I5 `/ Y9 G! G. E8 `7 c( ]
  22.   uint16_t SPI_NSS;                 /*!< Specifies whether the NSS signal is managed by
    - o$ t( l6 C5 o2 n+ k  F( g
  23.                                          hardware (NSS pin) or by software using the SSI bit.2 f+ `. r/ z. U* {
  24.                                          This parameter can be a value of @ref SPI_Slave_Select_management */
    . S% W0 A5 i' x- a

  25. # s& e" y; d; h4 n
  26.   uint16_t SPI_BaudRatePrescaler;   /*!< Specifies the Baud Rate prescaler value which will be7 P- W4 T2 m! n3 J; P% u2 C9 b
  27.                                          used to configure the transmit and receive SCK clock.
    7 X) Z% M$ r- b% C2 o+ u: F" @0 W
  28.                                          This parameter can be a value of @ref SPI_BaudRate_Prescaler
      C: s! w5 _" A8 W' f% D- Z1 r7 _2 F
  29.                                          @note The communication clock is derived from the master
    # B3 \8 O4 f) ~
  30.                                                clock. The slave clock does not need to be set. */0 R  l/ Y3 o; p; l
  31. * ]% h' Y- T- F# f; o5 `+ L9 a3 u
  32.   uint16_t SPI_FirstBit;            /*!< Specifies whether data transfers start from MSB or LSB bit., f5 u( D/ [$ K0 [# s4 M+ P9 R
  33.                                          This parameter can be a value of @ref SPI_MSB_LSB_transmission */
    ' j4 `- i7 U5 d
  34. 7 a. N1 c5 f# y' z
  35.   uint16_t SPI_CRCPolynomial;       /*!< Specifies the polynomial used for the CRC calculation. */
    * l, O6 k8 R" ^9 W
  36. }SPI_InitTypeDef;
复制代码

) q" ^6 ~! O% H0 H/ CI2S Init structure definition
1 N: T  d; e, P9 C3 V  G  Q: a+ i2 y
  1. /**
    , A$ ]: ]% ~  |1 ?
  2.   * @brief  I2S Init structure definition    p6 X- b. ~. v7 f2 X2 z
  3.   */
    2 F/ a- l9 D- i8 I. R
  4. - ~  j, l( t+ x1 l) C! n
  5. typedef struct
    , m) M9 N& n9 D
  6. {  n% D" E3 {4 [( X# B& s

  7. : m# W" D. x9 _8 t- p
  8.   uint16_t I2S_Mode;         /*!< Specifies the I2S operating mode.. g; ~" X9 d& O& v0 \* F: y
  9.                                   This parameter can be a value of @ref I2S_Mode */4 _; O9 l6 \8 z4 t0 e5 q, y% Q
  10.   D8 e) u4 k3 c- V% ]
  11.   uint16_t I2S_Standard;     /*!< Specifies the standard used for the I2S communication.) J4 ?2 S3 D4 N; ?  J- {
  12.                                   This parameter can be a value of @ref I2S_Standard */
    ) F6 u# m) L* Q1 `6 P( l' A; v7 }

  13. 9 w7 U3 A/ D# m
  14.   uint16_t I2S_DataFormat;   /*!< Specifies the data format for the I2S communication.
    . K9 s% N- f- M; B/ \2 K& x7 T
  15.                                   This parameter can be a value of @ref I2S_Data_Format */
    ! W, a7 I3 r* u1 ~

  16. ! h3 p8 R7 [- f4 t! `( d: I
  17.   uint16_t I2S_MCLKOutput;   /*!< Specifies whether the I2S MCLK output is enabled or not.
    : `+ |- d9 q) M
  18.                                   This parameter can be a value of @ref I2S_MCLK_Output */
    + O! z2 d( U5 [7 ~( \

  19. . c" [5 X# t3 i% S$ t/ B, v  w
  20.   uint32_t I2S_AudioFreq;    /*!< Specifies the frequency selected for the I2S communication.
    3 F2 G  W2 ?$ A$ L
  21.                                   This parameter can be a value of @ref I2S_Audio_Frequency */
    0 G# i* V2 t" j; u2 U4 z0 s6 u
  22. 1 m; v0 {  z# h  N; |* W0 G( _7 T' n
  23.   uint16_t I2S_CPOL;         /*!< Specifies the idle state of the I2S clock.8 T9 k8 f! h' `+ a
  24.                                   This parameter can be a value of @ref I2S_Clock_Polarity */  k4 c1 H4 b8 e
  25. }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 _
  1. /** @defgroup SPI_Exported_Constants
    # U8 \% `; l9 ]6 z, a
  2.   * @{
    + Z) m" i0 S' t) b
  3.   */+ s. h1 Z$ j" n* b; |& K# s+ u
  4. . _1 l$ a/ c7 ^/ c4 _. A
  5. #define IS_SPI_ALL_PERIPH(PERIPH) (((PERIPH) == SPI1) || \
    ! l3 o0 n3 D; q" g+ w1 s6 z! h
  6.                                    ((PERIPH) == SPI2) || \4 q8 _0 e5 V& T0 @, c3 M
  7.                                    ((PERIPH) == SPI3) || \
    # r% s4 j3 l# V, P
  8.                                    ((PERIPH) == SPI4) || \9 U; _, Y* I6 }  a: A
  9.                                    ((PERIPH) == SPI5) || \
    ! ]; a/ N- }; T4 ?" S
  10.                                    ((PERIPH) == SPI6))
    7 b- U# U9 b6 s6 f: W1 K# F

  11. 7 Q+ U, h  a7 {7 ~
  12. #define IS_SPI_ALL_PERIPH_EXT(PERIPH) (((PERIPH) == SPI1)    || \
    4 M* r8 {( d. n
  13.                                        ((PERIPH) == SPI2)    || \2 s4 v. J; t; ?- ~1 v
  14.                                        ((PERIPH) == SPI3)    || \1 H! B2 {9 H7 n( ~
  15.                                        ((PERIPH) == SPI4)    || \/ B2 e( s# ~3 z; {0 \
  16.                                        ((PERIPH) == SPI5)    || \. F0 v% s$ v) j  F
  17.                                        ((PERIPH) == SPI6)    || \
    , W1 F9 P3 C( _3 _/ }1 M6 C: c# N
  18.                                        ((PERIPH) == I2S2ext) || \
    % R5 t$ q0 ?" P, U
  19.                                        ((PERIPH) == I2S3ext))
    - e2 L& r+ Y0 [) p% }

  20.   [8 w, S* l/ X2 R6 }& L& {* z) a
  21. #define IS_SPI_23_PERIPH(PERIPH)  (((PERIPH) == SPI2) || \0 f2 Q. p: l) A1 B# F% Q% I2 @
  22.                                    ((PERIPH) == SPI3))
      O1 p. G: R0 n) E/ E, X
  23. 2 T( p+ ]$ T4 |7 W
  24. #define IS_SPI_23_PERIPH_EXT(PERIPH)  (((PERIPH) == SPI2)    || \. \3 h  M" C" \4 ^! B+ W
  25.                                        ((PERIPH) == SPI3)    || \
    ; z3 o7 Y. d' z0 x/ E, @4 i
  26.                                        ((PERIPH) == I2S2ext) || \* k  R) _$ b. d, q# A7 g
  27.                                        ((PERIPH) == I2S3ext))
    2 X7 `# d6 ]7 S. i5 \; s. J4 w
  28. 5 l" v+ }) Q3 W" L6 i. y: N
  29. #define IS_I2S_EXT_PERIPH(PERIPH)  (((PERIPH) == I2S2ext) || \
    ! d9 S5 u. |# G  H) K+ g- U* t9 `
  30.                                     ((PERIPH) == I2S3ext))
复制代码
3 j* Y$ K: R/ G) Q
SPI_data_direction
8 \# C0 d  \! z1 M6 P8 h
  1. /** @defgroup SPI_data_direction . U+ J1 t, r8 H. H) Q
  2.   * @{
    / i  f: r# k( k; K9 ?, W4 l# x/ T
  3.   */4 L$ f+ J+ R) k

  4. # [& D! K; C$ n' w/ D0 `( ]# V  [
  5. #define SPI_Direction_2Lines_FullDuplex ((uint16_t)0x0000)8 y- J% a7 e! Z$ k% P; r) S
  6. #define SPI_Direction_2Lines_RxOnly     ((uint16_t)0x0400)
    1 R5 }! C- c5 T: U( {5 ^) W+ |. B8 j3 M
  7. #define SPI_Direction_1Line_Rx          ((uint16_t)0x8000)
    - D8 r6 W# E, r, @- @7 ?0 U
  8. #define SPI_Direction_1Line_Tx          ((uint16_t)0xC000). n% E3 n0 m& I; v* f' @3 @
  9. #define IS_SPI_DIRECTION_MODE(MODE) (((MODE) == SPI_Direction_2Lines_FullDuplex) || \) i. ?- d2 S7 x' Q
  10.                                      ((MODE) == SPI_Direction_2Lines_RxOnly) || \! Q! i6 j0 r: y2 S
  11.                                      ((MODE) == SPI_Direction_1Line_Rx) || \
    * j4 Y' T7 ~$ {% \- |  I) `
  12.                                      ((MODE) == SPI_Direction_1Line_Tx))
复制代码

4 |+ \3 U( e' i5 H9 B6 K# a/ RSPI_mode) ?+ Z" Y, d5 R+ ~' j
  1. /** @defgroup SPI_mode 2 D, h: h* a  j6 f2 b
  2.   * @{
    ! Z& C+ ^$ Q& h; }) u" i! l
  3.   */" ~+ {  B+ l; P5 i9 y

  4. : [( x6 ?$ s0 q& b: ^
  5. #define SPI_Mode_Master                 ((uint16_t)0x0104)" Q3 U7 l& ]* E3 |2 o( E1 x2 X
  6. #define SPI_Mode_Slave                  ((uint16_t)0x0000)- ]8 y0 p% R) g( c7 t, ~. [
  7. #define IS_SPI_MODE(MODE) (((MODE) == SPI_Mode_Master) || \; n1 ?( d/ N, T! A
  8.                            ((MODE) == SPI_Mode_Slave))
复制代码
) o/ s2 W! |' E4 G) q
SPI_data_size) t" G7 }) g5 w& V$ ?
  1. /** @defgroup SPI_data_size
    0 G8 [3 {; h  r" n! ~8 l3 S5 ]  ?; h# O
  2.   * @{: ~9 I( o' B3 |; n$ O6 `1 \/ E
  3.   */5 q. v  S5 N8 q3 E9 G5 K! g7 y
  4. ) D, P6 f  s! D) _
  5. #define SPI_DataSize_16b                ((uint16_t)0x0800)
    3 _( D1 M' C; f
  6. #define SPI_DataSize_8b                 ((uint16_t)0x0000)9 K' Q5 X) w+ t  A2 c- ]7 l
  7. #define IS_SPI_DATASIZE(DATASIZE) (((DATASIZE) == SPI_DataSize_16b) || \
    3 l! N& c* D9 n6 v" g8 W: X# A
  8.                                    ((DATASIZE) == SPI_DataSize_8b))
复制代码
  i/ G3 ]$ h: v( y/ Y, k
SPI_Clock_Polarity
: W/ N* h1 V1 ?: l
  1. /** @defgroup SPI_Clock_Polarity 2 b3 \6 c$ K: V* z0 K# H
  2.   * @{
    6 y. S* u% g  C, I
  3.   */* v% _, x* M7 \6 H1 H

  4. . S6 M! @9 Z3 V" |% f
  5. #define SPI_CPOL_Low                    ((uint16_t)0x0000)# p( K( b1 K2 r( W
  6. #define SPI_CPOL_High                   ((uint16_t)0x0002)% U) I4 o# A( v+ ]2 s! F8 n* g
  7. #define IS_SPI_CPOL(CPOL) (((CPOL) == SPI_CPOL_Low) || \1 D- r( D, Y9 S! S8 A9 p
  8.                            ((CPOL) == SPI_CPOL_High))
复制代码

* M( |" U' j8 V- r3 J+ R/ W# jSPI_Clock_Phase
; J  }5 N/ l$ Z& Q1 @( o$ X% f
  1. /** @defgroup SPI_Clock_Phase + a+ P6 L, {$ \1 M/ T
  2.   * @{% C6 P( p. b; x, a; c% L
  3.   */) T5 l* o$ C- a1 }7 E

  4. ' Y, a! R7 y+ P% A) ^
  5. #define SPI_CPHA_1Edge                  ((uint16_t)0x0000)- k+ M  [# w3 }( L& \. ~7 C
  6. #define SPI_CPHA_2Edge                  ((uint16_t)0x0001)
    ' f( X$ h& i9 ]- B/ [8 A
  7. #define IS_SPI_CPHA(CPHA) (((CPHA) == SPI_CPHA_1Edge) || \
    " N  b( o* B/ @
  8.                            ((CPHA) == SPI_CPHA_2Edge))
复制代码
) q% u% k0 k) A$ u) m8 l- y
SPI_Slave_Select_management
  1. /** @defgroup SPI_Slave_Select_management , T/ W% {% X0 u- C( G: M
  2.   * @{
    " h6 U* C% }( U- r( @+ k0 I. R
  3.   *// ?5 ?) z7 |) o  G; M9 l+ G: I
  4. . n# v, \$ J4 T2 d0 h
  5. #define SPI_NSS_Soft                    ((uint16_t)0x0200)
    + D8 G+ W3 u7 R7 k9 O
  6. #define SPI_NSS_Hard                    ((uint16_t)0x0000)
    0 O# e& z4 [6 l* `
  7. #define IS_SPI_NSS(NSS) (((NSS) == SPI_NSS_Soft) || \
    5 @9 G2 ?( ]. m( W
  8.                          ((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) ~
  1. /** @defgroup SPI_BaudRate_Prescaler / C7 N( ^) F" z3 d* ]8 _
  2.   * @{
    0 s6 h% g  Z3 t: J; O- W8 T8 X
  3.   */
    6 |$ _" H* d1 j. M' v# W; h$ T0 a" x

  4. 8 H: a4 \8 w) W" K
  5. #define SPI_BaudRatePrescaler_2         ((uint16_t)0x0000)0 I5 v3 I# M6 @* H) Q
  6. #define SPI_BaudRatePrescaler_4         ((uint16_t)0x0008)
    ) a. V  d2 ]. W3 A- X, ^. x  K
  7. #define SPI_BaudRatePrescaler_8         ((uint16_t)0x0010)
    8 F, }+ C. w: U0 j
  8. #define SPI_BaudRatePrescaler_16        ((uint16_t)0x0018)
    * \& D- }' ?) |3 b* d, w% o
  9. #define SPI_BaudRatePrescaler_32        ((uint16_t)0x0020)/ m) r6 Z4 F1 g! n0 U
  10. #define SPI_BaudRatePrescaler_64        ((uint16_t)0x0028)0 G; Q, t. }- x- f5 j
  11. #define SPI_BaudRatePrescaler_128       ((uint16_t)0x0030). m: s. p) `7 r* k7 v
  12. #define SPI_BaudRatePrescaler_256       ((uint16_t)0x0038)9 y1 W) ^' Y( E+ F: Y
  13. #define IS_SPI_BAUDRATE_PRESCALER(PRESCALER) (((PRESCALER) == SPI_BaudRatePrescaler_2) || \
    - O9 o8 j5 `8 w
  14.                                               ((PRESCALER) == SPI_BaudRatePrescaler_4) || \7 C1 B) W' m" H* [2 R: \) ]" A& _: Q
  15.                                               ((PRESCALER) == SPI_BaudRatePrescaler_8) || \+ C  R) H8 o7 j7 @0 }
  16.                                               ((PRESCALER) == SPI_BaudRatePrescaler_16) || \0 W- Q4 S- J3 b1 q3 J3 c
  17.                                               ((PRESCALER) == SPI_BaudRatePrescaler_32) || \
    - d4 W* x1 X% b% R3 \3 Z& }$ `
  18.                                               ((PRESCALER) == SPI_BaudRatePrescaler_64) || \5 |5 v9 V: Y" `: D  o; |$ ?
  19.                                               ((PRESCALER) == SPI_BaudRatePrescaler_128) || \7 l. D5 s" d# a6 j6 A$ b- Y
  20.                                               ((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. \
  1. /** @defgroup SPI_MSB_LSB_transmission
    - o' l3 ]& ?. f" Q
  2.   * @{/ p8 P1 o& a" T
  3.   */
      ]" J  S; Z1 d: n
  4. ' c1 a8 H7 ]9 x7 t
  5. #define SPI_FirstBit_MSB                ((uint16_t)0x0000)6 H# c+ d% q7 j: a; W+ }1 D
  6. #define SPI_FirstBit_LSB                ((uint16_t)0x0080)
    & _5 P% q! f2 Y3 c& o' {
  7. #define IS_SPI_FIRST_BIT(BIT) (((BIT) == SPI_FirstBit_MSB) || \! e  Q! W( b4 B
  8.                                ((BIT) == SPI_FirstBit_LSB))
复制代码
; z2 }' h, a" _3 Q/ i/ z: E2 m
SPI_I2S_Mode7 |+ X# l" x0 T1 [$ s. O4 r$ ~) n
  1. /** @defgroup SPI_I2S_Mode 2 C  g7 S/ F0 k$ W
  2.   * @{+ N9 K- @" v  Y% L0 _  j$ f
  3.   */! N- F' t: U: g& p; s' S

  4. * h" [4 _8 Y) U5 O! @7 A" |( f
  5. #define I2S_Mode_SlaveTx                ((uint16_t)0x0000)# W' \) p6 W0 |( z
  6. #define I2S_Mode_SlaveRx                ((uint16_t)0x0100)) _. k2 N- S+ V4 x5 w6 V% S. g
  7. #define I2S_Mode_MasterTx               ((uint16_t)0x0200)
    3 s: q) a) M0 C
  8. #define I2S_Mode_MasterRx               ((uint16_t)0x0300)- Q5 D2 r# {5 `8 ]6 T) p8 s
  9. #define IS_I2S_MODE(MODE) (((MODE) == I2S_Mode_SlaveTx) || \
    * @0 t- i3 u; a9 E) H& E4 e% U* P
  10.                            ((MODE) == I2S_Mode_SlaveRx) || \
    ( N/ c) p% b& l* N5 n
  11.                            ((MODE) == I2S_Mode_MasterTx)|| \
    - ^; k& y7 W: X" t, Z) A/ I
  12.                            ((MODE) == I2S_Mode_MasterRx))
复制代码
0 Z0 f5 U  r3 q
SPI_I2S_Standard
0 y' s5 G( g: j) v

  1. ! m  h( z' H7 K6 l4 d% B" s; N
  2. /** @defgroup SPI_I2S_Standard
    - s; L9 R. ^; D1 x
  3.   * @{+ L5 _  K5 t$ b* D
  4.   */
    8 x  v8 H1 Q0 x
  5. ' t' a# n% D# N) h( X
  6. #define I2S_Standard_Phillips           ((uint16_t)0x0000)0 K0 O) w# j3 W) y4 ~) _
  7. #define I2S_Standard_MSB                ((uint16_t)0x0010)
    - X$ r& D$ T1 a  Q
  8. #define I2S_Standard_LSB                ((uint16_t)0x0020)
    % A3 r( O0 X6 c+ Q3 i( w& ^# L
  9. #define I2S_Standard_PCMShort           ((uint16_t)0x0030)  M# s0 V5 M6 z' N( \; X. L9 V
  10. #define I2S_Standard_PCMLong            ((uint16_t)0x00B0)
    ; q+ Y% M0 U- D5 B8 Y, Z
  11. #define IS_I2S_STANDARD(STANDARD) (((STANDARD) == I2S_Standard_Phillips) || \
    ' W9 ^0 F1 f9 W) q3 i& U4 w8 h
  12.                                    ((STANDARD) == I2S_Standard_MSB) || \
    ; `2 H3 L6 N9 t/ i
  13.                                    ((STANDARD) == I2S_Standard_LSB) || \8 e4 o1 r( S" z3 \
  14.                                    ((STANDARD) == I2S_Standard_PCMShort) || \
    ' p1 ^( l: |& N
  15.                                    ((STANDARD) == I2S_Standard_PCMLong))
复制代码

8 ^5 ?9 {' T7 U# cSPI_I2S_Data_Format( `! N. v2 m; b6 K' X" T- m
  1. /** @defgroup SPI_I2S_Data_Format
    ! _* U6 Z& _2 O1 x- H, O& B; c
  2.   * @{
      r* I& {+ @3 a9 W
  3.   */0 L7 o& h8 h) G* N2 O+ N; h7 w
  4. * Q+ w& r1 V. N1 T0 z% `; _
  5. #define I2S_DataFormat_16b              ((uint16_t)0x0000)
    " [1 Z) Z0 {. V/ R# J8 z
  6. #define I2S_DataFormat_16bextended      ((uint16_t)0x0001)
    ; C( V' B% r: m$ _3 n& g4 K
  7. #define I2S_DataFormat_24b              ((uint16_t)0x0003)0 G: [1 `8 K5 [* T- u  d
  8. #define I2S_DataFormat_32b              ((uint16_t)0x0005)
    7 D( |! n' e( y$ |
  9. #define IS_I2S_DATA_FORMAT(FORMAT) (((FORMAT) == I2S_DataFormat_16b) || \
    # [4 l. j, H3 r3 g: g$ ^4 D4 D
  10.                                     ((FORMAT) == I2S_DataFormat_16bextended) || \
    " T* q! t. K! S) Y) s& `; u- m# s# x! a
  11.                                     ((FORMAT) == I2S_DataFormat_24b) || \& l3 `6 g$ K- `3 ]/ R: n
  12.                                     ((FORMAT) == I2S_DataFormat_32b))
复制代码

: ?& u& B; p. P, K  x) L, q" uSPI_I2S_MCLK_Output
  1. /** @defgroup SPI_I2S_MCLK_Output 0 I5 o0 L2 D$ b$ R1 i
  2.   * @{
    # z* I+ J3 [. ]% q2 H8 b2 v  D
  3.   */0 F$ b! [9 [  q+ g' f1 R

  4. * [3 Y$ C; L' C5 F* |3 T. F! u# ^
  5. #define I2S_MCLKOutput_Enable           ((uint16_t)0x0200): W- T# S. {  n9 b2 }0 Y: \
  6. #define I2S_MCLKOutput_Disable          ((uint16_t)0x0000)
    4 K) R5 |1 \& O; @" p% U- {9 H
  7. #define IS_I2S_MCLK_OUTPUT(OUTPUT) (((OUTPUT) == I2S_MCLKOutput_Enable) || \
    0 a: i$ w0 E$ L1 d/ E) g
  8.                                     ((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
  1. /** @defgroup SPI_I2S_Audio_Frequency
    . f( ~* @! O  @5 l4 _% H# I
  2.   * @{
    9 O2 v+ A8 X2 e9 @  H" K+ m
  3.   */5 N# Q! C) I, t/ V5 m8 C

  4. % \( V: J- k/ r$ s
  5. #define I2S_AudioFreq_192k               ((uint32_t)192000)
    6 F! J3 J% ]) R. W* G; U
  6. #define I2S_AudioFreq_96k                ((uint32_t)96000)  Y8 x3 O! C3 W3 Q7 \
  7. #define I2S_AudioFreq_48k                ((uint32_t)48000)
    : N: p% O, P$ e
  8. #define I2S_AudioFreq_44k                ((uint32_t)44100)
    9 G3 q0 g+ c+ ^! o2 R8 q4 R
  9. #define I2S_AudioFreq_32k                ((uint32_t)32000)
    5 [8 Q! o' t0 F4 Z: k, @
  10. #define I2S_AudioFreq_22k                ((uint32_t)22050)
    $ e9 o& x; ^+ o8 [. y
  11. #define I2S_AudioFreq_16k                ((uint32_t)16000)1 k* N) I" s( u0 k% t
  12. #define I2S_AudioFreq_11k                ((uint32_t)11025)
    1 H- i6 U6 c) G6 f3 F! K6 s+ ?9 t8 w
  13. #define I2S_AudioFreq_8k                 ((uint32_t)8000)
    ' H- {7 Y9 I3 E! B9 Y
  14. #define I2S_AudioFreq_Default            ((uint32_t)2)
    & S. k3 S- @' I% J

  15. , L5 G0 d, `6 {+ e3 `" m+ @0 C
  16. #define IS_I2S_AUDIO_FREQ(FREQ) ((((FREQ) >= I2S_AudioFreq_8k) && \
    ( r2 e6 d9 g  |$ D
  17.                                  ((FREQ) <= I2S_AudioFreq_192k)) || \
    0 k/ v# E( t6 P* z) E) s* U
  18.                                  ((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
  1. /** @defgroup SPI_I2S_Clock_Polarity # P0 Y3 t. b5 w  G$ G
  2.   * @{
    7 H4 T* v" F% l& A% p9 k( x+ B
  3.   */
    6 K8 `' u, `1 S1 p" _  J7 W3 z

  4. * G: l3 l% p: Y9 w; o6 \3 d
  5. #define I2S_CPOL_Low                    ((uint16_t)0x0000)
    $ E9 z# T/ |7 s8 W( M/ G, `% L
  6. #define I2S_CPOL_High                   ((uint16_t)0x0008)
    6 w7 \, A. |3 X8 M- O7 K) I; r
  7. #define IS_I2S_CPOL(CPOL) (((CPOL) == I2S_CPOL_Low) || \
    1 @& g: _; Z; P/ K; t5 o
  8.                            ((CPOL) == I2S_CPOL_High))
复制代码
+ N  U3 ?4 e5 d7 ]
SPI_I2S_DMA_transfer_requests
  1. /** @defgroup SPI_I2S_DMA_transfer_requests 2 \/ [+ O6 D2 d
  2.   * @{
    9 y- L# Q3 ?, ^- f) W
  3.   */* m7 A7 j2 @& |$ w# E

  4. + C5 P1 ~5 U- J
  5. #define SPI_I2S_DMAReq_Tx               ((uint16_t)0x0002)
    . c* Q& G& C, X* w6 [
  6. #define SPI_I2S_DMAReq_Rx               ((uint16_t)0x0001)
    " r$ l6 R  g% j* f( v5 L
  7. #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
  1. /** @defgroup SPI_NSS_internal_software_management
    ; p3 C$ d: q( y) j
  2.   * @{( _# j6 V( C3 x2 i7 O' H$ X
  3.   */7 x6 M5 c4 V5 s, Y- E' `  H0 }3 z
  4. 0 C) J+ N3 Z! P
  5. #define SPI_NSSInternalSoft_Set         ((uint16_t)0x0100)* u$ o4 s3 U" |9 T- I. z" o
  6. #define SPI_NSSInternalSoft_Reset       ((uint16_t)0xFEFF)
    2 A/ ^" H; E$ \: }6 D% \
  7. #define IS_SPI_NSS_INTERNAL(INTERNAL) (((INTERNAL) == SPI_NSSInternalSoft_Set) || \0 i4 W: E' K3 M9 q
  8.                                        ((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
  1. /** @defgroup SPI_CRC_Transmit_Receive
    $ \4 L( r0 l( b$ B
  2.   * @{
    6 r0 U1 D: ^0 n- D' |( R- f
  3.   */
    ) n( X3 u1 ]6 l4 ]) k

  4. : T# D: s  |- y! g
  5. #define SPI_CRC_Tx                      ((uint8_t)0x00)
      [/ @/ a$ x4 P2 P) _/ r0 W2 E+ K5 K
  6. #define SPI_CRC_Rx                      ((uint8_t)0x01)
    5 B" M" K0 @9 V
  7. #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. \
  1. /** @defgroup SPI_direction_transmit_receive 9 i- X0 w) M! |- h
  2.   * @{& d, Z6 O3 E' i. Y, @9 U& |# ]* l
  3.   */9 m9 b1 f6 B+ u( g. H" _

  4. * A% b$ O" a2 R- n
  5. #define SPI_Direction_Rx                ((uint16_t)0xBFFF)9 x, a. N/ ~* A
  6. #define SPI_Direction_Tx                ((uint16_t)0x4000)
    0 L2 @& g( I. V4 R! l% E
  7. #define IS_SPI_DIRECTION(DIRECTION) (((DIRECTION) == SPI_Direction_Rx) || \" e+ {3 _7 w- L# q: d
  8.                                      ((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
  1. /** @defgroup SPI_I2S_interrupts_definition
    0 M: b" S% i  z8 {6 D* k0 l6 U' }
  2.   * @{
    : w1 X/ f/ w) h1 N
  3.   */
    0 H8 c9 P& e) A% f
  4. : Z: j# c. @+ `) B
  5. #define SPI_I2S_IT_TXE                  ((uint8_t)0x71)
    : G% S8 s% d# m4 k' v1 w
  6. #define SPI_I2S_IT_RXNE                 ((uint8_t)0x60)
    " \' M( e3 f: Y% o& Y! U
  7. #define SPI_I2S_IT_ERR                  ((uint8_t)0x50)
      W/ X/ {& _; O
  8. #define I2S_IT_UDR                      ((uint8_t)0x53)
      A$ I9 R! q3 c& J# r5 H" n3 K, `
  9. #define SPI_I2S_IT_TIFRFE               ((uint8_t)0x58)# v0 X( S. g: I
  10. ) w/ R1 j/ t2 t+ J! g$ R3 k; l
  11. #define IS_SPI_I2S_CONFIG_IT(IT) (((IT) == SPI_I2S_IT_TXE) || \
    ! X3 N7 q: f  X; ~
  12.                                   ((IT) == SPI_I2S_IT_RXNE) || \% b* B& ?& |+ V$ w5 Z0 X
  13.                                   ((IT) == SPI_I2S_IT_ERR))
    3 u1 {5 g; l* C+ q2 G. T5 i
  14. : M9 W+ e0 e9 _# I5 L1 L0 e( B
  15. #define SPI_I2S_IT_OVR                  ((uint8_t)0x56)
    . r! B8 u3 Q* P' @
  16. #define SPI_IT_MODF                     ((uint8_t)0x55), r  K; S, @4 ~% _0 V# `" @
  17. #define SPI_IT_CRCERR                   ((uint8_t)0x54)
      |8 U0 \* K, S6 T

  18. ) o4 s6 l( D9 L; x  x: k
  19. #define IS_SPI_I2S_CLEAR_IT(IT) (((IT) == SPI_IT_CRCERR))
    + G, t9 N! Y" P; W7 H1 f5 [
  20. * v7 Y( d7 m. l. ?' d
  21. #define IS_SPI_I2S_GET_IT(IT) (((IT) == SPI_I2S_IT_RXNE)|| ((IT) == SPI_I2S_IT_TXE) || \0 L' k+ r2 J- x/ F
  22.                                ((IT) == SPI_IT_CRCERR)  || ((IT) == SPI_IT_MODF) || \  i, ?2 r' c' s
  23.                                ((IT) == SPI_I2S_IT_OVR) || ((IT) == I2S_IT_UDR) ||\- H0 g( S  @* {2 k7 A
  24.                                ((IT) == SPI_I2S_IT_TIFRFE))
复制代码

" A8 ^& M, V6 Y* P% N; dSPI_I2S_flags_definition$ r/ _, Q/ x# X% `1 d1 u/ J
  1. /** @defgroup SPI_I2S_flags_definition : Z6 |+ T2 F* J8 P3 |9 u
  2.   * @{
    4 F0 u  v* v6 W7 g: m4 V
  3.   */
    ; Y! b3 N" w& F

  4. . y! @- U. O. K) v( |' }
  5. #define SPI_I2S_FLAG_RXNE               ((uint16_t)0x0001)
    4 X. t5 c4 B" v4 W, k
  6. #define SPI_I2S_FLAG_TXE                ((uint16_t)0x0002)
    * s. f2 L) [/ R( o! ?$ B4 s4 K7 |
  7. #define I2S_FLAG_CHSIDE                 ((uint16_t)0x0004)
    , Z# z! x5 F3 i# i; R
  8. #define I2S_FLAG_UDR                    ((uint16_t)0x0008)) T9 g4 h5 D2 V1 Z  f
  9. #define SPI_FLAG_CRCERR                 ((uint16_t)0x0010)
    . g5 }  j' m: W0 l
  10. #define SPI_FLAG_MODF                   ((uint16_t)0x0020)
    $ L8 J  U+ d' z) ^. g; P8 l
  11. #define SPI_I2S_FLAG_OVR                ((uint16_t)0x0040)4 T* c) b, |1 t. \+ L* G4 z
  12. #define SPI_I2S_FLAG_BSY                ((uint16_t)0x0080), [5 O+ d. Z. \& A! y1 I
  13. #define SPI_I2S_FLAG_TIFRFE             ((uint16_t)0x0100)
    : w0 X% x4 P2 B3 ]# U

  14. 8 _* p5 B4 u$ z( y
  15. #define IS_SPI_I2S_CLEAR_FLAG(FLAG) (((FLAG) == SPI_FLAG_CRCERR))
    % _5 r3 ]- U/ p7 @/ K1 n
  16. #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
  17.                                    ((FLAG) == SPI_FLAG_MODF) || ((FLAG) == SPI_FLAG_CRCERR) || \
    % e' y7 G; s. i5 Z
  18.                                    ((FLAG) == I2S_FLAG_UDR) || ((FLAG) == I2S_FLAG_CHSIDE) || \5 g# n4 X. R! N9 `0 u1 |' o
  19.                                    ((FLAG) == SPI_I2S_FLAG_TXE) || ((FLAG) == SPI_I2S_FLAG_RXNE)|| \, U7 E! m- a' M  b1 T' }1 N9 o
  20.                                    ((FLAG) == SPI_I2S_FLAG_TIFRFE))
复制代码

3 |4 i! q* q; N- n( m03. 相关函数) B  J" Q2 K) K5 B7 u7 A' f
  1. /* Exported macro ------------------------------------------------------------*/
    8 Q! d4 i# i$ a* n1 Q" a- C
  2. /* Exported functions --------------------------------------------------------*/ 1 y& y3 _3 o. \: e( `0 U4 T

  3. % q) z! h1 k4 _
  4. /*  Function used to set the SPI configuration to the default reset state *****/
    ( l- K* |7 X- @, q& A$ O4 T% Y* v
  5. void SPI_I2S_DeInit(SPI_TypeDef* SPIx);) p5 E0 S3 X: R8 h- c

  6. ! e& J- |3 z! R7 e$ x7 Z8 s
  7. /* Initialization and Configuration functions *********************************/3 N, P) q' D7 N* }, N
  8. void SPI_Init(SPI_TypeDef* SPIx, SPI_InitTypeDef* SPI_InitStruct);# z, k! I5 x$ @% a+ Y( \- T
  9. void I2S_Init(SPI_TypeDef* SPIx, I2S_InitTypeDef* I2S_InitStruct);) Q1 O: H/ a% f" l
  10. void SPI_StructInit(SPI_InitTypeDef* SPI_InitStruct);
    . l1 ^7 e/ K1 E4 u
  11. void I2S_StructInit(I2S_InitTypeDef* I2S_InitStruct);
    3 Z2 [: _4 Y6 Q( ]1 u( f$ y
  12. void SPI_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState);# x% i' K2 q6 ?% b# ?3 X
  13. void I2S_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState);: w6 \$ |8 _- f$ F0 W
  14. void SPI_DataSizeConfig(SPI_TypeDef* SPIx, uint16_t SPI_DataSize);
    3 j! `4 [; n( T' C
  15. void SPI_BiDirectionalLineConfig(SPI_TypeDef* SPIx, uint16_t SPI_Direction);4 s! {1 s& J' ^$ P: Z: n3 D
  16. void SPI_NSSInternalSoftwareConfig(SPI_TypeDef* SPIx, uint16_t SPI_NSSInternalSoft);
    & L- O2 a' O7 d" C& U3 o8 S
  17. void SPI_SSOutputCmd(SPI_TypeDef* SPIx, FunctionalState NewState);
    9 Q0 N/ r4 c+ D  {5 f. S  ^2 L) a
  18. void SPI_TIModeCmd(SPI_TypeDef* SPIx, FunctionalState NewState);! W9 e% Y7 f8 E3 _# E8 K

  19. 6 y% n* j' j2 v9 \* J
  20. void I2S_FullDuplexConfig(SPI_TypeDef* I2Sxext, I2S_InitTypeDef* I2S_InitStruct);
    9 z# ~& H) ?1 ]) V7 u3 h+ e

  21. $ z% J/ H! f! ^4 F! ^5 ~" \
  22. /* Data transfers functions ***************************************************/
    : |2 e" i6 |+ @8 \
  23. void SPI_I2S_SendData(SPI_TypeDef* SPIx, uint16_t Data);2 L: ]  n4 x$ }8 a. R
  24. uint16_t SPI_I2S_ReceiveData(SPI_TypeDef* SPIx);7 f% W1 i( a$ U4 |+ m
  25. , D; A! U9 N0 v. p7 H
  26. /* Hardware CRC Calculation functions *****************************************/
    7 Y4 S9 ~% P4 h+ T/ B; {
  27. void SPI_CalculateCRC(SPI_TypeDef* SPIx, FunctionalState NewState);) c3 a( S0 K* v' Z+ Q6 b
  28. void SPI_TransmitCRC(SPI_TypeDef* SPIx);
    - ^! L: @& v6 D2 e' b
  29. uint16_t SPI_GetCRC(SPI_TypeDef* SPIx, uint8_t SPI_CRC);
    / `7 s& l8 X/ v! b
  30. uint16_t SPI_GetCRCPolynomial(SPI_TypeDef* SPIx);
    % ]) F8 y" E7 b) K+ x- j4 o7 k/ M" {

  31. 6 B! k4 a: p6 h
  32. /* DMA transfers management functions *****************************************/
    * g1 N  M4 p5 x: g" O( s  @
  33. void SPI_I2S_DMACmd(SPI_TypeDef* SPIx, uint16_t SPI_I2S_DMAReq, FunctionalState NewState);" c* \  p: H. k8 V9 R6 }

  34. : _8 O1 g* C5 O; a1 \. A$ S0 M+ Y
  35. /* Interrupts and flags management functions **********************************// S  n8 ^; [+ j6 u
  36. void SPI_I2S_ITConfig(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT, FunctionalState NewState);5 _; l; ^7 G: Q, \
  37. FlagStatus SPI_I2S_GetFlagStatus(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG);
    6 C) X7 w: u; T: \) H; F5 |( z
  38. void SPI_I2S_ClearFlag(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG);: x" O5 A" E( U) P
  39. ITStatus SPI_I2S_GetITStatus(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT);
    ; T) O+ L1 t% |4 h1 F" a
  40. 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
  1. /** 3 `% m0 r+ @+ m, c- D
  2.   * @brief Serial Peripheral Interface
    3 `6 M+ {- L0 v1 T+ J- \6 Y! k
  3.   */% _: k" Z/ b+ _) r  ^

  4. - W( ~# Y1 t+ Z- H7 P$ x9 K  F
  5. typedef struct
    7 h2 q$ c8 Z! v' f3 _1 U
  6. {
    * F# _$ O0 c+ l6 t1 k; ]
  7.   __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
  8.   uint16_t      RESERVED0;  /*!< Reserved, 0x02                                                           */9 `% [! E" `; z
  9.   __IO uint16_t CR2;        /*!< SPI control register 2,                             Address offset: 0x04 */  f5 _2 @" r/ `) h  b8 R
  10.   uint16_t      RESERVED1;  /*!< Reserved, 0x06                                                           */1 ?+ m6 S) q- v, m
  11.   __IO uint16_t SR;         /*!< SPI status register,                                Address offset: 0x08 */2 \( ]7 D/ _& m$ ^& A& ?
  12.   uint16_t      RESERVED2;  /*!< Reserved, 0x0A                                                           */
    , n$ p* a2 |1 h4 A
  13.   __IO uint16_t DR;         /*!< SPI data register,                                  Address offset: 0x0C */
    - g( @; j* g6 @' N5 {9 Y3 z
  14.   uint16_t      RESERVED3;  /*!< Reserved, 0x0E                                                           */4 Z$ T& C* n: @- r6 N1 F
  15.   __IO uint16_t CRCPR;      /*!< SPI CRC polynomial register (not used in I2S mode), Address offset: 0x10 */& y2 z4 \+ {# z8 f& \) m
  16.   uint16_t      RESERVED4;  /*!< Reserved, 0x12                                                           */2 O/ `' g- x8 O2 t9 ?- W) P
  17.   __IO uint16_t RXCRCR;     /*!< SPI RX CRC register (not used in I2S mode),         Address offset: 0x14 */
    ' K7 A8 l0 Y2 f0 v' n0 `
  18.   uint16_t      RESERVED5;  /*!< Reserved, 0x16                                                           */
    & I% ?' }4 A- B( v
  19.   __IO uint16_t TXCRCR;     /*!< SPI TX CRC register (not used in I2S mode),         Address offset: 0x18 */- ^7 G+ n( g8 e! J
  20.   uint16_t      RESERVED6;  /*!< Reserved, 0x1A                                                           */1 @; Z, A# y) q! p1 a/ _  S
  21.   __IO uint16_t I2SCFGR;    /*!< SPI_I2S configuration register,                     Address offset: 0x1C */* D( M" Z) ^& H
  22.   uint16_t      RESERVED7;  /*!< Reserved, 0x1E                                                           */
    # h; O. g2 l, O
  23.   __IO uint16_t I2SPR;      /*!< SPI_I2S prescaler register,                         Address offset: 0x20 */
    & @. q! q7 \9 c# D. D: E0 @1 f$ y
  24.   uint16_t      RESERVED8;  /*!< Reserved, 0x22                                                           */. v; T" b7 ^5 _' z
  25. } SPI_TypeDef;
复制代码

  f; M8 v( Y8 b
- L, \/ N9 p: ?" L  {; x# ?$ h3 F
* `* d/ s( t4 G7 e& Y4 N" V+ c
赞 收藏 评论0 发布时间:2022-3-27 17:00

举报

0个回答

所属标签

相似技术帖

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版