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

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

[复制链接]
STMCU小助手 发布时间:2022-6-16 17:00
01. I2C简介4 {4 I+ N0 g: C9 M' S+ }  V
I2C(内部集成电路)总线接口用作微控制器和 I2C 串行总线之间的接口。它提供多主模式功能,可以控制所有I2C总线特定的序列、协议、仲裁和时序。它支持标准和快速模式。它还与 SMBus 2.0 兼容。
0 A5 o" l3 q2 R
' A' M" j# K  d5 A4 d1 s2 v# ^6 T它可以用于多种用途,包括 CRC 生成和验证、SMBus(系统管理总线)以及 PMBus(电源管理总线)。
/ p9 D2 k5 j& U/ f  u0 r: K2 u0 g4 c' K
根据器件的不同,可利用 DMA 功能来减轻 CPU 的工作量。
! y# c/ ^1 Z. l6 P' f
0 P5 X" J6 C4 D. b02. 相关类型6 O* ]% L* Z2 Y4 X* Z3 h0 ?
I2C Init structure
# g$ G: w) W4 ^1 ?9 H/ z/ Q& ]- @' `" g, X
  1. /**
    . x( R1 J/ A# F  ?5 I
  2.   * @brief  I2C Init structure definition  
    8 r3 r* c. ?) ~% e" W% c0 ~
  3.   */
    4 H, ^% k" y& m4 l$ z( q3 l

  4. # K& o# K+ N2 U4 m+ Y* F+ ^
  5. typedef struct7 [7 t! A; v7 l9 R" N6 l2 H
  6. {
    2 m* V5 h6 W1 Q0 p. B' @) x
  7.   uint32_t I2C_ClockSpeed;          /*!< Specifies the clock frequency.
    * |# j. D! Y7 _% @
  8.                                          This parameter must be set to a value lower than 400kHz */
    $ W+ c1 s. B' [' ^9 k# j2 d

  9. * u- v! b( f7 h
  10.   uint16_t I2C_Mode;                /*!< Specifies the I2C mode.
    % L4 ?7 U% l  t7 c4 H, Q$ X" C
  11.                                          This parameter can be a value of @ref I2C_mode */
    - b8 x- M/ l5 ]' x' ~# f" l" |
  12. ; G6 v. C% Q* b" f* k0 p- }8 b
  13.   uint16_t I2C_DutyCycle;           /*!< Specifies the I2C fast mode duty cycle.
    + E+ M9 G9 ~( K' e, \* u
  14.                                          This parameter can be a value of @ref I2C_duty_cycle_in_fast_mode *// H: S$ e  m7 u5 W/ N. P

  15. % Q* R9 A% X3 _& k) ]- G
  16.   uint16_t I2C_OwnAddress1;         /*!< Specifies the first device own address.
    & ^+ i+ ^6 m+ c) o$ a
  17.                                          This parameter can be a 7-bit or 10-bit address. */
    6 x8 D; m' X$ c! Q

  18. : z( B' L+ i, J5 R7 [
  19.   uint16_t I2C_Ack;                 /*!< Enables or disables the acknowledgement.+ F! c9 O9 N0 g( z# L. y" @
  20.                                          This parameter can be a value of @ref I2C_acknowledgement */
    - U6 }) L1 u/ z' F4 ]
  21. ' x! G& Y$ ?8 h6 j  C
  22.   uint16_t I2C_AcknowledgedAddress; /*!< Specifies if 7-bit or 10-bit address is acknowledged.; k+ i0 a2 \$ e$ P' [6 l* q
  23.                                          This parameter can be a value of @ref I2C_acknowledged_address */
    * f" T4 L* Y1 i& D/ G
  24. }I2C_InitTypeDef;
复制代码

+ }! b" x( F  y* W) T/ {" K  E1 EI2C_Exported_Constants# [0 f# }* Q) g) p
. h: [* q4 i7 A5 R4 m. B! I' I4 A
  1.   M( d' Y; j0 J5 K; Y$ [
  2. /** @defgroup I2C_Exported_Constants
    ' D+ a5 A# P" G8 @
  3.   * @{, u, N6 T0 y, W3 y. l. n/ z
  4.   */* _( A5 p% @4 E  e$ r  Y, G) R
  5. #define IS_I2C_ALL_PERIPH(PERIPH) (((PERIPH) == I2C1) || \
    $ k4 w: i+ y3 z
  6.                                    ((PERIPH) == I2C2) || \
    6 i) _! e! O1 ~
  7.                                    ((PERIPH) == I2C3))
    : ^2 S3 {, q% Q( x
  8. /** @defgroup I2C_Digital_Filter" D+ R; ]4 U! z  U2 Z
  9.   * @{2 I. E+ M9 c. _1 c$ K) p/ y7 N
  10.   */  n* e! s# |/ k5 ^( j* O8 `

  11. & M" m$ d. h3 ~. Z! M, u( I% X1 g
  12. #define IS_I2C_DIGITAL_FILTER(FILTER)   ((FILTER) <= 0x0000000F)4 S; X) ~# u( L: I
  13. /**3 j# J- q: d, M* B, F
  14.   * @}
    " p8 q' q1 Q8 k* ?/ {: C
  15.   */
复制代码

4 D& v" n4 ~" F1 @& Z( {I2C_mode
  1. /** @defgroup I2C_mode 5 h* U" I! b% D- t
  2.   * @{5 K+ D% z; f, x# }4 l2 L2 `) K$ I
  3.   */. `& W' Y. g( l& h8 L

  4. & K% H' e' b% v9 Z+ B) U, U
  5. #define I2C_Mode_I2C                    ((uint16_t)0x0000)
    # J- [$ X7 r6 {! E8 ~$ s
  6. #define I2C_Mode_SMBusDevice            ((uint16_t)0x0002)  
    & n& ~3 [' ?# y
  7. #define I2C_Mode_SMBusHost              ((uint16_t)0x000A)
    & C/ G" V5 Z8 e, W
  8. #define IS_I2C_MODE(MODE) (((MODE) == I2C_Mode_I2C) || \! R7 s% ]+ ]! t9 s6 b3 i0 [
  9.                            ((MODE) == I2C_Mode_SMBusDevice) || \. ]# z' n) j0 L
  10.                            ((MODE) == I2C_Mode_SMBusHost))
复制代码
, _8 ~0 ^7 f; c$ R
I2C_duty_cycle_in_fast_mode
1 Y, h7 \: S5 N4 _% J: _" g0 `8 l3 i  ~  V- L4 p' v1 [' Q
  1. /** @defgroup I2C_duty_cycle_in_fast_mode
    ; b0 r3 ~3 j" v% n
  2.   * @{3 x  i1 v% U# P# ^7 ]; n' Z
  3.   */
    5 `% b  J" D! i
  4. . @  S, z; k: b* j7 U) G
  5. #define I2C_DutyCycle_16_9              ((uint16_t)0x4000) /*!< I2C fast mode Tlow/Thigh = 16/9 */) W) H& s# j) @( L- p
  6. #define I2C_DutyCycle_2                 ((uint16_t)0xBFFF) /*!< I2C fast mode Tlow/Thigh = 2 *// t, \. `' @- C! C# [
  7. #define IS_I2C_DUTY_CYCLE(CYCLE) (((CYCLE) == I2C_DutyCycle_16_9) || \
    - }3 h9 d7 B7 t5 M
  8.                                   ((CYCLE) == I2C_DutyCycle_2))
复制代码

" U, e* b3 n/ ~$ N" n' eI2C_acknowledgement
4 I0 Y* h) @. K" G: k9 y& c( O: T! c) e( {9 i! j
  1. /** @defgroup I2C_acknowledgement- M* t$ w- i; q5 i* G6 p# h
  2.   * @{6 N) ~. J  G' G- j& q2 P, X
  3.   */$ o" l* @( M$ N3 T5 F- g

  4. ! p- H, G; U2 o' |- w; I, I
  5. #define I2C_Ack_Enable                  ((uint16_t)0x0400)
    ' y$ _. A9 N; b6 V/ G  l: @, _; a) L
  6. #define I2C_Ack_Disable                 ((uint16_t)0x0000)
    % m& S  u7 _3 h6 t+ u
  7. #define IS_I2C_ACK_STATE(STATE) (((STATE) == I2C_Ack_Enable) || \6 }$ ^3 @& D( G2 D, @
  8.                                  ((STATE) == I2C_Ack_Disable))
复制代码
/ S4 r# {7 @, y0 K
I2C_transfer_direction
/ a: x$ Z- {4 H% m* F) F9 z
: ^2 I- K$ u  z3 j
  1. /** @defgroup I2C_transfer_direction 4 m8 N: l) ^" q! l% V8 V: f
  2.   * @{
    ! f# U( `' m- M1 H
  3.   */4 A. o# q7 h' c& O+ i
  4. 4 J2 r" o& m7 G& P
  5. #define  I2C_Direction_Transmitter      ((uint8_t)0x00)( j( \+ v% @8 t; c7 _( M+ ]
  6. #define  I2C_Direction_Receiver         ((uint8_t)0x01)) l4 [! A' f& X$ C1 s
  7. #define IS_I2C_DIRECTION(DIRECTION) (((DIRECTION) == I2C_Direction_Transmitter) || \. Z. j2 f  r: N
  8.                                      ((DIRECTION) == I2C_Direction_Receiver))
复制代码

2 m+ Z3 V6 ^$ |% N5 HI2C_acknowledged_address5 O7 }* N! ^) k1 X

% V3 r5 i& q$ n9 r; M4 }3 w4 o
  1. /** @defgroup I2C_acknowledged_address
    0 B: z9 A; g% T4 C% M. E, R1 ~6 @
  2.   * @{
    9 v# k# S8 k2 b& C0 q8 ~
  3.   */
    & E5 T7 k# w- m! N$ Z
  4. * W/ c8 ]- _( L
  5. #define I2C_AcknowledgedAddress_7bit    ((uint16_t)0x4000)
    6 l6 n$ v: h4 M4 F* a
  6. #define I2C_AcknowledgedAddress_10bit   ((uint16_t)0xC000)
    ) a9 F" v& g+ T: c% w' o* J4 U
  7. #define IS_I2C_ACKNOWLEDGE_ADDRESS(ADDRESS) (((ADDRESS) == I2C_AcknowledgedAddress_7bit) || \
    2 Q' `# F( ?9 u$ w. e0 }6 N
  8.                                              ((ADDRESS) == I2C_AcknowledgedAddress_10bit))
复制代码

/ ?: A) b$ l& @8 e/ y9 F8 fI2C_registers
# W  ]$ n& ~! B( s& }1 l. `2 w# U( K0 g5 x+ d/ |9 l( E* p
  1. /** @defgroup I2C_registers 2 w! k6 I& e# Z0 S
  2.   * @{5 o7 N/ l& V6 C' f; b2 `3 o
  3.   */
    * y  {  {6 e/ }3 w5 ]5 @9 r
  4. 7 F: h+ s' X; M+ b# R
  5. #define I2C_Register_CR1                ((uint8_t)0x00)
    : L* [5 Q5 Q3 _% \) n/ G9 t, p
  6. #define I2C_Register_CR2                ((uint8_t)0x04)+ {' R4 A4 t; Z; @
  7. #define I2C_Register_OAR1               ((uint8_t)0x08)/ H  @6 d: \# s
  8. #define I2C_Register_OAR2               ((uint8_t)0x0C)- P7 O" e4 R' s- U0 N/ r
  9. #define I2C_Register_DR                 ((uint8_t)0x10)7 N" ?& K% S: v. G9 x( |
  10. #define I2C_Register_SR1                ((uint8_t)0x14)
    , `( ~" F2 F3 i' E& k2 V
  11. #define I2C_Register_SR2                ((uint8_t)0x18)
    $ q( t+ ~5 `5 [4 Z# E, e2 M
  12. #define I2C_Register_CCR                ((uint8_t)0x1C)
    " k  ^5 a' R1 I: a' m1 w
  13. #define I2C_Register_TRISE              ((uint8_t)0x20)
    0 w) @! C& O, u$ R7 X
  14. #define IS_I2C_REGISTER(REGISTER) (((REGISTER) == I2C_Register_CR1) || \3 E. d! ~7 J( R$ W( |
  15.                                    ((REGISTER) == I2C_Register_CR2) || \& H3 p7 k8 e0 K3 D$ e
  16.                                    ((REGISTER) == I2C_Register_OAR1) || \9 O5 ^" R" w. f9 S' {6 M
  17.                                    ((REGISTER) == I2C_Register_OAR2) || \
    1 `" \! n' d1 ]. N4 j2 Q3 t% t
  18.                                    ((REGISTER) == I2C_Register_DR) || \
    0 h4 n+ b  `( k$ `( x
  19.                                    ((REGISTER) == I2C_Register_SR1) || \) I- S* p+ J- l* p% \
  20.                                    ((REGISTER) == I2C_Register_SR2) || \- L: f3 c% w" G% D
  21.                                    ((REGISTER) == I2C_Register_CCR) || \
    ; Y+ Y0 U7 g: O8 C8 u! B( x0 ~, r2 R
  22.                                    ((REGISTER) == I2C_Register_TRISE))
复制代码
  N4 s$ ]) v6 y9 j1 Q
I2C_NACK_position* ~& D- ^% \) k1 n: D. O
; |7 Q( z5 X: C, ]
  1. /** @defgroup I2C_NACK_position
    6 D) N; b1 G* N0 V/ ~! ]
  2.   * @{
    0 n. q  `; B  X8 m! m7 S8 o" W
  3.   */9 u* z' Y/ S2 q" r% h

  4.   _' Q$ Z, o1 c( ~! [4 R$ R9 {! M2 z+ O
  5. #define I2C_NACKPosition_Next           ((uint16_t)0x0800)
    % R+ R/ E8 E5 m( U; y, z$ L
  6. #define I2C_NACKPosition_Current        ((uint16_t)0xF7FF)
    # d1 {- {$ b5 _7 v7 I5 F6 F! t3 \, @& g' O
  7. #define IS_I2C_NACK_POSITION(POSITION)  (((POSITION) == I2C_NACKPosition_Next) || \
    " [5 S5 I& _9 F, Z4 @; ?
  8.                                          ((POSITION) == I2C_NACKPosition_Current))
复制代码

3 T7 X, G* _$ Q( tI2C_SMBus_alert_pin_level: T7 f" k! ^) }" H
' z0 p# U& w: V2 W/ H6 o* O& N  X% S
  1. /** @defgroup I2C_SMBus_alert_pin_level % F& ?- w- @, P* b
  2.   * @{( `) F8 C. c3 j! S* d
  3.   */( r2 Q% l# L5 b  v  s
  4. , ?. ]& r; I/ W
  5. #define I2C_SMBusAlert_Low              ((uint16_t)0x2000)
    7 ~, ~! H. c$ H6 T
  6. #define I2C_SMBusAlert_High             ((uint16_t)0xDFFF)
    8 J" N( O5 C* i7 [* v8 D
  7. #define IS_I2C_SMBUS_ALERT(ALERT) (((ALERT) == I2C_SMBusAlert_Low) || \
    8 A8 O3 t8 Y9 M
  8.                                    ((ALERT) == I2C_SMBusAlert_High))
复制代码

1 q7 u  }, v! a7 R1 C" W8 |I2C_PEC_position5 e; t8 V5 ^- A1 }
/ `: X8 B) B+ |  {
  1. /** @defgroup I2C_PEC_position
    : M' n4 b# W$ k/ Q- V: A: `
  2.   * @{
    $ z8 T) ]+ W  r+ l1 _
  3.   */
    # `6 ]5 ^* p' k7 o0 N
  4. : }; T7 n" f; y$ T5 v4 u
  5. #define I2C_PECPosition_Next            ((uint16_t)0x0800)2 V. ?' o8 H: Y9 ]$ q
  6. #define I2C_PECPosition_Current         ((uint16_t)0xF7FF)
    " I; `: a" f3 h; I$ Y
  7. #define IS_I2C_PEC_POSITION(POSITION) (((POSITION) == I2C_PECPosition_Next) || \0 I8 t! T/ Y( ~$ T" U; B
  8.                                        ((POSITION) == I2C_PECPosition_Current))
复制代码
' A7 B% ]3 c. M
I2C_interrupts_definition6 @) G6 T! l, d; R: J9 C

; _5 }- ]' |  m+ B0 I; |2 b  j% h
  1. /** @defgroup I2C_interrupts_definition ' S" J! ^& z8 t, k" x8 Z' ~
  2.   * @{0 L9 _/ e& G& u9 S. j
  3.   */" `8 K2 v7 E8 z+ l  v

  4. & M# y3 v- ]. L) A( y
  5. #define I2C_IT_BUF                      ((uint16_t)0x0400)
    * D- R2 c$ \0 |  X
  6. #define I2C_IT_EVT                      ((uint16_t)0x0200)2 u# t) A% ]4 h/ _  O
  7. #define I2C_IT_ERR                      ((uint16_t)0x0100)
    / F* @1 ^- g! y3 H( o" q
  8. #define IS_I2C_CONFIG_IT(IT) ((((IT) & (uint16_t)0xF8FF) == 0x00) && ((IT) != 0x00))
复制代码

5 s' \! H% S8 j/ m1 X5 GI2C_interrupts_definition0 [. r! H( W. h( I6 j+ x4 P

- q! y( a* `* e: f& ~1 o
  1. /** @defgroup I2C_interrupts_definition
    " j+ ]% ^8 l8 H  n5 W* Z- i1 V
  2.   * @{& B& d9 _9 @1 x
  3.   */
    / h! @4 n: R, Y& N* K6 e

  4. 8 \& F- |1 D; P# k% i" J# x) ^
  5. #define I2C_IT_SMBALERT                 ((uint32_t)0x01008000)
    $ a" D% h8 W' q* i- j: r0 ~- m' J
  6. #define I2C_IT_TIMEOUT                  ((uint32_t)0x01004000)
    6 k, u5 E" y+ E. k" J7 a
  7. #define I2C_IT_PECERR                   ((uint32_t)0x01001000)3 H* {9 {. o) j) B
  8. #define I2C_IT_OVR                      ((uint32_t)0x01000800)
    0 D5 _9 W0 M0 M0 b
  9. #define I2C_IT_AF                       ((uint32_t)0x01000400)
    - Z# ~2 r  h# o; H) O* \
  10. #define I2C_IT_ARLO                     ((uint32_t)0x01000200)
    5 P( v* R; p1 v- D  k; E
  11. #define I2C_IT_BERR                     ((uint32_t)0x01000100)
      y2 e0 a2 q' s0 F6 b  o9 ~/ a
  12. #define I2C_IT_TXE                      ((uint32_t)0x06000080)
    ; S% M) Z: _, G0 \
  13. #define I2C_IT_RXNE                     ((uint32_t)0x06000040)
    7 H* ?8 b; S# W& n
  14. #define I2C_IT_STOPF                    ((uint32_t)0x02000010)
    % q3 b& ~1 M( S! A' n- T* Q
  15. #define I2C_IT_ADD10                    ((uint32_t)0x02000008)
    , s& t' I; c% H( i
  16. #define I2C_IT_BTF                      ((uint32_t)0x02000004); ]$ b5 B. }( A4 A/ L/ |! u
  17. #define I2C_IT_ADDR                     ((uint32_t)0x02000002)
    8 T" f6 T; l8 L, k
  18. #define I2C_IT_SB                       ((uint32_t)0x02000001)
    & Z. B/ v( x" C8 Q! \4 M
  19. 8 e6 j/ E" g2 b, E5 I- Q0 H7 ]& @
  20. #define IS_I2C_CLEAR_IT(IT) ((((IT) & (uint16_t)0x20FF) == 0x00) && ((IT) != (uint16_t)0x00))
    + |1 @( s& u4 l7 V! ^
  21. , Y) \' ~/ b; d  {
  22. #define IS_I2C_GET_IT(IT) (((IT) == I2C_IT_SMBALERT) || ((IT) == I2C_IT_TIMEOUT) || \
    & ]1 R, \& j- h* m6 z5 x
  23.                            ((IT) == I2C_IT_PECERR) || ((IT) == I2C_IT_OVR) || \
    2 z  [$ j5 f6 W4 X/ h: Y
  24.                            ((IT) == I2C_IT_AF) || ((IT) == I2C_IT_ARLO) || \( h8 n; J3 P0 J6 o% I6 Z
  25.                            ((IT) == I2C_IT_BERR) || ((IT) == I2C_IT_TXE) || \
    : f2 Q2 e8 x! f8 Q8 ]
  26.                            ((IT) == I2C_IT_RXNE) || ((IT) == I2C_IT_STOPF) || \/ ], F: _- X+ L7 y/ e4 G$ s
  27.                            ((IT) == I2C_IT_ADD10) || ((IT) == I2C_IT_BTF) || \
    7 |7 W4 i$ V, q7 Q- t
  28.                            ((IT) == I2C_IT_ADDR) || ((IT) == I2C_IT_SB))
复制代码

0 I- H' z4 J) H" RSR2 register flags7 P5 x3 _8 [, J

' Y& k8 d% V% |- G# @6 `4 B
  1. /**
    , }- j, v% f5 o% j6 w+ Q  j
  2.   * @brief  SR2 register flags  ) I+ I* h8 @/ a# c% A
  3.   */
    3 N6 C  t2 X. j" e& X4 z
  4. ! c: V! T5 r) A' B
  5. #define I2C_FLAG_DUALF                  ((uint32_t)0x00800000)
    ! ~8 S# `& w" e
  6. #define I2C_FLAG_SMBHOST                ((uint32_t)0x00400000)
    5 u4 Z3 p$ F' o# M, {" w, ~
  7. #define I2C_FLAG_SMBDEFAULT             ((uint32_t)0x00200000)1 k# X7 j# r. y" w. o/ ~  c' w( L
  8. #define I2C_FLAG_GENCALL                ((uint32_t)0x00100000)& P* U! Y( i" K1 k0 }
  9. #define I2C_FLAG_TRA                    ((uint32_t)0x00040000)& a/ y7 w% R5 ~, d& S) T( A) e
  10. #define I2C_FLAG_BUSY                   ((uint32_t)0x00020000)
    3 o8 z5 P8 I+ _# e) j5 h# |
  11. #define I2C_FLAG_MSL                    ((uint32_t)0x00010000)
复制代码

4 y% E1 j& Q' GSR1 register flags2 X/ h* _! ]) A' J6 F( x

: W6 E! s  I( D* D  s/ n
  1. /** 0 Q; g2 ?! j: E1 y0 _& z* B
  2.   * @brief  SR1 register flags  ! a4 @! ?9 v" F
  3.   */. t4 w& L4 J% Y! `# D" }/ k

  4. . ~- c, f) c. p! n4 ^2 ^7 }
  5. #define I2C_FLAG_SMBALERT               ((uint32_t)0x10008000)# B+ y9 Z! i! q5 P9 l
  6. #define I2C_FLAG_TIMEOUT                ((uint32_t)0x10004000)$ {7 S$ ]3 k- e" n
  7. #define I2C_FLAG_PECERR                 ((uint32_t)0x10001000)
    9 `' q8 w7 n( m
  8. #define I2C_FLAG_OVR                    ((uint32_t)0x10000800)
    - Z/ r6 X: C7 m- \
  9. #define I2C_FLAG_AF                     ((uint32_t)0x10000400)
    " ^; @; |% a) P- L: k% t' ^. }0 c
  10. #define I2C_FLAG_ARLO                   ((uint32_t)0x10000200)2 f& |* {* T( p, k$ J) ]+ E
  11. #define I2C_FLAG_BERR                   ((uint32_t)0x10000100)) Q, w& U, P, L+ S- P8 d
  12. #define I2C_FLAG_TXE                    ((uint32_t)0x10000080)
    ) a) T1 R4 @) l  q5 I3 o- g8 r- [
  13. #define I2C_FLAG_RXNE                   ((uint32_t)0x10000040)
    - J; M6 B% y3 s! o3 S+ E
  14. #define I2C_FLAG_STOPF                  ((uint32_t)0x10000010)- p5 f' @: B3 o: K
  15. #define I2C_FLAG_ADD10                  ((uint32_t)0x10000008)( C% K# V2 H+ h# u& X; K: a. S( _0 M
  16. #define I2C_FLAG_BTF                    ((uint32_t)0x10000004)# N: P- c; U  C) g/ e
  17. #define I2C_FLAG_ADDR                   ((uint32_t)0x10000002)
    ) l$ F6 j1 Y- G* S* g
  18. #define I2C_FLAG_SB                     ((uint32_t)0x10000001)
    6 l! c+ q; _& l0 F6 i4 q" Y

  19. 4 P& o7 k7 L8 u& g/ [& v* k
  20. #define IS_I2C_CLEAR_FLAG(FLAG) ((((FLAG) & (uint16_t)0x20FF) == 0x00) && ((FLAG) != (uint16_t)0x00))
    4 ?7 R0 u0 ]* F9 F

  21. $ |7 K7 w# t# X% x' }* ]8 S
  22. #define IS_I2C_GET_FLAG(FLAG) (((FLAG) == I2C_FLAG_DUALF) || ((FLAG) == I2C_FLAG_SMBHOST) || \2 f& m( b# \3 `3 x: M
  23.                                ((FLAG) == I2C_FLAG_SMBDEFAULT) || ((FLAG) == I2C_FLAG_GENCALL) || \
    5 g2 X* X5 x6 M0 g! x* I( ?
  24.                                ((FLAG) == I2C_FLAG_TRA) || ((FLAG) == I2C_FLAG_BUSY) || \
    ( ]. M, c6 q8 H
  25.                                ((FLAG) == I2C_FLAG_MSL) || ((FLAG) == I2C_FLAG_SMBALERT) || \3 C! `' J/ S# h+ n. \# m2 F0 S1 l
  26.                                ((FLAG) == I2C_FLAG_TIMEOUT) || ((FLAG) == I2C_FLAG_PECERR) || \
    # K5 ]9 @# B, s2 H
  27.                                ((FLAG) == I2C_FLAG_OVR) || ((FLAG) == I2C_FLAG_AF) || \
    * j1 Q/ j# n+ T: w5 n" l
  28.                                ((FLAG) == I2C_FLAG_ARLO) || ((FLAG) == I2C_FLAG_BERR) || \
    6 V4 G0 [$ z8 {% k. X6 I
  29.                                ((FLAG) == I2C_FLAG_TXE) || ((FLAG) == I2C_FLAG_RXNE) || \5 h  E+ j1 a; Y! `
  30.                                ((FLAG) == I2C_FLAG_STOPF) || ((FLAG) == I2C_FLAG_ADD10) || \$ G7 B) l" n  j, u% p
  31.                                ((FLAG) == I2C_FLAG_BTF) || ((FLAG) == I2C_FLAG_ADDR) || \  k- `% @4 f$ P( s4 B
  32.                                ((FLAG) == I2C_FLAG_SB))
复制代码

" Q, R* g! ~3 }03. 相关函数
* {) p6 n" n5 \9 o( ]
  1. /*  Function used to set the I2C configuration to the default reset state *****/
    / Z" l. Q, @2 y+ W
  2. void I2C_DeInit(I2C_TypeDef* I2Cx);4 `+ b: T; ]/ K6 D0 ^" j; I
  3. # f6 x- S1 r8 {1 o3 w
  4. /* Initialization and Configuration functions *********************************/
    ! K1 P$ L0 w. T% C. P, B
  5. void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct);
    / }9 K3 ^( J, ~7 ~* b% M8 m/ A
  6. void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct);3 ^2 @% Z$ ]' p7 V7 B
  7. void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState);& S0 g, c; K1 H) U7 [6 R
  8. void I2C_DigitalFilterConfig(I2C_TypeDef* I2Cx, uint16_t I2C_DigitalFilter);
      S: a0 [& M' u4 E' \
  9. void I2C_AnalogFilterCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
    1 A4 b* f3 X3 g1 x' J! e6 ?& n6 C! C
  10. void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState);" I- a. n1 U  S; M- W
  11. void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState);
    7 p7 `: W- ?0 t0 k
  12. void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, uint8_t I2C_Direction);
    . h+ T& b0 h& n7 r
  13. void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState);
    8 t( B$ L' \* C7 f  s$ k2 Z
  14. void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint8_t Address);
    / f3 h9 Y' R& H( V8 w6 H
  15. void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
    7 R' d! |% U/ {9 z
  16. void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);+ c* a9 {& B  v' \6 X) Z
  17. void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);- V+ Y2 E) U4 W+ F
  18. void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);  K/ W! ]  G3 R2 Z1 s" l
  19. void I2C_FastModeDutyCycleConfig(I2C_TypeDef* I2Cx, uint16_t I2C_DutyCycle);
    4 D/ |% S7 H, _  V( S
  20. void I2C_NACKPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_NACKPosition);
    ; @2 b6 i  X- x
  21. void I2C_SMBusAlertConfig(I2C_TypeDef* I2Cx, uint16_t I2C_SMBusAlert);
    3 }% E: E4 A- x/ i+ ]& I
  22. void I2C_ARPCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
    4 `: F. {& c; N) X

  23.   ]+ F$ u- z+ J1 T8 ]& J+ P
  24. /* Data transfers functions ***************************************************/
    & ~- H0 G! D1 \8 z
  25. void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data);
    8 ~: F" ?; G; v  v4 \" J
  26. uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx);
    . l( b# [, i  z" L
  27. 7 f' n" }% \% T0 x6 t3 Z+ {
  28. /* PEC management functions ***************************************************/
    & g! I3 T+ p; ~. f
  29. void I2C_TransmitPEC(I2C_TypeDef* I2Cx, FunctionalState NewState);
    ( \- @# C: B: q
  30. void I2C_PECPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_PECPosition);2 R) j8 T8 g* L/ b8 v$ \+ A
  31. void I2C_CalculatePEC(I2C_TypeDef* I2Cx, FunctionalState NewState);! ]2 m4 h' ^6 e- R
  32. uint8_t I2C_GetPEC(I2C_TypeDef* I2Cx);# T5 E3 _* x; Y# _) F3 ~. q3 e

  33. 6 O, I; n& A6 N) Z# f! k6 ]
  34. /* DMA transfers management functions *****************************************/
    " V( a+ R1 }' s2 t% J
  35. void I2C_DMACmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
    . |2 J0 h. _1 a: u. ]3 v
  36. void I2C_DMALastTransferCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
    & i, z) {* a' Q6 e

  37. $ d5 h; h. h' Y9 U6 Q8 R1 q9 s) }3 u
  38. /* Interrupts, events and flags management functions **************************// ]7 |0 c. E. ]* F! Y$ g+ o. n# i6 ]
  39. uint16_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register);/ B3 U9 I! p$ }% Q3 x
  40. void I2C_ITConfig(I2C_TypeDef* I2Cx, uint16_t I2C_IT, FunctionalState NewState);
    3 m% f" O' p8 r% t* r5 w- y
  41. , h" r, l) g* y1 R( ]* {8 C8 l
  42. /* 3 f! H& h6 B9 L- H
  43. ===============================================================================
    / L3 _+ [+ a: n4 K: {
  44.                           I2C State Monitoring Functions
    9 H$ e: U5 H6 @/ @
  45. ===============================================================================
    & J& p- P4 j- r( ~7 k5 B
  46.   This I2C driver provides three different ways for I2C state monitoring
    ' @& `# i3 x9 G2 H) B
  47.   depending on the application requirements and constraints:
    # X, N- M- s- |4 }
  48. 1 d/ c& {: W- k) M4 W8 j! k

  49. $ m* h  J5 D; S$ p7 t
  50.      1. Basic state monitoring (Using I2C_CheckEvent() function); `; |! N* B4 k' C/ u
  51.      -----------------------------------------------------------
    3 |  D4 O( K9 Z! C0 g" s1 U
  52.         It compares the status registers (SR1 and SR2) content to a given event. w( _7 u8 U* G  f9 Y8 Y
  53.         (can be the combination of one or more flags).& a/ s# A7 x* ^
  54.         It returns SUCCESS if the current status includes the given flags / M+ k4 j1 i. [5 \% F( ~: F
  55.         and returns ERROR if one or more flags are missing in the current status.* |7 F9 r( g  e9 X7 Q

  56. , o" C, J: W" G2 v
  57.           - When to use1 |4 W5 e" [! \4 E" k
  58.              - This function is suitable for most applications as well as for startup
    - M! j7 X) U: ~6 z) X4 j& m
  59.                activity since the events are fully described in the product reference ( E# [0 S% t# J
  60.                manual (RM0090).
    & f, l$ ]1 _7 {
  61.              - It is also suitable for users who need to define their own events.
    # q, r4 w9 g" R, S6 |3 k4 o
  62. # f4 r# a2 F) u# U
  63.           - Limitations2 N2 i, @; W" e, A* h: W
  64.              - If an error occurs (ie. error flags are set besides to the monitored
    0 Z8 `0 s, @1 w  l( H& T
  65.                flags), the I2C_CheckEvent() function may return SUCCESS despite
    - R% D  r' c& r* ?8 B6 f: I
  66.                the communication hold or corrupted real state.
    ( L& j" V  ]% ]; {$ j
  67.                In this case, it is advised to use error interrupts to monitor
    0 D1 l8 q' a  l1 e& B6 [! \
  68.                the error events and handle them in the interrupt IRQ handler.8 G" R' i- m3 J, L
  69.   x; R. X" Z! J; }0 [; w' x  z* z8 k
  70.      Note
      @, b" m( I# w" w) S3 @! P
  71.          For error management, it is advised to use the following functions:+ |2 x) E7 G; {: Z5 _
  72.            - I2C_ITConfig() to configure and enable the error interrupts (I2C_IT_ERR).
      r; P& R' O5 L% O% q( \! e' v
  73.            - I2Cx_ER_IRQHandler() which is called when the error interrupt occurs.
    9 `( Q) i' V! z6 a+ B$ t- K3 L
  74.              Where x is the peripheral instance (I2C1, I2C2 ...)
    ; K& X8 q. K. E2 D# f7 g1 N
  75.            - I2C_GetFlagStatus() or I2C_GetITStatus()  to be called into the / _4 H/ w! i7 \3 J0 A( Q1 w
  76.              I2Cx_ER_IRQHandler() function in order to determine which error occurred.
    2 I' X' n) |. [2 {5 o3 Y/ O
  77.            - I2C_ClearFlag() or I2C_ClearITPendingBit() and/or I2C_SoftwareResetCmd() ) @9 x$ h% ]7 z
  78.              and/or I2C_GenerateStop() in order to clear the error flag and source
    . P) t3 ?& c2 X5 f; `+ k, b
  79.              and return to correct  communication status.
    9 o; [' q; Q, \2 B
  80. 9 c* z+ `9 d: F/ [  y! A

  81. . L4 n! H* [7 X  s8 B) j8 v
  82.      2. Advanced state monitoring (Using the function I2C_GetLastEvent())
    " C! [7 k, ]7 g6 L: R! `5 h: z
  83.      --------------------------------------------------------------------
    * O1 N  s4 D* S5 q
  84.         Using the function I2C_GetLastEvent() which returns the image of both status " u5 v$ j3 L, }; p
  85.         registers in a single word (uint32_t) (Status Register 2 value is shifted left
    ) E2 O, `& M* g; F$ V- C9 \
  86.         by 16 bits and concatenated to Status Register 1).
    " H1 G+ k1 c# {6 w& a& j0 d+ }( ~

  87. ( w& i1 ^2 B8 S) q
  88.           - When to use' E* y) V  h' z7 F4 U
  89.              - This function is suitable for the same applications above but it 4 g5 Q9 R# i, }$ y' @
  90.                allows to overcome the mentioned limitation of I2C_GetFlagStatus() 0 p" X2 r- y0 o" Y  N* V
  91.                function.' H1 ]& `& r" v7 k/ X
  92.              - The returned value could be compared to events already defined in
    3 Y. H) M$ r& U) T6 s
  93.                this file or to custom values defined by user.
    * c- v8 h2 i, `5 E7 Z) _/ v: t9 M# F
  94.                This function is suitable when multiple flags are monitored at the
    $ t4 \! P4 ^0 A  v/ V8 X
  95.                same time.
    ! O7 x& w3 b4 _( Y6 a
  96.              - At the opposite of I2C_CheckEvent() function, this function allows * p$ K% o) L8 i; s% r
  97.                user to choose when an event is accepted (when all events flags are
    . B! Q2 ^& W, A! R. A- C
  98.                set and no other flags are set or just when the needed flags are set 4 [4 u0 @: @7 p- ]
  99.                like I2C_CheckEvent() function.
    , z2 ?2 @7 R2 b

  100. $ T2 a% }  M3 Z* @3 e
  101.           - Limitations3 u' z4 B% Y: V$ T9 i8 T4 Y  j
  102.              - User may need to define his own events.
    ; e  i$ B9 J  A0 K: g
  103.              - Same remark concerning the error management is applicable for this ( D) T- i! g; [
  104.                function if user decides to check only regular communication flags
    . N2 g2 z" T8 m5 P  v8 j( V+ J
  105.                (and ignores error flags).( I; n0 w4 }$ ^" d$ [, R4 P3 d% |$ T
  106. ) ^  P1 w% X2 T
  107. 1 t! _* k  ^; W" ^9 L
  108.      3. Flag-based state monitoring (Using the function I2C_GetFlagStatus()): H% v6 d3 L1 p; s3 [" S
  109.      -----------------------------------------------------------------------9 O7 g, N1 @8 Q; p: U7 T& l  [7 w8 j

  110. * ?) S& O! z( y: G) F. ~
  111.       Using the function I2C_GetFlagStatus() which simply returns the status of
    0 x1 M$ g% j; }! r7 M
  112.       one single flag (ie. I2C_FLAG_RXNE ...).
    & b0 ], f7 t- {% S* P: C% [

  113. : g( q1 h' X8 U* {8 r! J% U- y
  114.           - When to use, ~) j  r% p$ {7 {$ c! Y3 h
  115.              - This function could be used for specific applications or in debug
    ( B0 F- s9 y# }+ }' f+ Z9 M
  116.                phase.$ j# @: [) o- Y, y0 d  Z
  117.              - It is suitable when only one flag checking is needed (most I2C 5 c' j5 }( r, r' w
  118.                events are monitored through multiple flags).
    . a+ R4 u9 p3 l$ ^
  119.           - Limitations: 1 F+ T( h) T4 _
  120.              - When calling this function, the Status register is accessed. & d; t( _& h% I# |% c6 F: O2 k
  121.                Some flags are cleared when the status register is accessed.
    $ h) Z2 V6 n# g* N3 k; E% o) l
  122.                So checking the status of one Flag, may clear other ones.
    0 j8 A% _6 V3 l  u0 a
  123.              - Function may need to be called twice or more in order to monitor * ?. E% O4 Q& e; M' f
  124.                one single event.           * y3 c, o+ C7 N/ e3 t  f8 F
  125. */0 _5 y* u/ f# V0 ?

  126. : S. b& ^- p' q0 b4 {; S
  127. /*
    - L: n' Y; q, o$ K  @
  128. ===============================================================================& J8 \7 }, M$ K3 j
  129.                           1. Basic state monitoring0 m! r/ Z& `; P" y( }. \
  130. ===============================================================================) @  R6 N( \- z& s2 J& z  C
  131. */! J5 h/ g( g0 t4 g
  132. ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT);
    & H2 g" w7 Z% Q' S
  133. /*+ C5 b4 ?6 G4 ]0 s: I# a
  134. ===============================================================================4 x- o4 a8 |) C) G( r1 s8 w
  135.                           2. Advanced state monitoring0 U/ D5 w5 M. D! q8 b
  136. ===============================================================================3 f/ Q& c# [" W
  137. */
    # ], s, E: g6 s, ~$ u' d1 K
  138. uint32_t I2C_GetLastEvent(I2C_TypeDef* I2Cx);
    4 M0 Y) p9 R+ i. B) ]( ]/ }
  139. /*% ~2 y# r7 Y2 a6 w+ L5 Z/ j1 l; r' f
  140. ===============================================================================
    7 A/ y1 l( V8 V3 F& Z
  141.                           3. Flag-based state monitoring  G) @4 j6 r* U7 R/ H& b
  142. ===============================================================================$ i. t. X! X2 ]6 a; `6 n5 |
  143. */: r# Y3 b3 ^: ]4 k8 a
  144. FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);. p1 Z+ u" Q) U; l4 q
  145. 4 m# L1 j6 H4 i; [
  146. 1 k' v# I( w$ D& k! |2 B3 v# j9 `
  147. void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);
    " |, l: C+ \5 r: E$ R6 O. _
  148. ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT);
    ! x$ |+ ]% }% O( S% m5 v! v
  149. void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT);
复制代码

% u0 S& o; l8 p04. 结构体封装& s) y  \% [8 c( C' d% ~1 d
  1. /**
    $ D' ^* t& H, c! T7 F
  2.   * @brief Inter-integrated Circuit Interface
    5 ]; H) J- S8 ?' z5 a! s) J" ^* V
  3.   */
    % U+ d: F1 j1 a, W0 @3 f

  4. 7 H9 J( E$ O) s5 I' o9 \- i+ c8 O
  5. typedef struct& T6 n: g: H4 y2 E& s
  6. {* D1 v/ x+ j$ E; O7 O
  7.   __IO uint16_t CR1;        /*!< I2C Control register 1,     Address offset: 0x00 */
    / z0 Y6 K: b) |; w
  8.   uint16_t      RESERVED0;  /*!< Reserved, 0x02                                   */. s; _9 F; t% T, C7 R/ @
  9.   __IO uint16_t CR2;        /*!< I2C Control register 2,     Address offset: 0x04 */
    . g0 _: {1 [4 i2 D: C
  10.   uint16_t      RESERVED1;  /*!< Reserved, 0x06                                   */7 \% @# _$ a5 I' x  y- H/ H, Q0 b
  11.   __IO uint16_t OAR1;       /*!< I2C Own address register 1, Address offset: 0x08 *// y6 U% G5 Y% \+ i- |
  12.   uint16_t      RESERVED2;  /*!< Reserved, 0x0A                                   */4 s% B0 R5 a2 S. p, R
  13.   __IO uint16_t OAR2;       /*!< I2C Own address register 2, Address offset: 0x0C */8 g$ d* T4 e8 t; [5 u
  14.   uint16_t      RESERVED3;  /*!< Reserved, 0x0E                                   */
    6 T! f1 _, |4 e! l- ?
  15.   __IO uint16_t DR;         /*!< I2C Data register,          Address offset: 0x10 */
      V3 y. i. ?6 r  d  _8 V( {/ ?7 t# m
  16.   uint16_t      RESERVED4;  /*!< Reserved, 0x12                                   */
    ( r$ O; ?- ^. ]+ ?7 u$ I
  17.   __IO uint16_t SR1;        /*!< I2C Status register 1,      Address offset: 0x14 */
    - z  [9 D. v6 N4 E' T2 q  i
  18.   uint16_t      RESERVED5;  /*!< Reserved, 0x16                                   */
    / L! x& R; T) }% y1 [. w# u
  19.   __IO uint16_t SR2;        /*!< I2C Status register 2,      Address offset: 0x18 */
    5 E$ o  Y: z5 @. t, ?! ]
  20.   uint16_t      RESERVED6;  /*!< Reserved, 0x1A                                   */3 I: }8 n9 `; B, ~$ J" B+ f
  21.   __IO uint16_t CCR;        /*!< I2C Clock control register, Address offset: 0x1C */0 n7 B3 E- L: h7 [. m( s
  22.   uint16_t      RESERVED7;  /*!< Reserved, 0x1E                                   */. h; `, s( T2 {0 ^- ^6 X& M/ g
  23.   __IO uint16_t TRISE;      /*!< I2C TRISE register,         Address offset: 0x20 */
    & u; `' T9 Q/ i% {
  24.   uint16_t      RESERVED8;  /*!< Reserved, 0x22                                   */( K4 o7 f6 S7 Q8 ]- O
  25.   __IO uint16_t FLTR;       /*!< I2C FLTR register,          Address offset: 0x24 */4 A. t2 y3 I1 ]2 y+ w  J' Z
  26.   uint16_t      RESERVED9;  /*!< Reserved, 0x26                                   */
    $ M# B: e: h+ g' A/ E5 ]
  27. } I2C_TypeDef;
复制代码

1 I( I# E* h/ r; d1 Q+ D8 T: l% [% Y8 ]4 u8 Z# u
% s, t) J* J: ~2 w

' h0 [0 }. B' ^! t
赞 收藏 评论0 发布时间:2022-6-16 17:00

举报

0个回答

所属标签

相似技术帖

官网相关资源

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