Hello Robin, Im sharing my i2c communication code with you. Im using AXI IIC bus to connect it to the Sensor. I've designed the block in vivado and have written the software code in Vitis IDE. Here is my i2c code.
XIic IicInstance; // Declare the I2C instance as a global variable
// Function to initialize the I2C communication
BME68X_INTF_RET_TYPE bme68x_i2c_init(void) {
// Initialize the I2C instance for your platform.
XIic_Config *IicConfig;
IicConfig = XIic_LookupConfig(IIC_INSTANCE);
if (XIic_Initialize(&IicInstance, IIC_INSTANCE) != XST_SUCCESS) {
// Handle initialization error
return BME68X_E_COM_FAIL;
}
return BME68X_OK;
}
// Function to read data from a specific I2C device at a register address
BME68X_INTF_RET_TYPE bme68x_i2c_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, void *intf_ptr) {
// Set the target device address.
uint8_t device_addr = *(uint8_t*)intf_ptr;
if (XIic_SetAddress(&IicInstance, XII_ADDR_TO_SEND_TYPE, device_addr) != XST_SUCCESS) {
// Handle address setting error
return BME68X_E_COM_FAIL;
}
// Perform the I2C read operation to read data from the sensor.
if (XIic_MasterSend(&IicInstance, ®_addr, 1) != XST_SUCCESS) {
// Handle I2C register address write error
return BME68X_E_COM_FAIL;
}
if (XIic_MasterRecv(&IicInstance, reg_data, len) != XST_SUCCESS) {
// Handle I2C read error
return BME68X_E_COM_FAIL;
}
return BME68X_OK;
}
BME68X_INTF_RET_TYPE bme68x_i2c_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len, void *intf_ptr) {
// Set the target device address.
uint8_t device_addr = *(uint8_t*)intf_ptr;
(void)intf_ptr;
if (XIic_SetAddress(&IicInstance, XII_ADDR_TO_SEND_TYPE, device_addr) != XST_SUCCESS) {
// Handle address setting error
return BME68X_E_COM_FAIL;
}
// Perform the I2C write operation to write data to the sensor.
if (XIic_MasterSend(&IicInstance, (u8 *)reg_data, len) != XST_SUCCESS) {
// Handle I2C write error
return BME68X_E_COM_FAIL;
}
return BME68X_OK;// Return success or your custom success code
} Best Regards,
... View more