#ifndef GD32E23X_HW_I2C_H
#define GD32E23X_HW_I2C_H

// #define I2C1_ENABLE

#define I2C0_BUS_RCU          RCU_I2C0

#define I2C0_GPIO_RCU         RCU_GPIOF
#define I2C0_GPIO_PORT        GPIOF
#define I2C0_GPIO_SDA_PIN     GPIO_PIN_0
#define I2C0_GPIO_SCL_PIN     GPIO_PIN_1
#define I2C0_GPIO_AF          GPIO_AF_1

#define I2C0_SPEED            400000U
#define I2C0_DCTY             I2C_DTCY_2
#define I2C0_ADDR7            0xA0

#ifdef I2C1_ENABLE
#define I2C1_BUS_RCU          RCU_I2C1

#define I2C1_GPIO_RCU         RCU_GPIOA
#define I2C1_GPIO_PORT        GPIOA
#define I2C1_GPIO_SDA_PIN     GPIO_PIN_1
#define I2C1_GPIO_SCL_PIN     GPIO_PIN_0
#define I2C1_GPIO_AF          GPIO_AF_4

#define I2C1_SPEED            400000U
#define I2C1_DCTY             I2C_DTCY_2
#define I2C1_ADDR7            0xA1
#endif









#define I2C_TIME_OUT          (uint16_t)(5000)
#define I2C_OK                1
#define I2C_FAIL              0
#define I2C_END               1

typedef enum {
    I2C_START = 0,
    I2C_SEND_ADDR,
    I2C_CLEAR_ADDRESS_FLAG,
    I2C_TRANSMIT_DATA,
    I2C_STOP
} i2c_process_enum;

typedef struct
{
    uint32_t i2c_gpio_rcu;
    uint32_t i2c_gpio_port;
    uint32_t i2c_gpio_sda_pin;
    uint32_t i2c_gpio_scl_pin;
    uint32_t i2c_gpio_af;
    uint32_t i2c_speed;
    uint32_t i2c_dcty;
    uint32_t i2c_addr7;
}i2c_parameter_struct;




/**
 * \brief Return codes for IIC APIs.
 * @{
 */
#define IIC_SUCCESS              0
#define IIC_INVALID_ARGUMENT     1
#define IIC_ARBITRATION_LOST     2
#define IIC_NO_CHIP_FOUND        3
#define IIC_RECEIVE_OVERRUN      4
#define IIC_RECEIVE_NACK         5
#define IIC_SEND_OVERRUN         6
#define IIC_SEND_NACK            7
#define IIC_BUSY                 8
#define IIC_ERROR_TIMEOUT        9
/**
 * @}
 */

void i2c_master_initialize1(void);
void i2c_master_initialize3(void);
void i2c_master_init(void);
void i2c_master_deinit1(void);
void i2c_master_deinit3(void);

uint8_t i2c_master_read_register1(unsigned char Address, unsigned char RegisterAddr, unsigned short RegisterLen, unsigned char *RegisterValue);
uint8_t i2c_master_read_register3(unsigned char Address, unsigned char RegisterAddr, unsigned short RegisterLen, unsigned char *RegisterValue);

uint8_t i2c_master_read_register1_raw(unsigned char Address, unsigned short len, unsigned char *data);
uint8_t i2c_master_read_register3_raw(unsigned char Address, unsigned short len, unsigned char *data);


uint8_t i2c_master_write_register1(unsigned char Address, unsigned char RegisterAddr, unsigned short RegisterLen, unsigned char *RegisterValue);
uint8_t i2c_master_write_register3(unsigned char Address, unsigned char RegisterAddr, unsigned short RegisterLen, unsigned char *RegisterValue);

uint8_t i2c_master_write_register1_raw(unsigned char Address, unsigned short len, unsigned char *data);
uint8_t i2c_master_write_register3_raw(unsigned char Address, unsigned short len, unsigned char *data);


void i2c0_master_init(void);


int read_ir_mlx90614(void);

void i2c_struct_para_init(i2c_parameter_struct* initpara);
void i2c_init(uint32_t i2c_periph, i2c_parameter_struct* initpara);
void i2c0_config(void);


#endif /* GD32E23X_HW_I2C_H */