I’m trying to setup communication between an STM32F103RET6 and a MPU-9150 over I2C. After some tweaking I have got the write request to work but I have problem with read requests. I can see on the logic analyzer that it clock out 8 extra clock cycles, don’t set the stop signal and leave SDA low. In the sequence below I try to read 6 bytes and then stop.
This is my first I2C project so I’m not sure what to look for. Anyone recognizing this behavior and can give some ideas about possible errors to look for?
http://www.guzzzt.com/stuff/images/ledc … roblem.pngMy program looks something like this:
....
while (--cnt>0) // while more than one byte to read
{
*(buffer++) = I2C_read_ack(Open_I2C); // read next databyte from I2C device
}
*(buffer) = I2C_read_nack(Open_I2C); // read last databyte from I2C device
}
}
I2C_stop(Open_I2C); // stop the transmission
....
static int I2C_read_ack(I2C_TypeDef* I2Cx)
{
// enable acknowledge of received data
I2C_AcknowledgeConfig(I2Cx, ENABLE);
// wait until one byte has been received
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED))
{
}
// read data from I2C data register and return data byte
return I2C_ReceiveData(I2Cx);
}
static int I2C_read_nack(I2C_TypeDef* I2Cx)
{
// Disable acknowledge of received data
I2C_AcknowledgeConfig(I2Cx, DISABLE);
// wait until one byte has been received
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED))
{
}
// read data from I2C data register and return data byte
return I2C_ReceiveData(I2Cx);
static int I2C_stop(I2C_TypeDef* I2Cx)
{
// Send I2C1 STOP Condition
I2C_GenerateSTOP(I2Cx, ENABLE);
return OK;
}