9 years, 1 month ago.

Question about I2C with slave and master

Hi,

I´d like to use two LPC1768 called A and B. I´m using A for data-acquisition and B for other things. A has a ticker with frequency 3khz and reads values from adc-channels. I need one from these adc-values in B. The B-ticker runs with frequency 500hz and spi-ports are not availiable. My idea is using I2C with B as master and A as slave.

What happens when slave A is writing data to I2C with 3khz:

// mbed A as slave


I2CSlave slave(p9, p10);

ticker(3khz):
char value[2];
...
slave.write(value, 2);
...

and B as master:

// mbed B as master


I2C i2c(p28, p27);
 

ticker(500hz):
char value[2];
i2c.read(address, data, 2);

...

It´s my wish that mbed B as master reads only the last value from A. Is this possible?

2 Answers

9 years, 1 month ago.

For I2C communication to happen, both slave and master have to participate at the same time. The communication is initiated by the master, and the slave has got to be able to respond when that happens, and it must respond within a limited time.

Support for slave mode is weak in MBED, because it requires the slave to sit in a loop, polling to see if it has been addressed by a master. There is example code for this in the documentation for the I2CSlave class. For your application, it looks like an interrupt-driven solution for the slave would be better, because it would make it easier for the slave to have a main task that was getting on with other work. I have not seen any code that supports that, I'm sorry to say.

8 years, 11 months ago.

This is a pretty old question, but if anyone is still interested I'll add some thoughts.

Like Mike said mbed doesn't currently support and interrupt-driven I2CSlave class. But because you're using the LPC1768, there is library that Erik Olieman has written called MODI2C that ads this functionality for this platform. You can find it here: https://developer.mbed.org/users/Sissors/code/MODI2C/

For a more general solution that works on any platform, you could write your own class that supports interrupts, though this would require looking through the Reference Manual and sample code.

An alternative solution would be to connect a GPIO pin between the master and slave. Whenever you wish to read from the slave, just pull the pin high from the master and attach an interrupt for the slave on that pin. Then you can minimize the I2C polling on the slave.

Hope this helps!

Brian