1
\$\begingroup\$

I´m trying to read registers 0x28 through 0x2D from the L3G4200D (gyroscope, datasheet), which is part of the GY-80 board, using the esp32 (I2C info) as a master, which is part of the LOLIN32 board. But I am getting the first byte repeated eight times instead and I can´t figure out why. I do get the right result when reading the registers one by one.

The registers are found in the datasheet here:
registers 0x28 through 0x2D in the datasheet

This is the I2C packet seen trough my oscilloscope (picoscope):
I2C packet
Zoomed in on the write part:
I2C packet write part
Zoomed in on the read part:
I2C packet read part

I´m using this code to send the packet:

 
//I2C init:
  i2c_config_t conf;
  conf.mode = I2C_MODE_MASTER;
  conf.sda_io_num = GPIO_NUM_16;
  conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
  conf.scl_io_num = GPIO_NUM_17;
  conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
  conf.master.clk_speed = 400000;
  i2c_param_config(I2C_NUM_0, &conf);
  i2c_driver_install(I2C_NUM_0, conf.mode, 0, 0, 0);

//function call:
  uint8_t gyroData[6];
  getWireRegister(GYRO, OUT_X_L, &gyroData[0], 6); //GYRO = 0x69 , OUT_X_L = 0x28

//function:
bool getWireRegister(uint8_t device, uint8_t reg, uint8_t * receiveData, int amount){
  i2c_cmd_handle_t cmd = i2c_cmd_link_create();                           //create command to tell device what to send and read the registers
  i2c_master_start(cmd);                                                  //startbit
  i2c_master_write_byte(cmd, (device << 1) | I2C_MASTER_WRITE, 0x1);      //send device address and tell it that the master is writing , ack check is enabled
  i2c_master_write_byte(cmd, reg, 0x1);                                    //tell it what register to send , ack check is enabled
  i2c_master_start(cmd);                                                  //repeated startbit to tell it to send stuf to the master
  i2c_master_write_byte(cmd, (device << 1) | I2C_MASTER_READ, 0x1);       //send device address and tell it that the master is reading and it needs to write , ack check is enabled
  if(amount > 1){
    i2c_master_read(cmd, receiveData, amount-1, I2C_MASTER_ACK);          //give the device space to write
  }
  i2c_master_read(cmd, receiveData+amount-1, 1, I2C_MASTER_LAST_NACK);    //give the device space for its last bit and tell the device it is done writing
  i2c_master_stop(cmd);                                                   //stopbit

  i2c_master_cmd_begin(I2C_NUM_0, cmd, 10000);                            //send first I2C message

  i2c_cmd_link_delete(cmd);                                               //delete first message

  return true;
}
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Read 5.1.1 and Table 17 of the sensor datasheet and confirm if you are doing it right! \$\endgroup\$
    – MaNyYaCk
    Commented Nov 25, 2018 at 11:31

1 Answer 1

3
\$\begingroup\$

I found the problem!, I totally missed the part of the datasheet that explains how to send it I2C packets (section 5.1.1 as MaNyYaCk pointed out).

When reading this device, least significant 7 bits you send should be the address of the register you want to read, the most significant bit tells the device if it should increment the register address when reading multiple bytes.

To fix this problem I had to change the following line of code:

i2c_master_write_byte(cmd, reg, 0x1);    //tell it what register to send , ack check is enabled

To:

i2c_master_write_byte(cmd, reg | (0b10000000), 0x1);   //tell it what register to send , enables the increment register address bit , ack check is enabled
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.