30 #ifndef __STM32F4xx_I2C_H
31 #define __STM32F4xx_I2C_H
38 #include "stm32f4xx.h"
56 uint32_t I2C_ClockSpeed;
62 uint16_t I2C_DutyCycle;
65 uint16_t I2C_OwnAddress1;
71 uint16_t I2C_AcknowledgedAddress;
82 #define IS_I2C_ALL_PERIPH(PERIPH) (((PERIPH) == I2C1) || \
83 ((PERIPH) == I2C2) || \
89 #define I2C_Mode_I2C ((uint16_t)0x0000)
90 #define I2C_Mode_SMBusDevice ((uint16_t)0x0002)
91 #define I2C_Mode_SMBusHost ((uint16_t)0x000A)
92 #define IS_I2C_MODE(MODE) (((MODE) == I2C_Mode_I2C) || \
93 ((MODE) == I2C_Mode_SMBusDevice) || \
94 ((MODE) == I2C_Mode_SMBusHost))
103 #define I2C_DutyCycle_16_9 ((uint16_t)0x4000)
104 #define I2C_DutyCycle_2 ((uint16_t)0xBFFF)
105 #define IS_I2C_DUTY_CYCLE(CYCLE) (((CYCLE) == I2C_DutyCycle_16_9) || \
106 ((CYCLE) == I2C_DutyCycle_2))
115 #define I2C_Ack_Enable ((uint16_t)0x0400)
116 #define I2C_Ack_Disable ((uint16_t)0x0000)
117 #define IS_I2C_ACK_STATE(STATE) (((STATE) == I2C_Ack_Enable) || \
118 ((STATE) == I2C_Ack_Disable))
127 #define I2C_Direction_Transmitter ((uint8_t)0x00)
128 #define I2C_Direction_Receiver ((uint8_t)0x01)
129 #define IS_I2C_DIRECTION(DIRECTION) (((DIRECTION) == I2C_Direction_Transmitter) || \
130 ((DIRECTION) == I2C_Direction_Receiver))
139 #define I2C_AcknowledgedAddress_7bit ((uint16_t)0x4000)
140 #define I2C_AcknowledgedAddress_10bit ((uint16_t)0xC000)
141 #define IS_I2C_ACKNOWLEDGE_ADDRESS(ADDRESS) (((ADDRESS) == I2C_AcknowledgedAddress_7bit) || \
142 ((ADDRESS) == I2C_AcknowledgedAddress_10bit))
151 #define I2C_Register_CR1 ((uint8_t)0x00)
152 #define I2C_Register_CR2 ((uint8_t)0x04)
153 #define I2C_Register_OAR1 ((uint8_t)0x08)
154 #define I2C_Register_OAR2 ((uint8_t)0x0C)
155 #define I2C_Register_DR ((uint8_t)0x10)
156 #define I2C_Register_SR1 ((uint8_t)0x14)
157 #define I2C_Register_SR2 ((uint8_t)0x18)
158 #define I2C_Register_CCR ((uint8_t)0x1C)
159 #define I2C_Register_TRISE ((uint8_t)0x20)
160 #define IS_I2C_REGISTER(REGISTER) (((REGISTER) == I2C_Register_CR1) || \
161 ((REGISTER) == I2C_Register_CR2) || \
162 ((REGISTER) == I2C_Register_OAR1) || \
163 ((REGISTER) == I2C_Register_OAR2) || \
164 ((REGISTER) == I2C_Register_DR) || \
165 ((REGISTER) == I2C_Register_SR1) || \
166 ((REGISTER) == I2C_Register_SR2) || \
167 ((REGISTER) == I2C_Register_CCR) || \
168 ((REGISTER) == I2C_Register_TRISE))
177 #define I2C_NACKPosition_Next ((uint16_t)0x0800)
178 #define I2C_NACKPosition_Current ((uint16_t)0xF7FF)
179 #define IS_I2C_NACK_POSITION(POSITION) (((POSITION) == I2C_NACKPosition_Next) || \
180 ((POSITION) == I2C_NACKPosition_Current))
189 #define I2C_SMBusAlert_Low ((uint16_t)0x2000)
190 #define I2C_SMBusAlert_High ((uint16_t)0xDFFF)
191 #define IS_I2C_SMBUS_ALERT(ALERT) (((ALERT) == I2C_SMBusAlert_Low) || \
192 ((ALERT) == I2C_SMBusAlert_High))
201 #define I2C_PECPosition_Next ((uint16_t)0x0800)
202 #define I2C_PECPosition_Current ((uint16_t)0xF7FF)
203 #define IS_I2C_PEC_POSITION(POSITION) (((POSITION) == I2C_PECPosition_Next) || \
204 ((POSITION) == I2C_PECPosition_Current))
213 #define I2C_IT_BUF ((uint16_t)0x0400)
214 #define I2C_IT_EVT ((uint16_t)0x0200)
215 #define I2C_IT_ERR ((uint16_t)0x0100)
216 #define IS_I2C_CONFIG_IT(IT) ((((IT) & (uint16_t)0xF8FF) == 0x00) && ((IT) != 0x00))
225 #define I2C_IT_SMBALERT ((uint32_t)0x01008000)
226 #define I2C_IT_TIMEOUT ((uint32_t)0x01004000)
227 #define I2C_IT_PECERR ((uint32_t)0x01001000)
228 #define I2C_IT_OVR ((uint32_t)0x01000800)
229 #define I2C_IT_AF ((uint32_t)0x01000400)
230 #define I2C_IT_ARLO ((uint32_t)0x01000200)
231 #define I2C_IT_BERR ((uint32_t)0x01000100)
232 #define I2C_IT_TXE ((uint32_t)0x06000080)
233 #define I2C_IT_RXNE ((uint32_t)0x06000040)
234 #define I2C_IT_STOPF ((uint32_t)0x02000010)
235 #define I2C_IT_ADD10 ((uint32_t)0x02000008)
236 #define I2C_IT_BTF ((uint32_t)0x02000004)
237 #define I2C_IT_ADDR ((uint32_t)0x02000002)
238 #define I2C_IT_SB ((uint32_t)0x02000001)
240 #define IS_I2C_CLEAR_IT(IT) ((((IT) & (uint16_t)0x20FF) == 0x00) && ((IT) != (uint16_t)0x00))
242 #define IS_I2C_GET_IT(IT) (((IT) == I2C_IT_SMBALERT) || ((IT) == I2C_IT_TIMEOUT) || \
243 ((IT) == I2C_IT_PECERR) || ((IT) == I2C_IT_OVR) || \
244 ((IT) == I2C_IT_AF) || ((IT) == I2C_IT_ARLO) || \
245 ((IT) == I2C_IT_BERR) || ((IT) == I2C_IT_TXE) || \
246 ((IT) == I2C_IT_RXNE) || ((IT) == I2C_IT_STOPF) || \
247 ((IT) == I2C_IT_ADD10) || ((IT) == I2C_IT_BTF) || \
248 ((IT) == I2C_IT_ADDR) || ((IT) == I2C_IT_SB))
261 #define I2C_FLAG_DUALF ((uint32_t)0x00800000)
262 #define I2C_FLAG_SMBHOST ((uint32_t)0x00400000)
263 #define I2C_FLAG_SMBDEFAULT ((uint32_t)0x00200000)
264 #define I2C_FLAG_GENCALL ((uint32_t)0x00100000)
265 #define I2C_FLAG_TRA ((uint32_t)0x00040000)
266 #define I2C_FLAG_BUSY ((uint32_t)0x00020000)
267 #define I2C_FLAG_MSL ((uint32_t)0x00010000)
273 #define I2C_FLAG_SMBALERT ((uint32_t)0x10008000)
274 #define I2C_FLAG_TIMEOUT ((uint32_t)0x10004000)
275 #define I2C_FLAG_PECERR ((uint32_t)0x10001000)
276 #define I2C_FLAG_OVR ((uint32_t)0x10000800)
277 #define I2C_FLAG_AF ((uint32_t)0x10000400)
278 #define I2C_FLAG_ARLO ((uint32_t)0x10000200)
279 #define I2C_FLAG_BERR ((uint32_t)0x10000100)
280 #define I2C_FLAG_TXE ((uint32_t)0x10000080)
281 #define I2C_FLAG_RXNE ((uint32_t)0x10000040)
282 #define I2C_FLAG_STOPF ((uint32_t)0x10000010)
283 #define I2C_FLAG_ADD10 ((uint32_t)0x10000008)
284 #define I2C_FLAG_BTF ((uint32_t)0x10000004)
285 #define I2C_FLAG_ADDR ((uint32_t)0x10000002)
286 #define I2C_FLAG_SB ((uint32_t)0x10000001)
288 #define IS_I2C_CLEAR_FLAG(FLAG) ((((FLAG) & (uint16_t)0x20FF) == 0x00) && ((FLAG) != (uint16_t)0x00))
290 #define IS_I2C_GET_FLAG(FLAG) (((FLAG) == I2C_FLAG_DUALF) || ((FLAG) == I2C_FLAG_SMBHOST) || \
291 ((FLAG) == I2C_FLAG_SMBDEFAULT) || ((FLAG) == I2C_FLAG_GENCALL) || \
292 ((FLAG) == I2C_FLAG_TRA) || ((FLAG) == I2C_FLAG_BUSY) || \
293 ((FLAG) == I2C_FLAG_MSL) || ((FLAG) == I2C_FLAG_SMBALERT) || \
294 ((FLAG) == I2C_FLAG_TIMEOUT) || ((FLAG) == I2C_FLAG_PECERR) || \
295 ((FLAG) == I2C_FLAG_OVR) || ((FLAG) == I2C_FLAG_AF) || \
296 ((FLAG) == I2C_FLAG_ARLO) || ((FLAG) == I2C_FLAG_BERR) || \
297 ((FLAG) == I2C_FLAG_TXE) || ((FLAG) == I2C_FLAG_RXNE) || \
298 ((FLAG) == I2C_FLAG_STOPF) || ((FLAG) == I2C_FLAG_ADD10) || \
299 ((FLAG) == I2C_FLAG_BTF) || ((FLAG) == I2C_FLAG_ADDR) || \
300 ((FLAG) == I2C_FLAG_SB))
324 #define I2C_EVENT_MASTER_MODE_SELECT ((uint32_t)0x00030001)
352 #define I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ((uint32_t)0x00070082)
353 #define I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED ((uint32_t)0x00030002)
355 #define I2C_EVENT_MASTER_MODE_ADDRESS10 ((uint32_t)0x00030008)
388 #define I2C_EVENT_MASTER_BYTE_RECEIVED ((uint32_t)0x00030040)
392 #define I2C_EVENT_MASTER_BYTE_TRANSMITTING ((uint32_t)0x00070080)
394 #define I2C_EVENT_MASTER_BYTE_TRANSMITTED ((uint32_t)0x00070084)
431 #define I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED ((uint32_t)0x00020002)
432 #define I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED ((uint32_t)0x00060082)
435 #define I2C_EVENT_SLAVE_RECEIVER_SECONDADDRESS_MATCHED ((uint32_t)0x00820000)
436 #define I2C_EVENT_SLAVE_TRANSMITTER_SECONDADDRESS_MATCHED ((uint32_t)0x00860080)
439 #define I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED ((uint32_t)0x00120000)
470 #define I2C_EVENT_SLAVE_BYTE_RECEIVED ((uint32_t)0x00020040)
472 #define I2C_EVENT_SLAVE_STOP_DETECTED ((uint32_t)0x00000010)
476 #define I2C_EVENT_SLAVE_BYTE_TRANSMITTED ((uint32_t)0x00060084)
477 #define I2C_EVENT_SLAVE_BYTE_TRANSMITTING ((uint32_t)0x00060080)
479 #define I2C_EVENT_SLAVE_ACK_FAILURE ((uint32_t)0x00000400)
487 #define IS_I2C_EVENT(EVENT) (((EVENT) == I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED) || \
488 ((EVENT) == I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED) || \
489 ((EVENT) == I2C_EVENT_SLAVE_TRANSMITTER_SECONDADDRESS_MATCHED) || \
490 ((EVENT) == I2C_EVENT_SLAVE_RECEIVER_SECONDADDRESS_MATCHED) || \
491 ((EVENT) == I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED) || \
492 ((EVENT) == I2C_EVENT_SLAVE_BYTE_RECEIVED) || \
493 ((EVENT) == (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_DUALF)) || \
494 ((EVENT) == (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_GENCALL)) || \
495 ((EVENT) == I2C_EVENT_SLAVE_BYTE_TRANSMITTED) || \
496 ((EVENT) == (I2C_EVENT_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_DUALF)) || \
497 ((EVENT) == (I2C_EVENT_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_GENCALL)) || \
498 ((EVENT) == I2C_EVENT_SLAVE_STOP_DETECTED) || \
499 ((EVENT) == I2C_EVENT_MASTER_MODE_SELECT) || \
500 ((EVENT) == I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) || \
501 ((EVENT) == I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) || \
502 ((EVENT) == I2C_EVENT_MASTER_BYTE_RECEIVED) || \
503 ((EVENT) == I2C_EVENT_MASTER_BYTE_TRANSMITTED) || \
504 ((EVENT) == I2C_EVENT_MASTER_BYTE_TRANSMITTING) || \
505 ((EVENT) == I2C_EVENT_MASTER_MODE_ADDRESS10) || \
506 ((EVENT) == I2C_EVENT_SLAVE_ACK_FAILURE))
515 #define IS_I2C_OWN_ADDRESS1(ADDRESS1) ((ADDRESS1) <= 0x3FF)
524 #define IS_I2C_CLOCK_SPEED(SPEED) (((SPEED) >= 0x1) && ((SPEED) <= 400000))
542 void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
555 void I2C_ARPCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
568 void I2C_DMACmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
573 void I2C_ITConfig(I2C_TypeDef* I2Cx, uint16_t I2C_IT, FunctionalState NewState);
665 ErrorStatus
I2C_CheckEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT);