This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Read multiple data bytes in one I2C sequence.

Hi!

I'm trying to implement a I2C connection from a TM4C123 board to an Adafruit BNO055. I have success in reading one byte per sequence but unfortunately, the chip requires reading several bytes in one sequence to avoid data inconsistency. I'm trying first to read 2 bytes MSB and LSB with the following code:

uint16_t I2CStretchedReceive(uint32_t I2C_base, uint8_t slave_addr, uint8_t sub_addr)
{
// returns LSB and MSB in a 16-bit word uint8_t data[2]; uint16_t axis_value; //specify that we are writing (a register address) to the slave device I2CMasterSlaveAddrSet(I2C_base, slave_addr, false); //specify register to be read I2CMasterDataPut(I2C_base, sub_addr); //send control byte and register address byte to slave device I2CMasterControl(I2C_base, I2C_MASTER_CMD_BURST_SEND_START); //wait for MCU to finish transaction while(I2CMasterBusy(I2C_base)); //specify that we are going to read from slave device I2CMasterSlaveAddrSet(I2C_base, slave_addr, true); //send control byte and read from the register we //specified I2CMasterControl(I2C_base, I2C_MASTER_CMD_BURST_RECEIVE_START); //wait for MCU to finish transaction while(I2CMasterBusy(I2C_base)); //return 1st byte pulled from the specified register data[0] = (uint8_t)I2CMasterDataGet(I2C_base); I2CMasterControl(I2C_base, I2C_MASTER_CMD_BURST_RECEIVE_FINISH); //wait for MCU to finish transaction while(I2CMasterBusy(I2C_base)); //return 2th byte pulled from the specified register data[1] = I2CMasterDataGet(I2C_base); axis_value = (data[1] << 8)| data[0]; return axis_value; }

Then I write uint16_t axis_data = I2CStretchedReceive(I2C3_BASE, BNO055_ADDRESS, GYRO_DATA_X_LSB) but it does not work because I2CMasterDataGet() only returns 0x00 to data[i].

The I2C initialization is as followed:

void iic_init(void)
{
	//enable I2C module 3
	SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3);

	//reset module
	SysCtlPeripheralReset(SYSCTL_PERIPH_I2C3);

	//enable GPIO peripheral that contain I2C3
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);

	// Configure the pin muxing for I2C3 function on port D0 and D1.
	GPIOPinConfigure(GPIO_PD0_I2C3SCL);
	GPIOPinConfigure(GPIO_PD1_I2C3SDA);

	// Select the I2C function for these pins.
	GPIOPinTypeI2CSCL(GPIO_PORTD_BASE, GPIO_PIN_0);
	GPIOPinTypeI2C(GPIO_PORTD_BASE, GPIO_PIN_1);

	// Set 100 kbps transfer rate
	I2CMasterInitExpClk(I2C3_BASE, SysCtlClockGet(), false);
        // Enable clock stretching
	I2CMasterTimeoutSet(I2C3_BASE, 0xFF);
	// I2CMasterEnable(I2C3_BASE);
	while(I2CMasterBusy(I2C3_BASE));
}

Does anyone have any idea of what I'm doing wrong? Thank you very much for your time!