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- /**
% x) x/ S4 l' R6 z) j; z, y; j" E" U/ g - * @brief I2C Init structure definition 8 f u5 R$ a" @; C9 S) Y
- */
* [6 v" ], l' V8 ~9 ^- U& j
- a* a: `$ m* f: k+ H' O- m0 e. k. o- typedef struct; u# w* v. e/ ^' \) n
- {
; W# w K0 x" E& j6 L( L) T - uint32_t I2C_ClockSpeed; /*!< Specifies the clock frequency.0 S; X8 y0 C) ?6 b9 P; x7 o
- This parameter must be set to a value lower than 400kHz */
. \+ I9 c G* Y4 w - Z6 q0 p* l9 y! U2 n3 y
- uint16_t I2C_Mode; /*!< Specifies the I2C mode.6 B# p0 d2 V0 |! _; r% i% d
- This parameter can be a value of @ref I2C_mode */# \3 Z6 ^- ^2 G+ r
2 n1 C) G) l- W# j- uint16_t I2C_DutyCycle; /*!< Specifies the I2C fast mode duty cycle.
6 D/ x# k( A3 h- B7 `# u- } - This parameter can be a value of @ref I2C_duty_cycle_in_fast_mode */1 y0 H: \5 Z) x6 L
- % z9 }5 g# Z; O$ m7 m& Y- z
- uint16_t I2C_OwnAddress1; /*!< Specifies the first device own address.. ]6 D$ t: k4 o' ~8 u9 q3 Z
- This parameter can be a 7-bit or 10-bit address. */
% z* X. Q/ B% G7 R0 s" z
- c" W6 v+ T7 V/ Q! ~, M# m! K- uint16_t I2C_Ack; /*!< Enables or disables the acknowledgement.
* b+ Q2 l. M6 ~4 A( q - This parameter can be a value of @ref I2C_acknowledgement */
8 a4 i% r, N# }/ z' w. v - 8 ~3 H' F( P: q) ?4 p. o
- uint16_t I2C_AcknowledgedAddress; /*!< Specifies if 7-bit or 10-bit address is acknowledged., {: G" j9 g9 X" H5 Y
- This parameter can be a value of @ref I2C_acknowledged_address */- `- F% C1 \6 V3 c5 Q/ L6 X
- }I2C_InitTypeDef;
复制代码 , x- D4 A0 c: I8 T: T. G
I2C_Exported_Constants
. _. s. m" u& k5 b- /** @defgroup I2C_Exported_Constants
, O' `1 D' e4 ^" e - * @{0 r) y0 z6 g+ ?2 R k
- */" D- O* _ M( A
- #define IS_I2C_ALL_PERIPH(PERIPH) (((PERIPH) == I2C1) || \
# u, V3 I0 g2 ^" P) R2 v u2 d - ((PERIPH) == I2C2) || \
% l. g$ T7 Z$ Q/ O d6 b9 R# y - ((PERIPH) == I2C3))
9 o* \8 H% Y+ t+ d3 C7 h$ R. A. H4 W - /** @defgroup I2C_Digital_Filter a0 I' W" a7 v9 h
- * @{, R1 {% ^& ]1 {+ f8 T7 A( ]; @
- */
6 }7 `# L: s% q3 K- \/ i
" {! R. O6 ]6 t! R' j- #define IS_I2C_DIGITAL_FILTER(FILTER) ((FILTER) <= 0x0000000F)8 n, R2 P7 R& E" [* z1 u
- /**9 t( x* H2 q5 u6 ?* r
- * @}0 A3 A4 D. i$ e
- */
复制代码
4 C u* C! H5 sI2C_mode6 P- j! B; S- r* T3 W" r e
- /** @defgroup I2C_mode ( N2 J/ J9 m1 T1 `, }
- * @{% z* f: Z# K9 l1 N
- */ c8 a9 H5 [* b+ X, S3 g
4 ?) @9 W4 U, Z- #define I2C_Mode_I2C ((uint16_t)0x0000)% j6 w o' I! P! b( {8 M6 j
- #define I2C_Mode_SMBusDevice ((uint16_t)0x0002) ! r; j3 s3 R! B" y9 D7 }
- #define I2C_Mode_SMBusHost ((uint16_t)0x000A)+ g8 R" r! Q2 _9 a( J t
- #define IS_I2C_MODE(MODE) (((MODE) == I2C_Mode_I2C) || \; H/ o9 N+ ~, q i6 L
- ((MODE) == I2C_Mode_SMBusDevice) || \1 `% @+ r/ O, \8 I: ]- e
- ((MODE) == I2C_Mode_SMBusHost))
复制代码
9 [/ E( R6 r3 T' F9 }I2C_duty_cycle_in_fast_mode- /** @defgroup I2C_duty_cycle_in_fast_mode ) C: F+ ~0 q3 M' _+ ?
- * @{
* e2 _$ h: l2 y- H7 z8 L, a - */1 v/ r N' \' E) j" n/ \
: j: ?: n$ N; C& K- #define I2C_DutyCycle_16_9 ((uint16_t)0x4000) /*!< I2C fast mode Tlow/Thigh = 16/9 */
0 g, r7 Y# a* t( N O - #define I2C_DutyCycle_2 ((uint16_t)0xBFFF) /*!< I2C fast mode Tlow/Thigh = 2 */
6 V0 i- z0 w- R+ U2 i; j5 ^* l3 F - #define IS_I2C_DUTY_CYCLE(CYCLE) (((CYCLE) == I2C_DutyCycle_16_9) || \# M6 e, p3 k9 P; r9 i v1 p/ v0 c
- ((CYCLE) == I2C_DutyCycle_2))
复制代码 & P0 e+ d" l7 v$ g+ S
I2C_acknowledgement
" b4 }$ A! V& l6 t( o- $ I$ O$ h) U% o5 l: o$ l
- /** @defgroup I2C_acknowledgement
8 C( n+ v0 W- `1 D - * @{$ H6 ~9 L( Y+ K+ X
- */8 G8 g. b; g; b7 u5 K7 {
, F$ g6 I# w& r- #define I2C_Ack_Enable ((uint16_t)0x0400) q% G& p1 J8 u- C' c2 v' R
- #define I2C_Ack_Disable ((uint16_t)0x0000)5 q/ h# {3 `5 E4 [4 J/ y/ t
- #define IS_I2C_ACK_STATE(STATE) (((STATE) == I2C_Ack_Enable) || \+ U; c/ u! H7 M) ~
- ((STATE) == I2C_Ack_Disable))0 e2 c* q( x! ]$ _
复制代码 4 p9 b& e) `8 \0 S( d
I2C_transfer_direction6 I5 D. F4 o5 X# i3 u
- /** @defgroup I2C_transfer_direction ! U, Z8 G# D1 k" |1 N
- * @{
7 i! v \; p9 z. I0 t0 m3 @1 F - */5 ~) Z3 j( M3 v0 s
- - \* ?! C6 U8 O% n& n
- #define I2C_Direction_Transmitter ((uint8_t)0x00)
# J2 t! d t, [1 S/ g7 ` - #define I2C_Direction_Receiver ((uint8_t)0x01)% R* F3 b7 K$ F2 q4 J# C, C' X/ u
- #define IS_I2C_DIRECTION(DIRECTION) (((DIRECTION) == I2C_Direction_Transmitter) || \* a6 i; V, y7 Y1 U" Q( r
- ((DIRECTION) == I2C_Direction_Receiver))
复制代码
* }8 b9 R) c; XI2C_acknowledged_address- /** @defgroup I2C_acknowledged_address
* ?) f* N! P& h. z8 a/ V - * @{
3 [+ o4 L; O: @$ x5 p - */
3 g+ \4 |( _" Z+ P/ o' x - ) ^6 {5 U. n6 C7 n2 \. w. j
- #define I2C_AcknowledgedAddress_7bit ((uint16_t)0x4000)
7 c* T9 Q% V) c' t! f4 c b; S - #define I2C_AcknowledgedAddress_10bit ((uint16_t)0xC000)
0 a2 t; h$ S, }6 z! \ - #define IS_I2C_ACKNOWLEDGE_ADDRESS(ADDRESS) (((ADDRESS) == I2C_AcknowledgedAddress_7bit) || \) w& r$ {) s0 _" q
- ((ADDRESS) == I2C_AcknowledgedAddress_10bit))
复制代码
, u4 B, {( {# QI2C_registers
1 t X ?! \$ R7 ?7 T
( j; p* P4 f" m! _- /** @defgroup I2C_registers
: Y) C# z+ d: l2 r8 C - * @{
0 m# o* x+ c" H) v - */' u; b( Y/ [7 b% ~ L9 _7 x: l! x8 u
- % c; F! B* B* q+ q
- #define I2C_Register_CR1 ((uint8_t)0x00)
9 q& s* ?( g4 H9 Z {" \ - #define I2C_Register_CR2 ((uint8_t)0x04)5 J( z3 `, l8 ?+ c) d
- #define I2C_Register_OAR1 ((uint8_t)0x08)7 W, N* }7 m7 I. D! v7 r& A
- #define I2C_Register_OAR2 ((uint8_t)0x0C)
: w0 R+ `7 c+ e! H9 _2 R: x - #define I2C_Register_DR ((uint8_t)0x10)
& l( T3 j7 X) {3 v - #define I2C_Register_SR1 ((uint8_t)0x14)
/ B$ z/ K4 |6 ~( I - #define I2C_Register_SR2 ((uint8_t)0x18)
& P2 }8 t8 M: y5 K) O - #define I2C_Register_CCR ((uint8_t)0x1C)4 S6 l5 t4 Z2 c5 Y- v& u2 E; h
- #define I2C_Register_TRISE ((uint8_t)0x20)
- K6 J2 D) F9 |+ A - #define IS_I2C_REGISTER(REGISTER) (((REGISTER) == I2C_Register_CR1) || \; |3 Y5 p0 ?4 k" ~" `2 k
- ((REGISTER) == I2C_Register_CR2) || \
' f' S8 f3 _, V$ s - ((REGISTER) == I2C_Register_OAR1) || \
5 O$ X2 a$ w5 x - ((REGISTER) == I2C_Register_OAR2) || \
! r0 B- Y1 D5 m f - ((REGISTER) == I2C_Register_DR) || \, u% C) H0 h" L/ W4 U
- ((REGISTER) == I2C_Register_SR1) || \
( A) _" Y* \0 P - ((REGISTER) == I2C_Register_SR2) || \& S* X' e# b- a1 a( V0 }7 b( q3 Y
- ((REGISTER) == I2C_Register_CCR) || \
" s( L0 T/ U2 D4 h/ R7 U - ((REGISTER) == I2C_Register_TRISE))
复制代码 % F; {6 G. p- A7 A4 r
I2C_NACK_position% N* g% z5 { g$ I- d' N) U
- /** @defgroup I2C_NACK_position 3 J& {$ A; o8 p# h/ h
- * @{
' a5 t v0 p; H* `7 A; J$ w$ Q - */
+ V4 e$ f+ m9 Q( T: F7 ^* x - 9 b9 |. J5 S" R% j% t
- #define I2C_NACKPosition_Next ((uint16_t)0x0800), F/ ]" z& o h2 H7 w3 @2 J
- #define I2C_NACKPosition_Current ((uint16_t)0xF7FF)
2 j; a" _1 M3 u2 L - #define IS_I2C_NACK_POSITION(POSITION) (((POSITION) == I2C_NACKPosition_Next) || \
, H+ J/ A- s. t$ |0 [5 D - ((POSITION) == I2C_NACKPosition_Current))
复制代码 / |) g* e" f6 c/ G8 @
I2C_SMBus_alert_pin_level7 k% q3 r8 h% ^. n
7 `0 ?% @5 _/ q7 O0 g- /** @defgroup I2C_SMBus_alert_pin_level
9 S! L6 d3 S* L, X - * @{4 Z! I# f8 e" k$ w. D8 _, a% V
- */9 v) [7 q ^! L: a
- * f# \- E" |7 m4 a
- #define I2C_SMBusAlert_Low ((uint16_t)0x2000): x H, t9 \! |7 z
- #define I2C_SMBusAlert_High ((uint16_t)0xDFFF)
) Y# ?3 V8 [ S& j7 E - #define IS_I2C_SMBUS_ALERT(ALERT) (((ALERT) == I2C_SMBusAlert_Low) || \2 H7 M! [9 g0 K4 h3 U, }
- ((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- /** @defgroup I2C_PEC_position
% a/ ]" w/ ^; ?+ M - * @{6 H- \/ k# s9 \# Y* W+ V
- */
4 U' y9 e* _ i9 k! _
9 G# f) ^3 L" q/ i8 P: z7 D- #define I2C_PECPosition_Next ((uint16_t)0x0800)
8 y& n( w3 t+ D4 K* G/ b- r9 f - #define I2C_PECPosition_Current ((uint16_t)0xF7FF). W: T+ ]1 T* ^6 N. g
- #define IS_I2C_PEC_POSITION(POSITION) (((POSITION) == I2C_PECPosition_Next) || \
! _/ c5 ?; B/ n' c3 @3 n - ((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- /** @defgroup I2C_interrupts_definition * U6 y0 U; k5 `( a6 \
- * @{5 @& g0 ^( S) e( N. q' k% G' p
- */
: L- l: e8 y# n% i
- g5 w Z0 R# `; O. A- #define I2C_IT_BUF ((uint16_t)0x0400)# `! U2 Q/ h+ ^( T/ h
- #define I2C_IT_EVT ((uint16_t)0x0200)
" D" j/ L1 l% o6 `3 F" z - #define I2C_IT_ERR ((uint16_t)0x0100)" u1 P1 w- } ^9 l6 r& Q
- #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- /** @defgroup I2C_interrupts_definition ) A! z& E5 W- _- k
- * @{
& p: ?8 G3 E5 ~! _7 i8 m1 x8 R' ` - */
. O' I% G N$ {% `
2 P* N6 ]( u8 h1 `0 a2 C9 h8 |2 t- #define I2C_IT_SMBALERT ((uint32_t)0x01008000), J5 X( ^. t# c7 s6 P- V2 m5 ? o
- #define I2C_IT_TIMEOUT ((uint32_t)0x01004000)' P) k3 A# g; O
- #define I2C_IT_PECERR ((uint32_t)0x01001000)
! w4 P ^1 K0 _; z' N# Y - #define I2C_IT_OVR ((uint32_t)0x01000800)
" T( q5 C5 M/ W0 u W. p2 T! e - #define I2C_IT_AF ((uint32_t)0x01000400)
! a! k' m; X* Z0 x& a8 F% A" n - #define I2C_IT_ARLO ((uint32_t)0x01000200)1 } f/ }* r" r) v2 _, [
- #define I2C_IT_BERR ((uint32_t)0x01000100)
0 j: ~' W" i, ?" A8 J5 D5 o - #define I2C_IT_TXE ((uint32_t)0x06000080)
7 K& N, W, [7 a- R1 b2 l - #define I2C_IT_RXNE ((uint32_t)0x06000040)
( ]2 s6 g6 v" Z9 I" X - #define I2C_IT_STOPF ((uint32_t)0x02000010)
. `6 T* F3 N; D- l3 e* m- U - #define I2C_IT_ADD10 ((uint32_t)0x02000008)- o2 X, P/ S1 T) Q
- #define I2C_IT_BTF ((uint32_t)0x02000004)0 a/ C" }4 N9 I; X9 R; X
- #define I2C_IT_ADDR ((uint32_t)0x02000002)
, V& d V) ]7 o( b# M - #define I2C_IT_SB ((uint32_t)0x02000001)
8 v; i: y6 R. c+ u$ t/ b! E# G) b
3 [! S+ }3 @( v1 S l4 A U- #define IS_I2C_CLEAR_IT(IT) ((((IT) & (uint16_t)0x20FF) == 0x00) && ((IT) != (uint16_t)0x00))2 e% l: ^( p7 \ b
9 G, _# Q) ?5 c( [: C- #define IS_I2C_GET_IT(IT) (((IT) == I2C_IT_SMBALERT) || ((IT) == I2C_IT_TIMEOUT) || \6 Y3 J8 H9 A# @, m1 X& [
- ((IT) == I2C_IT_PECERR) || ((IT) == I2C_IT_OVR) || \
3 j8 }* J1 ^8 j - ((IT) == I2C_IT_AF) || ((IT) == I2C_IT_ARLO) || \
N4 n4 s. W8 C4 C# L8 n - ((IT) == I2C_IT_BERR) || ((IT) == I2C_IT_TXE) || \9 z% s# z. n0 G- r9 D( R2 t
- ((IT) == I2C_IT_RXNE) || ((IT) == I2C_IT_STOPF) || \) j) L* p$ R z! h8 C& E
- ((IT) == I2C_IT_ADD10) || ((IT) == I2C_IT_BTF) || \2 G% q6 g2 f7 Y. E d" B. r
- ((IT) == I2C_IT_ADDR) || ((IT) == I2C_IT_SB))
; w; n9 [$ U8 Y
% 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
- /**
, F2 I. p' h* s4 X1 Q. H& | - * @brief SR2 register flags
- s0 b% b; G2 e# u- @% a - */
9 J0 y* z1 x5 J0 u9 Z - + k5 Z' \; U+ m( J$ w
- #define I2C_FLAG_DUALF ((uint32_t)0x00800000). {4 p3 \% n9 d: }& a! q# H
- #define I2C_FLAG_SMBHOST ((uint32_t)0x00400000)
% N( c, ?, A( T - #define I2C_FLAG_SMBDEFAULT ((uint32_t)0x00200000)# Z6 O7 y. M/ \4 Z
- #define I2C_FLAG_GENCALL ((uint32_t)0x00100000)
( h1 _7 |: E- Q" q+ s! N1 p - #define I2C_FLAG_TRA ((uint32_t)0x00040000)4 c S/ B5 | Q/ |# ^
- #define I2C_FLAG_BUSY ((uint32_t)0x00020000)
/ J) z8 m1 I, }5 u5 s# D( b - #define I2C_FLAG_MSL ((uint32_t)0x00010000)
复制代码
( |6 Z7 S7 n4 E4 ?6 FSR1 register flags
2 m! f; f$ o @" m. c" W' }! |- ~
- /**
! Q0 U+ o; _$ t, m7 z$ c - * @brief SR1 register flags
* F1 n- c6 V. {4 J" S% x+ G' j! H6 ?0 U - */
6 r6 U" Q4 Z' R1 F# w
* v3 N% u1 o, ~- #define I2C_FLAG_SMBALERT ((uint32_t)0x10008000)
, w3 F/ h' \, p$ e0 G - #define I2C_FLAG_TIMEOUT ((uint32_t)0x10004000)1 W) _0 A( W! b# M7 y
- #define I2C_FLAG_PECERR ((uint32_t)0x10001000)# S% E7 l! U3 K& X
- #define I2C_FLAG_OVR ((uint32_t)0x10000800)
8 }2 ?( i3 L: J5 v3 v* x - #define I2C_FLAG_AF ((uint32_t)0x10000400)* l3 C% B$ @3 l4 I
- #define I2C_FLAG_ARLO ((uint32_t)0x10000200)9 K- p0 K+ R' A/ t! q* T7 a4 W- ^
- #define I2C_FLAG_BERR ((uint32_t)0x10000100)9 d* ~9 |& r, ~' n& Z
- #define I2C_FLAG_TXE ((uint32_t)0x10000080)
$ P8 ]+ F. A. o9 J& F4 T0 h - #define I2C_FLAG_RXNE ((uint32_t)0x10000040)6 E6 p( N7 F1 {" ?; c
- #define I2C_FLAG_STOPF ((uint32_t)0x10000010)
/ x$ Y* g: L4 X - #define I2C_FLAG_ADD10 ((uint32_t)0x10000008)
2 L. \1 o- f$ E) z - #define I2C_FLAG_BTF ((uint32_t)0x10000004)
& a6 {. q) a+ R# O+ [7 G$ [8 R6 E) L$ F - #define I2C_FLAG_ADDR ((uint32_t)0x10000002); I& `. \8 o, }% h7 v3 U
- #define I2C_FLAG_SB ((uint32_t)0x10000001)
: \/ W/ _5 C4 }. r7 f: [: B; C - & [+ _9 y( o" Q6 U$ }7 _! C
- #define IS_I2C_CLEAR_FLAG(FLAG) ((((FLAG) & (uint16_t)0x20FF) == 0x00) && ((FLAG) != (uint16_t)0x00))) s4 U9 u: ]; x, K! G5 Q
. l8 Y: p3 I) g o8 x- #define IS_I2C_GET_FLAG(FLAG) (((FLAG) == I2C_FLAG_DUALF) || ((FLAG) == I2C_FLAG_SMBHOST) || \ c+ B$ C: }7 \! O( x9 U7 c% ~0 {
- ((FLAG) == I2C_FLAG_SMBDEFAULT) || ((FLAG) == I2C_FLAG_GENCALL) || \8 \8 p x, {6 k2 Q2 M
- ((FLAG) == I2C_FLAG_TRA) || ((FLAG) == I2C_FLAG_BUSY) || \ N- S# C* f7 L% R
- ((FLAG) == I2C_FLAG_MSL) || ((FLAG) == I2C_FLAG_SMBALERT) || \
' j( s" ?! m9 [4 o - ((FLAG) == I2C_FLAG_TIMEOUT) || ((FLAG) == I2C_FLAG_PECERR) || \
. K/ S2 d6 h1 ^) ~ - ((FLAG) == I2C_FLAG_OVR) || ((FLAG) == I2C_FLAG_AF) || \5 \ d1 A$ G2 j! `
- ((FLAG) == I2C_FLAG_ARLO) || ((FLAG) == I2C_FLAG_BERR) || \
# {8 I0 T+ ^2 `1 z% F8 u2 u! n - ((FLAG) == I2C_FLAG_TXE) || ((FLAG) == I2C_FLAG_RXNE) || \
4 ~) @" Y! A/ y* p - ((FLAG) == I2C_FLAG_STOPF) || ((FLAG) == I2C_FLAG_ADD10) || \" D9 R& |- s I
- ((FLAG) == I2C_FLAG_BTF) || ((FLAG) == I2C_FLAG_ADDR) || \. G7 E& X2 M3 n$ l4 U0 {& ?
- ((FLAG) == I2C_FLAG_SB))
复制代码
) q0 M& V# Y' u3 r% T03. 相关函数) {7 S' L: Q( I+ B. g) i
- /* Function used to set the I2C configuration to the default reset state *****/- X7 T( D; d/ v4 W/ \
- void I2C_DeInit(I2C_TypeDef* I2Cx);
/ Z! D1 E. S m' b: U
* k4 F6 K8 g q( G" L( T V# @* H- /* Initialization and Configuration functions *********************************/
# ?* C* y, @7 Y M - void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct);
/ L' K' @3 B8 I2 D- ~ - void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct);
6 o5 e8 o+ E( ~ - void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState);% W5 N. S5 @% i" B0 A
- void I2C_DigitalFilterConfig(I2C_TypeDef* I2Cx, uint16_t I2C_DigitalFilter);4 n2 _( r2 e" J' m* w+ S2 K# I# N# }. X
- void I2C_AnalogFilterCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
/ m! v7 x1 Z" J- Z1 { M9 \ - void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState);
% O; g9 a6 z: p1 N - void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState);
& D! A$ v: u0 l9 {3 ?$ {$ Z) q3 B% K$ l - void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, uint8_t I2C_Direction);
2 w) n! P# G3 n! \$ ~ - void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState);7 `, B( G- c' j! M+ ~. X4 k8 R3 ^" i
- void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint8_t Address);7 p4 u' U* ^/ O6 D1 o, W- ^- @
- void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
3 w( p) e+ i, b6 Z$ v# j - void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
. G# y9 g5 A* d2 B* [ - void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);0 B% R5 p2 ?9 ^6 m
- void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);# Y- y4 v6 V( C( q& q" |3 a/ j
- void I2C_FastModeDutyCycleConfig(I2C_TypeDef* I2Cx, uint16_t I2C_DutyCycle);3 ?: t! g2 Y8 A) l0 J2 z3 F0 V
- void I2C_NACKPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_NACKPosition);
6 m9 [. j/ k/ e/ y+ s - void I2C_SMBusAlertConfig(I2C_TypeDef* I2Cx, uint16_t I2C_SMBusAlert);
% n- W( X3 q/ B" l& g! v - void I2C_ARPCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);+ i* U' h# V P' C+ Y( f
- ( j# d$ h/ A4 t* ~
- /* Data transfers functions ***************************************************/ 4 U! l3 u' U( p- c6 O" ]
- void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data);
0 E5 l- ?) |6 E( L - uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx);4 E" U/ Q9 \, g* m' F
- " R9 s+ z( I5 i* Y% |
- /* PEC management functions ***************************************************/
$ @! F5 _+ L; j - void I2C_TransmitPEC(I2C_TypeDef* I2Cx, FunctionalState NewState);- G7 |' H" q( z. e2 @6 X
- void I2C_PECPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_PECPosition);2 Z% ?# T) u2 Q8 _3 r. r p
- void I2C_CalculatePEC(I2C_TypeDef* I2Cx, FunctionalState NewState);
9 C0 h; \- \7 l - uint8_t I2C_GetPEC(I2C_TypeDef* I2Cx);
1 }1 x9 U2 ]$ v1 F3 S7 U% e F
6 k% E" h8 d" b( X' R" J- /* DMA transfers management functions *****************************************/' \5 s- r, G: }9 v2 G7 F3 d
- void I2C_DMACmd(I2C_TypeDef* I2Cx, FunctionalState NewState);: w- h( ~3 u" C: p
- void I2C_DMALastTransferCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
/ @$ I/ n/ k+ n9 l+ {8 v
# ^) s. ?$ f5 B+ Z: Z- /* Interrupts, events and flags management functions **************************/$ H; O* y: e% u. @# _* a+ P/ g+ a
- uint16_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register);
, a4 Y7 b9 Y8 z: {( s - void I2C_ITConfig(I2C_TypeDef* I2Cx, uint16_t I2C_IT, FunctionalState NewState);
& J/ |8 f- e6 e0 r5 S! ^
, ?& L: b+ W7 r" L1 [- /*
1 p9 {" J1 Y8 t - ===============================================================================
0 T4 w# o7 z% t- M3 j - I2C State Monitoring Functions1 O4 [4 o. _1 j0 ?" z v
- ===============================================================================- g) e, O2 g: ^; V& d
- This I2C driver provides three different ways for I2C state monitoring
! ?4 U1 ~! ^4 V - depending on the application requirements and constraints:' }3 I% u5 K8 u: t& W2 r
# @4 t" K$ b# q6 c+ ]- I5 w( ^7 i0 D/ q; u/ q% o/ b
- 1. Basic state monitoring (Using I2C_CheckEvent() function)
: N9 }; J. |! k2 o# G( W$ V, C- H - -----------------------------------------------------------
7 P1 `& k$ }3 Q' Y: ?9 \ - It compares the status registers (SR1 and SR2) content to a given event
0 u! W) V$ I9 t7 e6 b; \& y o. Q1 ? - (can be the combination of one or more flags).4 v) w6 x4 e8 `9 _- Q
- It returns SUCCESS if the current status includes the given flags 3 P" y! K$ h; ]' x- f# r/ q# n
- and returns ERROR if one or more flags are missing in the current status.$ X6 a [' R4 y0 r! D, v$ U
- + b( k `- S3 ~* A
- - When to use1 t+ V# `; Q# F/ S- d
- - This function is suitable for most applications as well as for startup . p' Z# t. x* S- v( {
- activity since the events are fully described in the product reference
6 p: d" ~( w( b, K, B3 ] - manual (RM0090).
" C; ^8 X: A$ j3 c+ p - - It is also suitable for users who need to define their own events.8 S6 J3 ], Y+ \ p; X3 C" G- l# R
- " v D) D$ Y' g4 X' a1 k# B& Y
- - Limitations+ v) U% ]8 o/ @# A" I6 S( c8 K
- - If an error occurs (ie. error flags are set besides to the monitored 1 L5 j( P1 `9 A# D# }; Y! w8 P
- flags), the I2C_CheckEvent() function may return SUCCESS despite
- n! O& |5 E, P( v/ V1 @/ C - the communication hold or corrupted real state. 2 g) Z7 Z9 a; O9 v9 r( Z
- In this case, it is advised to use error interrupts to monitor S. p `: Y) G6 \3 ?, h% u3 A( |
- the error events and handle them in the interrupt IRQ handler.
/ I8 _9 Y4 E, I
# M' B; w5 b% W' ^3 w [( V- Note
8 M3 o- }4 B0 ?" p: D - For error management, it is advised to use the following functions:
: h% `( }3 w$ C1 w+ E! a - - I2C_ITConfig() to configure and enable the error interrupts (I2C_IT_ERR).
* c1 d! T- \$ v+ k - - I2Cx_ER_IRQHandler() which is called when the error interrupt occurs.* y0 I. Y/ m: ~* Y% Q5 {' l
- Where x is the peripheral instance (I2C1, I2C2 ...)' X; A& J8 Q5 D& d
- - I2C_GetFlagStatus() or I2C_GetITStatus() to be called into the
) E# w u/ q) K6 V! v; { - I2Cx_ER_IRQHandler() function in order to determine which error occurred.
0 C3 w3 D8 n: ], l: e - - I2C_ClearFlag() or I2C_ClearITPendingBit() and/or I2C_SoftwareResetCmd()
0 X6 v0 ~) f$ R9 N C - and/or I2C_GenerateStop() in order to clear the error flag and source
. }0 U& X6 F" |; r6 C - and return to correct communication status.
$ s4 `7 s P V - & ]- ?3 M1 O5 C
- / y3 ~! w* I/ S; u/ U# a
- 2. Advanced state monitoring (Using the function I2C_GetLastEvent())
% B. B( M& X- O - -------------------------------------------------------------------- ; C% F7 y0 W' j4 ]
- Using the function I2C_GetLastEvent() which returns the image of both status
: Q9 Q0 h! m) h( U7 Y) |6 ^ - registers in a single word (uint32_t) (Status Register 2 value is shifted left
$ j1 b. s4 O" \# b/ n - by 16 bits and concatenated to Status Register 1).
% C' j- K5 H' e8 D5 | - 4 Y& V1 F& G% I! D
- - When to use/ [+ d$ ]2 s! ?/ F
- - This function is suitable for the same applications above but it
" m- M; E6 c2 z8 p9 Q - allows to overcome the mentioned limitation of I2C_GetFlagStatus()
8 ^8 B; v I+ u8 v. h3 R8 P5 V - function.
0 I! x! Q0 s7 a, D1 o - - The returned value could be compared to events already defined in 9 ` n6 @2 k3 }% ?# }/ i
- this file or to custom values defined by user.
/ \- N1 Y8 `' N t }! S2 \6 P5 o! [ - This function is suitable when multiple flags are monitored at the
( V+ z1 Y" x m3 D7 x% ^! M7 n - same time.
5 E4 o* k1 h& s0 j - - At the opposite of I2C_CheckEvent() function, this function allows
' d5 ~/ v+ ^4 f - user to choose when an event is accepted (when all events flags are % W) k/ y; h# A% Z& X
- set and no other flags are set or just when the needed flags are set
5 |4 @% E( u& `: e3 B4 Z7 y' x) \+ ~ - like I2C_CheckEvent() function.6 J, p. G# R7 P, E" u* E
- j9 _2 U" P" j3 i- - Limitations" e0 o* f& L/ A6 O$ \
- - User may need to define his own events.
: N$ K) b; i; ?6 K& F0 \+ b' @ - - Same remark concerning the error management is applicable for this 7 N$ ]; n* c& ?( S
- function if user decides to check only regular communication flags ; E: x$ c( G! [: ^$ k+ a, ?
- (and ignores error flags).
* v4 W1 E% s& m - 9 m: m. d, z8 K) P* M- [
- - | A# d U9 h0 u* @
- 3. Flag-based state monitoring (Using the function I2C_GetFlagStatus())
0 }! ` a* T0 h7 _ - -----------------------------------------------------------------------, _$ l5 y8 G# V; h1 S* l! x% x
9 [" T+ q" ^0 ]' I) r# R3 X- Using the function I2C_GetFlagStatus() which simply returns the status of
Z8 K( a4 w' }4 z4 @+ X0 s - one single flag (ie. I2C_FLAG_RXNE ...).
9 d0 M( ~4 e$ m) j; c K4 ]* V - 5 \$ N; c1 H. H, Z* g/ \9 N
- - When to use) w* G8 B1 H7 Y
- - This function could be used for specific applications or in debug * i" S# e5 V1 R
- phase.' t6 e6 F0 A7 p9 t! p' ]7 z; u$ o+ H
- - It is suitable when only one flag checking is needed (most I2C " l( P: t0 A K% k
- events are monitored through multiple flags).# N4 Q9 ^' u& v5 K: i' Q
- - Limitations: ; d6 O! R) ^$ r7 s5 i1 p7 i
- - When calling this function, the Status register is accessed.
+ B6 I5 E5 e, G - Some flags are cleared when the status register is accessed.
( S( ?$ D" x+ [: d9 N7 h! A - So checking the status of one Flag, may clear other ones.4 h5 B. r) j% h
- - Function may need to be called twice or more in order to monitor ) K# `/ }. J: o0 M' O+ J
- one single event.
$ u$ P# b/ u; Z( w# E" D - */
, y2 O9 s- a C - * i2 a9 w, X0 R/ o+ r
- /*
; U' U9 D- d6 V! h - ===============================================================================4 C5 H# x( }0 x d, }
- 1. Basic state monitoring
2 Y s, a7 x: S) U - ===============================================================================
- o# d# s$ i1 ~ - */
1 [$ u* Y/ e8 C - ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT);
, z: c% `0 z/ ?: n - /*& V0 |& |( q8 \8 D' w/ n
- ===============================================================================
6 x/ K5 U* _( s! U) }2 k2 X - 2. Advanced state monitoring
* y7 \( x$ S7 q: l - ===============================================================================
t/ h6 D. @, Z6 ~- L1 C; ^! f - */
" g* \9 J+ w4 a1 B' N9 ? - uint32_t I2C_GetLastEvent(I2C_TypeDef* I2Cx);3 M( w/ J) u+ |; e; ]* x
- /*
- T( {/ X$ {' N/ S' A7 q - ===============================================================================
' _# c: }# B% N) U - 3. Flag-based state monitoring
; V7 \( c# x5 u - ===============================================================================
* h5 Z% C+ [* |0 h4 U/ K, O - */
$ M" Y1 ^+ H7 Y# s) e1 c - FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);
' C& ]3 N% i/ W+ ~! A$ D' }
( T" ]7 Q3 i+ ^: C) {
% G O* W- n/ L p1 n F- void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);9 F9 z6 X# A8 _9 S
- ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT);
) ^) L* u8 D$ |/ i* L - 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- /**
5 n; }8 w* V7 z5 M - * @brief Inter-integrated Circuit Interface
+ q4 O1 T4 u/ O/ I - */
3 a# Y8 @) T- Z9 |7 q! E6 N! Q
y( G4 O/ t; B r" ] t- typedef struct0 g- {, ?8 D8 d5 o, M
- {& ]$ V7 h# ~5 ~& F# Z& l$ ^
- __IO uint16_t CR1; /*!< I2C Control register 1, Address offset: 0x00 */$ A4 z- A, l+ c
- uint16_t RESERVED0; /*!< Reserved, 0x02 */8 \0 T- A- A- j: K, S. W
- __IO uint16_t CR2; /*!< I2C Control register 2, Address offset: 0x04 */4 w; v. a- e, m# w( L8 _0 l8 ^
- uint16_t RESERVED1; /*!< Reserved, 0x06 */
6 D# T" ?4 ]5 t/ x) E9 R - __IO uint16_t OAR1; /*!< I2C Own address register 1, Address offset: 0x08 */
% R- b1 y5 Y% r7 a0 r - uint16_t RESERVED2; /*!< Reserved, 0x0A */, [& G# ?% r! O; E
- __IO uint16_t OAR2; /*!< I2C Own address register 2, Address offset: 0x0C */
% v$ c t5 l! D1 I - uint16_t RESERVED3; /*!< Reserved, 0x0E */+ I; B- `9 t% k* C: \
- __IO uint16_t DR; /*!< I2C Data register, Address offset: 0x10 */: j4 v. M& u; g1 T* D9 _
- uint16_t RESERVED4; /*!< Reserved, 0x12 */8 b y! p0 |) i, |+ Z3 D% K
- __IO uint16_t SR1; /*!< I2C Status register 1, Address offset: 0x14 */
4 E( e; U7 d# a+ |* S1 a - uint16_t RESERVED5; /*!< Reserved, 0x16 */
' [3 A9 n; v* L: c: u - __IO uint16_t SR2; /*!< I2C Status register 2, Address offset: 0x18 */7 v! h* J! w( @4 L6 D
- uint16_t RESERVED6; /*!< Reserved, 0x1A */
! w0 p% M% L I7 x* l1 f - __IO uint16_t CCR; /*!< I2C Clock control register, Address offset: 0x1C */" r) U+ W! M, p3 S5 l
- uint16_t RESERVED7; /*!< Reserved, 0x1E */
/ I. x9 l$ f, Y9 d7 G- }* | - __IO uint16_t TRISE; /*!< I2C TRISE register, Address offset: 0x20 */
+ l5 u1 y" w$ |. S, R - uint16_t RESERVED8; /*!< Reserved, 0x22 */
5 q8 j3 }+ ^) |# s/ Q+ W - __IO uint16_t FLTR; /*!< I2C FLTR register, Address offset: 0x24 */
+ g" ^) b+ z* t I/ X& E - uint16_t RESERVED9; /*!< Reserved, 0x26 */1 z' J+ V% j& w* x( A* a% b
- } 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 |