reading to fast from I2C Bus

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

cross mob
Not applicable
Hello,
I try to access a nfc (near field communication) reader via I2C using a xmc4200 and the xmc peripheral library. As there is very little documentation (or I was not able to find it) for the xmc i2c library part, I've picked the example, that was shipped with the xmc peripheral library. In a small example, I want to see that I'm able to write 10 bytes to a fifo (first in first out) within the nfc reader and after this, reading back the fifo content.

I've learned that I have to use XMC_I2C_CH_MasterReceiveAck() to indicate that I'm going to read data from the bus and acknowledge that data for all, but the first byte. For the last byte I have to send a Nack using XMC_I2C_CH_MasterReceiveNack(). For every byte to be received, I should be able to poll the protocol status register for XMC_I2C_CH_STATUS_FLAG_RECEIVE_INDICATION or XMC_I2C_CH_STATUS_FLAG_ALTERNATIVE_RECEIVE_INDICATION and seeing one of the flags would indicate that a new byte is ready in RBUF to be read:


static void receive( std::uint8_t* buffer, std::size_t buffer_size )
{
for ( ; buffer_size; --buffer_size, ++buffer )
{
if ( buffer_size == 1 )
{
XMC_I2C_CH_MasterReceiveNack(channel);
}
else
{
XMC_I2C_CH_MasterReceiveAck(channel);
}

while( ( XMC_I2C_CH_GetStatusFlag( channel )
& ( XMC_I2C_CH_STATUS_FLAG_RECEIVE_INDICATION | XMC_I2C_CH_STATUS_FLAG_ALTERNATIVE_RECEIVE_INDICATION ) ) == 0U )
{
/* wait for ACK */
}

XMC_I2C_CH_ClearStatusFlag(XMC_I2C1_CH0, XMC_I2C_CH_STATUS_FLAG_RECEIVE_INDICATION |
XMC_I2C_CH_STATUS_FLAG_ALTERNATIVE_RECEIVE_INDICATION);

*buffer = XMC_I2C_CH_GetReceivedData(channel);
}
}


The code above works well, when stepping through it, using a debugger. But if I'm setting a breakpoint behind the loop, I see that some (3) values at the beginning are repeated. If I write for example [ 0xff, 0x00, 0xaa, 1, 2 , 3 , 4 , 5, 6, 7 ] to the fifo, I read back [ 0xff, 0xff, 0xff, 0x00, 0xaa, 1, 2, 3 ...]. By using an oscilloscope, I can tell that the data on the bus was right in such cases. So to me, it looks like, I'm reading to much data from one event and there must be some other flags that must be checked too.

Does someone of you have used this API and knows how to use it? Neither the search function of this forum, nor google reveals any results to a search for "XMC_I2C_CH_GetReceivedData()".

Kind regards,
Torsten
0 Likes
2 Replies
jferreira
Employee
Employee
10 sign-ins 5 sign-ins First like received
Hi,

Could it be you make the following line is wrong?

XMC_I2C_CH_ClearStatusFlag(XMC_I2C1_CH0, XMC_I2C_CH_STATUS_FLAG_RECEIVE_INDICATION |
XMC_I2C_CH_STATUS_FLAG_ALTERNATIVE_RECEIVE_INDICATION);


should not be channel used as first parameter?

Best regards,
Jesus
0 Likes
Not applicable
Hello Jesus,
yes, that was the bug. Thank you very much for!

Cheers,
Torsten
0 Likes