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

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

[复制链接]
STMCU小助手 发布时间:2022-3-27 17:00
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
  1. /** ; l! p. Q5 C& l5 R* {$ Z
  2.   * @brief  SPI Init structure definition  
    8 Y( O3 u8 n% v7 Q* |
  3.   */
    - H3 k7 u! u; B3 G0 H' x* l! \7 m

  4. 4 y0 g) w2 }4 B
  5. typedef struct
    , I: J1 W) }6 s: I
  6. {
    ) i0 G" S9 y8 {' w! X; h
  7.   uint16_t SPI_Direction;           /*!< Specifies the SPI unidirectional or bidirectional data mode.
    + F% ]  E/ X" J
  8.                                          This parameter can be a value of @ref SPI_data_direction */9 e( R* a, _7 T  H" r( p6 q
  9. ; i& L5 U' R% d9 F
  10.   uint16_t SPI_Mode;                /*!< Specifies the SPI operating mode." ^; r9 ^+ P# j1 J1 H" y+ G# t7 D
  11.                                          This parameter can be a value of @ref SPI_mode */) o6 F1 }# i/ B+ x$ H2 J7 `
  12. 1 P" B* e" g% S( V9 E
  13.   uint16_t SPI_DataSize;            /*!< Specifies the SPI data size.0 `( h$ h# v! L5 z; \" n
  14.                                          This parameter can be a value of @ref SPI_data_size */6 a5 }. W, G) f

  15. , L0 z, y( W8 v$ g$ g/ D! E
  16.   uint16_t SPI_CPOL;                /*!< Specifies the serial clock steady state.
    0 r$ }0 n; Z: T( _
  17.                                          This parameter can be a value of @ref SPI_Clock_Polarity */
    ( m' O, p' j; ^- ~% u' m
  18. ) ~! R# w: D1 D' j6 `# Q* L
  19.   uint16_t SPI_CPHA;                /*!< Specifies the clock active edge for the bit capture.
    ' A4 k8 q) X* o, X7 `
  20.                                          This parameter can be a value of @ref SPI_Clock_Phase */
    3 R  L+ @7 @# n4 l* z$ f
  21. % w8 I& N( O0 j* n! ^( B- L$ ?
  22.   uint16_t SPI_NSS;                 /*!< Specifies whether the NSS signal is managed by
    8 R: o5 t) u4 L7 _" ?) I4 `
  23.                                          hardware (NSS pin) or by software using the SSI bit.: j: t7 m$ ]$ C8 V0 l
  24.                                          This parameter can be a value of @ref SPI_Slave_Select_management */
    % D8 E% x3 T! Y9 q  J: A  @' x
  25. 4 I/ J7 J6 t: D4 j
  26.   uint16_t SPI_BaudRatePrescaler;   /*!< Specifies the Baud Rate prescaler value which will be5 I9 o6 W* V! X4 T% g( A
  27.                                          used to configure the transmit and receive SCK clock.
      P9 _# D1 P' j* c7 Y
  28.                                          This parameter can be a value of @ref SPI_BaudRate_Prescaler: u6 g* A4 x( Q0 W7 [  k4 P
  29.                                          @note The communication clock is derived from the master
    9 l3 \% ~/ \8 R6 h. g! F
  30.                                                clock. The slave clock does not need to be set. */
    $ G2 j  P* e3 v- r  p! L8 z
  31. ) C9 q/ ~; T6 D9 E9 f, ?
  32.   uint16_t SPI_FirstBit;            /*!< Specifies whether data transfers start from MSB or LSB bit.
    ; r0 Z' Z" ^+ F, T1 ]0 q
  33.                                          This parameter can be a value of @ref SPI_MSB_LSB_transmission */: e" ]: f0 F8 y7 y; h1 z

  34.   p1 w8 d- ^# \/ Q% m& {  b/ d8 w
  35.   uint16_t SPI_CRCPolynomial;       /*!< Specifies the polynomial used for the CRC calculation. */
    : }7 A2 r& K3 ^( K3 M$ _
  36. }SPI_InitTypeDef;
复制代码
" M- B, M5 V, D* K
I2S Init structure definition2 Z9 o  _4 }4 R! y' t) h% z, [' D
  1. /**
    ) M, G& h7 k* v$ P: f$ N; [. e
  2.   * @brief  I2S Init structure definition  
    . Y* `8 J6 K) }# w1 Y0 P4 F: [) E0 d
  3.   */
    ; n: N1 q+ b) f! F
  4. & ~1 b5 A# v- F8 |+ Y1 W
  5. typedef struct
    , d0 w/ X; f" q# @7 S. u6 G4 U5 P
  6. {& P, P" q. |  J# f; i

  7. ' ]8 R* b4 e1 J$ u
  8.   uint16_t I2S_Mode;         /*!< Specifies the I2S operating mode.) k# E. l8 E# N
  9.                                   This parameter can be a value of @ref I2S_Mode */
    & F$ a+ N8 e. e
  10. " H$ E0 T# c7 d' o" E. L7 Q/ d
  11.   uint16_t I2S_Standard;     /*!< Specifies the standard used for the I2S communication.& d4 ]/ ^. e) _6 O" _
  12.                                   This parameter can be a value of @ref I2S_Standard */
    4 S8 p. r/ ]" S

  13. 6 [0 i' L/ o8 P
  14.   uint16_t I2S_DataFormat;   /*!< Specifies the data format for the I2S communication.% }7 q% z3 o5 p9 f, J  A& Z
  15.                                   This parameter can be a value of @ref I2S_Data_Format */# r, ^& E3 {6 Y; z3 N5 t
  16. + X( z, L0 r( w
  17.   uint16_t I2S_MCLKOutput;   /*!< Specifies whether the I2S MCLK output is enabled or not.. a7 T$ j# m* w" q0 O  d, ?% U
  18.                                   This parameter can be a value of @ref I2S_MCLK_Output */' K  _% W8 n/ o/ @+ K
  19. , D$ A  j8 N5 I7 a, g- i( t* d
  20.   uint32_t I2S_AudioFreq;    /*!< Specifies the frequency selected for the I2S communication.
    % g. f. f$ C, Y$ u
  21.                                   This parameter can be a value of @ref I2S_Audio_Frequency */
    # l6 ^6 i/ P' y4 {: `  e3 u* H

  22. , i+ q% L9 I. h" B0 j! K
  23.   uint16_t I2S_CPOL;         /*!< Specifies the idle state of the I2S clock.
    3 B' N& {5 r. k: K- @9 e
  24.                                   This parameter can be a value of @ref I2S_Clock_Polarity */+ S1 z7 Z7 i# t% w! |) v5 f
  25. }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
  1. /** @defgroup SPI_Exported_Constants
    2 b7 B; p/ i, y
  2.   * @{
    ' g' p* T  e  D5 e9 F7 @& M9 \
  3.   */- j8 F  w* J1 E, W% W
  4. 4 V% b" m" ], |8 I  L$ ?
  5. #define IS_SPI_ALL_PERIPH(PERIPH) (((PERIPH) == SPI1) || \# S2 D& R8 ]/ V5 L0 G
  6.                                    ((PERIPH) == SPI2) || \
    9 w; z# V% v9 u( S
  7.                                    ((PERIPH) == SPI3) || \
    * d0 t3 C; P2 K% h5 Y  _
  8.                                    ((PERIPH) == SPI4) || \
    . [* _. J, N/ u; {, J2 U1 B" k
  9.                                    ((PERIPH) == SPI5) || \
    1 h! b" `6 W7 _0 B8 n- Q9 D
  10.                                    ((PERIPH) == SPI6))
    ; O( @1 p* D- U4 b
  11. ) S; o0 F2 @! U3 U
  12. #define IS_SPI_ALL_PERIPH_EXT(PERIPH) (((PERIPH) == SPI1)    || \
    $ Z5 G: m  [7 ?/ e& \6 ]3 }3 z
  13.                                        ((PERIPH) == SPI2)    || \
    2 O. [4 h2 \+ _0 T; E
  14.                                        ((PERIPH) == SPI3)    || \6 O2 X- y2 J: C7 C3 I
  15.                                        ((PERIPH) == SPI4)    || \
    . p) n" G$ P0 ]; B$ B+ K5 E
  16.                                        ((PERIPH) == SPI5)    || \
    5 Y- S& C% G1 G5 u) q4 n) ^: c- O2 W
  17.                                        ((PERIPH) == SPI6)    || \2 Y/ f6 R6 J* n+ A/ v6 F  z6 r4 b+ `
  18.                                        ((PERIPH) == I2S2ext) || \
      N% k/ b/ Q$ c0 C. G4 ]1 Q
  19.                                        ((PERIPH) == I2S3ext))9 G7 I' a3 W& d8 A: l

  20. : f6 B$ ?- k: X/ s" Z) {
  21. #define IS_SPI_23_PERIPH(PERIPH)  (((PERIPH) == SPI2) || \7 f  D) }; K  ]% S) r
  22.                                    ((PERIPH) == SPI3))/ U2 Q/ V2 z( I% k7 L

  23. 1 o- w0 P& \, ]1 X2 p' h( }
  24. #define IS_SPI_23_PERIPH_EXT(PERIPH)  (((PERIPH) == SPI2)    || \
      R; a) U/ N- `5 Q  c& M
  25.                                        ((PERIPH) == SPI3)    || \! x! Q" g0 t4 E; L3 {
  26.                                        ((PERIPH) == I2S2ext) || \
    # @9 |7 i: ]( _# Y. O
  27.                                        ((PERIPH) == I2S3ext))4 a( Q' H: E% C% H) Q
  28. & m/ [4 M% o% I3 V; G; k7 B
  29. #define IS_I2S_EXT_PERIPH(PERIPH)  (((PERIPH) == I2S2ext) || \" S! t1 O& k' w3 D
  30.                                     ((PERIPH) == I2S3ext))
复制代码
3 o! y2 C1 S' v# a# s
SPI_data_direction
6 Q+ ^( M+ J  \: G( O
  1. /** @defgroup SPI_data_direction 8 u2 H. c0 B7 e
  2.   * @{
    ! p1 H6 r0 [1 h7 F' U# i# P" y
  3.   */6 ]2 ]: B/ B$ W
  4. 2 B, ~1 ?  z  n, z, h
  5. #define SPI_Direction_2Lines_FullDuplex ((uint16_t)0x0000), B* Z+ G. X7 w9 J( |
  6. #define SPI_Direction_2Lines_RxOnly     ((uint16_t)0x0400)- K  r6 O, W5 b$ b* F: j' Z
  7. #define SPI_Direction_1Line_Rx          ((uint16_t)0x8000). T8 H. f  E1 t; h2 [6 q
  8. #define SPI_Direction_1Line_Tx          ((uint16_t)0xC000)
    " t! b$ K2 q) {7 a
  9. #define IS_SPI_DIRECTION_MODE(MODE) (((MODE) == SPI_Direction_2Lines_FullDuplex) || \
    8 b! ]. @; {6 `" U
  10.                                      ((MODE) == SPI_Direction_2Lines_RxOnly) || \
    + m* V. H8 M7 k5 T% F
  11.                                      ((MODE) == SPI_Direction_1Line_Rx) || \  J( i3 R+ F2 D2 D! O9 s
  12.                                      ((MODE) == SPI_Direction_1Line_Tx))
复制代码
- @6 X5 M: U- B- l$ m7 l8 C
SPI_mode
# v. T0 i8 {. J* \) ^4 m
  1. /** @defgroup SPI_mode ) w" e2 s3 o3 k7 }) j4 p
  2.   * @{/ a4 q( v3 i5 t# L& \- ~
  3.   */
    8 C1 h# M) g1 ^: b/ z2 f
  4. : u+ P* c) k/ }: d4 F) U( r& d+ g
  5. #define SPI_Mode_Master                 ((uint16_t)0x0104)/ W: V7 r0 D: I; l9 u
  6. #define SPI_Mode_Slave                  ((uint16_t)0x0000)3 j) }6 U* ^% _1 A' r
  7. #define IS_SPI_MODE(MODE) (((MODE) == SPI_Mode_Master) || \
    : P" @/ b3 k# ]! g
  8.                            ((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
  1. /** @defgroup SPI_data_size 2 F# i" ?# L' x- v
  2.   * @{
    # f% L( q+ h3 W
  3.   */
    ; G  t0 n3 `7 J! m2 i/ v
  4. * m" h" {3 Q6 Q
  5. #define SPI_DataSize_16b                ((uint16_t)0x0800)
    ' _3 F' {/ n; H0 @& H/ R. g
  6. #define SPI_DataSize_8b                 ((uint16_t)0x0000)
    ! R) \. t+ v2 ^* V$ |  b
  7. #define IS_SPI_DATASIZE(DATASIZE) (((DATASIZE) == SPI_DataSize_16b) || \3 w6 f, x; m) S  c' `  f
  8.                                    ((DATASIZE) == SPI_DataSize_8b))
复制代码

, E+ Z& @. N2 E7 J! oSPI_Clock_Polarity0 q3 i& W: S( B$ Y$ J! t$ b; [
  1. /** @defgroup SPI_Clock_Polarity
    0 ?# K! {% K% c0 p- ~* _* i9 J- t1 N
  2.   * @{
    & M. X$ ^9 Y8 o) ]* M
  3.   */
    # I# \9 j( t' l' y

  4. : e) E' L1 P/ \5 I2 K2 g
  5. #define SPI_CPOL_Low                    ((uint16_t)0x0000)' z' ~" b: J# @2 p, K( T
  6. #define SPI_CPOL_High                   ((uint16_t)0x0002)' T" q  G& t* {0 B0 z
  7. #define IS_SPI_CPOL(CPOL) (((CPOL) == SPI_CPOL_Low) || \0 D) X1 p- |6 o
  8.                            ((CPOL) == SPI_CPOL_High))
复制代码
3 l1 C5 v1 H6 V1 z
SPI_Clock_Phase. X# l9 t+ ~/ ~6 N% E
  1. /** @defgroup SPI_Clock_Phase ; S. y5 G* Z. y2 y, D7 u6 p
  2.   * @{
    3 b& O+ X# T5 \0 r4 e
  3.   */7 X% K. t0 V( b) e3 e# d' U

  4. 5 l0 |' o$ W0 A) F0 w7 v! M
  5. #define SPI_CPHA_1Edge                  ((uint16_t)0x0000)
    2 k; I- L  y! X: G
  6. #define SPI_CPHA_2Edge                  ((uint16_t)0x0001)
    4 H% l5 L- R! w5 t
  7. #define IS_SPI_CPHA(CPHA) (((CPHA) == SPI_CPHA_1Edge) || \
    * ^: o* u! A9 V9 X
  8.                            ((CPHA) == SPI_CPHA_2Edge))
复制代码

, K% o" ~% Q, Z4 d2 I+ I# c* H5 vSPI_Slave_Select_management
  1. /** @defgroup SPI_Slave_Select_management 9 |/ U4 E2 ]8 Z, a
  2.   * @{( W4 M4 K! p9 t" F9 \
  3.   */
    $ e# n. I! X- m  |

  4. # Z4 x3 C1 r$ q4 R3 S3 n* r
  5. #define SPI_NSS_Soft                    ((uint16_t)0x0200)
    ( k; }1 E$ ^2 j( y$ Y
  6. #define SPI_NSS_Hard                    ((uint16_t)0x0000)
    4 F6 Z3 H) j! ^! v* L* X
  7. #define IS_SPI_NSS(NSS) (((NSS) == SPI_NSS_Soft) || \
    ) S% c8 V4 |8 \% ^. U6 [
  8.                          ((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
  1. /** @defgroup SPI_BaudRate_Prescaler
    ; J7 c0 n. P3 y+ A' f
  2.   * @{: E: f! x  }, z% X6 ^' a
  3.   */! Q5 |! S9 x  j8 O
  4. & s' t& ^5 Q- _! _
  5. #define SPI_BaudRatePrescaler_2         ((uint16_t)0x0000)' U' g5 {7 r; _
  6. #define SPI_BaudRatePrescaler_4         ((uint16_t)0x0008)
    # |: p9 i- F; P6 M, ]$ o
  7. #define SPI_BaudRatePrescaler_8         ((uint16_t)0x0010)
    $ a2 x) s& W+ G' C6 o& S! d' A$ b6 K
  8. #define SPI_BaudRatePrescaler_16        ((uint16_t)0x0018)& a$ F2 O0 h' a
  9. #define SPI_BaudRatePrescaler_32        ((uint16_t)0x0020)
    ) c5 ^' K* n3 X' T
  10. #define SPI_BaudRatePrescaler_64        ((uint16_t)0x0028)+ O/ z; [3 E# Y9 x+ j! N1 l
  11. #define SPI_BaudRatePrescaler_128       ((uint16_t)0x0030)5 K+ l' }& a" `: S9 B
  12. #define SPI_BaudRatePrescaler_256       ((uint16_t)0x0038)
    & N1 c& ?- o2 w* P0 [+ _
  13. #define IS_SPI_BAUDRATE_PRESCALER(PRESCALER) (((PRESCALER) == SPI_BaudRatePrescaler_2) || \
    5 B$ @& l; W( f( Q$ c1 q
  14.                                               ((PRESCALER) == SPI_BaudRatePrescaler_4) || \: B! T% u1 ^1 [
  15.                                               ((PRESCALER) == SPI_BaudRatePrescaler_8) || \3 A: `: k  _6 U7 n9 m' G
  16.                                               ((PRESCALER) == SPI_BaudRatePrescaler_16) || \$ B* H" V  \* k+ S* V8 d
  17.                                               ((PRESCALER) == SPI_BaudRatePrescaler_32) || \
    3 v1 u1 O% u9 n: y
  18.                                               ((PRESCALER) == SPI_BaudRatePrescaler_64) || \. b' N/ j, D, a1 N- b: I
  19.                                               ((PRESCALER) == SPI_BaudRatePrescaler_128) || \
    1 q* v% E7 A. c
  20.                                               ((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
  1. /** @defgroup SPI_MSB_LSB_transmission ) p: _& d1 G" \3 |, R( X
  2.   * @{, Q1 O2 \& J4 h8 j' U
  3.   */6 m( z7 x; u$ U" h
  4. 3 S# p. ~, x( P; i; R) U! E
  5. #define SPI_FirstBit_MSB                ((uint16_t)0x0000)
    ' q9 u/ C2 G; z1 G
  6. #define SPI_FirstBit_LSB                ((uint16_t)0x0080)% x4 ?* d: K! J4 T+ b
  7. #define IS_SPI_FIRST_BIT(BIT) (((BIT) == SPI_FirstBit_MSB) || \
    $ U, b0 w" W+ y; u+ J; W+ p8 c4 U
  8.                                ((BIT) == SPI_FirstBit_LSB))
复制代码
  S& `# p4 L7 n' r
SPI_I2S_Mode$ P4 T) T3 S8 U
  1. /** @defgroup SPI_I2S_Mode
    8 m9 ~  M6 r9 G! x' j3 F
  2.   * @{, @5 B0 f% {; G& \1 a
  3.   */! k# y" ^& `7 ~# M4 y% ~1 G2 g  S( R# Q* _; u

  4. 9 j" A4 }1 }+ Y& D; c+ d8 i, u; ^
  5. #define I2S_Mode_SlaveTx                ((uint16_t)0x0000)
    / o$ E0 w+ J7 g- {, Q2 w
  6. #define I2S_Mode_SlaveRx                ((uint16_t)0x0100)
    4 H1 Y5 g4 b( G2 A- J* B* f
  7. #define I2S_Mode_MasterTx               ((uint16_t)0x0200)* ^- b  g) i- c# D
  8. #define I2S_Mode_MasterRx               ((uint16_t)0x0300)
    7 a: L# \; k# r+ q: Q
  9. #define IS_I2S_MODE(MODE) (((MODE) == I2S_Mode_SlaveTx) || \
    - l6 H' b- f6 H' {# H1 W/ \3 R) d
  10.                            ((MODE) == I2S_Mode_SlaveRx) || \$ Z# \- n1 I7 n$ J( T- c9 m* p; ^
  11.                            ((MODE) == I2S_Mode_MasterTx)|| \
      c$ h/ N, m; n
  12.                            ((MODE) == I2S_Mode_MasterRx))
复制代码

5 R  o0 e) }& nSPI_I2S_Standard
) ]2 F0 C7 G+ H) o6 U1 t6 x
  1. ' L0 ]; D3 [+ v% f6 l) u: m' s
  2. /** @defgroup SPI_I2S_Standard 1 z# w3 J7 R) @; F" ~# z  b
  3.   * @{5 |6 c: q: l" v3 _& p% Y9 k
  4.   */
    0 h3 w# _0 N5 F! G, z9 r
  5. 0 Q" e* Z9 ]; b+ ?" ^, }; M
  6. #define I2S_Standard_Phillips           ((uint16_t)0x0000), r% [$ G6 G5 w0 G$ _4 T
  7. #define I2S_Standard_MSB                ((uint16_t)0x0010)7 A1 k; f, U! y* G4 Q2 A  Y: I& j
  8. #define I2S_Standard_LSB                ((uint16_t)0x0020)4 J; @* ~6 y9 ?( V6 |" Y+ k3 Q1 e8 C6 m
  9. #define I2S_Standard_PCMShort           ((uint16_t)0x0030). q! o' c8 D5 }7 m
  10. #define I2S_Standard_PCMLong            ((uint16_t)0x00B0)
    ( |" H4 w& |# V5 I# N
  11. #define IS_I2S_STANDARD(STANDARD) (((STANDARD) == I2S_Standard_Phillips) || \2 M4 k7 [8 f6 q) n! u) @
  12.                                    ((STANDARD) == I2S_Standard_MSB) || \3 Z2 C0 R4 J) ]
  13.                                    ((STANDARD) == I2S_Standard_LSB) || \
    ) P& D" k7 }0 F( V- _& k" R! i; O
  14.                                    ((STANDARD) == I2S_Standard_PCMShort) || \
    # }. v! S5 j: S* c. ]$ O2 p/ f
  15.                                    ((STANDARD) == I2S_Standard_PCMLong))
复制代码

: V" x  w% t/ J/ Y8 s0 d2 e/ OSPI_I2S_Data_Format
$ Q2 X$ L. A$ Y' p* [: L
  1. /** @defgroup SPI_I2S_Data_Format % f6 j+ {2 o& ~/ d; x
  2.   * @{
    * d. E7 p+ W5 l, C5 L
  3.   */5 Y! v1 n6 l1 e" F
  4. 8 t: X, y# W: A, v3 S
  5. #define I2S_DataFormat_16b              ((uint16_t)0x0000). d0 E% w  Q8 B" S( a+ J
  6. #define I2S_DataFormat_16bextended      ((uint16_t)0x0001)+ q/ O; h% {. s4 r: q  Q$ e
  7. #define I2S_DataFormat_24b              ((uint16_t)0x0003)* w" f% H& ]4 b# p
  8. #define I2S_DataFormat_32b              ((uint16_t)0x0005)
    8 W  `0 K" M9 k7 J. x" f
  9. #define IS_I2S_DATA_FORMAT(FORMAT) (((FORMAT) == I2S_DataFormat_16b) || \
    % X: E" Y1 Y2 ^
  10.                                     ((FORMAT) == I2S_DataFormat_16bextended) || \4 ~8 K3 ]" x+ n1 x
  11.                                     ((FORMAT) == I2S_DataFormat_24b) || \
    + _& a/ r3 U- f9 _0 t0 n& w
  12.                                     ((FORMAT) == I2S_DataFormat_32b))
复制代码

% E# Y/ t0 @! x) PSPI_I2S_MCLK_Output
  1. /** @defgroup SPI_I2S_MCLK_Output
    % c7 h5 }1 s; W/ c- i5 \2 N& L
  2.   * @{
    & t! d; l6 |. Z6 _% z
  3.   */$ L5 e8 U4 a( p% f; ^2 Q9 {
  4. 9 b4 p) Y: R5 j" u% [
  5. #define I2S_MCLKOutput_Enable           ((uint16_t)0x0200)! ~/ s7 i4 I! W2 v
  6. #define I2S_MCLKOutput_Disable          ((uint16_t)0x0000)
    1 d, {0 o5 H0 a* q
  7. #define IS_I2S_MCLK_OUTPUT(OUTPUT) (((OUTPUT) == I2S_MCLKOutput_Enable) || \
    ) u4 Y8 W9 A/ N( Q
  8.                                     ((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
  1. /** @defgroup SPI_I2S_Audio_Frequency
    2 p/ Q. C  [4 j; U- q# n$ @/ c6 i
  2.   * @{
    " ?3 g9 ~# I7 ]1 S$ x( c6 i9 j
  3.   */
    ; q3 F0 J" X0 U. f, a5 k! ?+ ?% X
  4. & a; S) s& v$ v. R0 e0 _. `2 @
  5. #define I2S_AudioFreq_192k               ((uint32_t)192000)
    6 x3 d0 G+ O4 e# G7 @" n$ D
  6. #define I2S_AudioFreq_96k                ((uint32_t)96000)
    2 [9 }6 i) f# N( A
  7. #define I2S_AudioFreq_48k                ((uint32_t)48000)5 z' L4 X# T8 F6 S; D, c& t8 ?
  8. #define I2S_AudioFreq_44k                ((uint32_t)44100)
    : a* v( V0 n5 k. |1 `5 t7 d9 G
  9. #define I2S_AudioFreq_32k                ((uint32_t)32000)
    + z! g% q7 [4 J+ ^
  10. #define I2S_AudioFreq_22k                ((uint32_t)22050)6 i- z  }2 y4 g
  11. #define I2S_AudioFreq_16k                ((uint32_t)16000)
    0 U' ^  d' q3 q4 X
  12. #define I2S_AudioFreq_11k                ((uint32_t)11025)
    / k$ U& j6 T% Q) l# Y6 ?
  13. #define I2S_AudioFreq_8k                 ((uint32_t)8000)
    , D6 L4 t, Y4 \0 O" |9 C
  14. #define I2S_AudioFreq_Default            ((uint32_t)2)  t, x9 _. h, _( C0 M5 b7 Q
  15. / i7 s- E" V: u3 c1 ^) h* d
  16. #define IS_I2S_AUDIO_FREQ(FREQ) ((((FREQ) >= I2S_AudioFreq_8k) && \
    8 {& r. B' b7 c
  17.                                  ((FREQ) <= I2S_AudioFreq_192k)) || \& C' R& Y  J, n% a
  18.                                  ((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
  1. /** @defgroup SPI_I2S_Clock_Polarity
    / [+ s( {0 |2 }! w3 q5 z+ ]
  2.   * @{3 q  J% m* T( X5 J; w3 @) m) o
  3.   */
    ( T4 f' X: W* B* e
  4. % l1 T: H' w9 h5 H* W& G6 R
  5. #define I2S_CPOL_Low                    ((uint16_t)0x0000)# A& D; I& r% {$ h: z. W
  6. #define I2S_CPOL_High                   ((uint16_t)0x0008)
    & }# }- H" o7 k3 f* v
  7. #define IS_I2S_CPOL(CPOL) (((CPOL) == I2S_CPOL_Low) || \
    * A( F6 c# {9 Q& \) `
  8.                            ((CPOL) == I2S_CPOL_High))
复制代码

( k4 U- f: S+ m4 ?0 |9 e% t) FSPI_I2S_DMA_transfer_requests
  1. /** @defgroup SPI_I2S_DMA_transfer_requests ( j2 R9 E; t9 w! ^; ?- W
  2.   * @{
    , J' V1 ^4 F- }
  3.   */3 H  C# e" b; P4 t

  4. ) e8 V4 I6 E3 q* I7 a; j7 ~3 Y
  5. #define SPI_I2S_DMAReq_Tx               ((uint16_t)0x0002)
    1 q* C! p- R) f4 k$ g6 p/ K
  6. #define SPI_I2S_DMAReq_Rx               ((uint16_t)0x0001)" s; d) ?3 U8 I. f7 r$ J7 p
  7. #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
  1. /** @defgroup SPI_NSS_internal_software_management
    4 w' q/ _0 t1 R+ o, `; D( U0 F
  2.   * @{9 N- n' b9 @: a# O
  3.   */
    3 ?6 H2 h+ z  z4 f
  4. 6 x9 r4 R0 u4 W  x
  5. #define SPI_NSSInternalSoft_Set         ((uint16_t)0x0100)- u3 X- f& ?3 w7 w7 @9 h
  6. #define SPI_NSSInternalSoft_Reset       ((uint16_t)0xFEFF)
    + `: J; z; p' l, k& r
  7. #define IS_SPI_NSS_INTERNAL(INTERNAL) (((INTERNAL) == SPI_NSSInternalSoft_Set) || \: r8 n5 x+ K, K5 L* B( u
  8.                                        ((INTERNAL) == SPI_NSSInternalSoft_Reset))
复制代码

  H) }6 I) I  [+ HSPI_CRC_Transmit_Receive
+ a3 ?+ N  U( d& z+ m5 A  _( N. u
  1. /** @defgroup SPI_CRC_Transmit_Receive
    & y& }/ p( Y  S- B' ^, o3 r  s, H
  2.   * @{( R: Z7 K3 I. [; y& t$ X
  3.   */
    & m% a2 `7 I. {* N+ I
  4. 8 [8 r: J7 a# H3 D* C: G' ], }
  5. #define SPI_CRC_Tx                      ((uint8_t)0x00)
    / C* @: h/ E' _! L
  6. #define SPI_CRC_Rx                      ((uint8_t)0x01)4 l8 Z, h- i; y7 y
  7. #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 {
  1. /** @defgroup SPI_direction_transmit_receive $ Y7 d3 p% f7 L. W" _% ~! Z
  2.   * @{! F/ S3 J, S+ M
  3.   */2 V1 W0 k* w6 s

  4. 5 d4 \: l: T) g, \0 N
  5. #define SPI_Direction_Rx                ((uint16_t)0xBFFF)
    3 \1 H3 v9 R; a) t7 M7 \/ J
  6. #define SPI_Direction_Tx                ((uint16_t)0x4000)/ U8 v6 z1 T: e8 p" k0 |4 x
  7. #define IS_SPI_DIRECTION(DIRECTION) (((DIRECTION) == SPI_Direction_Rx) || \  a1 ^) ^; l) @# c6 \
  8.                                      ((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
  1. /** @defgroup SPI_I2S_interrupts_definition 5 F% V( a  b8 O) }$ n
  2.   * @{7 ?# v/ T4 v2 G! r- E& P4 b
  3.   */
    $ O. |2 m& D  Y" g9 t

  4. 4 \# O7 U8 p$ Z. V
  5. #define SPI_I2S_IT_TXE                  ((uint8_t)0x71)& {0 j( W$ e% w! l3 T
  6. #define SPI_I2S_IT_RXNE                 ((uint8_t)0x60)
    $ y1 X% w" m. d( u
  7. #define SPI_I2S_IT_ERR                  ((uint8_t)0x50)# l2 w- j3 K& }) I
  8. #define I2S_IT_UDR                      ((uint8_t)0x53)
    ! @& \! d2 X7 v3 x+ J# y
  9. #define SPI_I2S_IT_TIFRFE               ((uint8_t)0x58)* q/ I& s! D8 ^& O

  10. ) ?+ p9 p( Q+ s+ m+ D$ ], F% i
  11. #define IS_SPI_I2S_CONFIG_IT(IT) (((IT) == SPI_I2S_IT_TXE) || \, @* K9 X$ N' [% P" A: }: z7 `% M
  12.                                   ((IT) == SPI_I2S_IT_RXNE) || \# L8 \% v0 w) F; `
  13.                                   ((IT) == SPI_I2S_IT_ERR))& V' r. \) O2 _3 A$ o& x9 a4 l* b

  14. " B6 y& \" u) V/ V
  15. #define SPI_I2S_IT_OVR                  ((uint8_t)0x56)+ n/ P+ Q1 S0 H: g
  16. #define SPI_IT_MODF                     ((uint8_t)0x55)% Q( {' N& P) H, O5 v8 Y' l
  17. #define SPI_IT_CRCERR                   ((uint8_t)0x54)
    % u, ^  {/ F( j- ]

  18. , E+ B$ a; A) L5 S
  19. #define IS_SPI_I2S_CLEAR_IT(IT) (((IT) == SPI_IT_CRCERR))
    ' Y6 Q5 _) j# C. `' C
  20. ' [5 y3 r# E4 t& U& t9 G/ R
  21. #define IS_SPI_I2S_GET_IT(IT) (((IT) == SPI_I2S_IT_RXNE)|| ((IT) == SPI_I2S_IT_TXE) || \
    3 q$ y% }( [* I9 h
  22.                                ((IT) == SPI_IT_CRCERR)  || ((IT) == SPI_IT_MODF) || \# M$ H: L/ B, M* q
  23.                                ((IT) == SPI_I2S_IT_OVR) || ((IT) == I2S_IT_UDR) ||\
    1 q* `; D3 K' H! e& @, f# _. {
  24.                                ((IT) == SPI_I2S_IT_TIFRFE))
复制代码
5 b, q% i& O9 y7 G4 C
SPI_I2S_flags_definition
# _. s: v4 Q$ o
  1. /** @defgroup SPI_I2S_flags_definition ) ^  [! C; }  T8 o
  2.   * @{
    1 R3 \1 f  }  y  w: r  T
  3.   */
    ( h' e3 a( M* g( `

  4. ; @+ g; Q) d% W" V* X: V
  5. #define SPI_I2S_FLAG_RXNE               ((uint16_t)0x0001): i7 x, w% }- _' h1 n
  6. #define SPI_I2S_FLAG_TXE                ((uint16_t)0x0002)3 \  P7 G  ]! S) V6 }$ d0 z' `3 G& ]
  7. #define I2S_FLAG_CHSIDE                 ((uint16_t)0x0004)
    ( I0 E7 }3 \2 `: W7 N
  8. #define I2S_FLAG_UDR                    ((uint16_t)0x0008)
    2 R4 \& b( D& P) m9 D  |' c
  9. #define SPI_FLAG_CRCERR                 ((uint16_t)0x0010)
    9 ], F4 [: n- ]; ~
  10. #define SPI_FLAG_MODF                   ((uint16_t)0x0020)
    9 S( G* U9 A* j' H7 C
  11. #define SPI_I2S_FLAG_OVR                ((uint16_t)0x0040)8 z1 B; O' ?& {6 A" k" ]# n' X8 e: c
  12. #define SPI_I2S_FLAG_BSY                ((uint16_t)0x0080)* m0 U. p3 n' g  ?4 B, ?. c0 S8 ~
  13. #define SPI_I2S_FLAG_TIFRFE             ((uint16_t)0x0100)- t( ~- ^+ [( B" v4 W& b
  14. + T2 \% j' V0 A  N& Y/ b6 j$ u# L
  15. #define IS_SPI_I2S_CLEAR_FLAG(FLAG) (((FLAG) == SPI_FLAG_CRCERR))% l) `) [. k$ I+ Q. a. V2 N
  16. #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
  17.                                    ((FLAG) == SPI_FLAG_MODF) || ((FLAG) == SPI_FLAG_CRCERR) || \
    4 Y1 L7 ]0 ~' r% T* K: ~' Z
  18.                                    ((FLAG) == I2S_FLAG_UDR) || ((FLAG) == I2S_FLAG_CHSIDE) || \
    ) H, L- s' K) o8 E6 [& Z' u# C& i9 K
  19.                                    ((FLAG) == SPI_I2S_FLAG_TXE) || ((FLAG) == SPI_I2S_FLAG_RXNE)|| \- ]7 Y* Z( V7 ]
  20.                                    ((FLAG) == SPI_I2S_FLAG_TIFRFE))
复制代码

0 B$ {- c; @+ P& ]' W: \; t' y03. 相关函数2 X! d- b, }6 C, ^, @/ G
  1. /* Exported macro ------------------------------------------------------------*/5 i6 B7 X9 q4 k! ]" K$ k0 v, b
  2. /* Exported functions --------------------------------------------------------*/ . A, ~2 s, L4 W) n

  3. , A, s5 o) ]3 k! y. B
  4. /*  Function used to set the SPI configuration to the default reset state *****/ # f: A  i! [, Q0 b0 G: }# p7 V
  5. void SPI_I2S_DeInit(SPI_TypeDef* SPIx);
    , P, u5 w2 C6 g) F

  6. * d& e/ X  a5 e; j
  7. /* Initialization and Configuration functions *********************************/
    . J$ D; ^- h( M' V& i% Y; X
  8. void SPI_Init(SPI_TypeDef* SPIx, SPI_InitTypeDef* SPI_InitStruct);7 t0 \3 t! S& B! s2 l: Z6 E3 U
  9. void I2S_Init(SPI_TypeDef* SPIx, I2S_InitTypeDef* I2S_InitStruct);
    / v% B3 v7 B6 _0 P. v1 ]
  10. void SPI_StructInit(SPI_InitTypeDef* SPI_InitStruct);! b. u* y' M3 n1 B
  11. void I2S_StructInit(I2S_InitTypeDef* I2S_InitStruct);
    & e3 L. b' q* }7 h1 G
  12. void SPI_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState);
    % ^/ b+ C8 g. ?& e# r  }  s% e
  13. void I2S_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState);4 B, [& M7 y" M2 p* m
  14. void SPI_DataSizeConfig(SPI_TypeDef* SPIx, uint16_t SPI_DataSize);
    6 ?4 W+ n& P% `3 X8 O9 N% I
  15. void SPI_BiDirectionalLineConfig(SPI_TypeDef* SPIx, uint16_t SPI_Direction);" e% o5 X: Q8 ]  B2 R3 O% ?
  16. void SPI_NSSInternalSoftwareConfig(SPI_TypeDef* SPIx, uint16_t SPI_NSSInternalSoft);5 h! |5 |& ~7 @
  17. void SPI_SSOutputCmd(SPI_TypeDef* SPIx, FunctionalState NewState);
      D+ `8 {- q: y4 b( i/ `+ I
  18. void SPI_TIModeCmd(SPI_TypeDef* SPIx, FunctionalState NewState);
    / y+ l( x0 J9 H, a5 f( P
  19. 7 W& ?/ a' a3 Y, }* `0 Q
  20. void I2S_FullDuplexConfig(SPI_TypeDef* I2Sxext, I2S_InitTypeDef* I2S_InitStruct);6 O0 S' D/ y  p: A5 T& N
  21. 7 \3 q4 C  C7 n# k
  22. /* Data transfers functions ***************************************************/ * _$ E4 l" T. t* h0 b# K. f* u4 f
  23. void SPI_I2S_SendData(SPI_TypeDef* SPIx, uint16_t Data);# F1 N1 `+ K- m
  24. uint16_t SPI_I2S_ReceiveData(SPI_TypeDef* SPIx);( Y( n, d3 v- `

  25. - r( f' }4 P' S
  26. /* Hardware CRC Calculation functions *****************************************/
    , O# S4 W9 q4 G% J( h
  27. void SPI_CalculateCRC(SPI_TypeDef* SPIx, FunctionalState NewState);- |4 E1 J& E7 A- M4 r
  28. void SPI_TransmitCRC(SPI_TypeDef* SPIx);
    8 A2 w+ R# J  \$ F
  29. uint16_t SPI_GetCRC(SPI_TypeDef* SPIx, uint8_t SPI_CRC);
    # t; F% L+ I2 P6 k2 w# s% B
  30. uint16_t SPI_GetCRCPolynomial(SPI_TypeDef* SPIx);
    9 Y$ j: j1 o# h- ^; V, {0 y

  31. 1 {! m9 H% \3 O
  32. /* DMA transfers management functions *****************************************/' V' n$ z; d% j* ]
  33. void SPI_I2S_DMACmd(SPI_TypeDef* SPIx, uint16_t SPI_I2S_DMAReq, FunctionalState NewState);  F; X) N" O9 Z( ]$ W

  34. 5 \- }  a2 }5 G2 i, R4 T  Z/ {0 I
  35. /* Interrupts and flags management functions **********************************/
    ' c$ R; m& f: p3 x" b" w$ ^3 @& z
  36. void SPI_I2S_ITConfig(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT, FunctionalState NewState);
    9 o& ]( Q( Z! i  ^1 h- R+ Q8 a
  37. FlagStatus SPI_I2S_GetFlagStatus(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG);
    ! [8 t& W6 |1 e1 E' _& y: P
  38. void SPI_I2S_ClearFlag(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG);
    7 W$ D$ V8 r# r+ l3 l' I  o$ x' {
  39. ITStatus SPI_I2S_GetITStatus(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT);
    2 u2 q+ M: P2 N" J
  40. 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
  1. /** / ^5 u$ S4 E& g/ a: T  Z
  2.   * @brief Serial Peripheral Interface% y/ r7 A/ Z; W3 M0 a  w
  3.   */, o( V* H/ C: }6 b6 }( h0 r0 f

  4. . V( n* B' I/ m5 o
  5. typedef struct- a. d9 |, y2 v1 p) b- ?
  6. {: c+ ?7 S$ n! X4 ^
  7.   __IO uint16_t CR1;        /*!< SPI control register 1 (not used in I2S mode),      Address offset: 0x00 */- q4 l8 h5 G# W  z4 J  ~
  8.   uint16_t      RESERVED0;  /*!< Reserved, 0x02                                                           */
    & Z' Z3 A& ]) O+ w
  9.   __IO uint16_t CR2;        /*!< SPI control register 2,                             Address offset: 0x04 */
    2 z" e8 `5 z& R' F4 C
  10.   uint16_t      RESERVED1;  /*!< Reserved, 0x06                                                           */
    0 m9 d7 I' z$ l$ C$ C
  11.   __IO uint16_t SR;         /*!< SPI status register,                                Address offset: 0x08 */
    ! O2 U, q8 \( `
  12.   uint16_t      RESERVED2;  /*!< Reserved, 0x0A                                                           */6 p8 r% |" `3 L. J
  13.   __IO uint16_t DR;         /*!< SPI data register,                                  Address offset: 0x0C */
    # ?- h9 H+ u% T! R3 O: _( ^( b
  14.   uint16_t      RESERVED3;  /*!< Reserved, 0x0E                                                           */8 A" p- [7 r% {6 K: V# k# |* j2 L
  15.   __IO uint16_t CRCPR;      /*!< SPI CRC polynomial register (not used in I2S mode), Address offset: 0x10 */
    : Z) w- p2 |3 a7 V' F7 J
  16.   uint16_t      RESERVED4;  /*!< Reserved, 0x12                                                           */8 C; _0 a/ y1 P7 J, m) ?7 b, B! n6 s
  17.   __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
  18.   uint16_t      RESERVED5;  /*!< Reserved, 0x16                                                           */
    ; p8 k" [, i( v3 ~* w6 L
  19.   __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
  20.   uint16_t      RESERVED6;  /*!< Reserved, 0x1A                                                           */: e( j6 q% ?( a
  21.   __IO uint16_t I2SCFGR;    /*!< SPI_I2S configuration register,                     Address offset: 0x1C */) V$ N6 \/ n6 {& s& c. Z8 C& f
  22.   uint16_t      RESERVED7;  /*!< Reserved, 0x1E                                                           */
    : ^1 t9 |# \8 P7 X
  23.   __IO uint16_t I2SPR;      /*!< SPI_I2S prescaler register,                         Address offset: 0x20 */! m- c9 a3 N( W* E0 x7 o0 i
  24.   uint16_t      RESERVED8;  /*!< Reserved, 0x22                                                           */
    : Z0 o7 n/ [' y( t9 g
  25. } SPI_TypeDef;
复制代码

3 e; g* H9 V8 y+ D5 }+ P% L) V2 {5 `  }6 S

3 n* Q* a8 j3 f) b
收藏 评论0 发布时间:2022-3-27 17:00

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版