Hi,
I2C can't communication with below code and attached fig, kindly help where issues happened ,thanks.
#define OPT3001_SLAVE_ADDR 0x44 //从机地址
#define OPT3001_RESULT_ADDR 0x00 //结果寄存器
#define OPT3001_CONF_ADDR 0x01 //配置寄存器
#define OPT3001_LOWER_ADDR 0x02 //下限寄存器
#define OPT3001_UPPER_ADDR 0x03 //上限寄存器
#define OPT3001_MANU_ADDR 0x7E //厂商寄存器
#define OPT3001_DEVI_ADDR 0x7F //设备寄存器
/**
* @brief Read a register(s) over I2C
* @param [in] i2c_addr 7-bit I2C slave address of the ZMOD44xx
* @param [in] reg_addr address of internal register to read
* @param [out] buf destination buffer; must have at least a size of len
* @param [in] len number of bytes to read
* @return error code
*/
int8_t user_i2c_read(uint8_t i2c_addr, uint8_t reg_addr, uint8_t *buf,
uint8_t len)
{
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (i2c_addr << 1) | WRITE_BIT, ACK_VAL);
if (NULL != reg_addr)
{
i2c_master_write_byte(cmd, reg_addr, ACK_VAL);
}
i2c_master_write_byte(cmd, (i2c_addr << 1) | READ_BIT, ACK_VAL);
i2c_master_read(cmd, buf, len, ACK_VAL);
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "read_i2c_master_cmd_begin failed");
return ret;
}
i2c_cmd_link_delete(cmd);
return ret;
}
/**
* @brief Write a register(s) over I2C
* @param [in] i2c_addr 7-bit I2C slave address of the ZMOD4xxx
* @param [in] reg_addr address of internal register to write
* @param [in] buf source buffer; must have at least a size of len
* @param [in] len number of bytes to write
* @return error code
*/
int8_t user_i2c_write(uint8_t i2c_addr, uint8_t reg_addr, uint8_t *buf,
uint8_t len)
{
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (i2c_addr << 1) | WRITE_BIT, ACK_VAL);
if (NULL != reg_addr)
{
i2c_master_write_byte(cmd, reg_addr, ACK_VAL);
}
i2c_master_write(cmd, buf, len, ACK_VAL);
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "write_i2c_master_cmd_begin failed");
return ret;
}
i2c_cmd_link_delete(cmd);
return ret;
}
/*
功能:读取设备ID
输入:无
输出:无
*/
void user_opt3001_device_id(void)
{
uint8_t data[20] = {};
ID = user_i2c_read(OPT3001_SLAVE_ADDR, OPT3001_DEVI_ADDR, data, 10);
ESP_LOGI(TAG, "device_addr data :0x%x,%x", data[0], data[1]);
ESP_LOGI(TAG, "device_addr data :0x%x,%x", data[2], data[3]);
ESP_LOGI(TAG, "device_addr data :0x%x,%x", data[4], data[5]);
ESP_LOGI(TAG, "device_addr data :0x%x,%x", data[6], data[7]);
ESP_LOGI(TAG, "device_addr data :0x%x,%x", data[8], data[9]);
}