cancel
Showing results for 
Search instead for 
Did you mean: 

Receiving register address byte with STM32F407 as I2C slave

AJE
Associate II
Posted on June 15, 2016 at 00:58

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 by

case 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
2 REPLIES 2
Posted on June 15, 2016 at 01:24

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 this

case I2C_EVENT_SLAVE_BYTE_RECEIVED:
buf[i++] = I2C_ReceiveData(I2C1); // Accumulate Received Bytes
break;

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
AJE
Associate II
Posted on June 15, 2016 at 11:11

Hi Clive

Thank you for your reply. My code is now working as I expect.

Best wishes

Andrew