STM32F10x Standard Peripherals Library
3.5.0
|
00001 00023 /* Define to prevent recursive inclusion -------------------------------------*/ 00024 #ifndef __STM32F10x_I2C_H 00025 #define __STM32F10x_I2C_H 00026 00027 #ifdef __cplusplus 00028 extern "C" { 00029 #endif 00030 00031 /* Includes ------------------------------------------------------------------*/ 00032 #include "stm32f10x.h" 00033 00050 typedef struct 00051 { 00052 uint32_t I2C_ClockSpeed; 00055 uint16_t I2C_Mode; 00058 uint16_t I2C_DutyCycle; 00061 uint16_t I2C_OwnAddress1; 00064 uint16_t I2C_Ack; 00067 uint16_t I2C_AcknowledgedAddress; 00069 }I2C_InitTypeDef; 00070 00080 #define IS_I2C_ALL_PERIPH(PERIPH) (((PERIPH) == I2C1) || \ 00081 ((PERIPH) == I2C2)) 00082 00086 #define I2C_Mode_I2C ((uint16_t)0x0000) 00087 #define I2C_Mode_SMBusDevice ((uint16_t)0x0002) 00088 #define I2C_Mode_SMBusHost ((uint16_t)0x000A) 00089 #define IS_I2C_MODE(MODE) (((MODE) == I2C_Mode_I2C) || \ 00090 ((MODE) == I2C_Mode_SMBusDevice) || \ 00091 ((MODE) == I2C_Mode_SMBusHost)) 00092 00100 #define I2C_DutyCycle_16_9 ((uint16_t)0x4000) 00101 #define I2C_DutyCycle_2 ((uint16_t)0xBFFF) 00102 #define IS_I2C_DUTY_CYCLE(CYCLE) (((CYCLE) == I2C_DutyCycle_16_9) || \ 00103 ((CYCLE) == I2C_DutyCycle_2)) 00104 00112 #define I2C_Ack_Enable ((uint16_t)0x0400) 00113 #define I2C_Ack_Disable ((uint16_t)0x0000) 00114 #define IS_I2C_ACK_STATE(STATE) (((STATE) == I2C_Ack_Enable) || \ 00115 ((STATE) == I2C_Ack_Disable)) 00116 00124 #define I2C_Direction_Transmitter ((uint8_t)0x00) 00125 #define I2C_Direction_Receiver ((uint8_t)0x01) 00126 #define IS_I2C_DIRECTION(DIRECTION) (((DIRECTION) == I2C_Direction_Transmitter) || \ 00127 ((DIRECTION) == I2C_Direction_Receiver)) 00128 00136 #define I2C_AcknowledgedAddress_7bit ((uint16_t)0x4000) 00137 #define I2C_AcknowledgedAddress_10bit ((uint16_t)0xC000) 00138 #define IS_I2C_ACKNOWLEDGE_ADDRESS(ADDRESS) (((ADDRESS) == I2C_AcknowledgedAddress_7bit) || \ 00139 ((ADDRESS) == I2C_AcknowledgedAddress_10bit)) 00140 00148 #define I2C_Register_CR1 ((uint8_t)0x00) 00149 #define I2C_Register_CR2 ((uint8_t)0x04) 00150 #define I2C_Register_OAR1 ((uint8_t)0x08) 00151 #define I2C_Register_OAR2 ((uint8_t)0x0C) 00152 #define I2C_Register_DR ((uint8_t)0x10) 00153 #define I2C_Register_SR1 ((uint8_t)0x14) 00154 #define I2C_Register_SR2 ((uint8_t)0x18) 00155 #define I2C_Register_CCR ((uint8_t)0x1C) 00156 #define I2C_Register_TRISE ((uint8_t)0x20) 00157 #define IS_I2C_REGISTER(REGISTER) (((REGISTER) == I2C_Register_CR1) || \ 00158 ((REGISTER) == I2C_Register_CR2) || \ 00159 ((REGISTER) == I2C_Register_OAR1) || \ 00160 ((REGISTER) == I2C_Register_OAR2) || \ 00161 ((REGISTER) == I2C_Register_DR) || \ 00162 ((REGISTER) == I2C_Register_SR1) || \ 00163 ((REGISTER) == I2C_Register_SR2) || \ 00164 ((REGISTER) == I2C_Register_CCR) || \ 00165 ((REGISTER) == I2C_Register_TRISE)) 00166 00174 #define I2C_SMBusAlert_Low ((uint16_t)0x2000) 00175 #define I2C_SMBusAlert_High ((uint16_t)0xDFFF) 00176 #define IS_I2C_SMBUS_ALERT(ALERT) (((ALERT) == I2C_SMBusAlert_Low) || \ 00177 ((ALERT) == I2C_SMBusAlert_High)) 00178 00186 #define I2C_PECPosition_Next ((uint16_t)0x0800) 00187 #define I2C_PECPosition_Current ((uint16_t)0xF7FF) 00188 #define IS_I2C_PEC_POSITION(POSITION) (((POSITION) == I2C_PECPosition_Next) || \ 00189 ((POSITION) == I2C_PECPosition_Current)) 00190 00198 #define I2C_NACKPosition_Next ((uint16_t)0x0800) 00199 #define I2C_NACKPosition_Current ((uint16_t)0xF7FF) 00200 #define IS_I2C_NACK_POSITION(POSITION) (((POSITION) == I2C_NACKPosition_Next) || \ 00201 ((POSITION) == I2C_NACKPosition_Current)) 00202 00210 #define I2C_IT_BUF ((uint16_t)0x0400) 00211 #define I2C_IT_EVT ((uint16_t)0x0200) 00212 #define I2C_IT_ERR ((uint16_t)0x0100) 00213 #define IS_I2C_CONFIG_IT(IT) ((((IT) & (uint16_t)0xF8FF) == 0x00) && ((IT) != 0x00)) 00214 00222 #define I2C_IT_SMBALERT ((uint32_t)0x01008000) 00223 #define I2C_IT_TIMEOUT ((uint32_t)0x01004000) 00224 #define I2C_IT_PECERR ((uint32_t)0x01001000) 00225 #define I2C_IT_OVR ((uint32_t)0x01000800) 00226 #define I2C_IT_AF ((uint32_t)0x01000400) 00227 #define I2C_IT_ARLO ((uint32_t)0x01000200) 00228 #define I2C_IT_BERR ((uint32_t)0x01000100) 00229 #define I2C_IT_TXE ((uint32_t)0x06000080) 00230 #define I2C_IT_RXNE ((uint32_t)0x06000040) 00231 #define I2C_IT_STOPF ((uint32_t)0x02000010) 00232 #define I2C_IT_ADD10 ((uint32_t)0x02000008) 00233 #define I2C_IT_BTF ((uint32_t)0x02000004) 00234 #define I2C_IT_ADDR ((uint32_t)0x02000002) 00235 #define I2C_IT_SB ((uint32_t)0x02000001) 00236 00237 #define IS_I2C_CLEAR_IT(IT) ((((IT) & (uint16_t)0x20FF) == 0x00) && ((IT) != (uint16_t)0x00)) 00238 00239 #define IS_I2C_GET_IT(IT) (((IT) == I2C_IT_SMBALERT) || ((IT) == I2C_IT_TIMEOUT) || \ 00240 ((IT) == I2C_IT_PECERR) || ((IT) == I2C_IT_OVR) || \ 00241 ((IT) == I2C_IT_AF) || ((IT) == I2C_IT_ARLO) || \ 00242 ((IT) == I2C_IT_BERR) || ((IT) == I2C_IT_TXE) || \ 00243 ((IT) == I2C_IT_RXNE) || ((IT) == I2C_IT_STOPF) || \ 00244 ((IT) == I2C_IT_ADD10) || ((IT) == I2C_IT_BTF) || \ 00245 ((IT) == I2C_IT_ADDR) || ((IT) == I2C_IT_SB)) 00246 00258 #define I2C_FLAG_DUALF ((uint32_t)0x00800000) 00259 #define I2C_FLAG_SMBHOST ((uint32_t)0x00400000) 00260 #define I2C_FLAG_SMBDEFAULT ((uint32_t)0x00200000) 00261 #define I2C_FLAG_GENCALL ((uint32_t)0x00100000) 00262 #define I2C_FLAG_TRA ((uint32_t)0x00040000) 00263 #define I2C_FLAG_BUSY ((uint32_t)0x00020000) 00264 #define I2C_FLAG_MSL ((uint32_t)0x00010000) 00265 00270 #define I2C_FLAG_SMBALERT ((uint32_t)0x10008000) 00271 #define I2C_FLAG_TIMEOUT ((uint32_t)0x10004000) 00272 #define I2C_FLAG_PECERR ((uint32_t)0x10001000) 00273 #define I2C_FLAG_OVR ((uint32_t)0x10000800) 00274 #define I2C_FLAG_AF ((uint32_t)0x10000400) 00275 #define I2C_FLAG_ARLO ((uint32_t)0x10000200) 00276 #define I2C_FLAG_BERR ((uint32_t)0x10000100) 00277 #define I2C_FLAG_TXE ((uint32_t)0x10000080) 00278 #define I2C_FLAG_RXNE ((uint32_t)0x10000040) 00279 #define I2C_FLAG_STOPF ((uint32_t)0x10000010) 00280 #define I2C_FLAG_ADD10 ((uint32_t)0x10000008) 00281 #define I2C_FLAG_BTF ((uint32_t)0x10000004) 00282 #define I2C_FLAG_ADDR ((uint32_t)0x10000002) 00283 #define I2C_FLAG_SB ((uint32_t)0x10000001) 00284 00285 #define IS_I2C_CLEAR_FLAG(FLAG) ((((FLAG) & (uint16_t)0x20FF) == 0x00) && ((FLAG) != (uint16_t)0x00)) 00286 00287 #define IS_I2C_GET_FLAG(FLAG) (((FLAG) == I2C_FLAG_DUALF) || ((FLAG) == I2C_FLAG_SMBHOST) || \ 00288 ((FLAG) == I2C_FLAG_SMBDEFAULT) || ((FLAG) == I2C_FLAG_GENCALL) || \ 00289 ((FLAG) == I2C_FLAG_TRA) || ((FLAG) == I2C_FLAG_BUSY) || \ 00290 ((FLAG) == I2C_FLAG_MSL) || ((FLAG) == I2C_FLAG_SMBALERT) || \ 00291 ((FLAG) == I2C_FLAG_TIMEOUT) || ((FLAG) == I2C_FLAG_PECERR) || \ 00292 ((FLAG) == I2C_FLAG_OVR) || ((FLAG) == I2C_FLAG_AF) || \ 00293 ((FLAG) == I2C_FLAG_ARLO) || ((FLAG) == I2C_FLAG_BERR) || \ 00294 ((FLAG) == I2C_FLAG_TXE) || ((FLAG) == I2C_FLAG_RXNE) || \ 00295 ((FLAG) == I2C_FLAG_STOPF) || ((FLAG) == I2C_FLAG_ADD10) || \ 00296 ((FLAG) == I2C_FLAG_BTF) || ((FLAG) == I2C_FLAG_ADDR) || \ 00297 ((FLAG) == I2C_FLAG_SB)) 00298 00306 /*======================================== 00307 00308 I2C Master Events (Events grouped in order of communication) 00309 ==========================================*/ 00318 /* --EV5 */ 00319 #define I2C_EVENT_MASTER_MODE_SELECT ((uint32_t)0x00030001) /* BUSY, MSL and SB flag */ 00320 00346 /* --EV6 */ 00347 #define I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ((uint32_t)0x00070082) /* BUSY, MSL, ADDR, TXE and TRA flags */ 00348 #define I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED ((uint32_t)0x00030002) /* BUSY, MSL and ADDR flags */ 00349 /* --EV9 */ 00350 #define I2C_EVENT_MASTER_MODE_ADDRESS10 ((uint32_t)0x00030008) /* BUSY, MSL and ADD10 flags */ 00351 00381 /* Master RECEIVER mode -----------------------------*/ 00382 /* --EV7 */ 00383 #define I2C_EVENT_MASTER_BYTE_RECEIVED ((uint32_t)0x00030040) /* BUSY, MSL and RXNE flags */ 00384 00385 /* Master TRANSMITTER mode --------------------------*/ 00386 /* --EV8 */ 00387 #define I2C_EVENT_MASTER_BYTE_TRANSMITTING ((uint32_t)0x00070080) /* TRA, BUSY, MSL, TXE flags */ 00388 /* --EV8_2 */ 00389 #define I2C_EVENT_MASTER_BYTE_TRANSMITTED ((uint32_t)0x00070084) /* TRA, BUSY, MSL, TXE and BTF flags */ 00390 00391 00392 /*======================================== 00393 00394 I2C Slave Events (Events grouped in order of communication) 00395 ==========================================*/ 00396 00422 /* --EV1 (all the events below are variants of EV1) */ 00423 /* 1) Case of One Single Address managed by the slave */ 00424 #define I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED ((uint32_t)0x00020002) /* BUSY and ADDR flags */ 00425 #define I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED ((uint32_t)0x00060082) /* TRA, BUSY, TXE and ADDR flags */ 00426 00427 /* 2) Case of Dual address managed by the slave */ 00428 #define I2C_EVENT_SLAVE_RECEIVER_SECONDADDRESS_MATCHED ((uint32_t)0x00820000) /* DUALF and BUSY flags */ 00429 #define I2C_EVENT_SLAVE_TRANSMITTER_SECONDADDRESS_MATCHED ((uint32_t)0x00860080) /* DUALF, TRA, BUSY and TXE flags */ 00430 00431 /* 3) Case of General Call enabled for the slave */ 00432 #define I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED ((uint32_t)0x00120000) /* GENCALL and BUSY flags */ 00433 00461 /* Slave RECEIVER mode --------------------------*/ 00462 /* --EV2 */ 00463 #define I2C_EVENT_SLAVE_BYTE_RECEIVED ((uint32_t)0x00020040) /* BUSY and RXNE flags */ 00464 /* --EV4 */ 00465 #define I2C_EVENT_SLAVE_STOP_DETECTED ((uint32_t)0x00000010) /* STOPF flag */ 00466 00467 /* Slave TRANSMITTER mode -----------------------*/ 00468 /* --EV3 */ 00469 #define I2C_EVENT_SLAVE_BYTE_TRANSMITTED ((uint32_t)0x00060084) /* TRA, BUSY, TXE and BTF flags */ 00470 #define I2C_EVENT_SLAVE_BYTE_TRANSMITTING ((uint32_t)0x00060080) /* TRA, BUSY and TXE flags */ 00471 /* --EV3_2 */ 00472 #define I2C_EVENT_SLAVE_ACK_FAILURE ((uint32_t)0x00000400) /* AF flag */ 00473 00474 /*=========================== End of Events Description ==========================================*/ 00475 00476 #define IS_I2C_EVENT(EVENT) (((EVENT) == I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED) || \ 00477 ((EVENT) == I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED) || \ 00478 ((EVENT) == I2C_EVENT_SLAVE_TRANSMITTER_SECONDADDRESS_MATCHED) || \ 00479 ((EVENT) == I2C_EVENT_SLAVE_RECEIVER_SECONDADDRESS_MATCHED) || \ 00480 ((EVENT) == I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED) || \ 00481 ((EVENT) == I2C_EVENT_SLAVE_BYTE_RECEIVED) || \ 00482 ((EVENT) == (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_DUALF)) || \ 00483 ((EVENT) == (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_GENCALL)) || \ 00484 ((EVENT) == I2C_EVENT_SLAVE_BYTE_TRANSMITTED) || \ 00485 ((EVENT) == (I2C_EVENT_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_DUALF)) || \ 00486 ((EVENT) == (I2C_EVENT_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_GENCALL)) || \ 00487 ((EVENT) == I2C_EVENT_SLAVE_STOP_DETECTED) || \ 00488 ((EVENT) == I2C_EVENT_MASTER_MODE_SELECT) || \ 00489 ((EVENT) == I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) || \ 00490 ((EVENT) == I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) || \ 00491 ((EVENT) == I2C_EVENT_MASTER_BYTE_RECEIVED) || \ 00492 ((EVENT) == I2C_EVENT_MASTER_BYTE_TRANSMITTED) || \ 00493 ((EVENT) == I2C_EVENT_MASTER_BYTE_TRANSMITTING) || \ 00494 ((EVENT) == I2C_EVENT_MASTER_MODE_ADDRESS10) || \ 00495 ((EVENT) == I2C_EVENT_SLAVE_ACK_FAILURE)) 00496 00504 #define IS_I2C_OWN_ADDRESS1(ADDRESS1) ((ADDRESS1) <= 0x3FF) 00505 00513 #define IS_I2C_CLOCK_SPEED(SPEED) (((SPEED) >= 0x1) && ((SPEED) <= 400000)) 00514 00534 void I2C_DeInit(I2C_TypeDef* I2Cx); 00535 void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct); 00536 void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct); 00537 void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState); 00538 void I2C_DMACmd(I2C_TypeDef* I2Cx, FunctionalState NewState); 00539 void I2C_DMALastTransferCmd(I2C_TypeDef* I2Cx, FunctionalState NewState); 00540 void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState); 00541 void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState); 00542 void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState); 00543 void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint8_t Address); 00544 void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState); 00545 void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState); 00546 void I2C_ITConfig(I2C_TypeDef* I2Cx, uint16_t I2C_IT, FunctionalState NewState); 00547 void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data); 00548 uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx); 00549 void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, uint8_t I2C_Direction); 00550 uint16_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register); 00551 void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx, FunctionalState NewState); 00552 void I2C_NACKPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_NACKPosition); 00553 void I2C_SMBusAlertConfig(I2C_TypeDef* I2Cx, uint16_t I2C_SMBusAlert); 00554 void I2C_TransmitPEC(I2C_TypeDef* I2Cx, FunctionalState NewState); 00555 void I2C_PECPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_PECPosition); 00556 void I2C_CalculatePEC(I2C_TypeDef* I2Cx, FunctionalState NewState); 00557 uint8_t I2C_GetPEC(I2C_TypeDef* I2Cx); 00558 void I2C_ARPCmd(I2C_TypeDef* I2Cx, FunctionalState NewState); 00559 void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState); 00560 void I2C_FastModeDutyCycleConfig(I2C_TypeDef* I2Cx, uint16_t I2C_DutyCycle); 00561 00645 ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT); 00651 uint32_t I2C_GetLastEvent(I2C_TypeDef* I2Cx); 00657 FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG); 00663 void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG); 00664 ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT); 00665 void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT); 00666 00667 #ifdef __cplusplus 00668 } 00669 #endif 00670 00671 #endif /*__STM32F10x_I2C_H */ 00672 00684 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/