00001
00029
00030 #ifndef __STM32F4xx_I2C_H
00031 #define __STM32F4xx_I2C_H
00032
00033 #ifdef __cplusplus
00034 extern "C" {
00035 #endif
00036
00037
00038 #include "stm32f4xx.h"
00039
00048
00049
00054 typedef struct
00055 {
00056 uint32_t I2C_ClockSpeed;
00059 uint16_t I2C_Mode;
00062 uint16_t I2C_DutyCycle;
00065 uint16_t I2C_OwnAddress1;
00068 uint16_t I2C_Ack;
00071 uint16_t I2C_AcknowledgedAddress;
00073 }I2C_InitTypeDef;
00074
00075
00076
00077
00082 #define IS_I2C_ALL_PERIPH(PERIPH) (((PERIPH) == I2C1) || \
00083 ((PERIPH) == I2C2) || \
00084 ((PERIPH) == I2C3))
00085
00089 #define I2C_Mode_I2C ((uint16_t)0x0000)
00090 #define I2C_Mode_SMBusDevice ((uint16_t)0x0002)
00091 #define I2C_Mode_SMBusHost ((uint16_t)0x000A)
00092 #define IS_I2C_MODE(MODE) (((MODE) == I2C_Mode_I2C) || \
00093 ((MODE) == I2C_Mode_SMBusDevice) || \
00094 ((MODE) == I2C_Mode_SMBusHost))
00095
00103 #define I2C_DutyCycle_16_9 ((uint16_t)0x4000)
00104 #define I2C_DutyCycle_2 ((uint16_t)0xBFFF)
00105 #define IS_I2C_DUTY_CYCLE(CYCLE) (((CYCLE) == I2C_DutyCycle_16_9) || \
00106 ((CYCLE) == I2C_DutyCycle_2))
00107
00115 #define I2C_Ack_Enable ((uint16_t)0x0400)
00116 #define I2C_Ack_Disable ((uint16_t)0x0000)
00117 #define IS_I2C_ACK_STATE(STATE) (((STATE) == I2C_Ack_Enable) || \
00118 ((STATE) == I2C_Ack_Disable))
00119
00127 #define I2C_Direction_Transmitter ((uint8_t)0x00)
00128 #define I2C_Direction_Receiver ((uint8_t)0x01)
00129 #define IS_I2C_DIRECTION(DIRECTION) (((DIRECTION) == I2C_Direction_Transmitter) || \
00130 ((DIRECTION) == I2C_Direction_Receiver))
00131
00139 #define I2C_AcknowledgedAddress_7bit ((uint16_t)0x4000)
00140 #define I2C_AcknowledgedAddress_10bit ((uint16_t)0xC000)
00141 #define IS_I2C_ACKNOWLEDGE_ADDRESS(ADDRESS) (((ADDRESS) == I2C_AcknowledgedAddress_7bit) || \
00142 ((ADDRESS) == I2C_AcknowledgedAddress_10bit))
00143
00151 #define I2C_Register_CR1 ((uint8_t)0x00)
00152 #define I2C_Register_CR2 ((uint8_t)0x04)
00153 #define I2C_Register_OAR1 ((uint8_t)0x08)
00154 #define I2C_Register_OAR2 ((uint8_t)0x0C)
00155 #define I2C_Register_DR ((uint8_t)0x10)
00156 #define I2C_Register_SR1 ((uint8_t)0x14)
00157 #define I2C_Register_SR2 ((uint8_t)0x18)
00158 #define I2C_Register_CCR ((uint8_t)0x1C)
00159 #define I2C_Register_TRISE ((uint8_t)0x20)
00160 #define IS_I2C_REGISTER(REGISTER) (((REGISTER) == I2C_Register_CR1) || \
00161 ((REGISTER) == I2C_Register_CR2) || \
00162 ((REGISTER) == I2C_Register_OAR1) || \
00163 ((REGISTER) == I2C_Register_OAR2) || \
00164 ((REGISTER) == I2C_Register_DR) || \
00165 ((REGISTER) == I2C_Register_SR1) || \
00166 ((REGISTER) == I2C_Register_SR2) || \
00167 ((REGISTER) == I2C_Register_CCR) || \
00168 ((REGISTER) == I2C_Register_TRISE))
00169
00177 #define I2C_NACKPosition_Next ((uint16_t)0x0800)
00178 #define I2C_NACKPosition_Current ((uint16_t)0xF7FF)
00179 #define IS_I2C_NACK_POSITION(POSITION) (((POSITION) == I2C_NACKPosition_Next) || \
00180 ((POSITION) == I2C_NACKPosition_Current))
00181
00189 #define I2C_SMBusAlert_Low ((uint16_t)0x2000)
00190 #define I2C_SMBusAlert_High ((uint16_t)0xDFFF)
00191 #define IS_I2C_SMBUS_ALERT(ALERT) (((ALERT) == I2C_SMBusAlert_Low) || \
00192 ((ALERT) == I2C_SMBusAlert_High))
00193
00201 #define I2C_PECPosition_Next ((uint16_t)0x0800)
00202 #define I2C_PECPosition_Current ((uint16_t)0xF7FF)
00203 #define IS_I2C_PEC_POSITION(POSITION) (((POSITION) == I2C_PECPosition_Next) || \
00204 ((POSITION) == I2C_PECPosition_Current))
00205
00213 #define I2C_IT_BUF ((uint16_t)0x0400)
00214 #define I2C_IT_EVT ((uint16_t)0x0200)
00215 #define I2C_IT_ERR ((uint16_t)0x0100)
00216 #define IS_I2C_CONFIG_IT(IT) ((((IT) & (uint16_t)0xF8FF) == 0x00) && ((IT) != 0x00))
00217
00225 #define I2C_IT_SMBALERT ((uint32_t)0x01008000)
00226 #define I2C_IT_TIMEOUT ((uint32_t)0x01004000)
00227 #define I2C_IT_PECERR ((uint32_t)0x01001000)
00228 #define I2C_IT_OVR ((uint32_t)0x01000800)
00229 #define I2C_IT_AF ((uint32_t)0x01000400)
00230 #define I2C_IT_ARLO ((uint32_t)0x01000200)
00231 #define I2C_IT_BERR ((uint32_t)0x01000100)
00232 #define I2C_IT_TXE ((uint32_t)0x06000080)
00233 #define I2C_IT_RXNE ((uint32_t)0x06000040)
00234 #define I2C_IT_STOPF ((uint32_t)0x02000010)
00235 #define I2C_IT_ADD10 ((uint32_t)0x02000008)
00236 #define I2C_IT_BTF ((uint32_t)0x02000004)
00237 #define I2C_IT_ADDR ((uint32_t)0x02000002)
00238 #define I2C_IT_SB ((uint32_t)0x02000001)
00239
00240 #define IS_I2C_CLEAR_IT(IT) ((((IT) & (uint16_t)0x20FF) == 0x00) && ((IT) != (uint16_t)0x00))
00241
00242 #define IS_I2C_GET_IT(IT) (((IT) == I2C_IT_SMBALERT) || ((IT) == I2C_IT_TIMEOUT) || \
00243 ((IT) == I2C_IT_PECERR) || ((IT) == I2C_IT_OVR) || \
00244 ((IT) == I2C_IT_AF) || ((IT) == I2C_IT_ARLO) || \
00245 ((IT) == I2C_IT_BERR) || ((IT) == I2C_IT_TXE) || \
00246 ((IT) == I2C_IT_RXNE) || ((IT) == I2C_IT_STOPF) || \
00247 ((IT) == I2C_IT_ADD10) || ((IT) == I2C_IT_BTF) || \
00248 ((IT) == I2C_IT_ADDR) || ((IT) == I2C_IT_SB))
00249
00261 #define I2C_FLAG_DUALF ((uint32_t)0x00800000)
00262 #define I2C_FLAG_SMBHOST ((uint32_t)0x00400000)
00263 #define I2C_FLAG_SMBDEFAULT ((uint32_t)0x00200000)
00264 #define I2C_FLAG_GENCALL ((uint32_t)0x00100000)
00265 #define I2C_FLAG_TRA ((uint32_t)0x00040000)
00266 #define I2C_FLAG_BUSY ((uint32_t)0x00020000)
00267 #define I2C_FLAG_MSL ((uint32_t)0x00010000)
00268
00273 #define I2C_FLAG_SMBALERT ((uint32_t)0x10008000)
00274 #define I2C_FLAG_TIMEOUT ((uint32_t)0x10004000)
00275 #define I2C_FLAG_PECERR ((uint32_t)0x10001000)
00276 #define I2C_FLAG_OVR ((uint32_t)0x10000800)
00277 #define I2C_FLAG_AF ((uint32_t)0x10000400)
00278 #define I2C_FLAG_ARLO ((uint32_t)0x10000200)
00279 #define I2C_FLAG_BERR ((uint32_t)0x10000100)
00280 #define I2C_FLAG_TXE ((uint32_t)0x10000080)
00281 #define I2C_FLAG_RXNE ((uint32_t)0x10000040)
00282 #define I2C_FLAG_STOPF ((uint32_t)0x10000010)
00283 #define I2C_FLAG_ADD10 ((uint32_t)0x10000008)
00284 #define I2C_FLAG_BTF ((uint32_t)0x10000004)
00285 #define I2C_FLAG_ADDR ((uint32_t)0x10000002)
00286 #define I2C_FLAG_SB ((uint32_t)0x10000001)
00287
00288 #define IS_I2C_CLEAR_FLAG(FLAG) ((((FLAG) & (uint16_t)0x20FF) == 0x00) && ((FLAG) != (uint16_t)0x00))
00289
00290 #define IS_I2C_GET_FLAG(FLAG) (((FLAG) == I2C_FLAG_DUALF) || ((FLAG) == I2C_FLAG_SMBHOST) || \
00291 ((FLAG) == I2C_FLAG_SMBDEFAULT) || ((FLAG) == I2C_FLAG_GENCALL) || \
00292 ((FLAG) == I2C_FLAG_TRA) || ((FLAG) == I2C_FLAG_BUSY) || \
00293 ((FLAG) == I2C_FLAG_MSL) || ((FLAG) == I2C_FLAG_SMBALERT) || \
00294 ((FLAG) == I2C_FLAG_TIMEOUT) || ((FLAG) == I2C_FLAG_PECERR) || \
00295 ((FLAG) == I2C_FLAG_OVR) || ((FLAG) == I2C_FLAG_AF) || \
00296 ((FLAG) == I2C_FLAG_ARLO) || ((FLAG) == I2C_FLAG_BERR) || \
00297 ((FLAG) == I2C_FLAG_TXE) || ((FLAG) == I2C_FLAG_RXNE) || \
00298 ((FLAG) == I2C_FLAG_STOPF) || ((FLAG) == I2C_FLAG_ADD10) || \
00299 ((FLAG) == I2C_FLAG_BTF) || ((FLAG) == I2C_FLAG_ADDR) || \
00300 ((FLAG) == I2C_FLAG_SB))
00301
00323
00324 #define I2C_EVENT_MASTER_MODE_SELECT ((uint32_t)0x00030001)
00325
00351
00352 #define I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ((uint32_t)0x00070082)
00353 #define I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED ((uint32_t)0x00030002)
00354
00355 #define I2C_EVENT_MASTER_MODE_ADDRESS10 ((uint32_t)0x00030008)
00356
00386
00387
00388 #define I2C_EVENT_MASTER_BYTE_RECEIVED ((uint32_t)0x00030040)
00389
00390
00391
00392 #define I2C_EVENT_MASTER_BYTE_TRANSMITTING ((uint32_t)0x00070080)
00393
00394 #define I2C_EVENT_MASTER_BYTE_TRANSMITTED ((uint32_t)0x00070084)
00395
00396
00429
00430
00431 #define I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED ((uint32_t)0x00020002)
00432 #define I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED ((uint32_t)0x00060082)
00433
00434
00435 #define I2C_EVENT_SLAVE_RECEIVER_SECONDADDRESS_MATCHED ((uint32_t)0x00820000)
00436 #define I2C_EVENT_SLAVE_TRANSMITTER_SECONDADDRESS_MATCHED ((uint32_t)0x00860080)
00437
00438
00439 #define I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED ((uint32_t)0x00120000)
00440
00468
00469
00470 #define I2C_EVENT_SLAVE_BYTE_RECEIVED ((uint32_t)0x00020040)
00471
00472 #define I2C_EVENT_SLAVE_STOP_DETECTED ((uint32_t)0x00000010)
00473
00474
00475
00476 #define I2C_EVENT_SLAVE_BYTE_TRANSMITTED ((uint32_t)0x00060084)
00477 #define I2C_EVENT_SLAVE_BYTE_TRANSMITTING ((uint32_t)0x00060080)
00478
00479 #define I2C_EVENT_SLAVE_ACK_FAILURE ((uint32_t)0x00000400)
00480
00481
00482
00483
00484
00485
00486
00487 #define IS_I2C_EVENT(EVENT) (((EVENT) == I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED) || \
00488 ((EVENT) == I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED) || \
00489 ((EVENT) == I2C_EVENT_SLAVE_TRANSMITTER_SECONDADDRESS_MATCHED) || \
00490 ((EVENT) == I2C_EVENT_SLAVE_RECEIVER_SECONDADDRESS_MATCHED) || \
00491 ((EVENT) == I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED) || \
00492 ((EVENT) == I2C_EVENT_SLAVE_BYTE_RECEIVED) || \
00493 ((EVENT) == (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_DUALF)) || \
00494 ((EVENT) == (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_GENCALL)) || \
00495 ((EVENT) == I2C_EVENT_SLAVE_BYTE_TRANSMITTED) || \
00496 ((EVENT) == (I2C_EVENT_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_DUALF)) || \
00497 ((EVENT) == (I2C_EVENT_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_GENCALL)) || \
00498 ((EVENT) == I2C_EVENT_SLAVE_STOP_DETECTED) || \
00499 ((EVENT) == I2C_EVENT_MASTER_MODE_SELECT) || \
00500 ((EVENT) == I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) || \
00501 ((EVENT) == I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) || \
00502 ((EVENT) == I2C_EVENT_MASTER_BYTE_RECEIVED) || \
00503 ((EVENT) == I2C_EVENT_MASTER_BYTE_TRANSMITTED) || \
00504 ((EVENT) == I2C_EVENT_MASTER_BYTE_TRANSMITTING) || \
00505 ((EVENT) == I2C_EVENT_MASTER_MODE_ADDRESS10) || \
00506 ((EVENT) == I2C_EVENT_SLAVE_ACK_FAILURE))
00507
00515 #define IS_I2C_OWN_ADDRESS1(ADDRESS1) ((ADDRESS1) <= 0x3FF)
00516
00524 #define IS_I2C_CLOCK_SPEED(SPEED) (((SPEED) >= 0x1) && ((SPEED) <= 400000))
00525
00533
00534
00535
00536
00537 void I2C_DeInit(I2C_TypeDef* I2Cx);
00538
00539
00540 void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct);
00541 void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct);
00542 void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
00543 void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState);
00544 void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState);
00545 void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, uint8_t I2C_Direction);
00546 void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState);
00547 void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint8_t Address);
00548 void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
00549 void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
00550 void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
00551 void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
00552 void I2C_FastModeDutyCycleConfig(I2C_TypeDef* I2Cx, uint16_t I2C_DutyCycle);
00553 void I2C_NACKPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_NACKPosition);
00554 void I2C_SMBusAlertConfig(I2C_TypeDef* I2Cx, uint16_t I2C_SMBusAlert);
00555 void I2C_ARPCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
00556
00557
00558 void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data);
00559 uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx);
00560
00561
00562 void I2C_TransmitPEC(I2C_TypeDef* I2Cx, FunctionalState NewState);
00563 void I2C_PECPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_PECPosition);
00564 void I2C_CalculatePEC(I2C_TypeDef* I2Cx, FunctionalState NewState);
00565 uint8_t I2C_GetPEC(I2C_TypeDef* I2Cx);
00566
00567
00568 void I2C_DMACmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
00569 void I2C_DMALastTransferCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
00570
00571
00572 uint16_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register);
00573 void I2C_ITConfig(I2C_TypeDef* I2Cx, uint16_t I2C_IT, FunctionalState NewState);
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665 ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT);
00666
00667
00668
00669
00670
00671 uint32_t I2C_GetLastEvent(I2C_TypeDef* I2Cx);
00672
00673
00674
00675
00676
00677 FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);
00678
00679
00680 void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);
00681 ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT);
00682 void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT);
00683
00684 #ifdef __cplusplus
00685 }
00686 #endif
00687
00688 #endif
00689
00698