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

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

[复制链接]
STMCU小助手 发布时间:2022-3-28 22:00
01. I2C简介
. [4 [$ X' z" yI2C(内部集成电路)总线接口用作微控制器和 I2C 串行总线之间的接口。它提供多主模式功能,可以控制所有I2C总线特定的序列、协议、仲裁和时序。它支持标准和快速模式。它还与 SMBus 2.0 兼容。1 X5 Z. B/ h# y- z. K0 n/ s

" e0 \6 V# W0 V# H它可以用于多种用途,包括 CRC 生成和验证、SMBus(系统管理总线)以及 PMBus(电源管理总线)。
1 [; `. L3 G, n! A3 D7 j, T& t8 r, S" u
根据器件的不同,可利用 DMA 功能来减轻 CPU 的工作量。  h( ^& ^1 d, E* l$ X5 u1 d
$ T! e% s! F8 j* D
02. 相关类型: O2 A& ~/ A) c
I2C Init structure
- O% d; }* N6 _- ~7 T) W. l
1 d) `1 @2 |# n; R! q' X5 a- r
  1. /**
    % x) x/ S4 l' R6 z) j; z, y; j" E" U/ g
  2.   * @brief  I2C Init structure definition  8 f  u5 R$ a" @; C9 S) Y
  3.   */
    * [6 v" ], l' V8 ~9 ^- U& j

  4. - a* a: `$ m* f: k+ H' O- m0 e. k. o
  5. typedef struct; u# w* v. e/ ^' \) n
  6. {
    ; W# w  K0 x" E& j6 L( L) T
  7.   uint32_t I2C_ClockSpeed;          /*!< Specifies the clock frequency.0 S; X8 y0 C) ?6 b9 P; x7 o
  8.                                          This parameter must be set to a value lower than 400kHz */
    . \+ I9 c  G* Y4 w
  9.   Z6 q0 p* l9 y! U2 n3 y
  10.   uint16_t I2C_Mode;                /*!< Specifies the I2C mode.6 B# p0 d2 V0 |! _; r% i% d
  11.                                          This parameter can be a value of @ref I2C_mode */# \3 Z6 ^- ^2 G+ r

  12. 2 n1 C) G) l- W# j
  13.   uint16_t I2C_DutyCycle;           /*!< Specifies the I2C fast mode duty cycle.
    6 D/ x# k( A3 h- B7 `# u- }
  14.                                          This parameter can be a value of @ref I2C_duty_cycle_in_fast_mode */1 y0 H: \5 Z) x6 L
  15. % z9 }5 g# Z; O$ m7 m& Y- z
  16.   uint16_t I2C_OwnAddress1;         /*!< Specifies the first device own address.. ]6 D$ t: k4 o' ~8 u9 q3 Z
  17.                                          This parameter can be a 7-bit or 10-bit address. */
    % z* X. Q/ B% G7 R0 s" z

  18. - c" W6 v+ T7 V/ Q! ~, M# m! K
  19.   uint16_t I2C_Ack;                 /*!< Enables or disables the acknowledgement.
    * b+ Q2 l. M6 ~4 A( q
  20.                                          This parameter can be a value of @ref I2C_acknowledgement */
    8 a4 i% r, N# }/ z' w. v
  21. 8 ~3 H' F( P: q) ?4 p. o
  22.   uint16_t I2C_AcknowledgedAddress; /*!< Specifies if 7-bit or 10-bit address is acknowledged., {: G" j9 g9 X" H5 Y
  23.                                          This parameter can be a value of @ref I2C_acknowledged_address */- `- F% C1 \6 V3 c5 Q/ L6 X
  24. }I2C_InitTypeDef;
复制代码
, x- D4 A0 c: I8 T: T. G
I2C_Exported_Constants
. _. s. m" u& k5 b
  1. /** @defgroup I2C_Exported_Constants
    , O' `1 D' e4 ^" e
  2.   * @{0 r) y0 z6 g+ ?2 R  k
  3.   */" D- O* _  M( A
  4. #define IS_I2C_ALL_PERIPH(PERIPH) (((PERIPH) == I2C1) || \
    # u, V3 I0 g2 ^" P) R2 v  u2 d
  5.                                    ((PERIPH) == I2C2) || \
    % l. g$ T7 Z$ Q/ O  d6 b9 R# y
  6.                                    ((PERIPH) == I2C3))
    9 o* \8 H% Y+ t+ d3 C7 h$ R. A. H4 W
  7. /** @defgroup I2C_Digital_Filter  a0 I' W" a7 v9 h
  8.   * @{, R1 {% ^& ]1 {+ f8 T7 A( ]; @
  9.   */
    6 }7 `# L: s% q3 K- \/ i

  10. " {! R. O6 ]6 t! R' j
  11. #define IS_I2C_DIGITAL_FILTER(FILTER)   ((FILTER) <= 0x0000000F)8 n, R2 P7 R& E" [* z1 u
  12. /**9 t( x* H2 q5 u6 ?* r
  13.   * @}0 A3 A4 D. i$ e
  14.   */
复制代码

4 C  u* C! H5 sI2C_mode6 P- j! B; S- r* T3 W" r  e
  1. /** @defgroup I2C_mode ( N2 J/ J9 m1 T1 `, }
  2.   * @{% z* f: Z# K9 l1 N
  3.   */  c8 a9 H5 [* b+ X, S3 g

  4. 4 ?) @9 W4 U, Z
  5. #define I2C_Mode_I2C                    ((uint16_t)0x0000)% j6 w  o' I! P! b( {8 M6 j
  6. #define I2C_Mode_SMBusDevice            ((uint16_t)0x0002)  ! r; j3 s3 R! B" y9 D7 }
  7. #define I2C_Mode_SMBusHost              ((uint16_t)0x000A)+ g8 R" r! Q2 _9 a( J  t
  8. #define IS_I2C_MODE(MODE) (((MODE) == I2C_Mode_I2C) || \; H/ o9 N+ ~, q  i6 L
  9.                            ((MODE) == I2C_Mode_SMBusDevice) || \1 `% @+ r/ O, \8 I: ]- e
  10.                            ((MODE) == I2C_Mode_SMBusHost))
复制代码

9 [/ E( R6 r3 T' F9 }I2C_duty_cycle_in_fast_mode
  1. /** @defgroup I2C_duty_cycle_in_fast_mode ) C: F+ ~0 q3 M' _+ ?
  2.   * @{
    * e2 _$ h: l2 y- H7 z8 L, a
  3.   */1 v/ r  N' \' E) j" n/ \

  4. : j: ?: n$ N; C& K
  5. #define I2C_DutyCycle_16_9              ((uint16_t)0x4000) /*!< I2C fast mode Tlow/Thigh = 16/9 */
    0 g, r7 Y# a* t( N  O
  6. #define I2C_DutyCycle_2                 ((uint16_t)0xBFFF) /*!< I2C fast mode Tlow/Thigh = 2 */
    6 V0 i- z0 w- R+ U2 i; j5 ^* l3 F
  7. #define IS_I2C_DUTY_CYCLE(CYCLE) (((CYCLE) == I2C_DutyCycle_16_9) || \# M6 e, p3 k9 P; r9 i  v1 p/ v0 c
  8.                                   ((CYCLE) == I2C_DutyCycle_2))
复制代码
& P0 e+ d" l7 v$ g+ S
I2C_acknowledgement
" b4 }$ A! V& l6 t( o
  1. $ I$ O$ h) U% o5 l: o$ l
  2. /** @defgroup I2C_acknowledgement
    8 C( n+ v0 W- `1 D
  3.   * @{$ H6 ~9 L( Y+ K+ X
  4.   */8 G8 g. b; g; b7 u5 K7 {

  5. , F$ g6 I# w& r
  6. #define I2C_Ack_Enable                  ((uint16_t)0x0400)  q% G& p1 J8 u- C' c2 v' R
  7. #define I2C_Ack_Disable                 ((uint16_t)0x0000)5 q/ h# {3 `5 E4 [4 J/ y/ t
  8. #define IS_I2C_ACK_STATE(STATE) (((STATE) == I2C_Ack_Enable) || \+ U; c/ u! H7 M) ~
  9.                                  ((STATE) == I2C_Ack_Disable))0 e2 c* q( x! ]$ _
复制代码
4 p9 b& e) `8 \0 S( d
I2C_transfer_direction
6 I5 D. F4 o5 X# i3 u
  1. /** @defgroup I2C_transfer_direction ! U, Z8 G# D1 k" |1 N
  2.   * @{
    7 i! v  \; p9 z. I0 t0 m3 @1 F
  3.   */5 ~) Z3 j( M3 v0 s
  4. - \* ?! C6 U8 O% n& n
  5. #define  I2C_Direction_Transmitter      ((uint8_t)0x00)
    # J2 t! d  t, [1 S/ g7 `
  6. #define  I2C_Direction_Receiver         ((uint8_t)0x01)% R* F3 b7 K$ F2 q4 J# C, C' X/ u
  7. #define IS_I2C_DIRECTION(DIRECTION) (((DIRECTION) == I2C_Direction_Transmitter) || \* a6 i; V, y7 Y1 U" Q( r
  8.                                      ((DIRECTION) == I2C_Direction_Receiver))
复制代码

* }8 b9 R) c; XI2C_acknowledged_address
  1. /** @defgroup I2C_acknowledged_address
    * ?) f* N! P& h. z8 a/ V
  2.   * @{
    3 [+ o4 L; O: @$ x5 p
  3.   */
    3 g+ \4 |( _" Z+ P/ o' x
  4. ) ^6 {5 U. n6 C7 n2 \. w. j
  5. #define I2C_AcknowledgedAddress_7bit    ((uint16_t)0x4000)
    7 c* T9 Q% V) c' t! f4 c  b; S
  6. #define I2C_AcknowledgedAddress_10bit   ((uint16_t)0xC000)
    0 a2 t; h$ S, }6 z! \
  7. #define IS_I2C_ACKNOWLEDGE_ADDRESS(ADDRESS) (((ADDRESS) == I2C_AcknowledgedAddress_7bit) || \) w& r$ {) s0 _" q
  8.                                              ((ADDRESS) == I2C_AcknowledgedAddress_10bit))
复制代码

, u4 B, {( {# QI2C_registers
1 t  X  ?! \$ R7 ?7 T
( j; p* P4 f" m! _
  1. /** @defgroup I2C_registers
    : Y) C# z+ d: l2 r8 C
  2.   * @{
    0 m# o* x+ c" H) v
  3.   */' u; b( Y/ [7 b% ~  L9 _7 x: l! x8 u
  4. % c; F! B* B* q+ q
  5. #define I2C_Register_CR1                ((uint8_t)0x00)
    9 q& s* ?( g4 H9 Z  {" \
  6. #define I2C_Register_CR2                ((uint8_t)0x04)5 J( z3 `, l8 ?+ c) d
  7. #define I2C_Register_OAR1               ((uint8_t)0x08)7 W, N* }7 m7 I. D! v7 r& A
  8. #define I2C_Register_OAR2               ((uint8_t)0x0C)
    : w0 R+ `7 c+ e! H9 _2 R: x
  9. #define I2C_Register_DR                 ((uint8_t)0x10)
    & l( T3 j7 X) {3 v
  10. #define I2C_Register_SR1                ((uint8_t)0x14)
    / B$ z/ K4 |6 ~( I
  11. #define I2C_Register_SR2                ((uint8_t)0x18)
    & P2 }8 t8 M: y5 K) O
  12. #define I2C_Register_CCR                ((uint8_t)0x1C)4 S6 l5 t4 Z2 c5 Y- v& u2 E; h
  13. #define I2C_Register_TRISE              ((uint8_t)0x20)
    - K6 J2 D) F9 |+ A
  14. #define IS_I2C_REGISTER(REGISTER) (((REGISTER) == I2C_Register_CR1) || \; |3 Y5 p0 ?4 k" ~" `2 k
  15.                                    ((REGISTER) == I2C_Register_CR2) || \
    ' f' S8 f3 _, V$ s
  16.                                    ((REGISTER) == I2C_Register_OAR1) || \
    5 O$ X2 a$ w5 x
  17.                                    ((REGISTER) == I2C_Register_OAR2) || \
    ! r0 B- Y1 D5 m  f
  18.                                    ((REGISTER) == I2C_Register_DR) || \, u% C) H0 h" L/ W4 U
  19.                                    ((REGISTER) == I2C_Register_SR1) || \
    ( A) _" Y* \0 P
  20.                                    ((REGISTER) == I2C_Register_SR2) || \& S* X' e# b- a1 a( V0 }7 b( q3 Y
  21.                                    ((REGISTER) == I2C_Register_CCR) || \
    " s( L0 T/ U2 D4 h/ R7 U
  22.                                    ((REGISTER) == I2C_Register_TRISE))
复制代码
% F; {6 G. p- A7 A4 r
I2C_NACK_position% N* g% z5 {  g$ I- d' N) U
  1. /** @defgroup I2C_NACK_position 3 J& {$ A; o8 p# h/ h
  2.   * @{
    ' a5 t  v0 p; H* `7 A; J$ w$ Q
  3.   */
    + V4 e$ f+ m9 Q( T: F7 ^* x
  4. 9 b9 |. J5 S" R% j% t
  5. #define I2C_NACKPosition_Next           ((uint16_t)0x0800), F/ ]" z& o  h2 H7 w3 @2 J
  6. #define I2C_NACKPosition_Current        ((uint16_t)0xF7FF)
    2 j; a" _1 M3 u2 L
  7. #define IS_I2C_NACK_POSITION(POSITION)  (((POSITION) == I2C_NACKPosition_Next) || \
    , H+ J/ A- s. t$ |0 [5 D
  8.                                          ((POSITION) == I2C_NACKPosition_Current))
复制代码
/ |) g* e" f6 c/ G8 @
I2C_SMBus_alert_pin_level
7 k% q3 r8 h% ^. n

7 `0 ?% @5 _/ q7 O0 g
  1. /** @defgroup I2C_SMBus_alert_pin_level
    9 S! L6 d3 S* L, X
  2.   * @{4 Z! I# f8 e" k$ w. D8 _, a% V
  3.   */9 v) [7 q  ^! L: a
  4. * f# \- E" |7 m4 a
  5. #define I2C_SMBusAlert_Low              ((uint16_t)0x2000): x  H, t9 \! |7 z
  6. #define I2C_SMBusAlert_High             ((uint16_t)0xDFFF)
    ) Y# ?3 V8 [  S& j7 E
  7. #define IS_I2C_SMBUS_ALERT(ALERT) (((ALERT) == I2C_SMBusAlert_Low) || \2 H7 M! [9 g0 K4 h3 U, }
  8.                                    ((ALERT) == I2C_SMBusAlert_High))6 G6 z& c3 C+ e, U6 c
复制代码
) t; u7 l6 O. O$ r+ H" o
I2C_PEC_position- W0 Q9 f+ v. p- f! e( f

; W' K$ D# ], ~6 P# w5 r+ T
  1. /** @defgroup I2C_PEC_position
    % a/ ]" w/ ^; ?+ M
  2.   * @{6 H- \/ k# s9 \# Y* W+ V
  3.   */
    4 U' y9 e* _  i9 k! _

  4. 9 G# f) ^3 L" q/ i8 P: z7 D
  5. #define I2C_PECPosition_Next            ((uint16_t)0x0800)
    8 y& n( w3 t+ D4 K* G/ b- r9 f
  6. #define I2C_PECPosition_Current         ((uint16_t)0xF7FF). W: T+ ]1 T* ^6 N. g
  7. #define IS_I2C_PEC_POSITION(POSITION) (((POSITION) == I2C_PECPosition_Next) || \
    ! _/ c5 ?; B/ n' c3 @3 n
  8.                                        ((POSITION) == I2C_PECPosition_Current))
复制代码
: e; `* p( v3 x1 f
I2C_interrupts_definition6 _9 x: _0 g) B& e8 i8 C1 m% Y1 d

! w6 W$ @3 Z& c: {/ g- Z
  1. /** @defgroup I2C_interrupts_definition * U6 y0 U; k5 `( a6 \
  2.   * @{5 @& g0 ^( S) e( N. q' k% G' p
  3.   */
    : L- l: e8 y# n% i

  4. - g5 w  Z0 R# `; O. A
  5. #define I2C_IT_BUF                      ((uint16_t)0x0400)# `! U2 Q/ h+ ^( T/ h
  6. #define I2C_IT_EVT                      ((uint16_t)0x0200)
    " D" j/ L1 l% o6 `3 F" z
  7. #define I2C_IT_ERR                      ((uint16_t)0x0100)" u1 P1 w- }  ^9 l6 r& Q
  8. #define IS_I2C_CONFIG_IT(IT) ((((IT) & (uint16_t)0xF8FF) == 0x00) && ((IT) != 0x00))
    5 W5 F& ?& F/ r3 K& S# s8 T
复制代码

2 U# B! W0 w5 m! m+ bI2C_interrupts_definition
: M+ s3 A, ~+ I2 v, N: l; c  c
/ B7 I' N8 x! q' n# u
  1. /** @defgroup I2C_interrupts_definition ) A! z& E5 W- _- k
  2.   * @{
    & p: ?8 G3 E5 ~! _7 i8 m1 x8 R' `
  3.   */
    . O' I% G  N$ {% `

  4. 2 P* N6 ]( u8 h1 `0 a2 C9 h8 |2 t
  5. #define I2C_IT_SMBALERT                 ((uint32_t)0x01008000), J5 X( ^. t# c7 s6 P- V2 m5 ?  o
  6. #define I2C_IT_TIMEOUT                  ((uint32_t)0x01004000)' P) k3 A# g; O
  7. #define I2C_IT_PECERR                   ((uint32_t)0x01001000)
    ! w4 P  ^1 K0 _; z' N# Y
  8. #define I2C_IT_OVR                      ((uint32_t)0x01000800)
    " T( q5 C5 M/ W0 u  W. p2 T! e
  9. #define I2C_IT_AF                       ((uint32_t)0x01000400)
    ! a! k' m; X* Z0 x& a8 F% A" n
  10. #define I2C_IT_ARLO                     ((uint32_t)0x01000200)1 }  f/ }* r" r) v2 _, [
  11. #define I2C_IT_BERR                     ((uint32_t)0x01000100)
    0 j: ~' W" i, ?" A8 J5 D5 o
  12. #define I2C_IT_TXE                      ((uint32_t)0x06000080)
    7 K& N, W, [7 a- R1 b2 l
  13. #define I2C_IT_RXNE                     ((uint32_t)0x06000040)
    ( ]2 s6 g6 v" Z9 I" X
  14. #define I2C_IT_STOPF                    ((uint32_t)0x02000010)
    . `6 T* F3 N; D- l3 e* m- U
  15. #define I2C_IT_ADD10                    ((uint32_t)0x02000008)- o2 X, P/ S1 T) Q
  16. #define I2C_IT_BTF                      ((uint32_t)0x02000004)0 a/ C" }4 N9 I; X9 R; X
  17. #define I2C_IT_ADDR                     ((uint32_t)0x02000002)
    , V& d  V) ]7 o( b# M
  18. #define I2C_IT_SB                       ((uint32_t)0x02000001)
    8 v; i: y6 R. c+ u$ t/ b! E# G) b

  19. 3 [! S+ }3 @( v1 S  l4 A  U
  20. #define IS_I2C_CLEAR_IT(IT) ((((IT) & (uint16_t)0x20FF) == 0x00) && ((IT) != (uint16_t)0x00))2 e% l: ^( p7 \  b

  21. 9 G, _# Q) ?5 c( [: C
  22. #define IS_I2C_GET_IT(IT) (((IT) == I2C_IT_SMBALERT) || ((IT) == I2C_IT_TIMEOUT) || \6 Y3 J8 H9 A# @, m1 X& [
  23.                            ((IT) == I2C_IT_PECERR) || ((IT) == I2C_IT_OVR) || \
    3 j8 }* J1 ^8 j
  24.                            ((IT) == I2C_IT_AF) || ((IT) == I2C_IT_ARLO) || \
      N4 n4 s. W8 C4 C# L8 n
  25.                            ((IT) == I2C_IT_BERR) || ((IT) == I2C_IT_TXE) || \9 z% s# z. n0 G- r9 D( R2 t
  26.                            ((IT) == I2C_IT_RXNE) || ((IT) == I2C_IT_STOPF) || \) j) L* p$ R  z! h8 C& E
  27.                            ((IT) == I2C_IT_ADD10) || ((IT) == I2C_IT_BTF) || \2 G% q6 g2 f7 Y. E  d" B. r
  28.                            ((IT) == I2C_IT_ADDR) || ((IT) == I2C_IT_SB))
    ; w; n9 [$ U8 Y

  29. % j4 h# Y% W% L4 E  l) Z" Q3 r- |
复制代码
5 Q: ]& ^8 H! h8 s9 ^% X
SR2 register flags
  ^# G" U5 d/ I0 S  E+ Q9 \5 G% |; |3 W' c# C
  1. /**
    , F2 I. p' h* s4 X1 Q. H& |
  2.   * @brief  SR2 register flags  
    - s0 b% b; G2 e# u- @% a
  3.   */
    9 J0 y* z1 x5 J0 u9 Z
  4. + k5 Z' \; U+ m( J$ w
  5. #define I2C_FLAG_DUALF                  ((uint32_t)0x00800000). {4 p3 \% n9 d: }& a! q# H
  6. #define I2C_FLAG_SMBHOST                ((uint32_t)0x00400000)
    % N( c, ?, A( T
  7. #define I2C_FLAG_SMBDEFAULT             ((uint32_t)0x00200000)# Z6 O7 y. M/ \4 Z
  8. #define I2C_FLAG_GENCALL                ((uint32_t)0x00100000)
    ( h1 _7 |: E- Q" q+ s! N1 p
  9. #define I2C_FLAG_TRA                    ((uint32_t)0x00040000)4 c  S/ B5 |  Q/ |# ^
  10. #define I2C_FLAG_BUSY                   ((uint32_t)0x00020000)
    / J) z8 m1 I, }5 u5 s# D( b
  11. #define I2C_FLAG_MSL                    ((uint32_t)0x00010000)
复制代码

( |6 Z7 S7 n4 E4 ?6 FSR1 register flags
2 m! f; f$ o  @" m. c" W' }! |- ~
  1. /**
    ! Q0 U+ o; _$ t, m7 z$ c
  2.   * @brief  SR1 register flags  
    * F1 n- c6 V. {4 J" S% x+ G' j! H6 ?0 U
  3.   */
    6 r6 U" Q4 Z' R1 F# w

  4. * v3 N% u1 o, ~
  5. #define I2C_FLAG_SMBALERT               ((uint32_t)0x10008000)
    , w3 F/ h' \, p$ e0 G
  6. #define I2C_FLAG_TIMEOUT                ((uint32_t)0x10004000)1 W) _0 A( W! b# M7 y
  7. #define I2C_FLAG_PECERR                 ((uint32_t)0x10001000)# S% E7 l! U3 K& X
  8. #define I2C_FLAG_OVR                    ((uint32_t)0x10000800)
    8 }2 ?( i3 L: J5 v3 v* x
  9. #define I2C_FLAG_AF                     ((uint32_t)0x10000400)* l3 C% B$ @3 l4 I
  10. #define I2C_FLAG_ARLO                   ((uint32_t)0x10000200)9 K- p0 K+ R' A/ t! q* T7 a4 W- ^
  11. #define I2C_FLAG_BERR                   ((uint32_t)0x10000100)9 d* ~9 |& r, ~' n& Z
  12. #define I2C_FLAG_TXE                    ((uint32_t)0x10000080)
    $ P8 ]+ F. A. o9 J& F4 T0 h
  13. #define I2C_FLAG_RXNE                   ((uint32_t)0x10000040)6 E6 p( N7 F1 {" ?; c
  14. #define I2C_FLAG_STOPF                  ((uint32_t)0x10000010)
    / x$ Y* g: L4 X
  15. #define I2C_FLAG_ADD10                  ((uint32_t)0x10000008)
    2 L. \1 o- f$ E) z
  16. #define I2C_FLAG_BTF                    ((uint32_t)0x10000004)
    & a6 {. q) a+ R# O+ [7 G$ [8 R6 E) L$ F
  17. #define I2C_FLAG_ADDR                   ((uint32_t)0x10000002); I& `. \8 o, }% h7 v3 U
  18. #define I2C_FLAG_SB                     ((uint32_t)0x10000001)
    : \/ W/ _5 C4 }. r7 f: [: B; C
  19. & [+ _9 y( o" Q6 U$ }7 _! C
  20. #define IS_I2C_CLEAR_FLAG(FLAG) ((((FLAG) & (uint16_t)0x20FF) == 0x00) && ((FLAG) != (uint16_t)0x00))) s4 U9 u: ]; x, K! G5 Q

  21. . l8 Y: p3 I) g  o8 x
  22. #define IS_I2C_GET_FLAG(FLAG) (((FLAG) == I2C_FLAG_DUALF) || ((FLAG) == I2C_FLAG_SMBHOST) || \  c+ B$ C: }7 \! O( x9 U7 c% ~0 {
  23.                                ((FLAG) == I2C_FLAG_SMBDEFAULT) || ((FLAG) == I2C_FLAG_GENCALL) || \8 \8 p  x, {6 k2 Q2 M
  24.                                ((FLAG) == I2C_FLAG_TRA) || ((FLAG) == I2C_FLAG_BUSY) || \  N- S# C* f7 L% R
  25.                                ((FLAG) == I2C_FLAG_MSL) || ((FLAG) == I2C_FLAG_SMBALERT) || \
    ' j( s" ?! m9 [4 o
  26.                                ((FLAG) == I2C_FLAG_TIMEOUT) || ((FLAG) == I2C_FLAG_PECERR) || \
    . K/ S2 d6 h1 ^) ~
  27.                                ((FLAG) == I2C_FLAG_OVR) || ((FLAG) == I2C_FLAG_AF) || \5 \  d1 A$ G2 j! `
  28.                                ((FLAG) == I2C_FLAG_ARLO) || ((FLAG) == I2C_FLAG_BERR) || \
    # {8 I0 T+ ^2 `1 z% F8 u2 u! n
  29.                                ((FLAG) == I2C_FLAG_TXE) || ((FLAG) == I2C_FLAG_RXNE) || \
    4 ~) @" Y! A/ y* p
  30.                                ((FLAG) == I2C_FLAG_STOPF) || ((FLAG) == I2C_FLAG_ADD10) || \" D9 R& |- s  I
  31.                                ((FLAG) == I2C_FLAG_BTF) || ((FLAG) == I2C_FLAG_ADDR) || \. G7 E& X2 M3 n$ l4 U0 {& ?
  32.                                ((FLAG) == I2C_FLAG_SB))
复制代码

) q0 M& V# Y' u3 r% T03. 相关函数) {7 S' L: Q( I+ B. g) i
  1. /*  Function used to set the I2C configuration to the default reset state *****/- X7 T( D; d/ v4 W/ \
  2. void I2C_DeInit(I2C_TypeDef* I2Cx);
    / Z! D1 E. S  m' b: U

  3. * k4 F6 K8 g  q( G" L( T  V# @* H
  4. /* Initialization and Configuration functions *********************************/
    # ?* C* y, @7 Y  M
  5. void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct);
    / L' K' @3 B8 I2 D- ~
  6. void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct);
    6 o5 e8 o+ E( ~
  7. void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState);% W5 N. S5 @% i" B0 A
  8. void I2C_DigitalFilterConfig(I2C_TypeDef* I2Cx, uint16_t I2C_DigitalFilter);4 n2 _( r2 e" J' m* w+ S2 K# I# N# }. X
  9. void I2C_AnalogFilterCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
    / m! v7 x1 Z" J- Z1 {  M9 \
  10. void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState);
    % O; g9 a6 z: p1 N
  11. void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState);
    & D! A$ v: u0 l9 {3 ?$ {$ Z) q3 B% K$ l
  12. void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, uint8_t I2C_Direction);
    2 w) n! P# G3 n! \$ ~
  13. void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState);7 `, B( G- c' j! M+ ~. X4 k8 R3 ^" i
  14. void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint8_t Address);7 p4 u' U* ^/ O6 D1 o, W- ^- @
  15. void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
    3 w( p) e+ i, b6 Z$ v# j
  16. void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
    . G# y9 g5 A* d2 B* [
  17. void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);0 B% R5 p2 ?9 ^6 m
  18. void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);# Y- y4 v6 V( C( q& q" |3 a/ j
  19. void I2C_FastModeDutyCycleConfig(I2C_TypeDef* I2Cx, uint16_t I2C_DutyCycle);3 ?: t! g2 Y8 A) l0 J2 z3 F0 V
  20. void I2C_NACKPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_NACKPosition);
    6 m9 [. j/ k/ e/ y+ s
  21. void I2C_SMBusAlertConfig(I2C_TypeDef* I2Cx, uint16_t I2C_SMBusAlert);
    % n- W( X3 q/ B" l& g! v
  22. void I2C_ARPCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);+ i* U' h# V  P' C+ Y( f
  23. ( j# d$ h/ A4 t* ~
  24. /* Data transfers functions ***************************************************/ 4 U! l3 u' U( p- c6 O" ]
  25. void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data);
    0 E5 l- ?) |6 E( L
  26. uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx);4 E" U/ Q9 \, g* m' F
  27. " R9 s+ z( I5 i* Y% |
  28. /* PEC management functions ***************************************************/
    $ @! F5 _+ L; j
  29. void I2C_TransmitPEC(I2C_TypeDef* I2Cx, FunctionalState NewState);- G7 |' H" q( z. e2 @6 X
  30. void I2C_PECPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_PECPosition);2 Z% ?# T) u2 Q8 _3 r. r  p
  31. void I2C_CalculatePEC(I2C_TypeDef* I2Cx, FunctionalState NewState);
    9 C0 h; \- \7 l
  32. uint8_t I2C_GetPEC(I2C_TypeDef* I2Cx);
    1 }1 x9 U2 ]$ v1 F3 S7 U% e  F

  33. 6 k% E" h8 d" b( X' R" J
  34. /* DMA transfers management functions *****************************************/' \5 s- r, G: }9 v2 G7 F3 d
  35. void I2C_DMACmd(I2C_TypeDef* I2Cx, FunctionalState NewState);: w- h( ~3 u" C: p
  36. void I2C_DMALastTransferCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
    / @$ I/ n/ k+ n9 l+ {8 v

  37. # ^) s. ?$ f5 B+ Z: Z
  38. /* Interrupts, events and flags management functions **************************/$ H; O* y: e% u. @# _* a+ P/ g+ a
  39. uint16_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register);
    , a4 Y7 b9 Y8 z: {( s
  40. void I2C_ITConfig(I2C_TypeDef* I2Cx, uint16_t I2C_IT, FunctionalState NewState);
    & J/ |8 f- e6 e0 r5 S! ^

  41. , ?& L: b+ W7 r" L1 [
  42. /*
    1 p9 {" J1 Y8 t
  43. ===============================================================================
    0 T4 w# o7 z% t- M3 j
  44.                           I2C State Monitoring Functions1 O4 [4 o. _1 j0 ?" z  v
  45. ===============================================================================- g) e, O2 g: ^; V& d
  46.   This I2C driver provides three different ways for I2C state monitoring
    ! ?4 U1 ~! ^4 V
  47.   depending on the application requirements and constraints:' }3 I% u5 K8 u: t& W2 r

  48. # @4 t" K$ b# q6 c+ ]
  49.   I5 w( ^7 i0 D/ q; u/ q% o/ b
  50.      1. Basic state monitoring (Using I2C_CheckEvent() function)
    : N9 }; J. |! k2 o# G( W$ V, C- H
  51.      -----------------------------------------------------------
    7 P1 `& k$ }3 Q' Y: ?9 \
  52.         It compares the status registers (SR1 and SR2) content to a given event
    0 u! W) V$ I9 t7 e6 b; \& y  o. Q1 ?
  53.         (can be the combination of one or more flags).4 v) w6 x4 e8 `9 _- Q
  54.         It returns SUCCESS if the current status includes the given flags 3 P" y! K$ h; ]' x- f# r/ q# n
  55.         and returns ERROR if one or more flags are missing in the current status.$ X6 a  [' R4 y0 r! D, v$ U
  56. + b( k  `- S3 ~* A
  57.           - When to use1 t+ V# `; Q# F/ S- d
  58.              - This function is suitable for most applications as well as for startup . p' Z# t. x* S- v( {
  59.                activity since the events are fully described in the product reference
    6 p: d" ~( w( b, K, B3 ]
  60.                manual (RM0090).
    " C; ^8 X: A$ j3 c+ p
  61.              - It is also suitable for users who need to define their own events.8 S6 J3 ], Y+ \  p; X3 C" G- l# R
  62. " v  D) D$ Y' g4 X' a1 k# B& Y
  63.           - Limitations+ v) U% ]8 o/ @# A" I6 S( c8 K
  64.              - If an error occurs (ie. error flags are set besides to the monitored 1 L5 j( P1 `9 A# D# }; Y! w8 P
  65.                flags), the I2C_CheckEvent() function may return SUCCESS despite
    - n! O& |5 E, P( v/ V1 @/ C
  66.                the communication hold or corrupted real state. 2 g) Z7 Z9 a; O9 v9 r( Z
  67.                In this case, it is advised to use error interrupts to monitor   S. p  `: Y) G6 \3 ?, h% u3 A( |
  68.                the error events and handle them in the interrupt IRQ handler.
    / I8 _9 Y4 E, I

  69. # M' B; w5 b% W' ^3 w  [( V
  70.      Note
    8 M3 o- }4 B0 ?" p: D
  71.          For error management, it is advised to use the following functions:
    : h% `( }3 w$ C1 w+ E! a
  72.            - I2C_ITConfig() to configure and enable the error interrupts (I2C_IT_ERR).
    * c1 d! T- \$ v+ k
  73.            - I2Cx_ER_IRQHandler() which is called when the error interrupt occurs.* y0 I. Y/ m: ~* Y% Q5 {' l
  74.              Where x is the peripheral instance (I2C1, I2C2 ...)' X; A& J8 Q5 D& d
  75.            - I2C_GetFlagStatus() or I2C_GetITStatus()  to be called into the
    ) E# w  u/ q) K6 V! v; {
  76.              I2Cx_ER_IRQHandler() function in order to determine which error occurred.
    0 C3 w3 D8 n: ], l: e
  77.            - I2C_ClearFlag() or I2C_ClearITPendingBit() and/or I2C_SoftwareResetCmd()
    0 X6 v0 ~) f$ R9 N  C
  78.              and/or I2C_GenerateStop() in order to clear the error flag and source
    . }0 U& X6 F" |; r6 C
  79.              and return to correct  communication status.
    $ s4 `7 s  P  V
  80. & ]- ?3 M1 O5 C
  81. / y3 ~! w* I/ S; u/ U# a
  82.      2. Advanced state monitoring (Using the function I2C_GetLastEvent())
    % B. B( M& X- O
  83.      -------------------------------------------------------------------- ; C% F7 y0 W' j4 ]
  84.         Using the function I2C_GetLastEvent() which returns the image of both status
    : Q9 Q0 h! m) h( U7 Y) |6 ^
  85.         registers in a single word (uint32_t) (Status Register 2 value is shifted left
    $ j1 b. s4 O" \# b/ n
  86.         by 16 bits and concatenated to Status Register 1).
    % C' j- K5 H' e8 D5 |
  87. 4 Y& V1 F& G% I! D
  88.           - When to use/ [+ d$ ]2 s! ?/ F
  89.              - This function is suitable for the same applications above but it
    " m- M; E6 c2 z8 p9 Q
  90.                allows to overcome the mentioned limitation of I2C_GetFlagStatus()
    8 ^8 B; v  I+ u8 v. h3 R8 P5 V
  91.                function.
    0 I! x! Q0 s7 a, D1 o
  92.              - The returned value could be compared to events already defined in 9 `  n6 @2 k3 }% ?# }/ i
  93.                this file or to custom values defined by user.
    / \- N1 Y8 `' N  t  }! S2 \6 P5 o! [
  94.                This function is suitable when multiple flags are monitored at the
    ( V+ z1 Y" x  m3 D7 x% ^! M7 n
  95.                same time.
    5 E4 o* k1 h& s0 j
  96.              - At the opposite of I2C_CheckEvent() function, this function allows
    ' d5 ~/ v+ ^4 f
  97.                user to choose when an event is accepted (when all events flags are % W) k/ y; h# A% Z& X
  98.                set and no other flags are set or just when the needed flags are set
    5 |4 @% E( u& `: e3 B4 Z7 y' x) \+ ~
  99.                like I2C_CheckEvent() function.6 J, p. G# R7 P, E" u* E

  100. - j9 _2 U" P" j3 i
  101.           - Limitations" e0 o* f& L/ A6 O$ \
  102.              - User may need to define his own events.
    : N$ K) b; i; ?6 K& F0 \+ b' @
  103.              - Same remark concerning the error management is applicable for this 7 N$ ]; n* c& ?( S
  104.                function if user decides to check only regular communication flags ; E: x$ c( G! [: ^$ k+ a, ?
  105.                (and ignores error flags).
    * v4 W1 E% s& m
  106. 9 m: m. d, z8 K) P* M- [
  107. - |  A# d  U9 h0 u* @
  108.      3. Flag-based state monitoring (Using the function I2C_GetFlagStatus())
    0 }! `  a* T0 h7 _
  109.      -----------------------------------------------------------------------, _$ l5 y8 G# V; h1 S* l! x% x

  110. 9 [" T+ q" ^0 ]' I) r# R3 X
  111.       Using the function I2C_GetFlagStatus() which simply returns the status of
      Z8 K( a4 w' }4 z4 @+ X0 s
  112.       one single flag (ie. I2C_FLAG_RXNE ...).
    9 d0 M( ~4 e$ m) j; c  K4 ]* V
  113. 5 \$ N; c1 H. H, Z* g/ \9 N
  114.           - When to use) w* G8 B1 H7 Y
  115.              - This function could be used for specific applications or in debug * i" S# e5 V1 R
  116.                phase.' t6 e6 F0 A7 p9 t! p' ]7 z; u$ o+ H
  117.              - It is suitable when only one flag checking is needed (most I2C " l( P: t0 A  K% k
  118.                events are monitored through multiple flags).# N4 Q9 ^' u& v5 K: i' Q
  119.           - Limitations: ; d6 O! R) ^$ r7 s5 i1 p7 i
  120.              - When calling this function, the Status register is accessed.
    + B6 I5 E5 e, G
  121.                Some flags are cleared when the status register is accessed.
    ( S( ?$ D" x+ [: d9 N7 h! A
  122.                So checking the status of one Flag, may clear other ones.4 h5 B. r) j% h
  123.              - Function may need to be called twice or more in order to monitor ) K# `/ }. J: o0 M' O+ J
  124.                one single event.           
    $ u$ P# b/ u; Z( w# E" D
  125. */
    , y2 O9 s- a  C
  126. * i2 a9 w, X0 R/ o+ r
  127. /*
    ; U' U9 D- d6 V! h
  128. ===============================================================================4 C5 H# x( }0 x  d, }
  129.                           1. Basic state monitoring
    2 Y  s, a7 x: S) U
  130. ===============================================================================
    - o# d# s$ i1 ~
  131. */
    1 [$ u* Y/ e8 C
  132. ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT);
    , z: c% `0 z/ ?: n
  133. /*& V0 |& |( q8 \8 D' w/ n
  134. ===============================================================================
    6 x/ K5 U* _( s! U) }2 k2 X
  135.                           2. Advanced state monitoring
    * y7 \( x$ S7 q: l
  136. ===============================================================================
      t/ h6 D. @, Z6 ~- L1 C; ^! f
  137. */
    " g* \9 J+ w4 a1 B' N9 ?
  138. uint32_t I2C_GetLastEvent(I2C_TypeDef* I2Cx);3 M( w/ J) u+ |; e; ]* x
  139. /*
    - T( {/ X$ {' N/ S' A7 q
  140. ===============================================================================
    ' _# c: }# B% N) U
  141.                           3. Flag-based state monitoring
    ; V7 \( c# x5 u
  142. ===============================================================================
    * h5 Z% C+ [* |0 h4 U/ K, O
  143. */
    $ M" Y1 ^+ H7 Y# s) e1 c
  144. FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);
    ' C& ]3 N% i/ W+ ~! A$ D' }

  145. ( T" ]7 Q3 i+ ^: C) {

  146. % G  O* W- n/ L  p1 n  F
  147. void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);9 F9 z6 X# A8 _9 S
  148. ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT);
    ) ^) L* u8 D$ |/ i* L
  149. void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT);
    * N  a/ O$ f7 O, K- B% V
复制代码

* m: o6 r5 i2 H, f- I04. 结构体封装
$ j* S  d' j2 |; j& B
  1. /**
    5 n; }8 w* V7 z5 M
  2.   * @brief Inter-integrated Circuit Interface
    + q4 O1 T4 u/ O/ I
  3.   */
    3 a# Y8 @) T- Z9 |7 q! E6 N! Q

  4.   y( G4 O/ t; B  r" ]  t
  5. typedef struct0 g- {, ?8 D8 d5 o, M
  6. {& ]$ V7 h# ~5 ~& F# Z& l$ ^
  7.   __IO uint16_t CR1;        /*!< I2C Control register 1,     Address offset: 0x00 */$ A4 z- A, l+ c
  8.   uint16_t      RESERVED0;  /*!< Reserved, 0x02                                   */8 \0 T- A- A- j: K, S. W
  9.   __IO uint16_t CR2;        /*!< I2C Control register 2,     Address offset: 0x04 */4 w; v. a- e, m# w( L8 _0 l8 ^
  10.   uint16_t      RESERVED1;  /*!< Reserved, 0x06                                   */
    6 D# T" ?4 ]5 t/ x) E9 R
  11.   __IO uint16_t OAR1;       /*!< I2C Own address register 1, Address offset: 0x08 */
    % R- b1 y5 Y% r7 a0 r
  12.   uint16_t      RESERVED2;  /*!< Reserved, 0x0A                                   */, [& G# ?% r! O; E
  13.   __IO uint16_t OAR2;       /*!< I2C Own address register 2, Address offset: 0x0C */
    % v$ c  t5 l! D1 I
  14.   uint16_t      RESERVED3;  /*!< Reserved, 0x0E                                   */+ I; B- `9 t% k* C: \
  15.   __IO uint16_t DR;         /*!< I2C Data register,          Address offset: 0x10 */: j4 v. M& u; g1 T* D9 _
  16.   uint16_t      RESERVED4;  /*!< Reserved, 0x12                                   */8 b  y! p0 |) i, |+ Z3 D% K
  17.   __IO uint16_t SR1;        /*!< I2C Status register 1,      Address offset: 0x14 */
    4 E( e; U7 d# a+ |* S1 a
  18.   uint16_t      RESERVED5;  /*!< Reserved, 0x16                                   */
    ' [3 A9 n; v* L: c: u
  19.   __IO uint16_t SR2;        /*!< I2C Status register 2,      Address offset: 0x18 */7 v! h* J! w( @4 L6 D
  20.   uint16_t      RESERVED6;  /*!< Reserved, 0x1A                                   */
    ! w0 p% M% L  I7 x* l1 f
  21.   __IO uint16_t CCR;        /*!< I2C Clock control register, Address offset: 0x1C */" r) U+ W! M, p3 S5 l
  22.   uint16_t      RESERVED7;  /*!< Reserved, 0x1E                                   */
    / I. x9 l$ f, Y9 d7 G- }* |
  23.   __IO uint16_t TRISE;      /*!< I2C TRISE register,         Address offset: 0x20 */
    + l5 u1 y" w$ |. S, R
  24.   uint16_t      RESERVED8;  /*!< Reserved, 0x22                                   */
    5 q8 j3 }+ ^) |# s/ Q+ W
  25.   __IO uint16_t FLTR;       /*!< I2C FLTR register,          Address offset: 0x24 */
    + g" ^) b+ z* t  I/ X& E
  26.   uint16_t      RESERVED9;  /*!< Reserved, 0x26                                   */1 z' J+ V% j& w* x( A* a% b
  27. } I2C_TypeDef;
复制代码

" ?; q/ }& [3 E/ t  v3 R1 M" p3 c% U! ~9 M3 U
* r! M: q4 k+ U6 s

$ T2 ]$ |, X  I  a
赞 收藏 评论0 发布时间:2022-3-28 22:00

举报

0个回答

所属标签

相似技术帖

官网相关资源

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