I2C communication
Posted: Fri May 26, 2017 1:06 am
Hi,
I am trying to use ESP32-IDF library in C++. I am trying to communication with LSM9DS1 sensor via i2c, but so far not able to to read even WHO_AM_I register.
I have checked the sensor my Arduino board and it works fine and communicated flawlessly.
Based on my little understanding I am using following code to communicate.
Initialization code ...
Here is how I am reading the WHO_AM_I register
Please let me know if you know what I am doing wrong. I also read through the datasheet, this should be the sequence.
Thanks,
avi
I am trying to use ESP32-IDF library in C++. I am trying to communication with LSM9DS1 sensor via i2c, but so far not able to to read even WHO_AM_I register.
I have checked the sensor my Arduino board and it works fine and communicated flawlessly.
Based on my little understanding I am using following code to communicate.
Initialization code ...
Code: Select all
i2c_config_t conf;
conf.mode = I2C_MODE_MASTER;
conf.sda_io_num = GPIO_NUM_21;
conf.scl_io_num = GPIO_NUM_22;
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, conf.mode, 0,0,0);
uint8_t sensor_data_h, sensor_data_l;
read_mag_channel(&sensor_data_h, &sensor_data_l);
Code: Select all
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd,(LSM9DS1_M << 1)| WRITE_BIT, ACK_CHECK_EN );
i2c_master_write_byte(cmd, WHO_AM_I_M, ACK_CHECK_EN);
i2c_master_stop(cmd);
int ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000/portTICK_RATE_MS);
printf(" error value %d\n", ret);
i2c_cmd_link_delete(cmd);
vTaskDelay(30/portTICK_RATE_MS);
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (LSM9DS1_M << 1) | READ_BIT, ACK_CHECK_EN);
i2c_master_read_byte(cmd, data_h, ACK_VAL);
//i2c_master_read_byte(cmd, data_l, NACK_VAL);
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_RATE_MS);
printf(" error value %d\n", ret);
i2c_cmd_link_delete(cmd);
Thanks,
avi