我正在尝试使用连接到freeRTOS中的ESP32的FDC1004读取一些专用传感器。我在写16位FDC1004寄存器时遇到问题。我编写了一个测试代码,用于读取ID和写入配置寄存器。读取任务很好,我可以正确读取FDC1004(0x1004)的ID,但是当我尝试写入寄存器时,我只能正确读取8个LSB。这是我的i2c函数:
esp_err_t I2C_write(i2c_port_t i2c_num, uint8_t slave_address, uint8_t data_address, uint8_t *data_wr ,size_t size)
{
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (slave_address << 1) | WRITE_BIT, ACK_CHECK_EN);
i2c_master_write_byte(cmd, data_address, ACK_CHECK_EN);
i2c_master_write(cmd, data_wr, size, ACK_CHECK_EN);
//i2c_master_write(cmd, data_addres, 1, ACK_CHECK_EN);
//i2c_master_write(cmd, data_wr, size, ACK_CHECK_EN);
//i2c_master_write_byte(cmd, data_wr + size - 1, ACK_CHECK_EN);
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(i2c_num, cmd, 1000/portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
return ret;
}
esp_err_t I2C_read(i2c_port_t i2c_num, uint8_t slave_address, uint8_t *data_addres, uint8_t *data_rd , size_t size){
if (size == 0) {
return ESP_OK;
}
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (slave_address << 1) | WRITE_BIT, ACK_CHECK_EN);
i2c_master_write(cmd, data_addres, size, ACK_CHECK_EN);
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (slave_address << 1) | READ_BIT, ACK_CHECK_EN);
if (size > 1) {
i2c_master_read(cmd, data_rd, size - 1, ACK_VAL);
}
i2c_master_read_byte(cmd, data_rd + size - 1, NACK_VAL);
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
return ret;
}
void FDC1004_writeReg(i2c_port_t i2c_num, uint8_t reg_address, uint16_t data){
uint8_t tx_buff[2];
tx_buff[0] = data >> 8;
tx_buff[1] = data & 0x00FF;
ESP_ERROR_CHECK(I2C_write( i2c_num, FDC_ADDR, reg_address, tx_buff, 2));
vTaskDelay(20 / portTICK_PERIOD_MS);
}
uint16_t FDC01004_readReg(i2c_port_t i2c_num, uint8_t reg_address){
uint8_t read_buff[2];
uint16_t data;
ESP_ERROR_CHECK(I2C_read( i2c_num, FDC_ADDR, ®_address, read_buff, 2));
data = ((uint16_t)(read_buff[0] << 8) | read_buff[1]);
return data;
}
[当我尝试分别在寄存器0x08、0x09、0x0A和0x0B中写入0x10C0、0x30C0、0x50C0、0x70C0时,结果分别是:0xFFC0、0xFFC0、0xFFC0和0xFFC0。
希望您能帮助我。
问候
最后,在对代码进行一些测试之后,我将读取功能更改为如下:
esp_err_t I2C_read(i2c_port_t i2c_num, uint8_t slave_address, uint8_t data_addres, uint8_t *data_rd , size_t size){
if (size == 0) {
return ESP_OK;
}
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (slave_address << 1) | WRITE_BIT, ACK_CHECK_EN);
i2c_master_write_byte(cmd, data_addres, ACK_CHECK_EN);
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (slave_address << 1) | READ_BIT, ACK_CHECK_EN);
if (size > 1) {
i2c_master_read(cmd, data_rd, size - 1, ACK_VAL);
}
i2c_master_read_byte(cmd, data_rd + size - 1, NACK_VAL);
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
return ret;
}
读取功能中的错误之处在于,它使对reg地址的写操作的大小增加了一倍,而只需要一次。这种方式对我有用。