2016-06-14 03:58 PM
Hi
I have written a small proeject where I have set up an STM32F407 on a discovery board as an I2C slave, and I'm pleased to say that it is mostly working. The problem I have is that when I send the following three bytes from the master: 0x30 // I2C slave address 0x02 // I2C device register 0xAA // Data I can see with a logic analyser that all three bytes are acknowledged by the slave, but I'm not able to read the register byte in the void I2C1_EV_IRQHandler(). The slave address is caught by case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED:, and the data byte is picked up bycase I2C_EVENT_SLAVE_BYTE_RECEIVED:
buf[0] = I2C_ReceiveData(I2C1);
break;
I'm not able to pick up the register value by a call to I2C_ReceiveData(). When I call I2C_ReceiveData() at
I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED, on the first attempt the value I get is the 0, and on subsequent attempts it is the same value as the data byte. I have tried calling I2C_ReceiveData() twice at
I2C_EVENT_SLAVE_BYTE_RECEIVED
, but I get the same value twice.
I2C_EVENT_SLAVE_BYTE_RECEIVED
is only trapped once per transaction.
What event do I need to trap to be able to read the register byte?
Kind regards
Andrew
2016-06-14 04:24 PM
The receiver only gets one byte at a time, the master is clocking, I'm pretty sure I2C doesn't understand what different bytes in your stream represent
You need to do something like thiscase I2C_EVENT_SLAVE_BYTE_RECEIVED:
buf[i++] = I2C_ReceiveData(I2C1); // Accumulate Received Bytes
break;
2016-06-15 02:11 AM
Hi Clive
Thank you for your reply. My code is now working as I expect. Best wishes Andrew