1 /** 2 ****************************************************************************** 3 * @file ft32f0xx_i2c.h 4 * @author FMD AE 5 * @brief This file contains all the functions prototypes for the I2C firmware 6 * library 7 * @version V1.0.0 8 * @data 2021-07-01 9 ****************************************************************************** 10 */ 11 12 13 /* Define to prevent recursive inclusion -------------------------------------*/ 14 #ifndef __FT32F0XX_I2C_H 15 #define __FT32F0XX_I2C_H 16 17 #ifdef __cplusplus 18 extern "C" { 19 #endif 20 21 /* Includes ------------------------------------------------------------------*/ 22 #include "ft32f0xx.h" 23 24 25 /** @addtogroup I2C 26 * @{ 27 */ 28 29 /* Exported types ------------------------------------------------------------*/ 30 31 /** 32 * @brief I2C Init structure definition 33 */ 34 35 typedef struct 36 { 37 uint32_t I2C_Timing; /*!< Specifies the I2C_TIMINGR_register value. 38 This parameter must be set by referring to I2C_Timing_Config_Tool*/ 39 40 uint32_t I2C_AnalogFilter; /*!< Enables or disables analog noise filter. 41 This parameter can be a value of @ref I2C_Analog_Filter*/ 42 43 uint32_t I2C_DigitalFilter; /*!< Configures the digital noise filter. 44 This parameter can be a number between 0x00 and 0x0F*/ 45 46 uint32_t I2C_Mode; /*!< Specifies the I2C mode. 47 This parameter can be a value of @ref I2C_mode*/ 48 49 uint32_t I2C_OwnAddress1; /*!< Specifies the device own address 1. 50 This parameter can be a 7-bit or 10-bit address*/ 51 52 uint32_t I2C_Ack; /*!< Enables or disables the acknowledgement. 53 This parameter can be a value of @ref I2C_acknowledgement*/ 54 55 uint32_t I2C_AcknowledgedAddress; /*!< Specifies if 7-bit or 10-bit address is acknowledged. 56 This parameter can be a value of @ref I2C_acknowledged_address*/ 57 }I2C_InitTypeDef; 58 59 /* Exported constants --------------------------------------------------------*/ 60 61 62 /** @defgroup I2C_Exported_Constants 63 * @{ 64 */ 65 66 #define IS_I2C_ALL_PERIPH(PERIPH) (((PERIPH) == I2C1) || \ 67 ((PERIPH) == I2C2)) 68 69 #define IS_I2C_1_PERIPH(PERIPH) ((PERIPH) == I2C1) 70 71 /** @defgroup I2C_Analog_Filter 72 * @{ 73 */ 74 75 #define I2C_AnalogFilter_Enable ((uint32_t)0x00000000) 76 #define I2C_AnalogFilter_Disable I2C_CR1_ANFOFF 77 78 #define IS_I2C_ANALOG_FILTER(FILTER) (((FILTER) == I2C_AnalogFilter_Enable) || \ 79 ((FILTER) == I2C_AnalogFilter_Disable)) 80 /** 81 * @} 82 */ 83 84 /** @defgroup I2C_Digital_Filter 85 * @{ 86 */ 87 88 #define IS_I2C_DIGITAL_FILTER(FILTER) ((FILTER) <= 0x0000000F) 89 /** 90 * @} 91 */ 92 93 /** @defgroup I2C_mode 94 * @{ 95 */ 96 97 #define I2C_Mode_I2C ((uint32_t)0x00000000) 98 #define I2C_Mode_SMBusDevice I2C_CR1_SMBDEN 99 #define I2C_Mode_SMBusHost I2C_CR1_SMBHEN 100 101 #define IS_I2C_MODE(MODE) (((MODE) == I2C_Mode_I2C) || \ 102 ((MODE) == I2C_Mode_SMBusDevice) || \ 103 ((MODE) == I2C_Mode_SMBusHost)) 104 /** 105 * @} 106 */ 107 108 /** @defgroup I2C_acknowledgement 109 * @{ 110 */ 111 112 #define I2C_Ack_Enable ((uint32_t)0x00000000) 113 #define I2C_Ack_Disable I2C_CR2_NACK 114 115 #define IS_I2C_ACK(ACK) (((ACK) == I2C_Ack_Enable) || \ 116 ((ACK) == I2C_Ack_Disable)) 117 /** 118 * @} 119 */ 120 121 /** @defgroup I2C_acknowledged_address 122 * @{ 123 */ 124 125 #define I2C_AcknowledgedAddress_7bit ((uint32_t)0x00000000) 126 #define I2C_AcknowledgedAddress_10bit I2C_OAR1_OA1MODE 127 128 #define IS_I2C_ACKNOWLEDGE_ADDRESS(ADDRESS) (((ADDRESS) == I2C_AcknowledgedAddress_7bit) || \ 129 ((ADDRESS) == I2C_AcknowledgedAddress_10bit)) 130 /** 131 * @} 132 */ 133 134 /** @defgroup I2C_own_address1 135 * @{ 136 */ 137 138 #define IS_I2C_OWN_ADDRESS1(ADDRESS1) ((ADDRESS1) <= (uint32_t)0x000003FF) 139 /** 140 * @} 141 */ 142 143 /** @defgroup I2C_transfer_direction 144 * @{ 145 */ 146 147 #define I2C_Direction_Transmitter ((uint16_t)0x0000) 148 #define I2C_Direction_Receiver ((uint16_t)0x0400) 149 150 #define IS_I2C_DIRECTION(DIRECTION) (((DIRECTION) == I2C_Direction_Transmitter) || \ 151 ((DIRECTION) == I2C_Direction_Receiver)) 152 /** 153 * @} 154 */ 155 156 /** @defgroup I2C_DMA_transfer_requests 157 * @{ 158 */ 159 160 #define I2C_DMAReq_Tx I2C_CR1_TXDMAEN 161 #define I2C_DMAReq_Rx I2C_CR1_RXDMAEN 162 163 #define IS_I2C_DMA_REQ(REQ) ((((REQ) & (uint32_t)0xFFFF3FFF) == 0x00) && ((REQ) != 0x00)) 164 /** 165 * @} 166 */ 167 168 /** @defgroup I2C_slave_address 169 * @{ 170 */ 171 172 #define IS_I2C_SLAVE_ADDRESS(ADDRESS) ((ADDRESS) <= (uint16_t)0x03FF) 173 /** 174 * @} 175 */ 176 177 178 /** @defgroup I2C_own_address2 179 * @{ 180 */ 181 182 #define IS_I2C_OWN_ADDRESS2(ADDRESS2) ((ADDRESS2) <= (uint16_t)0x00FF) 183 184 /** 185 * @} 186 */ 187 188 /** @defgroup I2C_own_address2_mask 189 * @{ 190 */ 191 192 #define I2C_OA2_NoMask ((uint8_t)0x00) 193 #define I2C_OA2_Mask01 ((uint8_t)0x01) 194 #define I2C_OA2_Mask02 ((uint8_t)0x02) 195 #define I2C_OA2_Mask03 ((uint8_t)0x03) 196 #define I2C_OA2_Mask04 ((uint8_t)0x04) 197 #define I2C_OA2_Mask05 ((uint8_t)0x05) 198 #define I2C_OA2_Mask06 ((uint8_t)0x06) 199 #define I2C_OA2_Mask07 ((uint8_t)0x07) 200 201 #define IS_I2C_OWN_ADDRESS2_MASK(MASK) (((MASK) == I2C_OA2_NoMask) || \ 202 ((MASK) == I2C_OA2_Mask01) || \ 203 ((MASK) == I2C_OA2_Mask02) || \ 204 ((MASK) == I2C_OA2_Mask03) || \ 205 ((MASK) == I2C_OA2_Mask04) || \ 206 ((MASK) == I2C_OA2_Mask05) || \ 207 ((MASK) == I2C_OA2_Mask06) || \ 208 ((MASK) == I2C_OA2_Mask07)) 209 210 /** 211 * @} 212 */ 213 214 /** @defgroup I2C_timeout 215 * @{ 216 */ 217 218 #define IS_I2C_TIMEOUT(TIMEOUT) ((TIMEOUT) <= (uint16_t)0x0FFF) 219 220 /** 221 * @} 222 */ 223 224 /** @defgroup I2C_registers 225 * @{ 226 */ 227 228 #define I2C_Register_CR1 ((uint8_t)0x00) 229 #define I2C_Register_CR2 ((uint8_t)0x04) 230 #define I2C_Register_OAR1 ((uint8_t)0x08) 231 #define I2C_Register_OAR2 ((uint8_t)0x0C) 232 #define I2C_Register_TIMINGR ((uint8_t)0x10) 233 #define I2C_Register_TIMEOUTR ((uint8_t)0x14) 234 #define I2C_Register_ISR ((uint8_t)0x18) 235 #define I2C_Register_ICR ((uint8_t)0x1C) 236 #define I2C_Register_PECR ((uint8_t)0x20) 237 #define I2C_Register_RXDR ((uint8_t)0x24) 238 #define I2C_Register_TXDR ((uint8_t)0x28) 239 240 #define IS_I2C_REGISTER(REGISTER) (((REGISTER) == I2C_Register_CR1) || \ 241 ((REGISTER) == I2C_Register_CR2) || \ 242 ((REGISTER) == I2C_Register_OAR1) || \ 243 ((REGISTER) == I2C_Register_OAR2) || \ 244 ((REGISTER) == I2C_Register_TIMINGR) || \ 245 ((REGISTER) == I2C_Register_TIMEOUTR) || \ 246 ((REGISTER) == I2C_Register_ISR) || \ 247 ((REGISTER) == I2C_Register_ICR) || \ 248 ((REGISTER) == I2C_Register_PECR) || \ 249 ((REGISTER) == I2C_Register_RXDR) || \ 250 ((REGISTER) == I2C_Register_TXDR)) 251 /** 252 * @} 253 */ 254 255 /** @defgroup I2C_interrupts_definition 256 * @{ 257 */ 258 259 #define I2C_IT_ERRI I2C_CR1_ERRIE 260 #define I2C_IT_TCI I2C_CR1_TCIE 261 #define I2C_IT_STOPI I2C_CR1_STOPIE 262 #define I2C_IT_NACKI I2C_CR1_NACKIE 263 #define I2C_IT_ADDRI I2C_CR1_ADDRIE 264 #define I2C_IT_RXI I2C_CR1_RXIE 265 #define I2C_IT_TXI I2C_CR1_TXIE 266 267 #define IS_I2C_CONFIG_IT(IT) ((((IT) & (uint32_t)0xFFFFFF01) == 0x00) && ((IT) != 0x00)) 268 269 /** 270 * @} 271 */ 272 273 /** @defgroup I2C_flags_definition 274 * @{ 275 */ 276 277 #define I2C_FLAG_TXE I2C_ISR_TXE 278 #define I2C_FLAG_TXIS I2C_ISR_TXIS 279 #define I2C_FLAG_RXNE I2C_ISR_RXNE 280 #define I2C_FLAG_ADDR I2C_ISR_ADDR 281 #define I2C_FLAG_NACKF I2C_ISR_NACKF 282 #define I2C_FLAG_STOPF I2C_ISR_STOPF 283 #define I2C_FLAG_TC I2C_ISR_TC 284 #define I2C_FLAG_TCR I2C_ISR_TCR 285 #define I2C_FLAG_BERR I2C_ISR_BERR 286 #define I2C_FLAG_ARLO I2C_ISR_ARLO 287 #define I2C_FLAG_OVR I2C_ISR_OVR 288 #define I2C_FLAG_PECERR I2C_ISR_PECERR 289 #define I2C_FLAG_TIMEOUT I2C_ISR_TIMEOUT 290 #define I2C_FLAG_ALERT I2C_ISR_ALERT 291 #define I2C_FLAG_BUSY I2C_ISR_BUSY 292 293 #define IS_I2C_CLEAR_FLAG(FLAG) ((((FLAG) & (uint32_t)0xFFFF4000) == 0x00) && ((FLAG) != 0x00)) 294 295 #define IS_I2C_GET_FLAG(FLAG) (((FLAG) == I2C_FLAG_TXE) || ((FLAG) == I2C_FLAG_TXIS) || \ 296 ((FLAG) == I2C_FLAG_RXNE) || ((FLAG) == I2C_FLAG_ADDR) || \ 297 ((FLAG) == I2C_FLAG_NACKF) || ((FLAG) == I2C_FLAG_STOPF) || \ 298 ((FLAG) == I2C_FLAG_TC) || ((FLAG) == I2C_FLAG_TCR) || \ 299 ((FLAG) == I2C_FLAG_BERR) || ((FLAG) == I2C_FLAG_ARLO) || \ 300 ((FLAG) == I2C_FLAG_OVR) || ((FLAG) == I2C_FLAG_PECERR) || \ 301 ((FLAG) == I2C_FLAG_TIMEOUT) || ((FLAG) == I2C_FLAG_ALERT) || \ 302 ((FLAG) == I2C_FLAG_BUSY)) 303 304 /** 305 * @} 306 */ 307 308 309 /** @defgroup I2C_interrupts_definition 310 * @{ 311 */ 312 313 #define I2C_IT_TXIS I2C_ISR_TXIS 314 #define I2C_IT_RXNE I2C_ISR_RXNE 315 #define I2C_IT_ADDR I2C_ISR_ADDR 316 #define I2C_IT_NACKF I2C_ISR_NACKF 317 #define I2C_IT_STOPF I2C_ISR_STOPF 318 #define I2C_IT_TC I2C_ISR_TC 319 #define I2C_IT_TCR I2C_ISR_TCR 320 #define I2C_IT_BERR I2C_ISR_BERR 321 #define I2C_IT_ARLO I2C_ISR_ARLO 322 #define I2C_IT_OVR I2C_ISR_OVR 323 #define I2C_IT_PECERR I2C_ISR_PECERR 324 #define I2C_IT_TIMEOUT I2C_ISR_TIMEOUT 325 #define I2C_IT_ALERT I2C_ISR_ALERT 326 327 #define IS_I2C_CLEAR_IT(IT) ((((IT) & (uint32_t)0xFFFFC001) == 0x00) && ((IT) != 0x00)) 328 329 #define IS_I2C_GET_IT(IT) (((IT) == I2C_IT_TXIS) || ((IT) == I2C_IT_RXNE) || \ 330 ((IT) == I2C_IT_ADDR) || ((IT) == I2C_IT_NACKF) || \ 331 ((IT) == I2C_IT_STOPF) || ((IT) == I2C_IT_TC) || \ 332 ((IT) == I2C_IT_TCR) || ((IT) == I2C_IT_BERR) || \ 333 ((IT) == I2C_IT_ARLO) || ((IT) == I2C_IT_OVR) || \ 334 ((IT) == I2C_IT_PECERR) || ((IT) == I2C_IT_TIMEOUT) || \ 335 ((IT) == I2C_IT_ALERT)) 336 337 338 /** 339 * @} 340 */ 341 342 /** @defgroup I2C_ReloadEndMode_definition 343 * @{ 344 */ 345 346 #define I2C_Reload_Mode I2C_CR2_RELOAD 347 #define I2C_AutoEnd_Mode I2C_CR2_AUTOEND 348 #define I2C_SoftEnd_Mode ((uint32_t)0x00000000) 349 350 351 #define IS_RELOAD_END_MODE(MODE) (((MODE) == I2C_Reload_Mode) || \ 352 ((MODE) == I2C_AutoEnd_Mode) || \ 353 ((MODE) == I2C_SoftEnd_Mode)) 354 355 356 /** 357 * @} 358 */ 359 360 /** @defgroup I2C_StartStopMode_definition 361 * @{ 362 */ 363 364 #define I2C_No_StartStop ((uint32_t)0x00000000) 365 #define I2C_Generate_Stop I2C_CR2_STOP 366 #define I2C_Generate_Start_Read (uint32_t)(I2C_CR2_START | I2C_CR2_RD_WRN) 367 #define I2C_Generate_Start_Write I2C_CR2_START 368 369 370 #define IS_START_STOP_MODE(MODE) (((MODE) == I2C_Generate_Stop) || \ 371 ((MODE) == I2C_Generate_Start_Read) || \ 372 ((MODE) == I2C_Generate_Start_Write) || \ 373 ((MODE) == I2C_No_StartStop)) 374 375 376 /** 377 * @} 378 */ 379 380 /** 381 * @} 382 */ 383 384 /* Exported macro ------------------------------------------------------------*/ 385 /* Exported functions ------------------------------------------------------- */ 386 387 388 /* Initialization and Configuration functions *********************************/ 389 void I2C_DeInit(I2C_TypeDef* I2Cx); 390 void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct); 391 void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct); 392 void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState); 393 void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx); 394 void I2C_ITConfig(I2C_TypeDef* I2Cx, uint32_t I2C_IT, FunctionalState NewState); 395 void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState); 396 void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState); 397 void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint16_t Address, uint8_t Mask); 398 void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState); 399 void I2C_SlaveByteControlCmd(I2C_TypeDef* I2Cx, FunctionalState NewState); 400 void I2C_SlaveAddressConfig(I2C_TypeDef* I2Cx, uint16_t Address); 401 void I2C_10BitAddressingModeCmd(I2C_TypeDef* I2Cx, FunctionalState NewState); 402 403 /* Communications handling functions ******************************************/ 404 void I2C_AutoEndCmd(I2C_TypeDef* I2Cx, FunctionalState NewState); 405 void I2C_ReloadCmd(I2C_TypeDef* I2Cx, FunctionalState NewState); 406 void I2C_NumberOfBytesConfig(I2C_TypeDef* I2Cx, uint8_t Number_Bytes); 407 void I2C_MasterRequestConfig(I2C_TypeDef* I2Cx, uint16_t I2C_Direction); 408 void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState); 409 void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState); 410 void I2C_10BitAddressHeaderCmd(I2C_TypeDef* I2Cx, FunctionalState NewState); 411 void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState); 412 uint8_t I2C_GetAddressMatched(I2C_TypeDef* I2Cx); 413 uint16_t I2C_GetTransferDirection(I2C_TypeDef* I2Cx); 414 void I2C_TransferHandling(I2C_TypeDef* I2Cx, uint16_t Address, uint8_t Number_Bytes, uint32_t ReloadEndMode, uint32_t StartStopMode); 415 416 /* SMBUS management functions ************************************************/ 417 void I2C_SMBusAlertCmd(I2C_TypeDef* I2Cx, FunctionalState NewState); 418 void I2C_ClockTimeoutCmd(I2C_TypeDef* I2Cx, FunctionalState NewState); 419 void I2C_ExtendedClockTimeoutCmd(I2C_TypeDef* I2Cx, FunctionalState NewState); 420 void I2C_IdleClockTimeoutCmd(I2C_TypeDef* I2Cx, FunctionalState NewState); 421 void I2C_TimeoutAConfig(I2C_TypeDef* I2Cx, uint16_t Timeout); 422 void I2C_TimeoutBConfig(I2C_TypeDef* I2Cx, uint16_t Timeout); 423 void I2C_CalculatePEC(I2C_TypeDef* I2Cx, FunctionalState NewState); 424 void I2C_PECRequestCmd(I2C_TypeDef* I2Cx, FunctionalState NewState); 425 uint8_t I2C_GetPEC(I2C_TypeDef* I2Cx); 426 427 /* I2C registers management functions *****************************************/ 428 uint32_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register); 429 430 /* Data transfers management functions ****************************************/ 431 void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data); 432 uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx); 433 434 /* DMA transfers management functions *****************************************/ 435 void I2C_DMACmd(I2C_TypeDef* I2Cx, uint32_t I2C_DMAReq, FunctionalState NewState); 436 437 /* Interrupts and flags management functions **********************************/ 438 FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG); 439 void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG); 440 ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT); 441 void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT); 442 443 444 #ifdef __cplusplus 445 } 446 #endif 447 448 #endif /*__FT32F0XX_I2C_H */ 449 450 /** 451 * @} 452 */ 453 454 /** 455 * @} 456 */ 457 458 /************************ (C) COPYRIGHT FMD *****END OF FILE****/ 459