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:
This is the I2C packet seen trough my oscilloscope (picoscope):
Zoomed in on the write part:
Zoomed in on the 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;
}