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
- /**
8 b2 C/ n; ?# E - * @brief I2C Init structure definition
& A* u, s& w! _+ p, O5 i - */
u& L9 o: z" ~
' i( j8 @% }2 l, [1 t- typedef struct( B4 U( `& E! a! B
- {
) I- l; B/ I( ^& w6 c - uint32_t I2C_ClockSpeed; /*!< Specifies the clock frequency.: n# {+ N5 |; g" T
- This parameter must be set to a value lower than 400kHz */
9 z! X. t, }/ R. [) ?
) `0 F7 W( C! P7 C/ J0 Y- uint16_t I2C_Mode; /*!< Specifies the I2C mode.
# y5 ^. @- M2 ]9 \ z( C3 j - This parameter can be a value of @ref I2C_mode */( E* K T/ r q/ V. {' r# o
$ e* K, v1 h/ c+ u- s% l- uint16_t I2C_DutyCycle; /*!< Specifies the I2C fast mode duty cycle.1 `5 w ?% R7 [ u; K2 Y1 e3 v
- This parameter can be a value of @ref I2C_duty_cycle_in_fast_mode */
! y' W9 A7 n3 E* o- [* Z# v9 {
' _& C% M& X4 \) d- uint16_t I2C_OwnAddress1; /*!< Specifies the first device own address.3 p5 m4 y4 H- y* k& e
- This parameter can be a 7-bit or 10-bit address. */
) D* j" M) O6 c _. o
; ^2 [. x. X1 R: ^- uint16_t I2C_Ack; /*!< Enables or disables the acknowledgement.( W( c4 i, U. o# A" s) o5 u
- This parameter can be a value of @ref I2C_acknowledgement */" e# X9 j# j. V" L# A$ g
- " L4 U" [7 s3 x$ O e
- uint16_t I2C_AcknowledgedAddress; /*!< Specifies if 7-bit or 10-bit address is acknowledged.% T( z* E9 ?( Z+ b" l) R" M! i
- This parameter can be a value of @ref I2C_acknowledged_address */
7 B# [" Z( k8 ?& [3 S - }I2C_InitTypeDef;
复制代码
5 I' ~3 `1 O) ?$ @9 V1 C7 \I2C_Exported_Constants7 X9 W, A5 U: H
+ ^5 p# {: n3 b
- 3 |2 [0 |! l0 l7 ?2 H
- /** @defgroup I2C_Exported_Constants
- s* Z- K( J( g! V. N - * @{
R" _2 F% G- ^ o+ `9 Z - */" i6 a/ h' o9 F& ^$ G" i
- #define IS_I2C_ALL_PERIPH(PERIPH) (((PERIPH) == I2C1) || \- d. M) B3 u5 k$ M; x" j
- ((PERIPH) == I2C2) || \2 s6 P5 x! p# j# _$ P- c2 T( B
- ((PERIPH) == I2C3))& a G! y6 I. g! s% o: L* a
- /** @defgroup I2C_Digital_Filter
2 s: L: J; F5 f2 D. { - * @{
$ J/ I [! L" y - */
4 T5 }4 \ Z9 [9 ^, U: h
! p5 E, K0 F' W( p6 g* R- #define IS_I2C_DIGITAL_FILTER(FILTER) ((FILTER) <= 0x0000000F)4 Q: B( ? a+ Y1 Z
- /**. R0 c5 r, e# {) J
- * @}+ P A& L2 {- o; u% k
- */
复制代码
: B3 j/ N) ^( \: v/ C5 } q8 w' HI2C_mode- /** @defgroup I2C_mode ' R0 I7 c- V0 _* M4 @& L7 N' Q' x
- * @{4 E9 P1 y2 q, }3 K8 C- F
- */+ L* f* ^0 {( H9 L9 O1 u2 ~
- * x3 D6 Q) _( S3 ^# i0 D, M
- #define I2C_Mode_I2C ((uint16_t)0x0000)
4 c3 r, \1 p8 C4 e I- s6 G6 v - #define I2C_Mode_SMBusDevice ((uint16_t)0x0002)
* X ^" w% U1 J6 z( K+ n - #define I2C_Mode_SMBusHost ((uint16_t)0x000A)9 `0 n1 r3 ]2 b. p2 q5 y, }' a) R
- #define IS_I2C_MODE(MODE) (((MODE) == I2C_Mode_I2C) || \
& `- _- ?9 P/ } - ((MODE) == I2C_Mode_SMBusDevice) || \& t b) W2 p$ ~( H( H2 q T
- ((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
- /** @defgroup I2C_duty_cycle_in_fast_mode
3 q/ l+ J4 x4 z1 }0 ? - * @{& ]" S% f7 ?7 Q8 I8 Y! w. A) w
- */
& @/ q9 J; F- k4 T
$ R7 Y, s9 @7 w" [' X- #define I2C_DutyCycle_16_9 ((uint16_t)0x4000) /*!< I2C fast mode Tlow/Thigh = 16/9 */: U, p+ u: r9 m U6 V7 X
- #define I2C_DutyCycle_2 ((uint16_t)0xBFFF) /*!< I2C fast mode Tlow/Thigh = 2 */# _/ `% S( f2 _ |( {. v1 d$ v
- #define IS_I2C_DUTY_CYCLE(CYCLE) (((CYCLE) == I2C_DutyCycle_16_9) || \; {2 K6 V) L( S
- ((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, ^- /** @defgroup I2C_acknowledgement
( r1 ?" a0 d8 e/ w. c - * @{
2 |. w# H2 K3 ? - */
5 ^- Q' q8 B/ n& Q Q
2 x& X9 w6 m2 o9 d- @1 k4 v- |4 x" y- #define I2C_Ack_Enable ((uint16_t)0x0400)
; b+ f( {6 U& c - #define I2C_Ack_Disable ((uint16_t)0x0000)
5 o/ d( h4 d3 b - #define IS_I2C_ACK_STATE(STATE) (((STATE) == I2C_Ack_Enable) || \. o/ K! O+ l/ f; F, {4 D" E) A5 @2 P
- ((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- /** @defgroup I2C_transfer_direction
" P& ]( {: p3 p: r& W - * @{0 S, [& H6 o3 e& f. @3 t
- */
2 x0 H2 a* N/ l4 R
0 e N" h# R4 r1 R! M1 I/ k- f7 n- #define I2C_Direction_Transmitter ((uint8_t)0x00)
: Y2 n' i6 r$ n, f* {9 O/ i* |5 E - #define I2C_Direction_Receiver ((uint8_t)0x01)
! ?5 m) A8 K" c! ^ - #define IS_I2C_DIRECTION(DIRECTION) (((DIRECTION) == I2C_Direction_Transmitter) || \
3 E0 W: ~3 a& ]& ^4 F1 O& _ - ((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
- /** @defgroup I2C_acknowledged_address
9 i2 z& L1 O! ?4 C4 r8 @) l - * @{
- H, Z1 g# r8 _- L - */
/ s& \2 A. [; V5 g( w5 H
/ P; f J5 s6 U9 U- #define I2C_AcknowledgedAddress_7bit ((uint16_t)0x4000); _, J W2 k( |1 e! n
- #define I2C_AcknowledgedAddress_10bit ((uint16_t)0xC000)' n6 e1 t1 s }8 i
- #define IS_I2C_ACKNOWLEDGE_ADDRESS(ADDRESS) (((ADDRESS) == I2C_AcknowledgedAddress_7bit) || \! w: w+ z3 W: E! O" O5 K' w
- ((ADDRESS) == I2C_AcknowledgedAddress_10bit))
复制代码 , e7 d( Z1 Z: M/ `
I2C_registers* \- u# e" j, b( C
, y7 h( t1 o3 C/ v- /** @defgroup I2C_registers 1 a) G% j0 ^8 y, ~ a" M
- * @{5 u0 T% v7 l6 ^: `3 M
- */2 P$ b: Y1 q1 e/ a: ]& I
) s+ [3 A u; V! x/ T) q- #define I2C_Register_CR1 ((uint8_t)0x00)
5 j2 e P6 G, p - #define I2C_Register_CR2 ((uint8_t)0x04)
! ]+ r& ]0 [& V: t ^ - #define I2C_Register_OAR1 ((uint8_t)0x08)
* v2 {0 f7 O6 x3 {' U/ F. {6 Q - #define I2C_Register_OAR2 ((uint8_t)0x0C)/ v- L0 r* R: J
- #define I2C_Register_DR ((uint8_t)0x10)
9 [# d% x, z" Y# V3 F - #define I2C_Register_SR1 ((uint8_t)0x14)
' H; D7 K" U# b2 B" i1 B5 i - #define I2C_Register_SR2 ((uint8_t)0x18)
" I4 k- @1 {/ T5 f/ Q) }5 T - #define I2C_Register_CCR ((uint8_t)0x1C)5 v0 ?1 U( f7 N6 H9 M
- #define I2C_Register_TRISE ((uint8_t)0x20)
7 m9 @1 M8 x1 }5 T' H) }/ } - #define IS_I2C_REGISTER(REGISTER) (((REGISTER) == I2C_Register_CR1) || \
7 e8 e- J8 y# z+ Y* x6 M - ((REGISTER) == I2C_Register_CR2) || \5 f% C+ r z3 O3 j# a( r5 F
- ((REGISTER) == I2C_Register_OAR1) || \% d" a( a8 e4 w$ P; y( Q7 c
- ((REGISTER) == I2C_Register_OAR2) || \! j! P$ a3 \+ |) t" ]% G# ~
- ((REGISTER) == I2C_Register_DR) || \; n, `; G8 d% ]
- ((REGISTER) == I2C_Register_SR1) || \: O% y. D1 E/ T: T
- ((REGISTER) == I2C_Register_SR2) || \
4 ]) ]6 B5 w; Y2 q; q% u - ((REGISTER) == I2C_Register_CCR) || \0 r, h9 ^) c8 b: C
- ((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- /** @defgroup I2C_NACK_position : J& Y+ c; |' ~, |
- * @{
$ f7 L1 r( s5 z: A& h- h: r - */: q {7 b0 n+ g! F; w% {
- 9 e) o! q1 \3 Z1 ~4 X
- #define I2C_NACKPosition_Next ((uint16_t)0x0800)
; I) q f: O5 g - #define I2C_NACKPosition_Current ((uint16_t)0xF7FF)
, F1 }8 E- N [ - #define IS_I2C_NACK_POSITION(POSITION) (((POSITION) == I2C_NACKPosition_Next) || \1 F" E! n0 B* {- S; S
- ((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 `
- /** @defgroup I2C_SMBus_alert_pin_level 2 B# t3 h: v+ c2 D/ V+ n1 B
- * @{# j! f2 q R) y! g8 P
- */
5 Y9 m- C2 z$ @. u - # I& u, Q; b4 G
- #define I2C_SMBusAlert_Low ((uint16_t)0x2000)
& z- P, ^: I* N, u" C% ]# ]8 p - #define I2C_SMBusAlert_High ((uint16_t)0xDFFF). |$ u( K- d# S9 r; Y
- #define IS_I2C_SMBUS_ALERT(ALERT) (((ALERT) == I2C_SMBusAlert_Low) || \
8 U q& x# t) x% f n - ((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
- /** @defgroup I2C_PEC_position
) q& h* d% l/ o - * @{0 R) T) a4 L3 V, D% p9 P
- */
; P, m9 Z" |4 f0 l5 R& E) N
: V: d8 n* t1 }- #define I2C_PECPosition_Next ((uint16_t)0x0800)
* e/ d" n5 P& H0 a) R - #define I2C_PECPosition_Current ((uint16_t)0xF7FF)) r& I0 c* h* v4 s! M% J. \
- #define IS_I2C_PEC_POSITION(POSITION) (((POSITION) == I2C_PECPosition_Next) || \
6 \- T/ a) r+ x, l; Y - ((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 ~' @- /** @defgroup I2C_interrupts_definition ; r% u+ I M: t% B+ k( v, w- M8 w3 J4 i S
- * @{5 Z( H8 [2 Q" T8 X- X
- */
$ g. M/ F/ Y9 S. y6 c7 O - . @! T# Q5 h& [; k3 F4 @7 Q
- #define I2C_IT_BUF ((uint16_t)0x0400)
% Y$ S+ w2 a3 c/ h - #define I2C_IT_EVT ((uint16_t)0x0200)) @2 x2 N: a$ O6 P# v, p
- #define I2C_IT_ERR ((uint16_t)0x0100)( x5 T: V: `" ^: r: g# t
- #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) ]
- /** @defgroup I2C_interrupts_definition ) I a5 H$ S* e4 t" [
- * @{- v% f5 E1 X% J+ q# C5 p# [' w
- *// Q5 H7 q1 ?- s" C
- % X3 x1 K' f3 J( e/ \3 ~+ h! O& F
- #define I2C_IT_SMBALERT ((uint32_t)0x01008000)4 d# O' i4 t& d+ M; g! l* B0 c: P
- #define I2C_IT_TIMEOUT ((uint32_t)0x01004000)
) [4 @; N" M; t2 Z8 R; J( d4 Y - #define I2C_IT_PECERR ((uint32_t)0x01001000)" L N8 Z9 ^3 ^6 }0 v w! D
- #define I2C_IT_OVR ((uint32_t)0x01000800)
0 s1 l( q" O9 g: A - #define I2C_IT_AF ((uint32_t)0x01000400)
8 P9 t# U3 K% y- c' R. g! i - #define I2C_IT_ARLO ((uint32_t)0x01000200)
$ v1 B) Y5 U S) B' R' _6 Z - #define I2C_IT_BERR ((uint32_t)0x01000100)4 `- _ v9 X) d" w! I
- #define I2C_IT_TXE ((uint32_t)0x06000080)
- } w- ]1 x: A& {% J2 f" r! s - #define I2C_IT_RXNE ((uint32_t)0x06000040)
) {7 s& ?9 k) _$ G' a - #define I2C_IT_STOPF ((uint32_t)0x02000010)/ K r6 u; s1 w/ F9 ~- u* U
- #define I2C_IT_ADD10 ((uint32_t)0x02000008)
- A3 Z/ M- ~. f7 b! [7 { s - #define I2C_IT_BTF ((uint32_t)0x02000004)
5 T! s* N# n' s. L; Z- Q' g+ t - #define I2C_IT_ADDR ((uint32_t)0x02000002); i2 y9 K/ s1 F" e' _
- #define I2C_IT_SB ((uint32_t)0x02000001)/ F! ~# ]0 s- Y" ~
+ @, v6 ?( f7 A. a, U# |3 L- #define IS_I2C_CLEAR_IT(IT) ((((IT) & (uint16_t)0x20FF) == 0x00) && ((IT) != (uint16_t)0x00))
8 s8 }4 K. R A5 _
5 D. I) d6 h5 x0 ]9 i: e* G- #define IS_I2C_GET_IT(IT) (((IT) == I2C_IT_SMBALERT) || ((IT) == I2C_IT_TIMEOUT) || \* s8 ]8 M" V" _4 v
- ((IT) == I2C_IT_PECERR) || ((IT) == I2C_IT_OVR) || \
# I9 Y W) R1 a - ((IT) == I2C_IT_AF) || ((IT) == I2C_IT_ARLO) || \# j- v* k( n) b0 q* T; n4 ~
- ((IT) == I2C_IT_BERR) || ((IT) == I2C_IT_TXE) || \
& @; ?, ~5 {# T - ((IT) == I2C_IT_RXNE) || ((IT) == I2C_IT_STOPF) || \
6 H+ E3 X! F! U - ((IT) == I2C_IT_ADD10) || ((IT) == I2C_IT_BTF) || \
$ X1 T! n0 N- J$ V- B g - ((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- /** & o& @( _/ `7 g
- * @brief SR2 register flags 8 Q6 U+ x5 ~: X: G! r
- */0 Z# ^( j+ m# B* ^, o
) ~$ ]6 d/ ^, U% }1 _1 y/ ^- #define I2C_FLAG_DUALF ((uint32_t)0x00800000)
" T3 U# j+ y* e+ }/ C - #define I2C_FLAG_SMBHOST ((uint32_t)0x00400000)2 A$ [/ O& Q4 n6 \0 ?
- #define I2C_FLAG_SMBDEFAULT ((uint32_t)0x00200000)
4 O: b: [0 |; c8 }: A - #define I2C_FLAG_GENCALL ((uint32_t)0x00100000)
3 i% v- x s9 L - #define I2C_FLAG_TRA ((uint32_t)0x00040000)
7 y. k6 l* F: g; T - #define I2C_FLAG_BUSY ((uint32_t)0x00020000)
2 W: Z# x" t4 S! k3 r - #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- /** ! y/ k2 R$ |3 r% W8 i* x
- * @brief SR1 register flags
/ g2 J, {* L$ R% X - */, b, r) Z0 q6 Q- P- f
- 7 c. ?( R, R, K6 ?
- #define I2C_FLAG_SMBALERT ((uint32_t)0x10008000)
* T( a" i0 o. Q- E' h - #define I2C_FLAG_TIMEOUT ((uint32_t)0x10004000)
) {7 q; z# k* X6 C+ k2 F9 g. A* q - #define I2C_FLAG_PECERR ((uint32_t)0x10001000)
, i1 c' [/ q* o6 J# M e - #define I2C_FLAG_OVR ((uint32_t)0x10000800)
5 c( {) m8 R; L/ h" d1 _0 e3 W# F - #define I2C_FLAG_AF ((uint32_t)0x10000400)" e4 c# P% p- b; y; b. m3 E% y
- #define I2C_FLAG_ARLO ((uint32_t)0x10000200)
% S2 s# I/ X, b. R - #define I2C_FLAG_BERR ((uint32_t)0x10000100)
2 l. F7 b5 ^' I: [& W0 u/ ]2 X, @ - #define I2C_FLAG_TXE ((uint32_t)0x10000080)8 V8 b5 R% M2 D: |9 f; D" k+ r% Q- `
- #define I2C_FLAG_RXNE ((uint32_t)0x10000040)% l t+ f J( o% I
- #define I2C_FLAG_STOPF ((uint32_t)0x10000010)
. M5 X! J. q/ j$ s# _7 v4 _1 O - #define I2C_FLAG_ADD10 ((uint32_t)0x10000008)
3 s% {/ m) R! ?1 [$ t% d - #define I2C_FLAG_BTF ((uint32_t)0x10000004)# n! |3 M" e4 Y0 P) N% t4 A& }
- #define I2C_FLAG_ADDR ((uint32_t)0x10000002)
, G8 `. \5 ?' m& L - #define I2C_FLAG_SB ((uint32_t)0x10000001)
5 t/ y$ I" W# r$ a' }; Q" Q- y - : C/ t$ Z f* ^' G4 G' d' @
- #define IS_I2C_CLEAR_FLAG(FLAG) ((((FLAG) & (uint16_t)0x20FF) == 0x00) && ((FLAG) != (uint16_t)0x00))- N% w# r( c+ Z% v( ]+ r" c
6 l* r7 B1 Y& \8 R |1 b9 d. }9 V- #define IS_I2C_GET_FLAG(FLAG) (((FLAG) == I2C_FLAG_DUALF) || ((FLAG) == I2C_FLAG_SMBHOST) || \
! {2 V3 N A+ ~' n - ((FLAG) == I2C_FLAG_SMBDEFAULT) || ((FLAG) == I2C_FLAG_GENCALL) || \
! Z/ P9 Z( t; d8 L$ Y9 B - ((FLAG) == I2C_FLAG_TRA) || ((FLAG) == I2C_FLAG_BUSY) || \; q7 x- V! j" z- b. P
- ((FLAG) == I2C_FLAG_MSL) || ((FLAG) == I2C_FLAG_SMBALERT) || \
4 m N* q7 z- W$ E. z" G6 r* K - ((FLAG) == I2C_FLAG_TIMEOUT) || ((FLAG) == I2C_FLAG_PECERR) || \
/ ^" P/ b% [: Q( b - ((FLAG) == I2C_FLAG_OVR) || ((FLAG) == I2C_FLAG_AF) || \3 r* J+ s9 K1 y4 L% p
- ((FLAG) == I2C_FLAG_ARLO) || ((FLAG) == I2C_FLAG_BERR) || \7 Z5 r+ E$ d6 ~
- ((FLAG) == I2C_FLAG_TXE) || ((FLAG) == I2C_FLAG_RXNE) || \. e3 ?! b- o( n W4 c4 U
- ((FLAG) == I2C_FLAG_STOPF) || ((FLAG) == I2C_FLAG_ADD10) || \
! t) { O c* G8 p - ((FLAG) == I2C_FLAG_BTF) || ((FLAG) == I2C_FLAG_ADDR) || \' U5 P A# a4 A; W J$ h) J
- ((FLAG) == I2C_FLAG_SB))
复制代码
+ }! G& z0 b3 q$ i; p03. 相关函数
% V% v* s+ E: H+ Y0 _4 l3 ]- /* Function used to set the I2C configuration to the default reset state *****/
6 [! Q* b1 _9 {, _ - void I2C_DeInit(I2C_TypeDef* I2Cx);
3 K+ w- w- N+ ?9 J$ Y5 m# C
5 U5 q9 H5 \. i. q. p# y/ a- /* Initialization and Configuration functions *********************************/, ^1 Z# _* ?, }, u( A1 N
- void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct);
8 t0 Z5 D) ?9 t! V' u - void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct);
% I; S9 h& ^7 _8 d- M7 o - void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState);. m b4 w2 R6 a1 l7 u
- void I2C_DigitalFilterConfig(I2C_TypeDef* I2Cx, uint16_t I2C_DigitalFilter);8 G3 e; d, d4 A+ ~9 d6 A
- void I2C_AnalogFilterCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
9 Z5 ]; D( a4 @2 }$ A2 F1 y8 ` - void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState);
! @7 @& G; c+ ?3 y! W! K - void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState);7 \7 U+ Y* [" X
- void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, uint8_t I2C_Direction);
& a" M2 y7 J6 c1 s! ` - void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState);3 c) @1 X( s$ o% U: B
- void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint8_t Address);* t! x* [1 @- Z0 U0 O( w/ i! w
- void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
/ e9 o2 u5 d% Q& D) z0 M/ @ - void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);( L2 o1 L( D2 ~" }1 B9 V
- void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);; U5 w$ k2 U* l. a
- void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
1 |: u) H+ z; ~/ e- U3 \- C - void I2C_FastModeDutyCycleConfig(I2C_TypeDef* I2Cx, uint16_t I2C_DutyCycle);/ h' ~/ n& H5 U/ c
- void I2C_NACKPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_NACKPosition);
+ u( P2 s* ]: J9 I* I; F - void I2C_SMBusAlertConfig(I2C_TypeDef* I2Cx, uint16_t I2C_SMBusAlert);! h" P6 E- P% k; V
- void I2C_ARPCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);# T( A/ b/ u, j
6 n" e; _: P3 t! x# T+ v0 K- /* Data transfers functions ***************************************************/
% n* g3 }2 ?0 d( E1 Z; b - void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data);
2 Q0 f+ M/ m+ R - uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx);
# d5 A4 K% t, l9 g
3 O: m6 }& u* g, r3 O$ [+ L1 l- /* PEC management functions ***************************************************/
6 P/ X6 T$ D3 v+ A8 _$ Y: ] - void I2C_TransmitPEC(I2C_TypeDef* I2Cx, FunctionalState NewState);$ v: B/ f6 @/ B" Y' X7 p+ y
- void I2C_PECPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_PECPosition);# @, T m; O( A g
- void I2C_CalculatePEC(I2C_TypeDef* I2Cx, FunctionalState NewState);
" c) J& }1 I/ S8 G - uint8_t I2C_GetPEC(I2C_TypeDef* I2Cx);
5 X, g8 O m" u/ b - / `7 ]# D9 y8 W- b3 N
- /* DMA transfers management functions *****************************************/
: P5 y5 m0 g& c0 m9 v - void I2C_DMACmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
; n2 L) [5 Z- S6 Y( } - void I2C_DMALastTransferCmd(I2C_TypeDef* I2Cx, FunctionalState NewState); m8 ~% |$ v) G; v: o0 b$ g
- 9 w% U! O9 k. |3 Q3 t3 G
- /* Interrupts, events and flags management functions **************************/
/ Z7 G# v) r% Z( W8 @" h+ @/ G - uint16_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register);8 D$ I. K* S0 J. K* m
- void I2C_ITConfig(I2C_TypeDef* I2Cx, uint16_t I2C_IT, FunctionalState NewState);
! C. p& v# v+ y! S. f& O
0 l( [8 ]: q2 W; q7 O- /*
& {' Q7 Z# T; C% a! I" J/ t - ===============================================================================3 u: G; l/ V$ m0 s# d% _/ r3 q
- I2C State Monitoring Functions0 x8 ]9 z4 _' c, I2 j" M8 D
- ===============================================================================6 u- J+ S0 A% E# k5 x
- This I2C driver provides three different ways for I2C state monitoring
/ d9 i: ]: k) ` - depending on the application requirements and constraints:& T, s+ |0 m& D# a% e; g
- + y: e2 S4 u. `5 s* V
2 D6 v/ A* u# x$ r3 ?" }- 1. Basic state monitoring (Using I2C_CheckEvent() function)0 L, V3 @# S) s. i" s3 t+ ?
- -----------------------------------------------------------+ s# U, s% Y& Z7 t: R
- It compares the status registers (SR1 and SR2) content to a given event5 {+ n2 A$ j. Q
- (can be the combination of one or more flags).
5 k0 p2 J. z# a) H1 s2 e - It returns SUCCESS if the current status includes the given flags
7 T( F, x( a6 ^% H) m. U - and returns ERROR if one or more flags are missing in the current status.. R2 y5 u- _( C
% i2 e6 [8 \! e5 o; K- - When to use3 t# @0 h' C# \% t/ {0 }
- - This function is suitable for most applications as well as for startup
# E% F }. G6 Q) J0 t - activity since the events are fully described in the product reference
2 `% _6 U0 i- ?) } - manual (RM0090). w7 t' `. \5 C8 v
- - It is also suitable for users who need to define their own events.5 {* i* N( F- `& e* ^
( O& {2 _/ I7 H4 @( U u- - Limitations
# d Y5 r# R r) B2 H - - If an error occurs (ie. error flags are set besides to the monitored : C+ z" s- x+ C& a( r9 Z" ?
- flags), the I2C_CheckEvent() function may return SUCCESS despite
, p# E8 b. R5 _ x S* Q, W - the communication hold or corrupted real state.
9 [, t, R% L& x2 k; D - In this case, it is advised to use error interrupts to monitor 9 j( o" X3 ?6 e8 ~5 j) C( m" E
- the error events and handle them in the interrupt IRQ handler.0 |* J( Y; I: M" k
- % t3 {6 {; E) Q: d
- Note / k' x5 r7 L; f0 M+ U. L9 R
- For error management, it is advised to use the following functions:- ^7 J* v/ ]( z8 n# I
- - I2C_ITConfig() to configure and enable the error interrupts (I2C_IT_ERR).
% a: V% v7 A* ], M! M - - I2Cx_ER_IRQHandler() which is called when the error interrupt occurs.# s/ T. B8 z+ K- n3 b) c7 _. \
- Where x is the peripheral instance (I2C1, I2C2 ...)
5 o, S! u& X+ z& ^ C! C - - I2C_GetFlagStatus() or I2C_GetITStatus() to be called into the
" h* j. B$ ?2 z# M% p! _ - I2Cx_ER_IRQHandler() function in order to determine which error occurred.$ m$ M( `* j8 _/ S) n: p$ w
- - I2C_ClearFlag() or I2C_ClearITPendingBit() and/or I2C_SoftwareResetCmd()
/ V5 ]8 P! A$ d - and/or I2C_GenerateStop() in order to clear the error flag and source
5 Q8 X" ~4 O" P+ t/ p# o" K - and return to correct communication status.* E% S$ i) `2 O I3 w" M {
- - B$ {" K" X- {4 i6 B
- ) }0 m) ]& D; c1 O {/ g
- 2. Advanced state monitoring (Using the function I2C_GetLastEvent())
]5 H, a1 h. F( I; ?" l - --------------------------------------------------------------------
' M% v7 E0 ?9 \+ F+ T' n. w - Using the function I2C_GetLastEvent() which returns the image of both status
- n. T6 k+ E/ |; \ M, H - registers in a single word (uint32_t) (Status Register 2 value is shifted left ) U- q, I' o' u" L3 k) ~" v
- by 16 bits and concatenated to Status Register 1).
$ F+ W& r/ u4 t$ B _ - : i3 Q& {6 d6 q! c( S, i6 T
- - When to use
+ Y6 y% b; U" i* W+ W5 `# A - - This function is suitable for the same applications above but it - I$ B2 l" y" {* h B) x: p' W9 ?& I
- allows to overcome the mentioned limitation of I2C_GetFlagStatus() # M; x# R* y( [) A$ g0 k% Y
- function.! i8 ~6 B: {- Q- a- {
- - The returned value could be compared to events already defined in
' r' Q: K1 e2 V. K/ m& R$ h ? - this file or to custom values defined by user.+ h% O% \4 P, j2 n2 Q
- This function is suitable when multiple flags are monitored at the 1 I; o) F3 L; ^8 E, M
- same time.
# w: Z$ l, R$ B - - At the opposite of I2C_CheckEvent() function, this function allows
) u+ ?$ u6 W! d, x - user to choose when an event is accepted (when all events flags are
2 _8 k7 K4 z- H+ H. ]2 O - 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 - like I2C_CheckEvent() function.0 K4 U* p+ F& H0 U" ~3 A4 X
( H. N+ L1 b- {7 H2 `% g- - Limitations
0 A1 S) z; p5 I# q4 m' H- I8 Q - - User may need to define his own events.# \4 n& ?( z3 `3 u$ p9 ?
- - Same remark concerning the error management is applicable for this
1 ], x5 T1 Q q @& Z. h - function if user decides to check only regular communication flags " L3 A/ x C1 P5 H) |# A6 }) A
- (and ignores error flags).
5 D W# s* U% I) d( B% J u - 3 f0 T* ]; y* F/ G0 ], z
- & U; b/ {. J& R' i' e5 s% c- E9 U8 E
- 3. Flag-based state monitoring (Using the function I2C_GetFlagStatus())
0 S3 u: N2 C. W5 q- ?4 o, F) R - -----------------------------------------------------------------------0 w% Q) y0 X( }1 d7 j/ z3 r$ }
( N1 ?. \, }8 S+ h; a' n" o/ K3 e- Using the function I2C_GetFlagStatus() which simply returns the status of
4 O$ c- O) B* q* Q% F/ Q* [* T - one single flag (ie. I2C_FLAG_RXNE ...). 7 H# q9 ]- s% `# z- ?) t& n4 o
5 C7 _( z' \9 _3 Z, X6 [2 }- - When to use$ i$ S5 v! B; r
- - This function could be used for specific applications or in debug
% |) b/ M6 G, |' J! U2 q - phase.7 r$ `1 v- }4 [
- - It is suitable when only one flag checking is needed (most I2C * [+ ]2 H# p1 l1 ]
- events are monitored through multiple flags)." S' [: ^) C7 A" k- V
- - Limitations: 8 f9 i" ~2 ]" E! t \' ]% v4 O
- - When calling this function, the Status register is accessed.
6 H! x& o, x" ^) u - Some flags are cleared when the status register is accessed.
0 j7 B2 A% _# d; ^# D+ ]7 [ - So checking the status of one Flag, may clear other ones.
9 D( O$ S7 F6 L( P$ N8 C. G; _ - - 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 - one single event.
4 Z, K8 i9 z# G1 p - */
4 L- @4 B' G6 `2 H9 E' D+ T; a, ] - ( N) W3 ?2 I. [+ D& g
- /*
6 Q" [. G5 b1 B! h6 h. q# j - ===============================================================================2 T8 w1 A: y/ d9 E( j1 S
- 1. Basic state monitoring
% D) a' e) v! l/ u0 k; r5 X - ===============================================================================$ |; V8 D9 D" F2 ~1 G7 @7 ^
- */( u& ]5 s; h4 }/ X# P+ H' U) d
- ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT);
4 O" f8 [# m" g. T. x- ^' W+ Q - /** Z" F4 s" k% m+ ^9 t. ^ |
- ===============================================================================$ Y( ?" i3 @- `! |% ]0 G
- 2. Advanced state monitoring$ m o8 W' j# g! H' I
- ===============================================================================
- V) e7 d* l: t" G - */ _1 T: } j4 D
- uint32_t I2C_GetLastEvent(I2C_TypeDef* I2Cx);% Y& c! L' Q, h, Q3 Y0 l, M# a6 q# S
- /*
% r. o3 _$ K( a2 f. ` - ===============================================================================
% D9 _8 U1 [, ?$ K+ Q% v& C - 3. Flag-based state monitoring/ X1 G$ u/ f, F1 P- d
- ===============================================================================
; W+ q7 R7 k: w% C9 B) f - */5 a" A% y9 J) O% N2 Z
- FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);2 A* H; v0 M# }. ~7 ]8 n% r! s2 \3 h
- 9 S# ~+ P* V! S1 s1 U' s' {
- 2 R0 I b# c9 J/ m+ ]9 p
- void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);/ A6 h, S7 o+ C2 [
- ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT);
* i4 w0 j7 k1 x) c. G - 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
- /** ( M; \% c V3 Q' b
- * @brief Inter-integrated Circuit Interface E1 g! y- w8 P# b+ o
- */
" |# Q9 }" P$ t7 e! E |$ F" K3 t0 Y - + D0 Y' w# z% d) J: ]
- typedef struct% T6 q3 u) z3 S: ]# ]0 P8 w
- {
) O/ x4 g* \8 z9 ` - __IO uint16_t CR1; /*!< I2C Control register 1, Address offset: 0x00 */
/ r+ e( L* @( m$ ~$ x3 j9 A - uint16_t RESERVED0; /*!< Reserved, 0x02 */ `4 O0 l9 y2 t2 M4 J* X
- __IO uint16_t CR2; /*!< I2C Control register 2, Address offset: 0x04 */& i: a% F+ q- w
- uint16_t RESERVED1; /*!< Reserved, 0x06 */7 b2 K" p) o7 c) C% t
- __IO uint16_t OAR1; /*!< I2C Own address register 1, Address offset: 0x08 */
7 t% p! D4 o/ U - uint16_t RESERVED2; /*!< Reserved, 0x0A */
2 t5 E+ ]! r& i6 ~0 p2 h - __IO uint16_t OAR2; /*!< I2C Own address register 2, Address offset: 0x0C */* r" P+ c6 H8 I1 T8 l
- uint16_t RESERVED3; /*!< Reserved, 0x0E */
; q+ J- m3 o) V+ _ - __IO uint16_t DR; /*!< I2C Data register, Address offset: 0x10 */: }4 ~" O. I) X' \5 p& s
- uint16_t RESERVED4; /*!< Reserved, 0x12 */
# c7 r6 o& y0 |( A4 R - __IO uint16_t SR1; /*!< I2C Status register 1, Address offset: 0x14 */
4 D' E( b( N5 N* U0 ?8 r5 | - uint16_t RESERVED5; /*!< Reserved, 0x16 */* H: C; d' T/ ^3 G
- __IO uint16_t SR2; /*!< I2C Status register 2, Address offset: 0x18 */- q. g- s* d5 w6 D% S) h
- uint16_t RESERVED6; /*!< Reserved, 0x1A */ a; h" A2 u4 \) {& W
- __IO uint16_t CCR; /*!< I2C Clock control register, Address offset: 0x1C */* v$ j! n( I, o) m7 c( G
- uint16_t RESERVED7; /*!< Reserved, 0x1E */4 I( q! a: ~, d1 H
- __IO uint16_t TRISE; /*!< I2C TRISE register, Address offset: 0x20 *// {* J3 X4 Q0 J- F' G
- uint16_t RESERVED8; /*!< Reserved, 0x22 */
+ n( v1 s4 G# p7 G$ A h* j4 o - __IO uint16_t FLTR; /*!< I2C FLTR register, Address offset: 0x24 */
: I8 G; U8 W+ x5 c+ k v - uint16_t RESERVED9; /*!< Reserved, 0x26 */
: w8 L/ |% z" H) Q% A - } 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 |