1 /*********************************************************************************************************//** 2 * @file ht32f5xxxx_i2c.h 3 * @version $Rev:: 7698 $ 4 * @date $Date:: 2024-04-15 #$ 5 * @brief The header file of the I2C library. 6 ************************************************************************************************************* 7 * @attention 8 * 9 * Firmware Disclaimer Information 10 * 11 * 1. The customer hereby acknowledges and agrees that the program technical documentation, including the 12 * code, which is supplied by Holtek Semiconductor Inc., (hereinafter referred to as "HOLTEK") is the 13 * proprietary and confidential intellectual property of HOLTEK, and is protected by copyright law and 14 * other intellectual property laws. 15 * 16 * 2. The customer hereby acknowledges and agrees that the program technical documentation, including the 17 * code, is confidential information belonging to HOLTEK, and must not be disclosed to any third parties 18 * other than HOLTEK and the customer. 19 * 20 * 3. The program technical documentation, including the code, is provided "as is" and for customer reference 21 * only. After delivery by HOLTEK, the customer shall use the program technical documentation, including 22 * the code, at their own risk. HOLTEK disclaims any expressed, implied or statutory warranties, including 23 * the warranties of merchantability, satisfactory quality and fitness for a particular purpose. 24 * 25 * <h2><center>Copyright (C) Holtek Semiconductor Inc. All rights reserved</center></h2> 26 ************************************************************************************************************/ 27 28 /* Define to prevent recursive inclusion -------------------------------------------------------------------*/ 29 #ifndef __HT32F5XXXX_I2C_H 30 #define __HT32F5XXXX_I2C_H 31 32 #ifdef __cplusplus 33 extern "C" { 34 #endif 35 36 /* Includes ------------------------------------------------------------------------------------------------*/ 37 #include "ht32.h" 38 39 /** @addtogroup HT32F5xxxx_Peripheral_Driver HT32F5xxxx Peripheral Driver 40 * @{ 41 */ 42 43 /** @addtogroup I2C 44 * @{ 45 */ 46 47 48 /* Exported types ------------------------------------------------------------------------------------------*/ 49 /** @defgroup I2C_Exported_Types I2C exported types 50 * @{ 51 */ 52 53 #if (LIBCFG_I2C_TWO_DEV_ADDR) 54 typedef u8 I2C_AddressTypeDef; 55 #else 56 typedef u16 I2C_AddressTypeDef; 57 #endif 58 59 typedef struct 60 { 61 u8 I2C_GeneralCall; 62 u8 I2C_AddressingMode; 63 u8 I2C_Acknowledge; 64 u8 I2C_SpeedOffset; /* Offset value to reach real speed, recommended I2C_SpeedOffset = I2C PCLK/8000000 */ 65 /* which based on 4.7K Pull up */ 66 u32 I2C_Speed; 67 u16 I2C_OwnAddress; 68 } I2C_InitTypeDef; 69 70 #if (LIBCFG_I2C_TWO_DEV_ADDR) 71 typedef enum 72 { 73 I2C_DEV_ADDR_0 = 0, 74 I2C_DEV_ADDR_1 75 } I2C_ADDR_Enum; 76 77 #define IS_I2C_ADDR(x) ((x == I2C_DEV_ADDR_0) || (x == I2C_DEV_ADDR_1)) 78 #endif 79 /** 80 * @} 81 */ 82 83 /* Exported constants --------------------------------------------------------------------------------------*/ 84 /** @defgroup I2C_Exported_Constants I2C exported constants 85 * @{ 86 */ 87 #define I2C_GENERALCALL_ENABLE ((u32)0x00000004) 88 #define I2C_GENERALCALL_DISABLE ((u32)0x00000000) 89 90 #define IS_I2C_GENERAL_CALL(CALL) ((CALL == I2C_GENERALCALL_ENABLE) || \ 91 (CALL == I2C_GENERALCALL_DISABLE)) 92 93 #define I2C_ADDRESSING_7BIT ((u32)0x00000000) 94 #if (LIBCFG_I2C_NO_10BIT_MODE) 95 #define IS_I2C_ACKNOWLEDGE_ADDRESS(ADDRESS) ((ADDRESS == I2C_ADDRESSING_7BIT)) 96 #else 97 #define I2C_ADDRESSING_10BIT ((u32)0x00000080) 98 99 #define IS_I2C_ACKNOWLEDGE_ADDRESS(ADDRESS) ((ADDRESS == I2C_ADDRESSING_7BIT) || \ 100 (ADDRESS == I2C_ADDRESSING_10BIT)) 101 #endif 102 103 #define I2C_ACK_ENABLE ((u32)0x00000001) 104 #define I2C_ACK_DISABLE ((u32)0x00000000) 105 106 #define IS_I2C_ACKNOWLEDGE(ACKNOWLEDGE) ((ACKNOWLEDGE == I2C_ACK_ENABLE) || \ 107 (ACKNOWLEDGE == I2C_ACK_DISABLE)) 108 109 #if (LIBCFG_I2C_TWO_DEV_ADDR) 110 #define I2C_DEV_ADDR0_ENABLE ((u32)0x00008000) 111 #define I2C_DEV_ADDR0_DISABLE ((u32)0x00000000) 112 113 #define IS_I2C_ADDR0_ENABLE(ADDR) ((ADDR == I2C_ADDR0_ENABLE) || \ 114 (ADDR == I2C_ADDR0_DISABLE)) 115 116 #define I2C_DEV_ADDR1_ENABLE ((u32)0x80000000) 117 #define I2C_DEV_ADDR1_DISABLE ((u32)0x00000000) 118 119 #define IS_I2C_ADDR1(ADDR) ((ADDR == I2C_ADDR1_ENABLE) || \ 120 (ADDR == I2C_ADDR1_DISABLE)) 121 #endif 122 123 #define I2C_INT_STA ((u32)0x00000001) 124 #define I2C_INT_STO ((u32)0x00000002) 125 #define I2C_INT_ADRS ((u32)0x00000004) 126 #define I2C_INT_GCS ((u32)0x00000008) 127 #if (LIBCFG_I2C_NO_ARBLOS == 0) 128 #define I2C_INT_ARBLOS ((u32)0x00000100) 129 #endif 130 #define I2C_INT_RXNACK ((u32)0x00000200) 131 #define I2C_INT_BUSERR ((u32)0x00000400) 132 #define I2C_INT_TOUT ((u32)0x00000800) 133 #define I2C_INT_RXDNE ((u32)0x00010000) 134 #define I2C_INT_TXDE ((u32)0x00020000) 135 #define I2C_INT_RXBF ((u32)0x00040000) 136 #if (LIBCFG_I2C_NO_ARBLOS) 137 #define I2C_INT_ALL ((u32)0x00070E0F) 138 #else 139 #define I2C_INT_ALL ((u32)0x00070F0F) 140 #endif 141 142 #if (LIBCFG_I2C_NO_ARBLOS) 143 #define IS_I2C_INT(int) (((int & 0xFFF8F1F0) == 0x0) && (int != 0x0)) 144 #else 145 #define IS_I2C_INT(int) (((int & 0xFFF8F0F0) == 0x0) && (int != 0x0)) 146 #endif 147 148 #define I2C_MASTER_READ ((u32)0x00000400) 149 #define I2C_MASTER_WRITE ((u32)0x00000000) 150 151 #define IS_I2C_DIRECTION(DIRECTION) ((DIRECTION == I2C_MASTER_READ) || \ 152 (DIRECTION == I2C_MASTER_WRITE)) 153 154 155 #define I2C_REGISTER_CR ((u8)0x00) 156 #define I2C_REGISTER_IER ((u8)0x04) 157 #define I2C_REGISTER_ADDR ((u8)0x08) 158 #define I2C_REGISTER_SR ((u8)0x0C) 159 #define I2C_REGISTER_SHPGR ((u8)0x10) 160 #define I2C_REGISTER_SLPGR ((u8)0x14) 161 #define I2C_REGISTER_DR ((u8)0x18) 162 #define I2C_REGISTER_BFCLR ((u8)0x1C) 163 #define I2C_REGISTER_TAR ((u8)0x20) 164 165 #define IS_I2C_REGISTER(REGISTER) ((REGISTER == I2C_REGISTER_CR) || \ 166 (REGISTER == I2C_REGISTER_IER) || \ 167 (REGISTER == I2C_REGISTER_ADDR) || \ 168 (REGISTER == I2C_REGISTER_SR) || \ 169 (REGISTER == I2C_REGISTER_SHPGR) || \ 170 (REGISTER == I2C_REGISTER_SLPGR) || \ 171 (REGISTER == I2C_REGISTER_DR) || \ 172 (REGISTER == I2C_REGISTER_BFCLR) || \ 173 (REGISTER == I2C_REGISTER_TAR)) 174 175 176 #define I2C_FLAG_STA ((u32)0x00000001) 177 #define I2C_FLAG_STO ((u32)0x00000002) 178 #define I2C_FLAG_ADRS ((u32)0x00000004) 179 #define I2C_FLAG_GCS ((u32)0x00000008) 180 #if (LIBCFG_I2C_NO_ARBLOS) 181 #define IS_FLAG_ARBLOS(x) (0) 182 #else 183 #define I2C_FLAG_ARBLOS ((u32)0x00000100) 184 #define IS_FLAG_ARBLOS(x) (x == I2C_FLAG_ARBLOS) 185 #endif 186 #define I2C_FLAG_RXNACK ((u32)0x00000200) 187 #define I2C_FLAG_BUSERR ((u32)0x00000400) 188 #define I2C_FLAG_TOUTF ((u32)0x00000800) 189 #define I2C_FLAG_RXDNE ((u32)0x00010000) 190 #define I2C_FLAG_TXDE ((u32)0x00020000) 191 #define I2C_FLAG_RXBF ((u32)0x00040000) 192 #define I2C_FLAG_BUSBUSY ((u32)0x00080000) 193 #define I2C_FLAG_MASTER ((u32)0x00100000) 194 #define I2C_FLAG_TXNRX ((u32)0x00200000) 195 196 #define IS_I2C_FLAG(FLAG) ((FLAG == I2C_FLAG_STA) || \ 197 (FLAG == I2C_FLAG_STO) || \ 198 (FLAG == I2C_FLAG_ADRS) || \ 199 (FLAG == I2C_FLAG_GCS) || \ 200 IS_FLAG_ARBLOS(FLAG) || \ 201 (FLAG == I2C_FLAG_RXNACK) || \ 202 (FLAG == I2C_FLAG_BUSERR) || \ 203 (FLAG == I2C_FLAG_TOUTF) || \ 204 (FLAG == I2C_FLAG_RXDNE) || \ 205 (FLAG == I2C_FLAG_TXDE) || \ 206 (FLAG == I2C_FLAG_RXBF) || \ 207 (FLAG == I2C_FLAG_BUSBUSY)|| \ 208 (FLAG == I2C_FLAG_MASTER) || \ 209 (FLAG == I2C_FLAG_TXNRX)) 210 211 #define IS_I2C_CLEAR_FLAG(FLAG) (IS_FLAG_ARBLOS(FLAG) || \ 212 (FLAG == I2C_FLAG_RXNACK) || \ 213 (FLAG == I2C_FLAG_BUSERR) || \ 214 (FLAG == I2C_FLAG_TOUTF)) 215 216 #define I2C_MASTER_SEND_START ((u32)0x00180001) 217 #define I2C_MASTER_RECEIVER_MODE ((u32)0x00180004) 218 #define I2C_MASTER_TRANSMITTER_MODE ((u32)0x003A0004) 219 #define I2C_MASTER_RX_NOT_EMPTY ((u32)0x00190000) 220 #define I2C_MASTER_RX_NOT_EMPTY_NOBUSY ((u32)0x00010000) 221 #define I2C_MASTER_TX_EMPTY ((u32)0x003A0000) 222 #define I2C_MASTER_RX_BUFFER_FULL ((u32)0x001D0000) 223 #define I2C_SLAVE_ACK_TRANSMITTER_ADDRESS ((u32)0x002A0004) 224 #define I2C_SLAVE_ACK_RECEIVER_ADDRESS ((u32)0x00080004) 225 #define I2C_SLAVE_ACK_GCALL_ADDRESS ((u32)0x00080008) 226 #define I2C_SLAVE_RX_NOT_EMPTY ((u32)0x00090000) 227 #define I2C_SLAVE_RX_NOT_EMPTY_STOP ((u32)0x00010002) 228 #define I2C_SLAVE_TX_EMPTY ((u32)0x002A0000) 229 #define I2C_SLAVE_RX_BUFFER_FULL ((u32)0x000D0000) 230 #define I2C_SLAVE_RECEIVED_NACK ((u32)0x00080200) 231 #define I2C_SLAVE_RECEIVED_NACK_STOP ((u32)0x00000202) 232 #define I2C_SLAVE_STOP_DETECTED ((u32)0x00000002) 233 234 235 #define IS_I2C_STATUS(STATUS) ((STATUS == I2C_MASTER_SEND_START) || \ 236 (STATUS == I2C_MASTER_RECEIVER_MODE) || \ 237 (STATUS == I2C_MASTER_TRANSMITTER_MODE) || \ 238 (STATUS == I2C_MASTER_RX_NOT_EMPTY) || \ 239 (STATUS == I2C_MASTER_RX_NOT_EMPTY_NOBUSY) || \ 240 (STATUS == I2C_MASTER_TX_EMPTY) || \ 241 (STATUS == I2C_MASTER_RX_BUFFER_FULL) || \ 242 (STATUS == I2C_SLAVE_ACK_TRANSMITTER_ADDRESS) || \ 243 (STATUS == I2C_SLAVE_ACK_RECEIVER_ADDRESS) || \ 244 (STATUS == I2C_SLAVE_ACK_GCALL_ADDRESS) || \ 245 (STATUS == I2C_SLAVE_RX_NOT_EMPTY) || \ 246 (STATUS == I2C_SLAVE_RX_NOT_EMPTY_STOP) || \ 247 (STATUS == I2C_SLAVE_TX_EMPTY) || \ 248 (STATUS == I2C_SLAVE_RX_BUFFER_FULL) || \ 249 (STATUS == I2C_SLAVE_RECEIVED_NACK) || \ 250 (STATUS == I2C_SLAVE_RECEIVED_NACK_STOP) || \ 251 (STATUS == I2C_SLAVE_STOP_DETECTED)) 252 253 #if (LIBCFG_PDMA) 254 #define I2C_PDMAREQ_TX ((u32)0x00000100) 255 #define I2C_PDMAREQ_RX ((u32)0x00000200) 256 257 #define IS_I2C_PDMA_REQ(REQ) (((REQ & 0xFFFFFCFF) == 0x0) && (REQ != 0x0)) 258 #endif 259 260 #define I2C_PRESCALER_1 ((u32)0x00000000) 261 #define I2C_PRESCALER_2 ((u32)0x00010000) 262 #define I2C_PRESCALER_4 ((u32)0x00020000) 263 #define I2C_PRESCALER_8 ((u32)0x00030000) 264 #if (LIBCFG_I2C_PRESCALER_2BIT == 0) 265 #define I2C_PRESCALER_16 ((u32)0x00040000) 266 #define I2C_PRESCALER_32 ((u32)0x00050000) 267 #define I2C_PRESCALER_64 ((u32)0x00060000) 268 #define I2C_PRESCALER_128 ((u32)0x00070000) 269 #endif 270 271 #if (LIBCFG_I2C_PRESCALER_2BIT) 272 #define IS_I2C_PRESCALER(PRESCALER) ((PRESCALER == I2C_PRESCALER_1) || \ 273 (PRESCALER == I2C_PRESCALER_2) || \ 274 (PRESCALER == I2C_PRESCALER_4) || \ 275 (PRESCALER == I2C_PRESCALER_8)) 276 #else 277 #define IS_I2C_PRESCALER(PRESCALER) ((PRESCALER == I2C_PRESCALER_1) || \ 278 (PRESCALER == I2C_PRESCALER_2) || \ 279 (PRESCALER == I2C_PRESCALER_4) || \ 280 (PRESCALER == I2C_PRESCALER_8) || \ 281 (PRESCALER == I2C_PRESCALER_16) || \ 282 (PRESCALER == I2C_PRESCALER_32) || \ 283 (PRESCALER == I2C_PRESCALER_64) || \ 284 (PRESCALER == I2C_PRESCALER_128)) 285 #endif 286 287 #if (LIBCFG_I2C_NO_ADDR_MASK == 0) 288 #define I2C_MASKBIT_0 ((u32)0x00000001) 289 #define I2C_MASKBIT_1 ((u32)0x00000002) 290 #define I2C_MASKBIT_2 ((u32)0x00000004) 291 #define I2C_MASKBIT_3 ((u32)0x00000008) 292 #define I2C_MASKBIT_4 ((u32)0x00000010) 293 #define I2C_MASKBIT_5 ((u32)0x00000020) 294 #define I2C_MASKBIT_6 ((u32)0x00000040) 295 #define I2C_MASKBIT_7 ((u32)0x00000080) 296 #define I2C_MASKBIT_8 ((u32)0x00000100) 297 #define I2C_MASKBIT_9 ((u32)0x00000200) 298 299 300 #define IS_I2C_ADDRESS_MASK(MASK) ((MASK == I2C_MASKBIT_0) || \ 301 (MASK == I2C_MASKBIT_1) || \ 302 (MASK == I2C_MASKBIT_2) || \ 303 (MASK == I2C_MASKBIT_3) || \ 304 (MASK == I2C_MASKBIT_4) || \ 305 (MASK == I2C_MASKBIT_5) || \ 306 (MASK == I2C_MASKBIT_6) || \ 307 (MASK == I2C_MASKBIT_7) || \ 308 (MASK == I2C_MASKBIT_8) || \ 309 (MASK == I2C_MASKBIT_9)) 310 #endif 311 312 #define IS_I2C(x) (IS_I2C0(x) || IS_I2C1(x) || IS_I2C2(x)) 313 #define IS_I2C0(x) (x == HT_I2C0) 314 315 #if (LIBCFG_I2C1) 316 #define IS_I2C1(x) (x == HT_I2C1) 317 #else 318 #define IS_I2C1(x) (0) 319 #endif 320 321 #if (LIBCFG_I2C2) 322 #define IS_I2C2(x) (x == HT_I2C2) 323 #else 324 #define IS_I2C2(x) (0) 325 #endif 326 327 #if (LIBCFG_I2C_NO_10BIT_MODE) 328 #define IS_I2C_ADDRESS(ADDRESS) (ADDRESS <= 0x7F) 329 #else 330 #define IS_I2C_ADDRESS(ADDRESS) (ADDRESS <= 0x3FF) 331 #endif 332 333 #define IS_I2C_SPEED(SPEED) ((SPEED >= 1) && (SPEED <= 1000000)) 334 335 #define IS_I2C_SCL_HIGH(HIGH) (HIGH <= 0xFFFF) 336 337 #define IS_I2C_SCL_LOW(LOW) (LOW <= 0xFFFF) 338 339 #if (LIBCFG_I2C_TOUT_COUNT_8BIT) 340 #define IS_I2C_TIMEOUT(TIMEOUT) (TIMEOUT <= 0xFF) 341 #else 342 #define IS_I2C_TIMEOUT(TIMEOUT) (TIMEOUT <= 0xFFFF) 343 #endif 344 345 #define SEQ_FILTER_DISABLE ((u32)0x00000000) 346 #define SEQ_FILTER_1_PCLK ((u32)0x00004000) 347 #define SEQ_FILTER_2_PCLK ((u32)0x00008000) 348 349 #define IS_I2C_SEQ_FILTER_MASK(CONFIG) ((CONFIG == SEQ_FILTER_DISABLE) || \ 350 (CONFIG == SEQ_FILTER_1_PCLK) || \ 351 (CONFIG == SEQ_FILTER_2_PCLK)) 352 /** 353 * @} 354 */ 355 356 /* Exported functions --------------------------------------------------------------------------------------*/ 357 /** @defgroup I2C_Exported_Functions I2C exported functions 358 * @{ 359 */ 360 void I2C_DeInit(HT_I2C_TypeDef* I2Cx); 361 void I2C_Init(HT_I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStructure); 362 void I2C_StructInit(I2C_InitTypeDef* I2C_InitStructure); 363 void I2C_Cmd(HT_I2C_TypeDef* I2Cx, ControlStatus NewState); 364 void I2C_GenerateSTOP(HT_I2C_TypeDef* I2Cx); 365 void I2C_IntConfig(HT_I2C_TypeDef* I2Cx, u32 I2C_Int, ControlStatus NewState); 366 void I2C_GeneralCallCmd(HT_I2C_TypeDef* I2Cx, ControlStatus NewState); 367 void I2C_AckCmd(HT_I2C_TypeDef* I2Cx, ControlStatus NewState); 368 void I2C_SetOwnAddress(HT_I2C_TypeDef* I2Cx, I2C_AddressTypeDef I2C_Address); 369 #if (LIBCFG_I2C_TWO_DEV_ADDR) 370 void I2C_SetOwnAddress1(HT_I2C_TypeDef* I2Cx, I2C_AddressTypeDef I2C_Address); 371 void I2C_OwnAddressCmd(HT_I2C_TypeDef* I2Cx, I2C_ADDR_Enum Address, ControlStatus NewState); 372 #endif 373 void I2C_TargetAddressConfig(HT_I2C_TypeDef* I2Cx, I2C_AddressTypeDef I2C_Address, u32 I2C_Direction); 374 void I2C_SendData(HT_I2C_TypeDef* I2Cx, u8 I2C_Data); 375 u8 I2C_ReceiveData(HT_I2C_TypeDef* I2Cx); 376 u32 I2C_ReadRegister(HT_I2C_TypeDef* I2Cx, u8 I2C_Register); 377 FlagStatus I2C_GetFlagStatus(HT_I2C_TypeDef* I2Cx, u32 I2C_Flag); 378 ErrStatus I2C_CheckStatus(HT_I2C_TypeDef* I2Cx, u32 I2C_Status); 379 void I2C_ClearFlag(HT_I2C_TypeDef* I2Cx, u32 I2C_Flag); 380 void I2C_SetSCLHighPeriod(HT_I2C_TypeDef* I2Cx, u32 I2C_HighPeriod); 381 void I2C_SetSCLLowPeriod(HT_I2C_TypeDef* I2Cx, u32 I2C_LowPeriod); 382 #if (LIBCFG_PDMA) 383 void I2C_PDMACmd(HT_I2C_TypeDef* I2Cx, u32 I2C_PDMAREQ, ControlStatus NewState); 384 void I2C_PDMANACKCmd(HT_I2C_TypeDef* I2Cx, ControlStatus NewState); 385 #endif 386 void I2C_TimeOutCmd(HT_I2C_TypeDef* I2Cx, ControlStatus NewState); 387 void I2C_SetTimeOutValue(HT_I2C_TypeDef* I2Cx, u32 I2C_Timeout); 388 void I2C_SetTimeOutPrescaler(HT_I2C_TypeDef* I2Cx, u32 I2C_Prescaler); 389 #if (LIBCFG_I2C_NO_ADDR_MASK == 0) 390 void I2C_AddressMaskConfig(HT_I2C_TypeDef* I2Cx, u32 I2C_Mask); 391 #endif 392 u16 I2C_GetAddressBuffer(HT_I2C_TypeDef* I2Cx); 393 void I2C_CombFilterCmd(HT_I2C_TypeDef* I2Cx, ControlStatus NewState); 394 void I2C_SequentialFilterConfig(HT_I2C_TypeDef* I2Cx, u32 Seq_Filter_Select); 395 /** 396 * @} 397 */ 398 399 400 /** 401 * @} 402 */ 403 404 /** 405 * @} 406 */ 407 408 #ifdef __cplusplus 409 } 410 #endif 411 412 #endif 413