I2C bus

19 Apr 2010

I have several modules that talk over an I2C bus (Accelerometer, Compass, ADC). The fun part is that the compass uses 100KHz and everything else is capable of 400KHz. I got all of these modules talking with the mbed with small test code and now I want to integrate them without it looking like a pile of nasty code (INS code will look bad enough without communication code mucking it up).

 

Sooo, I thought I would tinker with classes and include files for the first time. I'm hoping to split each module into a different class and header file. So the question is can I declare three I2C objects each using different frequencies but the same pins? In other words, I'm hoping that under the Compass class I can have

Compass.setup(p9,p10) which would have it's own i2c(p9,p10) and also call i2c.frequency(100000)

Then have an acceleromater class with the same thing but 400KHz.

 

Can I do this, or do I need to pass the I2C bus to the class then tinker with the frequency before each read/write?

19 Apr 2010

Hi Josh,

This is a really good question, and I hope I have a really good answer for you.

It should *just work* :)

The I2C and SPI classes actually have early support for what I think of as "interface virtalisation"; i.e. each class instance knows about it's own setup, and when they perform the transaction it ensures it is setup appropriately. This means if you have two I2C instances tied to the same physical pins (interface), but set them up differently, they should get reconfigured as and when needed.

It is early/unofficial support only but I believe should be working.

Please tell me how you get on or if you have questions,

Simon

20 Apr 2010

It works. Thanks for the help.