ESP32 I2C read issue with TCS34725

arpiabeka
Posts: 3
Joined: Fri May 04, 2018 5:28 pm

ESP32 I2C read issue with TCS34725

Postby arpiabeka » Fri May 04, 2018 5:42 pm

Hello,
I want to read the device id from a TCS34725 but it returns the last byte written in the communiation. (In this case TCS34725_ID.)
I am using this code:

#include "esp_log.h"
#include "driver/gpio.h"
#include <driver/i2c.h>
#include "freertos/FreeRTOS.h"
#include <stdio.h>

using namespace std;
#define TCS34725_ADDRESS 0x29
#define TCS34725_COMMAND_BIT 0x80
#define TCS34725_ID 0x12 /* 0x44 = TCS34721/TCS34725, 0x4D = TCS34723/TCS34727 */
#define SDA_PIN GPIO_NUM_5
#define SCL_PIN GPIO_NUM_4
#define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave*/
#define ACK_CHECK_DIS 0x0 /*!< I2C master will not check ack from slave */

void task(void *ignore) {
uint8_t data = 0;
int ret;
i2c_cmd_handle_t cmd;
i2c_config_t conf;

conf.mode = I2C_MODE_MASTER;
conf.sda_io_num = SDA_PIN;
conf.scl_io_num = SCL_PIN;
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
conf.master.clk_speed = 100000;
i2c_param_config(I2C_NUM_0, &conf);
i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0);

cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (TCS34725_ADDRESS << 1) | I2C_MASTER_WRITE, ACK_CHECK_EN);
i2c_master_write_byte(cmd, TCS34725_COMMAND_BIT, ACK_CHECK_EN);
i2c_master_write_byte(cmd, TCS34725_ID, ACK_CHECK_EN); // tried with ACK_CHECK_DIS also
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 100 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);

vTaskDelay(1000 / portTICK_PERIOD_MS);

cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (TCS34725_ADDRESS << 1) | I2C_MASTER_READ, ACK_CHECK_EN);
i2c_master_read_byte(cmd, &data, I2C_MASTER_NACK);
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 100 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
if (ret == ESP_OK) {
printf("ID %.2x ", data);
}
else
{
printf("No ack");
}

printf("\n");
vTaskDelete(NULL);
}
#ifdef __cplusplus
extern "C" {
#endif
void app_main() {
xTaskCreatePinnedToCore(&task, "task", 2048, NULL, 5, NULL, 1);
}
#ifdef __cplusplus
}
#endif

And here are the write and read result from the logic analyzer:
write.PNG
write.PNG (45.27 KiB) Viewed 2672 times
read.PNG
read.PNG (46.12 KiB) Viewed 2672 times
Any help is appreciated!

Thanks & Regards,
ArpiABeka

Who is online

Users browsing this forum: No registered users and 59 guests