I2C001 App Help

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Not applicable
Hi, I'm starting to use the I2C001 App to talk to an IMU sensor and I need some help.
This is my code:

// First write the address of the sensor with start bit
I2C001_DataType sData;
sData.Data1.TDF_Type = I2C_TDF_MStart;
sData.Data1.Data = (devAddr | I2C_WRITE);
I2C001_WriteData(i2c,&sData);

//Next write the reggister address that i want to read
sData.Data1.TDF_Type = I2C_TDF_MTxData;
sData.Data1.Data = regAddr;
I2C001_WriteData(i2c,&sData);

//Send stop bit
sData.Data1.TDF_Type =I2C_TDF_MStop;
sData.Data1.Data = (devAddr | I2C_WRITE);
I2C001_WriteData(i2c,&sData);

//And finally read the data sended by the sensor
uint16_t *readData=(uint16_t*)malloc(length);
I2C001_ReadData(i2c,readData);

But this gives an error that is saying that the FIFO is empty when reading. I'm using correctly the App?? And next when I try to write again say that the txFIFO is full. I have to clear the FIFO??

Best Regards,
Andre Morais (uSG)
0 Likes
3 Replies
Not applicable
Hi, you seemed to have omitted the read sequence. This means after the last stop condition, a new start condition followed by the device address + I2C_READ, etc, or the following e.g. from the Apps help document:
#include 

void receive_data_handler(void);

int main(void)
{

DAVE_Init();

// Configure message data length
I2C001_DataType data1,data2,data3,data4,data5;

// Read access IO expander device PCA9502
// Transmission by the master with start condition,
// I2C write condition and slave address
data1.Data1.TDF_Type = I2C_TDF_MStart;
data1.Data1.Data = (0x98 | I2C_WRITE);
I2C001_WriteData(&I2C001_Handle0,&data1);

// Write data to the device
data2.Data1.TDF_Type = I2C_TDF_MTxData;
data2.Data1.Data = 0x58;
I2C001_WriteData(&I2C001_Handle0,&data2);

// Transmit repeated start condition and slave address
data3.Data1.TDF_Type = I2C_TDF_MRStart;
data3.Data1.Data = 0x98 | I2C_READ;
I2C001_WriteData(&I2C001_Handle0,&data3);

// Read one data from slave
data4.Data1.TDF_Type = I2C_TDF_MRxAck1;
data4.Data1.Data = ubyteFF;
I2C001_WriteData(&I2C001_Handle0,&data4);

// Stop condition by the master
data5.Data1.TDF_Type = I2C_TDF_MStop;
data5.Data1.Data = ubyteFF;
I2C001_WriteData(&I2C001_Handle0,&data5);

while(1)
{}
return 0;
}

// Event Handler( FIFO receive interrupt event) registered with NVIC002 App
void receive_data_handler(void)
{
uint16_t DataReceive1 = 0x0000;
// Read receive buffer, put the data in DataReceive1
I2C001_ReadData(&I2C001_Handle0,&DataReceive1);


Therefore, the empty RxFIFO is expected but TxFIFO full is unexpected. Are you using a TxFIFO size of 4 or more?
0 Likes
Not applicable
Hi T.O.M. thank you for the response. I changed the code a little bit but still i can't communicate... I'm using an MPU6050.

The new code:

sData.Data1.TDF_Type = I2C_TDF_MStart;
sData.Data1.Data = (devAddr | I2C_WRITE);
I2C001_WriteData(i2c,&sData);

sData.Data1.TDF_Type = I2C_TDF_MTxData;
sData.Data1.Data = regAddr;
I2C001_WriteData(i2c,&sData);

sData.Data1.TDF_Type =I2C_TDF_MStart;
sData.Data1.Data = (devAddr | I2C_READ);
I2C001_WriteData(i2c,&sData);

sData.Data1.TDF_Type=I2C_TDF_MRxAck1;
I2C001_WriteData(i2c,&sData);

uint16_t *readData=(uint16_t*)malloc(length);

uint8_t i=0;
while(I2C001_GetFlagStatus(i2c,I2C001_FLAG_NACK_RECEIVED)==I2C001_RESET){

I2C001_ReadData(i2c,&readData);
i++;
if(i>length)
break;
}

sData.Data1.TDF_Type = I2C_TDF_MStop;
sData.Data1.Data = ubyteFF;
I2C001_WriteData(i2c,&sData);

Best Regards,
André Morais
0 Likes
Not applicable
Hi André,

The TDF_Type of restart condition should be I2C_TDF_MRStart but even with this, you should be able to generate at least the start condition and the slave addresses. Do you see these on the SCL and SDA lines with an oscilloscope?

Regards,
T.O.M.
0 Likes