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

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

[复制链接]
STMCU小助手 发布时间:2022-6-16 17:00
01. I2C简介1 g, J! |$ R% V% Y- t
I2C(内部集成电路)总线接口用作微控制器和 I2C 串行总线之间的接口。它提供多主模式功能,可以控制所有I2C总线特定的序列、协议、仲裁和时序。它支持标准和快速模式。它还与 SMBus 2.0 兼容。
# z0 I( o3 R& ]" v9 H8 W2 p3 T" S5 m
它可以用于多种用途,包括 CRC 生成和验证、SMBus(系统管理总线)以及 PMBus(电源管理总线)。
. ]+ t1 O4 ~$ L( W1 X; y, z) R% V0 F
+ t# s3 P, W6 S4 n: C: Q, g根据器件的不同,可利用 DMA 功能来减轻 CPU 的工作量。- K0 a1 {) X, L  Q* [7 C

* `8 ]: T$ R3 e/ Z02. 相关类型
, J( N0 J8 {$ }! N) UI2C Init structure
. _' t& z" k1 |- t4 l6 l( y. m) |$ X& q6 j) T, s3 y# P+ N3 z$ ]$ e2 i
  1. /**
    8 b2 C/ n; ?# E
  2.   * @brief  I2C Init structure definition  
    & A* u, s& w! _+ p, O5 i
  3.   */
      u& L9 o: z" ~

  4. ' i( j8 @% }2 l, [1 t
  5. typedef struct( B4 U( `& E! a! B
  6. {
    ) I- l; B/ I( ^& w6 c
  7.   uint32_t I2C_ClockSpeed;          /*!< Specifies the clock frequency.: n# {+ N5 |; g" T
  8.                                          This parameter must be set to a value lower than 400kHz */
    9 z! X. t, }/ R. [) ?

  9. ) `0 F7 W( C! P7 C/ J0 Y
  10.   uint16_t I2C_Mode;                /*!< Specifies the I2C mode.
    # y5 ^. @- M2 ]9 \  z( C3 j
  11.                                          This parameter can be a value of @ref I2C_mode */( E* K  T/ r  q/ V. {' r# o

  12. $ e* K, v1 h/ c+ u- s% l
  13.   uint16_t I2C_DutyCycle;           /*!< Specifies the I2C fast mode duty cycle.1 `5 w  ?% R7 [  u; K2 Y1 e3 v
  14.                                          This parameter can be a value of @ref I2C_duty_cycle_in_fast_mode */
    ! y' W9 A7 n3 E* o- [* Z# v9 {

  15. ' _& C% M& X4 \) d
  16.   uint16_t I2C_OwnAddress1;         /*!< Specifies the first device own address.3 p5 m4 y4 H- y* k& e
  17.                                          This parameter can be a 7-bit or 10-bit address. */
    ) D* j" M) O6 c  _. o

  18. ; ^2 [. x. X1 R: ^
  19.   uint16_t I2C_Ack;                 /*!< Enables or disables the acknowledgement.( W( c4 i, U. o# A" s) o5 u
  20.                                          This parameter can be a value of @ref I2C_acknowledgement */" e# X9 j# j. V" L# A$ g
  21. " L4 U" [7 s3 x$ O  e
  22.   uint16_t I2C_AcknowledgedAddress; /*!< Specifies if 7-bit or 10-bit address is acknowledged.% T( z* E9 ?( Z+ b" l) R" M! i
  23.                                          This parameter can be a value of @ref I2C_acknowledged_address */
    7 B# [" Z( k8 ?& [3 S
  24. }I2C_InitTypeDef;
复制代码

5 I' ~3 `1 O) ?$ @9 V1 C7 \I2C_Exported_Constants7 X9 W, A5 U: H
+ ^5 p# {: n3 b
  1. 3 |2 [0 |! l0 l7 ?2 H
  2. /** @defgroup I2C_Exported_Constants
    - s* Z- K( J( g! V. N
  3.   * @{
      R" _2 F% G- ^  o+ `9 Z
  4.   */" i6 a/ h' o9 F& ^$ G" i
  5. #define IS_I2C_ALL_PERIPH(PERIPH) (((PERIPH) == I2C1) || \- d. M) B3 u5 k$ M; x" j
  6.                                    ((PERIPH) == I2C2) || \2 s6 P5 x! p# j# _$ P- c2 T( B
  7.                                    ((PERIPH) == I2C3))& a  G! y6 I. g! s% o: L* a
  8. /** @defgroup I2C_Digital_Filter
    2 s: L: J; F5 f2 D. {
  9.   * @{
    $ J/ I  [! L" y
  10.   */
    4 T5 }4 \  Z9 [9 ^, U: h

  11. ! p5 E, K0 F' W( p6 g* R
  12. #define IS_I2C_DIGITAL_FILTER(FILTER)   ((FILTER) <= 0x0000000F)4 Q: B( ?  a+ Y1 Z
  13. /**. R0 c5 r, e# {) J
  14.   * @}+ P  A& L2 {- o; u% k
  15.   */
复制代码

: B3 j/ N) ^( \: v/ C5 }  q8 w' HI2C_mode
  1. /** @defgroup I2C_mode ' R0 I7 c- V0 _* M4 @& L7 N' Q' x
  2.   * @{4 E9 P1 y2 q, }3 K8 C- F
  3.   */+ L* f* ^0 {( H9 L9 O1 u2 ~
  4. * x3 D6 Q) _( S3 ^# i0 D, M
  5. #define I2C_Mode_I2C                    ((uint16_t)0x0000)
    4 c3 r, \1 p8 C4 e  I- s6 G6 v
  6. #define I2C_Mode_SMBusDevice            ((uint16_t)0x0002)  
    * X  ^" w% U1 J6 z( K+ n
  7. #define I2C_Mode_SMBusHost              ((uint16_t)0x000A)9 `0 n1 r3 ]2 b. p2 q5 y, }' a) R
  8. #define IS_I2C_MODE(MODE) (((MODE) == I2C_Mode_I2C) || \
    & `- _- ?9 P/ }
  9.                            ((MODE) == I2C_Mode_SMBusDevice) || \& t  b) W2 p$ ~( H( H2 q  T
  10.                            ((MODE) == I2C_Mode_SMBusHost))
复制代码
( K2 a( Q: y; O4 H8 b* k0 }4 O4 B
I2C_duty_cycle_in_fast_mode
( `9 _) {8 k! A$ h4 Y& d' y% ]* i7 [6 F: i* f7 A" j' I
  1. /** @defgroup I2C_duty_cycle_in_fast_mode
    3 q/ l+ J4 x4 z1 }0 ?
  2.   * @{& ]" S% f7 ?7 Q8 I8 Y! w. A) w
  3.   */
    & @/ q9 J; F- k4 T

  4. $ R7 Y, s9 @7 w" [' X
  5. #define I2C_DutyCycle_16_9              ((uint16_t)0x4000) /*!< I2C fast mode Tlow/Thigh = 16/9 */: U, p+ u: r9 m  U6 V7 X
  6. #define I2C_DutyCycle_2                 ((uint16_t)0xBFFF) /*!< I2C fast mode Tlow/Thigh = 2 */# _/ `% S( f2 _  |( {. v1 d$ v
  7. #define IS_I2C_DUTY_CYCLE(CYCLE) (((CYCLE) == I2C_DutyCycle_16_9) || \; {2 K6 V) L( S
  8.                                   ((CYCLE) == I2C_DutyCycle_2))
复制代码

- v* A- k+ @7 w" D. HI2C_acknowledgement( ^! i) E# S0 L  b5 B  ?/ U

7 o7 J2 @- ^: U, H9 C, ^
  1. /** @defgroup I2C_acknowledgement
    ( r1 ?" a0 d8 e/ w. c
  2.   * @{
    2 |. w# H2 K3 ?
  3.   */
    5 ^- Q' q8 B/ n& Q  Q

  4. 2 x& X9 w6 m2 o9 d- @1 k4 v- |4 x" y
  5. #define I2C_Ack_Enable                  ((uint16_t)0x0400)
    ; b+ f( {6 U& c
  6. #define I2C_Ack_Disable                 ((uint16_t)0x0000)
    5 o/ d( h4 d3 b
  7. #define IS_I2C_ACK_STATE(STATE) (((STATE) == I2C_Ack_Enable) || \. o/ K! O+ l/ f; F, {4 D" E) A5 @2 P
  8.                                  ((STATE) == I2C_Ack_Disable))
复制代码
8 A+ R/ d: R8 Y9 A
I2C_transfer_direction
5 a; C: g) {) x: u0 S% I  M% K
+ t* B* v4 Q0 k: o7 `7 J/ O9 T, e4 Z
  1. /** @defgroup I2C_transfer_direction
    " P& ]( {: p3 p: r& W
  2.   * @{0 S, [& H6 o3 e& f. @3 t
  3.   */
    2 x0 H2 a* N/ l4 R

  4. 0 e  N" h# R4 r1 R! M1 I/ k- f7 n
  5. #define  I2C_Direction_Transmitter      ((uint8_t)0x00)
    : Y2 n' i6 r$ n, f* {9 O/ i* |5 E
  6. #define  I2C_Direction_Receiver         ((uint8_t)0x01)
    ! ?5 m) A8 K" c! ^
  7. #define IS_I2C_DIRECTION(DIRECTION) (((DIRECTION) == I2C_Direction_Transmitter) || \
    3 E0 W: ~3 a& ]& ^4 F1 O& _
  8.                                      ((DIRECTION) == I2C_Direction_Receiver))
复制代码

3 |" [, f$ J. `2 L# N0 B% kI2C_acknowledged_address
5 a& o) C) a- E  |: x+ k; f3 P8 H5 y0 F* P
  1. /** @defgroup I2C_acknowledged_address
    9 i2 z& L1 O! ?4 C4 r8 @) l
  2.   * @{
    - H, Z1 g# r8 _- L
  3.   */
    / s& \2 A. [; V5 g( w5 H

  4. / P; f  J5 s6 U9 U
  5. #define I2C_AcknowledgedAddress_7bit    ((uint16_t)0x4000); _, J  W2 k( |1 e! n
  6. #define I2C_AcknowledgedAddress_10bit   ((uint16_t)0xC000)' n6 e1 t1 s  }8 i
  7. #define IS_I2C_ACKNOWLEDGE_ADDRESS(ADDRESS) (((ADDRESS) == I2C_AcknowledgedAddress_7bit) || \! w: w+ z3 W: E! O" O5 K' w
  8.                                              ((ADDRESS) == I2C_AcknowledgedAddress_10bit))
复制代码
, e7 d( Z1 Z: M/ `
I2C_registers* \- u# e" j, b( C

, y7 h( t1 o3 C/ v
  1. /** @defgroup I2C_registers 1 a) G% j0 ^8 y, ~  a" M
  2.   * @{5 u0 T% v7 l6 ^: `3 M
  3.   */2 P$ b: Y1 q1 e/ a: ]& I

  4. ) s+ [3 A  u; V! x/ T) q
  5. #define I2C_Register_CR1                ((uint8_t)0x00)
    5 j2 e  P6 G, p
  6. #define I2C_Register_CR2                ((uint8_t)0x04)
    ! ]+ r& ]0 [& V: t  ^
  7. #define I2C_Register_OAR1               ((uint8_t)0x08)
    * v2 {0 f7 O6 x3 {' U/ F. {6 Q
  8. #define I2C_Register_OAR2               ((uint8_t)0x0C)/ v- L0 r* R: J
  9. #define I2C_Register_DR                 ((uint8_t)0x10)
    9 [# d% x, z" Y# V3 F
  10. #define I2C_Register_SR1                ((uint8_t)0x14)
    ' H; D7 K" U# b2 B" i1 B5 i
  11. #define I2C_Register_SR2                ((uint8_t)0x18)
    " I4 k- @1 {/ T5 f/ Q) }5 T
  12. #define I2C_Register_CCR                ((uint8_t)0x1C)5 v0 ?1 U( f7 N6 H9 M
  13. #define I2C_Register_TRISE              ((uint8_t)0x20)
    7 m9 @1 M8 x1 }5 T' H) }/ }
  14. #define IS_I2C_REGISTER(REGISTER) (((REGISTER) == I2C_Register_CR1) || \
    7 e8 e- J8 y# z+ Y* x6 M
  15.                                    ((REGISTER) == I2C_Register_CR2) || \5 f% C+ r  z3 O3 j# a( r5 F
  16.                                    ((REGISTER) == I2C_Register_OAR1) || \% d" a( a8 e4 w$ P; y( Q7 c
  17.                                    ((REGISTER) == I2C_Register_OAR2) || \! j! P$ a3 \+ |) t" ]% G# ~
  18.                                    ((REGISTER) == I2C_Register_DR) || \; n, `; G8 d% ]
  19.                                    ((REGISTER) == I2C_Register_SR1) || \: O% y. D1 E/ T: T
  20.                                    ((REGISTER) == I2C_Register_SR2) || \
    4 ]) ]6 B5 w; Y2 q; q% u
  21.                                    ((REGISTER) == I2C_Register_CCR) || \0 r, h9 ^) c8 b: C
  22.                                    ((REGISTER) == I2C_Register_TRISE))
复制代码
8 S2 G6 \' A, n1 G
I2C_NACK_position
+ p7 d" R' h  Z* Z4 F  F
, M# a/ z& t4 s2 |" E
  1. /** @defgroup I2C_NACK_position : J& Y+ c; |' ~, |
  2.   * @{
    $ f7 L1 r( s5 z: A& h- h: r
  3.   */: q  {7 b0 n+ g! F; w% {
  4. 9 e) o! q1 \3 Z1 ~4 X
  5. #define I2C_NACKPosition_Next           ((uint16_t)0x0800)
    ; I) q  f: O5 g
  6. #define I2C_NACKPosition_Current        ((uint16_t)0xF7FF)
    , F1 }8 E- N  [
  7. #define IS_I2C_NACK_POSITION(POSITION)  (((POSITION) == I2C_NACKPosition_Next) || \1 F" E! n0 B* {- S; S
  8.                                          ((POSITION) == I2C_NACKPosition_Current))
复制代码
6 l3 L' J9 I/ p0 h4 h* m' W
I2C_SMBus_alert_pin_level
% k) a( g+ B# a( z1 K( T( k: m9 l: X' b4 w0 `
  1. /** @defgroup I2C_SMBus_alert_pin_level 2 B# t3 h: v+ c2 D/ V+ n1 B
  2.   * @{# j! f2 q  R) y! g8 P
  3.   */
    5 Y9 m- C2 z$ @. u
  4. # I& u, Q; b4 G
  5. #define I2C_SMBusAlert_Low              ((uint16_t)0x2000)
    & z- P, ^: I* N, u" C% ]# ]8 p
  6. #define I2C_SMBusAlert_High             ((uint16_t)0xDFFF). |$ u( K- d# S9 r; Y
  7. #define IS_I2C_SMBUS_ALERT(ALERT) (((ALERT) == I2C_SMBusAlert_Low) || \
    8 U  q& x# t) x% f  n
  8.                                    ((ALERT) == I2C_SMBusAlert_High))
复制代码
: r: U: a) X0 Q! F! l! x. P
I2C_PEC_position
) {" T9 w3 i: H" V; i7 t' X! M4 I" X9 ]$ N8 @6 U. m9 N
  1. /** @defgroup I2C_PEC_position
    ) q& h* d% l/ o
  2.   * @{0 R) T) a4 L3 V, D% p9 P
  3.   */
    ; P, m9 Z" |4 f0 l5 R& E) N

  4. : V: d8 n* t1 }
  5. #define I2C_PECPosition_Next            ((uint16_t)0x0800)
    * e/ d" n5 P& H0 a) R
  6. #define I2C_PECPosition_Current         ((uint16_t)0xF7FF)) r& I0 c* h* v4 s! M% J. \
  7. #define IS_I2C_PEC_POSITION(POSITION) (((POSITION) == I2C_PECPosition_Next) || \
    6 \- T/ a) r+ x, l; Y
  8.                                        ((POSITION) == I2C_PECPosition_Current))
复制代码

/ x. {, s, V$ n9 C1 t% AI2C_interrupts_definition
. p& o6 _0 t8 J" M/ s, K
3 d$ i2 u5 t; d6 ~' @
  1. /** @defgroup I2C_interrupts_definition ; r% u+ I  M: t% B+ k( v, w- M8 w3 J4 i  S
  2.   * @{5 Z( H8 [2 Q" T8 X- X
  3.   */
    $ g. M/ F/ Y9 S. y6 c7 O
  4. . @! T# Q5 h& [; k3 F4 @7 Q
  5. #define I2C_IT_BUF                      ((uint16_t)0x0400)
    % Y$ S+ w2 a3 c/ h
  6. #define I2C_IT_EVT                      ((uint16_t)0x0200)) @2 x2 N: a$ O6 P# v, p
  7. #define I2C_IT_ERR                      ((uint16_t)0x0100)( x5 T: V: `" ^: r: g# t
  8. #define IS_I2C_CONFIG_IT(IT) ((((IT) & (uint16_t)0xF8FF) == 0x00) && ((IT) != 0x00))
复制代码

+ `8 K  x" j- e+ B# q" H* u  q$ }I2C_interrupts_definition
, Y' ^# a+ v; G; O6 R# u- x* |& b" r* K0 x. U* c) ]
  1. /** @defgroup I2C_interrupts_definition ) I  a5 H$ S* e4 t" [
  2.   * @{- v% f5 E1 X% J+ q# C5 p# [' w
  3.   *// Q5 H7 q1 ?- s" C
  4. % X3 x1 K' f3 J( e/ \3 ~+ h! O& F
  5. #define I2C_IT_SMBALERT                 ((uint32_t)0x01008000)4 d# O' i4 t& d+ M; g! l* B0 c: P
  6. #define I2C_IT_TIMEOUT                  ((uint32_t)0x01004000)
    ) [4 @; N" M; t2 Z8 R; J( d4 Y
  7. #define I2C_IT_PECERR                   ((uint32_t)0x01001000)" L  N8 Z9 ^3 ^6 }0 v  w! D
  8. #define I2C_IT_OVR                      ((uint32_t)0x01000800)
    0 s1 l( q" O9 g: A
  9. #define I2C_IT_AF                       ((uint32_t)0x01000400)
    8 P9 t# U3 K% y- c' R. g! i
  10. #define I2C_IT_ARLO                     ((uint32_t)0x01000200)
    $ v1 B) Y5 U  S) B' R' _6 Z
  11. #define I2C_IT_BERR                     ((uint32_t)0x01000100)4 `- _  v9 X) d" w! I
  12. #define I2C_IT_TXE                      ((uint32_t)0x06000080)
    - }  w- ]1 x: A& {% J2 f" r! s
  13. #define I2C_IT_RXNE                     ((uint32_t)0x06000040)
    ) {7 s& ?9 k) _$ G' a
  14. #define I2C_IT_STOPF                    ((uint32_t)0x02000010)/ K  r6 u; s1 w/ F9 ~- u* U
  15. #define I2C_IT_ADD10                    ((uint32_t)0x02000008)
    - A3 Z/ M- ~. f7 b! [7 {  s
  16. #define I2C_IT_BTF                      ((uint32_t)0x02000004)
    5 T! s* N# n' s. L; Z- Q' g+ t
  17. #define I2C_IT_ADDR                     ((uint32_t)0x02000002); i2 y9 K/ s1 F" e' _
  18. #define I2C_IT_SB                       ((uint32_t)0x02000001)/ F! ~# ]0 s- Y" ~

  19. + @, v6 ?( f7 A. a, U# |3 L
  20. #define IS_I2C_CLEAR_IT(IT) ((((IT) & (uint16_t)0x20FF) == 0x00) && ((IT) != (uint16_t)0x00))
    8 s8 }4 K. R  A5 _

  21. 5 D. I) d6 h5 x0 ]9 i: e* G
  22. #define IS_I2C_GET_IT(IT) (((IT) == I2C_IT_SMBALERT) || ((IT) == I2C_IT_TIMEOUT) || \* s8 ]8 M" V" _4 v
  23.                            ((IT) == I2C_IT_PECERR) || ((IT) == I2C_IT_OVR) || \
    # I9 Y  W) R1 a
  24.                            ((IT) == I2C_IT_AF) || ((IT) == I2C_IT_ARLO) || \# j- v* k( n) b0 q* T; n4 ~
  25.                            ((IT) == I2C_IT_BERR) || ((IT) == I2C_IT_TXE) || \
    & @; ?, ~5 {# T
  26.                            ((IT) == I2C_IT_RXNE) || ((IT) == I2C_IT_STOPF) || \
    6 H+ E3 X! F! U
  27.                            ((IT) == I2C_IT_ADD10) || ((IT) == I2C_IT_BTF) || \
    $ X1 T! n0 N- J$ V- B  g
  28.                            ((IT) == I2C_IT_ADDR) || ((IT) == I2C_IT_SB))
复制代码
8 T% m0 n/ }# f6 r
SR2 register flags
, X- T7 R) W, N, {
  o% r( G0 T8 C3 `- p4 `5 z
  1. /** & o& @( _/ `7 g
  2.   * @brief  SR2 register flags  8 Q6 U+ x5 ~: X: G! r
  3.   */0 Z# ^( j+ m# B* ^, o

  4. ) ~$ ]6 d/ ^, U% }1 _1 y/ ^
  5. #define I2C_FLAG_DUALF                  ((uint32_t)0x00800000)
    " T3 U# j+ y* e+ }/ C
  6. #define I2C_FLAG_SMBHOST                ((uint32_t)0x00400000)2 A$ [/ O& Q4 n6 \0 ?
  7. #define I2C_FLAG_SMBDEFAULT             ((uint32_t)0x00200000)
    4 O: b: [0 |; c8 }: A
  8. #define I2C_FLAG_GENCALL                ((uint32_t)0x00100000)
    3 i% v- x  s9 L
  9. #define I2C_FLAG_TRA                    ((uint32_t)0x00040000)
    7 y. k6 l* F: g; T
  10. #define I2C_FLAG_BUSY                   ((uint32_t)0x00020000)
    2 W: Z# x" t4 S! k3 r
  11. #define I2C_FLAG_MSL                    ((uint32_t)0x00010000)
复制代码

" w% ~+ V9 A  U" j. Q! u/ k  C4 h. OSR1 register flags3 m( ]8 y  \+ y  ^

- x% F5 k! E# B7 N
  1. /** ! y/ k2 R$ |3 r% W8 i* x
  2.   * @brief  SR1 register flags  
    / g2 J, {* L$ R% X
  3.   */, b, r) Z0 q6 Q- P- f
  4. 7 c. ?( R, R, K6 ?
  5. #define I2C_FLAG_SMBALERT               ((uint32_t)0x10008000)
    * T( a" i0 o. Q- E' h
  6. #define I2C_FLAG_TIMEOUT                ((uint32_t)0x10004000)
    ) {7 q; z# k* X6 C+ k2 F9 g. A* q
  7. #define I2C_FLAG_PECERR                 ((uint32_t)0x10001000)
    , i1 c' [/ q* o6 J# M  e
  8. #define I2C_FLAG_OVR                    ((uint32_t)0x10000800)
    5 c( {) m8 R; L/ h" d1 _0 e3 W# F
  9. #define I2C_FLAG_AF                     ((uint32_t)0x10000400)" e4 c# P% p- b; y; b. m3 E% y
  10. #define I2C_FLAG_ARLO                   ((uint32_t)0x10000200)
    % S2 s# I/ X, b. R
  11. #define I2C_FLAG_BERR                   ((uint32_t)0x10000100)
    2 l. F7 b5 ^' I: [& W0 u/ ]2 X, @
  12. #define I2C_FLAG_TXE                    ((uint32_t)0x10000080)8 V8 b5 R% M2 D: |9 f; D" k+ r% Q- `
  13. #define I2C_FLAG_RXNE                   ((uint32_t)0x10000040)% l  t+ f  J( o% I
  14. #define I2C_FLAG_STOPF                  ((uint32_t)0x10000010)
    . M5 X! J. q/ j$ s# _7 v4 _1 O
  15. #define I2C_FLAG_ADD10                  ((uint32_t)0x10000008)
    3 s% {/ m) R! ?1 [$ t% d
  16. #define I2C_FLAG_BTF                    ((uint32_t)0x10000004)# n! |3 M" e4 Y0 P) N% t4 A& }
  17. #define I2C_FLAG_ADDR                   ((uint32_t)0x10000002)
    , G8 `. \5 ?' m& L
  18. #define I2C_FLAG_SB                     ((uint32_t)0x10000001)
    5 t/ y$ I" W# r$ a' }; Q" Q- y
  19. : C/ t$ Z  f* ^' G4 G' d' @
  20. #define IS_I2C_CLEAR_FLAG(FLAG) ((((FLAG) & (uint16_t)0x20FF) == 0x00) && ((FLAG) != (uint16_t)0x00))- N% w# r( c+ Z% v( ]+ r" c

  21. 6 l* r7 B1 Y& \8 R  |1 b9 d. }9 V
  22. #define IS_I2C_GET_FLAG(FLAG) (((FLAG) == I2C_FLAG_DUALF) || ((FLAG) == I2C_FLAG_SMBHOST) || \
    ! {2 V3 N  A+ ~' n
  23.                                ((FLAG) == I2C_FLAG_SMBDEFAULT) || ((FLAG) == I2C_FLAG_GENCALL) || \
    ! Z/ P9 Z( t; d8 L$ Y9 B
  24.                                ((FLAG) == I2C_FLAG_TRA) || ((FLAG) == I2C_FLAG_BUSY) || \; q7 x- V! j" z- b. P
  25.                                ((FLAG) == I2C_FLAG_MSL) || ((FLAG) == I2C_FLAG_SMBALERT) || \
    4 m  N* q7 z- W$ E. z" G6 r* K
  26.                                ((FLAG) == I2C_FLAG_TIMEOUT) || ((FLAG) == I2C_FLAG_PECERR) || \
    / ^" P/ b% [: Q( b
  27.                                ((FLAG) == I2C_FLAG_OVR) || ((FLAG) == I2C_FLAG_AF) || \3 r* J+ s9 K1 y4 L% p
  28.                                ((FLAG) == I2C_FLAG_ARLO) || ((FLAG) == I2C_FLAG_BERR) || \7 Z5 r+ E$ d6 ~
  29.                                ((FLAG) == I2C_FLAG_TXE) || ((FLAG) == I2C_FLAG_RXNE) || \. e3 ?! b- o( n  W4 c4 U
  30.                                ((FLAG) == I2C_FLAG_STOPF) || ((FLAG) == I2C_FLAG_ADD10) || \
    ! t) {  O  c* G8 p
  31.                                ((FLAG) == I2C_FLAG_BTF) || ((FLAG) == I2C_FLAG_ADDR) || \' U5 P  A# a4 A; W  J$ h) J
  32.                                ((FLAG) == I2C_FLAG_SB))
复制代码

+ }! G& z0 b3 q$ i; p03. 相关函数
% V% v* s+ E: H+ Y0 _4 l3 ]
  1. /*  Function used to set the I2C configuration to the default reset state *****/
    6 [! Q* b1 _9 {, _
  2. void I2C_DeInit(I2C_TypeDef* I2Cx);
    3 K+ w- w- N+ ?9 J$ Y5 m# C

  3. 5 U5 q9 H5 \. i. q. p# y/ a
  4. /* Initialization and Configuration functions *********************************/, ^1 Z# _* ?, }, u( A1 N
  5. void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct);
    8 t0 Z5 D) ?9 t! V' u
  6. void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct);
    % I; S9 h& ^7 _8 d- M7 o
  7. void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState);. m  b4 w2 R6 a1 l7 u
  8. void I2C_DigitalFilterConfig(I2C_TypeDef* I2Cx, uint16_t I2C_DigitalFilter);8 G3 e; d, d4 A+ ~9 d6 A
  9. void I2C_AnalogFilterCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
    9 Z5 ]; D( a4 @2 }$ A2 F1 y8 `
  10. void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState);
    ! @7 @& G; c+ ?3 y! W! K
  11. void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState);7 \7 U+ Y* [" X
  12. void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, uint8_t I2C_Direction);
    & a" M2 y7 J6 c1 s! `
  13. void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState);3 c) @1 X( s$ o% U: B
  14. void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint8_t Address);* t! x* [1 @- Z0 U0 O( w/ i! w
  15. void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
    / e9 o2 u5 d% Q& D) z0 M/ @
  16. void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);( L2 o1 L( D2 ~" }1 B9 V
  17. void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);; U5 w$ k2 U* l. a
  18. void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
    1 |: u) H+ z; ~/ e- U3 \- C
  19. void I2C_FastModeDutyCycleConfig(I2C_TypeDef* I2Cx, uint16_t I2C_DutyCycle);/ h' ~/ n& H5 U/ c
  20. void I2C_NACKPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_NACKPosition);
    + u( P2 s* ]: J9 I* I; F
  21. void I2C_SMBusAlertConfig(I2C_TypeDef* I2Cx, uint16_t I2C_SMBusAlert);! h" P6 E- P% k; V
  22. void I2C_ARPCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);# T( A/ b/ u, j

  23. 6 n" e; _: P3 t! x# T+ v0 K
  24. /* Data transfers functions ***************************************************/
    % n* g3 }2 ?0 d( E1 Z; b
  25. void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data);
    2 Q0 f+ M/ m+ R
  26. uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx);
    # d5 A4 K% t, l9 g

  27. 3 O: m6 }& u* g, r3 O$ [+ L1 l
  28. /* PEC management functions ***************************************************/
    6 P/ X6 T$ D3 v+ A8 _$ Y: ]
  29. void I2C_TransmitPEC(I2C_TypeDef* I2Cx, FunctionalState NewState);$ v: B/ f6 @/ B" Y' X7 p+ y
  30. void I2C_PECPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_PECPosition);# @, T  m; O( A  g
  31. void I2C_CalculatePEC(I2C_TypeDef* I2Cx, FunctionalState NewState);
    " c) J& }1 I/ S8 G
  32. uint8_t I2C_GetPEC(I2C_TypeDef* I2Cx);
    5 X, g8 O  m" u/ b
  33. / `7 ]# D9 y8 W- b3 N
  34. /* DMA transfers management functions *****************************************/
    : P5 y5 m0 g& c0 m9 v
  35. void I2C_DMACmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
    ; n2 L) [5 Z- S6 Y( }
  36. void I2C_DMALastTransferCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);  m8 ~% |$ v) G; v: o0 b$ g
  37. 9 w% U! O9 k. |3 Q3 t3 G
  38. /* Interrupts, events and flags management functions **************************/
    / Z7 G# v) r% Z( W8 @" h+ @/ G
  39. uint16_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register);8 D$ I. K* S0 J. K* m
  40. void I2C_ITConfig(I2C_TypeDef* I2Cx, uint16_t I2C_IT, FunctionalState NewState);
    ! C. p& v# v+ y! S. f& O

  41. 0 l( [8 ]: q2 W; q7 O
  42. /*
    & {' Q7 Z# T; C% a! I" J/ t
  43. ===============================================================================3 u: G; l/ V$ m0 s# d% _/ r3 q
  44.                           I2C State Monitoring Functions0 x8 ]9 z4 _' c, I2 j" M8 D
  45. ===============================================================================6 u- J+ S0 A% E# k5 x
  46.   This I2C driver provides three different ways for I2C state monitoring
    / d9 i: ]: k) `
  47.   depending on the application requirements and constraints:& T, s+ |0 m& D# a% e; g
  48. + y: e2 S4 u. `5 s* V

  49. 2 D6 v/ A* u# x$ r3 ?" }
  50.      1. Basic state monitoring (Using I2C_CheckEvent() function)0 L, V3 @# S) s. i" s3 t+ ?
  51.      -----------------------------------------------------------+ s# U, s% Y& Z7 t: R
  52.         It compares the status registers (SR1 and SR2) content to a given event5 {+ n2 A$ j. Q
  53.         (can be the combination of one or more flags).
    5 k0 p2 J. z# a) H1 s2 e
  54.         It returns SUCCESS if the current status includes the given flags
    7 T( F, x( a6 ^% H) m. U
  55.         and returns ERROR if one or more flags are missing in the current status.. R2 y5 u- _( C

  56. % i2 e6 [8 \! e5 o; K
  57.           - When to use3 t# @0 h' C# \% t/ {0 }
  58.              - This function is suitable for most applications as well as for startup
    # E% F  }. G6 Q) J0 t
  59.                activity since the events are fully described in the product reference
    2 `% _6 U0 i- ?) }
  60.                manual (RM0090).  w7 t' `. \5 C8 v
  61.              - It is also suitable for users who need to define their own events.5 {* i* N( F- `& e* ^

  62. ( O& {2 _/ I7 H4 @( U  u
  63.           - Limitations
    # d  Y5 r# R  r) B2 H
  64.              - If an error occurs (ie. error flags are set besides to the monitored : C+ z" s- x+ C& a( r9 Z" ?
  65.                flags), the I2C_CheckEvent() function may return SUCCESS despite
    , p# E8 b. R5 _  x  S* Q, W
  66.                the communication hold or corrupted real state.
    9 [, t, R% L& x2 k; D
  67.                In this case, it is advised to use error interrupts to monitor 9 j( o" X3 ?6 e8 ~5 j) C( m" E
  68.                the error events and handle them in the interrupt IRQ handler.0 |* J( Y; I: M" k
  69. % t3 {6 {; E) Q: d
  70.      Note / k' x5 r7 L; f0 M+ U. L9 R
  71.          For error management, it is advised to use the following functions:- ^7 J* v/ ]( z8 n# I
  72.            - I2C_ITConfig() to configure and enable the error interrupts (I2C_IT_ERR).
    % a: V% v7 A* ], M! M
  73.            - I2Cx_ER_IRQHandler() which is called when the error interrupt occurs.# s/ T. B8 z+ K- n3 b) c7 _. \
  74.              Where x is the peripheral instance (I2C1, I2C2 ...)
    5 o, S! u& X+ z& ^  C! C
  75.            - I2C_GetFlagStatus() or I2C_GetITStatus()  to be called into the
    " h* j. B$ ?2 z# M% p! _
  76.              I2Cx_ER_IRQHandler() function in order to determine which error occurred.$ m$ M( `* j8 _/ S) n: p$ w
  77.            - I2C_ClearFlag() or I2C_ClearITPendingBit() and/or I2C_SoftwareResetCmd()
    / V5 ]8 P! A$ d
  78.              and/or I2C_GenerateStop() in order to clear the error flag and source
    5 Q8 X" ~4 O" P+ t/ p# o" K
  79.              and return to correct  communication status.* E% S$ i) `2 O  I3 w" M  {
  80. - B$ {" K" X- {4 i6 B
  81. ) }0 m) ]& D; c1 O  {/ g
  82.      2. Advanced state monitoring (Using the function I2C_GetLastEvent())
      ]5 H, a1 h. F( I; ?" l
  83.      --------------------------------------------------------------------
    ' M% v7 E0 ?9 \+ F+ T' n. w
  84.         Using the function I2C_GetLastEvent() which returns the image of both status
    - n. T6 k+ E/ |; \  M, H
  85.         registers in a single word (uint32_t) (Status Register 2 value is shifted left ) U- q, I' o' u" L3 k) ~" v
  86.         by 16 bits and concatenated to Status Register 1).
    $ F+ W& r/ u4 t$ B  _
  87. : i3 Q& {6 d6 q! c( S, i6 T
  88.           - When to use
    + Y6 y% b; U" i* W+ W5 `# A
  89.              - This function is suitable for the same applications above but it - I$ B2 l" y" {* h  B) x: p' W9 ?& I
  90.                allows to overcome the mentioned limitation of I2C_GetFlagStatus() # M; x# R* y( [) A$ g0 k% Y
  91.                function.! i8 ~6 B: {- Q- a- {
  92.              - The returned value could be compared to events already defined in
    ' r' Q: K1 e2 V. K/ m& R$ h  ?
  93.                this file or to custom values defined by user.+ h% O% \4 P, j2 n2 Q
  94.                This function is suitable when multiple flags are monitored at the 1 I; o) F3 L; ^8 E, M
  95.                same time.
    # w: Z$ l, R$ B
  96.              - At the opposite of I2C_CheckEvent() function, this function allows
    ) u+ ?$ u6 W! d, x
  97.                user to choose when an event is accepted (when all events flags are
    2 _8 k7 K4 z- H+ H. ]2 O
  98.                set and no other flags are set or just when the needed flags are set
    : _( O* m, s1 B& V3 J# y9 _7 A# t
  99.                like I2C_CheckEvent() function.0 K4 U* p+ F& H0 U" ~3 A4 X

  100. ( H. N+ L1 b- {7 H2 `% g
  101.           - Limitations
    0 A1 S) z; p5 I# q4 m' H- I8 Q
  102.              - User may need to define his own events.# \4 n& ?( z3 `3 u$ p9 ?
  103.              - Same remark concerning the error management is applicable for this
    1 ], x5 T1 Q  q  @& Z. h
  104.                function if user decides to check only regular communication flags " L3 A/ x  C1 P5 H) |# A6 }) A
  105.                (and ignores error flags).
    5 D  W# s* U% I) d( B% J  u
  106. 3 f0 T* ]; y* F/ G0 ], z
  107. & U; b/ {. J& R' i' e5 s% c- E9 U8 E
  108.      3. Flag-based state monitoring (Using the function I2C_GetFlagStatus())
    0 S3 u: N2 C. W5 q- ?4 o, F) R
  109.      -----------------------------------------------------------------------0 w% Q) y0 X( }1 d7 j/ z3 r$ }

  110. ( N1 ?. \, }8 S+ h; a' n" o/ K3 e
  111.       Using the function I2C_GetFlagStatus() which simply returns the status of
    4 O$ c- O) B* q* Q% F/ Q* [* T
  112.       one single flag (ie. I2C_FLAG_RXNE ...). 7 H# q9 ]- s% `# z- ?) t& n4 o

  113. 5 C7 _( z' \9 _3 Z, X6 [2 }
  114.           - When to use$ i$ S5 v! B; r
  115.              - This function could be used for specific applications or in debug
    % |) b/ M6 G, |' J! U2 q
  116.                phase.7 r$ `1 v- }4 [
  117.              - It is suitable when only one flag checking is needed (most I2C * [+ ]2 H# p1 l1 ]
  118.                events are monitored through multiple flags)." S' [: ^) C7 A" k- V
  119.           - Limitations: 8 f9 i" ~2 ]" E! t  \' ]% v4 O
  120.              - When calling this function, the Status register is accessed.
    6 H! x& o, x" ^) u
  121.                Some flags are cleared when the status register is accessed.
    0 j7 B2 A% _# d; ^# D+ ]7 [
  122.                So checking the status of one Flag, may clear other ones.
    9 D( O$ S7 F6 L( P$ N8 C. G; _
  123.              - Function may need to be called twice or more in order to monitor
    - i/ y3 L3 [7 b. G1 E( g2 J% v7 x. p
  124.                one single event.           
    4 Z, K8 i9 z# G1 p
  125. */
    4 L- @4 B' G6 `2 H9 E' D+ T; a, ]
  126. ( N) W3 ?2 I. [+ D& g
  127. /*
    6 Q" [. G5 b1 B! h6 h. q# j
  128. ===============================================================================2 T8 w1 A: y/ d9 E( j1 S
  129.                           1. Basic state monitoring
    % D) a' e) v! l/ u0 k; r5 X
  130. ===============================================================================$ |; V8 D9 D" F2 ~1 G7 @7 ^
  131. */( u& ]5 s; h4 }/ X# P+ H' U) d
  132. ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT);
    4 O" f8 [# m" g. T. x- ^' W+ Q
  133. /** Z" F4 s" k% m+ ^9 t. ^  |
  134. ===============================================================================$ Y( ?" i3 @- `! |% ]0 G
  135.                           2. Advanced state monitoring$ m  o8 W' j# g! H' I
  136. ===============================================================================
    - V) e7 d* l: t" G
  137. */  _1 T: }  j4 D
  138. uint32_t I2C_GetLastEvent(I2C_TypeDef* I2Cx);% Y& c! L' Q, h, Q3 Y0 l, M# a6 q# S
  139. /*
    % r. o3 _$ K( a2 f. `
  140. ===============================================================================
    % D9 _8 U1 [, ?$ K+ Q% v& C
  141.                           3. Flag-based state monitoring/ X1 G$ u/ f, F1 P- d
  142. ===============================================================================
    ; W+ q7 R7 k: w% C9 B) f
  143. */5 a" A% y9 J) O% N2 Z
  144. FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);2 A* H; v0 M# }. ~7 ]8 n% r! s2 \3 h
  145. 9 S# ~+ P* V! S1 s1 U' s' {
  146. 2 R0 I  b# c9 J/ m+ ]9 p
  147. void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);/ A6 h, S7 o+ C2 [
  148. ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT);
    * i4 w0 j7 k1 x) c. G
  149. void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT);
复制代码
4 q% ]9 a( P( X7 R- g7 a- U
04. 结构体封装) G1 u- j. S5 F
  1. /** ( M; \% c  V3 Q' b
  2.   * @brief Inter-integrated Circuit Interface  E1 g! y- w8 P# b+ o
  3.   */
    " |# Q9 }" P$ t7 e! E  |$ F" K3 t0 Y
  4. + D0 Y' w# z% d) J: ]
  5. typedef struct% T6 q3 u) z3 S: ]# ]0 P8 w
  6. {
    ) O/ x4 g* \8 z9 `
  7.   __IO uint16_t CR1;        /*!< I2C Control register 1,     Address offset: 0x00 */
    / r+ e( L* @( m$ ~$ x3 j9 A
  8.   uint16_t      RESERVED0;  /*!< Reserved, 0x02                                   */  `4 O0 l9 y2 t2 M4 J* X
  9.   __IO uint16_t CR2;        /*!< I2C Control register 2,     Address offset: 0x04 */& i: a% F+ q- w
  10.   uint16_t      RESERVED1;  /*!< Reserved, 0x06                                   */7 b2 K" p) o7 c) C% t
  11.   __IO uint16_t OAR1;       /*!< I2C Own address register 1, Address offset: 0x08 */
    7 t% p! D4 o/ U
  12.   uint16_t      RESERVED2;  /*!< Reserved, 0x0A                                   */
    2 t5 E+ ]! r& i6 ~0 p2 h
  13.   __IO uint16_t OAR2;       /*!< I2C Own address register 2, Address offset: 0x0C */* r" P+ c6 H8 I1 T8 l
  14.   uint16_t      RESERVED3;  /*!< Reserved, 0x0E                                   */
    ; q+ J- m3 o) V+ _
  15.   __IO uint16_t DR;         /*!< I2C Data register,          Address offset: 0x10 */: }4 ~" O. I) X' \5 p& s
  16.   uint16_t      RESERVED4;  /*!< Reserved, 0x12                                   */
    # c7 r6 o& y0 |( A4 R
  17.   __IO uint16_t SR1;        /*!< I2C Status register 1,      Address offset: 0x14 */
    4 D' E( b( N5 N* U0 ?8 r5 |
  18.   uint16_t      RESERVED5;  /*!< Reserved, 0x16                                   */* H: C; d' T/ ^3 G
  19.   __IO uint16_t SR2;        /*!< I2C Status register 2,      Address offset: 0x18 */- q. g- s* d5 w6 D% S) h
  20.   uint16_t      RESERVED6;  /*!< Reserved, 0x1A                                   */  a; h" A2 u4 \) {& W
  21.   __IO uint16_t CCR;        /*!< I2C Clock control register, Address offset: 0x1C */* v$ j! n( I, o) m7 c( G
  22.   uint16_t      RESERVED7;  /*!< Reserved, 0x1E                                   */4 I( q! a: ~, d1 H
  23.   __IO uint16_t TRISE;      /*!< I2C TRISE register,         Address offset: 0x20 *// {* J3 X4 Q0 J- F' G
  24.   uint16_t      RESERVED8;  /*!< Reserved, 0x22                                   */
    + n( v1 s4 G# p7 G$ A  h* j4 o
  25.   __IO uint16_t FLTR;       /*!< I2C FLTR register,          Address offset: 0x24 */
    : I8 G; U8 W+ x5 c+ k  v
  26.   uint16_t      RESERVED9;  /*!< Reserved, 0x26                                   */
    : w8 L/ |% z" H) Q% A
  27. } I2C_TypeDef;
复制代码
5 a. [8 m+ S( s0 j  e8 \

+ r" g  r) J1 T8 u% Q
" v# u% y* {7 I6 I8 U
' N9 z5 N- k* {2 P' L8 X
赞 收藏 评论0 发布时间:2022-6-16 17:00

举报

0个回答

所属标签

相似技术帖

官网相关资源

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