01. I2C简介4 {4 I+ N0 g: C9 M' S+ } V
I2C(内部集成电路)总线接口用作微控制器和 I2C 串行总线之间的接口。它提供多主模式功能,可以控制所有I2C总线特定的序列、协议、仲裁和时序。它支持标准和快速模式。它还与 SMBus 2.0 兼容。
0 A5 o" l3 q2 R
' A' M" j# K d5 A4 d1 s2 v# ^6 T它可以用于多种用途,包括 CRC 生成和验证、SMBus(系统管理总线)以及 PMBus(电源管理总线)。
/ p9 D2 k5 j& U/ f u0 r: K2 u0 g4 c' K
根据器件的不同,可利用 DMA 功能来减轻 CPU 的工作量。
! y# c/ ^1 Z. l6 P' f
0 P5 X" J6 C4 D. b02. 相关类型6 O* ]% L* Z2 Y4 X* Z3 h0 ?
I2C Init structure
# g$ G: w) W4 ^1 ?9 H/ z/ Q& ]- @' `" g, X
- /**
. x( R1 J/ A# F ?5 I - * @brief I2C Init structure definition
8 r3 r* c. ?) ~% e" W% c0 ~ - */
4 H, ^% k" y& m4 l$ z( q3 l
# K& o# K+ N2 U4 m+ Y* F+ ^- typedef struct7 [7 t! A; v7 l9 R" N6 l2 H
- {
2 m* V5 h6 W1 Q0 p. B' @) x - uint32_t I2C_ClockSpeed; /*!< Specifies the clock frequency.
* |# j. D! Y7 _% @ - This parameter must be set to a value lower than 400kHz */
$ W+ c1 s. B' [' ^9 k# j2 d
* u- v! b( f7 h- uint16_t I2C_Mode; /*!< Specifies the I2C mode.
% L4 ?7 U% l t7 c4 H, Q$ X" C - This parameter can be a value of @ref I2C_mode */
- b8 x- M/ l5 ]' x' ~# f" l" | - ; G6 v. C% Q* b" f* k0 p- }8 b
- uint16_t I2C_DutyCycle; /*!< Specifies the I2C fast mode duty cycle.
+ E+ M9 G9 ~( K' e, \* u - This parameter can be a value of @ref I2C_duty_cycle_in_fast_mode *// H: S$ e m7 u5 W/ N. P
% Q* R9 A% X3 _& k) ]- G- uint16_t I2C_OwnAddress1; /*!< Specifies the first device own address.
& ^+ i+ ^6 m+ c) o$ a - This parameter can be a 7-bit or 10-bit address. */
6 x8 D; m' X$ c! Q
: z( B' L+ i, J5 R7 [- uint16_t I2C_Ack; /*!< Enables or disables the acknowledgement.+ F! c9 O9 N0 g( z# L. y" @
- This parameter can be a value of @ref I2C_acknowledgement */
- U6 }) L1 u/ z' F4 ] - ' x! G& Y$ ?8 h6 j C
- uint16_t I2C_AcknowledgedAddress; /*!< Specifies if 7-bit or 10-bit address is acknowledged.; k+ i0 a2 \$ e$ P' [6 l* q
- This parameter can be a value of @ref I2C_acknowledged_address */
* f" T4 L* Y1 i& D/ G - }I2C_InitTypeDef;
复制代码
+ }! b" x( F y* W) T/ {" K E1 EI2C_Exported_Constants# [0 f# }* Q) g) p
. h: [* q4 i7 A5 R4 m. B! I' I4 A
- M( d' Y; j0 J5 K; Y$ [
- /** @defgroup I2C_Exported_Constants
' D+ a5 A# P" G8 @ - * @{, u, N6 T0 y, W3 y. l. n/ z
- */* _( A5 p% @4 E e$ r Y, G) R
- #define IS_I2C_ALL_PERIPH(PERIPH) (((PERIPH) == I2C1) || \
$ k4 w: i+ y3 z - ((PERIPH) == I2C2) || \
6 i) _! e! O1 ~ - ((PERIPH) == I2C3))
: ^2 S3 {, q% Q( x - /** @defgroup I2C_Digital_Filter" D+ R; ]4 U! z U2 Z
- * @{2 I. E+ M9 c. _1 c$ K) p/ y7 N
- */ n* e! s# |/ k5 ^( j* O8 `
& M" m$ d. h3 ~. Z! M, u( I% X1 g- #define IS_I2C_DIGITAL_FILTER(FILTER) ((FILTER) <= 0x0000000F)4 S; X) ~# u( L: I
- /**3 j# J- q: d, M* B, F
- * @}
" p8 q' q1 Q8 k* ?/ {: C - */
复制代码
4 D& v" n4 ~" F1 @& Z( {I2C_mode- /** @defgroup I2C_mode 5 h* U" I! b% D- t
- * @{5 K+ D% z; f, x# }4 l2 L2 `) K$ I
- */. `& W' Y. g( l& h8 L
& K% H' e' b% v9 Z+ B) U, U- #define I2C_Mode_I2C ((uint16_t)0x0000)
# J- [$ X7 r6 {! E8 ~$ s - #define I2C_Mode_SMBusDevice ((uint16_t)0x0002)
& n& ~3 [' ?# y - #define I2C_Mode_SMBusHost ((uint16_t)0x000A)
& C/ G" V5 Z8 e, W - #define IS_I2C_MODE(MODE) (((MODE) == I2C_Mode_I2C) || \! R7 s% ]+ ]! t9 s6 b3 i0 [
- ((MODE) == I2C_Mode_SMBusDevice) || \. ]# z' n) j0 L
- ((MODE) == I2C_Mode_SMBusHost))
复制代码 , _8 ~0 ^7 f; c$ R
I2C_duty_cycle_in_fast_mode
1 Y, h7 \: S5 N4 _% J: _" g0 `8 l3 i ~ V- L4 p' v1 [' Q
- /** @defgroup I2C_duty_cycle_in_fast_mode
; b0 r3 ~3 j" v% n - * @{3 x i1 v% U# P# ^7 ]; n' Z
- */
5 `% b J" D! i - . @ S, z; k: b* j7 U) G
- #define I2C_DutyCycle_16_9 ((uint16_t)0x4000) /*!< I2C fast mode Tlow/Thigh = 16/9 */) W) H& s# j) @( L- p
- #define I2C_DutyCycle_2 ((uint16_t)0xBFFF) /*!< I2C fast mode Tlow/Thigh = 2 *// t, \. `' @- C! C# [
- #define IS_I2C_DUTY_CYCLE(CYCLE) (((CYCLE) == I2C_DutyCycle_16_9) || \
- }3 h9 d7 B7 t5 M - ((CYCLE) == I2C_DutyCycle_2))
复制代码
" U, e* b3 n/ ~$ N" n' eI2C_acknowledgement
4 I0 Y* h) @. K" G: k9 y& c( O: T! c) e( {9 i! j
- /** @defgroup I2C_acknowledgement- M* t$ w- i; q5 i* G6 p# h
- * @{6 N) ~. J G' G- j& q2 P, X
- */$ o" l* @( M$ N3 T5 F- g
! p- H, G; U2 o' |- w; I, I- #define I2C_Ack_Enable ((uint16_t)0x0400)
' y$ _. A9 N; b6 V/ G l: @, _; a) L - #define I2C_Ack_Disable ((uint16_t)0x0000)
% m& S u7 _3 h6 t+ u - #define IS_I2C_ACK_STATE(STATE) (((STATE) == I2C_Ack_Enable) || \6 }$ ^3 @& D( G2 D, @
- ((STATE) == I2C_Ack_Disable))
复制代码 / S4 r# {7 @, y0 K
I2C_transfer_direction
/ a: x$ Z- {4 H% m* F) F9 z
: ^2 I- K$ u z3 j- /** @defgroup I2C_transfer_direction 4 m8 N: l) ^" q! l% V8 V: f
- * @{
! f# U( `' m- M1 H - */4 A. o# q7 h' c& O+ i
- 4 J2 r" o& m7 G& P
- #define I2C_Direction_Transmitter ((uint8_t)0x00)( j( \+ v% @8 t; c7 _( M+ ]
- #define I2C_Direction_Receiver ((uint8_t)0x01)) l4 [! A' f& X$ C1 s
- #define IS_I2C_DIRECTION(DIRECTION) (((DIRECTION) == I2C_Direction_Transmitter) || \. Z. j2 f r: N
- ((DIRECTION) == I2C_Direction_Receiver))
复制代码
2 m+ Z3 V6 ^$ |% N5 HI2C_acknowledged_address5 O7 }* N! ^) k1 X
% V3 r5 i& q$ n9 r; M4 }3 w4 o- /** @defgroup I2C_acknowledged_address
0 B: z9 A; g% T4 C% M. E, R1 ~6 @ - * @{
9 v# k# S8 k2 b& C0 q8 ~ - */
& E5 T7 k# w- m! N$ Z - * W/ c8 ]- _( L
- #define I2C_AcknowledgedAddress_7bit ((uint16_t)0x4000)
6 l6 n$ v: h4 M4 F* a - #define I2C_AcknowledgedAddress_10bit ((uint16_t)0xC000)
) a9 F" v& g+ T: c% w' o* J4 U - #define IS_I2C_ACKNOWLEDGE_ADDRESS(ADDRESS) (((ADDRESS) == I2C_AcknowledgedAddress_7bit) || \
2 Q' `# F( ?9 u$ w. e0 }6 N - ((ADDRESS) == I2C_AcknowledgedAddress_10bit))
复制代码
/ ?: A) b$ l& @8 e/ y9 F8 fI2C_registers
# W ]$ n& ~! B( s& }1 l. `2 w# U( K0 g5 x+ d/ |9 l( E* p
- /** @defgroup I2C_registers 2 w! k6 I& e# Z0 S
- * @{5 o7 N/ l& V6 C' f; b2 `3 o
- */
* y { {6 e/ }3 w5 ]5 @9 r - 7 F: h+ s' X; M+ b# R
- #define I2C_Register_CR1 ((uint8_t)0x00)
: L* [5 Q5 Q3 _% \) n/ G9 t, p - #define I2C_Register_CR2 ((uint8_t)0x04)+ {' R4 A4 t; Z; @
- #define I2C_Register_OAR1 ((uint8_t)0x08)/ H @6 d: \# s
- #define I2C_Register_OAR2 ((uint8_t)0x0C)- P7 O" e4 R' s- U0 N/ r
- #define I2C_Register_DR ((uint8_t)0x10)7 N" ?& K% S: v. G9 x( |
- #define I2C_Register_SR1 ((uint8_t)0x14)
, `( ~" F2 F3 i' E& k2 V - #define I2C_Register_SR2 ((uint8_t)0x18)
$ q( t+ ~5 `5 [4 Z# E, e2 M - #define I2C_Register_CCR ((uint8_t)0x1C)
" k ^5 a' R1 I: a' m1 w - #define I2C_Register_TRISE ((uint8_t)0x20)
0 w) @! C& O, u$ R7 X - #define IS_I2C_REGISTER(REGISTER) (((REGISTER) == I2C_Register_CR1) || \3 E. d! ~7 J( R$ W( |
- ((REGISTER) == I2C_Register_CR2) || \& H3 p7 k8 e0 K3 D$ e
- ((REGISTER) == I2C_Register_OAR1) || \9 O5 ^" R" w. f9 S' {6 M
- ((REGISTER) == I2C_Register_OAR2) || \
1 `" \! n' d1 ]. N4 j2 Q3 t% t - ((REGISTER) == I2C_Register_DR) || \
0 h4 n+ b `( k$ `( x - ((REGISTER) == I2C_Register_SR1) || \) I- S* p+ J- l* p% \
- ((REGISTER) == I2C_Register_SR2) || \- L: f3 c% w" G% D
- ((REGISTER) == I2C_Register_CCR) || \
; Y+ Y0 U7 g: O8 C8 u! B( x0 ~, r2 R - ((REGISTER) == I2C_Register_TRISE))
复制代码 N4 s$ ]) v6 y9 j1 Q
I2C_NACK_position* ~& D- ^% \) k1 n: D. O
; |7 Q( z5 X: C, ]
- /** @defgroup I2C_NACK_position
6 D) N; b1 G* N0 V/ ~! ] - * @{
0 n. q `; B X8 m! m7 S8 o" W - */9 u* z' Y/ S2 q" r% h
_' Q$ Z, o1 c( ~! [4 R$ R9 {! M2 z+ O- #define I2C_NACKPosition_Next ((uint16_t)0x0800)
% R+ R/ E8 E5 m( U; y, z$ L - #define I2C_NACKPosition_Current ((uint16_t)0xF7FF)
# d1 {- {$ b5 _7 v7 I5 F6 F! t3 \, @& g' O - #define IS_I2C_NACK_POSITION(POSITION) (((POSITION) == I2C_NACKPosition_Next) || \
" [5 S5 I& _9 F, Z4 @; ? - ((POSITION) == I2C_NACKPosition_Current))
复制代码
3 T7 X, G* _$ Q( tI2C_SMBus_alert_pin_level: T7 f" k! ^) }" H
' z0 p# U& w: V2 W/ H6 o* O& N X% S
- /** @defgroup I2C_SMBus_alert_pin_level % F& ?- w- @, P* b
- * @{( `) F8 C. c3 j! S* d
- */( r2 Q% l# L5 b v s
- , ?. ]& r; I/ W
- #define I2C_SMBusAlert_Low ((uint16_t)0x2000)
7 ~, ~! H. c$ H6 T - #define I2C_SMBusAlert_High ((uint16_t)0xDFFF)
8 J" N( O5 C* i7 [* v8 D - #define IS_I2C_SMBUS_ALERT(ALERT) (((ALERT) == I2C_SMBusAlert_Low) || \
8 A8 O3 t8 Y9 M - ((ALERT) == I2C_SMBusAlert_High))
复制代码
1 q7 u }, v! a7 R1 C" W8 |I2C_PEC_position5 e; t8 V5 ^- A1 }
/ `: X8 B) B+ | {
- /** @defgroup I2C_PEC_position
: M' n4 b# W$ k/ Q- V: A: ` - * @{
$ z8 T) ]+ W r+ l1 _ - */
# `6 ]5 ^* p' k7 o0 N - : }; T7 n" f; y$ T5 v4 u
- #define I2C_PECPosition_Next ((uint16_t)0x0800)2 V. ?' o8 H: Y9 ]$ q
- #define I2C_PECPosition_Current ((uint16_t)0xF7FF)
" I; `: a" f3 h; I$ Y - #define IS_I2C_PEC_POSITION(POSITION) (((POSITION) == I2C_PECPosition_Next) || \0 I8 t! T/ Y( ~$ T" U; B
- ((POSITION) == I2C_PECPosition_Current))
复制代码 ' A7 B% ]3 c. M
I2C_interrupts_definition6 @) G6 T! l, d; R: J9 C
; _5 }- ]' | m+ B0 I; |2 b j% h- /** @defgroup I2C_interrupts_definition ' S" J! ^& z8 t, k" x8 Z' ~
- * @{0 L9 _/ e& G& u9 S. j
- */" `8 K2 v7 E8 z+ l v
& M# y3 v- ]. L) A( y- #define I2C_IT_BUF ((uint16_t)0x0400)
* D- R2 c$ \0 | X - #define I2C_IT_EVT ((uint16_t)0x0200)2 u# t) A% ]4 h/ _ O
- #define I2C_IT_ERR ((uint16_t)0x0100)
/ F* @1 ^- g! y3 H( o" q - #define IS_I2C_CONFIG_IT(IT) ((((IT) & (uint16_t)0xF8FF) == 0x00) && ((IT) != 0x00))
复制代码
5 s' \! H% S8 j/ m1 X5 GI2C_interrupts_definition0 [. r! H( W. h( I6 j+ x4 P
- q! y( a* `* e: f& ~1 o- /** @defgroup I2C_interrupts_definition
" j+ ]% ^8 l8 H n5 W* Z- i1 V - * @{& B& d9 _9 @1 x
- */
/ h! @4 n: R, Y& N* K6 e
8 \& F- |1 D; P# k% i" J# x) ^- #define I2C_IT_SMBALERT ((uint32_t)0x01008000)
$ a" D% h8 W' q* i- j: r0 ~- m' J - #define I2C_IT_TIMEOUT ((uint32_t)0x01004000)
6 k, u5 E" y+ E. k" J7 a - #define I2C_IT_PECERR ((uint32_t)0x01001000)3 H* {9 {. o) j) B
- #define I2C_IT_OVR ((uint32_t)0x01000800)
0 D5 _9 W0 M0 M0 b - #define I2C_IT_AF ((uint32_t)0x01000400)
- Z# ~2 r h# o; H) O* \ - #define I2C_IT_ARLO ((uint32_t)0x01000200)
5 P( v* R; p1 v- D k; E - #define I2C_IT_BERR ((uint32_t)0x01000100)
y2 e0 a2 q' s0 F6 b o9 ~/ a - #define I2C_IT_TXE ((uint32_t)0x06000080)
; S% M) Z: _, G0 \ - #define I2C_IT_RXNE ((uint32_t)0x06000040)
7 H* ?8 b; S# W& n - #define I2C_IT_STOPF ((uint32_t)0x02000010)
% q3 b& ~1 M( S! A' n- T* Q - #define I2C_IT_ADD10 ((uint32_t)0x02000008)
, s& t' I; c% H( i - #define I2C_IT_BTF ((uint32_t)0x02000004); ]$ b5 B. }( A4 A/ L/ |! u
- #define I2C_IT_ADDR ((uint32_t)0x02000002)
8 T" f6 T; l8 L, k - #define I2C_IT_SB ((uint32_t)0x02000001)
& Z. B/ v( x" C8 Q! \4 M - 8 e6 j/ E" g2 b, E5 I- Q0 H7 ]& @
- #define IS_I2C_CLEAR_IT(IT) ((((IT) & (uint16_t)0x20FF) == 0x00) && ((IT) != (uint16_t)0x00))
+ |1 @( s& u4 l7 V! ^ - , Y) \' ~/ b; d {
- #define IS_I2C_GET_IT(IT) (((IT) == I2C_IT_SMBALERT) || ((IT) == I2C_IT_TIMEOUT) || \
& ]1 R, \& j- h* m6 z5 x - ((IT) == I2C_IT_PECERR) || ((IT) == I2C_IT_OVR) || \
2 z [$ j5 f6 W4 X/ h: Y - ((IT) == I2C_IT_AF) || ((IT) == I2C_IT_ARLO) || \( h8 n; J3 P0 J6 o% I6 Z
- ((IT) == I2C_IT_BERR) || ((IT) == I2C_IT_TXE) || \
: f2 Q2 e8 x! f8 Q8 ] - ((IT) == I2C_IT_RXNE) || ((IT) == I2C_IT_STOPF) || \/ ], F: _- X+ L7 y/ e4 G$ s
- ((IT) == I2C_IT_ADD10) || ((IT) == I2C_IT_BTF) || \
7 |7 W4 i$ V, q7 Q- t - ((IT) == I2C_IT_ADDR) || ((IT) == I2C_IT_SB))
复制代码
0 I- H' z4 J) H" RSR2 register flags7 P5 x3 _8 [, J
' Y& k8 d% V% |- G# @6 `4 B- /**
, }- j, v% f5 o% j6 w+ Q j - * @brief SR2 register flags ) I+ I* h8 @/ a# c% A
- */
3 N6 C t2 X. j" e& X4 z - ! c: V! T5 r) A' B
- #define I2C_FLAG_DUALF ((uint32_t)0x00800000)
! ~8 S# `& w" e - #define I2C_FLAG_SMBHOST ((uint32_t)0x00400000)
5 u4 Z3 p$ F' o# M, {" w, ~ - #define I2C_FLAG_SMBDEFAULT ((uint32_t)0x00200000)1 k# X7 j# r. y" w. o/ ~ c' w( L
- #define I2C_FLAG_GENCALL ((uint32_t)0x00100000)& P* U! Y( i" K1 k0 }
- #define I2C_FLAG_TRA ((uint32_t)0x00040000)& a/ y7 w% R5 ~, d& S) T( A) e
- #define I2C_FLAG_BUSY ((uint32_t)0x00020000)
3 o8 z5 P8 I+ _# e) j5 h# | - #define I2C_FLAG_MSL ((uint32_t)0x00010000)
复制代码
4 y% E1 j& Q' GSR1 register flags2 X/ h* _! ]) A' J6 F( x
: W6 E! s I( D* D s/ n- /** 0 Q; g2 ?! j: E1 y0 _& z* B
- * @brief SR1 register flags ! a4 @! ?9 v" F
- */. t4 w& L4 J% Y! `# D" }/ k
. ~- c, f) c. p! n4 ^2 ^7 }- #define I2C_FLAG_SMBALERT ((uint32_t)0x10008000)# B+ y9 Z! i! q5 P9 l
- #define I2C_FLAG_TIMEOUT ((uint32_t)0x10004000)$ {7 S$ ]3 k- e" n
- #define I2C_FLAG_PECERR ((uint32_t)0x10001000)
9 `' q8 w7 n( m - #define I2C_FLAG_OVR ((uint32_t)0x10000800)
- Z/ r6 X: C7 m- \ - #define I2C_FLAG_AF ((uint32_t)0x10000400)
" ^; @; |% a) P- L: k% t' ^. }0 c - #define I2C_FLAG_ARLO ((uint32_t)0x10000200)2 f& |* {* T( p, k$ J) ]+ E
- #define I2C_FLAG_BERR ((uint32_t)0x10000100)) Q, w& U, P, L+ S- P8 d
- #define I2C_FLAG_TXE ((uint32_t)0x10000080)
) a) T1 R4 @) l q5 I3 o- g8 r- [ - #define I2C_FLAG_RXNE ((uint32_t)0x10000040)
- J; M6 B% y3 s! o3 S+ E - #define I2C_FLAG_STOPF ((uint32_t)0x10000010)- p5 f' @: B3 o: K
- #define I2C_FLAG_ADD10 ((uint32_t)0x10000008)( C% K# V2 H+ h# u& X; K: a. S( _0 M
- #define I2C_FLAG_BTF ((uint32_t)0x10000004)# N: P- c; U C) g/ e
- #define I2C_FLAG_ADDR ((uint32_t)0x10000002)
) l$ F6 j1 Y- G* S* g - #define I2C_FLAG_SB ((uint32_t)0x10000001)
6 l! c+ q; _& l0 F6 i4 q" Y
4 P& o7 k7 L8 u& g/ [& v* k- #define IS_I2C_CLEAR_FLAG(FLAG) ((((FLAG) & (uint16_t)0x20FF) == 0x00) && ((FLAG) != (uint16_t)0x00))
4 ?7 R0 u0 ]* F9 F
$ |7 K7 w# t# X% x' }* ]8 S- #define IS_I2C_GET_FLAG(FLAG) (((FLAG) == I2C_FLAG_DUALF) || ((FLAG) == I2C_FLAG_SMBHOST) || \2 f& m( b# \3 `3 x: M
- ((FLAG) == I2C_FLAG_SMBDEFAULT) || ((FLAG) == I2C_FLAG_GENCALL) || \
5 g2 X* X5 x6 M0 g! x* I( ? - ((FLAG) == I2C_FLAG_TRA) || ((FLAG) == I2C_FLAG_BUSY) || \
( ]. M, c6 q8 H - ((FLAG) == I2C_FLAG_MSL) || ((FLAG) == I2C_FLAG_SMBALERT) || \3 C! `' J/ S# h+ n. \# m2 F0 S1 l
- ((FLAG) == I2C_FLAG_TIMEOUT) || ((FLAG) == I2C_FLAG_PECERR) || \
# K5 ]9 @# B, s2 H - ((FLAG) == I2C_FLAG_OVR) || ((FLAG) == I2C_FLAG_AF) || \
* j1 Q/ j# n+ T: w5 n" l - ((FLAG) == I2C_FLAG_ARLO) || ((FLAG) == I2C_FLAG_BERR) || \
6 V4 G0 [$ z8 {% k. X6 I - ((FLAG) == I2C_FLAG_TXE) || ((FLAG) == I2C_FLAG_RXNE) || \5 h E+ j1 a; Y! `
- ((FLAG) == I2C_FLAG_STOPF) || ((FLAG) == I2C_FLAG_ADD10) || \$ G7 B) l" n j, u% p
- ((FLAG) == I2C_FLAG_BTF) || ((FLAG) == I2C_FLAG_ADDR) || \ k- `% @4 f$ P( s4 B
- ((FLAG) == I2C_FLAG_SB))
复制代码
" Q, R* g! ~3 }03. 相关函数
* {) p6 n" n5 \9 o( ]- /* Function used to set the I2C configuration to the default reset state *****/
/ Z" l. Q, @2 y+ W - void I2C_DeInit(I2C_TypeDef* I2Cx);4 `+ b: T; ]/ K6 D0 ^" j; I
- # f6 x- S1 r8 {1 o3 w
- /* Initialization and Configuration functions *********************************/
! K1 P$ L0 w. T% C. P, B - void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct);
/ }9 K3 ^( J, ~7 ~* b% M8 m/ A - void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct);3 ^2 @% Z$ ]' p7 V7 B
- void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState);& S0 g, c; K1 H) U7 [6 R
- void I2C_DigitalFilterConfig(I2C_TypeDef* I2Cx, uint16_t I2C_DigitalFilter);
S: a0 [& M' u4 E' \ - void I2C_AnalogFilterCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
1 A4 b* f3 X3 g1 x' J! e6 ?& n6 C! C - void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState);" I- a. n1 U S; M- W
- void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState);
7 p7 `: W- ?0 t0 k - void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, uint8_t I2C_Direction);
. h+ T& b0 h& n7 r - void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState);
8 t( B$ L' \* C7 f s$ k2 Z - void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint8_t Address);
/ f3 h9 Y' R& H( V8 w6 H - void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
7 R' d! |% U/ {9 z - void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);+ c* a9 {& B v' \6 X) Z
- void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);- V+ Y2 E) U4 W+ F
- void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState); K/ W! ] G3 R2 Z1 s" l
- void I2C_FastModeDutyCycleConfig(I2C_TypeDef* I2Cx, uint16_t I2C_DutyCycle);
4 D/ |% S7 H, _ V( S - void I2C_NACKPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_NACKPosition);
; @2 b6 i X- x - void I2C_SMBusAlertConfig(I2C_TypeDef* I2Cx, uint16_t I2C_SMBusAlert);
3 }% E: E4 A- x/ i+ ]& I - void I2C_ARPCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
4 `: F. {& c; N) X
]+ F$ u- z+ J1 T8 ]& J+ P- /* Data transfers functions ***************************************************/
& ~- H0 G! D1 \8 z - void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data);
8 ~: F" ?; G; v v4 \" J - uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx);
. l( b# [, i z" L - 7 f' n" }% \% T0 x6 t3 Z+ {
- /* PEC management functions ***************************************************/
& g! I3 T+ p; ~. f - void I2C_TransmitPEC(I2C_TypeDef* I2Cx, FunctionalState NewState);
( \- @# C: B: q - void I2C_PECPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_PECPosition);2 R) j8 T8 g* L/ b8 v$ \+ A
- void I2C_CalculatePEC(I2C_TypeDef* I2Cx, FunctionalState NewState);! ]2 m4 h' ^6 e- R
- uint8_t I2C_GetPEC(I2C_TypeDef* I2Cx);# T5 E3 _* x; Y# _) F3 ~. q3 e
6 O, I; n& A6 N) Z# f! k6 ]- /* DMA transfers management functions *****************************************/
" V( a+ R1 }' s2 t% J - void I2C_DMACmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
. |2 J0 h. _1 a: u. ]3 v - void I2C_DMALastTransferCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
& i, z) {* a' Q6 e
$ d5 h; h. h' Y9 U6 Q8 R1 q9 s) }3 u- /* Interrupts, events and flags management functions **************************// ]7 |0 c. E. ]* F! Y$ g+ o. n# i6 ]
- uint16_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register);/ B3 U9 I! p$ }% Q3 x
- void I2C_ITConfig(I2C_TypeDef* I2Cx, uint16_t I2C_IT, FunctionalState NewState);
3 m% f" O' p8 r% t* r5 w- y - , h" r, l) g* y1 R( ]* {8 C8 l
- /* 3 f! H& h6 B9 L- H
- ===============================================================================
/ L3 _+ [+ a: n4 K: { - I2C State Monitoring Functions
9 H$ e: U5 H6 @/ @ - ===============================================================================
& J& p- P4 j- r( ~7 k5 B - This I2C driver provides three different ways for I2C state monitoring
' @& `# i3 x9 G2 H) B - depending on the application requirements and constraints:
# X, N- M- s- |4 } - 1 d/ c& {: W- k) M4 W8 j! k
$ m* h J5 D; S$ p7 t- 1. Basic state monitoring (Using I2C_CheckEvent() function); `; |! N* B4 k' C/ u
- -----------------------------------------------------------
3 | D4 O( K9 Z! C0 g" s1 U - It compares the status registers (SR1 and SR2) content to a given event. w( _7 u8 U* G f9 Y8 Y
- (can be the combination of one or more flags).& a/ s# A7 x* ^
- It returns SUCCESS if the current status includes the given flags / M+ k4 j1 i. [5 \% F( ~: F
- and returns ERROR if one or more flags are missing in the current status.* |7 F9 r( g e9 X7 Q
, o" C, J: W" G2 v- - When to use1 |4 W5 e" [! \4 E" k
- - This function is suitable for most applications as well as for startup
- M! j7 X) U: ~6 z) X4 j& m - activity since the events are fully described in the product reference ( E# [0 S% t# J
- manual (RM0090).
& f, l$ ]1 _7 { - - It is also suitable for users who need to define their own events.
# q, r4 w9 g" R, S6 |3 k4 o - # f4 r# a2 F) u# U
- - Limitations2 N2 i, @; W" e, A* h: W
- - If an error occurs (ie. error flags are set besides to the monitored
0 Z8 `0 s, @1 w l( H& T - flags), the I2C_CheckEvent() function may return SUCCESS despite
- R% D r' c& r* ?8 B6 f: I - the communication hold or corrupted real state.
( L& j" V ]% ]; {$ j - In this case, it is advised to use error interrupts to monitor
0 D1 l8 q' a l1 e& B6 [! \ - the error events and handle them in the interrupt IRQ handler.8 G" R' i- m3 J, L
- x; R. X" Z! J; }0 [; w' x z* z8 k
- Note
@, b" m( I# w" w) S3 @! P - For error management, it is advised to use the following functions:+ |2 x) E7 G; {: Z5 _
- - I2C_ITConfig() to configure and enable the error interrupts (I2C_IT_ERR).
r; P& R' O5 L% O% q( \! e' v - - I2Cx_ER_IRQHandler() which is called when the error interrupt occurs.
9 `( Q) i' V! z6 a+ B$ t- K3 L - Where x is the peripheral instance (I2C1, I2C2 ...)
; K& X8 q. K. E2 D# f7 g1 N - - I2C_GetFlagStatus() or I2C_GetITStatus() to be called into the / _4 H/ w! i7 \3 J0 A( Q1 w
- I2Cx_ER_IRQHandler() function in order to determine which error occurred.
2 I' X' n) |. [2 {5 o3 Y/ O - - I2C_ClearFlag() or I2C_ClearITPendingBit() and/or I2C_SoftwareResetCmd() ) @9 x$ h% ]7 z
- and/or I2C_GenerateStop() in order to clear the error flag and source
. P) t3 ?& c2 X5 f; `+ k, b - and return to correct communication status.
9 o; [' q; Q, \2 B - 9 c* z+ `9 d: F/ [ y! A
. L4 n! H* [7 X s8 B) j8 v- 2. Advanced state monitoring (Using the function I2C_GetLastEvent())
" C! [7 k, ]7 g6 L: R! `5 h: z - --------------------------------------------------------------------
* O1 N s4 D* S5 q - Using the function I2C_GetLastEvent() which returns the image of both status " u5 v$ j3 L, }; p
- registers in a single word (uint32_t) (Status Register 2 value is shifted left
) E2 O, `& M* g; F$ V- C9 \ - by 16 bits and concatenated to Status Register 1).
" H1 G+ k1 c# {6 w& a& j0 d+ }( ~
( w& i1 ^2 B8 S) q- - When to use' E* y) V h' z7 F4 U
- - This function is suitable for the same applications above but it 4 g5 Q9 R# i, }$ y' @
- allows to overcome the mentioned limitation of I2C_GetFlagStatus() 0 p" X2 r- y0 o" Y N* V
- function.' H1 ]& `& r" v7 k/ X
- - The returned value could be compared to events already defined in
3 Y. H) M$ r& U) T6 s - this file or to custom values defined by user.
* c- v8 h2 i, `5 E7 Z) _/ v: t9 M# F - This function is suitable when multiple flags are monitored at the
$ t4 \! P4 ^0 A v/ V8 X - same time.
! O7 x& w3 b4 _( Y6 a - - At the opposite of I2C_CheckEvent() function, this function allows * p$ K% o) L8 i; s% r
- user to choose when an event is accepted (when all events flags are
. B! Q2 ^& W, A! R. A- C - set and no other flags are set or just when the needed flags are set 4 [4 u0 @: @7 p- ]
- like I2C_CheckEvent() function.
, z2 ?2 @7 R2 b
$ T2 a% } M3 Z* @3 e- - Limitations3 u' z4 B% Y: V$ T9 i8 T4 Y j
- - User may need to define his own events.
; e i$ B9 J A0 K: g - - Same remark concerning the error management is applicable for this ( D) T- i! g; [
- function if user decides to check only regular communication flags
. N2 g2 z" T8 m5 P v8 j( V+ J - (and ignores error flags).( I; n0 w4 }$ ^" d$ [, R4 P3 d% |$ T
- ) ^ P1 w% X2 T
- 1 t! _* k ^; W" ^9 L
- 3. Flag-based state monitoring (Using the function I2C_GetFlagStatus()): H% v6 d3 L1 p; s3 [" S
- -----------------------------------------------------------------------9 O7 g, N1 @8 Q; p: U7 T& l [7 w8 j
* ?) S& O! z( y: G) F. ~- Using the function I2C_GetFlagStatus() which simply returns the status of
0 x1 M$ g% j; }! r7 M - one single flag (ie. I2C_FLAG_RXNE ...).
& b0 ], f7 t- {% S* P: C% [
: g( q1 h' X8 U* {8 r! J% U- y- - When to use, ~) j r% p$ {7 {$ c! Y3 h
- - This function could be used for specific applications or in debug
( B0 F- s9 y# }+ }' f+ Z9 M - phase.$ j# @: [) o- Y, y0 d Z
- - It is suitable when only one flag checking is needed (most I2C 5 c' j5 }( r, r' w
- events are monitored through multiple flags).
. a+ R4 u9 p3 l$ ^ - - Limitations: 1 F+ T( h) T4 _
- - When calling this function, the Status register is accessed. & d; t( _& h% I# |% c6 F: O2 k
- Some flags are cleared when the status register is accessed.
$ h) Z2 V6 n# g* N3 k; E% o) l - So checking the status of one Flag, may clear other ones.
0 j8 A% _6 V3 l u0 a - - Function may need to be called twice or more in order to monitor * ?. E% O4 Q& e; M' f
- one single event. * y3 c, o+ C7 N/ e3 t f8 F
- */0 _5 y* u/ f# V0 ?
: S. b& ^- p' q0 b4 {; S- /*
- L: n' Y; q, o$ K @ - ===============================================================================& J8 \7 }, M$ K3 j
- 1. Basic state monitoring0 m! r/ Z& `; P" y( }. \
- ===============================================================================) @ R6 N( \- z& s2 J& z C
- */! J5 h/ g( g0 t4 g
- ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT);
& H2 g" w7 Z% Q' S - /*+ C5 b4 ?6 G4 ]0 s: I# a
- ===============================================================================4 x- o4 a8 |) C) G( r1 s8 w
- 2. Advanced state monitoring0 U/ D5 w5 M. D! q8 b
- ===============================================================================3 f/ Q& c# [" W
- */
# ], s, E: g6 s, ~$ u' d1 K - uint32_t I2C_GetLastEvent(I2C_TypeDef* I2Cx);
4 M0 Y) p9 R+ i. B) ]( ]/ } - /*% ~2 y# r7 Y2 a6 w+ L5 Z/ j1 l; r' f
- ===============================================================================
7 A/ y1 l( V8 V3 F& Z - 3. Flag-based state monitoring G) @4 j6 r* U7 R/ H& b
- ===============================================================================$ i. t. X! X2 ]6 a; `6 n5 |
- */: r# Y3 b3 ^: ]4 k8 a
- FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);. p1 Z+ u" Q) U; l4 q
- 4 m# L1 j6 H4 i; [
- 1 k' v# I( w$ D& k! |2 B3 v# j9 `
- void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);
" |, l: C+ \5 r: E$ R6 O. _ - ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT);
! x$ |+ ]% }% O( S% m5 v! v - void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT);
复制代码
% u0 S& o; l8 p04. 结构体封装& s) y \% [8 c( C' d% ~1 d
- /**
$ D' ^* t& H, c! T7 F - * @brief Inter-integrated Circuit Interface
5 ]; H) J- S8 ?' z5 a! s) J" ^* V - */
% U+ d: F1 j1 a, W0 @3 f
7 H9 J( E$ O) s5 I' o9 \- i+ c8 O- typedef struct& T6 n: g: H4 y2 E& s
- {* D1 v/ x+ j$ E; O7 O
- __IO uint16_t CR1; /*!< I2C Control register 1, Address offset: 0x00 */
/ z0 Y6 K: b) |; w - uint16_t RESERVED0; /*!< Reserved, 0x02 */. s; _9 F; t% T, C7 R/ @
- __IO uint16_t CR2; /*!< I2C Control register 2, Address offset: 0x04 */
. g0 _: {1 [4 i2 D: C - uint16_t RESERVED1; /*!< Reserved, 0x06 */7 \% @# _$ a5 I' x y- H/ H, Q0 b
- __IO uint16_t OAR1; /*!< I2C Own address register 1, Address offset: 0x08 *// y6 U% G5 Y% \+ i- |
- uint16_t RESERVED2; /*!< Reserved, 0x0A */4 s% B0 R5 a2 S. p, R
- __IO uint16_t OAR2; /*!< I2C Own address register 2, Address offset: 0x0C */8 g$ d* T4 e8 t; [5 u
- uint16_t RESERVED3; /*!< Reserved, 0x0E */
6 T! f1 _, |4 e! l- ? - __IO uint16_t DR; /*!< I2C Data register, Address offset: 0x10 */
V3 y. i. ?6 r d _8 V( {/ ?7 t# m - uint16_t RESERVED4; /*!< Reserved, 0x12 */
( r$ O; ?- ^. ]+ ?7 u$ I - __IO uint16_t SR1; /*!< I2C Status register 1, Address offset: 0x14 */
- z [9 D. v6 N4 E' T2 q i - uint16_t RESERVED5; /*!< Reserved, 0x16 */
/ L! x& R; T) }% y1 [. w# u - __IO uint16_t SR2; /*!< I2C Status register 2, Address offset: 0x18 */
5 E$ o Y: z5 @. t, ?! ] - uint16_t RESERVED6; /*!< Reserved, 0x1A */3 I: }8 n9 `; B, ~$ J" B+ f
- __IO uint16_t CCR; /*!< I2C Clock control register, Address offset: 0x1C */0 n7 B3 E- L: h7 [. m( s
- uint16_t RESERVED7; /*!< Reserved, 0x1E */. h; `, s( T2 {0 ^- ^6 X& M/ g
- __IO uint16_t TRISE; /*!< I2C TRISE register, Address offset: 0x20 */
& u; `' T9 Q/ i% { - uint16_t RESERVED8; /*!< Reserved, 0x22 */( K4 o7 f6 S7 Q8 ]- O
- __IO uint16_t FLTR; /*!< I2C FLTR register, Address offset: 0x24 */4 A. t2 y3 I1 ]2 y+ w J' Z
- uint16_t RESERVED9; /*!< Reserved, 0x26 */
$ M# B: e: h+ g' A/ E5 ] - } I2C_TypeDef;
复制代码
1 I( I# E* h/ r; d1 Q+ D8 T: l% [% Y8 ]4 u8 Z# u
% s, t) J* J: ~2 w
' h0 [0 }. B' ^! t |